2016-01-06 23:52:12 +00:00
|
|
|
#include "catch.hpp"
|
|
|
|
#include "parser/parser.hpp"
|
2015-11-11 22:54:05 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
static std::vector<std::string> valid_queries = {
|
2015-11-12 22:24:37 +00:00
|
|
|
// true/false predicates
|
2015-11-11 22:54:05 +00:00
|
|
|
"truepredicate",
|
|
|
|
"falsepredicate",
|
2015-11-12 22:51:31 +00:00
|
|
|
" TRUEPREDICATE ",
|
|
|
|
" FALSEPREDICATE ",
|
2015-11-12 22:24:37 +00:00
|
|
|
|
|
|
|
// characters/strings
|
|
|
|
"\"\" = ''",
|
|
|
|
"'azAZ09/ :()[]{}<>,.^@-+=*&~`' = '\\\" \\' \\\\ \\/ \\b \\f \\n \\r \\t \\0'",
|
|
|
|
"\"azAZ09/\" = \"\\\" \\' \\\\ \\/ \\b \\f \\n \\r \\t \\0\"",
|
|
|
|
"'\\uffFf' = '\\u0020'",
|
|
|
|
"'\\u01111' = 'asdf\\u0111asdf'",
|
|
|
|
|
2015-11-12 22:34:47 +00:00
|
|
|
// expressions (numbers, bools, keypaths, arguments)
|
2015-11-12 22:24:37 +00:00
|
|
|
"-1 = 12",
|
|
|
|
"0 = 001",
|
|
|
|
"0x0 = -0X398235fcAb",
|
|
|
|
"10. = -.034",
|
|
|
|
"10.0 = 5.034",
|
|
|
|
"true = false",
|
|
|
|
"_ = a",
|
|
|
|
"_a = _.aZ",
|
|
|
|
"a09._br.z = __-__.Z-9",
|
2015-11-12 23:04:15 +00:00
|
|
|
"$0 = $19",
|
|
|
|
"$0=$0",
|
2015-11-12 22:51:31 +00:00
|
|
|
|
|
|
|
// operators
|
|
|
|
"0=0",
|
|
|
|
"0 = 0",
|
|
|
|
"0!=0",
|
|
|
|
"0 != 0",
|
|
|
|
"0==0",
|
|
|
|
"0 == 0",
|
|
|
|
"0>0",
|
|
|
|
"0 > 0",
|
|
|
|
"0>=0",
|
|
|
|
"0 >= 0",
|
|
|
|
"0<0",
|
|
|
|
"0 < 0",
|
|
|
|
"0<=0",
|
|
|
|
"0 <= 0",
|
|
|
|
"0 contains 0",
|
|
|
|
"0 BeGiNsWiTh 0",
|
|
|
|
"0 ENDSWITH 0",
|
2016-02-16 19:56:29 +00:00
|
|
|
"contains contains 'contains'",
|
2016-02-16 20:19:26 +00:00
|
|
|
"beginswith beginswith 'beginswith'",
|
|
|
|
"endswith endswith 'endswith'",
|
2016-02-16 20:10:50 +00:00
|
|
|
"NOT NOT != 'NOT'",
|
2016-02-16 20:19:26 +00:00
|
|
|
"AND == 'AND' AND OR == 'OR'",
|
|
|
|
// FIXME - bug
|
|
|
|
// "truepredicate == 'falsepredicate' && truepredicate",
|
2015-11-12 23:19:32 +00:00
|
|
|
|
|
|
|
// atoms/groups
|
|
|
|
"(0=0)",
|
|
|
|
"( 0=0 )",
|
|
|
|
"((0=0))",
|
|
|
|
"!0=0",
|
|
|
|
"! 0=0",
|
|
|
|
"!(0=0)",
|
|
|
|
"! (0=0)",
|
|
|
|
"NOT0=0", // keypath NOT0
|
|
|
|
"not 0=0",
|
|
|
|
"NOT(0=0)",
|
|
|
|
"not (0=0)",
|
|
|
|
"NOT (!0=0)",
|
2015-11-12 23:40:54 +00:00
|
|
|
|
|
|
|
// compound
|
|
|
|
"a==a && a==a",
|
|
|
|
"a==a || a==a",
|
|
|
|
"a==a&&a==a||a=a",
|
|
|
|
"a==a and a==a",
|
|
|
|
"a==a OR a==a",
|
|
|
|
"and=='AND'&&'or'=='||'",
|
|
|
|
"and == or && ORE > GRAND",
|
2015-11-18 01:17:54 +00:00
|
|
|
"a=1AND NOTb=2",
|
2015-11-11 22:54:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static std::vector<std::string> invalid_queries = {
|
|
|
|
"predicate",
|
2015-11-12 22:24:37 +00:00
|
|
|
"'\\a' = ''", // invalid escape
|
|
|
|
|
|
|
|
// invalid unicode
|
|
|
|
"'\\u0' = ''",
|
|
|
|
|
|
|
|
// invalid strings
|
|
|
|
"\"' = ''",
|
|
|
|
"\" = ''",
|
|
|
|
"' = ''",
|
|
|
|
|
2015-11-12 22:34:47 +00:00
|
|
|
// expressions
|
2015-11-12 22:24:37 +00:00
|
|
|
"03a = 1",
|
|
|
|
"1..0 = 1",
|
|
|
|
"1.0. = 1",
|
2015-11-12 22:34:47 +00:00
|
|
|
"1-0 = 1",
|
2015-11-12 22:24:37 +00:00
|
|
|
"0x = 1",
|
|
|
|
"truey = false",
|
2015-11-12 22:34:47 +00:00
|
|
|
"- = a",
|
|
|
|
"a..b = a",
|
|
|
|
"a$a = a",
|
2015-11-12 23:04:15 +00:00
|
|
|
"{} = $0",
|
|
|
|
"$-1 = $0",
|
|
|
|
"$a = $0",
|
|
|
|
"$ = $",
|
2015-11-12 22:34:47 +00:00
|
|
|
|
2015-11-12 22:51:31 +00:00
|
|
|
// operators
|
|
|
|
"0===>0",
|
|
|
|
"0 <> 0",
|
|
|
|
"0 contains1",
|
|
|
|
"endswith 0",
|
2015-11-12 22:24:37 +00:00
|
|
|
|
2015-11-12 23:19:32 +00:00
|
|
|
// atoms/groups
|
|
|
|
"0=0)",
|
|
|
|
"(0=0",
|
|
|
|
"(0=0))",
|
|
|
|
"! =0",
|
|
|
|
"NOTNOT(0=0)",
|
|
|
|
"(!!0=0)",
|
|
|
|
"0=0 !",
|
|
|
|
|
2015-11-12 23:40:54 +00:00
|
|
|
// compound
|
|
|
|
"a==a & a==a",
|
|
|
|
"a==a | a==a",
|
|
|
|
"a==a &| a==a",
|
|
|
|
"a==a && OR a==a",
|
|
|
|
"a==aORa==a",
|
2015-11-20 23:16:35 +00:00
|
|
|
//"a=1ANDNOT b=2",
|
2015-11-12 23:40:54 +00:00
|
|
|
|
2015-11-11 22:54:05 +00:00
|
|
|
"truepredicate &&",
|
|
|
|
"truepredicate & truepredicate",
|
|
|
|
};
|
|
|
|
|
2016-01-06 23:52:12 +00:00
|
|
|
TEST_CASE("valid queries") {
|
|
|
|
for (auto& query : valid_queries) {
|
|
|
|
INFO("query: " << query);
|
|
|
|
CHECK_NOTHROW(realm::parser::parse(query));
|
2015-11-11 22:54:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 23:52:12 +00:00
|
|
|
TEST_CASE("invalid queries") {
|
|
|
|
for (auto& query : invalid_queries) {
|
|
|
|
INFO("query: " << query);
|
|
|
|
CHECK_THROWS(realm::parser::parse(query));
|
|
|
|
}
|
2015-11-20 23:16:35 +00:00
|
|
|
}
|