From 260df0a1158385736b2da24a2e4a14e365d1ec92 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 23 May 2025 11:06:04 +0200 Subject: lib: add remove_at to TurnOrder --- lib/tests/turns-turn-order.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'lib/tests/turns-turn-order.cpp') diff --git a/lib/tests/turns-turn-order.cpp b/lib/tests/turns-turn-order.cpp index ab119e1..2e11b52 100644 --- a/lib/tests/turns-turn-order.cpp +++ b/lib/tests/turns-turn-order.cpp @@ -11,6 +11,20 @@ #include #include +#include + +#include +#include + +namespace +{ + auto list_model_notification = std::optional>{}; + + auto on_list_model_notification(GListModel *, guint position, guint removed, guint added, void *) -> void + { + list_model_notification = std::tuple{position, removed, added}; + } +} // namespace SCENARIO("Creating a turn order", "[lib][object][lifetime]") { @@ -58,6 +72,8 @@ SCENARIO("Modifying a turn order", "[lib][object][data]") g_autoptr(TurnsTurnOrder) instance = turns_turn_order_new(); CHECK(turns_turn_order_get_participant_count(instance) == 0); + g_signal_connect(instance, "items-changed", reinterpret_cast(on_list_model_notification), nullptr); + WHEN("a participant is added") { g_autoptr(TurnsParticipant) participant = turns_participant_new_with("Test Participant", 10.0f, TURNS_PARTICIPANT_DISPOSITION_FRIENDLY); @@ -89,6 +105,7 @@ SCENARIO("Modifying a turn order", "[lib][object][data]") AND_WHEN("calling clear") { + list_model_notification.reset(); turns_turn_order_clear(instance); THEN("its participant count is 0") @@ -111,6 +128,61 @@ SCENARIO("Modifying a turn order", "[lib][object][data]") REQUIRE(g_list_model_get_item(G_LIST_MODEL(instance), 0) == nullptr); REQUIRE(g_list_model_get_object(G_LIST_MODEL(instance), 0) == nullptr); } + + THEN("the items-changed notification is emitted") + { + REQUIRE(list_model_notification.has_value()); + } + + THEN("all items got deleted at position 0 and none were added") + { + auto [position, removed, added] = *list_model_notification; + + REQUIRE(position == 0); + REQUIRE(removed == 1); + REQUIRE(added == 0); + } + } + + AND_WHEN("removing the first element") + { + list_model_notification.reset(); + turns_turn_order_remove_at(instance, 0); + + THEN("its participant count is 0") + { + REQUIRE(turns_turn_order_get_participant_count(instance) == 0); + } + + THEN("the items-changed notification is emitted") + { + REQUIRE(list_model_notification.has_value()); + } + + THEN("1 item got deleted at position 0 and none were added") + { + auto [position, removed, added] = *list_model_notification; + + REQUIRE(position == 0); + REQUIRE(removed == 1); + REQUIRE(added == 0); + } + + AND_WHEN("removing the first element again") + { + list_model_notification.reset(); + turns_turn_order_remove_at(instance, 0); + + THEN("its participant count is 0") + { + REQUIRE(turns_turn_order_get_participant_count(instance) == 0); + } + + THEN("the items-changed notification is not emitted") + { + REQUIRE(!list_model_notification.has_value()); + } + } } } } -- cgit v1.2.3