2015-04-10 18:56:31 +08:00
|
|
|
#define CATCH_CONFIG_MAIN
|
|
|
|
|
2015-04-12 22:13:21 +08:00
|
|
|
#include "catch.hpp"
|
2015-04-12 21:35:13 +08:00
|
|
|
#include "mstch/mstch.hpp"
|
2020-02-13 03:53:14 +08:00
|
|
|
#include "rapidjson/document.h"
|
2015-05-04 20:29:15 +08:00
|
|
|
#include "specs_data.hpp"
|
2015-05-04 22:14:32 +08:00
|
|
|
#include "specs_lambdas.hpp"
|
2020-02-13 03:53:14 +08:00
|
|
|
#include "test_context.hpp"
|
|
|
|
#include "test_data.hpp"
|
2015-04-10 18:56:31 +08:00
|
|
|
|
2015-04-12 22:13:21 +08:00
|
|
|
using namespace mstchtest;
|
|
|
|
|
2015-10-16 23:08:43 +08:00
|
|
|
mstch::node to_value(const rapidjson::Value& val) {
|
2020-02-13 03:53:14 +08:00
|
|
|
if (val.IsString())
|
|
|
|
return std::string{val.GetString()};
|
|
|
|
if (val.IsBool())
|
|
|
|
return val.GetBool();
|
|
|
|
if (val.IsDouble())
|
|
|
|
return val.GetDouble();
|
|
|
|
if (val.IsInt())
|
|
|
|
return val.GetInt();
|
|
|
|
return mstch::node{};
|
2015-10-16 23:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mstch::array to_array(const rapidjson::Value& val);
|
|
|
|
|
|
|
|
mstch::map to_object(const rapidjson::Value& val) {
|
2020-02-13 03:53:14 +08:00
|
|
|
mstch::map ret;
|
|
|
|
for (auto i = val.MemberBegin(); i != val.MemberEnd(); ++i) {
|
|
|
|
if (i->value.IsArray())
|
|
|
|
ret.insert(std::make_pair(i->name.GetString(),
|
|
|
|
mstch::array_wrapper{to_array(i->value)}));
|
|
|
|
else if (i->value.IsObject())
|
|
|
|
ret.insert(std::make_pair(i->name.GetString(),
|
|
|
|
mstch::map_wrapper{to_object(i->value)}));
|
|
|
|
else
|
|
|
|
ret.insert(std::make_pair(i->name.GetString(), to_value(i->value)));
|
|
|
|
}
|
|
|
|
return ret;
|
2015-10-16 23:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mstch::array to_array(const rapidjson::Value& val) {
|
2020-02-13 03:53:14 +08:00
|
|
|
mstch::array ret;
|
|
|
|
for (auto i = val.Begin(); i != val.End(); ++i) {
|
|
|
|
if (i->IsArray())
|
|
|
|
ret.push_back(mstch::array_wrapper{to_array(*i)});
|
|
|
|
else if (i->IsObject())
|
|
|
|
ret.push_back(mstch::map_wrapper{to_object(*i)});
|
|
|
|
else
|
|
|
|
ret.push_back(to_value(*i));
|
|
|
|
}
|
|
|
|
return ret;
|
2015-10-16 23:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mstch::node parse_with_rapidjson(const std::string& str) {
|
2020-02-13 03:53:14 +08:00
|
|
|
rapidjson::Document doc;
|
|
|
|
doc.Parse(str.c_str());
|
|
|
|
return mstch::map_wrapper{to_object(doc)};
|
2015-10-16 23:08:43 +08:00
|
|
|
}
|
|
|
|
|
2020-02-13 03:53:14 +08:00
|
|
|
#define MSTCH_PARTIAL_TEST(x) \
|
|
|
|
TEST_CASE(#x) { \
|
|
|
|
REQUIRE(x##_txt == mstch::render(x##_mustache, x##_data, {{"partial", x##_partial}})); \
|
|
|
|
}
|
2015-04-12 23:11:49 +08:00
|
|
|
|
2020-02-13 03:53:14 +08:00
|
|
|
#define MSTCH_TEST(x) \
|
|
|
|
TEST_CASE(#x) { REQUIRE(x##_txt == mstch::render(x##_mustache, x##_data)); }
|
2015-04-10 18:56:31 +08:00
|
|
|
|
2020-02-13 03:53:14 +08:00
|
|
|
#define SPECS_TEST(x) \
|
|
|
|
TEST_CASE("specs_" #x) { \
|
|
|
|
auto data = parse_with_rapidjson(x##_json); \
|
|
|
|
for (auto& test_item : std::get<mstch::array_wrapper>( \
|
|
|
|
std::get<mstch::map_wrapper>(data).values["tests"]) \
|
|
|
|
.values) { \
|
|
|
|
auto test = std::get<mstch::map_wrapper>(test_item).values; \
|
|
|
|
std::map<std::string, std::string> partials; \
|
|
|
|
if (test.count("partials")) \
|
|
|
|
for (auto& partial_item : \
|
|
|
|
std::get<mstch::map_wrapper>(test["partials"]).values) \
|
|
|
|
partials.insert( \
|
|
|
|
std::make_pair(partial_item.first, \
|
|
|
|
std::get<std::string>(partial_item.second))); \
|
|
|
|
mstch::map context; \
|
|
|
|
for (auto& data_item : std::get<mstch::map_wrapper>(test["data"]).values) \
|
|
|
|
if (data_item.first == "lambda") \
|
|
|
|
context.insert(std::make_pair( \
|
|
|
|
data_item.first, \
|
|
|
|
specs_lambdas[std::get<std::string>(test["name"])])); \
|
|
|
|
else \
|
|
|
|
context.insert(data_item); \
|
|
|
|
SECTION(std::get<std::string>(test["name"])) \
|
|
|
|
REQUIRE(mstch::render(std::get<std::string>(test["template"]), \
|
|
|
|
mstch::map_wrapper{context}, \
|
|
|
|
partials) == std::get<std::string>(test["expected"])); \
|
|
|
|
} \
|
|
|
|
}
|
2015-05-04 20:29:15 +08:00
|
|
|
|
2015-04-12 22:13:21 +08:00
|
|
|
MSTCH_TEST(ampersand_escape)
|
|
|
|
MSTCH_TEST(apostrophe)
|
|
|
|
MSTCH_TEST(array_of_strings)
|
|
|
|
MSTCH_TEST(backslashes)
|
|
|
|
MSTCH_TEST(bug_11_eating_whitespace)
|
|
|
|
MSTCH_TEST(bug_length_property)
|
2015-04-24 06:35:13 +08:00
|
|
|
MSTCH_TEST(changing_delimiters)
|
2015-04-17 08:07:14 +08:00
|
|
|
MSTCH_TEST(comments)
|
|
|
|
MSTCH_TEST(complex)
|
2015-04-12 22:13:21 +08:00
|
|
|
MSTCH_TEST(context_lookup)
|
2015-04-24 06:35:13 +08:00
|
|
|
MSTCH_TEST(delimiters)
|
2015-04-12 22:13:21 +08:00
|
|
|
MSTCH_TEST(disappearing_whitespace)
|
2015-04-17 08:07:14 +08:00
|
|
|
MSTCH_TEST(dot_notation)
|
2015-04-12 22:13:21 +08:00
|
|
|
MSTCH_TEST(double_render)
|
|
|
|
MSTCH_TEST(empty_list)
|
|
|
|
MSTCH_TEST(empty_sections)
|
|
|
|
MSTCH_TEST(empty_string)
|
|
|
|
MSTCH_TEST(empty_template)
|
2015-04-27 16:37:11 +08:00
|
|
|
MSTCH_TEST(error_eof_in_section)
|
|
|
|
MSTCH_TEST(error_eof_in_tag)
|
2015-04-12 22:13:21 +08:00
|
|
|
MSTCH_TEST(error_not_found)
|
2015-04-17 08:07:14 +08:00
|
|
|
MSTCH_TEST(escaped)
|
2015-04-12 22:13:21 +08:00
|
|
|
MSTCH_TEST(falsy)
|
|
|
|
MSTCH_TEST(falsy_array)
|
|
|
|
MSTCH_TEST(grandparent_context)
|
2015-04-23 21:55:18 +08:00
|
|
|
MSTCH_TEST(higher_order_sections)
|
2015-04-12 22:13:21 +08:00
|
|
|
MSTCH_TEST(implicit_iterator)
|
|
|
|
MSTCH_TEST(included_tag)
|
|
|
|
MSTCH_TEST(inverted_section)
|
|
|
|
MSTCH_TEST(keys_with_questionmarks)
|
|
|
|
MSTCH_TEST(multiline_comment)
|
|
|
|
MSTCH_TEST(nested_dot)
|
2015-04-23 21:55:18 +08:00
|
|
|
MSTCH_TEST(nested_higher_order_sections)
|
2015-04-12 22:13:21 +08:00
|
|
|
MSTCH_TEST(nested_iterating)
|
|
|
|
MSTCH_TEST(nesting)
|
|
|
|
MSTCH_TEST(nesting_same_name)
|
|
|
|
MSTCH_TEST(null_lookup_array)
|
|
|
|
MSTCH_TEST(null_lookup_object)
|
2015-04-21 22:55:12 +08:00
|
|
|
MSTCH_TEST(null_string)
|
2015-04-12 22:13:21 +08:00
|
|
|
MSTCH_TEST(null_view)
|
2015-04-12 23:11:49 +08:00
|
|
|
MSTCH_PARTIAL_TEST(partial_array)
|
|
|
|
MSTCH_PARTIAL_TEST(partial_array_of_partials)
|
|
|
|
MSTCH_PARTIAL_TEST(partial_array_of_partials_implicit)
|
|
|
|
MSTCH_PARTIAL_TEST(partial_empty)
|
2015-04-17 08:07:14 +08:00
|
|
|
MSTCH_PARTIAL_TEST(partial_template)
|
2015-04-21 22:55:12 +08:00
|
|
|
MSTCH_PARTIAL_TEST(partial_view)
|
|
|
|
MSTCH_PARTIAL_TEST(partial_whitespace)
|
2015-04-12 22:13:21 +08:00
|
|
|
MSTCH_TEST(recursion_with_same_names)
|
|
|
|
MSTCH_TEST(reuse_of_enumerables)
|
|
|
|
MSTCH_TEST(section_as_context)
|
2015-04-23 21:55:18 +08:00
|
|
|
MSTCH_PARTIAL_TEST(section_functions_in_partials)
|
2015-04-21 22:55:12 +08:00
|
|
|
MSTCH_TEST(simple)
|
2015-04-12 22:13:21 +08:00
|
|
|
MSTCH_TEST(string_as_context)
|
|
|
|
MSTCH_TEST(two_in_a_row)
|
|
|
|
MSTCH_TEST(two_sections)
|
2015-04-17 08:07:14 +08:00
|
|
|
MSTCH_TEST(unescaped)
|
2015-04-12 22:13:21 +08:00
|
|
|
MSTCH_TEST(whitespace)
|
|
|
|
MSTCH_TEST(zero_view)
|
2015-05-04 20:29:15 +08:00
|
|
|
|
|
|
|
SPECS_TEST(comments)
|
|
|
|
SPECS_TEST(delimiters)
|
|
|
|
SPECS_TEST(interpolation)
|
|
|
|
SPECS_TEST(inverted)
|
|
|
|
SPECS_TEST(partials)
|
|
|
|
SPECS_TEST(sections)
|
2015-05-04 22:14:32 +08:00
|
|
|
SPECS_TEST(lambdas)
|