Updated tests

This commit is contained in:
Pcornat 2024-11-25 16:46:51 +01:00
parent 20bfb96171
commit 7a5d36f6ea
Signed by: Pcornat
GPG Key ID: E0326CC678A00BDD
5 changed files with 57 additions and 31 deletions

View File

@ -2,11 +2,13 @@ cmake_minimum_required(VERSION 3.19 FATAL_ERROR)
project(UnitTest CXX) project(UnitTest CXX)
include(CTest)
include(../external/catch2/extras/Catch.cmake) include(../external/catch2/extras/Catch.cmake)
add_executable(UnitTest adummy.cpp add_executable(UnitTest
characteristics_tests.cpp characteristics_tests.cpp
billy_objects_tests.cpp billy_objects_tests.cpp
sheet_tests.cpp
) )
set_target_properties(Catch2 UnitTest PROPERTIES set_target_properties(Catch2 UnitTest PROPERTIES
@ -14,7 +16,7 @@ set_target_properties(Catch2 UnitTest PROPERTIES
CXX_STANDARD_REQUIRED ON CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF CXX_EXTENSIONS OFF
INTERPROCEDURAL_OPTIMIZATION_RELEASE ON INTERPROCEDURAL_OPTIMIZATION_RELEASE ON
UNITY_BUILD ON # UNITY_BUILD ON
) )
target_include_directories(UnitTest PRIVATE ${CMAKE_SOURCE_DIR}/include) target_include_directories(UnitTest PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_compile_definitions(UnitTest PRIVATE ${DEF_COMP}) target_compile_definitions(UnitTest PRIVATE ${DEF_COMP})

View File

@ -1,3 +0,0 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch_all.hpp>

View File

@ -20,12 +20,14 @@ namespace character::test {
void from_json(const json &j, DummyObject &dummy) { void from_json(const json &j, DummyObject &dummy) {
BillyObjects::from_json(j, dummy.objects); BillyObjects::from_json(j, dummy.objects);
} }
static std::unique_ptr<GenericObject> get_obj(const billyEnums &object) noexcept { static std::unique_ptr<GenericObject> get_obj(const billyEnums &object) noexcept {
return std::visit(overloaded{ return std::visit(overloaded{
[](const weapons &arg) { return std::unique_ptr<GenericObject>(std::make_unique<Weapons>(arg)); }, [](const weapons &arg) { return std::unique_ptr<GenericObject>(std::make_unique<Weapons>(arg)); },
[](const equipments &arg) { return std::unique_ptr<GenericObject>(std::make_unique<Equipments>(arg)); }, [](const equipments &arg) { return std::unique_ptr<GenericObject>(std::make_unique<Equipments>(arg)); },
[](const tools &arg) { return std::unique_ptr<GenericObject>(std::make_unique<Tools>(arg)); }, [](const tools &arg) { return std::unique_ptr<GenericObject>(std::make_unique<Tools>(arg)); },
}, object); },
object);
} }
} }
@ -39,9 +41,7 @@ TEST_CASE("[C] Serialize empty BillyObjects", "[serialize][0]") {
serializer.clear(); serializer.clear();
REQUIRE_NOTHROW(tester.empty() == true); REQUIRE_NOTHROW(tester.empty() == true);
REQUIRE(tester.empty()); REQUIRE(tester.empty()); {
{
std::ofstream file{ "billy_objects_empty.json" }; std::ofstream file{ "billy_objects_empty.json" };
file << tester << std::flush; file << tester << std::flush;
} }
@ -49,8 +49,7 @@ TEST_CASE("[C] Serialize empty BillyObjects", "[serialize][0]") {
TEST_CASE("[C] Serialize full BillyObjects", "[serialize][1]") { TEST_CASE("[C] Serialize full BillyObjects", "[serialize][1]") {
BillyObjects gestionnaire{}; BillyObjects gestionnaire{};
test::DummyObject dummy_object; test::DummyObject dummy_object; {
{
CharacterSheet sheet; CharacterSheet sheet;
REQUIRE(gestionnaire.insert_object(sheet, weapons::Sword)); REQUIRE(gestionnaire.insert_object(sheet, weapons::Sword));
REQUIRE(gestionnaire.insert_object(sheet, tools::Fourche)); REQUIRE(gestionnaire.insert_object(sheet, tools::Fourche));
@ -65,9 +64,7 @@ TEST_CASE("[C] Serialize full BillyObjects", "[serialize][1]") {
const auto &tester = serializer.at("sheet").at(BillyObjects::json_key); const auto &tester = serializer.at("sheet").at(BillyObjects::json_key);
REQUIRE_FALSE(tester.empty()); REQUIRE_FALSE(tester.empty()); {
{
std::ofstream file{ "billy_objects_full.json" }; std::ofstream file{ "billy_objects_full.json" };
file << tester << std::flush; file << tester << std::flush;
} }
@ -88,7 +85,6 @@ TEST_CASE("[D] Deserialize empty BillyObjects", "[deserialize][0]") {
} }
TEST_CASE("[D] Deserialize full BillyObjects", "[deserialize][1]") { TEST_CASE("[D] Deserialize full BillyObjects", "[deserialize][1]") {
// BillyObjects gestionnaire{};
test::DummyObject dummy_object; test::DummyObject dummy_object;
const auto deserializer = []() -> json { const auto deserializer = []() -> json {
@ -112,18 +108,18 @@ TEST_CASE("[D] Deserialize full BillyObjects", "[deserialize][1]") {
TEST_CASE("[D] Pushing popping BillyObjects", "[pushpop][0]") { TEST_CASE("[D] Pushing popping BillyObjects", "[pushpop][0]") {
constexpr std::array<billyEnums, 12> all_objects{ constexpr std::array<billyEnums, 12> all_objects{
weapons::Sword, weapons::Sword,
weapons::Lance, weapons::Lance,
weapons::Morgenstern, weapons::Morgenstern,
weapons::Bow, weapons::Bow,
equipments::Chainmail, equipments::Chainmail,
equipments::CookingPot, equipments::CookingPot,
equipments::PamphletTourist, equipments::PamphletTourist,
equipments::MedicKit, equipments::MedicKit,
tools::Fourche, tools::Fourche,
tools::Dagger, tools::Dagger,
tools::RockClimbingKit, tools::RockClimbingKit,
tools::SackOfGrain, tools::SackOfGrain,
}; };
BillyObjects gestionnaire{}; BillyObjects gestionnaire{};
@ -160,9 +156,9 @@ TEST_CASE("[D] Printing Billy's objects", "[printing]") {
TEST_CASE("[Z] Cleaning after all tests", "[cleaning]") { TEST_CASE("[Z] Cleaning after all tests", "[cleaning]") {
namespace fs = std::filesystem; namespace fs = std::filesystem;
const auto pwd = is_directory(fs::current_path()) ? const auto pwd = is_directory(fs::current_path())
fs::directory_iterator(fs::current_path()) : ? fs::directory_iterator(fs::current_path())
fs::directory_iterator(fs::current_path().root_directory()); : fs::directory_iterator(fs::current_path().root_directory());
for (const auto &entry: pwd) { for (const auto &entry: pwd) {
if (entry.is_regular_file() && entry.path().extension() == ".json") { if (entry.is_regular_file() && entry.path().extension() == ".json") {
fs::remove(entry); fs::remove(entry);

View File

@ -0,0 +1,31 @@
#include <catch2/catch_all.hpp>
#include <fstream>
#include <character_sheet.hpp>
using namespace character;
TEST_CASE("[E] Serialize sheet", "[serialize][0]") {
CharacterSheet sheet;
BillyObjects gestion{};
REQUIRE(gestion.insert_object(sheet, weapons::Sword));
REQUIRE(gestion.insert_object(sheet, weapons::Lance));
REQUIRE(gestion.insert_object(sheet, tools::Dagger));
BillyObjects::check_dagger_conditions(sheet);
BillyObjects::json serializer;
REQUIRE_NOTHROW(serializer["character_sheet"] = sheet); {
std::ofstream file{ "character_sheet.json" };
file << serializer << std::flush;
}
}
TEST_CASE("[F] Deserialize sheet", "[deserialize][0]") {
const auto deserializer = []() {
std::ifstream file{ "character_sheet.json" };
return json::parse(file);
}();
const auto sheet = deserializer.at("character_sheet").get<CharacterSheet>();
}