Skip to content

Commit

Permalink
yaml-cpp: 0.6.3 -> 0.7.0, rapidjson Map bugfix, and unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
injae committed Feb 28, 2022
1 parent af8a402 commit 5200f57
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 6 deletions.
32 changes: 31 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ find_cppkg(benchmark 1.5.2 MODULE benchmark::benchmark TYPE lib OPTIONAL OFF)
find_cppkg(RapidJSON 1.1.1 MODULE rapidjson TYPE lib OPTIONAL OFF)
find_cppkg(nlohmann_json 3.10.5 MODULE nlohmann_json::nlohmann_json TYPE lib OPTIONAL OFF)
find_cppkg(toml11 3.7.0 MODULE toml11::toml11 TYPE lib OPTIONAL OFF)
find_cppkg(yaml-cpp 0.6.3 MODULE yaml-cpp TYPE lib OPTIONAL OFF)
find_cppkg(yaml-cpp 0.7.0 MODULE yaml-cpp TYPE lib OPTIONAL OFF)
find_cppkg(fmt 8.0.1 MODULE fmt::fmt-header-only TYPE lib OPTIONAL OFF)
find_cppkg(magic_enum 0.7.3 MODULE magic_enum::magic_enum TYPE lib)
find_cppkg(nameof 0.10.0 MODULE nameof::nameof TYPE lib)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
find_cppkg(Catch2 2.9.1 MODULE Catch2::Catch2 TYPE lib)
find_cppkg(benchmark 1.5.2 MODULE benchmark::benchmark TYPE lib OPTIONAL OFF)
endif()


cppm_target_define(serdepp STATIC
SOURCES
Expand Down Expand Up @@ -84,6 +89,23 @@ SOURCES

endif()

cppm_unit_test_area()
if(SERDEPP_BUILD_TESTING)

cppm_target_define(unittest BINARY
SOURCES
tests/rapid_json.cpp
tests/catch_main.cpp
tests/yaml_cpp.cpp
tests/toml11.cpp
tests/reflection.cpp
tests/nlohmann_json.cpp
tests/test_struct.hpp
)

endif()
end_cppm_unit_test_area()

if(SERDEPP_BUILD_BENCHMARKS)

cppm_target_define(benchmark BINARY
Expand All @@ -105,6 +127,9 @@ endif()

set(serdepp_global_deps PRIVATE benchmark
PUBLIC RapidJSON nlohmann_json toml11 yaml-cpp fmt magic_enum nameof)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
list(APPEND serdepp_global_deps PRIVATE Catch2 benchmark)
endif()
cppm_target_dependencies(serdepp
${serdepp_global_deps})

Expand Down Expand Up @@ -152,6 +177,10 @@ cppm_target_dependencies(pointer
${serdepp_global_deps}
serdepp)

cppm_target_dependencies(unittest
${serdepp_global_deps}
serdepp)

cppm_target_dependencies(benchmark
${serdepp_global_deps}
serdepp)
Expand All @@ -166,6 +195,7 @@ cppm_target_dependencies(syntax_sugar_benchmark


cppm_target_install(serdepp)
cppm_target_install(unittest)
cppm_target_install(serde_example1)
cppm_target_install(serde_example)
cppm_target_install(simple_example)
Expand Down
11 changes: 8 additions & 3 deletions cppm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@
name = "syntax_sugar_benchmark"
source = ["benchmark/syntax_benchmark.cpp"]

[[tests]]
name = "unittest"
source = ["tests/.*"]

[dependencies]
nameof = { version="0.10.0", link="public"}
magic_enum = { version= "0.7.3", link="public"}
fmt = {version="8.0.1", link="public", optional=true}
yaml-cpp = { version="0.6.3", link="public", optional=true}
yaml-cpp = { version="0.7.0", link="public", optional=true}
toml11 = { version="3.7.0", link="public", optional=true}
nlohmann_json = { version="3.10.5", link="public", optional=true}
RapidJSON = { version="1.1.1", link="public", optional=true}
Expand All @@ -97,5 +101,6 @@
#simdjson = {version="0.9.7", link="public"}
#ryml = { version= "0.2.0", link="public", optional=true}
#sol2 = { version="3.2.2"}
#[dev-dependencies]
# benchmark = {version="1.5.2", optional=true}
[dev-dependencies]
benchmark = {version="1.5.2", optional=true}
Catch2 = "2.9.1"
2 changes: 1 addition & 1 deletion examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ int main()
"b" : "2",
"c" : "3" },
"nm" : { "a" : {"version" : "hello" },
"b" : "hello2" }
"b" : "hello2" }
})"_json;

// try {
Expand Down
5 changes: 4 additions & 1 deletion include/serdepp/adaptor/rapidjson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#ifndef __SERDEPP_ADAPTOR_RAPID_JSON_HPP__
#define __SERDEPP_ADAPTOR_RAPID_JSON_HPP__

#define RAPIDJSON_HAS_STDSTRING 1

#include "serdepp/serializer.hpp"
#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
Expand Down Expand Up @@ -221,8 +223,9 @@ namespace serde {
map.SetObject();
for(auto& [key_, value_] : data) {
Value val;
Value skey(key_.c_str(), key_.length(), s.GetAllocator());
val.CopyFrom(serialize<rapidjson_type>(value_), s.GetAllocator());
map.AddMember(StringRef(key_.c_str(), key_.length()), val.Move(), s.GetAllocator());
map.AddMember(skey, val.Move(), s.GetAllocator());
}
if(key.empty()) {
s.CopyFrom(map.Move(), s.GetAllocator());
Expand Down
7 changes: 7 additions & 0 deletions tests/catch_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define CATCH_CONFIG_MAIN

#include <catch2/catch.hpp>

TEST_CASE("1: test (pass)", "[multi-file:1]") {
REQUIRE(1==1);
}
23 changes: 23 additions & 0 deletions tests/nlohmann_json.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#include <catch2/catch.hpp>
#include "test_struct.hpp"
#include <serdepp/adaptor/nlohmann_json.hpp>


nlohmann::json json_v = R"({
"str" : "hello",
"i": 10,
"vec": [ "one", "two", "three"],
"sm": { "one" : "tone", "two" : "ttwo"},
"opt": "hello",
"sub" : { "str": "hello" }
})"_json;

