replace json.hpp with rapidjson in unit tests

This commit is contained in:
Daniel Sipka 2015-10-16 17:08:43 +02:00
parent 76514517e8
commit 9434021715
5 changed files with 59 additions and 17 deletions

6
.gitmodules vendored
View file

@ -1,6 +1,3 @@
[submodule "vendor/json.hpp"]
path = vendor/json.hpp
url = https://github.com/no1msd/json.hpp.git
[submodule "vendor/Catch"] [submodule "vendor/Catch"]
path = vendor/Catch path = vendor/Catch
url = https://github.com/philsquared/Catch.git url = https://github.com/philsquared/Catch.git
@ -13,3 +10,6 @@
[submodule "vendor/headerize"] [submodule "vendor/headerize"]
path = vendor/headerize path = vendor/headerize
url = https://github.com/no1msd/headerize.git url = https://github.com/no1msd/headerize.git
[submodule "vendor/rapidjson"]
path = vendor/rapidjson
url = https://github.com/miloyip/rapidjson.git

View file

@ -3,7 +3,7 @@ find_package(Boost 1.54 REQUIRED)
include_directories( include_directories(
${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/vendor/Catch/single_include ${CMAKE_SOURCE_DIR}/vendor/Catch/single_include
${CMAKE_SOURCE_DIR}/vendor/json.hpp/include ${CMAKE_SOURCE_DIR}/vendor/rapidjson/include
${Boost_INCLUDE_DIR}) ${Boost_INCLUDE_DIR})
file(GLOB data_files RELATIVE file(GLOB data_files RELATIVE
@ -40,9 +40,7 @@ foreach(specs_file ${specs_files})
list(APPEND specsargs "-i${specs_file}") list(APPEND specsargs "-i${specs_file}")
string(REGEX REPLACE "\\.json" "" test_name "${specs_file}") string(REGEX REPLACE "\\.json" "" test_name "${specs_file}")
string(REGEX REPLACE "~" "" test_name "${test_name}") string(REGEX REPLACE "~" "" test_name "${test_name}")
if(NOT APPLE) list(APPEND tests "specs_${test_name}")
list(APPEND tests "specs_${test_name}")
endif()
endforeach(specs_file) endforeach(specs_file)
add_custom_command( add_custom_command(

View file

@ -1,7 +1,7 @@
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_MAIN
#include "catch.hpp" #include "catch.hpp"
#include "json.hpp" #include "rapidjson/document.h"
#include "mstch/mstch.hpp" #include "mstch/mstch.hpp"
#include "test_context.hpp" #include "test_context.hpp"
#include "test_data.hpp" #include "test_data.hpp"
@ -10,6 +10,52 @@
using namespace mstchtest; 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) { \ #define MSTCH_PARTIAL_TEST(x) TEST_CASE(#x) { \
REQUIRE(x ## _txt == mstch::render(x ## _mustache, x ## _data, {{"partial", x ## _partial}})); \ 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) { \ #define SPECS_TEST(x) TEST_CASE("specs_" #x) { \
using boost::get; \ using boost::get; \
auto data = json::parse<mstch::node,mstch::map,mstch::array>(x ## _json); \ auto data = parse_with_rapidjson(x ## _json); \
for(auto& test_item: get<mstch::array>(get<mstch::map>(data)["tests"])) {\ for (auto& test_item: get<mstch::array>(get<mstch::map>(data)["tests"])) {\
auto test = get<mstch::map>(test_item); \ auto test = get<mstch::map>(test_item); \
std::map<std::string,std::string> partials; \ std::map<std::string,std::string> partials; \
if(test.count("partials")) \ if (test.count("partials")) \
for(auto& partial_item: get<mstch::map>(test["partials"])) \ for (auto& partial_item: get<mstch::map>(test["partials"])) \
partials.insert(std::make_pair(partial_item.first, get<std::string>(partial_item.second))); \ partials.insert(std::make_pair(partial_item.first, get<std::string>(partial_item.second))); \
for(auto& data_item: get<mstch::map>(test["data"])) \ for (auto& data_item: get<mstch::map>(test["data"])) \
if(data_item.first == "lambda") \ if (data_item.first == "lambda") \
data_item.second = specs_lambdas[get<std::string>(test["name"])]; \ data_item.second = specs_lambdas[get<std::string>(test["name"])]; \
SECTION(get<std::string>(test["name"])) \ SECTION(get<std::string>(test["name"])) \
REQUIRE(mstch::render( \ REQUIRE(mstch::render( \
@ -97,7 +143,6 @@ MSTCH_TEST(unescaped)
MSTCH_TEST(whitespace) MSTCH_TEST(whitespace)
MSTCH_TEST(zero_view) MSTCH_TEST(zero_view)
#ifndef __APPLE__
SPECS_TEST(comments) SPECS_TEST(comments)
SPECS_TEST(delimiters) SPECS_TEST(delimiters)
SPECS_TEST(interpolation) SPECS_TEST(interpolation)
@ -105,4 +150,3 @@ SPECS_TEST(inverted)
SPECS_TEST(partials) SPECS_TEST(partials)
SPECS_TEST(sections) SPECS_TEST(sections)
SPECS_TEST(lambdas) SPECS_TEST(lambdas)
#endif

1
vendor/json.hpp vendored

@ -1 +0,0 @@
Subproject commit f5fd9683b9753a040063d911014b8fc2bbc0501e

1
vendor/rapidjson vendored Submodule

@ -0,0 +1 @@
Subproject commit 54eb069f0618bc01205434e5c607d38e96a4ba59