code cleaning

This commit is contained in:
pantor
2017-08-18 15:38:32 +02:00
parent 134466d0b6
commit c06da24f4d
11 changed files with 315 additions and 368 deletions
+2 -3
View File
@@ -3,12 +3,11 @@
#include "inja.hpp"
using Environment = inja::Environment;
using json = nlohmann::json;
TEST_CASE("Files handling") {
Environment env = Environment();
inja::Environment env = inja::Environment();
json data;
data["name"] = "Jeff";
@@ -26,7 +25,7 @@ TEST_CASE("Files handling") {
}
TEST_CASE("Complete files") {
Environment env = Environment("../test/data/");
inja::Environment env = inja::Environment("../test/data/");
for (std::string test_name : {"simple-file", "nested", "nested-line"}) {
SECTION(test_name) {
+18 -19
View File
@@ -3,30 +3,29 @@
#include "inja.hpp"
using Environment = inja::Environment;
using json = nlohmann::json;
using Type = inja::Parser::Type;
TEST_CASE("Parse structure") {
Environment env = Environment();
inja::Parser parser = inja::Parser();
SECTION("Basic string") {
std::string test = "lorem ipsum";
json result = {{{"type", Type::String}, {"text", "lorem ipsum"}}};
CHECK( env.parse(test) == result );
CHECK( parser.parse(test) == result );
}
SECTION("Empty string") {
std::string test = "";
json result = {};
CHECK( env.parse(test) == result );
CHECK( parser.parse(test) == result );
}
SECTION("Variable") {
std::string test = "{{ name }}";
json result = {{{"type", Type::Variable}, {"command", "name"}}};
CHECK( env.parse(test) == result );
CHECK( parser.parse(test) == result );
}
SECTION("Combined string and variables") {
@@ -36,7 +35,7 @@ TEST_CASE("Parse structure") {
{{"type", Type::Variable}, {"command", "name"}},
{{"type", Type::String}, {"text", "!"}}
};
CHECK( env.parse(test) == result );
CHECK( parser.parse(test) == result );
}
SECTION("Multiple variables") {
@@ -48,11 +47,11 @@ TEST_CASE("Parse structure") {
{{"type", Type::Variable}, {"command", "city"}},
{{"type", Type::String}, {"text", "."}}
};
CHECK( env.parse(test) == result );
CHECK( parser.parse(test) == result );
}
SECTION("Loops") {
std::string test = "open (% for e in list %)lorem(% endfor %) closing";
std::string test = "open {% for e in list %}lorem{% endfor %} closing";
json result = {
{{"type", Type::String}, {"text", "open "}},
{{"type", Type::Loop}, {"command", "for e in list"}, {"children", {
@@ -60,11 +59,11 @@ TEST_CASE("Parse structure") {
}}},
{{"type", Type::String}, {"text", " closing"}}
};
CHECK( env.parse(test) == result );
CHECK( parser.parse(test) == result );
}
SECTION("Nested loops") {
std::string test = "(% for e in list %)(% for b in list2 %)lorem(% endfor %)(% endfor %)";
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", {
@@ -72,11 +71,11 @@ TEST_CASE("Parse structure") {
}}}
}}}
};
CHECK( env.parse(test) == result );
CHECK( parser.parse(test) == result );
}
SECTION("Basic conditional") {
std::string test = "(% if true %)dfgh(% endif %)";
std::string test = "{% if true %}dfgh{% endif %}";
json result = {
{{"type", Type::Condition}, {"children", {
{{"type", Type::ConditionBranch}, {"command", "if true"}, {"children", {
@@ -84,11 +83,11 @@ TEST_CASE("Parse structure") {
}}}
}}}
};
CHECK( env.parse(test) == result );
CHECK( parser.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 %)";
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", {
@@ -106,13 +105,13 @@ TEST_CASE("Parse structure") {
}}},
}}}
};
CHECK( env.parse(test) == result );
CHECK( parser.parse(test) == result );
}
SECTION("Comments") {
std::string test = "{# lorem ipsum #}";
json result = {{{"type", Type::Comment}, {"text", "lorem ipsum"}}};
CHECK( env.parse(test) == result );
CHECK( parser.parse(test) == result );
}
SECTION("Line Statements") {
@@ -126,12 +125,12 @@ lorem ipsum
}}}
}}}
};
CHECK( env.parse(test) == result );
CHECK( parser.parse(test) == result );
}
}
TEST_CASE("Parse json") {
Environment env = Environment();
inja::Environment env = inja::Environment();
json data;
data["name"] = "Peter";
@@ -159,7 +158,7 @@ TEST_CASE("Parse json") {
}
TEST_CASE("Parse conditions") {
Environment env = Environment();
inja::Environment env = inja::Environment();
json data;
data["age"] = 29;
+12 -12
View File
@@ -41,20 +41,20 @@ TEST_CASE("Renderer") {
}
SECTION("Loops") {
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, !" );
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") {
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" );
// CHECK( env.render(R"((% if name in ["Simon", "Tom"] %)Test1(% else if name in ["Peter"] %)Test2(% else %)Test3(% endif %))", data) == "Test2" );
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" );
// CHECK( env.render(R"({% if name in ["Simon", "Tom"] %}Test1{% else if name in ["Peter"] %}Test2{% else %}Test3{% endif %})", data) == "Test2" );
}
}
+43 -49
View File
@@ -3,31 +3,25 @@
#include "inja.hpp"
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") {
std::string input = "lorem ipsum dolor it";
std::regex regex("i(.*)m");
inja::Regex regex("i(.*)m");
SECTION("Basic search from start") {
inja::SearchMatch match = inja::search(input, regex, 0);
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" );
inja::Match match = inja::search(input, regex, 0);
CHECK( match.found() == true );
CHECK( match.position() == 6 );
CHECK( match.length() == 5 );
CHECK( match.end_position() == 11 );
CHECK( match.str() == "ipsum" );
CHECK( match.str(1) == "psu" );
}
SECTION("Basic search from position") {
inja::SearchMatch match = inja::search(input, regex, 8);
CHECK( match.found == false );
CHECK( match.length == 0 );
inja::Match match = inja::search(input, regex, 8);
CHECK( match.found() == false );
CHECK( match.length() == 0 );
}
}
@@ -35,56 +29,56 @@ TEST_CASE("Search in string with multiple possible regexes") {
std::string input = "lorem ipsum dolor amit estas tronum.";
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" );
std::vector<inja::Regex> regex_patterns = { inja::Regex("tras"), inja::Regex("do(\\w*)or"), inja::Regex("es(\\w*)as"), inja::Regex("ip(\\w*)um") };
inja::Match match = inja::search(input, regex_patterns, 0);
CHECK( match.regex_number() == 3 );
CHECK( match.str() == "ipsum" );
CHECK( match.str(1) == "s" );
}
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 );
CHECK( match.outer == "ipsum" );
CHECK( match.inner == "s" );
std::vector<inja::Regex> regex_patterns = { inja::Regex("tras"), inja::Regex("ip(\\w*)um"), inja::Regex("do(\\w*)or"), inja::Regex("es(\\w*)as") };
inja::Match match = inja::search(input, regex_patterns, 0);
CHECK( match.regex_number() == 1 );
CHECK( match.str() == "ipsum" );
CHECK( match.str(1) == "s" );
}
}
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+)");
inja::Regex regex_statement("\\(\\% (.*?) \\%\\)");
inja::Regex regex_level_up("up");
inja::Regex regex_level_down("down");
inja::Regex regex_search("N(\\d+)");
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::Match open_match = inja::search(input, regex_statement, 0);
CHECK( open_match.position() == 0 );
CHECK( open_match.end_position() == 8 );
CHECK( open_match.str(1) == "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 );
inja::MatchClosed 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") {
inja::SearchMatch open_match = inja::search(input, regex_statement, 4);
inja::Match open_match = inja::search(input, regex_statement, 4);
CHECK( open_match.position == 8 );
CHECK( open_match.end_position == 16 );
CHECK( open_match.inner == "up" );
CHECK( open_match.position() == 8 );
CHECK( open_match.end_position() == 16 );
CHECK( open_match.str(1) == "up" );
inja::SearchClosedMatch match = inja::search_closed_match_on_level(input, regex_statement, regex_level_up, regex_level_down, regex_search, open_match);
inja::MatchClosed 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 );
CHECK( match.close_match.end_position == 28 );
CHECK( match.position == 8 );
CHECK( match.end_position == 28 );
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" );
}
+1 -1
View File
@@ -1,2 +1,2 @@
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "catch.hpp"