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

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) );
}
}
}