2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <ecoli_log.h>
34 #include <ecoli_malloc.h>
35 #include <ecoli_test.h>
36 #include <ecoli_strvec.h>
37 #include <ecoli_node.h>
38 #include <ecoli_parsed.h>
39 #include <ecoli_completed.h>
40 #include <ecoli_parsed.h>
42 static struct ec_test_list test_list = TAILQ_HEAD_INITIALIZER(test_list);
44 /* register a driver */
45 void ec_test_register(struct ec_test *test)
47 TAILQ_INSERT_TAIL(&test_list, test, next);
48 // XXX check if already exist, like for type
51 int ec_test_check_parse(struct ec_node *tk, int expected, ...)
54 struct ec_strvec *vec = NULL;
59 va_start(ap, expected);
61 /* build a string vector */
66 for (s = va_arg(ap, const char *);
68 s = va_arg(ap, const char *)) {
72 if (ec_strvec_add(vec, s) < 0)
76 p = ec_node_parse_strvec(tk, vec);
77 /* XXX only for debug */
78 ec_parsed_dump(stdout, p);
80 ec_log(EC_LOG_ERR, "parsed is NULL\n");
82 if (ec_parsed_matches(p))
83 match = ec_parsed_len(p);
86 if (expected == match) {
90 "tk parsed len (%d) does not match expected (%d)\n",
102 int ec_test_check_complete(struct ec_node *tk, ...)
104 struct ec_completed *c = NULL;
105 struct ec_completed_elt *elt;
106 struct ec_strvec *vec = NULL;
107 const char *s, *expected;
109 unsigned int count = 0;
114 /* build a string vector */
119 for (s = va_arg(ap, const char *);
120 s != EC_NODE_ENDLIST;
121 s = va_arg(ap, const char *)) {
125 if (ec_strvec_add(vec, s) < 0)
129 c = ec_node_complete_strvec(tk, vec);
135 for (s = va_arg(ap, const char *);
136 s != EC_NODE_ENDLIST;
137 s = va_arg(ap, const char *)) {
144 TAILQ_FOREACH(elt, &c->elts, next) {
145 /* only check matching completions */
146 if (elt->add != NULL && strcmp(elt->add, s) == 0)
152 "completion <%s> not in list\n", s);
157 if (count != ec_completed_count(c, EC_MATCH)) {
159 "nb_completion (%d) does not match (%d)\n",
160 count, ec_completed_count(c, EC_MATCH));
161 ec_completed_dump(stdout, c);
166 expected = va_arg(ap, const char *);
167 s = ec_completed_smallest_start(c);
168 if (strcmp(s, expected)) {
171 "should complete with <%s> but completes with <%s>\n",
177 ec_completed_free(c);
182 static int launch_test(const char *name)
184 struct ec_test *test;
186 unsigned int count = 0;
188 TAILQ_FOREACH(test, &test_list, next) {
189 if (name != NULL && strcmp(name, test->name))
192 ec_log(EC_LOG_INFO, "== starting test %-20s\n", test->name);
195 if (test->test() == 0) {
196 ec_log(EC_LOG_INFO, "== test %-20s success\n",
199 ec_log(EC_LOG_INFO, "== test %-20s failed\n",
205 if (name != NULL && count == 0)
206 ec_log(EC_LOG_WARNING, "== test %s not found\n", name);
211 int ec_test_all(void)
213 return launch_test(NULL);
216 int ec_test_one(const char *name)
218 return launch_test(name);