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
|
package org.zwobble.couscous.tests.interpreter;
import com.google.common.collect.ImmutableMap;
import org.hamcrest.Matcher;
import org.junit.Ignore;
import org.junit.Test;
import org.zwobble.couscous.ast.*;
import org.zwobble.couscous.interpreter.*;
import org.zwobble.couscous.interpreter.errors.*;
import org.zwobble.couscous.interpreter.values.ArrayInterpreterValue;
import org.zwobble.couscous.interpreter.values.IntegerInterpreterValue;
import org.zwobble.couscous.interpreter.values.InterpreterValue;
import org.zwobble.couscous.interpreter.values.StringInterpreterValue;
import org.zwobble.couscous.tests.BackendEvalTests;
import org.zwobble.couscous.types.ScalarType;
import org.zwobble.couscous.types.Type;
import org.zwobble.couscous.types.Types;
import org.zwobble.couscous.values.PrimitiveValue;
import java.util.List;
import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.zwobble.couscous.ast.ArrayNode.array;
import static org.zwobble.couscous.ast.AssignmentNode.assign;
import static org.zwobble.couscous.ast.AssignmentNode.assignStatement;
import static org.zwobble.couscous.ast.CastNode.cast;
import static org.zwobble.couscous.ast.ConstructorCallNode.constructorCall;
import static org.zwobble.couscous.ast.FieldAccessNode.fieldAccess;
import static org.zwobble.couscous.ast.FormalArgumentNode.formalArg;
import static org.zwobble.couscous.ast.LiteralNode.literal;
import static org.zwobble.couscous.ast.MethodCallNode.methodCall;
import static org.zwobble.couscous.ast.MethodSignature.signature;
import static org.zwobble.couscous.ast.TernaryConditionalNode.ternaryConditional;
import static org.zwobble.couscous.ast.VariableDeclaration.var;
import static org.zwobble.couscous.interpreter.Evaluator.eval;
import static org.zwobble.couscous.interpreter.values.InterpreterValues.value;
import static org.zwobble.couscous.tests.TestIds.ANY_ID;
import static org.zwobble.couscous.tests.util.ExtraAsserts.assertThrows;
import static org.zwobble.couscous.tests.util.ExtraMatchers.*;
import static org.zwobble.couscous.types.ParameterizedType.parameterizedType;
import static org.zwobble.couscous.util.ExtraLists.list;
import static org.zwobble.couscous.util.ExtraMaps.map;
public class EvaluatorTests extends BackendEvalTests {
@Test
public void valueOfAssignmentExpressionIsNewValue() {
FormalArgumentNode arg = formalArg(var(ANY_ID, "x", Types.STRING));
Environment environment = new Environment(
new MapBackedProject(ImmutableMap.of()),
Optional.empty(),
new StackFrameBuilder().declare(arg, value("[initial value]")).build());
InterpreterValue result = eval(environment, assign(arg, literal("[updated value]")));
assertEquals(StringInterpreterValue.of("[updated value]"), result);
}
@Test
public void errorIfConditionIsNotBoolean() {
ConditionMustBeBoolean exception = assertThrows(ConditionMustBeBoolean.class,
() -> eval(emptyEnvironment(),
ternaryConditional(literal(1), literal("T"), literal("F"))));
assertEquals(new ConditionMustBeBoolean(new IntegerInterpreterValue(1)), exception);
}
@Test
public void errorIfMethodDoesNotExist() {
NoSuchMethod exception = assertThrows(NoSuchMethod.class,
() -> eval(emptyEnvironment(),
methodCall(
literal("hello"),
"size",
list(),
Types.INT)));
assertEquals(signature("size", list(), Types.INT), exception.getSignature());
}
@Test
public void errorIfWrongNumberOfArgumentsArePassed() {
NoSuchMethod exception = assertThrows(NoSuchMethod.class,
() -> eval(emptyEnvironment(),
methodCall(
literal("hello"),
"substring",
list(literal(1)),
Types.STRING)));
assertEquals(signature("substring", list(Types.INT), Types.STRING), exception.getSignature());
}
@Test
public void errorIfArgumentIsWrongType() {
NoSuchMethod exception = assertThrows(NoSuchMethod.class,
() -> eval(emptyEnvironment(),
methodCall(
literal("hello"),
"substring",
list(literal(0), literal("")),
Types.STRING)));
assertEquals(
signature("substring", list(Types.INT, Types.STRING), Types.STRING),
exception.getSignature());
}
@Test
public void errorIfConstructorArgumentIsWrongType() {
FormalArgumentNode argument = formalArg(var(ANY_ID, "x", Types.INT));
ClassNode classNode = ClassNode.builder("com.example.Example")
.constructor(constructor -> constructor
.argument(argument))
.build();
UnexpectedValueType exception = assertThrows(UnexpectedValueType.class,
() -> evalExpression(
list(classNode),
constructorCall(
classNode.getName(),
list(literal("")))));
assertEquals(new UnexpectedValueType(Types.INT, Types.STRING), exception);
}
@Test
public void cannotGetValueOfUndeclaredField() {
ClassNode classNode = ClassNode.builder("com.example.Example")
.build();
NoSuchField exception = assertThrows(NoSuchField.class,
() -> evalExpression(
list(classNode),
fieldAccess(
constructorCall(
classNode.getName(),
list()),
"value",
Types.INT)));
assertEquals(new NoSuchField("value"), exception);
}
@Test
public void cannotGetValueOfUnboundField() {
ClassNode classNode = ClassNode.builder("com.example.Example")
.field("value", Types.INT)
.build();
UnboundField exception = assertThrows(UnboundField.class,
() -> evalExpression(
list(classNode),
fieldAccess(
constructorCall(
classNode.getName(),
list()),
"value",
Types.INT)));
assertEquals(new UnboundField("value"), exception);
}
@Test
public void cannotSetValueOfUndeclaredField() {
ClassNode classNode = ClassNode.builder("com.example.Example")
.constructor(constructor -> constructor
.statement(assignStatement(
fieldAccess(constructor.thisReference(), "value", Types.INT),
literal(42))))
.build();
NoSuchField exception = assertThrows(NoSuchField.class,
() -> evalExpression(
list(classNode),
constructorCall(
classNode.getName(),
list())));
assertEquals(new NoSuchField("value"), exception);
}
@Test
public void cannotSetValueOfInstanceFieldAsThoughItIsAStaticField() {
ScalarType type = ScalarType.topLevel("com.example.Example");
ClassNode classNode = ClassNode.builder(type)
.field("value", Types.INT)
.constructor(constructor -> constructor
.statement(assignStatement(
fieldAccess(type, "value", Types.INT),
literal(42))))
.build();
NoSuchField exception = assertThrows(NoSuchField.class,
() -> evalExpression(
list(classNode),
constructorCall(
classNode.getName(),
list())));
assertEquals(new NoSuchField("value"), exception);
}
@Test
@Ignore("WIP")
public void cannotSetValueOfFieldWithWrongType() {
ClassNode classNode = ClassNode.builder("com.example.Example")
.field("value", Types.INT)
.constructor(constructor -> constructor
.statement(assignStatement(
fieldAccess(constructor.thisReference(), "value", Types.INT),
literal(""))))
.build();
UnexpectedValueType exception = assertThrows(UnexpectedValueType.class,
() -> evalExpression(
list(classNode),
constructorCall(
classNode.getName(),
list())));
assertEquals(new UnexpectedValueType(Types.INT, Types.STRING), exception);
}
@Test
public void validCastPassesValueThrough() {
InterpreterValue value = eval(
emptyEnvironment(),
cast(literal("42"), Types.OBJECT));
assertEquals(value("42"), value);
}
@Test
public void errorIfCastIsNotValid() {
InvalidCast exception = assertThrows(InvalidCast.class,
() -> eval(emptyEnvironment(),
cast(literal(42), Types.STRING)));
assertEquals(Types.STRING, exception.getExpected());
assertEquals(Types.INT, exception.getActual());
}
@Test
public void canEvaluateEmptyArray() {
InterpreterValue array = eval(emptyEnvironment(), array(Types.STRING, list()));
assertThat(array, isArrayInterpreterValue(Types.STRING, list()));
}
@Test
public void canEvaluateArrayWithElements() {
InterpreterValue array = eval(emptyEnvironment(), array(Types.STRING, list(literal("one"), literal("two"))));
assertThat(
array,
isArrayInterpreterValue(Types.STRING, list(
StringInterpreterValue.of("one"),
StringInterpreterValue.of("two"))));
}
@Test
public void typeOfValueCanBeParameterisedType() {
ScalarType rawType = ScalarType.topLevel("Box");
ClassNode classNode = ClassNode.builder(rawType)
.addTypeParameter("T")
.build();
Project project = JavaProject.of(list(classNode));
Environment environment = new Environment(project, Optional.empty(), map());
ConstructorCallNode call = constructorCall(
parameterizedType(rawType, list(Types.STRING)),
list());
InterpreterValue value = eval(environment, call);
assertThat(
value.getType().getType(),
equalTo(parameterizedType(rawType, list(Types.STRING))));
}
private Matcher<InterpreterValue> isArrayInterpreterValue(Type elementType, List<InterpreterValue> elements) {
return isInstance(
ArrayInterpreterValue.class,
allOf(
hasFeature("elementType", value -> value.getElementType(), equalTo(elementType)),
hasFeature("elements", value -> value.getElements(), containsExactElements(elements))));
}
private static Environment emptyEnvironment() {
return new Environment(
JavaProject.builder().build(),
Optional.empty(),
ImmutableMap.of());
}
@Override
protected PrimitiveValue evalExpression(
List<TypeNode> classes,
ExpressionNode expression) {
Environment environment = new Environment(
JavaProject.of(classes),
Optional.empty(),
ImmutableMap.of());
return eval(environment, expression).toPrimitiveValue().get();
}
}
|