mirror of
https://github.com/pantor/inja.git
synced 2026-04-22 07:49:27 +00:00
type enum, clode cleaning, readme tables
This commit is contained in:
@@ -13,15 +13,15 @@ TEST_CASE("files handling") {
|
||||
data["name"] = "Jeff";
|
||||
|
||||
SECTION("files should be loaded") {
|
||||
REQUIRE( env.load_file("../test/data/simple.txt") == "Hello {{ name }}." );
|
||||
CHECK( env.load_file("../test/data/simple.txt") == "Hello {{ name }}." );
|
||||
}
|
||||
|
||||
SECTION("files should be rendered") {
|
||||
REQUIRE( env.render_template("../test/data/simple.txt", data) == "Hello Jeff." );
|
||||
CHECK( env.render_template("../test/data/simple.txt", data) == "Hello Jeff." );
|
||||
}
|
||||
|
||||
SECTION("file includes should be rendered") {
|
||||
REQUIRE( env.render_template("../test/data/include.txt", data) == "Answer: Hello Jeff." );
|
||||
CHECK( env.render_template("../test/data/include.txt", data) == "Answer: Hello Jeff." );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ TEST_CASE("complete files") {
|
||||
|
||||
for (std::string test_name : {"simple-file", "nested"}) {
|
||||
SECTION(test_name) {
|
||||
REQUIRE( env.render_template_with_json_file(test_name + "/template.txt", test_name + "/data.json") == env.load_file(test_name + "/result.txt") );
|
||||
CHECK( env.render_template_with_json_file(test_name + "/template.txt", test_name + "/data.json") == env.load_file(test_name + "/result.txt") );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
using Environment = inja::Environment;
|
||||
using json = nlohmann::json;
|
||||
using Type = inja::Parser::Type;
|
||||
|
||||
|
||||
TEST_CASE("parser") {
|
||||
@@ -12,89 +13,89 @@ TEST_CASE("parser") {
|
||||
|
||||
SECTION("basic") {
|
||||
std::string test = "asdf";
|
||||
json result = {{{"type", "string"}, {"text", "asdf"}}};
|
||||
json result = {{{"type", Type::String}, {"text", "asdf"}}};
|
||||
|
||||
REQUIRE( env.parse(test) == result );
|
||||
CHECK( env.parse(test) == result );
|
||||
}
|
||||
|
||||
SECTION("variables") {
|
||||
std::string test = "{{ name }}";
|
||||
json result = {{{"type", "variable"}, {"command", "name"}}};
|
||||
REQUIRE( env.parse(test) == result );
|
||||
json result = {{{"type", Type::Variable}, {"command", "name"}}};
|
||||
CHECK( env.parse(test) == result );
|
||||
|
||||
std::string test_combined = "Hello {{ name }}!";
|
||||
json result_combined = {
|
||||
{{"type", "string"}, {"text", "Hello "}},
|
||||
{{"type", "variable"}, {"command", "name"}},
|
||||
{{"type", "string"}, {"text", "!"}}
|
||||
{{"type", Type::String}, {"text", "Hello "}},
|
||||
{{"type", Type::Variable}, {"command", "name"}},
|
||||
{{"type", Type::String}, {"text", "!"}}
|
||||
};
|
||||
REQUIRE( env.parse(test_combined) == result_combined );
|
||||
CHECK( env.parse(test_combined) == result_combined );
|
||||
|
||||
std::string test_multiple = "Hello {{ name }}! I come from {{ city }}.";
|
||||
json result_multiple = {
|
||||
{{"type", "string"}, {"text", "Hello "}},
|
||||
{{"type", "variable"}, {"command", "name"}},
|
||||
{{"type", "string"}, {"text", "! I come from "}},
|
||||
{{"type", "variable"}, {"command", "city"}},
|
||||
{{"type", "string"}, {"text", "."}}
|
||||
{{"type", Type::String}, {"text", "Hello "}},
|
||||
{{"type", Type::Variable}, {"command", "name"}},
|
||||
{{"type", Type::String}, {"text", "! I come from "}},
|
||||
{{"type", Type::Variable}, {"command", "city"}},
|
||||
{{"type", Type::String}, {"text", "."}}
|
||||
};
|
||||
REQUIRE( env.parse(test_multiple) == result_multiple );
|
||||
CHECK( env.parse(test_multiple) == result_multiple );
|
||||
}
|
||||
|
||||
SECTION("loops") {
|
||||
std::string test = "open (% for e in list %)lorem(% endfor %) closing";
|
||||
json result = {
|
||||
{{"type", "string"}, {"text", "open "}},
|
||||
{{"type", "loop"}, {"command", "for e in list"}, {"children", {
|
||||
{{"type", "string"}, {"text", "lorem"}}
|
||||
{{"type", Type::String}, {"text", "open "}},
|
||||
{{"type", Type::Loop}, {"command", "for e in list"}, {"children", {
|
||||
{{"type", Type::String}, {"text", "lorem"}}
|
||||
}}},
|
||||
{{"type", "string"}, {"text", " closing"}}
|
||||
{{"type", Type::String}, {"text", " closing"}}
|
||||
};
|
||||
|
||||
std::string test_nested = "(% for e in list %)(% for b in list2 %)lorem(% endfor %)(% endfor %)";
|
||||
json result_nested = {
|
||||
{{"type", "loop"}, {"command", "for e in list"}, {"children", {
|
||||
{{"type", "loop"}, {"command", "for b in list2"}, {"children", {
|
||||
{{"type", "string"}, {"text", "lorem"}}
|
||||
{{"type", Type::Loop}, {"command", "for e in list"}, {"children", {
|
||||
{{"type", Type::Loop}, {"command", "for b in list2"}, {"children", {
|
||||
{{"type", Type::String}, {"text", "lorem"}}
|
||||
}}}
|
||||
}}}
|
||||
};
|
||||
|
||||
REQUIRE( env.parse(test) == result );
|
||||
REQUIRE( env.parse(test_nested) == result_nested );
|
||||
CHECK( env.parse(test) == result );
|
||||
CHECK( env.parse(test_nested) == result_nested );
|
||||
}
|
||||
|
||||
SECTION("conditionals") {
|
||||
std::string test = "(% if true %)dfgh(% endif %)";
|
||||
json result = {
|
||||
{{"type", "condition"}, {"children", {
|
||||
{{"type", "condition_branch"}, {"command", "if true"}, {"children", {
|
||||
{{"type", "string"}, {"text", "dfgh"}}
|
||||
{{"type", Type::Condition}, {"children", {
|
||||
{{"type", Type::ConditionBranch}, {"command", "if true"}, {"children", {
|
||||
{{"type", Type::String}, {"text", "dfgh"}}
|
||||
}}}
|
||||
}}}
|
||||
};
|
||||
|
||||
std::string test2 = "if: (% if maybe %)first if(% else if perhaps %)first else if(% else if sometimes %)second else if(% else %)test else(% endif %)";
|
||||
json result2 = {
|
||||
{{"type", "string"}, {"text", "if: "}},
|
||||
{{"type", "condition"}, {"children", {
|
||||
{{"type", "condition_branch"}, {"command", "if maybe"}, {"children", {
|
||||
{{"type", "string"}, {"text", "first if"}}
|
||||
{{"type", Type::String}, {"text", "if: "}},
|
||||
{{"type", Type::Condition}, {"children", {
|
||||
{{"type", Type::ConditionBranch}, {"command", "if maybe"}, {"children", {
|
||||
{{"type", Type::String}, {"text", "first if"}}
|
||||
}}},
|
||||
{{"type", "condition_branch"}, {"command", "else if perhaps"}, {"children", {
|
||||
{{"type", "string"}, {"text", "first else if"}}
|
||||
{{"type", Type::ConditionBranch}, {"command", "else if perhaps"}, {"children", {
|
||||
{{"type", Type::String}, {"text", "first else if"}}
|
||||
}}},
|
||||
{{"type", "condition_branch"}, {"command", "else if sometimes"}, {"children", {
|
||||
{{"type", "string"}, {"text", "second else if"}}
|
||||
{{"type", Type::ConditionBranch}, {"command", "else if sometimes"}, {"children", {
|
||||
{{"type", Type::String}, {"text", "second else if"}}
|
||||
}}},
|
||||
{{"type", "condition_branch"}, {"command", "else"}, {"children", {
|
||||
{{"type", "string"}, {"text", "test else"}}
|
||||
{{"type", Type::ConditionBranch}, {"command", "else"}, {"children", {
|
||||
{{"type", Type::String}, {"text", "test else"}}
|
||||
}}},
|
||||
}}}
|
||||
};
|
||||
|
||||
REQUIRE( env.parse(test) == result );
|
||||
REQUIRE( env.parse(test2) == result2 );
|
||||
CHECK( env.parse(test) == result );
|
||||
CHECK( env.parse(test2) == result2 );
|
||||
}
|
||||
|
||||
|
||||
@@ -108,17 +109,17 @@ TEST_CASE("parser") {
|
||||
data["brother"]["daughter0"] = { { "name", "Maria" } };
|
||||
|
||||
SECTION("variables from values") {
|
||||
REQUIRE( env.parse_variable("42", data) == 42 );
|
||||
REQUIRE( env.parse_variable("3.1415", data) == 3.1415 );
|
||||
REQUIRE( env.parse_variable("\"hello\"", data) == "hello" );
|
||||
CHECK( env.parse_variable("42", data) == 42 );
|
||||
CHECK( env.parse_variable("3.1415", data) == 3.1415 );
|
||||
CHECK( env.parse_variable("\"hello\"", data) == "hello" );
|
||||
}
|
||||
|
||||
SECTION("variables from JSON data") {
|
||||
REQUIRE( env.parse_variable("name", data) == "Peter" );
|
||||
REQUIRE( env.parse_variable("age", data) == 29 );
|
||||
REQUIRE( env.parse_variable("names/1", data) == "Seb" );
|
||||
REQUIRE( env.parse_variable("brother/name", data) == "Chris" );
|
||||
REQUIRE( env.parse_variable("brother/daughters/0", data) == "Maria" );
|
||||
REQUIRE_THROWS_WITH( env.parse_variable("noelement", data), "JSON pointer found no element." );
|
||||
CHECK( env.parse_variable("name", data) == "Peter" );
|
||||
CHECK( env.parse_variable("age", data) == 29 );
|
||||
CHECK( env.parse_variable("names/1", data) == "Seb" );
|
||||
CHECK( env.parse_variable("brother/name", data) == "Chris" );
|
||||
CHECK( env.parse_variable("brother/daughters/0", data) == "Maria" );
|
||||
CHECK_THROWS_WITH( env.parse_variable("noelement", data), "JSON pointer found no element." );
|
||||
}
|
||||
}
|
||||
@@ -20,43 +20,43 @@ TEST_CASE("Renderer") {
|
||||
data["is_happy"] = true;
|
||||
|
||||
SECTION("Basic") {
|
||||
REQUIRE( env.render("Hello World!", data) == "Hello World!" );
|
||||
REQUIRE( env.render("", data, "../") == "" );
|
||||
CHECK( env.render("Hello World!", data) == "Hello World!" );
|
||||
CHECK( env.render("", data, "../") == "" );
|
||||
}
|
||||
|
||||
SECTION("Variables") {
|
||||
REQUIRE( env.render("Hello {{ name }}!", data) == "Hello Peter!" );
|
||||
REQUIRE( env.render("{{ name }}", data) == "Peter" );
|
||||
REQUIRE( env.render("{{name}}", data) == "Peter" );
|
||||
REQUIRE( env.render("{{ name }} is {{ age }} years old.", data) == "Peter is 29 years old." );
|
||||
REQUIRE( env.render("Hello {{ name }}! I come from {{ city }}.", data) == "Hello Peter! I come from Brunswick." );
|
||||
REQUIRE( env.render("Hello {{ names/1 }}!", data) == "Hello Seb!" );
|
||||
REQUIRE( env.render("Hello {{ brother/name }}!", data) == "Hello Chris!" );
|
||||
REQUIRE( env.render("Hello {{ brother/daughter0/name }}!", data) == "Hello Maria!" );
|
||||
CHECK( env.render("Hello {{ name }}!", data) == "Hello Peter!" );
|
||||
CHECK( env.render("{{ name }}", data) == "Peter" );
|
||||
CHECK( env.render("{{name}}", data) == "Peter" );
|
||||
CHECK( env.render("{{ name }} is {{ age }} years old.", data) == "Peter is 29 years old." );
|
||||
CHECK( env.render("Hello {{ name }}! I come from {{ city }}.", data) == "Hello Peter! I come from Brunswick." );
|
||||
CHECK( env.render("Hello {{ names/1 }}!", data) == "Hello Seb!" );
|
||||
CHECK( env.render("Hello {{ brother/name }}!", data) == "Hello Chris!" );
|
||||
CHECK( env.render("Hello {{ brother/daughter0/name }}!", data) == "Hello Maria!" );
|
||||
}
|
||||
|
||||
SECTION("Comments") {
|
||||
REQUIRE( env.render("Hello{# This is a comment #}!", data) == "Hello!" );
|
||||
REQUIRE( env.render("{# --- #Todo --- #}", data) == "" );
|
||||
CHECK( env.render("Hello{# This is a comment #}!", data) == "Hello!" );
|
||||
CHECK( env.render("{# --- #Todo --- #}", data) == "" );
|
||||
}
|
||||
|
||||
SECTION("Loops") {
|
||||
REQUIRE( env.render("Hello (% for name in names %){{ name }} (% endfor %)!", data) == "Hello Jeff Seb !" );
|
||||
REQUIRE( env.render("Hello (% for name in names %){{ index }}: {{ name }}, (% endfor %)!", data) == "Hello 0: Jeff, 1: Seb, !" );
|
||||
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") {
|
||||
REQUIRE( env.render("(% if is_happy %)Yeah!(% endif %)", data) == "Yeah!" );
|
||||
REQUIRE( env.render("(% if is_sad %)Yeah!(% endif %)", data) == "" );
|
||||
REQUIRE( env.render("(% if is_sad %)Yeah!(% else %)Nooo...(% endif %)", data) == "Nooo..." );
|
||||
REQUIRE( env.render("(% if age == 29 %)Right(% else %)Wrong(% endif %)", data) == "Right" );
|
||||
REQUIRE( env.render("(% if age > 29 %)Right(% else %)Wrong(% endif %)", data) == "Wrong" );
|
||||
REQUIRE( env.render("(% if age <= 29 %)Right(% else %)Wrong(% endif %)", data) == "Right" );
|
||||
REQUIRE( env.render("(% if age != 28 %)Right(% else %)Wrong(% endif %)", data) == "Right" );
|
||||
REQUIRE( env.render("(% if age >= 30 %)Right(% else %)Wrong(% endif %)", data) == "Wrong" );
|
||||
REQUIRE( env.render("(% if age in [28, 29, 30] %)True(% endif %)", data) == "True" );
|
||||
CHECK( env.render("(% if is_happy %)Yeah!(% endif %)", data) == "Yeah!" );
|
||||
CHECK( env.render("(% if is_sad %)Yeah!(% endif %)", data) == "" );
|
||||
CHECK( env.render("(% if is_sad %)Yeah!(% else %)Nooo...(% endif %)", data) == "Nooo..." );
|
||||
CHECK( env.render("(% if age == 29 %)Right(% else %)Wrong(% endif %)", data) == "Right" );
|
||||
CHECK( env.render("(% if age > 29 %)Right(% else %)Wrong(% endif %)", data) == "Wrong" );
|
||||
CHECK( env.render("(% if age <= 29 %)Right(% else %)Wrong(% endif %)", data) == "Right" );
|
||||
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
|
||||
// REQUIRE( env.render("(% if name in [\"Simon\", \"Tom\"] %)Test1(% else if name in [\"Peter\"] %)Test2(% else %)Test3(% endif %)", data) == "Test2" );
|
||||
// CHECK( env.render("(% if name in [\"Simon\", \"Tom\"] %)Test1(% else if name in [\"Peter\"] %)Test2(% else %)Test3(% endif %)", data) == "Test2" );
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,10 @@ using json = nlohmann::json;
|
||||
|
||||
|
||||
TEST_CASE("string vector join function") {
|
||||
REQUIRE( inja::join_strings({"1", "2", "3"}, ",") == "1,2,3" );
|
||||
REQUIRE( inja::join_strings({"1", "2", "3", "4", "5"}, " ") == "1 2 3 4 5" );
|
||||
REQUIRE( inja::join_strings({}, " ") == "" );
|
||||
REQUIRE( inja::join_strings({"single"}, "---") == "single" );
|
||||
CHECK( inja::join_strings({"1", "2", "3"}, ",") == "1,2,3" );
|
||||
CHECK( inja::join_strings({"1", "2", "3", "4", "5"}, " ") == "1 2 3 4 5" );
|
||||
CHECK( inja::join_strings({}, " ") == "" );
|
||||
CHECK( inja::join_strings({"single"}, "---") == "single" );
|
||||
}
|
||||
|
||||
TEST_CASE("basic search in string") {
|
||||
@@ -20,18 +20,18 @@ TEST_CASE("basic search in string") {
|
||||
|
||||
SECTION("basic search from start") {
|
||||
inja::SearchMatch match = inja::search(input, regex, 0);
|
||||
REQUIRE( match.found == true );
|
||||
REQUIRE( match.position == 6 );
|
||||
REQUIRE( match.length == 5 );
|
||||
REQUIRE( match.end_position == 11 );
|
||||
REQUIRE( match.outer == "ipsum" );
|
||||
REQUIRE( match.inner == "psu" );
|
||||
CHECK( match.found == true );
|
||||
CHECK( match.position == 6 );
|
||||
CHECK( match.length == 5 );
|
||||
CHECK( match.end_position == 11 );
|
||||
CHECK( match.outer == "ipsum" );
|
||||
CHECK( match.inner == "psu" );
|
||||
}
|
||||
|
||||
SECTION("basic search from position") {
|
||||
inja::SearchMatch match = inja::search(input, regex, 8);
|
||||
REQUIRE( match.found == false );
|
||||
REQUIRE( match.length == 0 );
|
||||
CHECK( match.found == false );
|
||||
CHECK( match.length == 0 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,54 +40,56 @@ TEST_CASE("search in string with multiple possible regexes") {
|
||||
|
||||
SECTION("basic 1") {
|
||||
std::vector<std::string> regex_patterns = { "tras", "do(\\w*)or", "es(\\w*)as", "ip(\\w*)um" };
|
||||
inja::SearchMatch match = inja::search(input, regex_patterns, 0);
|
||||
REQUIRE( match.regex_number == 3 );
|
||||
REQUIRE( match.outer == "ipsum" );
|
||||
REQUIRE( match.inner == "s" );
|
||||
inja::SearchMatchVector match = inja::search(input, regex_patterns, 0);
|
||||
CHECK( match.regex_number == 3 );
|
||||
CHECK( match.outer == "ipsum" );
|
||||
CHECK( match.inner == "s" );
|
||||
}
|
||||
|
||||
SECTION("basic 2") {
|
||||
std::vector<std::string> regex_patterns = { "tras", "ip(\\w*)um", "do(\\w*)or", "es(\\w*)as" };
|
||||
inja::SearchMatch match = inja::search(input, regex_patterns, 0);
|
||||
REQUIRE( match.regex_number == 1 );
|
||||
REQUIRE( match.outer == "ipsum" );
|
||||
REQUIRE( match.inner == "s" );
|
||||
inja::SearchMatchVector match = inja::search(input, regex_patterns, 0);
|
||||
CHECK( match.regex_number == 1 );
|
||||
CHECK( match.outer == "ipsum" );
|
||||
CHECK( match.inner == "s" );
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("search on level") {
|
||||
std::string input = "(% up %)(% up %)(% N1 %)(% down %)...(% up %)(% N2 %)(% up %)(% N3 %)(% down %)(% N4 %)(% down %)(% N5 %)(% down %)";
|
||||
std::string input = "(% up %)(% up %)Test(% N1 %)(% down %)...(% up %)(% N2 %)(% up %)(% N3 %)(% down %)(% N4 %)(% down %)(% N5 %)(% down %)";
|
||||
|
||||
std::regex regex_statement("\\(\\% (.*?) \\%\\)");
|
||||
std::regex regex_level_up("up");
|
||||
std::regex regex_level_down("down");
|
||||
std::regex regex_search("N(\\d+)");
|
||||
|
||||
SECTION("basic 1") {
|
||||
SECTION("first instance") {
|
||||
inja::SearchMatch open_match = inja::search(input, regex_statement, 0);
|
||||
CHECK( open_match.position == 0 );
|
||||
CHECK( open_match.end_position == 8 );
|
||||
CHECK( open_match.inner == "up" );
|
||||
|
||||
REQUIRE( open_match.position == 0 );
|
||||
REQUIRE( open_match.end_position == 8 );
|
||||
REQUIRE( open_match.inner == "up" );
|
||||
|
||||
inja::SearchClosedMatch match = inja::search_on_level(input, regex_statement, regex_level_up, regex_level_down, regex_search, open_match);
|
||||
|
||||
REQUIRE( match.position == 0 );
|
||||
REQUIRE( match.end_position == 105 );
|
||||
inja::SearchClosedMatch match = inja::search_closed_match_on_level(input, regex_statement, regex_level_up, regex_level_down, regex_search, open_match);
|
||||
CHECK( match.position == 0 );
|
||||
CHECK( match.end_position == 109 );
|
||||
}
|
||||
|
||||
SECTION("basic 1") {
|
||||
SECTION("second instance") {
|
||||
inja::SearchMatch open_match = inja::search(input, regex_statement, 4);
|
||||
|
||||
REQUIRE( open_match.position == 8 );
|
||||
REQUIRE( open_match.end_position == 16 );
|
||||
REQUIRE( open_match.inner == "up" );
|
||||
CHECK( open_match.position == 8 );
|
||||
CHECK( open_match.end_position == 16 );
|
||||
CHECK( open_match.inner == "up" );
|
||||
|
||||
inja::SearchClosedMatch match = inja::search_on_level(input, regex_statement, regex_level_up, regex_level_down, regex_search, open_match);
|
||||
inja::SearchClosedMatch match = inja::search_closed_match_on_level(input, regex_statement, regex_level_up, regex_level_down, regex_search, open_match);
|
||||
|
||||
REQUIRE( match.position == 8 );
|
||||
// REQUIRE( match.end_position == 24 );
|
||||
// REQUIRE( match.outer == "(% up %)(% N1 %)(% down %)" );
|
||||
// REQUIRE( match.inner == "(% N1 %)" );
|
||||
CHECK( match.open_match.position == 8 );
|
||||
CHECK( match.open_match.end_position== 16 );
|
||||
CHECK( match.close_match.position == 20 );
|
||||
CHECK( match.close_match.end_position == 28 );
|
||||
CHECK( match.position == 8 );
|
||||
CHECK( match.end_position == 28 );
|
||||
CHECK( match.outer == "(% up %)Test(% N1 %)" );
|
||||
CHECK( match.inner == "Test" );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user