realm-js/tests/parser.cpp

169 lines
3.2 KiB
C++

#include "catch.hpp"
#include "parser/parser.hpp"
#include <vector>
#include <string>
static std::vector<std::string> valid_queries = {
// true/false predicates
"truepredicate",
"falsepredicate",
" TRUEPREDICATE ",
" FALSEPREDICATE ",
"truepredicates = falsepredicates", // keypaths
// characters/strings
"\"\" = ''",
"'azAZ09/ :()[]{}<>,.^@-+=*&~`' = '\\\" \\' \\\\ \\/ \\b \\f \\n \\r \\t \\0'",
"\"azAZ09/\" = \"\\\" \\' \\\\ \\/ \\b \\f \\n \\r \\t \\0\"",
"'\\uffFf' = '\\u0020'",
"'\\u01111' = 'asdf\\u0111asdf'",
// expressions (numbers, bools, keypaths, arguments)
"-1 = 12",
"0 = 001",
"0x0 = -0X398235fcAb",
"10. = -.034",
"10.0 = 5.034",
"true = false",
"truelove = false",
"true = falsey",
"nullified = null",
"_ = a",
"_a = _.aZ",
"a09._br.z = __-__.Z-9",
"$0 = $19",
"$0=$0",
// operators
"0=0",
"0 = 0",
"0 =[c] 0",
"0!=0",
"0 != 0",
"0==0",
"0 == 0",
"0==[c]0",
"0 == [c] 0",
"0>0",
"0 > 0",
"0>=0",
"0 >= 0",
"0<0",
"0 < 0",
"0<=0",
"0 <= 0",
"0 contains 0",
"a CONTAINS[c] b",
"a contains [c] b",
"'a'CONTAINS[c]b",
"0 BeGiNsWiTh 0",
"0 ENDSWITH 0",
"contains contains 'contains'",
"beginswith beginswith 'beginswith'",
"endswith endswith 'endswith'",
"NOT NOT != 'NOT'",
"AND == 'AND' AND OR == 'OR'",
// FIXME - bug
// "truepredicate == 'falsepredicate' && truepredicate",
// atoms/groups
"(0=0)",
"( 0=0 )",
"((0=0))",
"!0=0",
"! 0=0",
"!(0=0)",
"! (0=0)",
"NOT0=0", // keypath NOT0
"NOT0.a=0", // keypath NOT0
"NOT0a.b=0", // keypath NOT0a
"not-1=1",
"not 0=0",
"NOT(0=0)",
"not (0=0)",
"NOT (!0=0)",
// 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",
"a=1AND NOTb=2",
};
static std::vector<std::string> invalid_queries = {
"predicate",
"'\\a' = ''", // invalid escape
// invalid unicode
"'\\u0' = ''",
// invalid strings
"\"' = ''",
"\" = ''",
"' = ''",
// expressions
"03a = 1",
"1..0 = 1",
"1.0. = 1",
"1-0 = 1",
"0x = 1",
"- = a",
"a..b = a",
"a$a = a",
"{} = $0",
"$-1 = $0",
"$a = $0",
"$ = $",
// operators
"0===>0",
"0 <> 0",
"0 contains1",
"a contains_something",
"endswith 0",
// atoms/groups
"0=0)",
"(0=0",
"(0=0))",
"! =0",
"NOTNOT(0=0)",
"not.a=0",
"(!!0=0)",
"0=0 !",
// compound
"a==a & a==a",
"a==a | a==a",
"a==a &| a==a",
"a==a && OR a==a",
"a==aORa==a",
"a==a ORa==a",
"a==a AND==a",
"a==a ANDa==a",
"a=1ANDNOT b=2",
"truepredicate &&",
"truepredicate & truepredicate",
};
TEST_CASE("valid queries") {
for (auto& query : valid_queries) {
INFO("query: " << query);
CHECK_NOTHROW(realm::parser::parse(query));
}
}
TEST_CASE("invalid queries") {
for (auto& query : invalid_queries) {
INFO("query: " << query);
CHECK_THROWS(realm::parser::parse(query));
}
}