Code

utils llist: Let llist_get() increment the reference count.
[sysdb.git] / t / frontend / parser_test.c
index 589553398bd9058ac50fe8d3e342e30a9c8ad00c..acd8ee69f079c5571be0b4786ef8ec9c647436fe 100644 (file)
@@ -26,6 +26,8 @@
  */
 
 #include "frontend/connection.h"
+#include "frontend/parser.h"
+#include "core/object.h"
 #include "libsysdb_test.h"
 
 #include <check.h>
@@ -38,25 +40,29 @@ START_TEST(test_parse)
 {
        struct {
                const char *query;
+               int len;
                int expected;
        } golden_data[] = {
                /* empty commands */
-               { NULL,                 -1 },
-               { "",                    0 },
-               { ";",                   0 },
-               { ";;",                  0 },
+               { NULL,                 -1, -1 },
+               { "",                   -1,  0 },
+               { ";",                  -1,  0 },
+               { ";;",                 -1,  0 },
 
                /* valid commands */
-               { "LIST",                1 },
-               { "LIST;",               1 },
+               { "LIST",               -1,  1 },
+               { "LIST -- comment",    -1,  1 },
+               { "LIST;",              -1,  1 },
+               { "LIST; INVALID",       5,  1 },
 
                /* comments */
-               { "/* some comment */",  0 },
-               { "-- another comment",  0 },
+               { "/* some comment */", -1,  0 },
+               { "-- another comment", -1,  0 },
 
                /* syntax errors */
-               { "INVALID",            -1 },
-               { "/* some incomplete", -1 },
+               { "INVALID",            -1, -1 },
+               { "LIST; INVALID",       8, -1 },
+               { "/* some incomplete", -1, -1 },
        };
 
        size_t i;
@@ -65,7 +71,7 @@ START_TEST(test_parse)
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
                _Bool ok;
 
-               check = sdb_fe_parse(golden_data[i].query);
+               check = sdb_fe_parse(golden_data[i].query, golden_data[i].len);
                if (golden_data[i].expected < 0)
                        ok = check == 0;
                else
@@ -85,6 +91,7 @@ START_TEST(test_parse)
                                        "sdb_fe_parse(LIST)->cmd = %i; expected: %d "
                                        "(CONNECTION_LIST)", SDB_CONN_NODE(obj)->cmd,
                                        CONNECTION_LIST);
+                       sdb_object_deref(obj);
                }
 
                sdb_llist_destroy(check);
@@ -92,6 +99,48 @@ START_TEST(test_parse)
 }
 END_TEST
 
+START_TEST(test_parse_matcher)
+{
+       struct {
+               const char *expr;
+               int len;
+               int expected;
+       } golden_data[] = {
+               /* empty expressions */
+               { NULL,                 -1, -1 },
+               { "",                   -1, -1 },
+
+               /* valid expressions */
+               { "localhost",          -1,  0 },
+               { "localhost -- foo",   -1,  0 },
+               { "localhost <garbage>", 9,  0 },
+
+               /* syntax errors */
+               { "LIST",               -1, -1 },
+               { "foo &^ bar",         -1, -1 },
+       };
+
+       size_t i;
+       sdb_store_matcher_t *m;
+
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               _Bool ok;
+
+               m = sdb_fe_parse_matcher(golden_data[i].expr, golden_data[i].len);
+               if (golden_data[i].expected < 0)
+                       ok = m == NULL;
+               else
+                       ok = m != NULL;
+
+               fail_unless(ok, "sdb_fe_parse_matcher(%s) = %p; expected: %s",
+                               golden_data[i].expr, m, (golden_data[i].expected < 0)
+                               ? "NULL" : "<matcher>");
+
+               sdb_object_deref(SDB_OBJ(m));
+       }
+}
+END_TEST
+
 Suite *
 fe_parser_suite(void)
 {
@@ -100,6 +149,7 @@ fe_parser_suite(void)
 
        tc = tcase_create("core");
        tcase_add_test(tc, test_parse);
+       tcase_add_test(tc, test_parse_matcher);
        suite_add_tcase(s, tc);
 
        return s;