unit test refactor

This commit is contained in:
Daniel Sipka 2015-04-10 12:56:31 +02:00
parent e31207b9cf
commit b38a107b46
31 changed files with 117 additions and 243 deletions

View File

@ -21,30 +21,32 @@ set(SRC
add_library(mstch STATIC ${SRC}) add_library(mstch STATIC ${SRC})
add_executable(mstch_test test/main.cpp) add_executable(mstch_test test/test_main.cpp)
target_link_libraries(mstch_test mstch) target_link_libraries(mstch_test mstch)
add_executable(filetoheader test/filetoheader.cpp) add_executable(filetoheader test/filetoheader.cpp)
target_link_libraries(filetoheader ${Boost_PROGRAM_OPTIONS_LIBRARY}) target_link_libraries(filetoheader ${Boost_PROGRAM_OPTIONS_LIBRARY})
file(GLOB data_files RELATIVE
"${CMAKE_SOURCE_DIR}/test/data"
"${CMAKE_SOURCE_DIR}/test/data/*.data")
foreach(data_file ${data_files})
list(APPEND genargs "-D${data_file}")
endforeach(data_file)
file(GLOB string_files RELATIVE
"${CMAKE_SOURCE_DIR}/test/data"
"${CMAKE_SOURCE_DIR}/test/data/*.mustache"
"${CMAKE_SOURCE_DIR}/test/data/*.txt")
foreach(string_file ${string_files})
list(APPEND genargs "-S${string_file}")
endforeach(string_file)
add_custom_command( add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/test_data.h OUTPUT ${CMAKE_BINARY_DIR}/test_data.h
COMMAND ${CMAKE_BINARY_DIR}/filetoheader --output ${CMAKE_BINARY_DIR}/test_data.h --namespace mstchtest COMMAND ${CMAKE_BINARY_DIR}/filetoheader --output ${CMAKE_BINARY_DIR}/test_data.h --namespace mstchtest ${genargs}
-Dampersand_escape.data -Sampersand_escape.mustache -Sampersand_escape.txt
-Dapostrophe.data -Sapostrophe.mustache -Sapostrophe.txt
-Darray_of_strings.data -Sarray_of_strings.mustache -Sarray_of_strings.txt
-Dbackslashes.data -Sbackslashes.mustache -Sbackslashes.txt
-Dbug_11_eating_whitespace.data -Sbug_11_eating_whitespace.mustache -Sbug_11_eating_whitespace.txt
-Dbug_length_property.data -Sbug_length_property.mustache -Sbug_length_property.txt
-Dcontext_lookup.data -Scontext_lookup.mustache -Scontext_lookup.txt
-Ddisappearing_whitespace.data -Sdisappearing_whitespace.mustache -Sdisappearing_whitespace.txt
-Ddouble_render.data -Sdouble_render.mustache -Sdouble_render.txt
-Dempty_list.data -Sempty_list.mustache -Sempty_list.txt
-Dempty_sections.data -Sempty_sections.mustache -Sempty_sections.txt
-Dempty_string.data -Sempty_string.mustache -Sempty_string.txt
-Dempty_template.data -Sempty_template.mustache -Sempty_template.txt
-Derror_not_found.data -Serror_not_found.mustache -Serror_not_found.txt
-Dfalsy.data -Sfalsy.mustache -Sfalsy.txt
DEPENDS ${CMAKE_BINARY_DIR}/filetoheader DEPENDS ${CMAKE_BINARY_DIR}/filetoheader
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test/data/) WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test/data/)
set_source_files_properties(${CMAKE_BINARY_DIR}/test_data.h PROPERTIES GENERATED TRUE) set_source_files_properties(${CMAKE_BINARY_DIR}/test_data.h PROPERTIES GENERATED TRUE)

View File

@ -1,8 +1,8 @@
auto data = mstch::object{ mstch::object{
{"list", mstch::array{ {"list", mstch::array{
mstch::array{std::string{""}, std::string{"emptyString"}}, mstch::array{std::string{""}, std::string{"emptyString"}},
mstch::array{mstch::array{}, std::string{"emptyArray"}}, mstch::array{mstch::array{}, std::string{"emptyArray"}},
mstch::array{0, std::string{"zero"}}, mstch::array{0, std::string{"zero"}},
mstch::array{mstch::node{}, std::string{"null"}} mstch::array{mstch::node{}, std::string{"null"}}
}} }}
}; }

View File

