-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathTags.cpp
More file actions
301 lines (287 loc) · 9.56 KB
/
Tags.cpp
File metadata and controls
301 lines (287 loc) · 9.56 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "catch.hpp"
#include "system.h"
#include <../runner_impl.h>
#include <choice.h>
#include <compiler.h>
#include <globals.h>
#include <runner.h>
#include <story.h>
using namespace ink::runtime;
SCENARIO("tags", "[ahf]")
{
std::unique_ptr<story> ink{story::from_file(INK_TEST_RESOURCE_DIR "AHF.bin")};
runner thread = ink->new_runner();
thread->move_to(ink::hash_string("test_knot"));
while (thread->can_continue()) {
auto line = thread->getline();
}
REQUIRE(thread->can_continue() == false);
}
SCENARIO("run story with tags", "[tags][story]")
{
GIVEN("a story with tags")
{
std::unique_ptr<story> _ink{story::from_file(INK_TEST_RESOURCE_DIR "TagsStory.bin")};
runner _thread = _ink->new_runner();
WHEN("starting the thread")
{
CHECK_FALSE(_thread->has_tags());
CHECK(_thread->get_current_knot() == 0);
}
WHEN("on the first line")
{
CHECK(_thread->getline() == "First line has global tags only\n");
THEN("it has the global tags")
{
CHECK(_thread->get_current_knot() == ink::hash_string("global_tags_only"));
CHECK(_thread->has_global_tags());
CHECK_FALSE(_thread->has_knot_tags());
CHECK(_thread->has_tags());
REQUIRE(_thread->num_global_tags() == 1);
CHECK(std::string(_thread->get_global_tag(0)) == "global_tag");
REQUIRE(_thread->num_tags() == 1);
CHECK(std::string(_thread->get_tag(0)) == "global_tag");
}
}
WHEN("on the second line")
{
_thread->getline();
CHECK(_thread->getline() == "Second line has one tag\n");
THEN("it has one tag")
{
CHECK(_thread->get_current_knot() == ink::hash_string("global_tags_only"));
CHECK(_thread->has_tags());
CHECK(_thread->num_global_tags() == 1);
CHECK(_thread->num_knot_tags() == 0);
REQUIRE(_thread->num_tags() == 1);
CHECK(std::string(_thread->get_tag(0)) == "tagged");
}
}
WHEN("on the third line")
{
_thread->getline();
_thread->getline();
CHECK(_thread->getline() == "Third line has two tags\n");
THEN("it has two tags")
{
CHECK(_thread->get_current_knot() == ink::hash_string("global_tags_only"));
CHECK(_thread->has_tags());
CHECK(_thread->num_global_tags() == 1);
CHECK(_thread->num_knot_tags() == 0);
REQUIRE(_thread->num_tags() == 2);
CHECK(std::string(_thread->get_tag(0)) == "tag next line");
CHECK(std::string(_thread->get_tag(1)) == "more tags");
}
}
WHEN("on the fourth line")
{
_thread->getline();
_thread->getline();
_thread->getline();
CHECK(_thread->getline() == "Fourth line has three tags\n");
THEN("it has three tags")
{
CHECK(_thread->get_current_knot() == ink::hash_string("global_tags_only"));
CHECK(_thread->has_tags());
CHECK(_thread->num_global_tags() == 1);
CHECK(_thread->num_knot_tags() == 0);
REQUIRE(_thread->num_tags() == 3);
CHECK(std::string(_thread->get_tag(0)) == "above");
CHECK(std::string(_thread->get_tag(1)) == "side");
CHECK(std::string(_thread->get_tag(2)) == "across");
}
}
WHEN("entering a knot")
{
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
CHECK(_thread->getline() == "Hello\n");
THEN("it has four tags")
{
CHECK(_thread->get_current_knot() == ink::hash_string("start"));
CHECK(_thread->has_tags());
CHECK(_thread->num_global_tags() == 1);
REQUIRE(_thread->num_tags() == 4);
CHECK(std::string(_thread->get_tag(0)) == "knot_tag_start");
CHECK(std::string(_thread->get_tag(1)) == "second_knot_tag_start");
CHECK(std::string(_thread->get_tag(2)) == "third_knot_tag");
CHECK(std::string(_thread->get_tag(3)) == "output_tag_h");
REQUIRE(_thread->num_knot_tags() == 3);
CHECK(std::string(_thread->get_knot_tag(0)) == "knot_tag_start");
CHECK(std::string(_thread->get_knot_tag(1)) == "second_knot_tag_start");
CHECK(std::string(_thread->get_knot_tag(2)) == "third_knot_tag");
}
}
WHEN("on the next line")
{
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
CHECK(_thread->getline() == "Second line has no tags\n");
THEN("it has no tags")
{
CHECK(_thread->get_current_knot() == ink::hash_string("start"));
CHECK(_thread->num_global_tags() == 1);
CHECK(_thread->num_knot_tags() == 3);
CHECK_FALSE(_thread->has_tags());
REQUIRE(_thread->num_tags() == 0);
}
}
WHEN("at the first choice list")
{
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
CHECK_FALSE(_thread->can_continue());
REQUIRE(std::distance(_thread->begin(), _thread->end()) == 2);
auto choice_list = _thread->begin();
THEN("check tags on choices")
{
CHECK(_thread->get_current_knot() == ink::hash_string("start"));
CHECK(_thread->num_global_tags() == 1);
CHECK(_thread->num_knot_tags() == 3);
CHECK(std::string(choice_list[0].text()) == "a");
CHECK_FALSE(choice_list[0].has_tags());
REQUIRE(choice_list[0].num_tags() == 0);
CHECK(std::string(choice_list[1].text()) == "b");
CHECK(choice_list[1].has_tags());
REQUIRE(choice_list[1].num_tags() == 2);
CHECK(std::string(choice_list[1].get_tag(0)) == "choice_tag_b");
CHECK(std::string(choice_list[1].get_tag(1)) == "choice_tag_b_2");
}
}
WHEN("selecting the second choice")
{
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->choose(1);
CHECK(_thread->getline() == "Knot2\n");
THEN("it has two tags")
{
CHECK(_thread->get_current_knot() == ink::hash_string("knot2.sub"));
INFO(_thread->get_knot_tag(0));
CHECK(_thread->num_global_tags() == 1);
CHECK(_thread->has_tags());
REQUIRE(_thread->num_tags() == 2);
CHECK(std::string(_thread->get_tag(0)) == "knot_tag_2");
CHECK(std::string(_thread->get_tag(1)) == "output_tag_k");
CHECK(_thread->has_knot_tags());
REQUIRE(_thread->num_knot_tags() == 1);
CHECK(std::string(_thread->get_knot_tag(0)) == "knot_tag_2");
}
}
WHEN("jumping to a knot")
{
_thread->move_to(ink::hash_string("knot2"));
REQUIRE(_thread->getline() == "Knot2\n");
THEN("global tags are missing")
{
CHECK(_thread->get_current_knot() == ink::hash_string("knot2.sub"));
CHECK(_thread->num_global_tags() == 0);
CHECK(_thread->has_tags());
REQUIRE(_thread->num_tags() == 2);
CHECK(std::string(_thread->get_tag(0)) == "knot_tag_2");
CHECK(std::string(_thread->get_tag(1)) == "output_tag_k");
CHECK(_thread->has_knot_tags());
REQUIRE(_thread->num_knot_tags() == 1);
CHECK(std::string(_thread->get_knot_tag(0)) == "knot_tag_2");
}
}
WHEN("at the second choice list")
{
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->choose(1);
_thread->getline();
CHECK(! _thread->can_continue());
REQUIRE(std::distance(_thread->begin(), _thread->end()) == 3);
auto choice_list = _thread->begin();
THEN("check tags on choices")
{
CHECK(_thread->get_current_knot() == ink::hash_string("knot2.sub"));
CHECK(_thread->num_global_tags() == 1);
CHECK(_thread->num_knot_tags() == 1);
CHECK(_thread->num_tags() == 2);
CHECK(std::string(choice_list[0].text()) == "e");
CHECK_FALSE(choice_list[0].has_tags());
REQUIRE(choice_list[0].num_tags() == 0);
CHECK(std::string(choice_list[1].text()) == "f with detail");
CHECK(choice_list[1].has_tags());
REQUIRE(choice_list[1].num_tags() == 4);
CHECK(std::string(choice_list[1].get_tag(0)) == "shared_tag");
CHECK(std::string(choice_list[1].get_tag(1)) == "shared_tag_2");
CHECK(std::string(choice_list[1].get_tag(2)) == "choice_tag");
CHECK(std::string(choice_list[1].get_tag(3)) == "choice_tag_2");
CHECK(std::string(choice_list[2].text()) == "g");
CHECK(choice_list[2].has_tags());
REQUIRE(choice_list[2].num_tags() == 1);
CHECK(std::string(choice_list[2].get_tag(0)) == "choice_tag_g");
}
}
WHEN("selecting the choice with shared tags")
{
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->choose(1);
_thread->getline();
_thread->choose(1);
REQUIRE(_thread->getline() == "f and content\n");
THEN("it has four tags")
{
CHECK(_thread->get_current_knot() == ink::hash_string("knot2.sub"));
CHECK(_thread->num_global_tags() == 1);
CHECK(_thread->num_knot_tags() == 1);
CHECK(_thread->has_tags());
REQUIRE(_thread->num_tags() == 4);
CHECK(std::string(_thread->get_tag(0)) == "shared_tag");
CHECK(std::string(_thread->get_tag(1)) == "shared_tag_2");
CHECK(std::string(_thread->get_tag(2)) == "content_tag");
CHECK(std::string(_thread->get_tag(3)) == "content_tag_2");
}
}
WHEN("on the last line")
{
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->getline();
_thread->choose(1);
_thread->getline();
_thread->choose(1);
_thread->getline();
CHECK(_thread->getline() == "out\n");
THEN("it has one tag")
{
CHECK(_thread->get_current_knot() == ink::hash_string("knot2.sub"));
CHECK(_thread->num_global_tags() == 1);
CHECK(std::string(_thread->get_global_tag(0)) == "global_tag");
REQUIRE(_thread->num_knot_tags() == 1);
CHECK(std::string(_thread->get_knot_tag(0)) == "knot_tag_2");
CHECK(_thread->has_tags());
REQUIRE(_thread->num_tags() == 1);
CHECK(std::string(_thread->get_tag(0)) == "close_tag");
}
}
}
}