add at function

This commit is contained in:
pantor
2019-04-07 16:15:12 +02:00
parent bf071019b9
commit d5532ac26a
6 changed files with 38 additions and 14 deletions

View File

@@ -62,11 +62,10 @@ TEST_CASE("types") {
CHECK( env.render("{% for name in names %}{{ loop.index }}: {{ name }}{% if not loop.is_last %}, {% endif %}{% endfor %}!", data) == "0: Jeff, 1: Seb!" );
CHECK( env.render("{% for name in names %}{{ loop.index }}: {{ name }}{% if loop.is_last == false %}, {% endif %}{% endfor %}!", data) == "0: Jeff, 1: Seb!" );
data["empty_loop"] = {};
CHECK( env.render("{% for name in empty_loop %}a{% endfor %}", data) == "" );
CHECK( env.render("{% for name in {} %}a{% endfor %}", data) == "" );
CHECK_THROWS_WITH( env.render("{% for name ins names %}a{% endfor %}", data), "[inja.exception.parser_error] expected 'in', got 'ins'" );
CHECK_THROWS_WITH( env.render("{% for name in empty_loop %}a{% endfor %}", data), "[inja.exception.render_error] variable 'empty_loop' not found" );
// 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" );
}
@@ -144,6 +143,7 @@ TEST_CASE("functions") {
data["brother"]["daughters"] = {"Maria", "Helen"};
data["property"] = "name";
data["age"] = 29;
data["i"] = 1;
data["is_happy"] = true;
data["is_sad"] = false;
data["vars"] = {2, 3, 4, 0, -1, -2, -3};
@@ -182,6 +182,11 @@ TEST_CASE("functions") {
// CHECK_THROWS_WITH( env.render("{{ sort(5) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be array, but is number" );
}
SECTION("at") {
CHECK( env.render("{{ at(names, 0) }}", data) == "Jeff" );
CHECK( env.render("{{ at(names, i) }}", data) == "Seb" );
}
SECTION("first") {
CHECK( env.render("{{ first(names) }}", data) == "Jeff" );
// CHECK_THROWS_WITH( env.render("{{ first(5) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be array, but is number" );