mirror of
https://github.com/pantor/inja.git
synced 2026-05-20 03:05:23 +00:00
clang-format
This commit is contained in:
+9
-6
@@ -3,7 +3,6 @@
|
||||
#include "hayai/hayai.hpp"
|
||||
#include <inja/inja.hpp>
|
||||
|
||||
|
||||
inja::Environment env;
|
||||
|
||||
const std::string test_file_directory {"../test/data/benchmark/"};
|
||||
@@ -13,11 +12,15 @@ auto large_data = env.load_json(test_file_directory + "large_data.json");
|
||||
std::string medium_template = env.load_file(test_file_directory + "medium_template.txt");
|
||||
std::string large_template = env.load_file(test_file_directory + "large_template.txt");
|
||||
|
||||
|
||||
BENCHMARK(SmallDataMediumTemplate, render, 5, 30) { env.render(medium_template, small_data); }
|
||||
BENCHMARK(LargeDataMediumTemplate, render, 5, 15) { env.render(medium_template, large_data); }
|
||||
BENCHMARK(LargeDataLargeTemplate, render, 5, 5) { env.render(large_template, large_data); }
|
||||
|
||||
BENCHMARK(SmallDataMediumTemplate, render, 5, 30) {
|
||||
env.render(medium_template, small_data);
|
||||
}
|
||||
BENCHMARK(LargeDataMediumTemplate, render, 5, 15) {
|
||||
env.render(medium_template, large_data);
|
||||
}
|
||||
BENCHMARK(LargeDataLargeTemplate, render, 5, 5) {
|
||||
env.render(large_template, large_data);
|
||||
}
|
||||
|
||||
int main() {
|
||||
hayai::ConsoleOutputter consoleOutputter;
|
||||
|
||||
+7
-6
@@ -5,7 +5,9 @@ TEST_CASE("loading") {
|
||||
inja::json data;
|
||||
data["name"] = "Jeff";
|
||||
|
||||
SUBCASE("Files should be loaded") { CHECK(env.load_file(test_file_directory + "simple.txt") == "Hello {{ name }}."); }
|
||||
SUBCASE("Files should be loaded") {
|
||||
CHECK(env.load_file(test_file_directory + "simple.txt") == "Hello {{ name }}.");
|
||||
}
|
||||
|
||||
SUBCASE("Files should be rendered") {
|
||||
CHECK(env.render_file(test_file_directory + "simple.txt", data) == "Hello Jeff.");
|
||||
@@ -29,8 +31,7 @@ TEST_CASE("complete-files") {
|
||||
|
||||
for (std::string test_name : {"simple-file", "nested", "nested-line", "html", "html-extend"}) {
|
||||
SUBCASE(test_name.c_str()) {
|
||||
CHECK(env.render_file_with_json_file(test_name + "/template.txt", test_name + "/data.json") ==
|
||||
env.load_file(test_name + "/result.txt"));
|
||||
CHECK(env.render_file_with_json_file(test_name + "/template.txt", test_name + "/data.json") == env.load_file(test_name + "/result.txt"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,8 +50,7 @@ TEST_CASE("complete-files-whitespace-control") {
|
||||
|
||||
for (std::string test_name : {"nested-whitespace"}) {
|
||||
SUBCASE(test_name.c_str()) {
|
||||
CHECK(env.render_file_with_json_file(test_name + "/template.txt", test_name + "/data.json") ==
|
||||
env.load_file(test_name + "/result.txt"));
|
||||
CHECK(env.render_file_with_json_file(test_name + "/template.txt", test_name + "/data.json") == env.load_file(test_name + "/result.txt"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,7 +73,8 @@ TEST_CASE("include-without-local-files") {
|
||||
inja::Environment env {test_file_directory};
|
||||
env.set_search_included_templates_in_files(false);
|
||||
|
||||
CHECK_THROWS_WITH(env.render_file_with_json_file("html/template.txt", "html/data.json"), "[inja.exception.render_error] (at 3:14) include 'header.txt' not found");
|
||||
CHECK_THROWS_WITH(env.render_file_with_json_file("html/template.txt", "html/data.json"),
|
||||
"[inja.exception.render_error] (at 3:14) include 'header.txt' not found");
|
||||
}
|
||||
|
||||
TEST_CASE("include-in-memory-and-file-template") {
|
||||
|
||||
+9
-12
@@ -56,8 +56,8 @@ TEST_CASE("functions") {
|
||||
SUBCASE("length") {
|
||||
CHECK(env.render("{{ length(names) }}", data) == "4"); // Length of array
|
||||
CHECK(env.render("{{ length(name) }}", data) == "5"); // Length of string
|
||||
// CHECK_THROWS_WITH( env.render("{{ length(5) }}", data), "[inja.exception.json_error]
|
||||
// [json.exception.type_error.302] type must be array, but is number" );
|
||||
// CHECK_THROWS_WITH( env.render("{{ length(5) }}", data), "[inja.exception.json_error]
|
||||
// [json.exception.type_error.302] type must be array, but is number" );
|
||||
}
|
||||
|
||||
SUBCASE("sort") {
|
||||
@@ -150,8 +150,7 @@ TEST_CASE("functions") {
|
||||
CHECK(env.render("{{ default(name, \"nobody\") }}", data) == "Peter");
|
||||
CHECK(env.render("{{ default(surname, \"nobody\") }}", data) == "nobody");
|
||||
CHECK(env.render("{{ default(surname, \"{{ surname }}\") }}", data) == "{{ surname }}");
|
||||
CHECK_THROWS_WITH(env.render("{{ default(surname, lastname) }}", data),
|
||||
"[inja.exception.render_error] (at 1:21) variable 'lastname' not found");
|
||||
CHECK_THROWS_WITH(env.render("{{ default(surname, lastname) }}", data), "[inja.exception.render_error] (at 1:21) variable 'lastname' not found");
|
||||
}
|
||||
|
||||
SUBCASE("exists") {
|
||||
@@ -159,7 +158,7 @@ TEST_CASE("functions") {
|
||||
CHECK(env.render("{{ exists(\"zipcode\") }}", data) == "false");
|
||||
CHECK(env.render("{{ exists(name) }}", data) == "false");
|
||||
CHECK(env.render("{{ exists(property) }}", data) == "true");
|
||||
|
||||
|
||||
// CHECK(env.render("{{ exists(\"keywords\") and length(keywords) > 0 }}", data) == "false");
|
||||
}
|
||||
|
||||
@@ -168,10 +167,8 @@ TEST_CASE("functions") {
|
||||
CHECK(env.render("{{ existsIn(brother, \"parents\") }}", data) == "false");
|
||||
CHECK(env.render("{{ existsIn(brother, property) }}", data) == "true");
|
||||
CHECK(env.render("{{ existsIn(brother, name) }}", data) == "false");
|
||||
CHECK_THROWS_WITH(env.render("{{ existsIn(sister, \"lastname\") }}", data),
|
||||
"[inja.exception.render_error] (at 1:13) variable 'sister' not found");
|
||||
CHECK_THROWS_WITH(env.render("{{ existsIn(brother, sister) }}", data),
|
||||
"[inja.exception.render_error] (at 1:22) variable 'sister' not found");
|
||||
CHECK_THROWS_WITH(env.render("{{ existsIn(sister, \"lastname\") }}", data), "[inja.exception.render_error] (at 1:13) variable 'sister' not found");
|
||||
CHECK_THROWS_WITH(env.render("{{ existsIn(brother, sister) }}", data), "[inja.exception.render_error] (at 1:22) variable 'sister' not found");
|
||||
}
|
||||
|
||||
SUBCASE("join") {
|
||||
@@ -212,7 +209,7 @@ TEST_CASE("callbacks") {
|
||||
inja::json data;
|
||||
data["age"] = 28;
|
||||
|
||||
env.add_callback("double", 1, [](inja::Arguments &args) {
|
||||
env.add_callback("double", 1, [](inja::Arguments& args) {
|
||||
int number = args.at(0)->get<int>();
|
||||
return 2 * number;
|
||||
});
|
||||
@@ -244,7 +241,7 @@ TEST_CASE("callbacks") {
|
||||
});
|
||||
|
||||
env.add_void_callback("log", 1, [](inja::Arguments) {
|
||||
|
||||
|
||||
});
|
||||
|
||||
env.add_callback("multiply", 0, [](inja::Arguments) { return 1.0; });
|
||||
@@ -262,7 +259,7 @@ TEST_CASE("callbacks") {
|
||||
|
||||
SUBCASE("Variadic") {
|
||||
env.add_callback("argmax", [](inja::Arguments& args) {
|
||||
auto result = std::max_element(args.begin(), args.end(), [](const inja::json* a, const inja::json* b) { return *a < *b;});
|
||||
auto result = std::max_element(args.begin(), args.end(), [](const inja::json* a, const inja::json* b) { return *a < *b; });
|
||||
return std::distance(args.begin(), result);
|
||||
});
|
||||
|
||||
|
||||
+13
-29
@@ -50,25 +50,20 @@ TEST_CASE("types") {
|
||||
SUBCASE("loops") {
|
||||
CHECK(env.render("{% for name in names %}a{% endfor %}", data) == "aa");
|
||||
CHECK(env.render("Hello {% for name in names %}{{ name }} {% endfor %}!", data) == "Hello Jeff Seb !");
|
||||
CHECK(env.render("Hello {% for name in names %}{{ loop.index }}: {{ name }}, {% endfor %}!", data) ==
|
||||
"Hello 0: Jeff, 1: Seb, !");
|
||||
CHECK(env.render("Hello {% for name in names %}{{ loop.index }}: {{ name }}, {% endfor %}!", data) == "Hello 0: Jeff, 1: Seb, !");
|
||||
CHECK(env.render("{% for type, name in relatives %}{{ loop.index1 }}: {{ type }}: {{ name }}{% if loop.is_last == "
|
||||
"false %}, {% endif %}{% endfor %}",
|
||||
data) == "1: brother: Chris, 2: mother: Maria, 3: sister: Jenny");
|
||||
CHECK(env.render("{% for v in vars %}{% if v > 0 %}+{% endif %}{% endfor %}", data) == "+++");
|
||||
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 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!");
|
||||
|
||||
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] (at 1:13) expected 'in', got 'ins'");
|
||||
CHECK_THROWS_WITH(env.render("{% for name in empty_loop %}a{% endfor %}", data),
|
||||
"[inja.exception.render_error] (at 1:16) variable 'empty_loop' not found");
|
||||
CHECK_THROWS_WITH(env.render("{% for name ins names %}a{% endfor %}", data), "[inja.exception.parser_error] (at 1:13) expected 'in', got 'ins'");
|
||||
CHECK_THROWS_WITH(env.render("{% for name in empty_loop %}a{% endfor %}", data), "[inja.exception.render_error] (at 1:16) 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" );
|
||||
}
|
||||
@@ -112,12 +107,10 @@ TEST_CASE("types") {
|
||||
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");
|
||||
CHECK(env.render("{% if age == 28 %}28{% else if age == 29 %}29{% endif %}", data) == "29");
|
||||
CHECK(env.render("{% if age == 26 %}26{% else if age == 27 %}27{% else if age == 28 %}28{% else %}29{% endif %}",
|
||||
data) == "29");
|
||||
CHECK(env.render("{% if age == 26 %}26{% else if age == 27 %}27{% else if age == 28 %}28{% else %}29{% endif %}", data) == "29");
|
||||
CHECK(env.render("{% if age == 25 %}+{% endif %}{% if age == 29 %}+{% else %}-{% endif %}", data) == "+");
|
||||
|
||||
CHECK_THROWS_WITH(env.render("{% if is_happy %}{% if is_happy %}{% endif %}", data),
|
||||
"[inja.exception.parser_error] (at 1:46) unmatched if");
|
||||
CHECK_THROWS_WITH(env.render("{% if is_happy %}{% if is_happy %}{% endif %}", data), "[inja.exception.parser_error] (at 1:46) unmatched if");
|
||||
CHECK_THROWS_WITH(env.render("{% if is_happy %}{% else if is_happy %}{% end if %}", data),
|
||||
"[inja.exception.parser_error] (at 1:43) expected statement, got 'end'");
|
||||
}
|
||||
@@ -128,8 +121,7 @@ TEST_CASE("types") {
|
||||
CHECK(env.render("{% set age=30 %}{{age}}", data) == "30");
|
||||
CHECK(env.render("{% set predefined.value=1 %}{% if existsIn(predefined, \"value\") %}{{predefined.value}}{% endif %}", data) == "1");
|
||||
CHECK(env.render("{% set brother.name=\"Bob\" %}{{brother.name}}", data) == "Bob");
|
||||
CHECK_THROWS_WITH(env.render("{% if predefined %}{% endif %}", data),
|
||||
"[inja.exception.render_error] (at 1:7) variable 'predefined' not found");
|
||||
CHECK_THROWS_WITH(env.render("{% if predefined %}{% endif %}", data), "[inja.exception.render_error] (at 1:7) variable 'predefined' not found");
|
||||
CHECK(env.render("{{age}}", data) == "29");
|
||||
CHECK(env.render("{{brother.name}}", data) == "Chris");
|
||||
}
|
||||
@@ -181,31 +173,24 @@ TEST_CASE("templates") {
|
||||
|
||||
inja::Template t2 = env.parse("{% include \"greeting\" %}!");
|
||||
CHECK(env.render(t2, data) == "Hello Peter!");
|
||||
CHECK_THROWS_WITH(env.parse("{% include \"does-not-exist\" %}!"),
|
||||
"[inja.exception.file_error] failed accessing file at 'does-not-exist'");
|
||||
CHECK_THROWS_WITH(env.parse("{% include \"does-not-exist\" %}!"), "[inja.exception.file_error] failed accessing file at 'does-not-exist'");
|
||||
|
||||
CHECK_THROWS_WITH(env.parse("{% include does-not-exist %}!"),
|
||||
"[inja.exception.parser_error] (at 1:12) expected string, got 'does-not-exist'");
|
||||
CHECK_THROWS_WITH(env.parse("{% include does-not-exist %}!"), "[inja.exception.parser_error] (at 1:12) expected string, got 'does-not-exist'");
|
||||
}
|
||||
|
||||
SUBCASE("include-callback") {
|
||||
inja::Environment env;
|
||||
|
||||
CHECK_THROWS_WITH(env.parse("{% include \"does-not-exist\" %}!"),
|
||||
"[inja.exception.file_error] failed accessing file at 'does-not-exist'");
|
||||
CHECK_THROWS_WITH(env.parse("{% include \"does-not-exist\" %}!"), "[inja.exception.file_error] failed accessing file at 'does-not-exist'");
|
||||
|
||||
env.set_search_included_templates_in_files(false);
|
||||
env.set_include_callback([&env](const std::string&, const std::string&) {
|
||||
return env.parse("Hello {{ name }}");
|
||||
});
|
||||
env.set_include_callback([&env](const std::string&, const std::string&) { return env.parse("Hello {{ name }}"); });
|
||||
|
||||
inja::Template t1 = env.parse("{% include \"greeting\" %}!");
|
||||
CHECK(env.render(t1, data) == "Hello Peter!");
|
||||
|
||||
env.set_search_included_templates_in_files(true);
|
||||
env.set_include_callback([&env](const std::string&, const std::string& name) {
|
||||
return env.parse("Bye " + name);
|
||||
});
|
||||
env.set_include_callback([&env](const std::string&, const std::string& name) { return env.parse("Bye " + name); });
|
||||
|
||||
inja::Template t2 = env.parse("{% include \"Jeff\" %}!");
|
||||
CHECK(env.render(t2, data) == "Bye Jeff!");
|
||||
@@ -218,8 +203,7 @@ TEST_CASE("templates") {
|
||||
inja::Environment env;
|
||||
env.include_template("city.tpl", env.parse("{{ loop.index }}:{{ city.name }};"));
|
||||
|
||||
CHECK(env.render("{% for city in cities %}{% include \"city.tpl\" %}{% endfor %}", loop_data) ==
|
||||
"0:Munich;1:New York;");
|
||||
CHECK(env.render("{% for city in cities %}{% include \"city.tpl\" %}{% endfor %}", loop_data) == "0:Munich;1:New York;");
|
||||
}
|
||||
|
||||
SUBCASE("count variables") {
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@ Try this
|
||||
|
||||
TEST_CASE("copy environment") {
|
||||
inja::Environment env;
|
||||
env.add_callback("double", 1, [](inja::Arguments &args) {
|
||||
env.add_callback("double", 1, [](inja::Arguments& args) {
|
||||
int number = args.at(0)->get<int>();
|
||||
return 2 * number;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user