replace json.hpp with rapidjson in unit tests
This commit is contained in:
parent
76514517e8
commit
9434021715
6
.gitmodules
vendored
6
.gitmodules
vendored
@ -1,6 +1,3 @@
|
||||
[submodule "vendor/json.hpp"]
|
||||
path = vendor/json.hpp
|
||||
url = https://github.com/no1msd/json.hpp.git
|
||||
[submodule "vendor/Catch"]
|
||||
path = vendor/Catch
|
||||
url = https://github.com/philsquared/Catch.git
|
||||
@ -13,3 +10,6 @@
|
||||
[submodule "vendor/headerize"]
|
||||
path = vendor/headerize
|
||||
url = https://github.com/no1msd/headerize.git
|
||||
[submodule "vendor/rapidjson"]
|
||||
path = vendor/rapidjson
|
||||
url = https://github.com/miloyip/rapidjson.git
|
||||
|
@ -3,7 +3,7 @@ find_package(Boost 1.54 REQUIRED)
|
||||
include_directories(
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
${CMAKE_SOURCE_DIR}/vendor/Catch/single_include
|
||||
${CMAKE_SOURCE_DIR}/vendor/json.hpp/include
|
||||
${CMAKE_SOURCE_DIR}/vendor/rapidjson/include
|
||||
${Boost_INCLUDE_DIR})
|
||||
|
||||
file(GLOB data_files RELATIVE
|
||||
@ -40,9 +40,7 @@ foreach(specs_file ${specs_files})
|
||||
list(APPEND specsargs "-i${specs_file}")
|
||||
string(REGEX REPLACE "\\.json" "" test_name "${specs_file}")
|
||||
string(REGEX REPLACE "~" "" test_name "${test_name}")
|
||||
if(NOT APPLE)
|
||||
list(APPEND tests "specs_${test_name}")
|
||||
endif()
|
||||
list(APPEND tests "specs_${test_name}")
|
||||
endforeach(specs_file)
|
||||
|
||||
add_custom_command(
|
||||
|
@ -1,7 +1,7 @@
|
||||
#define CATCH_CONFIG_MAIN
|
||||
|
||||
#include "catch.hpp"
|
||||
#include "json.hpp"
|
||||
#include "rapidjson/document.h"
|
||||
#include "mstch/mstch.hpp"
|
||||
#include "test_context.hpp"
|
||||
#include "test_data.hpp"
|
||||
@ -10,6 +10,52 @@
|
||||
|
||||
using namespace mstchtest;
|
||||
|
||||
mstch::node to_value(const rapidjson::Value& val) {
|
||||
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{};
|
||||
}
|
||||
|
||||
mstch::array to_array(const rapidjson::Value& val);
|
||||
|
||||
mstch::map to_object(const rapidjson::Value& val) {
|
||||
mstch::map ret;
|
||||
for (auto i = val.MemberBegin(); i != val.MemberEnd(); ++i) {
|
||||
if (i->value.IsArray())
|
||||
ret.insert(std::make_pair(i->name.GetString(), to_array(i->value)));
|
||||
else if (i->value.IsObject())
|
||||
ret.insert(std::make_pair(i->name.GetString(), to_object(i->value)));
|
||||
else
|
||||
ret.insert(std::make_pair(i->name.GetString(), to_value(i->value)));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
mstch::array to_array(const rapidjson::Value& val) {
|
||||
mstch::array ret;
|
||||
for (auto i = val.Begin(); i != val.End(); ++i) {
|
||||
if (i->IsArray())
|
||||
ret.push_back(to_array(*i));
|
||||
else if (i->IsObject())
|
||||
ret.push_back(to_object(*i));
|
||||
else
|
||||
ret.push_back(to_value(*i));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
mstch::node parse_with_rapidjson(const std::string& str) {
|
||||
rapidjson::Document doc;
|
||||
doc.Parse(str.c_str());
|
||||
return to_object(doc);
|
||||
}
|
||||
|
||||
#define MSTCH_PARTIAL_TEST(x) TEST_CASE(#x) { \
|
||||
REQUIRE(x ## _txt == mstch::render(x ## _mustache, x ## _data, {{"partial", x ## _partial}})); \
|
||||
}
|
||||
@ -20,15 +66,15 @@ using namespace mstchtest;
|
||||
|
||||
#define SPECS_TEST(x) TEST_CASE("specs_" #x) { \
|
||||
using boost::get; \
|
||||
auto data = json::parse<mstch::node,mstch::map,mstch::array>(x ## _json); \
|
||||
for(auto& test_item: get<mstch::array>(get<mstch::map>(data)["tests"])) {\
|
||||
auto data = parse_with_rapidjson(x ## _json); \
|
||||
for (auto& test_item: get<mstch::array>(get<mstch::map>(data)["tests"])) {\
|
||||
auto test = get<mstch::map>(test_item); \
|
||||
std::map<std::string,std::string> partials; \
|
||||
if(test.count("partials")) \
|
||||
for(auto& partial_item: get<mstch::map>(test["partials"])) \
|
||||
if (test.count("partials")) \
|
||||
for (auto& partial_item: get<mstch::map>(test["partials"])) \
|
||||
partials.insert(std::make_pair(partial_item.first, get<std::string>(partial_item.second))); \
|
||||
for(auto& data_item: get<mstch::map>(test["data"])) \
|
||||
if(data_item.first == "lambda") \
|
||||
for (auto& data_item: get<mstch::map>(test["data"])) \
|
||||
if (data_item.first == "lambda") \
|
||||
data_item.second = specs_lambdas[get<std::string>(test["name"])]; \
|
||||
SECTION(get<std::string>(test["name"])) \
|
||||
REQUIRE(mstch::render( \
|
||||
@ -97,7 +143,6 @@ MSTCH_TEST(unescaped)
|
||||
MSTCH_TEST(whitespace)
|
||||
MSTCH_TEST(zero_view)
|
||||
|
||||
#ifndef __APPLE__
|
||||
SPECS_TEST(comments)
|
||||
SPECS_TEST(delimiters)
|
||||
SPECS_TEST(interpolation)
|
||||
@ -105,4 +150,3 @@ SPECS_TEST(inverted)
|
||||
SPECS_TEST(partials)
|
||||
SPECS_TEST(sections)
|
||||
SPECS_TEST(lambdas)
|
||||
#endif
|
||||
|
1
vendor/json.hpp
vendored
1
vendor/json.hpp
vendored
@ -1 +0,0 @@
|
||||
Subproject commit f5fd9683b9753a040063d911014b8fc2bbc0501e
|
1
vendor/rapidjson
vendored
Submodule
1
vendor/rapidjson
vendored
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 54eb069f0618bc01205434e5c607d38e96a4ba59
|
Loading…
Reference in New Issue
Block a user