allow empty lists for loops

This commit is contained in:
pantor
2018-03-19 10:30:26 +01:00
parent b458ed576d
commit 44f7461a8a
2 changed files with 4 additions and 0 deletions
+1
View File
@@ -380,6 +380,7 @@ public:
template<typename T = json>
T eval_expression(const Parsed::ElementExpression& element, const json& data) {
const json var = eval_function(element, data);
if (var.empty()) return T();
try {
return var.get<T>();
} catch (json::type_error& e) {
+3
View File
@@ -56,6 +56,9 @@ TEST_CASE("types") {
CHECK( env.render("{% for v in vars %}{% if v > 0 %}+{% endif %}{% endfor %}", data) == "+++" );
// CHECK( env.render("{% if 1 >= 18 %}test{% endif %}{% for v in vars %}{% if v > 0 %}+{% else %}-{% endif %}{% endfor %}", data) == "+++----" );
data["empty_loop"] = {};
CHECK( env.render("{% for name in empty_loop %}a{% endfor %}", data) == "" );
CHECK_THROWS_WITH( env.render("{% for name ins names %}a{% endfor %}", data), "[inja.exception.parser_error] unknown loop statement: for name ins names" );
// CHECK_THROWS_WITH( env.render("{% for name in relatives %}{{ name }}{% endfor %}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be array, but is object" );
}