@ -1,4 +1,4 @@
auto data = mstch::object{ mstch::object{
{"grand_parent_id", std::string{"grand_parent1"}}, {"grand_parent_id", std::string{"grand_parent1"}},
{"parent_contexts", mstch::array{ {"parent_contexts", mstch::array{
mstch::object{ mstch::object{
@ -16,4 +16,4 @@ auto data = mstch::object{
}} }}
} }
}} }}
}; }

View File

@ -1,8 +1,8 @@
auto data = mstch::object{ mstch::object{
{"data", mstch::object{ {"data", mstch::object{
{"author", mstch::object{ {"author", mstch::object{
{"twitter_id", 819606}, {"twitter_id", 819606},
{"name", std::string{"janl"}} {"name", std::string{"janl"}}
}} }}
}} }}
}; }

View File

@ -1,3 +1,3 @@
auto data = mstch::object{ mstch::object{
{"html", std::string{"I like {{mustache}}"}} {"html", std::string{"I like {{mustache}}"}}
}; }

View File

@ -0,0 +1,3 @@
mstch::object{
{"repos", mstch::array{}}
}

View File

@ -1,3 +0,0 @@
auto data = mstch::object{
{"repos", mstch::array{}}
};

View File

@ -1,5 +1,5 @@
auto data = mstch::object{ mstch::object{
{"person?", mstch::object{ {"person?", mstch::object{
{"name", std::string{"Jon"}} {"name", std::string{"Jon"}}
}} }}
}; }

View File

@ -0,0 +1 @@
mstch::object{}

View File

@ -1 +0,0 @@
auto data = mstch::object{};

View File

@ -0,0 +1 @@
mstch::object{{"name", std::string{"Bruno"}}}

View File

@ -1 +0,0 @@
auto data = mstch::object{{"name", std::string{"Bruno"}}};

View File

@ -1,8 +1,8 @@
auto data = mstch::object{ mstch::object{
{"inner", mstch::array{mstch::object{ {"inner", mstch::array{mstch::object{
{"foo", std::string{"foo"}}, {"foo", std::string{"foo"}},
{"inner", mstch::array{mstch::object{ {"inner", mstch::array{mstch::object{
{"bar", std::string{"bar"}} {"bar", std::string{"bar"}}
}}} }}}
}}} }}}
}; }

View File

@ -1,7 +1,7 @@
auto data = mstch::object{ mstch::object{
{"foo", mstch::array{ {"foo", mstch::array{
mstch::object{{"a", mstch::object{{"b", 1}}}}, mstch::object{{"a", mstch::object{{"b", 1}}}},
mstch::object{{"a", mstch::object{{"b", 2}}}}, mstch::object{{"a", mstch::object{{"b", 2}}}},
mstch::object{{"a", mstch::object{{"b", 3}}}} mstch::object{{"a", mstch::object{{"b", 3}}}}
}} }}
}; }

View File

@ -1,8 +1,8 @@
auto data = mstch::object{ mstch::object{
{"items", mstch::array{ {"items", mstch::array{
mstch::object{ mstch::object{
{"name", std::string{"name"}}, {"name", std::string{"name"}},
{"items", mstch::array{1, 2, 3, 4}} {"items", mstch::array{1, 2, 3, 4}}
} }
}} }}
}; }

View File

@ -1,8 +1,8 @@
auto data = mstch::object{ mstch::object{
{"name", std::string{"David"}}, {"name", std::string{"David"}},
{"twitter", std::string{"@dasilvacontin"}}, {"twitter", std::string{"@dasilvacontin"}},
{"farray", mstch::array{ {"farray", mstch::array{
mstch::array{std::string{"Flor"}, std::string{"@florrts"}}, mstch::array{std::string{"Flor"}, std::string{"@florrts"}},
mstch::array{std::string{"Miquel"}, mstch::node{}}, mstch::array{std::string{"Miquel"}, mstch::node{}},
}} }}
}; }

View File

@ -1,4 +1,4 @@
auto data = mstch::object{ mstch::object{
{"name", std::string{"David"}}, {"name", std::string{"David"}},
{"twitter", std::string{"@dasilvacontin"}}, {"twitter", std::string{"@dasilvacontin"}},
{"fobject", mstch::array{ {"fobject", mstch::array{
@ -11,4 +11,4 @@ auto data = mstch::object{
{"twitter", mstch::node{}} {"twitter", mstch::node{}}
} }
}} }}
}; }

View File

