diff --git a/Unit testing/billy_objects_tests.cpp b/Unit testing/billy_objects_tests.cpp index 3270a11..d1b73ab 100644 --- a/Unit testing/billy_objects_tests.cpp +++ b/Unit testing/billy_objects_tests.cpp @@ -153,6 +153,18 @@ TEST_CASE("[D] Pushing popping BillyObjects", "[pushpop][0]") { } } +TEST_CASE("[D] Double erase no throw", "[pushpop][1]") { + BillyObjects gestionnaire{}; + CharacterSheet sheet; + REQUIRE(sheet.get_objects().empty()); + + REQUIRE_NOTHROW(gestionnaire.insert_object(sheet, weapons::Sword)); + REQUIRE(gestionnaire.insert_object(sheet, weapons::Sword)); + + REQUIRE_NOTHROW(gestionnaire.erase_object(sheet, weapons::Bow)); + REQUIRE_FALSE(gestionnaire.erase_object(sheet, weapons::Bow)); +} + TEST_CASE("[D] Printing Billy's objects", "[printing]") { for (const auto &object: BillyObjects::all_objects) { const std::unique_ptr obj = test::get_obj(object); diff --git a/include/billy_objects.hpp b/include/billy_objects.hpp index 5b2a025..ab59c37 100644 --- a/include/billy_objects.hpp +++ b/include/billy_objects.hpp @@ -52,7 +52,7 @@ namespace character { [[nodiscard]] bool insert_object(CharacterSheet &sheet, const billyEnums objType) noexcept; - void erase_object(CharacterSheet &sheet, const billyEnums objToErase) noexcept; + bool erase_object(CharacterSheet &sheet, const billyEnums objToErase) noexcept; [[nodiscard]] const std::plus &get_plus_operation() const { return plus; } diff --git a/src/billy_objects.cpp b/src/billy_objects.cpp index 3c3b3b1..d094440 100644 --- a/src/billy_objects.cpp +++ b/src/billy_objects.cpp @@ -57,9 +57,9 @@ namespace character { return false; } - void BillyObjects::erase_object(CharacterSheet &sheet, const billyEnums objToErase) noexcept { - if (!sheet.objects.empty()) { - const billyObjects obj{ sheet.objects[objToErase].release() }; + bool BillyObjects::erase_object(CharacterSheet &sheet, const billyEnums objToErase) noexcept { + if (const auto it = sheet.objects.find(objToErase); it != std::end(sheet.objects)) { + const billyObjects obj{ it->second.release() }; sheet.objects.erase(objToErase); sheet.available_objects.insert(objToErase); @@ -69,13 +69,15 @@ namespace character { auto &local_chance = static_cast(sheet.chance); std::visit(overloaded{ - [&](const weapons &arg) { --sheet.nb_weapons; }, - [&](const equipments &arg) { --sheet.nb_equipments; }, - [&](const tools &arg) { --sheet.nb_tools; } + [&](const weapons &) { --sheet.nb_weapons; }, + [&](const equipments &) { --sheet.nb_equipments; }, + [&](const tools &) { --sheet.nb_tools; } }, objToErase); change_carac(obj, sheet, local_habilete, local_adresse, local_endurance, local_chance, minus); + return true; } + return false; } void BillyObjects::change_carac(const billyObjects &arg,