forked from PLRI/arden2bytecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecificationTest.java
238 lines (202 loc) · 8.42 KB
/
SpecificationTest.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
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
package arden.tests.specification.testcompiler;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeNotNull;
import static org.junit.Assume.assumeTrue;
import org.junit.Rule;
import arden.tests.specification.testcompiler.CompatibilityRule.Compatibility;
/**
* Base test class which all tests extend. Adds useful asserts, annotations and
* access to the compiler.
*/
public abstract class SpecificationTest {
/** Initialise your compiler here. It will be used by all tests. */
private final TestCompiler compiler = new arden.tests.specification.testcompiler.impl.TestCompilerImpl();
private final TestCompilerSettings settings = compiler.getSettings();
// decorator which intercepts calls to the compiler to skip unsupported tests
private TestCompiler runtimeCheckedCompiler = new TestCompiler() {
@Override
public TestCompilerSettings getSettings() {
return compiler.getSettings();
};
@Override
public TestCompilerMappings getMappings() {
TestCompilerMappings mappings = compiler.getMappings();
// only run test if mappings are provided
assumeNotNull("Compiler doesn't support tests with mappings", mappings);
return mappings;
};
@Override
public TestCompilerResult compileAndRun(String code) throws TestCompilerException {
// only run tests if runtime is supported
assumeTrue("Compiler doesn't support runtime tests", getSettings().isRuntimeSupported);
return compiler.compileAndRun(code);
}
@Override
public void compile(String code) throws TestCompilerCompiletimeException {
compiler.compile(code);
}
@Override
public TestCompilerDelayedMessage[] compileAndRunForEvent(String code, String eventMapping,
int messagesToCollect) throws TestCompilerException {
assumeTrue("Compiler doesn't support runtime tests", getSettings().isRuntimeSupported);
assumeTrue("Compiler doesn't support event tests", getSettings().runDelayTests);
return compiler.compileAndRunForEvent(code, eventMapping, messagesToCollect);
}
};
protected TestCompilerSettings getSettings() {
return settings;
}
protected TestCompiler getCompiler() {
return runtimeCheckedCompiler;
}
protected TestCompilerMappings getMappings() {
return runtimeCheckedCompiler.getMappings();
}
/**
* Initializes a code builder with a
* {@link ArdenCodeBuilder#ArdenCodeBuilder(ArdenVersion) template}
* compatible to the highest possible version. <br>
* The highest possible version is either the compiler's
* {@link TestCompilerSettings#targetVersion target version} or the tests
* {@link Compatibility#max() max compatible version}, whichever is lower.
*
* @return the {@link ArdenCodeBuilder}
*/
protected ArdenCodeBuilder createCodeBuilder() {
ArdenVersion maxPossibleVersion;
if (settings.targetVersion.ordinal() <= compatibilityRule.getCurrentTestMaxVersion().ordinal()) {
maxPossibleVersion = settings.targetVersion;
} else {
maxPossibleVersion = compatibilityRule.getCurrentTestMaxVersion();
}
return new ArdenCodeBuilder(maxPossibleVersion);
}
protected ArdenCodeBuilder createEmptyLogicSlotCodeBuilder() {
return createCodeBuilder().clearSlotContent("logic:");
}
// Used for compatibility tests with the @Compatibility annotation.
@Rule
public CompatibilityRule compatibilityRule = new CompatibilityRule(getSettings());
/**
* Tests if the result of the evaluated expressions is equal to the expected
* string (case insensitive).
*/
protected void assertEvaluatesTo(String expression, String expected) throws TestCompilerException {
assertEvaluatesToWithData(null, expression, expected);
}
protected void assertEvaluatesToWithData(String dataCode, String expression, String expected) throws TestCompilerException {
ArdenCodeBuilder builder;
if (dataCode != null) {
builder = new ArdenCodeBuilder(dataCode);
} else {
builder = createCodeBuilder();
}
String code = builder.addExpression(expression).toString();
assertReturns(code, expected);
}
protected void assertReturns(String code, String... expected) throws TestCompilerException {
if (!getSettings().isRuntimeSupported) {
assertValid(code);
return;
}
TestCompilerResult result = getCompiler().compileAndRun(code);
if (expected.length == 0) {
// no return values
if (result.returnValues.isEmpty()) {
// test passed
} else {
// a single "NULL" is also okay
assertEquals("Too many return values.", 1, result.returnValues.size());
assertEquals("null", result.returnValues.get(0).toLowerCase());
}
} else if (expected.length == 1) {
// single return value
assertEquals("Wrong number of return values.", 1, result.returnValues.size());
String returnValue = result.returnValues.get(0);
assertEquals(expected[0].toLowerCase(), returnValue.toLowerCase());
} else {
// multiple return values
assertEquals("Too many or few return values.", expected.length, result.returnValues.size());
String[] expected_lowercase = new String[expected.length];
String[] returnValues_lowercase = new String[result.returnValues.size()];
for (int i = 0; i < expected.length; i++) {
expected_lowercase[i] = expected[i].toLowerCase();
returnValues_lowercase[i] = result.returnValues.get(i).toLowerCase();
}
assertArrayEquals(expected_lowercase, returnValues_lowercase);
}
}
protected void assertNoReturn(String code) throws TestCompilerException {
assertReturns(code); // no expected values
}
protected void assertWrites(String code, String expected) throws TestCompilerException {
if (!getSettings().isRuntimeSupported) {
assertValid(code);
return;
}
TestCompilerResult result = getCompiler().compileAndRun(code);
String message = result.messages.get(0);
assertEquals(expected.toLowerCase(), message.toLowerCase());
}
protected void assertValidStatement(String statement) throws TestCompilerException {
String code = createCodeBuilder().addData(statement).toString();
assertValid(code);
}
protected void assertInvalidStatement(String statement) throws TestCompilerException {
String code = createCodeBuilder().addData(statement).toString();
assertInvalid(code);
}
protected void assertInvalidExpression(String expression) throws TestCompilerException {
String code = createCodeBuilder().addExpression(expression).toString();
assertInvalid(code);
}
protected void assertValid(String code) throws TestCompilerCompiletimeException {
getCompiler().compile(code);
}
protected void assertInvalid(String code) {
try {
getCompiler().compile(code);
fail("Expected a " + TestCompilerCompiletimeException.class.getSimpleName() + " to be thrown.");
} catch (TestCompilerCompiletimeException e) {
// test passed
}
}
protected void assertValidSlot(String slotname, String slotcontent) throws TestCompilerCompiletimeException {
String code = createCodeBuilder().replaceSlotContent(slotname, slotcontent).toString();
assertValid(code);
}
protected void assertInvalidSlot(String slotname, String slotcontent) {
String code = createCodeBuilder().replaceSlotContent(slotname, slotcontent).toString();
assertInvalid(code);
}
protected void assertSlotIsRequired(String slotname) {
String missingSlot = createCodeBuilder().removeSlot(slotname).toString();
assertInvalid(missingSlot);
}
protected void assertSlotIsOptional(String slotname) throws TestCompilerCompiletimeException {
String missingSlot = createCodeBuilder().removeSlot(slotname).toString();
assertValid(missingSlot);
}
protected void assertWritesAfterEvent(String code, String event, String... messages) throws TestCompilerException {
if (!getSettings().isRuntimeSupported || !getSettings().runDelayTests) {
assertValid(code);
return;
}
TestCompilerDelayedMessage[] delayedMessages = getCompiler().compileAndRunForEvent(code, event, messages.length);
for (int i = 0; i < messages.length; i++) {
assertEquals(messages[i].toLowerCase(), delayedMessages[i].message.toLowerCase());
}
}
protected void assertDelayedBy(String code, String event, long... delayMillis) throws TestCompilerException {
if (!getSettings().isRuntimeSupported || !getSettings().runDelayTests) {
assertValid(code);
return;
}
TestCompilerDelayedMessage[] messages = getCompiler().compileAndRunForEvent(code, event, delayMillis.length);
for (int i = 0; i < delayMillis.length; i++) {
assertEquals(delayMillis[i], messages[i].delayMillis, TestCompilerDelayedMessage.PRECISION_MILLIS);
}
}
}