summaryrefslogtreecommitdiff
path: root/core/tests
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2025-05-08 22:00:54 +0200
committerFelix Morgner <felix.morgner@gmail.com>2025-05-08 22:00:54 +0200
commit87c6607602e969d94ed8b8f97bb45f416e37b22d (patch)
treea9c01322cfb9f32753ea55519937e6c0e3ee1892 /core/tests
parent917d1bc227490918854b827a856fd0613745c452 (diff)
downloadturns-87c6607602e969d94ed8b8f97bb45f416e37b22d.tar.xz
turns-87c6607602e969d94ed8b8f97bb45f416e37b22d.zip
core/tests: improve turn_order tests
Diffstat (limited to 'core/tests')
-rw-r--r--core/tests/single_participant.trns11
-rw-r--r--core/tests/turn_order.cpp91
2 files changed, 102 insertions, 0 deletions
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 <catch2/catch_test_macros.hpp>
+#include <giomm/resource.h>
+
+#include <nlohmann/json.hpp>
+
+#include <string_view>
+
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<char const *>(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<Participant>(0);
+ REQUIRE_FALSE(participant->get_name().empty());
+ }
+
+ AND_THEN("adding another participant with a higher priority")
+ {
+ auto participant = instance.get_typed_object<Participant>(0);
+ instance.add("Higher Priority", 100.0f, Disposition::Neutral);
+
+ THEN("the index of the test participant is 1")
+ {
+ CHECK(instance.get_typed_object<Participant>(0) != participant);
+ REQUIRE(instance.get_typed_object<Participant>(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<Participant>(0)->get_name() == "A");
+ REQUIRE(instance.get_typed_object<Participant>(1)->get_name() == "B");
+ REQUIRE(instance.get_typed_object<Participant>(2)->get_name() == "C");
+ }
+
+ WHEN("changing the priority of the B to 50")
+ {
+ instance.get_typed_object<Participant>(1)->set_priority(50);
+
+ THEN("the order stays the same")
+ {
+ REQUIRE(instance.get_typed_object<Participant>(0)->get_name() == "A");
+ REQUIRE(instance.get_typed_object<Participant>(1)->get_name() == "B");
+ REQUIRE(instance.get_typed_object<Participant>(2)->get_name() == "C");
+ }
+ }
+ }
+ }
+
} // namespace turns::core::tests \ No newline at end of file