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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
#include "turnsmm/turn-order.hpp"
#include "turnsmm/participant.hpp"
#include <catch2/catch_test_macros.hpp>
#include <turnsmm/enums.hpp>
#include <cstddef>
#include <tuple>
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<std::size_t>("participant-count") == 0);
}
THEN("its running state is false")
{
REQUIRE_FALSE(instance.get_running());
REQUIRE_FALSE(instance.get_property<std::size_t>("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<Turns::Participant>(0) == nullptr);
}
THEN("its sort mode is descending")
{
REQUIRE(instance.get_sort_mode() == Turns::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::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::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);
}
}
}
}
}
|