move unit tests to doctest

This commit is contained in:
pantor
2020-03-18 13:41:11 +01:00
parent 10ac6d8839
commit 62d8593e6c
7 changed files with 6041 additions and 13429 deletions

View File

@@ -1,23 +0,0 @@
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

File diff suppressed because it is too large Load Diff

21
test/doctest/LICENSE.txt Normal file
View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2016-2019 Viktor Kirilov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

5971
test/doctest/doctest.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
// Copyright (c) 2019 Pantor. All rights reserved.
#include "catch/catch.hpp"
#include "doctest/doctest.h"
#include "inja/inja.hpp"
@@ -14,22 +14,24 @@ TEST_CASE("loading") {
json data;
data["name"] = "Jeff";
SECTION("Files should be loaded") {
SUBCASE("Files should be loaded") {
CHECK(env.load_file(test_file_directory + "simple.txt") == "Hello {{ name }}.");
}
SECTION("Files should be rendered") {
SUBCASE("Files should be rendered") {
CHECK(env.render_file(test_file_directory + "simple.txt", data) == "Hello Jeff.");
}
SECTION("File includes should be rendered") {
SUBCASE("File includes should be rendered") {
CHECK(env.render_file(test_file_directory + "include.txt", data) == "Answer: Hello Jeff.");
}
SECTION("File error should throw") {
SUBCASE("File error should throw") {
std::string path(test_file_directory + "does-not-exist");
CHECK_THROWS_WITH(env.load_file(path), "[inja.exception.file_error] failed accessing file at '" + path + "'");
CHECK_THROWS_WITH(env.load_json(path), "[inja.exception.file_error] failed accessing file at '" + path + "'");
std::string file_error_message = "[inja.exception.file_error] failed accessing file at '" + path + "'";
CHECK_THROWS_WITH(env.load_file(path), file_error_message.c_str());
CHECK_THROWS_WITH(env.load_json(path), file_error_message.c_str());
}
}
@@ -37,7 +39,7 @@ TEST_CASE("complete-files") {
inja::Environment env {test_file_directory};
for (std::string test_name : {"simple-file", "nested", "nested-line", "html"}) {
SECTION(test_name) {
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"));
}
}
@@ -49,7 +51,7 @@ TEST_CASE("complete-files-whitespace-control") {
env.set_lstrip_blocks(true);
for (std::string test_name : {"nested-whitespace"}) {
SECTION(test_name) {
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"));
}
}
@@ -61,7 +63,7 @@ TEST_CASE("global-path") {
json data;
data["name"] = "Jeff";
SECTION("Files should be written") {
SUBCASE("Files should be written") {
env.write("simple.txt", data, "global-path-result.txt");
CHECK(env_result.load_file("global-path-result.txt") == "Hello Jeff.");
}

View File

@@ -1,6 +1,6 @@
// Copyright (c) 2019 Pantor. All rights reserved.
#include "catch/catch.hpp"
#include "doctest/doctest.h"
#include "inja/inja.hpp"
@@ -32,12 +32,12 @@ TEST_CASE("types") {
data["vars"] = {2, 3, 4, 0, -1, -2, -3};
SECTION("basic") {
SUBCASE("basic") {
CHECK(env.render("", data) == "");
CHECK(env.render("Hello World!", data) == "Hello World!");
}
SECTION("variables") {
SUBCASE("variables") {
CHECK(env.render("Hello {{ name }}!", data) == "Hello Peter!");
CHECK(env.render("{{ name }}", data) == "Peter");
CHECK(env.render("{{name}}", data) == "Peter");
@@ -51,12 +51,12 @@ TEST_CASE("types") {
CHECK_THROWS_WITH(env.render("{{unknown}}", data), "[inja.exception.render_error] variable 'unknown' not found");
}
SECTION("comments") {
SUBCASE("comments") {
CHECK(env.render("Hello{# This is a comment #}!", data) == "Hello!");
CHECK(env.render("{# --- #Todo --- #}", data) == "");
}
SECTION("loops") {
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, !");
@@ -72,7 +72,7 @@ TEST_CASE("types") {
// 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" );
}
SECTION("nested loops") {
SUBCASE("nested loops") {
auto ldata = json::parse(
R"DELIM(
{ "outer" : [
@@ -100,7 +100,7 @@ R"DELIM(
ldata) == "\n0:0::false\n1,2,\n0:1::false\n\n0:2::false\n\n2:0::true\n3,4,\n2:1::true\n5,6,\n\n");
}
SECTION("conditionals") {
SUBCASE("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...");
@@ -118,7 +118,7 @@ R"DELIM(
CHECK_THROWS_WITH(env.render("{% if is_happy %}{% else if is_happy %}{% end if %}", data), "[inja.exception.parser_error] expected statement, got 'end'");
}
SECTION("line statements") {
SUBCASE("line statements") {
CHECK(env.render(R"(## if is_happy
Yeah!
## endif)", data) == R"(Yeah!
@@ -151,7 +151,7 @@ TEST_CASE("functions") {
data["is_sad"] = false;
data["vars"] = {2, 3, 4, 0, -1, -2, -3};
SECTION("upper") {
SUBCASE("upper") {
CHECK(env.render("{{ upper(name) }}", data) == "PETER");
CHECK(env.render("{{ upper( name ) }}", data) == "PETER");
CHECK(env.render("{{ upper(city) }}", data) == "NEW YORK");
@@ -160,95 +160,95 @@ TEST_CASE("functions") {
// CHECK_THROWS_WITH( env.render("{{ upper(true) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be string, but is boolean" );
}
SECTION("lower") {
SUBCASE("lower") {
CHECK(env.render("{{ lower(name) }}", data) == "peter");
CHECK(env.render("{{ lower(city) }}", data) == "new york");
// CHECK_THROWS_WITH( env.render("{{ lower(5.45) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be string, but is number" );
}
SECTION("range") {
SUBCASE("range") {
CHECK(env.render("{{ range(2) }}", data) == "[0,1]");
CHECK(env.render("{{ range(4) }}", data) == "[0,1,2,3]");
// CHECK_THROWS_WITH( env.render("{{ range(name) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be number, but is string" );
}
SECTION("length") {
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" );
}
SECTION("sort") {
SUBCASE("sort") {
CHECK(env.render("{{ sort([3, 2, 1]) }}", data) == "[1,2,3]");
CHECK(env.render("{{ sort([\"bob\", \"charlie\", \"alice\"]) }}", data) == "[\"alice\",\"bob\",\"charlie\"]");
// CHECK_THROWS_WITH( env.render("{{ sort(5) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be array, but is number" );
}
SECTION("at") {
SUBCASE("at") {
CHECK(env.render("{{ at(names, 0) }}", data) == "Jeff");
CHECK(env.render("{{ at(names, i) }}", data) == "Seb");
}
SECTION("first") {
SUBCASE("first") {
CHECK(env.render("{{ first(names) }}", data) == "Jeff");
// CHECK_THROWS_WITH( env.render("{{ first(5) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be array, but is number" );
}
SECTION("last") {
SUBCASE("last") {
CHECK(env.render("{{ last(names) }}", data) == "Tom");
// CHECK_THROWS_WITH( env.render("{{ last(5) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be array, but is number" );
}
SECTION("round") {
SUBCASE("round") {
CHECK(env.render("{{ round(4, 0) }}", data) == "4.0");
CHECK(env.render("{{ round(temperature, 2) }}", data) == "25.68");
// CHECK_THROWS_WITH( env.render("{{ round(name, 2) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be number, but is string" );
}
SECTION("divisibleBy") {
SUBCASE("divisibleBy") {
CHECK(env.render("{{ divisibleBy(50, 5) }}", data) == "true");
CHECK(env.render("{{ divisibleBy(12, 3) }}", data) == "true");
CHECK(env.render("{{ divisibleBy(11, 3) }}", data) == "false");
// CHECK_THROWS_WITH( env.render("{{ divisibleBy(name, 2) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be number, but is string" );
}
SECTION("odd") {
SUBCASE("odd") {
CHECK(env.render("{{ odd(11) }}", data) == "true");
CHECK(env.render("{{ odd(12) }}", data) == "false");
// CHECK_THROWS_WITH( env.render("{{ odd(name) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be number, but is string" );
}
SECTION("even") {
SUBCASE("even") {
CHECK(env.render("{{ even(11) }}", data) == "false");
CHECK(env.render("{{ even(12) }}", data) == "true");
// CHECK_THROWS_WITH( env.render("{{ even(name) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be number, but is string" );
}
SECTION("max") {
SUBCASE("max") {
CHECK(env.render("{{ max([1, 2, 3]) }}", data) == "3");
CHECK(env.render("{{ max([-5.2, 100.2, 2.4]) }}", data) == "100.2");
// CHECK_THROWS_WITH( env.render("{{ max(name) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be array, but is string" );
}
SECTION("min") {
SUBCASE("min") {
CHECK(env.render("{{ min([1, 2, 3]) }}", data) == "1");
CHECK(env.render("{{ min([-5.2, 100.2, 2.4]) }}", data) == "-5.2");
// CHECK_THROWS_WITH( env.render("{{ min(name) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be array, but is string" );
}
SECTION("float") {
SUBCASE("float") {
CHECK(env.render("{{ float(\"2.2\") == 2.2 }}", data) == "true");
CHECK(env.render("{{ float(\"-1.25\") == -1.25 }}", data) == "true");
// CHECK_THROWS_WITH( env.render("{{ max(name) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be array, but is string" );
}
SECTION("int") {
SUBCASE("int") {
CHECK(env.render("{{ int(\"2\") == 2 }}", data) == "true");
CHECK(env.render("{{ int(\"-1.25\") == -1 }}", data) == "true");
// CHECK_THROWS_WITH( env.render("{{ max(name) }}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be array, but is string" );
}
SECTION("default") {
SUBCASE("default") {
CHECK(env.render("{{ default(11, 0) }}", data) == "11");
CHECK(env.render("{{ default(nothing, 0) }}", data) == "0");
CHECK(env.render("{{ default(name, \"nobody\") }}", data) == "Peter");
@@ -257,14 +257,14 @@ TEST_CASE("functions") {
CHECK_THROWS_WITH( env.render("{{ default(surname, lastname) }}", data), "[inja.exception.render_error] variable 'lastname' not found");
}
SECTION("exists") {
SUBCASE("exists") {
CHECK(env.render("{{ exists(\"name\") }}", data) == "true");
CHECK(env.render("{{ exists(\"zipcode\") }}", data) == "false");
CHECK(env.render("{{ exists(name) }}", data) == "false");
CHECK(env.render("{{ exists(property) }}", data) == "true");
}
SECTION("existsIn") {
SUBCASE("existsIn") {
CHECK(env.render("{{ existsIn(brother, \"name\") }}", data) == "true");
CHECK(env.render("{{ existsIn(brother, \"parents\") }}", data) == "false");
CHECK(env.render("{{ existsIn(brother, property) }}", data) == "true");
@@ -273,7 +273,7 @@ TEST_CASE("functions") {
CHECK_THROWS_WITH(env.render("{{ existsIn(brother, sister) }}", data), "[inja.exception.render_error] variable 'sister' not found");
}
SECTION("isType") {
SUBCASE("isType") {
CHECK(env.render("{{ isBoolean(is_happy) }}", data) == "true");
CHECK(env.render("{{ isBoolean(vars) }}", data) == "false");
CHECK(env.render("{{ isNumber(age) }}", data) == "true");
@@ -362,7 +362,7 @@ TEST_CASE("templates") {
data["city"] = "Brunswick";
data["is_happy"] = true;
SECTION("reuse") {
SUBCASE("reuse") {
inja::Environment env;
inja::Template temp = env.parse("{% if is_happy %}{{ name }}{% else %}{{ city }}{% endif %}");
@@ -373,7 +373,7 @@ TEST_CASE("templates") {
CHECK(env.render(temp, data) == "Brunswick");
}
SECTION("include") {
SUBCASE("include") {
inja::Environment env;
inja::Template t1 = env.parse("Hello {{ name }}");
env.include_template("greeting", t1);
@@ -383,7 +383,7 @@ TEST_CASE("templates") {
CHECK_THROWS_WITH(env.parse("{% include \"does-not-exist\" %}!"), "[inja.exception.file_error] failed accessing file at 'does-not-exist'");
}
SECTION("include-in-loop") {
SUBCASE("include-in-loop") {
json loop_data;
loop_data["cities"] = json::array({{{"name", "Munich"}}, {{"name", "New York"}}});
@@ -406,7 +406,7 @@ TEST_CASE("other-syntax") {
data["brother"]["daughter0"] = { { "name", "Maria" } };
data["is_happy"] = true;
SECTION("variables") {
SUBCASE("variables") {
inja::Environment env;
env.set_element_notation(inja::ElementNotation::Pointer);
@@ -418,7 +418,7 @@ TEST_CASE("other-syntax") {
CHECK_THROWS_WITH(env.render("{{unknown/name}}", data), "[inja.exception.render_error] variable 'unknown/name' not found");
}
SECTION("other expression syntax") {
SUBCASE("other expression syntax") {
inja::Environment env;
CHECK(env.render("Hello {{ name }}!", data) == "Hello Peter!");
@@ -429,7 +429,7 @@ TEST_CASE("other-syntax") {
CHECK(env.render("Hello (& name &)!", data) == "Hello Peter!");
}
SECTION("other comment syntax") {
SUBCASE("other comment syntax") {
inja::Environment env;
env.set_comment("(&", "&)");
@@ -437,7 +437,7 @@ TEST_CASE("other-syntax") {
CHECK(env.render("Hello (& Test &)", data) == "Hello ");
}
SECTION("multiple changes") {
SUBCASE("multiple changes") {
inja::Environment env;
env.set_line_statement("$$");
env.set_expression("<%", "%>");

View File

@@ -1,7 +1,7 @@
// Copyright (c) 2019 Pantor. All rights reserved.
#define CATCH_CONFIG_MAIN
#include "catch/catch.hpp"
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"
#include "inja/inja.hpp"