BillySheet/Unit testing/billy_objects_tests.cpp

172 lines
5.8 KiB
C++
Raw Normal View History

2024-03-04 17:25:11 +01:00
#include <catch2/catch_all.hpp>
#include <iostream>
#include <fstream>
#include "billy_objects.hpp"
#include "character_sheet.hpp"
2024-10-30 22:19:23 +01:00
#include "generic_object.hpp"
2024-03-04 17:25:11 +01:00
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);
}
2024-10-30 22:19:23 +01:00
static std::unique_ptr<GenericObject> get_obj(const billyEnums &object) noexcept {
return std::visit(overloaded{
[](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 tools &arg) { return std::unique_ptr<GenericObject>(std::make_unique<Tools>(arg)); },
}, object);
}
2024-03-04 17:25:11 +01:00
}
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;
2024-10-30 22:19:23 +01:00
REQUIRE(gestionnaire.insert_object(sheet, weapons::Sword));
REQUIRE(gestionnaire.insert_object(sheet, tools::Fourche));
REQUIRE(gestionnaire.insert_object(sheet, equipments::MedicKit));
for (const auto &[obj_type, _]: sheet.get_objects()) {
dummy_object.objects.emplace(obj_type, test::get_obj(obj_type));
}
2024-03-04 17:25:11 +01:00
}
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]") {
2024-10-30 22:19:23 +01:00
// BillyObjects gestionnaire{};
2024-03-04 17:25:11 +01:00
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());
2024-10-30 22:19:23 +01:00
REQUIRE_NOTHROW(dummy_object.objects.at(weapons::Sword));
REQUIRE(std::get<weapons>(dummy_object.objects.at(weapons::Sword)->get_type()) == weapons::Sword);
2024-03-04 17:25:11 +01:00
2024-10-30 22:19:23 +01:00
REQUIRE_NOTHROW(dummy_object.objects.at(tools::Fourche));
REQUIRE(std::get<tools>(dummy_object.objects.at(tools::Fourche)->get_type()) == tools::Fourche);
2024-03-04 17:25:11 +01:00
2024-10-30 22:19:23 +01:00
REQUIRE_NOTHROW(dummy_object.objects.at(equipments::MedicKit));
REQUIRE(std::get<equipments>(dummy_object.objects.at(equipments::MedicKit)->get_type()) == equipments::MedicKit);
2024-03-04 17:25:11 +01:00
}
TEST_CASE("[D] Pushing popping BillyObjects", "[pushpop][0]") {
2024-10-30 22:19:23 +01:00
constexpr std::array<billyEnums, 12> 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,
};
BillyObjects gestionnaire{};
CharacterSheet sheet;
REQUIRE(sheet.get_objects().empty());
2024-10-30 22:19:23 +01:00
REQUIRE(gestionnaire.insert_object(sheet, weapons::Sword));
REQUIRE_FALSE(sheet.get_objects().empty());
2024-10-30 22:19:23 +01:00
REQUIRE_NOTHROW(sheet.get_objects().at(weapons::Sword));
REQUIRE(std::get<weapons>(sheet.get_objects().at(weapons::Sword)->get_type()) == weapons::Sword);
2024-10-30 22:19:23 +01:00
REQUIRE_NOTHROW(gestionnaire.erase_object(sheet, weapons::Sword));
REQUIRE(sheet.get_objects().empty());
for (std::size_t j = 0; j < all_objects.size(); j += 3) {
for (std::size_t i = 0; i < 3; ++i) {
2024-10-30 22:19:23 +01:00
REQUIRE_NOTHROW(gestionnaire.insert_object(sheet, all_objects[i + j]));
}
REQUIRE_FALSE(sheet.get_objects().empty());
for (std::size_t i = 0; i < 3; ++i) {
2024-10-30 22:19:23 +01:00
REQUIRE_NOTHROW(gestionnaire.erase_object(sheet, all_objects[i + j]));
}
REQUIRE(sheet.get_objects().empty());
}
}
TEST_CASE("[D] Printing Billy's objects", "[printing]") {
for (const auto &object: BillyObjects::all_objects) {
2024-10-30 22:19:23 +01:00
const std::unique_ptr<GenericObject> obj = test::get_obj(object);
REQUIRE_NOTHROW(BillyObjects::billy_object_to_string(obj));
}
}
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());
for (const auto &entry: pwd) {
if (entry.is_regular_file() && entry.path().extension() == ".json") {
fs::remove(entry);
}
}
}