From 20bfb961713aad30a23b6add0375c6ee604ecfc8 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Mon, 25 Nov 2024 16:46:39 +0100 Subject: [PATCH 1/2] Commit without knowing what was changed --- include/billy_objects.hpp | 43 +++++++++++++------------- src/billy_objects.cpp | 65 ++++++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 51 deletions(-) diff --git a/include/billy_objects.hpp b/include/billy_objects.hpp index 7a9e765..f20b9ec 100644 --- a/include/billy_objects.hpp +++ b/include/billy_objects.hpp @@ -17,7 +17,8 @@ namespace character { class CharacterSheet; - namespace characteristic{ + + namespace characteristic { class Characteristic; } @@ -27,18 +28,18 @@ namespace character { using container = std::unordered_map; static constexpr std::array all_objects{ - weapons::Sword, - weapons::Lance, - weapons::Morgenstern, - weapons::Bow, - equipments::Chainmail, - equipments::CookingPot, - equipments::PamphletTourist, - equipments::MedicKit, - tools::Fourche, - tools::Dagger, - tools::RockClimbingKit, - tools::SackOfGrain, + weapons::Sword, + weapons::Lance, + weapons::Morgenstern, + weapons::Bow, + equipments::Chainmail, + equipments::CookingPot, + equipments::PamphletTourist, + equipments::MedicKit, + tools::Fourche, + tools::Dagger, + tools::RockClimbingKit, + tools::SackOfGrain, }; static constexpr std::string_view json_key{ "billy_objects" }; @@ -61,9 +62,7 @@ namespace character { [[nodiscard]] const std::minus &get_minus_operation() const { return minus; } - static void check_dagger_conditions(const CharacterSheet &sheet, - characteristic::Characteristic &localHabilete, - const std::function &operation); + static void check_dagger_conditions(CharacterSheet &sheet); private: std::plus plus; @@ -71,12 +70,12 @@ namespace character { std::minus minus; static void change_carac(const billyObjects &arg, - CharacterSheet &sheet, - characteristic::Characteristic &localHabilete, - characteristic::Characteristic &localAdresse, - characteristic::Characteristic &localEndurance, - characteristic::Characteristic &localChance, - const std::function &operation) noexcept; + CharacterSheet &sheet, + characteristic::Characteristic &localHabilete, + characteristic::Characteristic &localAdresse, + characteristic::Characteristic &localEndurance, + characteristic::Characteristic &localChance, + const std::function &operation) noexcept; }; } diff --git a/src/billy_objects.cpp b/src/billy_objects.cpp index 6b7ce6e..d1eb30d 100644 --- a/src/billy_objects.cpp +++ b/src/billy_objects.cpp @@ -17,11 +17,17 @@ namespace character { bool BillyObjects::insert_object(CharacterSheet &sheet, const billyEnums objType) noexcept { if (sheet.objects.size() < 3) { - sheet.objects.emplace(objType, std::visit(overloaded{ - [](const weapons &arg) { return std::unique_ptr(std::make_unique(arg)); }, - [](const equipments &arg) { return std::unique_ptr(std::make_unique(arg)); }, - [](const tools &arg) { return std::unique_ptr(std::make_unique(arg)); }, - }, objType)); + sheet.objects.emplace(objType, + std::visit(overloaded{ + [](const weapons &arg) { + return std::unique_ptr(std::make_unique(arg)); + }, + [](const equipments &arg) { + return std::unique_ptr(std::make_unique(arg)); + }, + [](const tools &arg) { return std::unique_ptr(std::make_unique(arg)); }, + }, + objType)); const auto &object = sheet.objects[objType]; sheet.available_objects.erase(objType); @@ -34,7 +40,8 @@ namespace character { [&](const weapons &arg) { ++sheet.nb_weapons; }, [&](const equipments &arg) { ++sheet.nb_equipments; }, [&](const tools &arg) { ++sheet.nb_tools; }, - }, objType); + }, + objType); change_carac(object, sheet, local_habilete, local_adresse, local_endurance, local_chance, plus); return true; } @@ -56,18 +63,19 @@ namespace character { [&](const weapons &arg) { --sheet.nb_weapons; }, [&](const equipments &arg) { --sheet.nb_equipments; }, [&](const tools &arg) { --sheet.nb_tools; } - }, objToErase); + }, + objToErase); change_carac(obj, sheet, local_habilete, local_adresse, local_endurance, local_chance, minus); } } void BillyObjects::change_carac(const billyObjects &arg, - CharacterSheet &sheet, - Characteristic &localHabilete, - Characteristic &localAdresse, - Characteristic &localEndurance, - Characteristic &localChance, - const std::function &operation) noexcept { + CharacterSheet &sheet, + Characteristic &localHabilete, + Characteristic &localAdresse, + Characteristic &localEndurance, + Characteristic &localChance, + const std::function &operation) noexcept { localHabilete.materiel = operation(localHabilete.materiel, arg->add_materiel(localHabilete.type)); localAdresse.materiel = operation(localAdresse.materiel, arg->add_materiel(localAdresse.type)); localEndurance.materiel = operation(localEndurance.materiel, arg->add_materiel(localEndurance.type)); @@ -77,19 +85,17 @@ namespace character { sheet.critique = operation(sheet.critique, arg->add_critique()); } - void BillyObjects::check_dagger_conditions(const CharacterSheet &sheet, - Characteristic &localHabilete, - const std::function &operation) { - if (const auto it_dagger = sheet.objects.find(tools::Dagger); it_dagger != sheet.objects.cend()) { - const std::size_t count_weapons = std::count_if(sheet.objects.cbegin(), - sheet.objects.cend(), - [](container::const_reference node) { - return std::get_if(&node.first) != nullptr; - }); - const bool is_there_bow = sheet.objects.find(weapons::Bow) != sheet.objects.cend(); - if (count_weapons > 1 || is_there_bow) { - localHabilete.materiel = operation(localHabilete.materiel, - -it_dagger->second->add_materiel(localHabilete.type)); + void BillyObjects::check_dagger_conditions(CharacterSheet &sheet) { + if (const auto it_dagger = sheet.get_objects().find(tools::Dagger); it_dagger != sheet.get_objects().cend()) { + const std::size_t count_weapons = std::count_if(sheet.get_objects().cbegin(), + sheet.get_objects().cend(), + [](container::const_reference node) { + return std::get_if(&node.first) != nullptr; + } + ); + if (const bool is_there_bow = sheet.get_objects().find(weapons::Bow) != sheet.get_objects().cend(); + count_weapons > 1 || is_there_bow) { + sheet.habilete.materiel -= it_dagger->second->add_materiel(sheet.get_habilete().type); } } } @@ -108,7 +114,7 @@ namespace character { break; case equipmentHash: billy.emplace(static_cast(value), - std::make_unique(static_cast(value))); + std::make_unique(static_cast(value))); break; case toolsHash: billy.emplace(static_cast(value), std::make_unique(static_cast(value))); @@ -129,7 +135,8 @@ namespace character { [&j](const tools tool) { j.emplace_back(std::pair{ toolsHash, static_cast(tool) }); } - }, object_type); + }, + object_type); } } -} \ No newline at end of file +} From 7a5d36f6ead66e695e3efd0c9d4a480ff6d2b13c Mon Sep 17 00:00:00 2001 From: Pcornat Date: Mon, 25 Nov 2024 16:46:51 +0100 Subject: [PATCH 2/2] Updated tests --- Unit testing/CMakeLists.txt | 6 ++-- Unit testing/adummy.cpp | 3 -- Unit testing/billy_objects_tests.cpp | 46 ++++++++++++-------------- Unit testing/characteristics_tests.cpp | 2 +- Unit testing/sheet_tests.cpp | 31 +++++++++++++++++ 5 files changed, 57 insertions(+), 31 deletions(-) delete mode 100644 Unit testing/adummy.cpp create mode 100644 Unit testing/sheet_tests.cpp diff --git a/Unit testing/CMakeLists.txt b/Unit testing/CMakeLists.txt index b6b2dc5..2b91628 100644 --- a/Unit testing/CMakeLists.txt +++ b/Unit testing/CMakeLists.txt @@ -2,11 +2,13 @@ cmake_minimum_required(VERSION 3.19 FATAL_ERROR) project(UnitTest CXX) +include(CTest) include(../external/catch2/extras/Catch.cmake) -add_executable(UnitTest adummy.cpp +add_executable(UnitTest characteristics_tests.cpp billy_objects_tests.cpp + sheet_tests.cpp ) set_target_properties(Catch2 UnitTest PROPERTIES @@ -14,7 +16,7 @@ set_target_properties(Catch2 UnitTest PROPERTIES CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF INTERPROCEDURAL_OPTIMIZATION_RELEASE ON - UNITY_BUILD ON + # UNITY_BUILD ON ) target_include_directories(UnitTest PRIVATE ${CMAKE_SOURCE_DIR}/include) target_compile_definitions(UnitTest PRIVATE ${DEF_COMP}) diff --git a/Unit testing/adummy.cpp b/Unit testing/adummy.cpp deleted file mode 100644 index c4be9a8..0000000 --- a/Unit testing/adummy.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#define CATCH_CONFIG_MAIN - -#include \ No newline at end of file diff --git a/Unit testing/billy_objects_tests.cpp b/Unit testing/billy_objects_tests.cpp index 904a393..afeb14d 100644 --- a/Unit testing/billy_objects_tests.cpp +++ b/Unit testing/billy_objects_tests.cpp @@ -20,12 +20,14 @@ namespace character::test { void from_json(const json &j, DummyObject &dummy) { BillyObjects::from_json(j, dummy.objects); } + static std::unique_ptr get_obj(const billyEnums &object) noexcept { return std::visit(overloaded{ [](const weapons &arg) { return std::unique_ptr(std::make_unique(arg)); }, [](const equipments &arg) { return std::unique_ptr(std::make_unique(arg)); }, [](const tools &arg) { return std::unique_ptr(std::make_unique(arg)); }, - }, object); + }, + object); } } @@ -39,9 +41,7 @@ TEST_CASE("[C] Serialize empty BillyObjects", "[serialize][0]") { serializer.clear(); REQUIRE_NOTHROW(tester.empty() == true); - REQUIRE(tester.empty()); - - { + REQUIRE(tester.empty()); { std::ofstream file{ "billy_objects_empty.json" }; file << tester << std::flush; } @@ -49,8 +49,7 @@ TEST_CASE("[C] Serialize empty BillyObjects", "[serialize][0]") { TEST_CASE("[C] Serialize full BillyObjects", "[serialize][1]") { BillyObjects gestionnaire{}; - test::DummyObject dummy_object; - { + test::DummyObject dummy_object; { CharacterSheet sheet; REQUIRE(gestionnaire.insert_object(sheet, weapons::Sword)); 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); - REQUIRE_FALSE(tester.empty()); - - { + REQUIRE_FALSE(tester.empty()); { std::ofstream file{ "billy_objects_full.json" }; file << tester << std::flush; } @@ -88,7 +85,6 @@ TEST_CASE("[D] Deserialize empty BillyObjects", "[deserialize][0]") { } TEST_CASE("[D] Deserialize full BillyObjects", "[deserialize][1]") { -// BillyObjects gestionnaire{}; test::DummyObject dummy_object; const auto deserializer = []() -> json { @@ -112,18 +108,18 @@ TEST_CASE("[D] Deserialize full BillyObjects", "[deserialize][1]") { TEST_CASE("[D] Pushing popping BillyObjects", "[pushpop][0]") { constexpr std::array all_objects{ - weapons::Sword, - weapons::Lance, - weapons::Morgenstern, - weapons::Bow, - equipments::Chainmail, - equipments::CookingPot, - equipments::PamphletTourist, - equipments::MedicKit, - tools::Fourche, - tools::Dagger, - tools::RockClimbingKit, - tools::SackOfGrain, + weapons::Sword, + weapons::Lance, + weapons::Morgenstern, + weapons::Bow, + equipments::Chainmail, + equipments::CookingPot, + equipments::PamphletTourist, + equipments::MedicKit, + tools::Fourche, + tools::Dagger, + tools::RockClimbingKit, + tools::SackOfGrain, }; BillyObjects gestionnaire{}; @@ -160,9 +156,9 @@ TEST_CASE("[D] Printing Billy's objects", "[printing]") { TEST_CASE("[Z] Cleaning after all tests", "[cleaning]") { namespace fs = std::filesystem; - const auto pwd = is_directory(fs::current_path()) ? - fs::directory_iterator(fs::current_path()) : - fs::directory_iterator(fs::current_path().root_directory()); + const auto pwd = is_directory(fs::current_path()) + ? fs::directory_iterator(fs::current_path()) + : fs::directory_iterator(fs::current_path().root_directory()); for (const auto &entry: pwd) { if (entry.is_regular_file() && entry.path().extension() == ".json") { fs::remove(entry); diff --git a/Unit testing/characteristics_tests.cpp b/Unit testing/characteristics_tests.cpp index 34b179b..9888369 100644 --- a/Unit testing/characteristics_tests.cpp +++ b/Unit testing/characteristics_tests.cpp @@ -139,4 +139,4 @@ TEST_CASE("[B] Deserialize habilete", "[deserialize][3]") { REQUIRE(adresse.get_materiel() == 0); REQUIRE(adresse.get_additional() == 0); }()); -} \ No newline at end of file +} diff --git a/Unit testing/sheet_tests.cpp b/Unit testing/sheet_tests.cpp new file mode 100644 index 0000000..39de7ec --- /dev/null +++ b/Unit testing/sheet_tests.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +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(); +}