Skip to content

Commit 32c402e

Browse files
committed
Add tests regarding type inference, overflow, BEP and array inference, as well as NaNs
1 parent 8835754 commit 32c402e

9 files changed

Lines changed: 164 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#output: NaN
2+
3+
println(2.0 / 0);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#output: NaN
2+
3+
println(2.0 / 0.0f);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#output: 2
2+
3+
println(8 % 3);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#output: -2147483648
2+
#output: int
3+
4+
val n = 2147483647;
5+
val m = n + 1;
6+
println(m);
7+
println(typeof(m));
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#output: -9223372036854775808
2+
#output: long
3+
4+
val n = 9223372036854775807;
5+
val m = n + 1;
6+
println(m);
7+
println(typeof(m));
8+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#output: 0.3
2+
3+
println(0.1f + 0.2f);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#output: NaN
2+
3+
println(2.0 % 0.0);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#output: 2
2+
#output: 2
3+
4+
virtual class Car {
5+
6+
}
7+
8+
class ElectricCar : Car {
9+
10+
}
11+
12+
val c = new Car();
13+
val ec = new ElectricCar();
14+
15+
val firstArray = [
16+
c,
17+
ec
18+
];
19+
20+
val secondArray = [
21+
ec,
22+
c
23+
];
24+
25+
println(firstArray.length);
26+
println(secondArray.length);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package org.openzen.zenscript.scriptingexample.tests.actual_test.java_native.bep;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.openzen.zencode.java.ZenCodeType;
5+
import org.openzen.zencode.shared.CodePosition;
6+
import org.openzen.zenscript.codemodel.compilation.CompilableExpression;
7+
import org.openzen.zenscript.lexer.ParseException;
8+
import org.openzen.zenscript.lexer.ZSTokenParser;
9+
import org.openzen.zenscript.lexer.ZSTokenType;
10+
import org.openzen.zenscript.parser.BracketExpressionParser;
11+
import org.openzen.zenscript.parser.expression.*;
12+
import org.openzen.zenscript.parser.type.ParsedNamedType;
13+
import org.openzen.zenscript.scriptingexample.tests.helpers.ScriptBuilder;
14+
import org.openzen.zenscript.scriptingexample.tests.helpers.ZenCodeTest;
15+
16+
import java.util.Arrays;
17+
import java.util.Collections;
18+
import java.util.List;
19+
20+
public class InherititedOrderedBEPTest extends ZenCodeTest {
21+
22+
@Override
23+
public BracketExpressionParser getBEP() {
24+
return new Parser();
25+
}
26+
27+
@Override
28+
public List<Class<?>> getRequiredClasses() {
29+
List<Class<?>> requiredClasses = super.getRequiredClasses();
30+
requiredClasses.add(BracketHandlers.class);
31+
requiredClasses.add(Car.class);
32+
return requiredClasses;
33+
}
34+
35+
@Test
36+
void works() {
37+
ScriptBuilder.create()
38+
.add("val array = [")
39+
.add(" <car>,")
40+
.add(" <electric>")
41+
.add(" ];")
42+
.add("println(array.length);")
43+
44+
.add("val second = [")
45+
.add(" <electric>,")
46+
.add(" <car>")
47+
.add("];")
48+
.add("println(second.length);")
49+
.execute(this);
50+
51+
logger.printlnOutputs()
52+
.assertLinesInOrder("2", "2");
53+
}
54+
55+
56+
@ZenCodeType.Name("test_module.Car")
57+
public static class Car {
58+
59+
}
60+
61+
static class ElectricCar extends Car {
62+
63+
}
64+
65+
@ZenCodeType.Name("test_module.BracketHandlers")
66+
public static final class BracketHandlers {
67+
68+
@ZenCodeType.Method
69+
public static Car parseCar(String value) {
70+
if ("car".equals(value)) {
71+
return new Car();
72+
}
73+
if ("electric".equals(value)) {
74+
return new ElectricCar();
75+
}
76+
return null;
77+
}
78+
}
79+
80+
private static final class Parser implements BracketExpressionParser {
81+
@Override
82+
public CompilableExpression parse(CodePosition position, ZSTokenParser tokens) throws ParseException {
83+
StringBuilder stringBuilder = new StringBuilder();
84+
while (!tokens.isNext(ZSTokenType.T_GREATER)) {
85+
stringBuilder.append(tokens.next().getContent());
86+
}
87+
tokens.next();
88+
return new ParsedExpressionCall(
89+
position,
90+
new ParsedExpressionMember(
91+
position,
92+
new ParsedTypeExpression(position, new ParsedNamedType(position, Arrays.asList(
93+
new ParsedNamedType.ParsedNamePart("test_module", null),
94+
new ParsedNamedType.ParsedNamePart("BracketHandlers", null)
95+
))),
96+
"parseCar",
97+
null
98+
),
99+
new ParsedCallArguments(
100+
Collections.singletonList(
101+
new ParsedExpressionString(position, stringBuilder.toString(), false)
102+
)
103+
)
104+
);
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)