1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
11 #include <ecoli_log.h>
12 #include <ecoli_malloc.h>
13 #include <ecoli_test.h>
14 #include <ecoli_strvec.h>
15 #include <ecoli_node.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_parse.h>
20 static struct ec_test_list test_list = TAILQ_HEAD_INITIALIZER(test_list);
22 EC_LOG_TYPE_REGISTER(test);
24 static struct ec_test *ec_test_lookup(const char *name)
28 TAILQ_FOREACH(test, &test_list, next) {
29 if (!strcmp(name, test->name))
37 int ec_test_register(struct ec_test *test)
39 if (ec_test_lookup(test->name) != NULL)
42 TAILQ_INSERT_TAIL(&test_list, test, next);
47 int ec_test_check_parse(struct ec_node *tk, int expected, ...)
50 struct ec_strvec *vec = NULL;
55 va_start(ap, expected);
57 /* build a string vector */
62 for (s = va_arg(ap, const char *);
64 s = va_arg(ap, const char *)) {
68 if (ec_strvec_add(vec, s) < 0)
72 p = ec_node_parse_strvec(tk, vec);
74 EC_LOG(EC_LOG_ERR, "parse is NULL\n");
76 if (ec_parse_matches(p))
77 match = ec_parse_len(p);
80 if (expected == match) {
84 "parse len (%d) does not match expected (%d)\n",
96 int ec_test_check_complete(struct ec_node *tk, enum ec_comp_type type, ...)
98 struct ec_comp *c = NULL;
99 struct ec_strvec *vec = NULL;
102 unsigned int count = 0;
107 /* build a string vector */
112 for (s = va_arg(ap, const char *);
113 s != EC_NODE_ENDLIST;
114 s = va_arg(ap, const char *)) {
118 if (ec_strvec_add(vec, s) < 0)
122 c = ec_node_complete_strvec(tk, vec);
128 /* for each expected completion, check it is there */
129 for (s = va_arg(ap, const char *);
130 s != EC_NODE_ENDLIST;
131 s = va_arg(ap, const char *)) {
132 struct ec_comp_iter *iter;
133 const struct ec_comp_item *item;
142 /* only check matching completions */
143 iter = ec_comp_iter(c, type);
144 while ((item = ec_comp_iter_next(iter)) != NULL) {
145 const char *str = ec_comp_item_get_str(item);
146 if (str != NULL && strcmp(str, s) == 0)
152 "completion <%s> not in list\n", s);
155 ec_comp_iter_free(iter);
158 /* check if we have more completions (or less) than expected */
159 if (count != ec_comp_count(c, type)) {
161 "nb_completion (%d) does not match (%d)\n",
162 count, ec_comp_count(c, type));
163 ec_comp_dump(stdout, c);
174 static int launch_test(const char *name)
176 struct ec_test *test;
178 unsigned int count = 0;
180 TAILQ_FOREACH(test, &test_list, next) {
181 if (name != NULL && strcmp(name, test->name))
184 EC_LOG(EC_LOG_INFO, "== starting test %-20s\n",
188 if (test->test() == 0) {
190 "== test %-20s success\n",
194 "== test %-20s failed\n",
200 if (name != NULL && count == 0) {
201 EC_LOG(EC_LOG_WARNING,
202 "== test %s not found\n", name);
209 int ec_test_all(void)
211 return launch_test(NULL);
214 int ec_test_one(const char *name)
216 return launch_test(name);