-
Notifications
You must be signed in to change notification settings - Fork 0
/
lox-test-runner.sh
385 lines (332 loc) · 6.82 KB
/
lox-test-runner.sh
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash
# Configure location for executable to be tested
EXECUTABLE="Debug/lox-cpp.exe"
function run_all_tests {
begin_tests
# Comment out tests to skip them
test_print
test_single_line_comment
test_multi_line_comment
test_expressions
test_ternary
test_logical
test_string_concat
test_variables
test_scopes
test_control
test_break
test_functions
test_recursion
test_closure
test_lambda
test_classes
test_static_method
test_inheritance
end_tests
}
function test_print {
run_test 'print "Hello, world!";' 'Hello, world!'
}
function test_single_line_comment {
run_test '
// print "commented code";
print "uncommented code"; // ignored
'\
'uncommented code'
}
function test_multi_line_comment {
run_test '
/*
print "commented";
print "code";
*/
print /* ignored */ "uncommented code"; /* ignored */
'\
'uncommented code'
}
function test_expressions {
run_test 'print 123;' '123'
run_test 'print (-100)+(-20)+(-3);' '-123'
run_test 'print 12.34 * 100;' '1234'
run_test 'print "hello";' 'hello'
run_test 'print "";' ''
run_test 'print ((5+5)*10)/2;' '50'
run_test 'print true;' 'true'
run_test 'print !true;' 'false'
run_test '1/0;' 'Divide by zero.\n[line 1]'
run_test '-"muffin";' 'Operand must be a number.\n[line 1]'
}
function test_ternary {
run_test 'print true ? "yes" : "no";' 'yes'
run_test 'print false ? "yes" : "no";' 'no'
}
function test_logical {
run_test 'print "hi" or 2;' 'hi'
run_test 'print nil or "yes";' 'yes'
}
function test_string_concat {
run_test 'print "a" + 1;' 'a1'
run_test 'print 1 + "a";' '1a'
run_test 'print "a" + "b";' 'ab'
run_test 'print 1 + 1;' '2'
}
function test_variables {
run_test 'var a=1; var b=2; var c=a+b; print c;' '3'
run_test '
var breakfast = "bagels";
breakfast = "beignets";
print breakfast;
' \
'beignets'
}
function test_scopes {
run_test '
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
outer b
global c
outer a
outer b
global c
global a
global b
global c'
run_test '
var volume = 11;
volume = 0;
{
var volume = 3 * 4 * 5;
print volume;
}
' \
'60'
}
function test_control {
run_test 'if (true) { print "yes"; } else { print "no"; }' 'yes'
run_test 'if (false) { print "yes"; } else { print "no"; }' 'no'
run_test 'var a = 1; while (a < 10) { a = a + 1; } print a;' '10'
run_test '
var total = 0;
for (var a = 1; a < 10; a = a + 1) {
total = total + a;
}
print total;
' \
'45'
}
function test_break {
run_test 'var a = 1; while (a = 1) { break; a = 2; } print a;' '1'
}
function test_functions {
run_test '
fun returnSum(a, b) {
return a + b;
}
var result = returnSum(1, 2);
print result;
' \
'3'
run_test '
fun count(n) {
if (n > 1) count(n - 1);
print n;
}
count(3);
' \
'1\n2\n3'
run_test 'print clock;' '<native fn>'
run_test 'print clock() > 0;' 'true'
run_test 'fun foo(){} print foo;' '<fn foo>'
run_test 'fun foo(){} print foo();' 'nil'
}
function test_recursion {
run_test '
fun fib(n) {
if (n <= 1) return n;
return fib(n - 2) + fib(n - 1);
}
print fib(10);
' \
'55'
}
function test_closure {
run_test '
fun returnFunction() {
var outside = "outside";
fun inner() {
print outside;
}
return inner;
}
var fn = returnFunction();
fn();
' \
'outside'
run_test '
fun makeCounter() {
var i = 0;
fun count() {
i = i + 1;
print i;
}
return count;
}
var counter = makeCounter();
counter();
counter();
' \
'1\n2'
}
function test_lambda {
run_test 'var x = fun () { return "lambda"; }; print x();' 'lambda'
run_test 'var x = fun () {}; print x;' '<lambda fn>'
}
function test_classes {
run_test '
class Foo {}
var foo = Foo();
print foo;
' \
'Foo instance'
run_test '
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!'
run_test '
class Breakfast {
init(meat, bread) {
this.meat = meat;
this.bread = bread;
}
serve(who) {
print "Enjoy your " + this.meat + " and " + this.bread + ", " + who + ".";
}
}
var baconAndToast = Breakfast("bacon", "toast");
baconAndToast.serve("Dear Reader");
' \
'Enjoy your bacon and toast, Dear Reader.'
}
function test_static_method {
run_test '
class Math {
class square(n) {
return n * n;
}
}
print Math.square(3);
' \
'9'
}
function test_inheritance {
run_test '
class A { a() { return "ok"; } }
class B < A {}
var b = B();
print b.a();
' \
'ok'
run_test '
class A {
method() {
print "A method";
}
}
class B < A {
method() {
print "B method";
}
test() {
super.method();
}
}
class C < B {}
C().test();
' \
'A method'
}
function begin_tests {
let COUNTER=0
let PASSED=0
let FAILED=0
let START=`date +%s`
}
function end_tests {
local end=`date +%s`
local runtime=$((end-START))
echo "======"
echo -e "Total: $COUNTER \e[32mPassed: $PASSED \e[31mFailed: $FAILED\e[0m Runtime (seconds): $runtime"
}
function watch {
local LTIME=0
while true
do
local ATIME=`stat -c %Z $EXECUTABLE`
if [[ "$ATIME" != "$LTIME" ]]
then
sleep 2 # Wait recompilation to end
echo
echo "Testing $EXECUTABLE"
run_all_tests
LTIME=$ATIME
fi
sleep 2
done
}
function run_test {
local input=$1
local expect=`echo -e "$2" | tr -d "\r"`
local actual=`echo "$input" | $EXECUTABLE 2>&1 | tr -d "\r"`
if [ $? -ne 0 ]; then
echo "Retry..."
return
fi
if [ "$actual" = "$expect" ]; then
let PASSED=PASSED+1
else
echo "Test failed for input: $input"
echo "------"
echo -e "\e[32mExpect: $expect\e[0m"
echo -e "\e[31mActual: $actual\e[0m"
let FAILED=FAILED+1
fi
let COUNTER=COUNTER+1
}
COUNTER=0
PASSED=0
FAILED=0
START=0
if [ "$1" = "-w" ]; then
echo "Running in watch mode"
watch
else
echo "Running tests once, use '-w' flag to run in watch mode"
run_all_tests
fi