From 9434021715f4794d19c5ebcc7464143cb27c0d75 Mon Sep 17 00:00:00 2001 From: Daniel Sipka Date: Fri, 16 Oct 2015 17:08:43 +0200 Subject: [PATCH] replace json.hpp with rapidjson in unit tests --- .gitmodules | 6 ++--- test/CMakeLists.txt | 6 ++--- test/test_main.cpp | 62 ++++++++++++++++++++++++++++++++++++++------- vendor/json.hpp | 1 - vendor/rapidjson | 1 + 5 files changed, 59 insertions(+), 17 deletions(-) delete mode 160000 vendor/json.hpp create mode 160000 vendor/rapidjson diff --git a/.gitmodules b/.gitmodules index 0387dda..d252f03 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 02b0d3f..061bc2e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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( diff --git a/test/test_main.cpp b/test/test_main.cpp index 653da60..be0f2b6 100644 --- a/test/test_main.cpp +++ b/test/test_main.cpp @@ -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(x ## _json); \ - for(auto& test_item: get(get(data)["tests"])) {\ + auto data = parse_with_rapidjson(x ## _json); \ + for (auto& test_item: get(get(data)["tests"])) {\ auto test = get(test_item); \ std::map partials; \ - if(test.count("partials")) \ - for(auto& partial_item: get(test["partials"])) \ + if (test.count("partials")) \ + for (auto& partial_item: get(test["partials"])) \ partials.insert(std::make_pair(partial_item.first, get(partial_item.second))); \ - for(auto& data_item: get(test["data"])) \ - if(data_item.first == "lambda") \ + for (auto& data_item: get(test["data"])) \ + if (data_item.first == "lambda") \ data_item.second = specs_lambdas[get(test["name"])]; \ SECTION(get(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 diff --git a/vendor/json.hpp b/vendor/json.hpp deleted file mode 160000 index f5fd968..0000000 --- a/vendor/json.hpp +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f5fd9683b9753a040063d911014b8fc2bbc0501e diff --git a/vendor/rapidjson b/vendor/rapidjson new file mode 160000 index 0000000..54eb069 --- /dev/null +++ b/vendor/rapidjson @@ -0,0 +1 @@ +Subproject commit 54eb069f0618bc01205434e5c607d38e96a4ba59