code cleaning, add condition operators

This commit is contained in:
pantor
2017-08-15 15:52:52 +02:00
parent e03bb14b17
commit d277cb0acd
3 changed files with 26 additions and 14 deletions
+12 -5
View File
@@ -29,7 +29,7 @@ inline string join_strings(std::vector<string> vector, string delimiter) {
}
class smatch: public std::smatch {
/* class smatch: public std::smatch {
size_t offset;
smatch() {}
@@ -38,10 +38,10 @@ class smatch: public std::smatch {
public:
size_t pos() { return offset + position(); }
size_t end_pos() { return pos() + length(); }
size_t found() { return !empty(); }
size_t found() { return not empty(); }
string outer() { return str(0); }
string inner() { return str(1); }
};
}; */
struct SearchMatch {
@@ -301,7 +301,6 @@ public:
}
default: {
throw std::runtime_error("Parser error: Unknown delimiter.");
break;
}
}
@@ -347,12 +346,14 @@ public:
json::json_pointer ptr(input);
json result = data[ptr];
if (throw_error && result.is_null()) throw std::runtime_error("JSON pointer found no element.");
if (throw_error && result.is_null()) { throw std::runtime_error("JSON pointer found no element."); }
return result;
}
bool parse_condition(string condition, json data) {
const std::regex regex_condition_not("not (.+)");
const std::regex regex_condition_and("(.+) and (.+)");
const std::regex regex_condition_or("(.+) or (.+)");
const std::regex regex_condition_equal("(.+) == (.+)");
const std::regex regex_condition_greater("(.+) > (.+)");
const std::regex regex_condition_less("(.+) < (.+)");
@@ -365,6 +366,12 @@ public:
if (std::regex_match(condition, match_condition, regex_condition_not)) {
return not parse_condition(match_condition.str(1), data);
}
else if (std::regex_match(condition, match_condition, regex_condition_and)) {
return (parse_condition(match_condition.str(1), data) and parse_condition(match_condition.str(2), data));
}
else if (std::regex_match(condition, match_condition, regex_condition_or)) {
return (parse_condition(match_condition.str(1), data) or parse_condition(match_condition.str(2), data));
}
else if (std::regex_match(condition, match_condition, regex_condition_equal)) {
json comp1 = parse_variable(match_condition.str(1), data);
json comp2 = parse_variable(match_condition.str(2), data);
+7
View File
@@ -169,7 +169,14 @@ TEST_CASE("Parse conditions") {
SECTION("Elements") {
CHECK( env.parse_condition("age", data) );
CHECK_FALSE( env.parse_condition("size", data) );
}
SECTION("Operators") {
CHECK( env.parse_condition("not size", data) );
CHECK_FALSE( env.parse_condition("not true", data) );
CHECK( env.parse_condition("true and true", data) );
CHECK( env.parse_condition("true or false", data) );
CHECK_FALSE( env.parse_condition("true and not true", data) );
}
SECTION("Numbers") {
+7 -9
View File
@@ -18,12 +18,12 @@ TEST_CASE("Renderer") {
data["brother"]["daughters"] = {"Maria", "Helen"};
data["brother"]["daughter0"] = { { "name", "Maria" } };
data["is_happy"] = true;
SECTION("Basic") {
CHECK( env.render("Hello World!", data) == "Hello World!" );
CHECK( env.render("", data, "../") == "" );
}
SECTION("Variables") {
CHECK( env.render("Hello {{ name }}!", data) == "Hello Peter!" );
CHECK( env.render("{{ name }}", data) == "Peter" );
@@ -34,17 +34,17 @@ TEST_CASE("Renderer") {
CHECK( env.render("Hello {{ brother/name }}!", data) == "Hello Chris!" );
CHECK( env.render("Hello {{ brother/daughter0/name }}!", data) == "Hello Maria!" );
}
SECTION("Comments") {
CHECK( env.render("Hello{# This is a comment #}!", data) == "Hello!" );
CHECK( env.render("{# --- #Todo --- #}", data) == "" );
}
SECTION("Loops") {
CHECK( env.render("Hello (% for name in names %){{ name }} (% endfor %)!", data) == "Hello Jeff Seb !" );
CHECK( env.render("Hello (% for name in names %){{ index }}: {{ name }}, (% endfor %)!", data) == "Hello 0: Jeff, 1: Seb, !" );
}
SECTION("Conditionals") {
CHECK( env.render("(% if is_happy %)Yeah!(% endif %)", data) == "Yeah!" );
CHECK( env.render("(% if is_sad %)Yeah!(% endif %)", data) == "" );
@@ -55,8 +55,6 @@ TEST_CASE("Renderer") {
CHECK( env.render("(% if age != 28 %)Right(% else %)Wrong(% endif %)", data) == "Right" );
CHECK( env.render("(% if age >= 30 %)Right(% else %)Wrong(% endif %)", data) == "Wrong" );
CHECK( env.render("(% if age in [28, 29, 30] %)True(% endif %)", data) == "True" );
// Only works with gcc-5
// CHECK( env.render("(% if name in [\"Simon\", \"Tom\"] %)Test1(% else if name in [\"Peter\"] %)Test2(% else %)Test3(% endif %)", data) == "Test2" );
CHECK( env.render(R"((% if name in ["Simon", "Tom"] %)Test1(% else if name in ["Peter"] %)Test2(% else %)Test3(% endif %))", data) == "Test2" );
}
}
}