@ -1,4 +1,4 @@
auto data = mstch::object{ mstch::object{
{"name", std::string{"Joe"}}, {"name", std::string{"Joe"}},
{"friends", mstch::node{}} {"friends", mstch::node{}}
}; }

View File

@ -1,8 +1,8 @@
auto data = mstch::object{ mstch::object{
{"name", std::string{"name"}}, {"name", std::string{"name"}},
{"description", std::string{"desc"}}, {"description", std::string{"desc"}},
{"terms", mstch::array{ {"terms", mstch::array{
mstch::object{{"name", std::string{"t1"}}, {"index", 0}}, mstch::object{{"name", std::string{"t1"}}, {"index", 0}},
mstch::object{{"name", std::string{"t2"}}, {"index", 1}} mstch::object{{"name", std::string{"t2"}}, {"index", 1}}
}} }}
}; }

View File

@ -1,6 +1,6 @@
auto data = mstch::object{ mstch::object{
{"terms", mstch::array{ {"terms", mstch::array{
mstch::object{{"name", std::string{"t1"}}, {"index", 0}}, mstch::object{{"name", std::string{"t1"}}, {"index", 0}},
mstch::object{{"name", std::string{"t2"}}, {"index", 1}} mstch::object{{"name", std::string{"t2"}}, {"index", 1}}
}} }}
}; }

View File

@ -1,4 +1,4 @@
auto data = mstch::object{ mstch::object{
{"a_object", mstch::object{ {"a_object", mstch::object{
{"title", std::string{"this is an object"}}, {"title", std::string{"this is an object"}},
{"description", std::string{"one of its attributes is a list"}}, {"description", std::string{"one of its attributes is a list"}},
@ -7,4 +7,4 @@ auto data = mstch::object{
mstch::object{{"label", std::string{"listitem2"}}} mstch::object{{"label", std::string{"listitem2"}}}
}} }}
}} }}
}; }

View File

@ -1,4 +1,4 @@
auto data = mstch::object{ mstch::object{
{"a_string", std::string{"aa"}}, {"a_string", std::string{"aa"}},
{"a_list", mstch::array{std::string{"a"},std::string{"b"},std::string{"c"}}} {"a_list", mstch::array{std::string{"a"},std::string{"b"},std::string{"c"}}}
}; }

View File

@ -1,4 +1,4 @@
auto data = mstch::object{ mstch::object{
{"name", std::string{"Joe"}}, {"name", std::string{"Joe"}},
{"greeting", std::string{"Welcome"}} {"greeting", std::string{"Welcome"}}
}; }

View File

@ -0,0 +1 @@
mstch::object{}

View File

@ -1 +0,0 @@
auto data = mstch::object{};

View File

@ -1,4 +1,4 @@
auto data = mstch::object{ mstch::object{
{"tag1", std::string{"Hello"}}, {"tag1", std::string{"Hello"}},
{"tag2", std::string{"World"}} {"tag2", std::string{"World"}}
}; }

1
test/data/zero_view.data Normal file
View File

@ -0,0 +1 @@
mstch::object{{"nums", mstch::array{0, 1, 2}}}

View File

@ -1 +0,0 @@
auto data = mstch::object{{"nums", mstch::array{0, 1, 2}}};

View File

