summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/CMakeLists.txt87
-rw-r--r--core/include/turns/core/json_ext.hpp43
-rw-r--r--core/include/turns/core/settings.hpp21
-rw-r--r--core/include/turns/core/turn_order_model.hpp102
-rw-r--r--core/schemas/ch.arknet.Turns.gschema.xml25
-rw-r--r--core/src/settings.cpp28
-rw-r--r--core/src/turn_order_model.cpp312
-rw-r--r--core/tests/disposition.cpp32
-rw-r--r--core/tests/glib_test_init.cpp28
-rw-r--r--core/tests/participant.cpp310
-rw-r--r--core/tests/single_participant.trns11
-rw-r--r--core/tests/turn_order_bugs.cpp40
-rw-r--r--core/tests/turn_order_model.cpp314
13 files changed, 0 insertions, 1353 deletions
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
- "$<$<CXX_COMPILER_ID:GNU,Clang>:-Wall>"
- "$<$<CXX_COMPILER_ID:GNU,Clang>:-Wextra>"
- "$<$<CXX_COMPILER_ID:GNU,Clang>:-Werror>"
- "$<$<CXX_COMPILER_ID:GNU,Clang>:-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
- "$<$<AND:$<CXX_COMPILER_ID:GNU,Clang>,$<CONFIG:Debug>>:--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 <glibmm/property.h>
-#include <glibmm/ustring.h>
-
-#include <nlohmann/json.hpp>
-
-#include <string>
-
-NLOHMANN_JSON_NAMESPACE_BEGIN
-
-template<typename T>
-struct adl_serializer<Glib::Property<T>>
-{
- static void to_json(json & to, Glib::Property<T> const & from)
- {
- to = from.get_value();
- }
-
- static void from_json(json const & from, Glib::Property<T> & to)
- {
- to = from.template get<T>();
- }
-};
-
-template<>
-struct adl_serializer<Glib::ustring>
-{
- static void to_json(json & to, Glib::ustring const & from)
- {
- to = static_cast<std::string>(from);
- }
-
- static void from_json(json const & from, Glib::ustring & to)
- {
- to = from.template get<std::string>();
- }
-};
-
-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 <glibmm/refptr.h>
-
-#include <giomm/settings.h>
-
-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<Gio::Settings>;
-} // 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 <glibmm/object.h>
-#include <glibmm/property.h>
-#include <glibmm/propertyproxy.h>
-#include <glibmm/refptr.h>
-#include <glibmm/ustring.h>
-
-#include <giomm/listmodel.h>
-
-#include <glib-object.h>
-#include <nlohmann/json_fwd.hpp>
-
-#include <limits>
-#include <optional>
-#include <vector>
-
-namespace turns::core
-{
-
- struct TurnOderModel : Gio::ListModel,
- Glib::Object
- {
- using value_type = Glib::RefPtr<Participant>;
- using container_type = std::vector<value_type>;
- 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<active_participant_type>::max();
- // auto static constexpr invalid_round_number = std::numeric_limits<round_number_type>::max();
-
- /** Life-time */
- // TurnOderModel();
-
- // auto static create() -> Glib::RefPtr<TurnOderModel>;
- // auto static create(nlohmann::json const & from) -> Glib::RefPtr<TurnOderModel>;
-
- /** Properties */
- // auto is_empty() const -> Glib::PropertyProxy_ReadOnly<is_empty_type>;
- auto has_next() const -> Glib::PropertyProxy_ReadOnly<has_next_type>;
- auto has_previous() const -> Glib::PropertyProxy_ReadOnly<has_previous_type>;
- auto is_running() const -> Glib::PropertyProxy_ReadOnly<is_running_type>;
- auto progress() const -> Glib::PropertyProxy_ReadOnly<progress_type>;
- auto round_number() const -> Glib::PropertyProxy_ReadOnly<round_number_type>;
- auto skip_defeated() -> Glib::PropertyProxy<skip_defeated_type>;
-
- /** 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<unsigned> m_active{};
-
- Glib::Property<has_next_type> m_has_next{*this, "has-next", false};
- Glib::Property<has_previous_type> m_has_previous{*this, "has-previous", false};
- Glib::Property<is_empty_type> m_is_empty{*this, "is-empty", true};
- Glib::Property<is_running_type> m_is_running{*this, "is-running", false};
- Glib::Property<progress_type> m_progress{*this, "progress", 0.0};
- Glib::Property<round_number_type> m_round_number{*this, "round-number", invalid_round_number};
- Glib::Property<skip_defeated_type> 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 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<schemalist>
- <schema path="/ch/arknet/Turns/" id="ch.arknet.Turns">
- <key name="disposition-color-friendly" type="s">
- <default>'#33d17a'</default>
- <summary>Friendly Disposition Color</summary>
- <description>The color used to shade friendly participants.</description>
- </key>
- <key name="disposition-color-hostile" type="s">
- <default>'#e01b24'</default>
- <summary>Hostile Disposition Color</summary>
- <description>The color used to shade hostile participants.</description>
- </key>
- <key name="disposition-color-secret" type="s">
- <default>'#9141ac'</default>
- <summary>Secret Disposition Color</summary>
- <description>The color used to shade secret participants.</description>
- </key>
- <key name="skip-defeated" type="b">
- <default>false</default>
- <summary>Skip Defeated Participants</summary>
- <description>Whether or not defeated participants shall be skipped while stepping through the turn order.</description>
- </key>
- </schema>
-</schemalist> \ 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 <glibmm/refptr.h>
-#include <glibmm/wrap.h>
-
-#include <giomm/settings.h>
-#include <giomm/settingsschemasource.h>
-
-#include <gio/gsettings.h>
-
-namespace turns::core
-{
-
- auto get_settings() -> Glib::RefPtr<Gio::Settings>
- {
- 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 <sigc++/adaptors/bind.h>
-#include <sigc++/functors/mem_fun.h>
-
-#include <glibmm/objectbase.h>
-#include <glibmm/propertyproxy.h>
-#include <glibmm/refptr.h>
-#include <glibmm/ustring.h>
-#include <glibmm/wrap.h>
-
-#include <giomm/listmodel.h>
-
-#include <glib-object.h>
-#include <nlohmann/json.hpp>
-
-#include <algorithm>
-#include <functional>
-#include <iterator>
-#include <ranges>
-#include <typeinfo>
-#include <vector>
-
-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<TurnOderModel>
- {
- return Glib::make_refptr_for_instance(new TurnOderModel{});
- }
-
- auto TurnOderModel::create(nlohmann::json const & from) -> Glib::RefPtr<TurnOderModel>
- {
- return Glib::make_refptr_for_instance(new TurnOderModel{from});
- }
-
- /** Queries */
-
- auto TurnOderModel::is_empty() const -> Glib::PropertyProxy_ReadOnly<is_empty_type>
- {
- return m_is_empty.get_proxy();
- }
-
- auto TurnOderModel::has_next() const -> Glib::PropertyProxy_ReadOnly<has_next_type>
- {
- return m_has_next.get_proxy();
- }
-
- auto TurnOderModel::has_previous() const -> Glib::PropertyProxy_ReadOnly<has_previous_type>
- {
- return m_has_previous.get_proxy();
- }
-
- auto TurnOderModel::is_running() const -> Glib::PropertyProxy_ReadOnly<is_running_type>
- {
- return m_is_running.get_proxy();
- }
-
- auto TurnOderModel::progress() const -> Glib::PropertyProxy_ReadOnly<progress_type>
- {
- return m_progress.get_proxy();
- }
-
- auto TurnOderModel::round_number() const -> Glib::PropertyProxy_ReadOnly<round_number_type>
- {
- return m_round_number.get_proxy();
- }
-
- auto TurnOderModel::skip_defeated() -> Glib::PropertyProxy<skip_defeated_type>
- {
- 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<float>(*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<float>(*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<float>(*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<nlohmann::json>{});
- 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<std::vector>();
- ;
- 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 <catch2/catch_test_macros.hpp>
-#include <catch2/generators/catch_generators.hpp>
-
-#include <glibmm/i18n.h>
-#include <glibmm/ustring.h>
-
-#include <format>
-#include <limits>
-#include <type_traits>
-#include <utility>
-
-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<Disposition>(std::numeric_limits<std::underlying_type_t<Disposition>>::max()),
- Glib::ustring{_("Unknown disposition value")}});
-
- SECTION(std::format("the presentation name for '{}' is '{}'", static_cast<std::underlying_type_t<Disposition>>(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 <catch2/reporters/catch_reporter_event_listener.hpp>
-#include <catch2/reporters/catch_reporter_registrars.hpp>
-
-#include <glibmm/init.h>
-
-#include <giomm/init.h>
-
-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 <catch2/catch_approx.hpp>
-#include <catch2/catch_test_macros.hpp>
-
-#include <nlohmann/json.hpp>
-
-#include <compare>
-
-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<core::Disposition>(static_cast<int>(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<core::Disposition>(static_cast<int>(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("