From 5d8f799a1171f92054d4b45ba130cd7fdad0bd01 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 23 May 2025 14:04:27 +0200 Subject: app: prepare restructuring --- core/CMakeLists.txt | 87 -------- core/include/turns/core/json_ext.hpp | 43 ---- core/include/turns/core/settings.hpp | 21 -- core/include/turns/core/turn_order_model.hpp | 102 --------- core/schemas/ch.arknet.Turns.gschema.xml | 25 --- core/src/settings.cpp | 28 --- core/src/turn_order_model.cpp | 312 -------------------------- core/tests/disposition.cpp | 32 --- core/tests/glib_test_init.cpp | 28 --- core/tests/participant.cpp | 310 -------------------------- core/tests/single_participant.trns | 11 - core/tests/turn_order_bugs.cpp | 40 ---- core/tests/turn_order_model.cpp | 314 --------------------------- 13 files changed, 1353 deletions(-) delete mode 100644 core/CMakeLists.txt delete mode 100644 core/include/turns/core/json_ext.hpp delete mode 100644 core/include/turns/core/settings.hpp delete mode 100644 core/include/turns/core/turn_order_model.hpp delete mode 100644 core/schemas/ch.arknet.Turns.gschema.xml delete mode 100644 core/src/settings.cpp delete mode 100644 core/src/turn_order_model.cpp delete mode 100644 core/tests/disposition.cpp delete mode 100644 core/tests/glib_test_init.cpp delete mode 100644 core/tests/participant.cpp delete mode 100644 core/tests/single_participant.trns delete mode 100644 core/tests/turn_order_bugs.cpp delete mode 100644 core/tests/turn_order_model.cpp (limited to 'core') diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt deleted file mode 100644 index 788d80b..0000000 --- a/core/CMakeLists.txt +++ /dev/null @@ -1,87 +0,0 @@ -# Library - -add_library("core" - "src/disposition.cpp" - "src/init.cpp" - "src/participant.cpp" - "src/settings.cpp" - "src/turn_order_model.cpp" - - "src/turns-participant.cpp" -) - -add_library("turns::core" ALIAS "core") - -target_compile_options("core" PUBLIC - "$<$:-Wall>" - "$<$:-Wextra>" - "$<$:-Werror>" - "$<$:-pedantic-errors>" -) - -if(NOT TURNS_USE_INSTALLED_SCHEMA_FILES) - target_compile_definitions("core" PUBLIC - "TURNS_SETTINGS_SCHEMA_DIR=\"${CMAKE_CURRENT_BINARY_DIR}\"" - ) -endif() - -target_include_directories("core" PUBLIC - "include" -) - -target_link_libraries("core" PUBLIC - "PkgConfig::giomm" - "PkgConfig::glibmm" - "nlohmann_json::nlohmann_json" -) - -target_add_glib_schemas("core" - SCHEMA_DIR "schemas" -) - -target_add_glib_enums("core" - HEADER_OUTPUT_DIR "turns" - HEADER_TEMPLATE "include/turns/turns-enums.h.in" - SOURCE_TEMPLATE "src/turns-enums.c.in" - OUTPUT_NAME "turns-enums" - HEADERS - "include/turns/turns-disposition.h" -) - -enable_coverage("core") - -install(FILES - "schemas/ch.arknet.Turns.gschema.xml" - DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/glib-2.0/schemas" -) - -# Tests - -add_executable("core-tests" - "tests/glib_test_init.cpp" - - "tests/disposition.cpp" - "tests/participant.cpp" - "tests/turn_order_bugs.cpp" - "tests/turn_order_model.cpp" -) - -target_link_libraries("core-tests" PRIVATE - "Catch2::Catch2WithMain" - - "turns::core" -) - -target_link_options("core-tests" PRIVATE - "$<$,$>:--coverage>" -) - -file(GLOB_RECURSE TEST_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/tests" CONFIGURE_DEPENDS "*.trns") - -target_add_glib_resources("core-tests" - PREFIX "/ch/arknet/Turns/core-tests/" - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/tests" - CSS_FILES ${TEST_FILES} -) - -catch_discover_tests("core-tests") \ No newline at end of file diff --git a/core/include/turns/core/json_ext.hpp b/core/include/turns/core/json_ext.hpp deleted file mode 100644 index 3eedb64..0000000 --- a/core/include/turns/core/json_ext.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef TURNS_CORE_JSON_EXT_HPP -#define TURNS_CORE_JSON_EXT_HPP - -#include -#include - -#include - -#include - -NLOHMANN_JSON_NAMESPACE_BEGIN - -template -struct adl_serializer> -{ - static void to_json(json & to, Glib::Property const & from) - { - to = from.get_value(); - } - - static void from_json(json const & from, Glib::Property & to) - { - to = from.template get(); - } -}; - -template<> -struct adl_serializer -{ - static void to_json(json & to, Glib::ustring const & from) - { - to = static_cast(from); - } - - static void from_json(json const & from, Glib::ustring & to) - { - to = from.template get(); - } -}; - -NLOHMANN_JSON_NAMESPACE_END - -#endif \ No newline at end of file diff --git a/core/include/turns/core/settings.hpp b/core/include/turns/core/settings.hpp deleted file mode 100644 index 304394d..0000000 --- a/core/include/turns/core/settings.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef TURNS_CORE_SETTINGS_HPP -#define TURNS_CORE_SETTINGS_HPP - -#include - -#include - -namespace turns::core -{ - namespace settings::key - { - auto constexpr disposition_friendly_color = "disposition-color-friendly"; - auto constexpr disposition_hostile_color = "disposition-color-hostile"; - auto constexpr disposition_secret_color = "disposition-color-secret"; - auto constexpr skip_defeated = "skip-defeated"; - } // namespace settings::key - - auto get_settings() -> Glib::RefPtr; -} // namespace turns::core - -#endif \ No newline at end of file diff --git a/core/include/turns/core/turn_order_model.hpp b/core/include/turns/core/turn_order_model.hpp deleted file mode 100644 index b7ce484..0000000 --- a/core/include/turns/core/turn_order_model.hpp +++ /dev/null @@ -1,102 +0,0 @@ -#ifndef TURNS_CORE_TURN_ORDER_MODEL_HPP -#define TURNS_CORE_TURN_ORDER_MODEL_HPP - -#include "turns/core/fwd.hpp" - -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include -#include -#include - -namespace turns::core -{ - - struct TurnOderModel : Gio::ListModel, - Glib::Object - { - using value_type = Glib::RefPtr; - using container_type = std::vector; - using iterator = container_type::iterator; - using const_iterator = container_type::const_iterator; - - using active_participant_type = unsigned int; - using is_empty_type = bool; - using has_next_type = bool; - using has_previous_type = bool; - using is_running_type = bool; - using progress_type = double; - using round_number_type = unsigned int; - using skip_defeated_type = bool; - - // auto static constexpr invalid_participant_index = std::numeric_limits::max(); - // auto static constexpr invalid_round_number = std::numeric_limits::max(); - - /** Life-time */ - // TurnOderModel(); - - // auto static create() -> Glib::RefPtr; - // auto static create(nlohmann::json const & from) -> Glib::RefPtr; - - /** Properties */ - // auto is_empty() const -> Glib::PropertyProxy_ReadOnly; - auto has_next() const -> Glib::PropertyProxy_ReadOnly; - auto has_previous() const -> Glib::PropertyProxy_ReadOnly; - auto is_running() const -> Glib::PropertyProxy_ReadOnly; - auto progress() const -> Glib::PropertyProxy_ReadOnly; - auto round_number() const -> Glib::PropertyProxy_ReadOnly; - auto skip_defeated() -> Glib::PropertyProxy; - - /** Element Modifications */ - // auto add(Glib::ustring const & name, float priority, Disposition disposition) -> void; - // auto clear() -> void; - // auto remove(unsigned index) -> void; - - /** Turn Modification */ - auto next() -> void; - auto previous() -> void; - auto start() -> void; - auto stop() -> void; - - /** Serialization */ - auto load(nlohmann::json const & from) -> void; - auto serialize() -> nlohmann::json; - - private: - explicit TurnOderModel(nlohmann::json const & from); - - auto get_item_type_vfunc() -> GType override; - auto get_n_items_vfunc() -> unsigned override; - auto get_item_vfunc(unsigned position) -> void * override; - - /** Signal handlers */ - auto handle_priority_changed(value_type entry) -> void; - - /** Data management */ - auto find(value_type entry) const -> const_iterator; - auto insert(value_type entry) -> const_iterator; - - container_type m_data{}; - std::optional m_active{}; - - Glib::Property m_has_next{*this, "has-next", false}; - Glib::Property m_has_previous{*this, "has-previous", false}; - Glib::Property m_is_empty{*this, "is-empty", true}; - Glib::Property m_is_running{*this, "is-running", false}; - Glib::Property m_progress{*this, "progress", 0.0}; - Glib::Property m_round_number{*this, "round-number", invalid_round_number}; - Glib::Property m_skip_defeated{*this, "skip-defeated", false}; - }; - -} // namespace turns::core - -#endif \ No newline at end of file diff --git a/core/schemas/ch.arknet.Turns.gschema.xml b/core/schemas/ch.arknet.Turns.gschema.xml deleted file mode 100644 index d764608..0000000 --- a/core/schemas/ch.arknet.Turns.gschema.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - '#33d17a' - Friendly Disposition Color - The color used to shade friendly participants. - - - '#e01b24' - Hostile Disposition Color - The color used to shade hostile participants. - - - '#9141ac' - Secret Disposition Color - The color used to shade secret participants. - - - false - Skip Defeated Participants - Whether or not defeated participants shall be skipped while stepping through the turn order. - - - \ No newline at end of file diff --git a/core/src/settings.cpp b/core/src/settings.cpp deleted file mode 100644 index 13cf665..0000000 --- a/core/src/settings.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "turns/core/settings.hpp" - -#include -#include - -#include -#include - -#include - -namespace turns::core -{ - - auto get_settings() -> Glib::RefPtr - { - auto constexpr schema_id = "ch.arknet.Turns"; - -#ifdef TURNS_SETTINGS_SCHEMA_DIR - auto source = Gio::SettingsSchemaSource::create(TURNS_SETTINGS_SCHEMA_DIR "/glib-2.0/schemas", true); - auto schema = source->lookup(schema_id, true); - auto settings = g_settings_new_full(Glib::unwrap(schema), nullptr, nullptr); - return Glib::wrap(settings); -#else - return Gio::Settings::create(schema_id); -#endif - } - -} // namespace turns::core diff --git a/core/src/turn_order_model.cpp b/core/src/turn_order_model.cpp deleted file mode 100644 index e430fed..0000000 --- a/core/src/turn_order_model.cpp +++ /dev/null @@ -1,312 +0,0 @@ -#include "turns/core/turn_order_model.hpp" - -#include "turns/core/disposition.hpp" -#include "turns/core/json_ext.hpp" -#include "turns/core/participant.hpp" - -#include -#include - -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include -#include -#include -#include -#include -#include - -using namespace std::placeholders; - -namespace turns::core -{ - - namespace - { - auto constexpr comparator = [](auto lhs, auto rhs) { - return *lhs > *rhs; - }; - } // namespace - - /** Construction */ - - TurnOderModel::TurnOderModel() - : Glib::ObjectBase{typeid(TurnOderModel)} - , Gio::ListModel{} - { - } - - TurnOderModel::TurnOderModel(nlohmann::json const & from) - : TurnOderModel{} - { - load(from); - } - - auto TurnOderModel::create() -> Glib::RefPtr - { - return Glib::make_refptr_for_instance(new TurnOderModel{}); - } - - auto TurnOderModel::create(nlohmann::json const & from) -> Glib::RefPtr - { - return Glib::make_refptr_for_instance(new TurnOderModel{from}); - } - - /** Queries */ - - auto TurnOderModel::is_empty() const -> Glib::PropertyProxy_ReadOnly - { - return m_is_empty.get_proxy(); - } - - auto TurnOderModel::has_next() const -> Glib::PropertyProxy_ReadOnly - { - return m_has_next.get_proxy(); - } - - auto TurnOderModel::has_previous() const -> Glib::PropertyProxy_ReadOnly - { - return m_has_previous.get_proxy(); - } - - auto TurnOderModel::is_running() const -> Glib::PropertyProxy_ReadOnly - { - return m_is_running.get_proxy(); - } - - auto TurnOderModel::progress() const -> Glib::PropertyProxy_ReadOnly - { - return m_progress.get_proxy(); - } - - auto TurnOderModel::round_number() const -> Glib::PropertyProxy_ReadOnly - { - return m_round_number.get_proxy(); - } - - auto TurnOderModel::skip_defeated() -> Glib::PropertyProxy - { - return m_skip_defeated.get_proxy(); - } - - /** Modifiers */ - - auto TurnOderModel::add(Glib::ustring const & name, float priority, Disposition disposition) -> void - { - auto entry = Participant::create(name, priority, disposition); - entry->property_priority().signal_changed().connect(sigc::bind(sigc::mem_fun(*this, &TurnOderModel::handle_priority_changed), entry)); - auto position = std::distance(m_data.cbegin(), insert(entry)); - items_changed(position, 0, 1); - - if (get_n_items() == 1) - { - m_is_empty = false; - m_has_next = true; - } - } - - auto TurnOderModel::clear() -> void - { - m_is_running = false; - m_is_empty = true; - m_has_next = false; - m_has_previous = false; - m_active.reset(); - m_round_number = invalid_round_number; - m_progress = 0; - - auto old_size = get_n_items(); - m_data.clear(); - items_changed(0, old_size, 0); - } - - auto TurnOderModel::next() -> void - { - auto old_active = *m_active; - m_active = m_active.transform([this](auto index) { return (index + 1) % get_n_items(); }); - - m_has_previous = true; - m_data[old_active]->property_is_active() = false; - m_data[*m_active]->property_is_active() = true; - - m_progress = (static_cast(*m_active) + 1) / get_n_items(); - - if (m_active == 0) - { - m_round_number = m_round_number + 1; - } - } - - auto TurnOderModel::previous() -> void - { - if (!(m_has_previous && m_is_running)) - { - return; - } - - auto old_active = *m_active; - m_active = m_active.transform([this](auto index) { return index ? index - 1 : get_n_items() - 1; }); - - m_has_previous = m_round_number > 0 || m_active > 0; - m_data[old_active]->property_is_active() = false; - m_data[*m_active]->property_is_active() = true; - - m_progress = (static_cast(*m_active) + 1) / get_n_items(); - - if (m_active == 0 && m_round_number > 0) - { - m_round_number = m_round_number - 1; - } - } - - auto TurnOderModel::remove(unsigned index) -> void - { - if (index >= get_n_items()) - { - return; - } - - auto position = m_data.begin() + index; - m_data.erase(position); - items_changed(index, 1, 0); - if (get_n_items() == 0) - { - m_is_empty = true; - m_is_running = false; - m_has_next = false; - } - } - - auto TurnOderModel::start() -> void - { - if (!m_active) - { - m_active = 0; - m_data[*m_active]->property_is_active() = true; - } - if (m_round_number == invalid_round_number) - { - m_round_number = 0; - } - m_is_running = true; - - m_progress = (static_cast(*m_active) + 1) / get_n_items(); - } - - auto TurnOderModel::stop() -> void - { - m_is_running = false; - m_progress = 0; - } - - /** Serialization */ - auto TurnOderModel::load(nlohmann::json const & from) -> void - { - auto old_size = get_n_items(); - - this->freeze_notify(); - - m_round_number = from.value("round", invalid_round_number); - - m_data.clear(); - auto participants = from.value("participants", std::vector{}); - auto factory = [](auto s) { - return Participant::create(s); - }; - auto inserter = std::bind(&TurnOderModel::insert, this, _1); - std::ranges::for_each(participants | std::views::transform(factory), inserter); - - auto active = std::ranges::find_if(m_data, [](auto participant) { return participant->property_is_active(); }); - if (active != std::ranges::end(m_data)) - { - m_active = std::ranges::distance(std::ranges::begin(m_data), active); - } - - m_is_empty = m_data.empty(); - m_has_next = !m_is_empty; - m_has_previous = m_round_number > 0 || m_active > 0; - - this->thaw_notify(); - - items_changed(0, old_size, get_n_items()); - } - - auto TurnOderModel::serialize() -> nlohmann::json - { - auto serialized = nlohmann::json{}; - if (m_round_number != invalid_round_number) - { - serialized["round"] = m_round_number; - } - serialized["participants"] = m_data | std::views::transform([](auto const & p) { return p->serialize(); }) | std::ranges::to(); - ; - return serialized; - } - - /** ListModel implementation */ - - auto TurnOderModel::get_item_type_vfunc() -> GType - { - return Participant::get_type(); - } - - auto TurnOderModel::get_n_items_vfunc() -> unsigned - { - return m_data.size(); - } - - auto TurnOderModel::get_item_vfunc(unsigned position) -> void * - { - if (position >= get_n_items()) - { - return nullptr; - } - auto item = m_data[position]; - item->reference(); - return Glib::unwrap(item); - } - - /** Signal handlers */ - - auto TurnOderModel::handle_priority_changed(value_type entry) -> void - { - auto original_position = find(entry); - auto original_index = distance(m_data.cbegin(), original_position); - auto target_position = std::ranges::upper_bound(m_data, entry, comparator); - if (original_position == target_position) - { - return; - } - - m_data.erase(original_position); - auto inserted_position = insert(entry); - items_changed(0, get_n_items(), get_n_items()); - if (m_active == original_index) - { - m_active = distance(m_data.cbegin(), inserted_position); - m_has_previous = m_round_number > 0 || m_active > 0; - } - } - - /** Data management */ - - auto TurnOderModel::find(value_type entry) const -> const_iterator - { - return std::ranges::find(m_data, entry); - } - - auto TurnOderModel::insert(value_type entry) -> const_iterator - { - return m_data.insert(std::ranges::upper_bound(m_data, entry, comparator), entry); - } - -} // namespace turns::core \ No newline at end of file diff --git a/core/tests/disposition.cpp b/core/tests/disposition.cpp deleted file mode 100644 index fadf476..0000000 --- a/core/tests/disposition.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "turns/core/disposition.hpp" - -#include -#include - -#include -#include - -#include -#include -#include -#include - -namespace turns::core::tests -{ - - TEST_CASE("to_presentation_name returns the correct string for the current language", "[disposition]") - { - auto [value, name] = GENERATE(std::pair{Disposition::Neutral, Glib::ustring{_("Neutral")}}, - std::pair{Disposition::Friendly, Glib::ustring{_("Friendly")}}, - std::pair{Disposition::Hostile, Glib::ustring{_("Hostile")}}, - std::pair{Disposition::Secret, Glib::ustring{_("Secret")}}, - std::pair{static_cast(std::numeric_limits>::max()), - Glib::ustring{_("Unknown disposition value")}}); - - SECTION(std::format("the presentation name for '{}' is '{}'", static_cast>(value), name.c_str())) - { - REQUIRE(presentation_name_for(value) == name); - } - } - -} // namespace turns::core::tests \ No newline at end of file diff --git a/core/tests/glib_test_init.cpp b/core/tests/glib_test_init.cpp deleted file mode 100644 index b521189..0000000 --- a/core/tests/glib_test_init.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "turns/core/init.hpp" - -#include -#include - -#include - -#include - -namespace turns::core::tests -{ - - struct glib_test_init : Catch::EventListenerBase - { - using Catch::EventListenerBase::EventListenerBase; - - auto testRunStarting(Catch::TestRunInfo const &) -> void override - { - Gio::init(); - Glib::init(); - - register_types(); - } - }; - - CATCH_REGISTER_LISTENER(glib_test_init); - -} // namespace turns::core::tests \ No newline at end of file diff --git a/core/tests/participant.cpp b/core/tests/participant.cpp deleted file mode 100644 index b417e36..0000000 --- a/core/tests/participant.cpp +++ /dev/null @@ -1,310 +0,0 @@ -#include "turns/core/participant.hpp" - -#include "turns/core/disposition.hpp" - -#include -#include - -#include - -#include - -namespace turns::core::tests -{ - - TEST_CASE("Constructing a participant", "[core][construction]") - { - auto constexpr name = "Participant #1"; - auto constexpr priority = 17; - auto constexpr disposition = Disposition::Friendly; - - auto json = nlohmann::json::parse(R"( - { - "name": "Participant #1", - "priority": 17, - "disposition": 1, - "is-active": false, - "is-defeated": false - } - )"); - - SECTION("Can be constructed using default ctor") - { - auto instance = Participant{}; - REQUIRE(instance.property_name() == ""); - REQUIRE(instance.property_priority() == 0); - REQUIRE(instance.property_disposition() == Disposition::Neutral); - REQUIRE(!instance.property_is_active()); - REQUIRE(!instance.property_is_defeated()); - } - - SECTION("Can be constructed using ctor") - { - auto instance = Participant{name, priority, disposition}; - REQUIRE(instance.property_name() == name); - REQUIRE(instance.property_priority() == priority); - REQUIRE(instance.property_disposition() == disposition); - REQUIRE(!instance.property_is_active()); - REQUIRE(!instance.property_is_defeated()); - } - - SECTION("Can be constructed using factory") - { - auto instance = Participant::create(name, priority, disposition); - REQUIRE(instance->property_name() == name); - REQUIRE(instance->property_priority() == priority); - REQUIRE(instance->property_disposition() == disposition); - REQUIRE(!instance->property_is_active()); - REQUIRE(!instance->property_is_defeated()); - } - - SECTION("Can be constructed using JSON factory", "[json]") - { - auto instance = Participant::create(json); - REQUIRE(instance->property_name() == name); - REQUIRE(instance->property_priority() == priority); - REQUIRE(instance->property_disposition() == disposition); - REQUIRE(!instance->property_is_active()); - REQUIRE(!instance->property_is_defeated()); - } - } - - TEST_CASE("Setting properties on participant", "[core][properties]") - { - auto constexpr name = "Participant #2"; - auto constexpr priority = 10; - auto constexpr disposition = Disposition::Hostile; - - auto instance = Participant{name, priority, disposition}; - - SECTION("Setting '::disposition' via proxy changes the 'disposition' value") - { - auto old = instance.property_disposition().get_value(); - instance.property_disposition() = static_cast(static_cast(disposition) - 1); - REQUIRE(instance.property_disposition() != old); - } - - SECTION("Setting '::disposition' via glib changes the 'disposition' value") - { - auto old = instance.property_disposition().get_value(); - instance.set_property("disposition", static_cast(static_cast(disposition) - 1)); - REQUIRE(instance.property_disposition() != old); - } - - SECTION("Setting '::is-active' via proxy changes the 'is_active' value") - { - auto old = instance.property_is_active().get_value(); - instance.property_is_active() = !old; - REQUIRE(instance.property_is_active() != old); - } - - SECTION("Setting '::is-active' via glib changes the 'is_active' value") - { - auto old = instance.property_is_active().get_value(); - instance.set_property("is-active", !old); - REQUIRE(instance.property_is_active() != old); - } - - SECTION("Setting '::is-defeated' via proxy changes the 'is_defeated' value") - { - auto old = instance.property_is_defeated().get_value(); - instance.property_is_defeated() = !old; - REQUIRE(instance.property_is_defeated() != old); - } - - SECTION("Setting '::is-defeated' via glib changes the 'is_defeated' value") - { - auto old = instance.property_is_defeated().get_value(); - instance.set_property("is-defeated", !old); - REQUIRE(instance.property_is_defeated() != old); - } - - SECTION("Setting '::name' via proxy changes the 'name' value") - { - auto old = instance.property_name().get_value(); - instance.property_name() = old + " Changed"; - REQUIRE(instance.property_name().get_value() != old); - } - - SECTION("Setting '::name' via glib changes the 'name' value") - { - auto old = instance.property_name().get_value(); - instance.set_property("name", old + " Changed"); - REQUIRE(instance.property_name().get_value() != old); - } - - SECTION("Setting '::priority' via proxy changes the 'priority' value") - { - auto old = instance.property_priority().get_value(); - instance.property_priority() = old + 1; - REQUIRE(instance.property_priority() != old); - } - - SECTION("Setting '::priority' via glib changes the 'priority' value") - { - auto old = instance.property_priority().get_value(); - instance.set_property("priority", old + 1); - REQUIRE(instance.property_priority() != old); - } - } - - TEST_CASE("A freshly constructed participant") - { - auto constexpr constructed_name = "Vana Thistletop"; - auto constexpr constructed_priority = 17; - auto constexpr constructed_disposition = Disposition::Friendly; - auto instance = Participant{constructed_name, constructed_priority, constructed_disposition}; - - SECTION("can be created") - { - REQUIRE(Participant::create(constructed_name, constructed_priority, constructed_disposition)); - } - - SECTION("allows access to its disposition") - { - SECTION("allowing to get it") - { - REQUIRE(instance.property_disposition() == constructed_disposition); - } - - SECTION("allowing to get it via a constant object") - { - auto const & cref = instance; - REQUIRE(cref.property_disposition() == constructed_disposition); - } - - SECTION("allowing to set it") - { - instance.property_disposition() = Disposition::Hostile; - REQUIRE(instance.property_disposition() == Disposition::Hostile); - } - } - - SECTION("allows access to its name") - { - SECTION("allowing to get it") - { - REQUIRE(instance.property_name() == constructed_name); - } - - SECTION("allowing to get it via a constant object") - { - auto const & cref = instance; - REQUIRE(cref.property_name() == constructed_name); - } - - SECTION("allowing to set it") - { - instance.property_name() = "replaced"; - REQUIRE(instance.property_name() == "replaced"); - } - } - - SECTION("allows access to its priority") - { - SECTION("allowing to get it") - { - REQUIRE(instance.property_priority() == constructed_priority); - } - - SECTION("allowing to get it via a constant object") - { - auto const & cref = instance; - REQUIRE(cref.property_priority() == constructed_priority); - } - - SECTION("allowing to set it") - { - instance.property_priority() = 4; - REQUIRE(instance.property_priority() == 4); - } - } - - SECTION("can be compared with another participant") - { - auto equivalent_instance = Participant{"Equivalent", constructed_priority, constructed_disposition}; - auto lesser_instance = Participant{"Lesser", constructed_priority - 1, constructed_disposition}; - auto greater_instance = Participant{"Greater", constructed_priority + 1, constructed_disposition}; - - SECTION("yielding std::partial_ordering::equivalent for itself") - { - REQUIRE((instance <=> equivalent_instance) == std::partial_ordering::equivalent); - } - - SECTION("yielding std::partial_ordering::equivalent for an equivalent participant") - { - REQUIRE((instance <=> equivalent_instance) == std::partial_ordering::equivalent); - } - - SECTION("yielding std::partial_ordering::greater for a lesser participant") - { - REQUIRE((instance <=> lesser_instance) == std::partial_ordering::greater); - } - - SECTION("yielding std::partial_ordering::less for a greater participant") - { - REQUIRE((instance <=> greater_instance) == std::partial_ordering::less); - } - } - } - - TEST_CASE("Serializing a participant") - { - auto instance = Participant::create("Participant #0", 17.2, Disposition::Friendly); - auto serialized = instance->serialize(); - - SECTION("the active state is serialized correctly") - { - REQUIRE_FALSE(serialized.at("is-active")); - } - - SECTION("the disposition is serialized correctly") - { - REQUIRE(serialized.at("disposition") == Disposition::Friendly); - } - - SECTION("the name is serialized correctly") - { - REQUIRE(serialized.at("name") == "Participant #0"); - } - - SECTION("the priority is serialized correctly") - { - REQUIRE(serialized.at("priority") == Catch::Approx{17.2}); - } - } - - TEST_CASE("De-Serializing a participant") - { - auto serialized = nlohmann::json::parse(R"( - { - "name": "Participant #1", - "priority": -2.3, - "disposition": 2, - "is-active": true - } - )"); - auto instance = Participant::create(serialized); - - SECTION("the active state is de-serialized correctly") - { - REQUIRE(instance->property_is_active()); - } - - SECTION("the disposition is de-serialized correctly") - { - REQUIRE(instance->property_disposition() == Disposition::Hostile); - } - - SECTION("the name is de-serialized correctly") - { - REQUIRE(instance->property_name() == "Participant #1"); - } - - SECTION("the priority is de-serialized correctly") - { - REQUIRE(instance->property_priority() == Catch::Approx{-2.3}); - } - } - -} // namespace turns::core::tests \ No newline at end of file diff --git a/core/tests/single_participant.trns b/core/tests/single_participant.trns deleted file mode 100644 index bcce137..0000000 --- a/core/tests/single_participant.trns +++ /dev/null @@ -1,11 +0,0 @@ -{ - "participants": [ - { - "disposition": 0, - "is-active": false, - "is-defeated": false, - "name": "Riamin", - "priority": 0.0 - } - ] -} \ No newline at end of file diff --git a/core/tests/turn_order_bugs.cpp b/core/tests/turn_order_bugs.cpp deleted file mode 100644 index 7bfde78..0000000 --- a/core/tests/turn_order_bugs.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "turns/core/disposition.hpp" -#include "turns/core/turn_order_model.hpp" - -#include - -namespace turns::core::tests -{ - /** - * Bug description: - * - * After having stepped according to the step pattern below, tt was possible to step backward often enough to underflow the round number: - * - forward - * - backward - * - forward - */ - SCENARIO("Can step back infinitely", "[turn_order][bug]") - { - GIVEN("a non-empty turn_order") - { - auto instance = TurnOderModel::create(); - - instance->add("A", 0, Disposition::Neutral); - - WHEN("it is started and then stepped forward, backward, forward") - { - instance->start(); - instance->next(); - instance->previous(); - instance->next(); - - THEN("it is not possible to step backwards more than once") - { - instance->previous(); - instance->previous(); - REQUIRE(instance->round_number() == 0); - } - } - } - } -} // namespace turns::core::tests \ No newline at end of file diff --git a/core/tests/turn_order_model.cpp b/core/tests/turn_order_model.cpp deleted file mode 100644 index 1cd3633..0000000 --- a/core/tests/turn_order_model.cpp +++ /dev/null @@ -1,314 +0,0 @@ -#include "turns/core/turn_order_model.hpp" - -#include "turns/core/disposition.hpp" -#include "turns/core/participant.hpp" - -#include - -#include - -#include - -#include - -namespace turns::core::tests -{ - SCENARIO("Queries on a fresh turn_order instance", "[turn_order]") - { - GIVEN("an empty turn_order") - { - auto instance = TurnOderModel::create(); - - THEN("get_n_items() returns 0") - { - REQUIRE(instance->get_n_items() == 0); - } - - THEN("get_type() returns participant::get_type()") - { - REQUIRE(instance->get_item_type() == Participant::get_type()); - } - - THEN("get_typed_object(0) returns nullptr") - { - REQUIRE(instance->get_typed_object(0) == nullptr); - } - - THEN("has_next() returns false") - { - REQUIRE_FALSE(instance->has_next()); - } - - THEN("has_previous() returns false") - { - REQUIRE_FALSE(instance->has_previous()); - } - - THEN("is_empty() returns true") - { - REQUIRE(instance->is_empty()); - } - - THEN("is_running() returns false") - { - REQUIRE_FALSE(instance->is_running()); - } - - THEN("round_number() returns invalid_round_number") - { - REQUIRE(instance->round_number() == TurnOderModel::invalid_round_number); - } - } - } - - SCENARIO("Adding participants") - { - auto instance = TurnOderModel::create(); - - GIVEN("a participant has been added to a turn_order") - { - instance->add("Participant #0", 0, Disposition::Neutral); - - THEN("get_n_items() returns 1") - { - REQUIRE(instance->get_n_items() == 1); - } - - THEN("get_typed_object(0) returns a non-null pointer") - { - REQUIRE(instance->get_typed_object(0) != nullptr); - } - - THEN("has_next() returns true") - { - REQUIRE(instance->has_next()); - } - - THEN("has_previous() returns false") - { - REQUIRE_FALSE(instance->has_previous()); - } - - THEN("is_empty() returns false") - { - REQUIRE_FALSE(instance->is_empty()); - } - - THEN("is_running() returns false") - { - REQUIRE_FALSE(instance->is_running()); - } - - THEN("round_number() returns invalid_round_number") - { - REQUIRE(instance->round_number() == TurnOderModel::invalid_round_number); - } - - WHEN("the turn_order is start()ed") - { - instance->start(); - - THEN("get_n_items() still returns 1") - { - REQUIRE(instance->get_n_items() == 1); - } - - THEN("get_typed_object(0) still returns a non-null pointer") - { - REQUIRE(instance->get_typed_object(0) != nullptr); - } - - THEN("has_next() still returns true") - { - REQUIRE(instance->has_next()); - } - - THEN("has_previous() still returns false") - { - REQUIRE_FALSE(instance->has_previous()); - } - - THEN("is_empty() still returns false") - { - REQUIRE_FALSE(instance->is_empty()); - } - - THEN("is_running() returns true") - { - REQUIRE(instance->is_running()); - } - - THEN("round_number() returns 0") - { - REQUIRE(instance->round_number() == 0); - } - - AND_WHEN("invoking previous()") - { - instance->previous(); - - THEN("get_n_items() still returns 1") - { - REQUIRE(instance->get_n_items() == 1); - } - - THEN("get_typed_object(0) still returns a non-null pointer") - { - REQUIRE(instance->get_typed_object(0) != nullptr); - } - - THEN("has_next() still returns true") - { - REQUIRE(instance->has_next()); - } - - THEN("has_previous() still returns false") - { - REQUIRE_FALSE(instance->has_previous()); - } - - THEN("is_empty() still returns false") - { - REQUIRE_FALSE(instance->is_empty()); - } - - THEN("is_running() returns true") - { - REQUIRE(instance->is_running()); - } - - THEN("round_number() returns 0") - { - REQUIRE(instance->round_number() == 0); - } - } - - AND_WHEN("invoking next()") - { - instance->next(); - - THEN("get_n_items() still returns 1") - { - REQUIRE(instance->get_n_items() == 1); - } - - THEN("get_typed_object(0) still returns a non-null pointer") - { - REQUIRE(instance->get_typed_object(0) != nullptr); - } - - THEN("has_next() still returns true") - { - REQUIRE(instance->has_next()); - } - - THEN("has_previous() returns true") - { - REQUIRE(instance->has_previous()); - } - - THEN("is_empty() still returns false") - { - REQUIRE_FALSE(instance->is_empty()); - } - - THEN("is_running() returns true") - { - REQUIRE(instance->is_running()); - } - - THEN("round_number() returns 1") - { - REQUIRE(instance->round_number() == 1); - } - } - } - } - } - - SCENARIO("Loading from json data") - { - GIVEN("an empty turn order") - { - auto instance = TurnOderModel{}; - CHECK(instance.is_empty()); - - WHEN("loading a serialized turn order with one named participant") - { - auto data = Gio::Resource::lookup_data_global("/ch/arknet/Turns/core-tests/single_participant.trns"); - auto size = 0uz; - instance.load(nlohmann::json::parse(std::string_view{static_cast(data->get_data(size)), data->get_size()})); - - THEN("it is no longer empty") - { - REQUIRE_FALSE(instance.is_empty()); - } - - THEN("it has 1 element") - { - REQUIRE(instance.get_n_items() == 1); - } - - THEN("the single participant's name is set") - { - auto participant = instance.get_typed_object(0); - REQUIRE_FALSE(participant->get_name().empty()); - } - - AND_THEN("adding another participant with a higher priority") - { - auto participant = instance.get_typed_object(0); - instance.add("Higher Priority", 100.0f, Disposition::Neutral); - - THEN("the index of the test participant is 1") - { - CHECK(instance.get_typed_object(0) != participant); - REQUIRE(instance.get_typed_object(1) == participant); - } - } - - AND_WHEN("calling clear") - { - instance.clear(); - - THEN("it is empty") - { - REQUIRE(instance.is_empty()); - } - } - } - } - } - - SCENARIO("Sorting of participants") - { - GIVEN("A turn order with 3 participants (A,B,C) of priorities 100, 0, and -100 respectively") - { - auto instance = TurnOderModel{}; - - instance.add("A", 100, Disposition::Friendly); - instance.add("B", 0, Disposition::Friendly); - instance.add("C", -100, Disposition::Friendly); - - THEN("A comes before B comes before C") - { - REQUIRE(instance.get_typed_object(0)->get_name() == "A"); - REQUIRE(instance.get_typed_object(1)->get_name() == "B"); - REQUIRE(instance.get_typed_object(2)->get_name() == "C"); - } - - WHEN("changing the priority of the B to 50") - { - instance.get_typed_object(1)->set_priority(50); - - THEN("the order stays the same") - { - REQUIRE(instance.get_typed_object(0)->get_name() == "A"); - REQUIRE(instance.get_typed_object(1)->get_name() == "B"); - REQUIRE(instance.get_typed_object(2)->get_name() == "C"); - } - } - } - } - -} // namespace turns::core::tests \ No newline at end of file -- cgit v1.2.3