code cleaning, add condition operators
diff --git a/src/inja.hpp b/src/inja.hpp
index 887f62a..292c453 100644
--- a/src/inja.hpp
+++ b/src/inja.hpp
@@ -29,7 +29,7 @@
}
-class smatch: public std::smatch {
+/* class smatch: public std::smatch {
size_t offset;
smatch() {}
@@ -38,10 +38,10 @@
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 @@
}
default: {
throw std::runtime_error("Parser error: Unknown delimiter.");
- break;
}
}
@@ -347,12 +346,14 @@
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 @@
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);
diff --git a/test/src/unit-parser.cpp b/test/src/unit-parser.cpp
index cbda515..b9a6409 100644
--- a/test/src/unit-parser.cpp
+++ b/test/src/unit-parser.cpp
@@ -169,7 +169,14 @@
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") {
diff --git a/test/src/unit-renderer.cpp b/test/src/unit-renderer.cpp
index 8e686bb..ab0c920 100644
--- a/test/src/unit-renderer.cpp
+++ b/test/src/unit-renderer.cpp
@@ -18,12 +18,12 @@
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 @@
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 @@
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" );
}
-}
\ No newline at end of file
+}