save
[protos/libecoli.git] / lib / ecoli_test.c
index 161c52c..3cce2da 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
 
+#include <ecoli_log.h>
+#include <ecoli_malloc.h>
 #include <ecoli_test.h>
 #include <ecoli_tk.h>
 
@@ -56,9 +59,9 @@ int ec_test_check_tk_parse(const struct ec_tk *tk, const char *input,
                ret = 0;
 
        if (expected == NULL && ret != 0)
-               printf("tk should not match but matches <%s>\n", s);
+               ec_log(EC_LOG_ERR, "tk should not match but matches <%s>\n", s);
        if (expected != NULL && ret != 0)
-               printf("tk should match <%s> but matches <%s>\n",
+               ec_log(EC_LOG_ERR, "tk should match <%s> but matches <%s>\n",
                        expected, s);
 
        ec_parsed_tk_free(p);
@@ -69,44 +72,90 @@ int ec_test_check_tk_parse(const struct ec_tk *tk, const char *input,
 int ec_test_check_tk_complete(const struct ec_tk *tk, const char *input,
        const char *expected)
 {
-       struct ec_completed_tk *p;
+       struct ec_completed_tk *c;
        const char *s;
        int ret = -1;
 
-       p = ec_tk_complete(tk, input);
-       s = ec_completed_tk_smallest_start(p);
-       if (s == NULL && expected == NULL)
-               ret = 0;
-       else if (s != NULL && expected != NULL &&
-               !strcmp(s, expected))
+       assert(expected != NULL);
+
+       c = ec_tk_complete(tk, input);
+       s = ec_completed_tk_smallest_start(c);
+       if (!strcmp(s, expected))
                ret = 0;
 
-       if (expected == NULL && ret != 0)
-               printf("tk should not complete but completes with <%s>\n", s);
-       if (expected != NULL && ret != 0)
-               printf("tk should complete with <%s> but completes with <%s>\n",
+       if (ret != 0)
+               ec_log(EC_LOG_ERR,
+                       "should complete with <%s> but completes with <%s>\n",
                        expected, s);
 
-       ec_completed_tk_free(p);
+       ec_completed_tk_free(c);
 
        return ret;
 }
 
-/* int ec_test_check_tk_complete_list(const struct ec_tk *tk, */
-/*     const char *input, ...) */
+int ec_test_check_tk_complete_list(const struct ec_tk *tk,
+       const char *input, ...)
+{
+       struct ec_completed_tk *c = NULL;
+       struct ec_completed_tk_elt *elt;
+       const char *s;
+       int ret = -1;
+       unsigned int count = 0;
+       va_list ap;
+
+       va_start(ap, input);
+
+       c = ec_tk_complete(tk, input);
+       if (c == NULL)
+               goto out;
+
+       for (s = va_arg(ap, const char *);
+            s != EC_TK_ENDLIST;
+            s = va_arg(ap, const char *)) {
+               if (s == NULL)
+                       goto out;
+
+               count++;
+               TAILQ_FOREACH(elt, &c->elts, next) {
+                       if (strcmp(elt->add, s) == 0)
+                               break;
+               }
+
+               if (elt == NULL) {
+                       ec_log(EC_LOG_ERR,
+                               "completion <%s> not in list\n", s);
+                       goto out;
+               }
+       }
+
+       if (count != ec_completed_tk_count(c)) {
+               ec_log(EC_LOG_ERR,
+                       "nb_completion (%d) does not match (%d)\n",
+                       count, ec_completed_tk_count(c));
+               ec_completed_tk_dump(stdout, c);
+               goto out;
+       }
+
+       ret = 0;
+
+out:
+       ec_completed_tk_free(c);
+       va_end(ap);
+       return ret;
+}
 
 int ec_test_all(void)
 {
        struct ec_test *test;
        int ret = 0;
 
-       // XXX register a new malloc to trac memleaks
-
        TAILQ_FOREACH(test, &test_list, next) {
                if (test->test() == 0) {
-                       printf("== test %-20s success\n", test->name);
+                       ec_log(EC_LOG_INFO, "== test %-20s success\n",
+                               test->name);
                } else {
-                       printf("== test %-20s failed\n", test->name);
+                       ec_log(EC_LOG_INFO, "== test %-20s failed\n",
+                               test->name);
                        ret = -1;
                }
        }