diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2025-05-19 13:42:39 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2025-05-19 13:42:39 +0200 |
| commit | 0a5f3b25214c11556f62ce04601c06812a3464d8 (patch) | |
| tree | d35ed88fc142927f6d34af7e0ddcc4bafb25145a /lib/tests | |
| parent | d0b647c3156b07c70e9831aa86ef76ee40413d0c (diff) | |
| download | turns-0a5f3b25214c11556f62ce04601c06812a3464d8.tar.xz turns-0a5f3b25214c11556f62ce04601c06812a3464d8.zip | |
lib: add support for different sort modes
Diffstat (limited to 'lib/tests')
| -rw-r--r-- | lib/tests/turns-turn-order.cpp | 14 | ||||
| -rw-r--r-- | lib/tests/turnsmm/turn-order.cpp | 129 |
2 files changed, 143 insertions, 0 deletions
diff --git a/lib/tests/turns-turn-order.cpp b/lib/tests/turns-turn-order.cpp index 9f4d5d6..d0224ce 100644 --- a/lib/tests/turns-turn-order.cpp +++ b/lib/tests/turns-turn-order.cpp @@ -148,5 +148,19 @@ SCENARIO("Sorting a turn order") REQUIRE(second == participant_b); } } + + WHEN("the sort mode is changed to ascending") + { + turns_turn_order_set_sort_mode(instance, TURNS_TURN_ORDER_SORT_MODE_ASCENDING); + + THEN("A is the first and B the second element") + { + g_autoptr(TurnsParticipant) first = TURNS_PARTICIPANT(g_list_model_get_object(G_LIST_MODEL(instance), 0)); + g_autoptr(TurnsParticipant) second = TURNS_PARTICIPANT(g_list_model_get_object(G_LIST_MODEL(instance), 1)); + + REQUIRE(first == participant_a); + REQUIRE(second == participant_b); + } + } } }
\ No newline at end of file diff --git a/lib/tests/turnsmm/turn-order.cpp b/lib/tests/turnsmm/turn-order.cpp index 21c75d3..fcb7727 100644 --- a/lib/tests/turnsmm/turn-order.cpp +++ b/lib/tests/turnsmm/turn-order.cpp @@ -7,6 +7,7 @@ #include <turnsmm/enums.hpp> #include <cstddef> +#include <tuple> SCENARIO("Creating a turn order", "[lib][object][lifetime]") { @@ -41,6 +42,11 @@ SCENARIO("Creating a turn order", "[lib][object][lifetime]") REQUIRE(instance.get_object(0) == nullptr); REQUIRE(instance.get_typed_object<Turns::Participant>(0) == nullptr); } + + THEN("its sort mode is descending") + { + REQUIRE(instance.get_sort_mode() == Turns::SortMode::Descending); + } } } @@ -78,6 +84,129 @@ SCENARIO("Modifying a turn order", "[lib][object][data]") REQUIRE(cnt > 0); REQUIRE(obj == participant); } + + THEN("its sort mode is descending") + { + REQUIRE(instance.get_sort_mode() == Turns::SortMode::Descending); + } } } } + +SCENARIO("Sorting a turn order") +{ + GIVEN("A turn order with two participants - A/10/H and B/20/N") + { + auto instance = Turns::TurnOrder{}; + auto participant_a = Turns::Participant::create("A", 10, Turns::Disposition::Hostile); + auto participant_b = Turns::Participant::create("B", 20, Turns::Disposition::Neutral); + + instance.add(participant_a); + instance.add(participant_b); + + THEN("B is the first and A the second element") + { + auto first = instance.get_typed_object<Turns::Participant>(0); + auto second = instance.get_typed_object<Turns::Participant>(1); + + REQUIRE(first == participant_b); + REQUIRE(second == participant_a); + } + + WHEN("the priority of A is changed to 30") + { + participant_a->set_priority(30); + + THEN("A is the first and B the second element") + { + auto first = instance.get_typed_object<Turns::Participant>(0); + auto second = instance.get_typed_object<Turns::Participant>(1); + + REQUIRE(first == participant_a); + REQUIRE(second == participant_b); + } + } + + WHEN("the priority of A is changed to 0") + { + participant_a->set_priority(0); + + THEN("B is the first and A the second element") + { + auto first = instance.get_typed_object<Turns::Participant>(0); + auto second = instance.get_typed_object<Turns::Participant>(1); + + REQUIRE(first == participant_b); + REQUIRE(second == participant_a); + } + } + + WHEN("the priority of B is changed to 0") + { + participant_b->set_priority(0); + + THEN("A is the first and B the second element") + { + auto first = instance.get_typed_object<Turns::Participant>(0); + auto second = instance.get_typed_object<Turns::Participant>(1); + + REQUIRE(first == participant_a); + REQUIRE(second == participant_b); + } + } + + WHEN("the sort mode is changed to ascending") + { + instance.set_sort_mode(Turns::SortMode::Ascending); + + THEN("A is the first and B the second element") + { + auto first = instance.get_typed_object<Turns::Participant>(0); + auto second = instance.get_typed_object<Turns::Participant>(1); + + REQUIRE(first == participant_a); + REQUIRE(second == participant_b); + } + } + + WHEN("a signal handler is registered for the 'items-changed' signal") + { + auto notified = false; + auto data = std::tuple{0u, 0u, 0u}; + auto callback = [&](auto position, auto removed, auto added) { + notified = true, data = std::tie(position, removed, added); + }; + instance.signal_items_changed().connect(callback); + + AND_WHEN("the sort mode is changed to ascending") + { + instance.set_sort_mode(Turns::SortMode::Ascending); + + THEN("the signal is notified") + { + REQUIRE(notified); + } + + THEN("all items were removed and added again at position 0") + { + auto [position, removed, added] = data; + + REQUIRE(position == 0); + REQUIRE(removed == 2); + REQUIRE(added == 2); + } + } + + AND_WHEN("the sort mode is set to the current value") + { + auto current_sort_mode = instance.get_sort_mode(); + instance.set_sort_mode(current_sort_mode); + + THEN("the signal is not notified") + { + REQUIRE_FALSE(notified); + } + } + } + } +}
\ No newline at end of file |
