fix travis, more tests

This commit is contained in:
pantor
2017-08-15 09:44:39 +02:00
parent 27ce2ffcc5
commit ebe6c5f671
5 changed files with 213 additions and 187 deletions
+10 -10
View File
@@ -7,30 +7,30 @@ using Environment = inja::Environment;
using json = nlohmann::json;
TEST_CASE("files handling") {
TEST_CASE("Files handling") {
Environment env = Environment();
json data;
data["name"] = "Jeff";
SECTION("files should be loaded") {
SECTION("Files should be loaded") {
CHECK( env.load_file("../test/data/simple.txt") == "Hello {{ name }}." );
}
SECTION("files should be rendered") {
SECTION("Files should be rendered") {
CHECK( env.render_template("../test/data/simple.txt", data) == "Hello Jeff." );
}
SECTION("file includes should be rendered") {
SECTION("File includes should be rendered") {
CHECK( env.render_template("../test/data/include.txt", data) == "Answer: Hello Jeff." );
}
}
TEST_CASE("complete files") {
TEST_CASE("Complete files") {
Environment env = Environment("../test/data/");
for (std::string test_name : {"simple-file", "nested"}) {
SECTION(test_name) {
CHECK( env.render_template_with_json_file(test_name + "/template.txt", test_name + "/data.json") == env.load_file(test_name + "/result.txt") );
}
}
}
}
+68 -49
View File
@@ -8,41 +8,50 @@ using json = nlohmann::json;
using Type = inja::Parser::Type;
TEST_CASE("parse structure") {
TEST_CASE("Parse structure") {
Environment env = Environment();
SECTION("basic") {
std::string test = "asdf";
json result = {{{"type", Type::String}, {"text", "asdf"}}};
SECTION("Basic string") {
std::string test = "lorem ipsum";
json result = {{{"type", Type::String}, {"text", "lorem ipsum"}}};
CHECK( env.parse(test) == result );
}
SECTION("variables") {
SECTION("Empty string") {
std::string test = "";
json result = {};
CHECK( env.parse(test) == result );
}
SECTION("Variable") {
std::string test = "{{ name }}";
json result = {{{"type", Type::Variable}, {"command", "name"}}};
CHECK( env.parse(test) == result );
std::string test_combined = "Hello {{ name }}!";
json result_combined = {
}
SECTION("Combined string and variables") {
std::string test = "Hello {{ name }}!";
json result = {
{{"type", Type::String}, {"text", "Hello "}},
{{"type", Type::Variable}, {"command", "name"}},
{{"type", Type::String}, {"text", "!"}}
};
CHECK( env.parse(test_combined) == result_combined );
std::string test_multiple = "Hello {{ name }}! I come from {{ city }}.";
json result_multiple = {
CHECK( env.parse(test) == result );
}
SECTION("Multiple variables") {
std::string test = "Hello {{ name }}! I come from {{ city }}.";
json result = {
{{"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", "."}}
};
CHECK( env.parse(test_multiple) == result_multiple );
CHECK( env.parse(test) == result );
}
SECTION("loops") {
SECTION("Loops") {
std::string test = "open (% for e in list %)lorem(% endfor %) closing";
json result = {
{{"type", Type::String}, {"text", "open "}},
@@ -51,21 +60,22 @@ TEST_CASE("parse structure") {
}}},
{{"type", Type::String}, {"text", " closing"}}
};
std::string test_nested = "(% for e in list %)(% for b in list2 %)lorem(% endfor %)(% endfor %)";
json result_nested = {
CHECK( env.parse(test) == result );
}
SECTION("Nested loops") {
std::string test = "(% for e in list %)(% for b in list2 %)lorem(% endfor %)(% endfor %)";
json result = {
{{"type", Type::Loop}, {"command", "for e in list"}, {"children", {
{{"type", Type::Loop}, {"command", "for b in list2"}, {"children", {
{{"type", Type::String}, {"text", "lorem"}}
}}}
}}}
};
CHECK( env.parse(test) == result );
CHECK( env.parse(test_nested) == result_nested );
}
SECTION("conditionals") {
SECTION("Basic conditional") {
std::string test = "(% if true %)dfgh(% endif %)";
json result = {
{{"type", Type::Condition}, {"children", {
@@ -74,9 +84,12 @@ TEST_CASE("parse structure") {
}}}
}}}
};
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 = {
CHECK( env.parse(test) == result );
}
SECTION("If/else if/else conditional") {
std::string test = "if: (% if maybe %)first if(% else if perhaps %)first else if(% else if sometimes %)second else if(% else %)test else(% endif %)";
json result = {
{{"type", Type::String}, {"text", "if: "}},
{{"type", Type::Condition}, {"children", {
{{"type", Type::ConditionBranch}, {"command", "if maybe"}, {"children", {
@@ -87,21 +100,25 @@ TEST_CASE("parse structure") {
}}},
{{"type", Type::ConditionBranch}, {"command", "else if sometimes"}, {"children", {
{{"type", Type::String}, {"text", "second else if"}}
}}},
}}},
{{"type", Type::ConditionBranch}, {"command", "else"}, {"children", {
{{"type", Type::String}, {"text", "test else"}}
}}},
}}},
}}}
};
CHECK( env.parse(test) == result );
CHECK( env.parse(test2) == result2 );
}
SECTION("Comments") {
std::string test = "{# lorem ipsum #}";
json result = {{{"type", Type::Comment}, {"text", "lorem ipsum"}}};
CHECK( env.parse(test) == result );
}
}
TEST_CASE("parse json") {
TEST_CASE("Parse json") {
Environment env = Environment();
json data;
data["name"] = "Peter";
data["city"] = "Washington D.C.";
@@ -110,14 +127,14 @@ TEST_CASE("parse json") {
data["brother"]["name"] = "Chris";
data["brother"]["daughters"] = {"Maria", "Helen"};
data["brother"]["daughter0"] = { { "name", "Maria" } };
SECTION("variables from values") {
SECTION("Variables from values") {
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") {
SECTION("Variables from JSON data") {
CHECK( env.parse_variable("name", data) == "Peter" );
CHECK( env.parse_variable("age", data) == 29 );
CHECK( env.parse_variable("names/1", data) == "Seb" );
@@ -127,20 +144,21 @@ TEST_CASE("parse json") {
}
}
TEST_CASE("parse conditions") {
TEST_CASE("Parse conditions") {
Environment env = Environment();
json data;
data["age"] = 29;
data["brother"] = "Peter";
data["father"] = "Peter";
SECTION("elements") {
// CHECK( env.parse_condition("age", data) );
SECTION("Elements") {
CHECK( env.parse_condition("age", data) );
CHECK_FALSE( env.parse_condition("size", data) );
CHECK( env.parse_condition("not size", data) );
}
SECTION("numbers") {
SECTION("Numbers") {
CHECK( env.parse_condition("age == 29", data) );
CHECK( env.parse_condition("age >= 29", data) );
CHECK( env.parse_condition("age <= 29", data) );
@@ -150,9 +168,10 @@ TEST_CASE("parse conditions") {
CHECK_FALSE( env.parse_condition("age < 28", data) );
CHECK_FALSE( env.parse_condition("age < -100.0", data) );
}
SECTION("strings") {
SECTION("Strings") {
CHECK( env.parse_condition("brother == father", data) );
CHECK( env.parse_condition("brother == \"Peter\"", data) );
CHECK_FALSE( env.parse_condition("not brother == father", data) );
}
}
}
+22 -26
View File
@@ -3,22 +3,18 @@
#include "inja.hpp"
using Environment = inja::Environment;
using json = nlohmann::json;
TEST_CASE("string vector join function") {
TEST_CASE("String vector join function") {
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") {
TEST_CASE("Basic search in string") {
std::string input = "lorem ipsum dolor it";
std::regex regex("i(.*)m");
SECTION("basic search from start") {
SECTION("Basic search from start") {
inja::SearchMatch match = inja::search(input, regex, 0);
CHECK( match.found == true );
CHECK( match.position == 6 );
@@ -27,26 +23,26 @@ TEST_CASE("basic search in string") {
CHECK( match.outer == "ipsum" );
CHECK( match.inner == "psu" );
}
SECTION("basic search from position") {
SECTION("Basic search from position") {
inja::SearchMatch match = inja::search(input, regex, 8);
CHECK( match.found == false );
CHECK( match.length == 0 );
}
}
TEST_CASE("search in string with multiple possible regexes") {
TEST_CASE("Search in string with multiple possible regexes") {
std::string input = "lorem ipsum dolor amit estas tronum.";
SECTION("basic 1") {
SECTION("Basic 1") {
std::vector<std::string> regex_patterns = { "tras", "do(\\w*)or", "es(\\w*)as", "ip(\\w*)um" };
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") {
SECTION("Basic 2") {
std::vector<std::string> regex_patterns = { "tras", "ip(\\w*)um", "do(\\w*)or", "es(\\w*)as" };
inja::SearchMatchVector match = inja::search(input, regex_patterns, 0);
CHECK( match.regex_number == 1 );
@@ -55,34 +51,34 @@ TEST_CASE("search in string with multiple possible regexes") {
}
}
TEST_CASE("search on level") {
TEST_CASE("Search on level") {
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("first instance") {
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" );
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("second instance") {
SECTION("Second instance") {
inja::SearchMatch open_match = inja::search(input, regex_statement, 4);
CHECK( open_match.position == 8 );
CHECK( open_match.end_position == 16 );
CHECK( open_match.inner == "up" );
inja::SearchClosedMatch match = inja::search_closed_match_on_level(input, regex_statement, regex_level_up, regex_level_down, regex_search, open_match);
CHECK( match.open_match.position == 8 );
CHECK( match.open_match.end_position== 16 );
CHECK( match.close_match.position == 20 );
@@ -92,4 +88,4 @@ TEST_CASE("search on level") {
CHECK( match.outer == "(% up %)Test(% N1 %)" );
CHECK( match.inner == "Test" );
}
}
}