summaryrefslogtreecommitdiff
path: root/lib/tests/turns-turn-order.cpp
blob: ab119e19a8e5b37df5aa001870294ec9ee477b05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * SPDX-FileCopyrightText: 2025 Felix Morgner <felix.morgner@gmail.com>
 * SPDX-License-Identifier: LGPL-2.1-only
 */

#include "turns-turn-order.h"

#include "turns-participant.h"

#include <catch2/catch_test_macros.hpp>

#include <gio/gio.h>
#include <glib-object.h>

SCENARIO("Creating a turn order", "[lib][object][lifetime]")
{
  GIVEN("A turn order constructed using turns_turn_order_new()")
  {
    g_autoptr(TurnsTurnOrder) instance = turns_turn_order_new();

    THEN("its participant count is 0")
    {
      REQUIRE(turns_turn_order_get_participant_count(instance) == 0uz);
    }

    THEN("its running state is false")
    {
      REQUIRE_FALSE(turns_turn_order_get_running(instance));

      auto property_value = decltype(turns_turn_order_get_participant_count(instance)){};
      g_object_get(instance, "running", &property_value, nullptr);

      REQUIRE_FALSE(property_value);
    }

    THEN("its item count is 0")
    {
      REQUIRE(g_list_model_get_n_items(G_LIST_MODEL(instance)) == 0);
    }

    THEN("its item type is Turns.Participant")
    {
      REQUIRE(g_list_model_get_item_type(G_LIST_MODEL(instance)) == turns_participant_get_type());
    }

    THEN("its first item is NULL")
    {
      REQUIRE(g_list_model_get_item(G_LIST_MODEL(instance), 0) == nullptr);
      REQUIRE(g_list_model_get_object(G_LIST_MODEL(instance), 0) == nullptr);
    }
  }
}

SCENARIO("Modifying a turn order", "[lib][object][data]")
{
  GIVEN("An empty turn order")
  {
    g_autoptr(TurnsTurnOrder) instance = turns_turn_order_new();
    CHECK(turns_turn_order_get_participant_count(instance) == 0);

    WHEN("a participant is added")
    {
      g_autoptr(TurnsParticipant) participant = turns_participant_new_with("Test Participant", 10.0f, TURNS_PARTICIPANT_DISPOSITION_FRIENDLY);
      turns_turn_order_add(instance, participant);

      THEN("its participant count is 1")
      {
        REQUIRE(turns_turn_order_get_participant_count(instance) == 1);
      }

      THEN("its running state is false")
      {
        REQUIRE_FALSE(turns_turn_order_get_running(instance));
      }

      THEN("its item count is 1")
      {
        REQUIRE(g_list_model_get_n_items(G_LIST_MODEL(instance)) == 1);
      }

      THEN("its first item is the same participant")
      {
        g_autoptr(TurnsParticipant) item = TURNS_PARTICIPANT(g_list_model_get_item(G_LIST_MODEL(instance), 0));
        REQUIRE(item == participant);

        g_autoptr(TurnsParticipant) object = TURNS_PARTICIPANT(g_list_model_get_object(G_LIST_MODEL(instance), 0));
        REQUIRE(TURNS_PARTICIPANT(object) == participant);
      }

      AND_WHEN("calling clear")
      {
        turns_turn_order_clear(instance);

        THEN("its participant count is 0")
        {
          REQUIRE(turns_turn_order_get_participant_count(instance) == 0);
        }

        THEN("its running state is false")
        {
          REQUIRE_FALSE(turns_turn_order_get_running(instance));
        }

        THEN("its item count is 0")
        {
          REQUIRE(g_list_model_get_n_items(G_LIST_MODEL(instance)) == 0);
        }

        THEN("its first item is NULL")
        {
          REQUIRE(g_list_model_get_item(G_LIST_MODEL(instance), 0) == nullptr);
          REQUIRE(g_list_model_get_object(G_LIST_MODEL(instance), 0) == nullptr);
        }
      }
    }
  }
}

SCENARIO("Sorting a turn order")
{
  GIVEN("A turn order with two participants - A/10/H and B/20/N")
  {
    g_autoptr(TurnsTurnOrder) instance = turns_turn_order_new();
    g_autoptr(TurnsParticipant) participant_a = turns_participant_new_with("A", 10, TURNS_PARTICIPANT_DISPOSITION_HOSTILE);
    g_autoptr(TurnsParticipant) participant_b = turns_participant_new_with("B", 20, TURNS_PARTICIPANT_DISPOSITION_NEUTRAL);

    turns_turn_order_add(instance, participant_a);
    turns_turn_order_add(instance, participant_b);

    THEN("B is the first and A 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_b);
      REQUIRE(second == participant_a);
    }

    WHEN("the priority of A is changed to 30")
    {
      turns_participant_set_priority(participant_a, 30);

      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);
      }
    }

    WHEN("the priority of A is changed to 0")
    {
      turns_participant_set_priority(participant_a, 0);

      THEN("B is the first and A 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_b);
        REQUIRE(second == participant_a);
      }
    }

    WHEN("the priority of B is changed to 0")
    {
      turns_participant_set_priority(participant_b, 0);

      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);
      }
    }

    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);
      }
    }
  }
}