-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEndToEndGeneratorTests.java
More file actions
190 lines (171 loc) · 6.75 KB
/
Copy pathEndToEndGeneratorTests.java
File metadata and controls
190 lines (171 loc) · 6.75 KB
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
package plc.project;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.function.Function;
import java.util.function.Consumer;
import java.util.stream.Stream;
public class EndToEndGeneratorTests {
@ParameterizedTest(name = "{0}")
@MethodSource
void testSource(String test, String input, String expected) {
test(input, expected, new Scope(null), Parser::parseSource);
}
private static Stream<Arguments> testSource() {
return Stream.of(
Arguments.of("Hello, World!",
// DEF main(): Integer DO
// print("Hello, World!");
// RETURN 0;
// END
"DEF main(): Integer DO\n print(\"Hello, World!\");\n RETURN 0;\nEND",
String.join(System.lineSeparator(),
"public class Main {",
"",
" public static void main(String[] args) {",
" System.exit(new Main().main());",
" }",
"",
" int main() {",
" System.out.println(\"Hello, World!\");",
" return 0;",
" }",
"",
"}"
)
)
);
}
@ParameterizedTest(name = "{0}")
@MethodSource
void testDeclarationStatement(String test, String input, String expected) {
test(input, expected, new Scope(null), Parser::parseStatement);
}
private static Stream<Arguments> testDeclarationStatement() {
return Stream.of(
Arguments.of("Declaration",
// LET name: Integer;
"LET name: Integer;",
"int name;"
),
Arguments.of("Initialization",
// LET name = 1.0;
"LET name = 1.0;",
"double name = 1.0;"
)
);
}
@ParameterizedTest(name = "{0}")
@MethodSource
void testIfStatement(String test, String input, String expected) {
test(input, expected, new Scope(null), Parser::parseStatement);
}
private static Stream<Arguments> testIfStatement() {
return Stream.of(
Arguments.of("If",
// IF TRUE DO
// print(1);
// END
"IF TRUE DO\n print(1);\nEND",
String.join(System.lineSeparator(),
"if (true) {",
" System.out.println(1);",
"}"
)
),
Arguments.of("Else",
// IF FALSE DO
// print(1);
// ELSE
// print(0);
// END
"IF FALSE DO\n print(1);\nELSE\n print(0);\nEND",
String.join(System.lineSeparator(),
"if (false) {",
" System.out.println(1);",
"} else {",
" System.out.println(0);",
"}"
)
)
);
}
@ParameterizedTest(name = "{0}")
@MethodSource
void testForStatement(String test, String input, String expected) {
test(input, expected, init(new Scope(null), scope -> {
scope.defineVariable("num", "num", Environment.Type.INTEGER, false, Environment.NIL);
}), Parser::parseStatement);
}
private static Stream<Arguments> testForStatement() {
return Stream.of(
Arguments.of("For",
// FOR (num = 0; num < 5; num = num + 1)
// print(num);
// END
"FOR (num = 0; num < 5; num = num + 1)\n print(num);\nEND",
String.join(System.lineSeparator(),
"for ( num = 0; num < 5; num = num + 1 ) {",
" System.out.println(num);",
"}"
)
)
);
}
@ParameterizedTest(name = "{0}")
@MethodSource
void testBinaryExpression(String test, String input, String expected) {
test(input, expected, new Scope(null), Parser::parseExpression);
}
private static Stream<Arguments> testBinaryExpression() {
return Stream.of(
Arguments.of("And",
// TRUE && FALSE
"TRUE && FALSE",
"true && false"
),
Arguments.of("Concatenation",
// "Ben" + 10
"\"Ben\" + 10",
"\"Ben\" + 10"
)
);
}
@ParameterizedTest(name = "{0}")
@MethodSource
void testFunctionExpression(String test, String input, String expected) {
test(input, expected, new Scope(null), Parser::parseExpression);
}
private static Stream<Arguments> testFunctionExpression() {
return Stream.of(
Arguments.of("Print",
// print("Hello, World!")
"print(\"Hello, World!\")",
"System.out.println(\"Hello, World!\")"
)
);
}
/**
* Helper function for tests, using a StringWriter as the output stream.
*/
private static <T extends Ast> void test(String input, String expected, Scope scope, Function<Parser, T> function) {
StringWriter writer = new StringWriter();
Lexer lexer = new Lexer(input);
Parser parser = new Parser(lexer.lex());
Ast ast = function.apply(parser);
Analyzer analyzer = new Analyzer(scope);
analyzer.visit(ast);
new Generator(new PrintWriter(writer)).visit(ast);
Assertions.assertEquals(expected, writer.toString());
}
/**
* Runs a callback on the given value, used for inline initialization.
*/
private static <T> T init(T value, Consumer<T> initializer) {
initializer.accept(value);
return value;
}
}