Code

frontend: Updated unit-tests for expression related parsing.
[sysdb.git] / t / frontend / parser_test.c
index 589553398bd9058ac50fe8d3e342e30a9c8ad00c..4b177328c084483edc1c74d7e167cf89533a8db3 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,34 +40,49 @@ START_TEST(test_parse)
 {
        struct {
                const char *query;
+               int len;
                int expected;
+               sdb_conn_state_t expected_cmd;
        } golden_data[] = {
                /* empty commands */
-               { NULL,                 -1 },
-               { "",                    0 },
-               { ";",                   0 },
-               { ";;",                  0 },
+               { NULL,                 -1, -1, 0 },
+               { "",                   -1,  0, 0 },
+               { ";",                  -1,  0, 0 },
+               { ";;",                 -1,  0, 0 },
 
                /* valid commands */
-               { "LIST",                1 },
-               { "LIST;",               1 },
+               { "FETCH 'host'",       -1,  1, CONNECTION_FETCH  },
+               { "LIST",               -1,  1, CONNECTION_LIST   },
+               { "LIST -- comment",    -1,  1, CONNECTION_LIST   },
+               { "LIST;",              -1,  1, CONNECTION_LIST   },
+               { "LIST; INVALID",       5,  1, CONNECTION_LIST   },
+
+               { "LOOKUP hosts WHERE "
+                 "host.name = 'host'", -1,  1, CONNECTION_LOOKUP },
 
                /* comments */
-               { "/* some comment */",  0 },
-               { "-- another comment",  0 },
+               { "/* some comment */", -1,  0, 0 },
+               { "-- another comment", -1,  0, 0 },
 
                /* syntax errors */
-               { "INVALID",            -1 },
-               { "/* some incomplete", -1 },
+               { "INVALID",            -1, -1, 0 },
+               { "FETCH host",         -1, -1, 0 },
+               { "LIST; INVALID",       8, -1, 0 },
+               { "/* some incomplete", -1, -1, 0 },
+
+               { "LOOKUP hosts",       -1, -1, 0 },
+               { "LOOKUP foo WHERE "
+                 "host.name = 'host'", -1, -1, 0 },
        };
 
        size_t i;
        sdb_llist_t *check;
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               sdb_object_t *obj;
                _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
@@ -78,20 +95,65 @@ START_TEST(test_parse)
                if (! check)
                        continue;
 
-               if ((! strcmp(golden_data[i].query, "LIST"))
-                               || (! strcmp(golden_data[i].query, "LIST;"))) {
-                       sdb_object_t *obj = sdb_llist_get(check, 0);
-                       fail_unless(SDB_CONN_NODE(obj)->cmd == CONNECTION_LIST,
-                                       "sdb_fe_parse(LIST)->cmd = %i; expected: %d "
-                                       "(CONNECTION_LIST)", SDB_CONN_NODE(obj)->cmd,
-                                       CONNECTION_LIST);
+               if ((! golden_data[i].expected_cmd)
+                               || (golden_data[i].expected <= 0)) {
+                       sdb_llist_destroy(check);
+                       continue;
                }
 
+               obj = sdb_llist_get(check, 0);
+               fail_unless(SDB_CONN_NODE(obj)->cmd == golden_data[i].expected_cmd,
+                               "sdb_fe_parse(%s)->cmd = %i; expected: %d",
+                               golden_data[i].query, SDB_CONN_NODE(obj)->cmd,
+                               golden_data[i].expected_cmd);
+               sdb_object_deref(obj);
                sdb_llist_destroy(check);
        }
 }
 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 */
+               { "host.name = 'localhost'",        -1,  0 },
+               { "host.name = 'localhost' -- foo", -1,  0 },
+               { "host.name = 'host' <garbage>",   18,  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 +162,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;