forked from grol-io/grol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.txtar
290 lines (231 loc) · 6.87 KB
/
main_test.txtar
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
# testscript framework tests for grol's main binary / command line
# Basic usage test
!grol -foo
!stdout .
stderr 'flag provided but not defined: -foo'
# (short) version
grol version
stdout '^dev$'
!stderr .
# (long) version
grol buildinfo
stdout '^dev go'
stdout 'path grol.io/grol'
!stderr .
# most basic expression
grol -c '1+1'
stdout '^2$'
stderr welcome
!stderr 'rror' # no Errors or other case/singular/plural
# syntax error non mode, stdout doesn't repeat errors
!grol -c 'foo'
stderr 'Total 1 error'
stderr 'identifier not found: foo'
!stdout .
# sample_test.gr
grol sample_test.gr
!stderr 'Errors'
cmp stdout sample_test_stdout
stderr 'I] Running sample_test.gr'
stderr 'called fact 5'
stderr 'called fact 1'
stderr 'I] All done'
# macro output (there is a macro in sample_test.gr, it should show that stage with -parse)
grol -parse sample_test.gr
!stderr 'Errors'
stdout '== Macro ==>'
# no macro stage when no macro in the file:
grol -parse fib_50.gr
!stderr 'Errors'
!stdout '== Macro ==>'
stdout '12586269025\n'
# fib_50.gr (redoing, checking exact match of output)
grol fib_50.gr
!stderr 'Errors'
cmp stdout fib50_stdout
stderr 'I] Running fib_50.gr'
stderr 'I] All done'
# Bug repro, return aborts the whole program
grol -c 'f=func(){return 1;2};log(f());f();3'
stdout '^1\n3$'
!stderr '\[E\]'
# lambda version of previous
grol -c 'f=()=>{return 1;2};log(f());f();3'
stdout '^1\n3$'
!stderr '\[E\]'
# same one more level
grol -c 'f=func(n){if (n==5) {return 1};2};log(f(5));f(5);3'
stdout '^1\n3$'
!stderr '\[E\]'
# lambda version of previous
grol -c 'f=n=>{if (n==5) {return 1};2};log(f(5));f(5);3'
stdout '^1\n3$'
!stderr '\[E\]'
# test bad macro as well as indirectly the error() builtin
!grol -c 'm=macro(){};m()'
stderr '<err: macro should return Quote. got=object.Null \({}\)>'
# quiet mode and fast fibbonaci (log won't show)
grol -quiet -c 'fib=func(x){log("fib",x);if x<=1 {x} else {fib(x-1)+fib(x-2)}}; fib(92)'
stdout '^7540113804746346429\n$'
!stdout '\n\n'
!stderr .
# lambda version
grol -quiet -c 'fib= x => if x<=1 {x} else {self(x-1)+self(x-2)}; fib(92)'
stdout '^7540113804746346429\n$'
!stdout '\n\n'
!stderr .
# variadic errors
!grol -c 'func f(a,b,..){};f(1)'
stderr 'wrong number of arguments for f. got=1, want at least=2'
# variadic ok 1
grol -quiet -c 'func f(a,b,..){println(a,b,..)};f(1,2)'
stdout '1 2 \[]'
!stderr .
# variadic ok 2
grol -quiet -c 'func f(a,b,..){println(a,b,..)};f(1,2,"ab",PI)'
stdout '1 2 \["ab",3.141592653589793]'
!stderr .
# printf expands correctly
grol -quiet -c 'printf("%d %s %.2f\n", 42, "ab\ncd", 1.5)'
stdout '42 ab\ncd 1.50\n'
!stderr .
# eval() runs in the same context as when called
grol -quiet -c 'a=1;eval("a=2");a'
stdout '^2$'
!stderr .
# eval() runs in the same context as when called
grol -quiet -c 'func foo(a) {eval("b=2");a+b};foo(1)'
stdout '^3$'
!stderr .
# eval() runs in the same context as when called
grol -quiet -c 'func foo(x) {eval("info")};foo(42)["all_ids"][1]'
stdout '^{"self":func foo\(x\){eval\("info"\)},"x":42}$'
!stderr .
# json of (nested) arrays
grol -quiet -c 'a=[1,2,3,["a", "b"],4];println(json(a))'
stdout '^\[1,2,3,\["a","b"],4]$'
!stderr .
# dot notation and priority with functions
grol -quiet -c 'n1={"f1":func(){println("f1")},"f2":func(x){x+1}}n1.f1()n1.f2(41)'
stdout '^f1\n42$'
!stderr .
# lambda version
grol -quiet -c 'n1={"f1":()=>println("f1"),"f2":(x)=>x+1}n1.f1()n1.f2(41)'
stdout '^f1\n42$'
!stderr .
!grol -no-load-save -c 'save("foo.gr"); load("foo.gr")'
stderr 'identifier not found: save'
!grol -no-load-save -c 'load("foo.gr")'
stderr 'identifier not found: load'
!grol -c 'save("/tmp/foo.gr"); load("/tmp/foo.gr")'
stderr 'invalid character in filename "/tmp/foo.gr": /'
grol -c 'load("fib_50")'
stdout '^12586269025\n$'
stderr 'Read/evaluated: fib_50.gr'
!grol -c 'load("./fib_50.gr")'
stderr 'invalid character in filename "./fib_50.gr": \.'
grol -unrestricted-io -c 'load("./fib_50.gr")'
stdout '^12586269025\n$'
stderr 'Read/evaluated: ./fib_50.gr'
!grol -empty-only -c 'load("fib_50")'
stderr 'empty only mode, filename must be empty or no arguments, got: "fib_50"'
grol -empty-only -c 'save();load()'
stderr 'Saved .* ids/fns to: .gr'
stderr 'Read/evaluated: .gr'
# max depth
!grol -max-depth 12 -c 'func foo(n) {if n<=1 {1} else {self(n-1);n}}; foo(13)'
stderr 'max depth 13 reached'
grol -max-depth 12 -c 'func foo(n) {if n<=1 {1} else {self(n-1);n}}; foo(12)'
stdout '^12$'
!stderr 'max depth.*reached'
# map don't mutate on append
grol -quiet -c 'm={2:"b"};n={1:"a"};println(m+n); println(m)'
stdout '^{1:"a",2:"b"}\n{2:"b"}$'
!stderr .
grol -quiet -c 'm={1:1, nil:"foo"}; println(m+{nil:"bar"}); m'
stdout '^{1:1,nil:"bar"}\n{1:1,nil:"foo"}$'
!stderr .
# int
grol -quiet -c 'print(int("0xff"), int(PI))'
stdout '^255 3$'
!stderr .
# short circuiting
grol -quiet -c 'if true || println("not ran") {println("is true")}'
stdout '^is true$'
!stderr .
!stdout 'not ran'
grol -quiet -c 'if false && println("not ran") {true} else {println("is false")}'
stdout '^is false$'
!stderr .
!stdout 'not ran'
# parse error context not crashing
!grol -quiet -panic -c '^&@%%^!%^&^&!%^%^&!'
stderr 'parser error'
!stderr panic
!grol -quiet -panic -c '@'
!stderr panic
!stderr NIL_TOKEN
!stderr '\[CRI\]'
stderr 'parser error'
stderr '@'
# panic on keys() of large array
grol -quiet -panic -c 'keys(info.all_ids[0])'
!stderr panic
# range
grol -quiet -c '(23:31)[4:]'
stdout '^\[27,28,29,30\]$'
# range
grol -quiet -c '(23:31)[5-1:23-24]'
stdout '^\[27,28,29\]$'
# crash on weird half function body
!grol -quiet -c '(x) {1'
stderr 'Incomplete input'
!stderr 'panic'
!stderr 'runtime error: invalid memory'
# no crash on empty param list in lambda short form.
grol -quiet -c '(()=>1)()'
stdout '^1$'
-- sample_test.gr --
// Sample file that our gorepl can interpret
// <--- comments
// See also the other *.gr files
unless = macro(cond, iffalse, iftrue) {
quote(if (!(unquote(cond))) {
unquote(iffalse)
} else {
unquote(iftrue)
})
}
unless(10 > 5, print("BUG: not greater\n"), print("macro test: greater\n"))
fact= n => { // could be func(n) instead of n => short lambda form
log("called fact", n) // log (timestamped stderr output)
if (n<=1) {
return 1
}
n*fact(n-1) // recursion, also last evaluated expression is returned (ie return at the end is optional)
}
a=[fact(5), "abc", 76-3] // array can contain different types
m={"key": a, 73: 29} // so do maps
println("m is:", m) // stdout print
println("Outputting a smiley: 😀")
first(m["key"]) // get the value from key from map, which is an array, and the first element of the array is our factorial 5
// could also have been m["key"][0]
// ^^^ gorepl sample.gr should output 120
-- fib_50.gr --
fib = func(x) {
if (x == 0) {
return 0
}
if (x == 1) {
return 1
}
fib(x - 1) + fib(x - 2)
}
fib(50)
-- sample_test_stdout --
macro test: greater
m is: {73:29,"key":[120,"abc",73]}
Outputting a smiley: 😀
120
-- fib50_stdout --
12586269025