From 87c6607602e969d94ed8b8f97bb45f416e37b22d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 8 May 2025 22:00:54 +0200 Subject: core/tests: improve turn_order tests --- core/CMakeLists.txt | 8 ++++ core/tests/single_participant.trns | 11 +++++ core/tests/turn_order.cpp | 91 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 core/tests/single_participant.trns (limited to 'core') diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 5ec88e4..b52519f 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -65,4 +65,12 @@ 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/tests/single_participant.trns b/core/tests/single_participant.trns new file mode 100644 index 0000000..bcce137 --- /dev/null +++ b/core/tests/single_participant.trns @@ -0,0 +1,11 @@ +{ + "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.cpp b/core/tests/turn_order.cpp index ceed924..a7633cd 100644 --- a/core/tests/turn_order.cpp +++ b/core/tests/turn_order.cpp @@ -5,6 +5,12 @@ #include +#include + +#include + +#include + namespace turns::core::tests { SCENARIO("Queries on a fresh turn_order instance", "[turn_order]") @@ -220,4 +226,89 @@ namespace turns::core::tests } } + SCENARIO("Loading from json data") + { + GIVEN("an empty turn order") + { + auto instance = turn_order{}; + 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 = turn_order{}; + + 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