first string tests and custom error messages

This commit is contained in:
Ari Lazier 2015-11-18 12:17:39 -08:00
parent a64fab83ad
commit be93d3dd58
2 changed files with 39 additions and 12 deletions

View File

@ -288,17 +288,34 @@ template<> struct action< not_pre >
}
};
template< typename Rule >
struct error_message_control : pegtl::normal< Rule >
{
static const std::string error_message;
template< typename Input, typename ... States >
static void raise( const Input & in, States && ... )
{
throw pegtl::parse_error( error_message, in );
}
};
template<>
const std::string error_message_control< chars >::error_message = "Invalid characters in string constant.";
template< typename Rule>
const std::string error_message_control< Rule >::error_message = "Invalid predicate.";
Predicate parse(const std::string &query)
{
analyze< pred >();
const std::string source = "user query";
Predicate out_predicate(Predicate::Type::And);
ParserState state;
state.predicate_stack.push_back(&out_predicate);
pegtl::parse< must< pred, eof >, action >(query, source, state);
pegtl::parse< must< pred, eof >, action, error_message_control >(query, query, state);
if (out_predicate.type == Predicate::Type::And && out_predicate.cpnd.sub_predicates.size() == 1) {
return std::move(out_predicate.cpnd.sub_predicates.back());
}

View File

@ -158,7 +158,7 @@ var testCases = {
"floatTests" : {
"schema" : [{
"name": "FloatObject",
"properties": [{ "name": "floatCol", "type": Realm.Types.Float }],
"properties": [{ "name": "floatCol", "type": Realm.Types.FLOAT }],
}],
"objects": [
{ "type": "FloatObject", "value": [-1.001] },
@ -191,7 +191,7 @@ var testCases = {
"doubleTests" : {
"schema" : [{
"name": "DoubleObject",
"properties": [{ "name": "doubleCol", "type": Realm.Types.Double }],
"properties": [{ "name": "doubleCol", "type": Realm.Types.DOUBLE }],
}],
"objects": [
{ "type": "DoubleObject", "value": [-1.001] },
@ -221,16 +221,23 @@ var testCases = {
"stringTests" : {
"schema" : [{
"name": "DoubleObject",
"properties": [{ "name": "doubleCol", "type": Realm.Types.Double }],
"name": "StringObject",
"properties": [{ "name": "stringCol", "type": Realm.Types.STRING }],
}],
"objects": [
{ "type": "DoubleObject", "value": [-1.001] },
{ "type": "DoubleObject", "value": [0.0] },
{ "type": "DoubleObject", "value": [100.2] },
{ "type": "StringObject", "value": ["A"] },
{ "type": "StringObject", "value": ["a"] },
{ "type": "StringObject", "value": ["a"] },
{ "type": "StringObject", "value": ["abc"] },
{ "type": "StringObject", "value": [""] },
{ "type": "StringObject", "value": ["\\\"\\n\\0\\r\\\\'"] },
],
"tests": [
["QueryCount", 1, "DoubleObject", "doubleCol == -1.001"],
["QueryCount", 2, "StringObject", "stringCol == 'a'"],
["QueryCount", 2, "StringObject", "stringCol == \"a\""],
["QueryCount", 1, "StringObject", "stringCol == 'abc'"],
["QueryCount", 1, "StringObject", "stringCol == ''"],
["QueryCount", 1, "StringObject", "stringCol == \"\\\"\\n\\0\\r\\\\'\""],
]
},
@ -374,10 +381,13 @@ module.exports = BaseTest.extend({
runQuerySuite(testCases.intTests);
},
testFloatQueries: function() {
runQuerySuite(testCases.intTests);
runQuerySuite(testCases.floatTests);
},
testDoubleQueries: function() {
runQuerySuite(testCases.intTests);
runQuerySuite(testCases.doubleTests);
},
testStringQueries: function() {
runQuerySuite(testCases.stringTests);
},
});