fd9e32fa3ee19ee7e99edf7b1e0dc3fc49be1538
1 /*
2 * SysDB - t/unit/frontend/parser_test.c
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "frontend/connection.h"
29 #include "frontend/parser.h"
30 #include "core/store-private.h"
31 #include "core/object.h"
32 #include "libsysdb_test.h"
34 #include <check.h>
35 #include <limits.h>
37 /*
38 * tests
39 */
41 START_TEST(test_parse)
42 {
43 struct {
44 const char *query;
45 int len;
46 int expected;
47 sdb_conn_state_t expected_cmd;
48 } golden_data[] = {
49 /* empty commands */
50 { NULL, -1, -1, 0 },
51 { "", -1, 0, 0 },
52 { ";", -1, 0, 0 },
53 { ";;", -1, 0, 0 },
55 /* valid commands */
56 { "FETCH host 'host'", -1, 1, CONNECTION_FETCH },
57 { "FETCH host 'host' FILTER "
58 "host = 'host'", -1, 1, CONNECTION_FETCH },
60 { "LIST hosts", -1, 1, CONNECTION_LIST },
61 { "LIST hosts -- foo", -1, 1, CONNECTION_LIST },
62 { "LIST hosts;", -1, 1, CONNECTION_LIST },
63 { "LIST hosts; INVALID", 11, 1, CONNECTION_LIST },
64 { "LIST hosts FILTER "
65 "host = 'host'", -1, 1, CONNECTION_LIST },
66 { "LIST services", -1, 1, CONNECTION_LIST },
67 { "LIST services FILTER "
68 "ANY service = 'svc'", -1, 1, CONNECTION_LIST },
69 { "LIST metrics", -1, 1, CONNECTION_LIST },
70 { "LIST metrics FILTER "
71 "ANY metric = 'm'", -1, 1, CONNECTION_LIST },
73 { "LOOKUP hosts", -1, 1, CONNECTION_LOOKUP },
74 { "LOOKUP hosts MATCHING "
75 "host = 'host'", -1, 1, CONNECTION_LOOKUP },
76 { "LOOKUP hosts MATCHING NOT "
77 "host = 'host'", -1, 1, CONNECTION_LOOKUP },
78 { "LOOKUP hosts MATCHING "
79 "host =~ 'p' AND "
80 "ANY service =~ 'p'", -1, 1, CONNECTION_LOOKUP },
81 { "LOOKUP hosts MATCHING NOT "
82 "host =~ 'p' AND "
83 "ANY service =~ 'p'", -1, 1, CONNECTION_LOOKUP },
84 { "LOOKUP hosts MATCHING "
85 "host =~ 'p' AND "
86 "ANY service =~ 'p' OR "
87 "ANY service =~ 'r'", -1, 1, CONNECTION_LOOKUP },
88 { "LOOKUP hosts MATCHING NOT "
89 "host =~ 'p' AND "
90 "ANY service =~ 'p' OR "
91 "ANY service =~ 'r'", -1, 1, CONNECTION_LOOKUP },
92 { "LOOKUP hosts MATCHING "
93 "host =~ 'p' "
94 "FILTER .age > 1D", -1, 1, CONNECTION_LOOKUP },
95 { "LOOKUP hosts MATCHING "
96 "host =~ 'p' "
97 "FILTER .age > 1D AND "
98 ".interval < 240s" , -1, 1, CONNECTION_LOOKUP },
99 { "LOOKUP hosts MATCHING "
100 "host =~ 'p' "
101 "FILTER NOT .age>1D", -1, 1, CONNECTION_LOOKUP },
102 { "LOOKUP hosts MATCHING "
103 "host =~ 'p' "
104 "FILTER .age>"
105 ".interval", -1, 1, CONNECTION_LOOKUP },
107 { "TIMESERIES 'host'.'metric' "
108 "START 2014-01-01 "
109 "END 2014-12-31 "
110 "23:59:59", -1, 1, CONNECTION_TIMESERIES },
111 { "TIMESERIES 'host'.'metric' "
112 "START 2014-02-02 "
113 "14:02", -1, 1, CONNECTION_TIMESERIES },
114 { "TIMESERIES 'host'.'metric' "
115 "END 2014-02-02", -1, 1, CONNECTION_TIMESERIES },
116 { "TIMESERIES "
117 "'host'.'metric'", -1, 1, CONNECTION_TIMESERIES },
119 /* string constants */
120 { "LOOKUP hosts MATCHING "
121 "host = ''''", -1, 1, CONNECTION_LOOKUP },
122 { "LOOKUP hosts MATCHING "
123 "host = '''foo'", -1, 1, CONNECTION_LOOKUP },
124 { "LOOKUP hosts MATCHING "
125 "host = 'f''oo'", -1, 1, CONNECTION_LOOKUP },
126 { "LOOKUP hosts MATCHING "
127 "host = 'foo'''", -1, 1, CONNECTION_LOOKUP },
128 { "LOOKUP hosts MATCHING "
129 "host = '''", -1, -1, 0 },
131 /* numeric constants */
132 { "LOOKUP hosts MATCHING "
133 "attribute['foo'] = "
134 "1234", -1, 1, CONNECTION_LOOKUP },
135 { "LOOKUP hosts MATCHING "
136 "attribute['foo'] != "
137 "+234", -1, 1, CONNECTION_LOOKUP },
138 { "LOOKUP hosts MATCHING "
139 "attribute['foo'] < "
140 "-234", -1, 1, CONNECTION_LOOKUP },
141 { "LOOKUP hosts MATCHING "
142 "attribute['foo'] > "
143 "12.4", -1, 1, CONNECTION_LOOKUP },
144 { "LOOKUP hosts MATCHING "
145 "attribute['foo'] <= "
146 "12. + .3", -1, 1, CONNECTION_LOOKUP },
147 { "LOOKUP hosts MATCHING "
148 "attribute['foo'] <= "
149 "'f' || 'oo'", -1, 1, CONNECTION_LOOKUP },
150 { "LOOKUP hosts MATCHING "
151 "attribute['foo'] >= "
152 ".4", -1, 1, CONNECTION_LOOKUP },
153 { "LOOKUP hosts MATCHING "
154 "attribute['foo'] = "
155 "+12e3", -1, 1, CONNECTION_LOOKUP },
156 { "LOOKUP hosts MATCHING "
157 "attribute['foo'] = "
158 "+12e-3", -1, 1, CONNECTION_LOOKUP },
159 { "LOOKUP hosts MATCHING "
160 "attribute['foo'] = "
161 "-12e+3", -1, 1, CONNECTION_LOOKUP },
163 /* date, time, interval constants */
164 { "LOOKUP hosts MATCHING "
165 "attribute['foo'] = "
166 "1 Y 42D", -1, 1, CONNECTION_LOOKUP },
167 { "LOOKUP hosts MATCHING "
168 "attribute['foo'] = "
169 "1s 42D", -1, 1, CONNECTION_LOOKUP },
170 /*
171 * TODO: Something like 1Y42D should work as well but it doesn't since
172 * the scanner will tokenize it into {digit}{identifier} :-/
173 *
174 { "LOOKUP hosts MATCHING "
175 "attribute['foo'] = "
176 "1Y42D", -1, 1, CONNECTION_LOOKUP },
177 */
179 /* NULL */
180 { "LOOKUP hosts MATCHING "
181 "attribute['foo'] "
182 "IS NULL", -1, 1, CONNECTION_LOOKUP },
183 { "LOOKUP hosts MATCHING "
184 "attribute['foo'] "
185 "IS NOT NULL", -1, 1, CONNECTION_LOOKUP },
186 { "LOOKUP hosts MATCHING "
187 "NOT attribute['foo'] "
188 "IS NULL", -1, 1, CONNECTION_LOOKUP },
189 { "LOOKUP hosts MATCHING "
190 "host IS NULL", -1, -1, 0 },
191 { "LOOKUP hosts MATCHING "
192 "ANY service IS NULL", -1, -1, 0 },
194 /* invalid numeric constants */
195 { "LOOKUP hosts MATCHING "
196 "attribute['foo'] = "
197 "+-12e+3", -1, -1, 0 },
198 { "LOOKUP hosts MATCHING "
199 "attribute['foo'] = "
200 "-12e-+3", -1, -1, 0 },
201 { "LOOKUP hosts MATCHING "
202 "attribute['foo'] = "
203 "e+3", -1, -1, 0 },
204 { "LOOKUP hosts MATCHING "
205 "attribute['foo'] = "
206 "3e", -1, -1, 0 },
207 /* following SQL standard, we don't support hex numbers */
208 { "LOOKUP hosts MATCHING "
209 "attribute['foo'] = "
210 "0x12", -1, -1, 0 },
212 /* invalid expressions */
213 { "LOOKUP hosts MATCHING "
214 "attribute['foo'] = "
215 "1.23 + 'foo'", -1, -1, 0 },
216 { "LOOKUP hosts MATCHING "
217 "attr['foo'] = 1.23", -1, -1, 0 },
218 { "LOOKUP hosts MATCHING "
219 "attr['foo'] IS NULL", -1, -1, 0 },
221 /* comments */
222 { "/* some comment */", -1, 0, 0 },
223 { "-- another comment", -1, 0, 0 },
225 /* syntax errors */
226 { "INVALID", -1, -1, 0 },
227 { "FETCH host", -1, -1, 0 },
228 { "FETCH 'host'", -1, -1, 0 },
229 { "LIST hosts; INVALID", -1, -1, 0 },
230 { "/* some incomplete", -1, -1, 0 },
232 { "LIST", -1, -1, 0 },
233 { "LIST foo", -1, -1, 0 },
234 { "LIST hosts MATCHING "
235 "host = 'host'", -1, -1, 0 },
236 { "LIST foo FILTER "
237 "host = 'host'", -1, -1, 0 },
238 { "FETCH host 'host' MATCHING "
239 "host = 'host'", -1, -1, 0 },
240 { "FETCH foo 'host'", -1, -1, 0 },
241 { "FETCH foo 'host' FILTER "
242 "host = 'host'", -1, -1, 0 },
244 { "LOOKUP foo", -1, -1, 0 },
245 { "LOOKUP foo MATCHING "
246 "host = 'host'", -1, -1, 0 },
247 { "LOOKUP foo FILTER "
248 "host = 'host'", -1, -1, 0 },
249 { "LOOKUP foo MATCHING "
250 "host = 'host' FILTER "
251 "host = 'host'", -1, -1, 0 },
252 { "LOOKUP hosts MATCHING "
253 "attribute['foo'] <= "
254 "f || 'oo'", -1, -1, 0 },
255 { "LOOKUP hosts MATCHING "
256 "attribute['foo'] <= "
257 "'f' || oo", -1, -1, 0 },
258 };
260 size_t i;
261 sdb_llist_t *check;
263 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
264 sdb_object_t *obj;
265 _Bool ok;
267 check = sdb_fe_parse(golden_data[i].query, golden_data[i].len);
268 if (golden_data[i].expected < 0)
269 ok = check == 0;
270 else
271 ok = sdb_llist_len(check) == (size_t)golden_data[i].expected;
273 fail_unless(ok, "sdb_fe_parse(%s) = %p (len: %zu); expected: %d",
274 golden_data[i].query, check, sdb_llist_len(check),
275 golden_data[i].expected);
277 if (! check)
278 continue;
280 if ((! golden_data[i].expected_cmd)
281 || (golden_data[i].expected <= 0)) {
282 sdb_llist_destroy(check);
283 continue;
284 }
286 obj = sdb_llist_get(check, 0);
287 fail_unless(SDB_CONN_NODE(obj)->cmd == golden_data[i].expected_cmd,
288 "sdb_fe_parse(%s)->cmd = %i; expected: %d",
289 golden_data[i].query, SDB_CONN_NODE(obj)->cmd,
290 golden_data[i].expected_cmd);
291 sdb_object_deref(obj);
292 sdb_llist_destroy(check);
293 }
294 }
295 END_TEST
297 START_TEST(test_parse_matcher)
298 {
299 struct {
300 const char *expr;
301 int len;
302 int expected;
303 } golden_data[] = {
304 /* empty expressions */
305 { NULL, -1, -1 },
306 { "", -1, -1 },
308 /* match hosts by name */
309 { "host < 'localhost'", -1, MATCHER_LT },
310 { "host <= 'localhost'", -1, MATCHER_LE },
311 { "host = 'localhost'", -1, MATCHER_EQ },
312 { "host != 'localhost'", -1, MATCHER_NE },
313 { "host >= 'localhost'", -1, MATCHER_GE },
314 { "host > 'localhost'", -1, MATCHER_GT },
315 { "host =~ 'host'", -1, MATCHER_REGEX },
316 { "host !~ 'host'", -1, MATCHER_NREGEX },
317 { "host = 'localhost' -- foo", -1, MATCHER_EQ },
318 { "host = 'host' <garbage>", 13, MATCHER_EQ },
319 { "host &^ 'localhost'", -1, -1 },
320 /* match hosts by service */
321 { "ANY service < 'name'", -1, MATCHER_ANY },
322 { "ANY service <= 'name'", -1, MATCHER_ANY },
323 { "ANY service = 'name'", -1, MATCHER_ANY },
324 { "ANY service != 'name'", -1, MATCHER_ANY },
325 { "ANY service >= 'name'", -1, MATCHER_ANY },
326 { "ANY service > 'name'", -1, MATCHER_ANY },
327 { "ANY service =~ 'pattern'", -1, MATCHER_ANY },
328 { "ANY service !~ 'pattern'", -1, MATCHER_ANY },
329 { "ANY service &^ 'name'", -1, -1 },
330 /* match hosts by metric */
331 { "ANY metric < 'name'", -1, MATCHER_ANY },
332 { "ANY metric <= 'name'", -1, MATCHER_ANY },
333 { "ANY metric = 'name'", -1, MATCHER_ANY },
334 { "ANY metric != 'name'", -1, MATCHER_ANY },
335 { "ANY metric >= 'name'", -1, MATCHER_ANY },
336 { "ANY metric > 'name'", -1, MATCHER_ANY },
337 { "ANY metric =~ 'pattern'", -1, MATCHER_ANY },
338 { "ANY metric !~ 'pattern'", -1, MATCHER_ANY },
339 /* match hosts by attribute */
340 { "ANY attribute < 'name'", -1, MATCHER_ANY },
341 { "ANY attribute <= 'name'", -1, MATCHER_ANY },
342 { "ANY attribute = 'name'", -1, MATCHER_ANY },
343 { "ANY attribute != 'name'", -1, MATCHER_ANY },
344 { "ANY attribute >= 'name'", -1, MATCHER_ANY },
345 { "ANY attribute > 'name'", -1, MATCHER_ANY },
346 { "ANY attribute =~ 'pattern'", -1, MATCHER_ANY },
347 { "ANY attribute !~ 'pattern'", -1, MATCHER_ANY },
348 { "ANY attribute &^ 'pattern'", -1, -1 },
349 /* composite expressions */
350 { "host =~ 'pattern' AND "
351 "ANY service =~ 'pattern'", -1, MATCHER_AND },
352 { "host =~ 'pattern' OR "
353 "ANY service =~ 'pattern'", -1, MATCHER_OR },
354 { "NOT host = 'host'", -1, MATCHER_NOT },
355 /* numeric expressions */
356 { "attribute['foo'] < 123", -1, MATCHER_LT },
357 { "attribute['foo'] <= 123", -1, MATCHER_LE },
358 { "attribute['foo'] = 123", -1, MATCHER_EQ },
359 { "attribute['foo'] >= 123", -1, MATCHER_GE },
360 { "attribute['foo'] > 123", -1, MATCHER_GT },
361 /* datetime expressions */
362 { "attribute['foo'] = "
363 "2014-08-16", -1, MATCHER_EQ },
364 { "attribute['foo'] = "
365 "17:23", -1, MATCHER_EQ },
366 { "attribute['foo'] = "
367 "17:23:53", -1, MATCHER_EQ },
368 { "attribute['foo'] = "
369 "17:23:53.123", -1, MATCHER_EQ },
370 { "attribute['foo'] = "
371 "17:23:53.123456789", -1, MATCHER_EQ },
372 { "attribute['foo'] = "
373 "2014-08-16 17:23", -1, MATCHER_EQ },
374 { "attribute['foo'] = "
375 "2014-08-16 17:23:53", -1, MATCHER_EQ },
376 /* NULL; while this is an implementation detail,
377 * IS NULL currently maps to an equality matcher */
378 { "attribute['foo'] IS NULL", -1, MATCHER_ISNULL },
379 { "attribute['foo'] IS NOT NULL", -1, MATCHER_ISNNULL },
381 /* object field matchers */
382 { ".name < 'a'", -1, MATCHER_LT },
383 { ".name <= 'a'", -1, MATCHER_LE },
384 { ".name = 'a'", -1, MATCHER_EQ },
385 { ".name != 'a'", -1, MATCHER_NE },
386 { ".name >= 'a'", -1, MATCHER_GE },
387 { ".name > 'a'", -1, MATCHER_GT },
388 { ".last_update < 2014-10-01", -1, MATCHER_LT },
389 { ".last_update <= 2014-10-01", -1, MATCHER_LE },
390 { ".last_update = 2014-10-01", -1, MATCHER_EQ },
391 { ".last_update != 2014-10-01", -1, MATCHER_NE },
392 { ".last_update >= 2014-10-01", -1, MATCHER_GE },
393 { ".last_update > 2014-10-01", -1, MATCHER_GT },
394 { ".Last_Update >= 24D", -1, MATCHER_GE },
395 { ".age < 20s", -1, MATCHER_LT },
396 { ".age <= 20s", -1, MATCHER_LE },
397 { ".age = 20s", -1, MATCHER_EQ },
398 { ".age != 20s", -1, MATCHER_NE },
399 { ".age >= 20s", -1, MATCHER_GE },
400 { ".age > 20s", -1, MATCHER_GT },
401 { ".AGE <= 1m", -1, MATCHER_LE },
402 { ".age > 1M", -1, MATCHER_GT },
403 { ".age != 20Y", -1, MATCHER_NE },
404 { ".age <= 2 * .interval", -1, MATCHER_LE },
405 { ".interval < 20s", -1, MATCHER_LT },
406 { ".interval <= 20s", -1, MATCHER_LE },
407 { ".interval = 20s", -1, MATCHER_EQ },
408 { ".interval != 20s", -1, MATCHER_NE },
409 { ".interval >= 20s", -1, MATCHER_GE },
410 { ".interval > 20s", -1, MATCHER_GT },
411 { "'be' IN .backend", -1, MATCHER_IN },
413 /* check operator precedence */
414 { "host = 'name' OR "
415 "ANY service = 'name' AND "
416 "ANY attribute = 'name' OR "
417 "attribute['foo'] = 'bar'", -1, MATCHER_OR },
418 { "host = 'name' AND "
419 "ANY service = 'name' AND "
420 "ANY attribute = 'name' OR "
421 "attribute['foo'] = 'bar'", -1, MATCHER_OR },
422 { "host = 'name' AND "
423 "ANY service = 'name' OR "
424 "ANY attribute = 'name' AND "
425 "attribute['foo'] = 'bar'", -1, MATCHER_OR },
426 { "(host = 'name' OR "
427 "ANY service = 'name') AND "
428 "(ANY attribute = 'name' OR "
429 "attribute['foo'] = 'bar')", -1, MATCHER_AND },
430 { "NOT host = 'name' OR "
431 "ANY service = 'name'", -1, MATCHER_OR },
432 { "NOT host = 'name' OR "
433 "NOT ANY service = 'name'", -1, MATCHER_OR },
434 { "NOT (host = 'name' OR "
435 "NOT ANY service = 'name')", -1, MATCHER_NOT },
437 /* syntax errors */
438 { "LIST", -1, -1 },
439 { "foo &^ bar", -1, -1 },
440 { ".invalid", -1, -1 },
441 };
443 size_t i;
445 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
446 sdb_store_matcher_t *m;
447 m = sdb_fe_parse_matcher(golden_data[i].expr, golden_data[i].len);
449 if (golden_data[i].expected < 0) {
450 fail_unless(m == NULL,
451 "sdb_fe_parse_matcher(%s) = %p; expected: NULL",
452 golden_data[i].expr, m);
453 continue;
454 }
456 fail_unless(m != NULL, "sdb_fe_parse_matcher(%s) = NULL; "
457 "expected: <matcher>", golden_data[i].expr);
458 fail_unless(M(m)->type == golden_data[i].expected,
459 "sdb_fe_parse_matcher(%s) returned matcher of type %d; "
460 "expected: %d", golden_data[i].expr, M(m)->type,
461 golden_data[i].expected);
463 sdb_object_deref(SDB_OBJ(m));
464 }
465 }
466 END_TEST
468 START_TEST(test_parse_expr)
469 {
470 struct {
471 const char *expr;
472 int len;
473 int expected;
474 } golden_data[] = {
475 /* empty expressions */
476 { NULL, -1, INT_MAX },
477 { "", -1, INT_MAX },
479 /* constant expressions */
480 { "'localhost'", -1, 0 },
481 { "123", -1, 0 },
482 { "2014-08-16", -1, 0 },
483 { "17:23", -1, 0 },
484 { "17:23:53", -1, 0 },
485 { "17:23:53.123", -1, 0 },
486 { "17:23:53.123456789", -1, 0 },
487 { "2014-08-16 17:23", -1, 0 },
488 { "2014-08-16 17:23:53", -1, 0 },
489 { "10s", -1, 0 },
490 { "60m", -1, 0 },
491 { "10Y 24D 1h", -1, 0 },
493 { "123 + 456", -1, 0 },
494 { "'foo' || 'bar'", -1, 0 },
495 { "456 - 123", -1, 0 },
496 { "1.2 * 3.4", -1, 0 },
497 { "1.2 / 3.4", -1, 0 },
498 { "5 % 2", -1, 0 },
500 /* queryable fields */
501 { ".last_update", -1, FIELD_VALUE },
502 { ".AGE", -1, FIELD_VALUE },
503 { ".interval", -1, FIELD_VALUE },
504 { ".Last_Update", -1, FIELD_VALUE },
505 { ".backend", -1, FIELD_VALUE },
507 /* attributes */
508 { "attribute['foo']", -1, ATTR_VALUE },
510 /* arithmetic expressions */
511 { ".age + .age", -1, SDB_DATA_ADD },
512 { ".age - .age", -1, SDB_DATA_SUB },
513 { ".age * .age", -1, SDB_DATA_MUL },
514 { ".age / .age", -1, SDB_DATA_DIV },
515 { ".age % .age", -1, SDB_DATA_MOD },
516 { ".age || .age", -1, SDB_DATA_CONCAT },
518 /* operator precedence */
519 { ".age + .age * .age", -1, SDB_DATA_ADD },
520 { ".age * .age + .age", -1, SDB_DATA_ADD },
521 { ".age + .age - .age", -1, SDB_DATA_SUB },
522 { ".age - .age + .age", -1, SDB_DATA_ADD },
523 { "(.age + .age) * .age", -1, SDB_DATA_MUL },
524 { ".age + (.age * .age)", -1, SDB_DATA_ADD },
526 /* syntax errors */
527 { "LIST", -1, INT_MAX },
528 { "foo &^ bar", -1, INT_MAX },
529 { ".invalid", -1, INT_MAX },
530 };
532 size_t i;
534 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
535 sdb_store_expr_t *e;
536 e = sdb_fe_parse_expr(golden_data[i].expr, golden_data[i].len);
538 if (golden_data[i].expected == INT_MAX) {
539 fail_unless(e == NULL,
540 "sdb_fe_parse_expr(%s) = %p; expected: NULL",
541 golden_data[i].expr, e);
542 continue;
543 }
545 fail_unless(e != NULL, "sdb_fe_parse_expr(%s) = NULL; "
546 "expected: <expr>", golden_data[i].expr);
547 fail_unless(e->type == golden_data[i].expected,
548 "sdb_fe_parse_expr(%s) returned expression of type %d; "
549 "expected: %d", golden_data[i].expr, e->type,
550 golden_data[i].expected);
552 sdb_object_deref(SDB_OBJ(e));
553 }
554 }
555 END_TEST
557 Suite *
558 fe_parser_suite(void)
559 {
560 Suite *s = suite_create("frontend::parser");
561 TCase *tc;
563 tc = tcase_create("core");
564 tcase_add_test(tc, test_parse);
565 tcase_add_test(tc, test_parse_matcher);
566 tcase_add_test(tc, test_parse_expr);
567 suite_add_tcase(s, tc);
569 return s;
570 } /* util_parser_suite */
572 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */