#include "turnsmm/turn-order.hpp" #include "turnsmm/participant.hpp" #include #include #include SCENARIO("Creating a turn order", "[lib][object][lifetime]") { GIVEN("A turn order constructed using the default constructor") { auto instance = Turns::TurnOrder{}; THEN("its participant count is 0") { REQUIRE(instance.get_participant_count() == 0uz); REQUIRE(instance.get_property("participant-count") == 0); } THEN("its running state is false") { REQUIRE_FALSE(instance.get_running()); REQUIRE_FALSE(instance.get_property("running")); } THEN("its item count is 0") { REQUIRE(instance.get_n_items() == 0); } THEN("its item type is Turns.Participant") { REQUIRE(instance.get_item_type() == Turns::Participant::get_type()); } THEN("its first item is NULL") { REQUIRE(instance.get_object(0) == nullptr); REQUIRE(instance.get_typed_object(0) == nullptr); } THEN("its sort mode is descending") { REQUIRE(instance.get_sort_mode() == Turns::TurnOrder::SortMode::Descending); } } } SCENARIO("Modifying a turn order", "[lib][object][data]") { GIVEN("An empty turn order") { auto instance = Turns::TurnOrder{}; CHECK(instance.get_participant_count() == 0); WHEN("a participant is added") { auto participant = Turns::Participant::create("Test Participant", 10.0f, Turns::Participant::Disposition::Friendly); instance.add(participant); THEN("its participant count is 1") { REQUIRE(instance.get_participant_count() == 1); } THEN("its running state is false") { REQUIRE_FALSE(instance.get_running()); } THEN("its item count is 1") { REQUIRE(instance.get_n_items() == 1); } THEN("its first item is the same participant") { auto obj = instance.get_object(0); auto cnt = obj->gobj()->ref_count; REQUIRE(cnt > 0); REQUIRE(obj == participant); } THEN("its sort mode is descending") { REQUIRE(instance.get_sort_mode() == Turns::TurnOrder::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::Participant::Disposition::Hostile); auto participant_b = Turns::Participant::create("B", 20, Turns::Participant::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(0); auto second = instance.get_typed_object(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(0); auto second = instance.get_typed_object(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(0); auto second = instance.get_typed_object(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(0); auto second = instance.get_typed_object(1); REQUIRE(first == participant_a); REQUIRE(second == participant_b); } } WHEN("the sort mode is changed to ascending") { instance.set_sort_mode(Turns::TurnOrder::SortMode::Ascending); THEN("A is the first and B the second element") { auto first = instance.get_typed_object(0); auto second = instance.get_typed_object(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::TurnOrder::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); } } } } }