-
Notifications
You must be signed in to change notification settings - Fork 7
/
TestUtils.java
118 lines (90 loc) · 3.52 KB
/
TestUtils.java
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
package com.bladecoder.ink.runtime.test;
import static org.junit.Assert.fail;
import com.bladecoder.ink.runtime.Choice;
import com.bladecoder.ink.runtime.Story;
import com.bladecoder.ink.runtime.StoryException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class TestUtils {
public static String getJsonString(String filename) throws IOException {
InputStream systemResourceAsStream = ClassLoader.getSystemResourceAsStream(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(systemResourceAsStream, "UTF-8"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
// Replace the BOM mark
if (line != null) line = line.replace('\uFEFF', ' ');
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
return sb.toString();
} finally {
br.close();
}
}
public static List<String> runStory(String filename, List<Integer> choiceList, List<String> errors)
throws Exception {
// 1) Load story
String json = getJsonString(filename);
// System.out.println(json);
Story story = new Story(json);
List<String> text = new ArrayList<>();
// System.out.println(story.BuildStringOfHierarchy());
int choiceListIndex = 0;
while (story.canContinue() || story.getCurrentChoices().size() > 0) {
// 2) Game content, line by line
while (story.canContinue()) {
String line = story.Continue();
System.out.print(line);
text.add(line);
}
if (story.hasError()) {
for (String errorMsg : story.getCurrentErrors()) {
System.out.println(errorMsg);
errors.add(errorMsg);
}
}
// 3) Display story.currentChoices list, allow player to choose one
if (story.getCurrentChoices().size() > 0) {
for (Choice c : story.getCurrentChoices()) {
System.out.println(c.getText());
text.add(c.getText() + "\n");
}
if (choiceList == null || choiceListIndex >= choiceList.size())
story.chooseChoiceIndex(
(int) (Math.random() * story.getCurrentChoices().size()));
else {
story.chooseChoiceIndex(choiceList.get(choiceListIndex));
choiceListIndex++;
}
}
}
return text;
}
public static String joinText(List<String> text) {
StringBuilder sb = new StringBuilder();
for (String s : text) {
sb.append(s);
}
return sb.toString();
}
public static boolean isEnded(Story story) {
return !story.canContinue() && story.getCurrentChoices().isEmpty();
}
public static void nextAll(Story story, List<String> text) throws StoryException, Exception {
while (story.canContinue()) {
String line = story.Continue();
System.out.print(line);
if (!line.trim().isEmpty()) text.add(line.trim());
}
if (story.hasError()) {
fail(TestUtils.joinText(story.getCurrentErrors()));
}
}
}