-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
338 lines (295 loc) · 8.13 KB
/
main_test.go
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package main
import (
"bytes"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
)
func Test_Run(t *testing.T) {
tests := []struct {
name string
source string
stdOut string
stdErr string
}{
// atoms
{"string", "print \"hello world\";", "hello world\n", ""},
{"multi-line string", "print \"hello\nworld\";", "hello\nworld\n", ""},
{"number", "print 342.32461932591235;", "342.32461932591235\n", ""},
{"string as boolean", "print \"\" and 34;", "34\n", ""},
{"nil as boolean", "print nil and 34;", "nil\n", ""},
{"line ending without semi-colon", "print", "", "[line 1] Error at end: Expect expression.\n"},
// comments
{"single-line comment after source", "print 1 + 1; // hello", "2\n", ""},
{"single-line comment", `// hello
print 1 + 1;`, "2\n", ""},
// unary, binary, and ternary operations
{"arithmetic operations", "print -1 + 2 * 3 - 4 / 5;", "4.2\n", ""},
{"decimal arithmetic", "print 1.234 / 5.678;", "0.2173300457907714\n", ""},
{"logical operations", "print (!true or false) and false;", "false\n", ""},
{"ternary", "print 3 < 4 ? 2 > 5 ? \"no\" : \"yes\" : \"also no\";", "yes\n", ""},
{"string concatenation", "print \"hello\" + \" \" + \"world\";", "hello world\n", ""},
{"greater than or equal to", "print 4 >= 3 ? 3 >= 3 ? 2 >= 3 : true : true;", "false\n", ""},
{"less than or equal to", "print 4 <= 5 ? 5 <= 5 ? 6 <= 5 : true : true;", "false\n", ""},
{"equal to", "print 5 == 5 ? 4 == 5 : true;", "false\n", ""},
{"not equal to", "print 4 != 5 ? 5 != 5 : true;", "false\n", ""},
{"comma", "print (1, 2);", "2\n", ""},
// variables
{"variable declaration", "var a = 10; print a*2;", "20\n", ""},
{"variable assignment after declaration", "var a; a = 20; print a*2;", "40\n", ""},
{"variable re-assignment", "var a = 10; print a; a = 20; print a*2;", "10\n40\n", ""},
// block scoping
{"block scoping", `var a = "global a";
var b = "global b";
var c = "global c";
{
var a = "outer a";
var b = "outer b";
{
var a = "inner a";
print a;
print b;
print c;
}
print a;
print b;
print c;
}
print a;
print b;
print c;`, "inner a\nouter b\nglobal c\nouter a\nouter b\nglobal c\nglobal a\nglobal b\nglobal c\n", ""},
// conditionals
{"if block", "if (true) { if (false) { print \"hello\"; } else { print \"world\"; } }", "world\n", ""},
// loops
{"for loop", `var a = 0;
var temp;
for (var b = 1; a < 10; b = temp + b) {
print a;
temp = a;
a = b;
}`, "0\n1\n1\n2\n3\n5\n8\n", ""},
{"while loop", `var a = 0;
var temp;
var b = 1;
while (a < 10) {
print a;
temp = a;
a = b;
b = temp + b;
}`, "0\n1\n1\n2\n3\n5\n8\n", ""},
{"break statement", `var a = 1;
while (true) {
a = a + 1;
print a;
if (a == 4) break;
}`, "2\n3\n4\n", ""},
{"continue statement", `var a = 1;
while (a < 10) {
a = a * 2;
print a;
if (a > 4) {
continue;
} else {
a = a + 1;
}
}`, "2\n6\n12\n", ""},
// functions
{"function", `fun sayHi(first, last) {
print "Hello, " + first + " " + last;
}
sayHi("Dear", "Reader");`, "Hello, Dear Reader\n", ""},
{"return statement", `fun sayHi(first, last) {
return "Hello, " + first + " " + last;
}
print sayHi("Dear", "Reader");`, "Hello, Dear Reader\n", ""},
{"closure", `fun makeCounter() {
var i = 0;
fun count() {
i = i + 1;
print i;
}
return count;
}
var counter = makeCounter();
counter();
counter();`, "1\n2\n", ""},
{"anonymous function", `fun makeCounter() {
var i = 0;
return fun () {
i = i + 1;
print i;
};
}
var counter = makeCounter();
counter();
counter();`, "1\n2\n", ""},
{"iife", `(fun count(next) {
print next;
if (next < 5) return count(next + 1);
return;
})(1);`, "1\n2\n3\n4\n5\n", ""},
{"calling function with wrong arity", `fun sayHello(a, b) {
print a + b;
}
sayHello("only first");`, "", "Expected 2 arguments but got 1.\n[line 3]\n"},
// Variable scoping
{"scoping", `var a = "global";
{
fun showA() {
print a;
}
showA();
var a = "block";
showA();
a; // mutes error about the local variable not being used
}`, "global\nglobal\n", ""},
{"re-declaring variables in same scope", `{
var a = "global";
var a = "global2";
}`, "", "[line 2] Error at 'a': Already a variable with this name in this scope\n[line 2] Error at 'a': Variable 'a' declared but not used.\n"},
{"unused local variable", `{
var a = "global";
}`, "", "[line 1] Error at 'a': Variable 'a' declared but not used.\n"},
// Classes
{"class method", `class Bacon {
eat() {
print "Crunch crunch";
}
}
Bacon().eat();
`, "Crunch crunch\n", ""},
{"call and assign", `class Person {
init(age) {
this.age = age;
}
getAge() {
return this.age;
}
}
var person = Person(0);
var sum = 0;
sum = sum + person.getAge();
print sum;`, "0\n", ""},
{"this", `class Cake {
taste() {
var adjective = "delicious";
print "The " + this.flavor + " cake is " + adjective + "!";
}
}
var cake = Cake();
cake.flavor = "German chocolate";
cake.taste();
`, "The German chocolate cake is delicious!\n", ""},
{"init class", `class Circle {
init(radius) {
this.radius = radius;
}
area() {
return 3.141592653 * this.radius * this.radius;
}
}
var circle = Circle(7);
print circle.area();`, "153.938039997\n", ""},
{"class with static methods", `class Math {
add(x, y) {
return x + y;
}
}
print Math.add(1, 2);`, "3\n", ""},
{"getter", `class Circle {
init(radius) {
this.radius = radius;
}
area {
return 3.141592653 * this.radius * this.radius;
}
}
var circle = Circle(7);
print circle.area;`, "153.938039997\n", ""},
{"inheritance", `class Doughnut {
cook() {
print "Fry until golden brown.";
}
}
class BostonCream < Doughnut {}
BostonCream.cook();`, "Fry until golden brown.\n", ""},
{"calling super", `class Doughnut {
cook() {
print "Fry until golden brown.";
}
}
class BostonCream < Doughnut {
cook() {
super.cook();
print "Pipe full of custard and coat with chocolate.";
}
}
BostonCream().cook();`, "Fry until golden brown.\nPipe full of custard and coat with chocolate.\n", ""},
{"calling super outside class", `super.hello();`, "", "[line 0] Error at 'super': Can't use 'super' outside of a class.\n"},
{"calling super in class with no superclass", `class Doughnut {
cook() {
super.cook();
}
}`, "", "[line 2] Error at 'super': Can't use 'super' in a class with no superclass.\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stdOut := &bytes.Buffer{}
stdErr := &bytes.Buffer{}
r := newRunner(stdOut, stdErr)
r.run(tt.source)
if stdOut.String() != tt.stdOut {
t.Errorf("stdOut: got %s, expected %s", strconv.Quote(stdOut.String()), strconv.Quote(tt.stdOut))
}
if stdErr.String() != tt.stdErr {
t.Errorf("stdErr: got %s, expected %s", strconv.Quote(stdErr.String()), strconv.Quote(tt.stdErr))
}
})
}
}
func TestRunFromFile(t *testing.T) {
paths, err := filepath.Glob(filepath.Join("testdata", "*.input"))
if err != nil {
t.Fatal(err)
}
for _, path := range paths {
_, filename := filepath.Split(path)
testName := filename[:len(filename)-len(filepath.Ext(path))]
t.Run(testName, func(t *testing.T) {
source, err := os.ReadFile(path)
if err != nil {
t.Fatal("error reading test source file:", err)
}
goldenFile := filepath.Join("testdata", testName+".golden")
want, err := os.ReadFile(goldenFile)
if err != nil {
t.Fatal("error reading golden file", err)
}
wantOutput := strings.Trim(string(want), "\n")
wantStdOut, wantStdErr := splitWantOutput(wantOutput)
stdOut := &bytes.Buffer{}
stdErr := &bytes.Buffer{}
r := newRunner(stdOut, stdErr)
r.run(string(source))
if stdOut.String() != wantStdOut {
t.Errorf("stdOut: got %s, expected %s", strconv.Quote(stdOut.String()), strconv.Quote(wantStdOut))
}
if stdErr.String() != wantStdErr {
t.Errorf("stdErr: got %s, expected %s", strconv.Quote(stdErr.String()), strconv.Quote(wantStdErr))
}
})
}
}
func splitWantOutput(output string) (stdOut string, stdErr string) {
lines := strings.Split(output, "\n")
for _, line := range lines {
if len(line) >= 5 && line[:5] == "err: " {
stdErr += line + "\n"
} else {
stdOut += line + "\n"
}
}
return
}