using namespace serde;

TEST_CASE("2: nlohmann json struct (pass)", "[multi-file:2]") {
REQUIRE(json_v.dump() == serialize<nlohmann::json>(deserialize<test>(json_v)).dump());
}



28 changes: 28 additions & 0 deletions tests/rapid_json.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <catch2/catch.hpp>
#include "test_struct.hpp"
#include <serdepp/adaptor/rapidjson.hpp>

std::string str(rapidjson::Document& doc) {
using namespace rapidjson;
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
doc.Accept(writer);
return buffer.GetString();
};

using namespace serde;

TEST_CASE("5: toml11 struct (pass)", "[multi-file:5]") {
rapidjson::Document json_v;
json_v.Parse(R"({
"str" : "hello",
"i": 10,
"vec": [ "one", "two", "three"],
"opt": "hello",
"sm": { "one" : "tone", "two" : "ttwo"},
"sub" : { "str": "hello" }
})");
rapidjson::Document json_c = serialize<rapidjson::Document>(deserialize<test>(json_v));

REQUIRE(str(json_v) == str(json_c));
}
18 changes: 18 additions & 0 deletions tests/reflection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <catch2/catch.hpp>
#include "test_struct.hpp"
#include <serdepp/adaptor/reflection.hpp>

using namespace serde;

TEST_CASE("3: reflection struct (pass)", "[multi-file:3]") {
REQUIRE(std::is_same_v<to_tuple_t<test>,
std::tuple<std::string,
int,
std::vector<std::string>,
std::optional<std::string>,
std::optional<std::string>,
std::map<std::string, std::string>,
nested>>);
}


25 changes: 25 additions & 0 deletions tests/test_struct.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <serdepp/serde.hpp>

#pragma once

#ifndef __CATCH_TEST_STRUCT_HPP__
#define __CATCH_TEST_STRUCT_HPP__

struct nested {
DERIVE_SERDE(nested, _SF_(str))
std::string str;
};

struct test {
DERIVE_SERDE(test, _SF_(str)_SF_(i)_SF_(vec)_SF_(opt)_SF_(none_opt)_SF_(sm)_SF_(sub))
std::string str;
int i;
std::vector<std::string> vec;
std::optional<std::string> none_opt;
std::optional<std::string> opt;
std::map<std::string, std::string> sm;
nested sub;
};

#endif

25 changes: 25 additions & 0 deletions tests/toml11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <catch2/catch.hpp>
#include "test_struct.hpp"
#include <serdepp/adaptor/toml11.hpp>

using namespace serde;

using namespace toml::literals;

toml::value toml_vl = R"(
str = "hello"
i = 10
vec = [ "one", "two", "three" ]
opt = "hello"
[sm]
one = "tone"
two = "ttwo"
[sub]
str = "hello"
)"_toml;

TEST_CASE("4: toml11 struct (pass)", "[multi-file:4]") {
REQUIRE(toml_vl == serialize<toml::value>(deserialize<test>(toml_vl)));
}
30 changes: 30 additions & 0 deletions tests/yaml_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <catch2/catch.hpp>
#include "test_struct.hpp"
#include <serdepp/adaptor/yaml-cpp.hpp>


YAML::Node yaml_v = YAML::Load(R"(
str: hello
i: 10
vec:
- one
- two
- three
opt: hello
sm:
one: tone
two: ttwo
sub:
str: hello
)");

using namespace serde;

TEST_CASE("5: yaml-cpp struct (pass)", "[multi-file:5]") {
std::ostringstream origin;
origin << yaml_v;
std::ostringstream convert;
convert << serialize<YAML::Node>(deserialize<test>(yaml_v));

REQUIRE(origin.str() == convert.str());
}
5 changes: 5 additions & 0 deletions thirdparty/Catch2/2.9.1/cppkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Catch2]
version = "2.9.1"
description = "modern, C++-native, header-only, test framework for unit-tests"
module = "Catch2::Catch2"
url="https://github.com/catchorg/Catch2/archive/v2.9.1.tar.gz"
7 changes: 7 additions & 0 deletions thirdparty/yaml-cpp/0.7.0/cppkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[yaml-cpp]
version="0.7.0" #(require)
type="lib" #lib(default) | bin | cmake
description="" #(require)
module="yaml-cpp" #(require) if none_module=true -> no require
flags="-DYAML_CPP_BUILD_TESTS=OFF"
url="https://github.com/jbeder/yaml-cpp/archive/refs/tags/yaml-cpp-0.7.0.zip" #(require)
8 changes: 8 additions & 0 deletions thirdparty/yaml-cpp/0.7.0/yaml-cpp.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Cppkg Base Dependency Downloader
cmake_minimum_required(VERSION 3.6)
project(yaml-cpp-0.6.3-install)

set(CPPM_VERSION ${CPPM_VERSION})
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/cppm_loader.cmake)
download_package(yaml-cpp 0.6.3 URL https://github.com/jbeder/yaml-cpp/archive/yaml-cpp-0.6.3.tar.gz TYPE lib CMAKE_ARGS ${CMAKE_ARGS} -DYAML_CPP_BUILD_TESTS=OFF)

0 comments on commit 5200f57

Please sign in to comment.