test coverage
authorOlivier Matz <zer0@droids-corp.org>
Thu, 15 Mar 2018 21:56:48 +0000 (22:56 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Thu, 15 Mar 2018 21:56:48 +0000 (22:56 +0100)
lib/ecoli_complete.c
lib/ecoli_complete.h
lib/ecoli_node.c
lib/ecoli_parse.c

index 14012ad..cbe51b2 100644 (file)
 #include <ecoli_strvec.h>
 #include <ecoli_keyval.h>
 #include <ecoli_log.h>
+#include <ecoli_test.h>
 #include <ecoli_node.h>
 #include <ecoli_parse.h>
+#include <ecoli_node_sh_lex.h>
+#include <ecoli_node_str.h>
+#include <ecoli_node_or.h>
 #include <ecoli_complete.h>
 
+EC_LOG_TYPE_REGISTER(comp);
+
 struct ec_comp_item {
        TAILQ_ENTRY(ec_comp_item) next;
        enum ec_comp_type type;
-       const struct ec_node *node;
        struct ec_comp_group *grp;
        char *start;      /* the initial token */
        char *full;       /* the full token after completion */
@@ -181,8 +186,8 @@ fail:
 }
 
 static struct ec_comp_item *
-ec_comp_item(const struct ec_node *node, enum ec_comp_type type,
-               const char *start, const char *full)
+ec_comp_item(enum ec_comp_type type,
+       const char *start, const char *full)
 {
        struct ec_comp_item *item = NULL;
        struct ec_keyval *attrs = NULL;
@@ -226,7 +231,6 @@ ec_comp_item(const struct ec_node *node, enum ec_comp_type type,
                        goto fail;
        }
 
-       item->node = node;
        item->type = type;
        item->start = start_cp;
        item->full = full_cp;
@@ -324,10 +328,10 @@ fail:
 }
 
 static int
-ec_comp_item_add(struct ec_comp *comp,
+ec_comp_item_add(struct ec_comp *comp, const struct ec_node *node,
                struct ec_comp_item *item)
 {
-       if (comp == NULL || item == NULL || item->node == NULL)
+       if (comp == NULL || item == NULL)
                return -EINVAL;
 
        switch (item->type) {
@@ -347,7 +351,7 @@ ec_comp_item_add(struct ec_comp *comp,
        if (comp->cur_group == NULL) {
                struct ec_comp_group *grp;
 
-               grp = ec_comp_group(item->node, comp->cur_state);
+               grp = ec_comp_group(node, comp->cur_state);
                if (grp == NULL)
                        return -ENOMEM;
                TAILQ_INSERT_TAIL(&comp->groups, grp, next);
@@ -385,18 +389,18 @@ ec_comp_item_get_type(const struct ec_comp_item *item)
        return item->type;
 }
 
-const struct ec_node *
-ec_comp_item_get_node(const struct ec_comp_item *item)
-{
-       return item->node;
-}
-
 const struct ec_comp_group *
 ec_comp_item_get_grp(const struct ec_comp_item *item)
 {
        return item->grp;
 }
 
+const struct ec_node *
+ec_comp_item_get_node(const struct ec_comp_item *item)
+{
+       return ec_comp_item_get_grp(item)->node;
+}
+
 static void
 ec_comp_item_free(struct ec_comp_item *item)
 {
@@ -420,11 +424,11 @@ int ec_comp_add_item(struct ec_comp *comp,
        struct ec_comp_item *item = NULL;
        int ret;
 
-       item = ec_comp_item(node, type, start, full);
+       item = ec_comp_item(type, start, full);
        if (item == NULL)
                return -1;
 
-       ret = ec_comp_item_add(comp, item);
+       ret = ec_comp_item_add(comp, node, item);
        if (ret < 0)
                goto fail;
 
@@ -634,3 +638,108 @@ void ec_comp_iter_free(struct ec_comp_iter *iter)
 {
        ec_free(iter);
 }
+
+/* LCOV_EXCL_START */
+static int ec_comp_testcase(void)
+{
+       struct ec_node *node = NULL;
+       struct ec_comp *c = NULL;
+       struct ec_comp_iter *iter = NULL;
+       struct ec_comp_item *item;
+       FILE *f = NULL;
+       char *buf = NULL;
+       size_t buflen = 0;
+       int testres = 0;
+
+       node = ec_node_sh_lex(EC_NO_ID,
+                       EC_NODE_OR(EC_NO_ID,
+                               ec_node_str("id_x", "xx"),
+                               ec_node_str("id_y", "yy")));
+       if (node == NULL)
+               goto fail;
+
+       c = ec_node_complete(node, "xcdscds");
+       testres |= EC_TEST_CHECK(
+               c != NULL && ec_comp_count(c, EC_COMP_ALL) == 0,
+               "complete count should is not 0\n");
+       ec_comp_free(c);
+
+       c = ec_node_complete(node, "x");
+       testres |= EC_TEST_CHECK(
+               c != NULL && ec_comp_count(c, EC_COMP_ALL) == 1,
+               "complete count should is not 1\n");
+       ec_comp_free(c);
+
+       c = ec_node_complete(node, "");
+       testres |= EC_TEST_CHECK(
+               c != NULL && ec_comp_count(c, EC_COMP_ALL) == 2,
+               "complete count should is not 2\n");
+
+       f = open_memstream(&buf, &buflen);
+       if (f == NULL)
+               goto fail;
+       ec_comp_dump(f, c);
+       fclose(f);
+       f = NULL;
+
+       /* testres |= EC_TEST_CHECK( */
+       /*      strstr(buf, "no match"), "bad dump\n"); */
+       free(buf);
+       buf = NULL;
+
+       iter = ec_comp_iter(c, EC_COMP_ALL);
+       item = ec_comp_iter_next(iter);
+       if (item == NULL)
+               goto fail;
+
+       testres |= EC_TEST_CHECK(
+               !strcmp(ec_comp_item_get_display(item), "xx"),
+               "bad item display\n");
+       testres |= EC_TEST_CHECK(
+               ec_comp_item_get_type(item) == EC_COMP_FULL,
+               "bad item type\n");
+       testres |= EC_TEST_CHECK(
+               !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_x"),
+               "bad item node\n");
+
+       item = ec_comp_iter_next(iter);
+       if (item == NULL)
+               goto fail;
+
+       testres |= EC_TEST_CHECK(
+               !strcmp(ec_comp_item_get_display(item), "yy"),
+               "bad item display\n");
+       testres |= EC_TEST_CHECK(
+               ec_comp_item_get_type(item) == EC_COMP_FULL,
+               "bad item type\n");
+       testres |= EC_TEST_CHECK(
+               !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_y"),
+               "bad item node\n");
+
+       item = ec_comp_iter_next(iter);
+       testres |= EC_TEST_CHECK(item == NULL, "should be the last item\n");
+
+       ec_comp_iter_free(iter);
+       ec_comp_free(c);
+       ec_node_free(node);
+
+       return testres;
+
+fail:
+       ec_comp_iter_free(iter);
+       ec_comp_free(c);
+       ec_node_free(node);
+       if (f != NULL)
+               fclose(f);
+       free(buf);
+
+       return -1;
+}
+/* LCOV_EXCL_STOP */
+
+static struct ec_test ec_comp_test = {
+       .name = "comp",
+       .test = ec_comp_testcase,
+};
+
+EC_TEST_REGISTER(ec_comp_test);
index 14d4b85..1ed67f0 100644 (file)
@@ -20,7 +20,7 @@
 
 struct ec_node;
 
-enum ec_comp_type {
+enum ec_comp_type { /* XXX should be a define */
        EC_COMP_UNKNOWN = 0x1,
        EC_COMP_FULL = 0x2,
        EC_COMP_PARTIAL = 0x4,
index 20f90b3..4360336 100644 (file)
@@ -330,6 +330,7 @@ static int ec_node_testcase(void)
                        "type=str id=id_y"),
                "bad dump\n");
        free(buf);
+       buf = NULL;
 
        testres |= EC_TEST_CHECK(
                !strcmp(ec_node_type(node)->name, "seq") &&
@@ -390,6 +391,7 @@ fail:
        ec_node_free(node);
        if (f != NULL)
                fclose(f);
+       free(buf);
 
        return -1;
 }
index 7a94ef8..a6def04 100644 (file)
@@ -458,6 +458,7 @@ static int ec_parse_testcase(void)
        testres |= EC_TEST_CHECK(
                strstr(buf, "no match"), "bad dump\n");
        free(buf);
+       buf = NULL;
        ec_parse_free(p);
 
        p = ec_node_parse(node, "x y");
@@ -505,6 +506,7 @@ static int ec_parse_testcase(void)
                strstr(buf, "type=str id=id_x"),
                "bad dump\n");
        free(buf);
+       buf = NULL;
 
        ec_parse_free(p);
        ec_node_free(node);
@@ -516,6 +518,7 @@ fail:
        ec_node_free(node);
        if (f != NULL)
                fclose(f);
+       free(buf);
 
        return -1;
 }