@ -59,17 +59,21 @@ int main(int argc, char* argv[]) {
if(vm.count("namespace")) if(vm.count("namespace"))
output << "namespace " << vm["namespace"].as<std::string>() << " {" << std::endl; output << "namespace " << vm["namespace"].as<std::string>() << " {" << std::endl;
if(vm.count("input-string")) {
for (auto &string_filename: vm["input-string"].as<std::vector<std::string>>()) { for (auto &string_filename: vm["input-string"].as<std::vector<std::string>>()) {
std::ifstream input(string_filename, std::ios::in); std::ifstream input(string_filename, std::ios::in);
wrap_string(input, output, boost::replace_all_copy(string_filename, ".", "_")); wrap_string(input, output, boost::replace_all_copy(string_filename, ".", "_"));
input.close(); input.close();
} }
}
if(vm.count("input-data")) {
for(auto& data_filename: vm["input-data"].as<std::vector<std::string>>()) { for(auto& data_filename: vm["input-data"].as<std::vector<std::string>>()) {
std::ifstream input(data_filename, std::ios::in); std::ifstream input(data_filename, std::ios::in);
wrap_data(input, output, boost::replace_all_copy(data_filename, ".", "_")); wrap_data(input, output, boost::replace_all_copy(data_filename, ".", "_"));
input.close(); input.close();
} }
}
if(vm.count("namespace")) if(vm.count("namespace"))
output << "}" << std::endl; output << "}" << std::endl;

View File

@ -1,177 +0,0 @@
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "mstch.h"
#include "test_data.h"
#define MSTCH_TEST(x,y) TEST_CASE(x) { REQUIRE(y ## _txt == mstch::render(y ## _mustache, y ## _data)); }
MSTCH_TEST("Ampersand escape", mstchtest::ampersand_escape)
MSTCH_TEST("Apostrophe", mstchtest::apostrophe)
MSTCH_TEST("Array of strings", mstchtest::array_of_strings)
MSTCH_TEST("Backslashes", mstchtest::backslashes)
MSTCH_TEST("Eating whitespace", mstchtest::bug_11_eating_whitespace)
MSTCH_TEST("Length property", mstchtest::bug_length_property)
MSTCH_TEST("Context lookup", mstchtest::context_lookup)
MSTCH_TEST("Disappearing whitespace", mstchtest::disappearing_whitespace)
MSTCH_TEST("Double render", mstchtest::double_render)
MSTCH_TEST("Empty list", mstchtest::empty_list)
MSTCH_TEST("Empty sections", mstchtest::empty_sections)
MSTCH_TEST("Empty string", mstchtest::empty_string)
MSTCH_TEST("Empty template", mstchtest::empty_template)
MSTCH_TEST("Error not found", mstchtest::error_not_found)
MSTCH_TEST("Falsy", mstchtest::falsy)
/*TEST_CASE("Falsy array") {
#include "data/falsy_array.h"
auto tpl = file_to_string("data/falsy_array.mustache");
auto exp = file_to_string("data/falsy_array.txt");
REQUIRE(exp == mstch::render(tpl, data));
}*/
/*TEST_CASE("Grandparent context") {
#include "data/grandparent_context.h"
auto tpl = file_to_string("data/grandparent_context.mustache");
auto exp = file_to_string("data/grandparent_context.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Implicit iterator") {
#include "data/implicit_iterator.h"
auto tpl = file_to_string("data/implicit_iterator.mustache");
auto exp = file_to_string("data/implicit_iterator.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Included tag") {
#include "data/included_tag.h"
auto tpl = file_to_string("data/included_tag.mustache");
auto exp = file_to_string("data/included_tag.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Inverted section") {
#include "data/inverted_section.h"
auto tpl = file_to_string("data/inverted_section.mustache");
auto exp = file_to_string("data/inverted_section.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Keys with questionmarks") {
#include "data/keys_with_questionmarks.h"
auto tpl = file_to_string("data/keys_with_questionmarks.mustache");
auto exp = file_to_string("data/keys_with_questionmarks.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Multiline comment") {
#include "data/multiline_comment.h"
auto tpl = file_to_string("data/multiline_comment.mustache");
auto exp = file_to_string("data/multiline_comment.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Nested dot") {
#include "data/nested_dot.h"
auto tpl = file_to_string("data/nested_dot.mustache");
auto exp = file_to_string("data/nested_dot.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Nested iterating") {
#include "data/nested_iterating.h"
auto tpl = file_to_string("data/nested_iterating.mustache");
auto exp = file_to_string("data/nested_iterating.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Nesting") {
#include "data/nesting.h"
auto tpl = file_to_string("data/nesting.mustache");
auto exp = file_to_string("data/nesting.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Nesting same name") {
#include "data/nesting_same_name.h"
auto tpl = file_to_string("data/nesting_same_name.mustache");
auto exp = file_to_string("data/nesting_same_name.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Null lookup array") {
#include "data/null_lookup_array.h"
auto tpl = file_to_string("data/null_lookup_array.mustache");
auto exp = file_to_string("data/null_lookup_array.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Null lookup object") {
#include "data/null_lookup_object.h"
auto tpl = file_to_string("data/null_lookup_object.mustache");
auto exp = file_to_string("data/null_lookup_object.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Null view") {
#include "data/null_view.h"
auto tpl = file_to_string("data/null_view.mustache");
auto exp = file_to_string("data/null_view.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Recursion with same names") {
#include "data/recursion_with_same_names.h"
auto tpl = file_to_string("data/recursion_with_same_names.mustache");
auto exp = file_to_string("data/recursion_with_same_names.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Reuse of enumerables") {
#include "data/reuse_of_enumerables.h"
auto tpl = file_to_string("data/reuse_of_enumerables.mustache");
auto exp = file_to_string("data/reuse_of_enumerables.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Section as context") {
#include "data/section_as_context.h"
auto tpl = file_to_string("data/section_as_context.mustache");
auto exp = file_to_string("data/section_as_context.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("String as context") {
#include "data/string_as_context.h"
auto tpl = file_to_string("data/string_as_context.mustache");
auto exp = file_to_string("data/string_as_context.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Two in a row") {
#include "data/two_in_a_row.h"
auto tpl = file_to_string("data/two_in_a_row.mustache");
auto exp = file_to_string("data/two_in_a_row.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Two sections") {
#include "data/two_sections.h"
auto tpl = file_to_string("data/two_sections.mustache");
auto exp = file_to_string("data/two_sections.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Whitespace") {
#include "data/whitespace.h"
auto tpl = file_to_string("data/whitespace.mustache");
auto exp = file_to_string("data/whitespace.txt");
REQUIRE(exp == mstch::render(tpl, data));
}
TEST_CASE("Zero view") {
#include "data/zero_view.h"
auto tpl = file_to_string("data/zero_view.mustache");
auto exp = file_to_string("data/zero_view.txt");
REQUIRE(exp == mstch::render(tpl, data));
}*/

45
test/test_main.cpp Normal file
View File

@ -0,0 +1,45 @@
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "mstch.h"
#include "test_data.h"
#define MSTCH_TEST(x,y) TEST_CASE(x) { REQUIRE(y ## _txt == mstch::render(y ## _mustache, y ## _data)); }
MSTCH_TEST("Ampersand escape", mstchtest::ampersand_escape)
MSTCH_TEST("Apostrophe", mstchtest::apostrophe)
MSTCH_TEST("Array of strings", mstchtest::array_of_strings)
MSTCH_TEST("Backslashes", mstchtest::backslashes)
MSTCH_TEST("Eating whitespace", mstchtest::bug_11_eating_whitespace)
MSTCH_TEST("Length property", mstchtest::bug_length_property)
MSTCH_TEST("Context lookup", mstchtest::context_lookup)
MSTCH_TEST("Disappearing whitespace", mstchtest::disappearing_whitespace)
MSTCH_TEST("Double render", mstchtest::double_render)
MSTCH_TEST("Empty list", mstchtest::empty_list)
MSTCH_TEST("Empty sections", mstchtest::empty_sections)
MSTCH_TEST("Empty string", mstchtest::empty_string)
MSTCH_TEST("Empty template", mstchtest::empty_template)
MSTCH_TEST("Error not found", mstchtest::error_not_found)
MSTCH_TEST("Falsy", mstchtest::falsy)
//MSTCH_TEST("Falsy array", mstchtest::falsy_array)
MSTCH_TEST("Grandparent context", mstchtest::grandparent_context)
MSTCH_TEST("Implicit iterator", mstchtest::implicit_iterator)
MSTCH_TEST("Included tag", mstchtest::included_tag)
MSTCH_TEST("Inverted section", mstchtest::inverted_section)
MSTCH_TEST("Keys with questionsmarks", mstchtest::keys_with_questionmarks)
MSTCH_TEST("Multiline comment", mstchtest::multiline_comment)
MSTCH_TEST("Nested dot", mstchtest::nested_dot)
MSTCH_TEST("Nested iterating", mstchtest::nested_iterating)
MSTCH_TEST("Nesting", mstchtest::nesting)
MSTCH_TEST("Nesting same name", mstchtest::nesting_same_name)
//MSTCH_TEST("Null lookup array", mstchtest::null_lookup_array)
MSTCH_TEST("Null lookup object", mstchtest::null_lookup_object)
MSTCH_TEST("Null view", mstchtest::null_view)
MSTCH_TEST("Recursion with same names", mstchtest::recursion_with_same_names)
MSTCH_TEST("Reuse of enumerables", mstchtest::reuse_of_enumerables)
MSTCH_TEST("Section as context", mstchtest::section_as_context)
MSTCH_TEST("String as context", mstchtest::string_as_context)
MSTCH_TEST("Two in a row", mstchtest::two_in_a_row)
MSTCH_TEST("Two sections", mstchtest::two_sections)
MSTCH_TEST("Whitespace", mstchtest::whitespace)
//MSTCH_TEST("Zero view", mstchtest::zero_view)