-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.cpp
3063 lines (2559 loc) · 99 KB
/
tests.cpp
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <gtest/gtest.h>
#include "ink_compiler.h"
#include "runtime/ink_story.h"
#include "ink_utils.h"
#include "expression_parser/expression_parser.h"
#include <utility>
#include <cstdlib>
#define FIXTURE(name) class name : public testing::Test {\
protected:\
InkCompiler compiler;\
}
static bool serialize_mode = false;
#define STORY(path) InkStory story; if (serialize_mode) {\
compiler.compile_file_to_file(INKCPP_WORKING_DIR "/tests/" path, std::string(INKCPP_WORKING_DIR "/tests/" path) + "b"); story = InkStory(std::string(INKCPP_WORKING_DIR "/tests/" path) + "b");\
} else {\
story = compiler.compile_file(INKCPP_WORKING_DIR "/tests/" path);\
}
#define STORY_INC(path, include_root) InkStory story; compiler.set_root_include_path(include_root); if (serialize_mode) {\
compiler.compile_file_to_file(INKCPP_WORKING_DIR "/tests/" path, std::string(INKCPP_WORKING_DIR "/tests/" path) + "b"); story = InkStory(std::string(INKCPP_WORKING_DIR "/tests/" path) + "b");\
} else {\
story = compiler.compile_file(INKCPP_WORKING_DIR "/tests/" path);\
}
#define EXPECT_TEXT(...) {\
std::vector<std::string> seq = {__VA_ARGS__};\
for (const std::string& expected_text : seq) {\
EXPECT_EQ(story.continue_story(), expected_text);\
}\
}
#define EXPECT_CHOICES(...) {\
std::vector<std::string> expected_choices = {__VA_ARGS__};\
EXPECT_EQ(story.get_current_choices(), expected_choices);\
}
template <typename TT, typename DT>
bool token_matches(ExpressionParserV2::Token& token, DT data) {
if (auto* token_cast = dynamic_cast<TT*>(token)) {
return token_cast->data == data;
}
return false;
}
#define EXPECT_TOKENS(tokens, ...) {\
std::vector<ExpressionParser::TokenType> types = {__VA_ARGS__};\
EXPECT_EQ(types.size(), tokens.size());\
for (unsigned int i = 0; i < tokens.size(); ++i) {\
EXPECT_EQ(tokens[i].type, types[i]);\
}\
}
////////////////////////////////////////////////////////////////////////////////////////////////////
FIXTURE(NonStoryFunctionTests);
FIXTURE(ExpressionParserTests);
FIXTURE(ContentTests);
FIXTURE(ChoiceTests);
FIXTURE(KnotTests);
FIXTURE(DivertTests);
FIXTURE(BranchingTests);
FIXTURE(StitchTests);
FIXTURE(VaryingChoiceTests);
FIXTURE(VariableTextTests);
FIXTURE(GameQueriesTests);
FIXTURE(GatherTests);
FIXTURE(NestedFlowTests);
FIXTURE(TrackingWeaveTests);
FIXTURE(GlobalVariableTests);
FIXTURE(LogicTests);
FIXTURE(ConditionalBlockTests);
FIXTURE(TemporaryVariableTests);
FIXTURE(FunctionTests);
FIXTURE(ConstantTests);
FIXTURE(TunnelTests);
FIXTURE(ThreadTests);
FIXTURE(ListTests);
FIXTURE(MiscBugTests);
FIXTURE(LongExampleTests);
FIXTURE(ErrorTests);
FIXTURE(MiscellaneousTests);
FIXTURE(InkProof);
#pragma region NonStoryFunctionTests
TEST_F(NonStoryFunctionTests, InkListFunctions) {
InkListDefinitionMap definition_map;
definition_map.add_list_definition("colors", {{"red", 1}, {"orange", 2}, {"yellow", 3}});
definition_map.add_list_definition("days", {{"sunday", 1}, {"monday", 2}, {"tuesday", 3}, {"wednesday", 4}, {"thursday", 5}, {"friday", 6}, {"saturday", 7}});
InkList best_days{{"friday", "saturday", "sunday"}, definition_map};
InkList weekends{{"saturday", "sunday"}, definition_map};
EXPECT_TRUE(best_days.contains(weekends));
EXPECT_EQ(best_days + weekends, best_days);
EXPECT_EQ(best_days - weekends, InkList({"friday"}, definition_map));
EXPECT_EQ(best_days ^ weekends, weekends);
InkList all{{"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}, definition_map};
InkList all2 = best_days.all_possible_items();
EXPECT_EQ(all, all2);
InkList combined_list{{"red", "yellow", "tuesday"}, definition_map};
EXPECT_FALSE(combined_list.contains(weekends));
EXPECT_EQ(combined_list + weekends, InkList({"red", "yellow", "tuesday", "saturday", "sunday"}, definition_map));
EXPECT_EQ(combined_list - weekends, combined_list);
EXPECT_EQ(combined_list ^ weekends, InkList(definition_map));
InkList all_c{{"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "red", "orange", "yellow"}, definition_map};
InkList all2_c = combined_list.all_possible_items();
EXPECT_EQ(all_c, all2_c);
// advanced operations
InkList inverse{{"monday", "tuesday", "wednesday", "thursday"}, definition_map};
InkList inverse2 = best_days.inverted();
EXPECT_EQ(inverse, inverse2);
InkList inverse_c{{"orange", "monday", "wednesday", "thursday", "friday", "saturday", "sunday"}, definition_map};
InkList inverse2_c = combined_list.inverted();
EXPECT_EQ(inverse_c, inverse2_c);
EXPECT_FALSE(weekends > best_days);
EXPECT_TRUE(weekends >= best_days);
EXPECT_FALSE(weekends < best_days);
EXPECT_TRUE(weekends <= best_days);
}
#pragma endregion
#pragma region ExpressionParserTests
TEST_F(ExpressionParserTests, BasicTokenization) {
namespace ExpressionParser = ExpressionParserV2;
using ExpressionParser::Token;
using ExpressionParser::TokenType;
std::string exp = "test = 5 + 7";
ExpressionParser::StoryVariableInfo blank_variable_info;
std::vector<ExpressionParser::Token> result = ExpressionParser::tokenize_expression(exp, blank_variable_info);
EXPECT_TOKENS(result,
ExpressionParser::TokenType::Variable,
ExpressionParser::TokenType::Operator,
ExpressionParser::TokenType::LiteralNumberInt,
ExpressionParser::TokenType::Operator,
ExpressionParser::TokenType::LiteralNumberInt,
);
std::vector<ExpressionParser::Token> result_postfix = ExpressionParser::shunt(result);
EXPECT_EQ(result.size(), result_postfix.size());
ExpressionParser::ExecuteResult result_token = ExpressionParser::execute_expression_tokens(result_postfix, blank_variable_info);
EXPECT_FALSE(result_token.has_value());
EXPECT_EQ(static_cast<std::int64_t>(blank_variable_info.variables["test"]), 12);
}
TEST_F(ExpressionParserTests, ExpressionEvaluation) {
namespace ExpressionParser = ExpressionParserV2;
using ExpressionParser::Variant;
using ExpressionParser::execute_expression;
ExpressionParser::StoryVariableInfo blank_variable_info;
Variant t1 = execute_expression("5 + 7", blank_variable_info).value();
EXPECT_EQ(static_cast<std::int64_t>(t1), 12);
Variant t2 = execute_expression("5 + 7 * 52 - 8", blank_variable_info).value();
EXPECT_EQ(static_cast<std::int64_t>(t2), 361);
Variant t3 = execute_expression("5.0 * 4.2", blank_variable_info).value();
EXPECT_EQ(static_cast<double>(t3), 21.0);
Variant t4 = execute_expression("5.0 - 4.2 - 3.7 / 2.5", blank_variable_info).value();
EXPECT_TRUE(std::abs(static_cast<double>(t4) - -0.68) < 0.0001);
Variant t5 = execute_expression("5 == 2", blank_variable_info).value();
EXPECT_EQ(static_cast<bool>(t5), false);
Variant t6 = execute_expression("3 != 6", blank_variable_info).value();
EXPECT_EQ(static_cast<bool>(t6), true);
Variant t7 = execute_expression("7 < 12", blank_variable_info).value();
EXPECT_EQ(static_cast<bool>(t7), true);
Variant t8 = execute_expression(R"("hello" + " " + "there")", blank_variable_info).value();
EXPECT_EQ(t8.get<std::string>(), "hello there");
/*Variant t9 = execute_expression("++5", blank_variable_info).value();
EXPECT_EQ(static_cast<std::int64_t>(t9), 6);
Variant t10 = execute_expression("5++", blank_variable_info).value();
EXPECT_EQ(static_cast<std::int64_t>(t10), 5);*/
Variant t11 = execute_expression(R"("hello" ? "llo")", blank_variable_info).value();
EXPECT_EQ(static_cast<bool>(t11), true);
Variant t12 = execute_expression(R"("hello" ? "blah")", blank_variable_info).value();
EXPECT_EQ(static_cast<bool>(t12), false);
Variant t13 = execute_expression("5 * (3 + 4)", blank_variable_info).value();
EXPECT_EQ(static_cast<std::int64_t>(t13), 35);
Variant t14 = execute_expression("POW(3, 2)", blank_variable_info).value();
EXPECT_EQ(static_cast<double>(t14), 9);
Variant t15 = execute_expression("-> my_knot", blank_variable_info).value();
EXPECT_EQ(t15.get<std::string>(), "my_knot");
Variant t16 = execute_expression("POW(FLOOR(3.5), FLOOR(2.9)", blank_variable_info).value();
EXPECT_EQ(static_cast<double>(t16), 9);
Variant t17 = execute_expression("(5 * 5) - (3 * 3) + 3", blank_variable_info).value();
EXPECT_EQ(static_cast<std::int64_t>(t17), 19);
ExpressionParser::StoryVariableInfo vars;
vars.variables = {{"x", 5}, {"y", 3}, {"c", 3}};
execute_expression("x = (x * x) - (y * y) + c", vars);
EXPECT_EQ(static_cast<std::int64_t>(vars.variables["x"]), 19);
ExpressionParser::StoryVariableInfo vars2;
vars2.variables = {{"test", 6}};
Variant t19 = execute_expression("POW(test, 2)", vars2).value();
EXPECT_EQ(static_cast<double>(t19), 36);
ExpressionParser::StoryVariableInfo vars3;
vars3.variables = {{"x", 5}};
execute_expression("x++", vars3);
EXPECT_EQ(static_cast<std::int64_t>(vars3.variables["x"]), 6);
ExpressionParser::StoryVariableInfo vars4;
vars4.variables = {{"visited_snakes", true}, {"dream_about_snakes", false}};
Variant t20 = execute_expression("visited_snakes && not dream_about_snakes", vars4).value();
EXPECT_EQ(static_cast<bool>(t20), true);
}
#pragma endregion
#pragma region ContentTests
TEST_F(ContentTests, SingleLineText) {
STORY("1_content/1a_text.ink");
EXPECT_TEXT("Hello, world");
}
TEST_F(ContentTests, MultiLineText) {
STORY("1_content/1b_multiline_text.ink");
EXPECT_TEXT("Hello, world", "Hello?", "Hello, are you there?");
}
TEST_F(ContentTests, Comments) {
STORY("1_content/1c_comments.ink");
EXPECT_TEXT(R"("What do you make of this?" she asked.)", R"("I couldn't possibly comment," I replied.)");
}
TEST_F(ContentTests, Tags) {
STORY("1_content/1d_tags.ink");
EXPECT_TEXT("A line of normal game-text.");
std::vector<std::string> expected_tags = {"three * test", "other tags", "right here", "colour it blue"};
EXPECT_EQ(story.get_current_tags(), expected_tags);
}
#pragma endregion
#pragma region ChoiceTests
TEST_F(ChoiceTests, SingleChoice) {
STORY("2_choices/2a_choice.ink");
EXPECT_TEXT("Hello world!");
EXPECT_CHOICES("Hello back!");
story.choose_choice_index(0);
EXPECT_TEXT("Hello back!", "Nice to hear from you!");
}
TEST_F(ChoiceTests, SuppressedChoiceText) {
STORY("2_choices/2b_choice_suppressed.ink");
EXPECT_TEXT("Hello world!");
EXPECT_CHOICES("Hello back!");
story.choose_choice_index(0);
EXPECT_TEXT("Nice to hear from you!");
}
TEST_F(ChoiceTests, MixedChoiceText) {
STORY("2_choices/2c_choice_mixed.ink");
EXPECT_TEXT("Hello world!");
EXPECT_CHOICES("Hello back!");
story.choose_choice_index(0);
EXPECT_TEXT("Hello right back to you!", "Nice to hear from you!");
}
TEST_F(ChoiceTests, MultipleChoices) {
std::vector<std::vector<std::string>> expected_texts = {
{R"("I am somewhat tired," I repeated.)", R"("Really," he responded. "How deleterious.")"},
{R"("Nothing, Monsieur!" I replied.)", R"("Very good, then.")"},
{R"("I said, this journey is appalling and I want no more of it.")", R"("Ah," he replied, not unkindly. "I see you are feeling frustrated. Tomorrow, things will improve.")"},
};
for (int i = 0; i < 3; ++i) {
STORY("2_choices/2d_choice_multiple.ink");
EXPECT_TEXT(R"("What's that?" my master asked.)");
EXPECT_CHOICES(R"("I am somewhat tired.")", R"("Nothing, Monsieur!")", R"("I said, this journey is appalling.")");
story.choose_choice_index(i);
EXPECT_TEXT(expected_texts[i][0], expected_texts[i][1]);
}
}
#pragma endregion
#pragma region KnotTests
TEST_F(KnotTests, Knots) {
STORY("3_knots/3a_knot.ink");
EXPECT_TEXT("Hello world");
}
#pragma endregion
#pragma region DivertTests
TEST_F(DivertTests, Diverts) {
STORY("4_diverts/4a_divert.ink");
EXPECT_TEXT("We hurried home to Savile Row as fast as we could.");
}
TEST_F(DivertTests, DivertsWithGlue) {
STORY("4_diverts/4b_glue.ink");
EXPECT_TEXT("We hurried home to Savile Row as fast as we could.");
}
#pragma endregion
#pragma region BranchingTests
TEST_F(BranchingTests, AdvancedBranching) {
std::vector<std::string> expected_texts = {
"You open the gate, and step out onto the path.",
"You smash down the gate, and charge out onto the path.",
"You turn back and go home. Pitiful.",
};
for (int i = 0; i < 3; ++i) {
STORY("5_branching/5a_branch.ink");
EXPECT_TEXT("You stand by the wall of Analand, sword in hand.");
EXPECT_CHOICES("Open the gate", "Smash down the gate", "Turn back and go home");
story.choose_choice_index(i);
EXPECT_TEXT(expected_texts[i]);
}
}
TEST_F(BranchingTests, BranchingAndJoining) {
std::vector<std::vector<std::string>> expected_texts = {
{R"("There is not a moment to lose!" I declared.)", "We hurried home to Savile Row as fast as we could."},
{R"("Monsieur, let us savour this moment!" I declared.)", "My master clouted me firmly around the head and dragged me out of the door.", "He insisted that we hurried home to Savile Row as fast as we could."},
{"We hurried home to Savile Row as fast as we could."},
};
for (int i = 0; i < 3; ++i) {
STORY("5_branching/5b_branch_and_join.ink");
EXPECT_TEXT("We arrived into London at 9.45pm exactly.");
EXPECT_CHOICES(R"("There is not a moment to lose!")", R"("Monsieur, let us savour this moment!")", R"(We hurried home)");
story.choose_choice_index(i);
for (const std::string& expected_text : expected_texts[i]) {
EXPECT_EQ(story.continue_story(), expected_text);
}
}
}
#pragma endregion
#pragma region StitchTests
TEST_F(StitchTests, BasicStitches) {
std::vector<std::string> expected_texts = {
"Welcome to first class, sir.",
"Welcome to third class, sir.",
"You're not supposed to be here.",
};
for (int i = 0; i < 3; ++i) {
STORY("6_stitches/6a_stitches.ink");
story.continue_story();
EXPECT_CHOICES("Travel in first class", "Travel in third class", "Travel in the guard's van");
story.choose_choice_index(i);
EXPECT_TEXT(expected_texts[i]);
}
}
// TODO: Expect compilation fail
/*TEST_F(StitchTests, StitchesWithoutFallthrough) {
STORY("6_stitches/6b_stitches_no_fallthrough.ink");
EXPECT_TEXT("You are on a train. Whoa?", "");
}*/
TEST_F(StitchTests, StitchesWithLocalDiverts) {
STORY("6_stitches/6c_stitches_local_divert.ink");
EXPECT_TEXT("I settled my master.");
EXPECT_CHOICES("Move to third class");
story.choose_choice_index(0);
EXPECT_TEXT("I put myself in third.");
}
#pragma endregion
#pragma region VaryingChoiceTests
TEST_F(VaryingChoiceTests, ChoicesBeingUsedUp) {
STORY("7_varying/7a_fallback_choice.ink");
EXPECT_TEXT("You search desperately for a friendly face in the crowd.");
EXPECT_CHOICES("The woman in the hat?", "The man with the briefcase?");
story.choose_choice_index(0);
EXPECT_TEXT("The woman in the hat pushes you roughly aside. You search desperately for a friendly face in the crowd.");
EXPECT_CHOICES("The man with the briefcase?");
}
TEST_F(VaryingChoiceTests, FallbackChoices) {
STORY("7_varying/7a_fallback_choice.ink");
EXPECT_TEXT("You search desperately for a friendly face in the crowd.");
EXPECT_CHOICES("The woman in the hat?", "The man with the briefcase?");
story.choose_choice_index(1);
EXPECT_TEXT("The man with the briefcase looks disgusted as you stumble past him. You search desperately for a friendly face in the crowd.");
EXPECT_CHOICES("The woman in the hat?");
story.choose_choice_index(0);
EXPECT_TEXT("The woman in the hat pushes you roughly aside. You search desperately for a friendly face in the crowd.");
EXPECT_TEXT("But it is too late: you collapse onto the station platform. This is the end.");
}
TEST_F(VaryingChoiceTests, StickyChoices) {
STORY("7_varying/7b_sticky_choice.ink");
EXPECT_TEXT("You are on the couch.", "");
EXPECT_CHOICES("Eat another donut", "Get off the couch");
story.choose_choice_index(0);
EXPECT_TEXT("You eat another donut.");
EXPECT_CHOICES("Eat another donut", "Get off the couch");
story.choose_choice_index(0);
EXPECT_TEXT("You eat another donut.");
EXPECT_CHOICES("Eat another donut", "Get off the couch");
story.choose_choice_index(1);
EXPECT_TEXT("You struggle up off the couch to go and compose epic poetry.");
EXPECT_CHOICES("Eat another donut");
}
TEST_F(VaryingChoiceTests, ConditionalChoices) {
STORY("7_varying/7c_conditional_choice.ink");
EXPECT_TEXT("");
EXPECT_CHOICES("Choice 1", "Choice 2");
story.choose_choice_index(1);
EXPECT_TEXT("Hello 2", "Hello 3");
EXPECT_CHOICES("Choice 1", "Choice 2", "Choice 3");
}
TEST_F(VaryingChoiceTests, FallbackChoice2) {
STORY("7_varying/7d_fallback_choice_2.ink");
EXPECT_TEXT("But alas, there was no choice.");
}
#pragma endregion
#pragma region VariableTextTests
TEST_F(VariableTextTests, Sequences) {
STORY("8_variable_text/8a_sequence.ink");
EXPECT_TEXT("I bought a coffee with my five-pound note.");
EXPECT_CHOICES("Buy another one");
story.choose_choice_index(0);
EXPECT_TEXT("I bought a second coffee for my friend.");
EXPECT_CHOICES("Buy another one");
story.choose_choice_index(0);
EXPECT_TEXT("I didn't have enough money to buy any more coffee.");
EXPECT_CHOICES("Buy another one");
story.choose_choice_index(0);
EXPECT_TEXT("I didn't have enough money to buy any more coffee.");
}
TEST_F(VariableTextTests, Cycles) {
STORY("8_variable_text/8b_cycle.ink");
EXPECT_TEXT("It was Monday today.");
EXPECT_CHOICES("Tomorrow?");
story.choose_choice_index(0);
EXPECT_TEXT("It was Tuesday today.");
EXPECT_CHOICES("Tomorrow?");
story.choose_choice_index(0);
EXPECT_TEXT("It was Wednesday today.");
EXPECT_CHOICES("Tomorrow?");
story.choose_choice_index(0);
EXPECT_TEXT("It was Thursday today.");
EXPECT_CHOICES("Tomorrow?");
story.choose_choice_index(0);
EXPECT_TEXT("It was Friday today.");
EXPECT_CHOICES("Tomorrow?");
story.choose_choice_index(0);
EXPECT_TEXT("It was Saturday today.");
EXPECT_CHOICES("Tomorrow?");
story.choose_choice_index(0);
EXPECT_TEXT("It was Sunday today.");
EXPECT_CHOICES("Tomorrow?");
story.choose_choice_index(0);
EXPECT_TEXT("It was Monday today.");
EXPECT_CHOICES("Tomorrow?");
story.choose_choice_index(0);
EXPECT_TEXT("It was Tuesday today.");
EXPECT_CHOICES("Tomorrow?");
story.choose_choice_index(0);
EXPECT_TEXT("It was Wednesday today.");
EXPECT_CHOICES("Tomorrow?");
story.choose_choice_index(0);
EXPECT_TEXT("It was Thursday today.");
EXPECT_CHOICES("Tomorrow?");
story.choose_choice_index(0);
EXPECT_TEXT("It was Friday today.");
}
TEST_F(VariableTextTests, OnceOnly) {
STORY("8_variable_text/8c_onceonly.ink");
EXPECT_TEXT("He told me a joke. I laughed politely.");
story.choose_choice_index(0);
EXPECT_TEXT("He told me a joke. I smiled.");
story.choose_choice_index(0);
EXPECT_TEXT("He told me a joke. I grimaced.");
story.choose_choice_index(0);
EXPECT_TEXT("He told me a joke. I promised myself to not react again.");
story.choose_choice_index(0);
EXPECT_TEXT("He told me a joke.");
}
TEST_F(VariableTextTests, OnceOnlyWithEmptyEntries) {
STORY("8_variable_text/8e_onceonly_empty.ink");
EXPECT_TEXT("I took a step forward.");
story.choose_choice_index(0);
EXPECT_TEXT("I took a step forward.");
story.choose_choice_index(0);
EXPECT_TEXT("I took a step forward.");
story.choose_choice_index(0);
EXPECT_TEXT("I took a step forward.");
story.choose_choice_index(0);
EXPECT_TEXT("I took a step forward. Then the lights went out.");
}
TEST_F(VariableTextTests, CycleNested) {
STORY("8_variable_text/8f_cycle_nested.ink");
EXPECT_TEXT("The Ratbear wastes no time and swipes at you.");
story.choose_choice_index(0);
EXPECT_TEXT("The Ratbear scratches into your leg.");
story.choose_choice_index(0);
EXPECT_TEXT("The Ratbear swipes at you.");
story.choose_choice_index(0);
EXPECT_TEXT("The Ratbear scratches into your arm.");
story.choose_choice_index(0);
EXPECT_TEXT("The Ratbear swipes at you.");
story.choose_choice_index(0);
EXPECT_TEXT("The Ratbear scratches into your cheek.");
story.choose_choice_index(0);
EXPECT_TEXT("The Ratbear swipes at you.");
story.choose_choice_index(0);
EXPECT_TEXT("The Ratbear scratches into your leg.");
story.choose_choice_index(0);
EXPECT_TEXT("The Ratbear swipes at you.");
}
TEST_F(VariableTextTests, AlternativeWithDivert) {
STORY("8_variable_text/8g_alternative_divert.ink");
EXPECT_TEXT("I waited.");
story.choose_choice_index(0);
EXPECT_TEXT("I waited some more.");
story.choose_choice_index(0);
EXPECT_TEXT("I snoozed.");
story.choose_choice_index(0);
EXPECT_TEXT("I woke up and waited more.");
story.choose_choice_index(0);
EXPECT_TEXT("I gave up and left. I walked away in frustration.");
}
TEST_F(VariableTextTests, AlternativeInChoice) {
STORY("8_variable_text/8h_alternative_in_choice.ink");
EXPECT_TEXT(R"("Hello, boy," Monsieur Fogg proclaimed.)");
EXPECT_CHOICES(R"("Hello, Master!")", "Say nothing");
story.choose_choice_index(0);
EXPECT_TEXT(R"("Hello, Monsieur Fogg!" I declared.)", R"("Hello, boy," Monsieur Fogg proclaimed.)");
EXPECT_CHOICES(R"("Hello, you!")", "Say nothing");
story.choose_choice_index(0);
EXPECT_TEXT(R"("Hello, brown-eyes!" I declared.)", R"("Hello, boy," Monsieur Fogg proclaimed.)");
EXPECT_CHOICES(R"("Hello, Master!")", "Say nothing");
story.choose_choice_index(0);
EXPECT_TEXT(R"("Hello, Monsieur Fogg!" I declared.)", R"("Hello, boy," Monsieur Fogg proclaimed.)");
EXPECT_CHOICES(R"("Hello, you!")", "Say nothing");
story.choose_choice_index(0);
EXPECT_TEXT(R"("Hello, brown-eyes!" I declared.)", R"("Hello, boy," Monsieur Fogg proclaimed.)");
EXPECT_CHOICES(R"("Hello, Master!")", "Say nothing");
story.choose_choice_index(0);
EXPECT_TEXT(R"("Hello, Monsieur Fogg!" I declared.)", R"("Hello, boy," Monsieur Fogg proclaimed.)");
EXPECT_CHOICES(R"("Hello, you!")", "Say nothing");
story.choose_choice_index(1);
EXPECT_TEXT("You remain silent.", R"("Hello, boy," Monsieur Fogg proclaimed.)");
}
TEST_F(VariableTextTests, AlternativeAtChoiceStart) {
STORY("8_variable_text/8i_alternative_at_choice_start.ink");
story.continue_story();
EXPECT_CHOICES("They headed towards the Sandlands");
story.choose_choice_index(0);
EXPECT_TEXT("They set off for the desert", "");
EXPECT_CHOICES("The party followed the old road South");
story.choose_choice_index(0);
EXPECT_TEXT("They headed towards the Sandlands", "");
EXPECT_CHOICES("They set off for the desert",);
story.choose_choice_index(0);
EXPECT_TEXT("The party followed the old road South");
}
TEST_F(VariableTextTests, ConditionalText) {
std::vector<std::pair<bool ,bool>> var_values = {{false, false}, {true, false}, {true, true}};
std::vector<std::pair<std::string, std::string>> expected_text_outer = {
{"There was a man.", "I missed him. Was he particularly evil?"},
{"There was a man. He had a peculiar face.", "I saw him. Only for a moment. His real name was kept a secret."},
{"There was a man. He had a peculiar face.", "I saw him. Only for a moment. His real name was Franz."},
};
for (std::size_t i = 0; i < expected_text_outer.size(); ++i) {
STORY("8_variable_text/8k_conditional_text.ink");
story.set_variable("met_blofeld", var_values[i].first);
story.set_variable("learned_his_name", var_values[i].second);
EXPECT_TEXT(expected_text_outer[i].first, expected_text_outer[i].second);
}
}
#pragma endregion
#pragma region GameQueriesTests
TEST_F(GameQueriesTests, ChoiceCountFunction) {
STORY("9_game_queries/9a_choice_count.ink");
EXPECT_TEXT("");
EXPECT_CHOICES("Option B", "Option C");
}
TEST_F(GameQueriesTests, TurnsFunction) {
STORY("9_game_queries/9b_turns.ink");
EXPECT_TEXT("0 turns");
story.choose_choice_index(0);
EXPECT_TEXT("1: 1", "1 turns");
story.choose_choice_index(0);
EXPECT_TEXT("1: 2");
}
TEST_F(GameQueriesTests, TurnsSinceFunction) {
STORY("9_game_queries/9c_turns_since.ink");
EXPECT_TEXT("It's been -1 turns now", "HELLO THERE", "It's been 0 turns now");
story.choose_choice_index(0);
EXPECT_TEXT("It's been 1 turn now");
}
TEST_F(GameQueriesTests, SeedRandomFunction) {
STORY("9_game_queries/9d_seed_random.ink");
std::string text1 = story.continue_story();
std::string text2 = story.continue_story();
EXPECT_EQ(text1, text2);
}
#pragma endregion
#pragma region GatherTests
TEST_F(GatherTests, Gathers) {
std::vector<std::vector<std::string>> expected_texts = {
{R"("I am somewhat tired," I repeated.)", R"("Really," he responded. "How deleterious.")"},
{R"("Nothing, Monsieur!" I replied.)", R"("Very good, then.")"},
{R"("I said, this journey is appalling and I want no more of it.")", R"("Ah," he replied, not unkindly. "I see you are feeling frustrated. Tomorrow, things will improve.")"},
};
for (int i = 0; i < 3; ++i) {
STORY("10_gathers/10a_gather.ink");
EXPECT_TEXT(R"("What's that?" my master asked.)");
EXPECT_CHOICES(R"("I am somewhat tired.")", R"("Nothing, Monsieur!")", R"("I said, this journey is appalling.")");
story.choose_choice_index(i);
EXPECT_TEXT(expected_texts[i][0], expected_texts[i][1], "With that Monsieur Fogg left the room.");
}
}
TEST_F(GatherTests, MultipleGathers) {
std::vector<std::string> expected_texts_1 = {
"I checked the jewels were still in my pocket, and the feel of them brought a spring to my step. ",
"I did not pause for breath but kept on running. ",
"I cheered with joy. ",
};
std::vector<std::string> expected_texts_2 = {
"I reached the road and looked about. And would you believe it?",
"I should interrupt to say Mackie is normally very reliable. He's never once let me down. Or rather, never once, previously to that night.",
};
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 2; j++) {
STORY("10_gathers/10b_multi_gather.ink");
EXPECT_TEXT("I ran through the forest, the dogs snapping at my heels.");
EXPECT_CHOICES("I checked the jewels", "I did not pause for breath", "I cheered with joy.");
story.choose_choice_index(i);
std::string text1 = expected_texts_1[i] + "The road could not be much further! Mackie would have the engine running, and then I'd be safe.";
EXPECT_TEXT(text1);
EXPECT_CHOICES("I reached the road and looked about", "I should interrupt to say Mackie is normally very reliable");
story.choose_choice_index(j);
EXPECT_TEXT(expected_texts_2[j], "The road was empty. Mackie was nowhere to be seen.");
}
}
}
#pragma endregion
#pragma region NestedFlowTests
TEST_F(NestedFlowTests, NestedChoices) {
for (int i = 0; i < 2; ++i) {
STORY("11_nested_flow/11a_nested_choice.ink");
EXPECT_TEXT(R"("Well, Poirot? Murder or suicide?")");
EXPECT_CHOICES(R"("Murder!")", R"("Suicide!")");
story.choose_choice_index(i);
if (i == 0) {
EXPECT_TEXT(R"("Murder!")", R"("And who did it?")");
EXPECT_CHOICES(R"("Detective-Inspector Japp!")", R"("Captain Hastings!")", R"("Myself!")");
story.choose_choice_index(1);
EXPECT_TEXT(R"("Captain Hastings!")");
} else {
EXPECT_TEXT(R"("Suicide!")");
}
EXPECT_TEXT("Mrs. Christie lowered her manuscript a moment. The rest of the writing group sat, open-mouthed.");
}
}
TEST_F(NestedFlowTests, NestedChoicesInBoth) {
for (int i = 0; i < 2; ++i) {
STORY("11_nested_flow/11b_nested_choices_in_both.ink");
EXPECT_TEXT(R"("Well, Poirot? Murder or suicide?")");
EXPECT_CHOICES(R"("Murder!")", R"("Suicide!")");
story.choose_choice_index(i);
if (i == 0) {
EXPECT_TEXT(R"("Murder!")", R"("And who did it?")");
EXPECT_CHOICES(R"("Detective-Inspector Japp!")", R"("Captain Hastings!")", R"("Myself!")");
story.choose_choice_index(1);
EXPECT_TEXT(R"("Captain Hastings!")");
} else {
EXPECT_TEXT(R"("Suicide!")", R"("Really, Poirot? Are you quite sure?")");
EXPECT_CHOICES(R"("Quite sure.")", R"("It is perfectly obvious.")")
story.choose_choice_index(0);
EXPECT_TEXT(R"("Quite sure.")");
}
EXPECT_TEXT("Mrs. Christie lowered her manuscript a moment. The rest of the writing group sat, open-mouthed.");
}
}
TEST_F(NestedFlowTests, NestedGathers) {
for (int i = 0; i < 2; ++i) {
STORY("11_nested_flow/11c_nested_gathers.ink");
EXPECT_TEXT(R"("Well, Poirot? Murder or suicide?")");
EXPECT_CHOICES(R"("Murder!")", R"("Suicide!")");
story.choose_choice_index(i);
if (i == 0) {
EXPECT_TEXT(R"("Murder!")", R"("And who did it?")");
EXPECT_CHOICES(R"("Detective-Inspector Japp!")", R"("Captain Hastings!")", R"("Myself!")");
story.choose_choice_index(1);
EXPECT_TEXT(R"("Captain Hastings!")", R"("You must be joking!")");
EXPECT_CHOICES(R"("Mon ami, I am deadly serious.")", R"("If only...")");
story.choose_choice_index(0);
EXPECT_TEXT(R"("Mon ami, I am deadly serious.")");
} else {
EXPECT_TEXT(R"("Suicide!")", R"("Really, Poirot? Are you quite sure?")");
EXPECT_CHOICES(R"("Quite sure.")", R"("It is perfectly obvious.")")
story.choose_choice_index(0);
EXPECT_TEXT(R"("Quite sure.")");
}
EXPECT_TEXT("Mrs. Christie lowered her manuscript a moment. The rest of the writing group sat, open-mouthed.");
}
}
TEST_F(NestedFlowTests, DeepNesting) {
std::vector<std::string> texts = {
R"("It was a dark and stormy night...")",
R"("...and the crew were restless...")",
R"("... and they said to their Captain...")",
R"("...Tell us a tale Captain!")",
};
for (int i = 0; i < 2; ++i) {
STORY("11_nested_flow/11d_deep_nesting.ink");
EXPECT_TEXT(R"("Tell us a tale, Captain!")");
EXPECT_CHOICES(R"("Very well, you sea-dogs. Here's a tale...")", R"("No, it's past your bed-time.")");
story.choose_choice_index(i);
if (i == 0) {
EXPECT_TEXT(R"("Very well, you sea-dogs. Here's a tale...")");
for (const std::string& text : texts) {
EXPECT_CHOICES(text);
story.choose_choice_index(0);
EXPECT_TEXT(text);
}
} else {
EXPECT_TEXT(R"("No, it's past your bed-time.")");
}
EXPECT_TEXT("To a man, the crew began to yawn.");
}
}
TEST_F(NestedFlowTests, ComplexNesting) {
for (int i = 0; i < 3; ++i) {
STORY("11_nested_flow/11e_complex_nesting.ink");
EXPECT_TEXT("I looked at Monsieur Fogg");
EXPECT_CHOICES("... and I could contain myself no longer.", "... but I said nothing");
if (i == 0) {
story.choose_choice_index(1);
EXPECT_TEXT("... but I said nothing and we passed the day in silence.");
} else {
story.choose_choice_index(0);
EXPECT_TEXT("... and I could contain myself no longer.", "'What is the purpose of our journey, Monsieur?'", "'A wager,' he replied.");
EXPECT_CHOICES("'A wager!'", "'Ah.'");
story.choose_choice_index(0);
EXPECT_TEXT("'A wager!' I returned.", "He nodded.");
EXPECT_CHOICES("'But surely that is foolishness!'", "'A most serious matter then!'");
story.choose_choice_index(1);
EXPECT_TEXT("'A most serious matter then!'", "He nodded again.");
EXPECT_CHOICES("'But can we win?'", "'A modest wager, I trust?'", "I asked nothing further of him then.");
if (i == 1) {
story.choose_choice_index(1);
EXPECT_TEXT("'A modest wager, I trust?'", "'Twenty thousand pounds,' he replied, quite flatly.", "After that, we passed the day in silence.");
} else {
story.choose_choice_index(2);
EXPECT_TEXT("I asked nothing further of him then, and after a final, polite cough, he offered nothing more to me. After that, we passed the day in silence.");
}
}
}
}
#pragma endregion
#pragma region TrackingWeaveTests
TEST_F(TrackingWeaveTests, ChoiceLabels) {
for (int i = 0; i < 2; ++i) {
STORY("12_tracking_weave/12a_choice_labels.ink");
EXPECT_TEXT("The guard frowns at you.");
EXPECT_CHOICES("Greet him", "'Get out of my way.'");
if (i == 0) {
story.choose_choice_index(0);
EXPECT_TEXT("'Greetings.'", "'Hmm,' replies the guard.");
EXPECT_CHOICES("'Having a nice day?'", "'Hmm?'");
story.choose_choice_index(0);
EXPECT_TEXT("'Having a nice day?'", "'Mff,' the guard replies, and then offers you a paper bag. 'Toffee?'");
} else {
story.choose_choice_index(1);
EXPECT_TEXT("'Get out of my way,' you tell the guard.", "'Hmm,' replies the guard.");
EXPECT_CHOICES("'Hmm?'", "Shove him aside");
story.choose_choice_index(1);
EXPECT_TEXT("You shove him sharply. He stares in reply, and draws his sword!", "You appear to be in trouble.");
}
}
}
TEST_F(TrackingWeaveTests, WeaveScope) {
STORY("12_tracking_weave/12b_weave_scope.ink");
EXPECT_TEXT("");
EXPECT_CHOICES("one");
story.choose_choice_index(0);
EXPECT_TEXT("Some content.");
EXPECT_CHOICES("two", "end");
story.choose_choice_index(0);
EXPECT_TEXT("two");
EXPECT_CHOICES("Option");
story.choose_choice_index(0);
EXPECT_TEXT("Option", "");
}
TEST_F(TrackingWeaveTests, ChoiceOptionLabels) {
for (int i = 0; i < 2; ++i) {
STORY("12_tracking_weave/12a_choice_labels.ink");
EXPECT_TEXT("The guard frowns at you.");
EXPECT_CHOICES("Greet him", "'Get out of my way.'");
story.choose_choice_index(1);
EXPECT_TEXT("'Get out of my way,' you tell the guard.", "'Hmm,' replies the guard.");
EXPECT_CHOICES("'Hmm?'", "Shove him aside");
story.choose_choice_index(1);
EXPECT_TEXT("You shove him sharply. He stares in reply, and draws his sword!", "You appear to be in trouble.");
EXPECT_CHOICES("Throw rock at guard", "Throw sand at guard");
story.choose_choice_index(i);
EXPECT_TEXT(i == 0 ? "You hurl a rock at the guard." : "You hurl a handful of sand at the guard.",
"The guard thrusts his sword through your chest. That went about as well as you expected.");
}
}
TEST_F(TrackingWeaveTests, WeaveLoops) {
for (int i = 0; i < 2; ++i) {
STORY("12_tracking_weave/12d_weave_loops.ink");
EXPECT_TEXT("");
EXPECT_CHOICES("'Can I get a uniform from somewhere?'", "'Tell me about the security system.'", "'Are there dogs?'");
story.choose_choice_index(0);
EXPECT_TEXT("'Can I get a uniform from somewhere?' you ask the cheerful guard.",
"'Sure. In the locker.' He grins. 'Don't think it'll fit you, though.'");
EXPECT_CHOICES("'Tell me about the security system.'", "'Are there dogs?'", "Enough talking");
if (i == 0) {
story.choose_choice_index(2);
EXPECT_TEXT("You thank the guard, and move away.");
} else {
story.choose_choice_index(0);
EXPECT_TEXT("'Tell me about the security system.'", "'It's ancient,' the guard assures you. 'Old as coal.'");
EXPECT_CHOICES("'Are there dogs?'", "Enough talking");
story.choose_choice_index(0);
EXPECT_TEXT("'Are there dogs?'", "'Hundreds,' the guard answers, with a toothy grin. 'Hungry devils, too.'",
"He scratches his head.", "'Well, can't stand around talking all day,' he declares.", "You thank the guard, and move away.");
}
}
}
#pragma endregion
#pragma region GlobalVariableTests
TEST_F(GlobalVariableTests, VariableChecks) {
std::vector<std::tuple<int, bool>> var_values = {
{0, false},
{1, false},
{0, true},
{1, true},
};
std::vector<std::vector<std::string>> choices_expected = {
{"'But, Monsieur, why are we travelling?'"},
{"'But, Monsieur, why are we travelling?'"},
{"I contemplated our strange adventure"},
{"I contemplated our strange adventure"},
};
for (int i = 0; i < 4; ++i) {
STORY("13_global_variables/13a_variable_checks.ink");
int mood = std::get<0>(var_values[i]);
bool knows_about_wager = std::get<1>(var_values[i]);
story.set_variable("mood", mood);
story.set_variable("knows_about_wager", knows_about_wager);
EXPECT_TEXT(std::format("The train jolted and rattled. {}.",
mood > 0 ? "I was feeling positive enough, however, and did not mind the odd bump"
: "It was more than I could bear"));
EXPECT_EQ(story.get_current_choices(), choices_expected[i]);
}
}
TEST_F(GlobalVariableTests, DivertVariables) {
STORY("13_global_variables/13b_divert_variables.ink");
EXPECT_TEXT("Your Kingdom is in dire straits.", "Give up now, or keep trying to save your Kingdom?");
EXPECT_CHOICES("Keep trying!", "Give up");
story.choose_choice_index(0);
EXPECT_TEXT("It's not working. Your Kingdom is still in dire straits.", "Give up now, or keep trying to save your Kingdom?");
EXPECT_CHOICES("Keep trying!", "Give up");
story.choose_choice_index(0);
EXPECT_TEXT("It's not working. Your Kingdom is still in dire straits.", "Give up now, or keep trying to save your Kingdom?");
EXPECT_CHOICES("Keep trying!", "Give up");
story.choose_choice_index(1);
EXPECT_TEXT("Everybody in your Kingdom died.");
}
TEST_F(GlobalVariableTests, PrintingVariables) {
STORY("13_global_variables/13c_printing_variables.ink");
EXPECT_TEXT("My name is Jean Passepartout, but my friends call me Jackie. I'm 23 years old.");
}
/*TEST_F(GlobalVariableTests, EvalLogicInString) {
// TODO: string logic
STORY("13_global_variables/13d_string_logic.ink");
std::string text = story.continue_story();
EXPECT_FALSE(text.contains("{~red|blue|green|yellow}"));
std::string end = text.substr(52);
std::size_t split_index = end.find(" and ");
std::string left = end.substr(0, split_index);
std::string right = end.substr(split_index + 5);
right.pop_back();
EXPECT_EQ(left, right);
}*/
#pragma endregion
#pragma region LogicTests
TEST_F(LogicTests, BasicLogic) {
STORY("14_logic/14a_basic_logic.ink");
EXPECT_TEXT("knows_about_wager = true", "x = 27", "y = 108", "p1 = 9", "p2 = 4");
}
TEST_F(LogicTests, StringComparisons) {
STORY("14_logic/14b_string_comparisons.ink");
EXPECT_TEXT("true", "true", "true");
}
#pragma endregion