Conversion to JSON.
This commit is contained in:
parent
a693da20bf
commit
2c3d736d5f
4 changed files with 179 additions and 12 deletions
|
@ -4,20 +4,24 @@ project(UnitTest CXX)
|
|||
|
||||
include(../external/catch2/extras/Catch.cmake)
|
||||
|
||||
add_executable(UnitTest adummy.cpp characteristics_tests.cpp)
|
||||
add_executable(UnitTest adummy.cpp
|
||||
characteristics_tests.cpp
|
||||
../src/billy_objects.cpp
|
||||
billy_objects_tests.cpp
|
||||
)
|
||||
|
||||
set_target_properties(Catch2 UnitTest PROPERTIES
|
||||
set_target_properties(Catch2 UnitTest svector PROPERTIES
|
||||
CXX_STANDARD 17
|
||||
CXX_STANDARD_REQUIRED ON
|
||||
CXX_EXTENSIONS OFF
|
||||
INTERPROCEDURAL_OPTIMIZATION ON
|
||||
INTERPROCEDURAL_OPTIMIZATION_RELEASE ON
|
||||
# UNITY_BUILD ON
|
||||
)
|
||||
target_include_directories(UnitTest PRIVATE ${CMAKE_SOURCE_DIR}/include)
|
||||
target_compile_definitions(UnitTest PRIVATE ${DEF_COMP})
|
||||
target_compile_options(UnitTest PRIVATE ${COMPILE_FLAGS})
|
||||
target_link_options(UnitTest PRIVATE ${LINKER_OPTIONS})
|
||||
target_link_libraries(UnitTest ${LINKER_FLAGS} Catch2::Catch2WithMain nlohmann_json::nlohmann_json)
|
||||
target_link_libraries(UnitTest ${LINKER_FLAGS} spdlog::spdlog_header_only Catch2::Catch2WithMain nlohmann_json::nlohmann_json svector::svector)
|
||||
|
||||
enable_testing()
|
||||
catch_discover_tests(UnitTest WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} REPORTER junit OUTPUT_DIR ${CMAKE_SOURCE_DIR} OUTPUT_PREFIX cppspec- OUTPUT_SUFFIX .xml)
|
||||
|
|
107
Unit testing/billy_objects_tests.cpp
Normal file
107
Unit testing/billy_objects_tests.cpp
Normal file
|
@ -0,0 +1,107 @@
|
|||
//
|
||||
// Created by postaron on 29/02/24.
|
||||
//
|
||||
#include <catch2/catch_all.hpp>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include "billy_objects.hpp"
|
||||
#include "character_sheet.hpp"
|
||||
|
||||
using namespace character;
|
||||
|
||||
namespace character::test {
|
||||
struct DummyObject {
|
||||
BillyObjects::container objects;
|
||||
};
|
||||
|
||||
void to_json(json &j, const DummyObject &a) {
|
||||
j.emplace(std::pair{ BillyObjects::json_key, json::array() });
|
||||
BillyObjects::to_json(j.at(BillyObjects::json_key), a.objects);
|
||||
}
|
||||
|
||||
void from_json(const json &j, DummyObject &dummy) {
|
||||
BillyObjects::from_json(j, dummy.objects);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("[C] Serialize empty BillyObjects", "[serialize][0]") {
|
||||
CharacterSheet sheet;
|
||||
json serializer;
|
||||
|
||||
REQUIRE_NOTHROW(serializer.emplace("sheet", sheet));
|
||||
|
||||
const auto tester = serializer.at("sheet").at(BillyObjects::json_key);
|
||||
serializer.clear();
|
||||
|
||||
REQUIRE_NOTHROW(tester.empty() == true);
|
||||
REQUIRE(tester.empty());
|
||||
|
||||
{
|
||||
std::ofstream file{ "billy_objects_empty.json" };
|
||||
file << tester << std::flush;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("[C] Serialize full BillyObjects", "[serialize][1]") {
|
||||
BillyObjects gestionnaire{};
|
||||
test::DummyObject dummy_object;
|
||||
{
|
||||
CharacterSheet sheet;
|
||||
gestionnaire.push_object(weapons::Sword, sheet);
|
||||
gestionnaire.push_object(tools::Fourche, sheet);
|
||||
gestionnaire.push_object(equipments::MedicKit, sheet);
|
||||
dummy_object.objects = sheet.get_objects();
|
||||
}
|
||||
|
||||
json serializer;
|
||||
REQUIRE_NOTHROW(serializer.emplace("sheet", dummy_object));
|
||||
|
||||
const auto &tester = serializer.at("sheet").at(BillyObjects::json_key);
|
||||
|
||||
REQUIRE_FALSE(tester.empty());
|
||||
|
||||
{
|
||||
std::ofstream file{ "billy_objects_full.json" };
|
||||
file << tester << std::flush;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("[D] Deserialize empty BillyObjects", "[deserialize][0]") {
|
||||
BillyObjects gestionnaire{};
|
||||
test::DummyObject dummy_object;
|
||||
|
||||
const auto deserializer = []() -> json {
|
||||
std::ifstream file{ "billy_objects_empty.json" };
|
||||
return json::parse(file);
|
||||
}();
|
||||
|
||||
REQUIRE_NOTHROW(deserializer.get_to(dummy_object));
|
||||
|
||||
REQUIRE_FALSE(!dummy_object.objects.empty());
|
||||
}
|
||||
|
||||
TEST_CASE("[D] Deserialize full BillyObjects", "[deserialize][1]") {
|
||||
BillyObjects gestionnaire{};
|
||||
test::DummyObject dummy_object;
|
||||
|
||||
const auto deserializer = []() -> json {
|
||||
std::ifstream file{ "billy_objects_full.json" };
|
||||
return json::parse(file);
|
||||
}();
|
||||
|
||||
REQUIRE_NOTHROW(deserializer.get_to(dummy_object));
|
||||
|
||||
REQUIRE_FALSE(dummy_object.objects.empty());
|
||||
|
||||
REQUIRE_FALSE(std::holds_alternative<tools>(dummy_object.objects[0]));
|
||||
REQUIRE(std::holds_alternative<weapons>(dummy_object.objects[0]));
|
||||
REQUIRE(std::get<weapons>(dummy_object.objects[0]) == weapons::Sword);
|
||||
|
||||
REQUIRE_FALSE(std::holds_alternative<weapons>(dummy_object.objects[1]));
|
||||
REQUIRE(std::holds_alternative<tools>(dummy_object.objects[1]));
|
||||
REQUIRE(std::get<tools>(dummy_object.objects[1]) == tools::Fourche);
|
||||
|
||||
REQUIRE_FALSE(std::holds_alternative<weapons>(dummy_object.objects[2]));
|
||||
REQUIRE(std::holds_alternative<equipments>(dummy_object.objects[2]));
|
||||
REQUIRE(std::get<equipments>(dummy_object.objects[2]) == equipments::MedicKit);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue