-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathFixes.cpp
More file actions
297 lines (281 loc) · 9.31 KB
/
Fixes.cpp
File metadata and controls
297 lines (281 loc) · 9.31 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
#include "catch.hpp"
#include "snapshot.h"
#include "../snapshot_impl.h"
#include <story.h>
#include <globals.h>
#include <choice.h>
#include <runner.h>
#include <compiler.h>
#include <sstream>
using namespace ink::runtime;
SCENARIO("string_table fill up #97", "[fixes]")
{
GIVEN("story murder_scene")
{
std::unique_ptr<story> ink{story::from_file(INK_TEST_RESOURCE_DIR "murder_scene.bin")};
globals globStore = ink->new_globals();
runner main = ink->new_runner(globStore);
WHEN("Run first choice 50 times")
{
std::string story = "";
main->getall();
while (main->has_choices()) {
main->choose(0);
story += main->getall();
}
THEN("string table should still have room")
{
REQUIRE(story.length() == 3082);
// TEST string table size
}
}
}
}
SCENARIO("unknown command _ #109", "[fixes]")
{
GIVEN("story")
{
std::stringstream ss;
ss << "{\"inkVersion\":21,\"root\":[[\"ev\",{\"VAR?\":\"boolvar\"},\"out\",\"/"
"ev\",\"\\n\",\"ev\",{\"VAR?\":\"boolvar\"},\"_\",\"out\",\"/"
"ev\",\"\\n\",\"ev\",{\"VAR?\":\"boolvar\"},\"_\",\"/"
"ev\",[{\"->\":\".^.b\",\"c\":true},{\"b\":[\"^ first boolvar "
"\",{\"->\":\"0.16\"},null]}],\"nop\",\"\\n\",[\"ev\",{\"VAR?\":\"boolvar\"},\"/"
"ev\",{\"->\":\".^.b\",\"c\":true},{\"b\":[\"\\n\",\"^second "
"boolvar\",\"\\n\",{\"->\":\"0.19\"},null]}],\"nop\",\"\\n\",\"end\",[\"done\",{\"#n\":"
"\"g-0\"}],null],\"done\",{\"global "
"decl\":[\"ev\",true,{\"VAR=\":\"boolvar\"},\"/ev\",\"end\",null]}],\"listDefs\":{}}";
WHEN("Run")
{
std::stringstream out;
ink::compiler::compilation_results res;
ink::compiler::run(ss, out, &res);
unsigned char* data;
std::string out_str = out.str();
data = new unsigned char[out_str.size()];
for (size_t i = 0; i < out_str.size(); ++i) {
data[i] = out_str[i];
}
std::unique_ptr<story> ink{story::from_binary(data, static_cast<ink::size_t>(out_str.size()))
};
globals globStore = ink->new_globals();
runner main = ink->new_runner(globStore);
std::string story = main->getall();
THEN("expect correct output")
{
REQUIRE(res.warnings.size() == 0);
REQUIRE(res.errors.size() == 0);
REQUIRE(
story ==
R"(true
-1
first boolvar
second boolvar
)"
);
}
}
}
}
SCENARIO("snapshot failed inside execution _ #111", "[fixes]")
{
GIVEN("story with multiline output with a knot")
{
std::unique_ptr<story> ink{story::from_file(INK_TEST_RESOURCE_DIR "111_crash.bin")};
std::unique_ptr<story> ink2{story::from_file(INK_TEST_RESOURCE_DIR "111_crash.bin")};
runner thread = ink->new_runner();
WHEN("run store and reload")
{
auto line = thread->getline();
THEN("outputs first line") { REQUIRE(line == "First line of text\n"); }
std::unique_ptr<ink::runtime::snapshot> snapshot{thread->create_snapshot()};
runner thread2 = ink2->new_runner_from_snapshot(*snapshot);
line = thread->getline();
THEN("outputs second line") { REQUIRE(line == "Second line of test\n"); }
}
}
}
SCENARIO("missing leading whitespace inside choice-only text and glued text _ #130 #131", "[fixes]")
{
GIVEN("story with problematic text")
{
std::unique_ptr<story> ink{story::from_file(INK_TEST_RESOURCE_DIR
"130_131_missing_whitespace.bin")};
runner thread = ink->new_runner();
WHEN("run story")
{
auto line = thread->getline();
THEN("expect spaces in glued text") { REQUIRE(line == "Glue with no gaps.\n"); }
THEN("choice contains space")
{
REQUIRE(thread->num_choices() == 1);
REQUIRE(std::string(thread->get_choice(0)->text()) == "Look around");
}
thread->choose(0);
line = thread->getall();
THEN("no space in post choice text")
{
REQUIRE(line == "Looking around the saloon, you don't find much.");
}
}
}
}
SCENARIO(
"choice tag references are not correctly stored (as pointer instead of index) _ #116", "[fixes]"
)
{
GIVEN("story with choice tag")
{
std::unique_ptr<story> ink{story::from_file(INK_TEST_RESOURCE_DIR
"116_story_with_choice_tags.bin")};
runner thread = ink->new_runner();
WHEN("run story, store, and reload")
{
thread->getall();
REQUIRE(thread->num_choices() == 1);
REQUIRE(thread->get_choice(0)->num_tags() == 1);
REQUIRE(thread->get_choice(0)->get_tag(0) == std::string("Type:Idle"));
std::unique_ptr<snapshot> snap{thread->create_snapshot()};
THEN("snapshot loaded works")
{
runner loaded = ink->new_runner_from_snapshot(*snap);
loaded->getall();
REQUIRE(loaded->num_choices() == 1);
REQUIRE(loaded->get_choice(0)->num_tags() == 1);
REQUIRE(loaded->get_choice(0)->get_tag(0) == std::string("Type:Idle"));
}
}
WHEN("loading a snipshot multiple times")
{
thread->getall();
std::unique_ptr<snapshot> snap{thread->create_snapshot()};
runner thread2 = ink->new_runner_from_snapshot(*snap);
const size_t s = reinterpret_cast<internal::snapshot_impl*>(snap.get())->strings().size();
THEN("loading it again will not change the string_table size")
{
runner thread3 = ink->new_runner_from_snapshot(*snap);
const size_t s2 = reinterpret_cast<internal::snapshot_impl*>(snap.get())->strings().size();
REQUIRE(s == s2);
}
}
}
}
SCENARIO("Casting during redefinition is too strict _ #134", "[fixes]")
{
GIVEN("story with problematic text")
{
auto ink = story::from_file(INK_TEST_RESOURCE_DIR "134_restrictive_casts.bin");
runner thread = ink->new_runner();
WHEN("run story")
{
// Initial casts/assignments are allowed.
auto line = thread->getline();
THEN("expect initial values") { REQUIRE(line == "true 1 1 text A\n"); }
line = thread->getline();
THEN("expect evaluated") { REQUIRE(line == "1.5 1.5 1.5 text0.5 B\n"); }
line = thread->getline();
THEN("expect assigned") { REQUIRE(line == "1.5 1.5 1.5 text0.5 B\n"); }
}
// Six cases that should fail. We can't pollute lookahead with these so they need to be
// separated out.
for (int i = 0; i < 6; ++i) {
WHEN("Jump to failing case")
{
const std::string name = "Fail" + std::to_string(i);
REQUIRE_NOTHROW(thread->move_to(ink::hash_string(name.c_str())));
std::string line;
REQUIRE_THROWS_AS(line = thread->getline(), ink::ink_exception);
}
}
}
}
SCENARIO("Using knot visit count as condition _ #139", "[fixes]")
{
GIVEN("story with conditional choice.")
{
std::unique_ptr<story> ink{story::from_file(INK_TEST_RESOURCE_DIR "139_conditional_choice.bin")
};
runner thread = ink->new_runner();
WHEN("visit knot 'one' an going back to choice")
{
std::string content = thread->getall();
REQUIRE_FALSE(thread->can_continue());
REQUIRE(thread->num_choices() == 2);
thread->choose(1);
content += thread->getall();
REQUIRE(content == "Check\nFirst time at one\n");
THEN("conditinal choice is displayed")
{
REQUIRE(thread->num_choices() == 3);
CHECK(thread->get_choice(0)->text() == std::string("DEFAULT"));
CHECK(thread->get_choice(1)->text() == std::string("Check"));
CHECK(thread->get_choice(2)->text() == std::string("Test"));
WHEN("go to 'one' twice")
{
thread->choose(1);
std::string content2 = thread->getall();
REQUIRE(thread->num_choices() == 3);
THEN("get both one strings") { REQUIRE(content2 == "Check\nBeen here before\n"); }
}
}
}
WHEN("loop back to choice")
{
std::string content = thread->getall();
REQUIRE_FALSE(thread->can_continue());
REQUIRE(thread->num_choices() == 2);
thread->choose(0);
content += thread->getall();
REQUIRE(content == "DEFAULT\nLoopback");
THEN("conditinal choice is not displayed")
{
REQUIRE(thread->num_choices() == 2);
CHECK(thread->get_choice(0)->text() == std::string("DEFAULT"));
CHECK(thread->get_choice(1)->text() == std::string("Check"));
}
}
}
}
SCENARIO("Provoke thread array expension _ #142", "[fixes]")
{
GIVEN("story with 15 threads in one know")
{
std::unique_ptr<story> ink{story::from_file(INK_TEST_RESOURCE_DIR "142_many_threads.bin")};
runner thread = ink->new_runner();
WHEN("just go to choice")
{
std::string content = thread->getall();
REQUIRE(content == "At the top\n");
THEN("expect to see 15 choices")
{
REQUIRE(thread->num_choices() == 15);
const char options[] = "abcdefghijklmno";
for (const char* c = options; *c; ++c) {
CHECK(thread->get_choice(static_cast<size_t>(c - options))->text()[0] == *c);
}
}
}
WHEN("choose 5 options")
{
std::string content = thread->getall();
for (int i = 0; i < 5; ++i) {
REQUIRE_FALSE(thread->can_continue());
thread->choose(i);
content += thread->getall();
}
REQUIRE(
content
== "At the top\na\nAt the top\nc\nAt the top\ne\nAt the top\ng\nAt the top\ni\nAt the "
"top\n"
);
THEN("only 11 choices are left")
{
REQUIRE(thread->num_choices() == 10);
const char* options = "bdfhjklmno";
for (const char* c = options; *c; ++c) {
CHECK(thread->get_choice(static_cast<size_t>(c - options))->text()[0] == *c);
}
}
}
}
}