1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Usage:
|
4 | # test/ysh-runtime-errors.sh <function name>
|
5 |
|
6 | set -o nounset
|
7 | set -o pipefail
|
8 | set -o errexit
|
9 |
|
10 | source test/common.sh
|
11 | source test/sh-assert.sh # _assert-sh-status
|
12 |
|
13 | #
|
14 | # Cases
|
15 | #
|
16 |
|
17 | test-no-typed-args() {
|
18 | _ysh-error-X 2 'echo (42)'
|
19 | _ysh-error-X 2 'echo { echo hi }'
|
20 |
|
21 | # Hm write could be J8 notation? like json8 write (x)?
|
22 | _ysh-error-X 2 'write (42)'
|
23 |
|
24 | _ysh-error-X 2 'true (42)'
|
25 | _ysh-error-X 2 'false { echo hi }'
|
26 |
|
27 | _ysh-error-X 2 'test x (42)'
|
28 | _ysh-error-X 2 'test x { echo hi }'
|
29 | }
|
30 |
|
31 | test-undefined-vars() {
|
32 | _ysh-error-1 'echo hi; const y = 2 + x + 3'
|
33 | _ysh-error-1 'if (x) { echo hello }'
|
34 | _ysh-error-1 'if (${x}) { echo hi }'
|
35 |
|
36 | # BareDecl and regex
|
37 | _ysh-error-1 'const x = / @undef /; echo hi'
|
38 |
|
39 | _ysh-error-1 'var x = undef; echo $x' # VarDecl
|
40 | _ysh-error-1 'setvar a = undef' # PlaceMutation
|
41 | }
|
42 |
|
43 | test-word-eval-with-ysh-data() {
|
44 | _ysh-expr-error 'var d = {}; echo ${d:-}'
|
45 |
|
46 | _osh-error-X 3 'var d = {}; echo ${#d}'
|
47 |
|
48 | _osh-error-X 3 'var d = {}; echo ${d[0]}'
|
49 |
|
50 | _osh-error-X 3 'var d = {}; echo ${d[@]:1:3}'
|
51 |
|
52 | _osh-error-X 3 'var d = {}; echo ${!d}'
|
53 |
|
54 | _osh-error-X 3 'var d = {}; echo ${!d[@]}'
|
55 |
|
56 | _osh-error-X 3 'var d = {}; echo ${d#prefix}'
|
57 |
|
58 | _osh-error-X 3 'var d = {}; echo ${d//a/b}'
|
59 |
|
60 | }
|
61 |
|
62 | test-ysh-word-eval() {
|
63 | # Wrong sigil
|
64 | _ysh-expr-error 'echo $[maybe("foo")]'
|
65 |
|
66 | # Wrong sigil
|
67 | _ysh-expr-error 'source $LIB_YSH/math.ysh; echo $[identity({key: "val"})]'
|
68 |
|
69 | # this should be consistent
|
70 | _ysh-expr-error 'source $LIB_YSH/math.ysh; write -- @[identity([{key: "val"}])]'
|
71 |
|
72 | _ysh-should-run 'var x = [1, 2]; write @x'
|
73 |
|
74 | # errors in items
|
75 | _ysh-expr-error 'var x = [3, {}]; write @x'
|
76 |
|
77 | _ysh-expr-error 'var x = [3, {}]; write @[x]'
|
78 |
|
79 | # errors at top level
|
80 | _ysh-expr-error 'var x = /d+/; write @x'
|
81 |
|
82 | _ysh-expr-error 'var x = /d+/; write @[x]'
|
83 | }
|
84 |
|
85 | # Continuation of above
|
86 | test-cannot-stringify-list() {
|
87 | # List can't be stringified
|
88 | _ysh-expr-error 'var mylist = [1,2,3]; write $mylist'
|
89 | _ysh-expr-error 'var mylist = [1,2,3]; write $[mylist]'
|
90 |
|
91 | _ysh-should-run '= str(/d+/)'
|
92 |
|
93 | _ysh-expr-error '= str([1,2])'
|
94 | _ysh-expr-error '= str({})'
|
95 |
|
96 | # Not sure if I like this join() behavior
|
97 | _ysh-should-run '= join([true, null])'
|
98 |
|
99 | # Bad error
|
100 | _ysh-expr-error '= join([[1,2], null])'
|
101 | }
|
102 |
|
103 | test-ysh-expr-eval() {
|
104 | _ysh-expr-error 'echo $[42 / 0 ]'
|
105 |
|
106 | _ysh-expr-error 'var d = {}; var item = d->nonexistent'
|
107 |
|
108 | _ysh-expr-error 'var d = {}; var item = d["nonexistent"]'
|
109 |
|
110 | _ysh-expr-error 'var a = []; setvar item = a[1]'
|
111 |
|
112 | _ysh-expr-error 'const x = 42 / 0'
|
113 |
|
114 | # command sub as part of expression retains its exit code
|
115 | _ysh-error-1 'var x = "z" ++ $(false)'
|
116 | #_ysh-error-1 'var x = "z" ++ $(exit 42)'
|
117 |
|
118 | _ysh-expr-error 'case (42 / 0) { * { echo hi } }; echo OK'
|
119 |
|
120 | _ysh-expr-error 'var d = {}; for x in $[d->zzz] { echo hi }'
|
121 |
|
122 | # Wrong index type
|
123 | _ysh-expr-error 'var d = {}; setvar d[42] = 3'
|
124 | _ysh-expr-error 'var L = []; setvar L["key"] = 3'
|
125 |
|
126 | }
|
127 |
|
128 | test-ysh-expr-eval-2() {
|
129 | _ysh-expr-error 'var L = []; var slice = L["foo": "bar"]'
|
130 |
|
131 | _ysh-expr-error '= 3 < true'
|
132 | _ysh-expr-error '= "a" < "b"'
|
133 |
|
134 | _ysh-expr-error 'var key = 42; var d = {[key]: 3}'
|
135 |
|
136 | _ysh-expr-error 'var d = {}; var a = d.a'
|
137 | _ysh-expr-error 'var d = []; var a = d.a'
|
138 |
|
139 | _ysh-expr-error '= 3 ** -2'
|
140 | _ysh-expr-error '= 3.2 ** 2'
|
141 |
|
142 | _ysh-expr-error '= - "foo"'
|
143 | }
|
144 |
|
145 | test-user-reported() {
|
146 | #_ysh-error-1 'echo'
|
147 |
|
148 | # Issue #1118
|
149 | # Some tests became test/parse-errors.sh
|
150 |
|
151 |
|
152 | # len(INTEGER) causes the same problem
|
153 | _ysh-expr-error '
|
154 | var snippets = [{status: 42}]
|
155 | for snippet in (snippets) {
|
156 | if (len(42)) {
|
157 | echo hi
|
158 | }
|
159 | }
|
160 | '
|
161 |
|
162 | # len(INTEGER) causes the same problem
|
163 | _ysh-expr-error '
|
164 | var count = 0
|
165 |
|
166 | # The $ causes a weird error
|
167 | while (count < len(count)) {
|
168 | setvar count += 1
|
169 | }
|
170 | '
|
171 | }
|
172 |
|
173 | test-fallback-locations() {
|
174 | # Melvin noticed bug here
|
175 | _ysh-expr-error 'if (len(42)) { echo hi }'
|
176 |
|
177 | # Be even more specific
|
178 | _ysh-expr-error 'if (1 + len(42)) { echo hi }'
|
179 |
|
180 | # From Aidan's PR -- redefinition
|
181 | _ysh-error-1 'const f = 42; func f() { echo hi }'
|
182 |
|
183 | # ForEach shell
|
184 | _ysh-expr-error 'for x in $[2 + len(42)] { echo hi }'
|
185 |
|
186 | # ForEach YSH
|
187 | _ysh-expr-error 'for x in (len(42)) { echo hi }'
|
188 |
|
189 | _ysh-expr-error 'while (len(42)) { echo hi }'
|
190 |
|
191 | _ysh-expr-error 'case (len(42)) { pat { echo argument } }'
|
192 | _ysh-expr-error 'case (42) { (len(42)) { echo arm } }'
|
193 |
|
194 | _ysh-expr-error 'case "$[len(42)]" in pat) echo hi ;; esac'
|
195 |
|
196 | _ysh-expr-error 'var x = 3 + len(42)'
|
197 | _ysh-expr-error 'const x = 3 + len(42)'
|
198 | _ysh-expr-error 'setvar x = 3 + len(42)'
|
199 |
|
200 | _ysh-expr-error 'setvar x = "s" + 5'
|
201 | _ysh-expr-error 'while ("s" + 5) { echo yes } '
|
202 |
|
203 | #_ysh-expr-error 'func f(x) { return (x) }; var x = f([1,2])(3); echo $x'
|
204 |
|
205 | # Really bad one
|
206 | _ysh-expr-error 'func f(x) { return (x) }; var x = f([1,2])[1](3); echo $x'
|
207 | }
|
208 |
|
209 | test-EvalExpr-calls() {
|
210 | ### Test everywhere expr_ev.EvalExpr() is invoked
|
211 |
|
212 | _ysh-expr-error 'json write (len(42))'
|
213 |
|
214 | _ysh-expr-error '= len(42)'
|
215 | _ysh-expr-error 'call len(42)'
|
216 |
|
217 | _ysh-expr-error 'echo $[len(42)]'
|
218 | _ysh-expr-error 'echo $[len(z = 42)]'
|
219 |
|
220 | _ysh-expr-error 'echo @[len(42)]'
|
221 | _ysh-expr-error 'echo @[len(z = 42)]'
|
222 |
|
223 | _ysh-expr-error 'const x = len(42)'
|
224 | _ysh-expr-error 'setvar x += len(42)'
|
225 |
|
226 | _ysh-expr-error '
|
227 | var d = {}
|
228 | setvar d[len(42)] = "foo"
|
229 | '
|
230 |
|
231 | _ysh-error-X 2 '
|
232 | var d = {}
|
233 | setvar len(42).z = "foo"
|
234 | '
|
235 |
|
236 | _ysh-expr-error '
|
237 | hay define Package
|
238 | Package foo {
|
239 | x = len(42)
|
240 | }
|
241 | '
|
242 |
|
243 | _ysh-expr-error 'if (len(42)) { echo hi }'
|
244 |
|
245 | _ysh-expr-error 'while (len(42)) { echo hi }'
|
246 |
|
247 | _ysh-expr-error 'for x in (len(42)) { echo $x }'
|
248 | }
|
249 |
|
250 |
|
251 | test-hay() {
|
252 | _ysh-error-X 127 '
|
253 | hay define package user TASK
|
254 |
|
255 | hay eval :result {
|
256 | package foo {
|
257 | # commands can be run while evaluating
|
258 | oops
|
259 | }
|
260 |
|
261 | bad 2
|
262 | }
|
263 | '
|
264 | }
|
265 |
|
266 |
|
267 | test-hay-osh() {
|
268 | # forgot parse_brace
|
269 | _osh-error-X 2 '
|
270 | hay define package TASK
|
271 |
|
272 | package foo {
|
273 | version = 1
|
274 | }
|
275 | '
|
276 |
|
277 | # forgot parse_equals
|
278 | _osh-error-X 127 '
|
279 | shopt --set parse_brace
|
280 |
|
281 | hay define package TASK
|
282 |
|
283 | hay eval :result {
|
284 | package foo {
|
285 | version = 1
|
286 | }
|
287 | }
|
288 | '
|
289 | }
|
290 |
|
291 | test-eggex() {
|
292 | # forgot parse_brace
|
293 | _ysh-should-run ' = / [ \x00 \xff ] /'
|
294 | _ysh-should-run ' = / [ \x00-\xff ] /'
|
295 |
|
296 | # Shouldn't be in strings
|
297 |
|
298 | cat >_tmp/test-eggex.txt <<'EOF'
|
299 | = / [ $'\x00 \xff' ] /
|
300 | EOF
|
301 |
|
302 | _ysh-error-1 "$(cat _tmp/test-eggex.txt)"
|
303 |
|
304 | _ysh-should-run ' = / [ \u{0} ] /'
|
305 | _ysh-should-run ' = / [ \u{0}-\u{1} ] /'
|
306 |
|
307 | # Too high
|
308 | _ysh-error-1 'var x =/ [ \u{80} ] /; echo $x'
|
309 | _ysh-error-1 'var x = / [ \u{7f}-\u{80} ] /; echo $x'
|
310 |
|
311 | # Now test special characters
|
312 | _ysh-should-run "$(cat <<'EOF'
|
313 | = / [ \\ '^-]' 'abc' ] /
|
314 | EOF
|
315 | )"
|
316 |
|
317 | # Special chars in ranges are disallowed for simplicity
|
318 | _ysh-error-1 "var x = / [ a-'^' ] /; echo \$x"
|
319 | _ysh-error-1 "var x = / [ '-'-z ] /; echo \$x"
|
320 | _ysh-error-1 "var x = / [ ']'-z ] /; echo \$x"
|
321 |
|
322 | # TODO: Disallow this. It translates to [^], which is a syntax error in
|
323 | # egrep "Unmatched [ or [^"
|
324 | _ysh-should-run "var x = / ['^'] /; echo \$x"
|
325 |
|
326 | _ysh-expr-error '
|
327 | var i = 42
|
328 | = / @i / # splice object of wrong type
|
329 | '
|
330 |
|
331 | _ysh-expr-error '
|
332 | var i = 42
|
333 | = / [a @i] / # char class splice object of wrong type
|
334 | '
|
335 | }
|
336 |
|
337 | test-eggex-2() {
|
338 | _ysh-should-run "var sq = / 'foo'+ /"
|
339 |
|
340 | _ysh-should-run "$(cat <<'EOF'
|
341 | var sq = / ('foo')+ /
|
342 | echo $sq
|
343 |
|
344 | var sq2 = / <capture 'foo'>+ /
|
345 | echo $sq2
|
346 | EOF
|
347 | )"
|
348 |
|
349 | _ysh-error-1 '
|
350 | var literal = "foo"
|
351 | var svs = / @literal+ /
|
352 | echo $svs
|
353 | '
|
354 | }
|
355 |
|
356 | test-eggex-api() {
|
357 | _ysh-expr-error '= _group(0)' # No groups
|
358 |
|
359 | _ysh-expr-error 'if ("foo" ~ /[a-z]/) { echo $[_group(1)] }'
|
360 | _ysh-expr-error 'if ("foo" ~ /[a-z]/) { echo $[_group("name")] }'
|
361 |
|
362 | # ERE
|
363 | _ysh-expr-error 'if ("foo" ~ "[a-z]") { echo $[_group(1)] }'
|
364 | _ysh-expr-error 'if ("foo" ~ "[a-z]") { echo $[_group("name")] }'
|
365 |
|
366 | _ysh-expr-error '= _group("foo")' # No such group
|
367 | }
|
368 |
|
369 | test-eggex-convert-func() {
|
370 |
|
371 | _ysh-should-run '= / <capture d+ as month: int> /'
|
372 | _ysh-should-run '= / <capture d+: int> /'
|
373 | _ysh-should-run '= / <capture d+> /'
|
374 |
|
375 | # bad convert func
|
376 | _ysh-expr-error '= / <capture d+ as month: BAD> /'
|
377 | _ysh-expr-error '= / <capture d+: BAD> /'
|
378 |
|
379 | # type error calling convert func (evalExpr)
|
380 | _ysh-expr-error 'var pat = / <capture d+: evalExpr> /; var m = "10" => search(pat) => group(1)'
|
381 | }
|
382 |
|
383 | test-int-convert() {
|
384 | _ysh-expr-error '= int({})'
|
385 | _ysh-expr-error '= int([])'
|
386 | _ysh-expr-error '= int("foo")'
|
387 | _ysh-expr-error '= int(len)'
|
388 | _ysh-expr-error '= int("foo" => startsWith)'
|
389 |
|
390 | _ysh-expr-error '= int(NAN)'
|
391 | _ysh-expr-error '= int(-INFINITY)'
|
392 | }
|
393 |
|
394 | test-float-convert() {
|
395 | _ysh-expr-error '= float({})'
|
396 | _ysh-expr-error '= float([])'
|
397 | _ysh-expr-error '= float("foo")'
|
398 | _ysh-expr-error '= float(len)'
|
399 | _ysh-expr-error '= float("foo"->startswith)'
|
400 | }
|
401 |
|
402 | test-str-convert() {
|
403 | _ysh-expr-error '= str({})'
|
404 | _ysh-expr-error '= str([])'
|
405 | _ysh-expr-error '= str(len)'
|
406 | _ysh-expr-error '= str("foo"->startswith)'
|
407 | }
|
408 |
|
409 | test-list-convert() {
|
410 | _ysh-expr-error '= list(1)'
|
411 | _ysh-expr-error '= list(len)'
|
412 | _ysh-expr-error '= list("foo"->startswith)'
|
413 | }
|
414 |
|
415 | test-dict-convert() {
|
416 | _ysh-expr-error '= dict(1)'
|
417 | _ysh-expr-error '= dict("foo")'
|
418 | _ysh-expr-error '= dict(len)'
|
419 | _ysh-expr-error '= dict("foo"->startswith)'
|
420 | _ysh-expr-error '= dict([["too", "many", "parts"]])'
|
421 | }
|
422 |
|
423 | test-proc-error-locs() {
|
424 |
|
425 | # positional
|
426 | _ysh-expr-error '
|
427 | var d = [1]
|
428 |
|
429 | func f(a=1, x=d[2]) {
|
430 | echo hi
|
431 | }
|
432 | '
|
433 |
|
434 | _ysh-expr-error '
|
435 | var d = [1]
|
436 |
|
437 | func f(; n=1, m=d[2]) {
|
438 | echo hi
|
439 | }
|
440 | '
|
441 | }
|
442 |
|
443 | test-func-error-locs() {
|
444 | # free funcs
|
445 | _ysh-expr-error '= join(["foo", "bar"], " ", 99)' # too many args
|
446 | _ysh-expr-error '= int()' # not enough args
|
447 | _ysh-expr-error '= str({})' # wrong type
|
448 |
|
449 | # bound funcs
|
450 | _ysh-expr-error '= "foo"->startswith("f", "o")' # too many args
|
451 | _ysh-expr-error '= "foo"->startswith()' # not enough args
|
452 | _ysh-expr-error '= "foo"->startswith(1)' # wrong type
|
453 |
|
454 | _ysh-expr-error '
|
455 | func f(x) {
|
456 | return (x)
|
457 | }
|
458 | = f()
|
459 | '
|
460 | }
|
461 |
|
462 | test-attr-error-locs() {
|
463 | _ysh-expr-error '= {}.key'
|
464 | _ysh-expr-error '= {}->method'
|
465 |
|
466 | _ysh-expr-error 'var obj = Object(null, {}); = obj.attr'
|
467 | _ysh-expr-error 'var obj = Object(null, {}); = obj->method'
|
468 |
|
469 | }
|
470 |
|
471 | # TODO:
|
472 | test-error-loc-bugs() {
|
473 | _ysh-expr-error '
|
474 | func id(x) {
|
475 | return (x)
|
476 | }
|
477 |
|
478 | #pp test_ (id(len(42)))
|
479 |
|
480 | # This should point at ( in len, not id(
|
481 | pp test_ (len(id(42)))
|
482 | '
|
483 |
|
484 | _ysh-expr-error '
|
485 | var methods = {}
|
486 |
|
487 | # Should point at methods, not {}
|
488 | var o = Object(methods, {})
|
489 | '
|
490 | }
|
491 |
|
492 | test-var-decl() {
|
493 | _ysh-expr-error 'var x, y = 1, 2, 3'
|
494 | _ysh-expr-error 'setvar x, y = 1, 2, 3'
|
495 | }
|
496 |
|
497 | test-const-decl() {
|
498 | _ysh-error-1 'const x = {}; const x = {};'
|
499 | _ysh-error-1 'const x; const x;'
|
500 | }
|
501 |
|
502 | test-proc-defaults() {
|
503 |
|
504 | # should be string
|
505 | _ysh-expr-error 'proc p(word=42) { echo }'
|
506 | _ysh-expr-error 'proc p(word=null) { echo }'
|
507 |
|
508 | # should be ^() or null
|
509 | _ysh-expr-error 'proc p( ; ; ; block="str") { echo }'
|
510 | _ysh-expr-error 'proc p( ; ; ; block=[]) { echo }'
|
511 |
|
512 | _ysh-should-run 'proc p( ; ; ; block=^(echo hi)) { true }'
|
513 | _ysh-should-run 'proc p( ; ; ; block=null) { true }'
|
514 |
|
515 | # divide by zero
|
516 | _ysh-expr-error 'proc p(word; t=42/0) { echo }'
|
517 |
|
518 | _ysh-error-X 1 'proc p(word; t=f()) { echo }'
|
519 |
|
520 | _ysh-error-X 1 'proc p(word; t=42; named=undef) { echo }'
|
521 |
|
522 | _ysh-error-X 1 'proc p(word; t=42; named=43; block=ZZ) { echo }'
|
523 |
|
524 | _ysh-should-run '
|
525 | proc p(word="yo"; t=42; named=43; block=null) {
|
526 | #echo $word $t $named $block
|
527 | echo $word $t $block
|
528 | }
|
529 | p
|
530 | '
|
531 | }
|
532 |
|
533 | test-proc-passing() {
|
534 | # Too few words
|
535 | _ysh-error-X 3 '
|
536 | proc p(a, b) { echo }
|
537 | p a
|
538 | '
|
539 |
|
540 | # Too many words
|
541 | _ysh-error-X 3 '
|
542 | proc p(a, b) { echo }
|
543 | p AA b c DD
|
544 | '
|
545 |
|
546 | # Too few typed
|
547 | _ysh-error-X 3 '
|
548 | proc p( ; a, b) { echo }
|
549 | p (42)
|
550 | '
|
551 |
|
552 | # Too many words
|
553 | _ysh-error-X 3 '
|
554 | proc p( ; a, b) { echo }
|
555 | p (42, 43, 44, 45)
|
556 | '
|
557 |
|
558 | _ysh-expr-error '
|
559 | proc p(; a, b) {
|
560 | echo $a - $b -
|
561 | }
|
562 | p (...[1, 2])
|
563 | p (...3)
|
564 | '
|
565 |
|
566 | # positional: rest args and spread
|
567 | _ysh-should-run '
|
568 | proc p(; a, ...b) {
|
569 | echo $a - @b -
|
570 | }
|
571 | p (1, 2, 3)
|
572 |
|
573 | var x = [4, 5, 6]
|
574 | p (...x)
|
575 | '
|
576 |
|
577 | # named: splat
|
578 | _ysh-should-run '
|
579 | proc myproc (; p ; a, b) {
|
580 | echo "$p ; $a $b"
|
581 | }
|
582 | var kwargs = {a: 42, b: 43}
|
583 | myproc (99; ...kwargs)
|
584 | '
|
585 |
|
586 | # named: rest args
|
587 | _ysh-should-run '
|
588 | proc myproc (; p ; a, b, ...named) {
|
589 | = p
|
590 | = a
|
591 | = b
|
592 | = named
|
593 | }
|
594 | var kwargs = {a: 42, b: 43, c:44}
|
595 | myproc (99; ...kwargs)
|
596 | '
|
597 | }
|
598 |
|
599 | # TODO: improve locations for all of these
|
600 | test-proc-missing() {
|
601 | # missing word param
|
602 | _ysh-error-X 3 '
|
603 | proc myproc (w) {
|
604 | = w
|
605 | }
|
606 | myproc
|
607 | '
|
608 |
|
609 | # missing typed param
|
610 | _ysh-error-X 3 '
|
611 | proc myproc (w; t1, t2) {
|
612 | = w
|
613 | = t
|
614 | }
|
615 | myproc foo (42)
|
616 | '
|
617 |
|
618 | # missing named param
|
619 | _ysh-error-X 3 '
|
620 | proc myproc (; p ; a, b) {
|
621 | echo "$p ; $a $b"
|
622 | }
|
623 | myproc (99, b=3)
|
624 | '
|
625 |
|
626 | # missing named param with semicolon
|
627 | _ysh-error-X 3 '
|
628 | proc myproc (; p ; a, b) {
|
629 | echo "$p ; $a $b"
|
630 | }
|
631 | myproc (99; b=3)
|
632 | '
|
633 |
|
634 | # missing block param
|
635 | _ysh-error-X 3 '
|
636 | proc myproc (w; p ; a, b; block) {
|
637 | = block
|
638 | }
|
639 | myproc foo (99, a=1, b=2)
|
640 | '
|
641 | }
|
642 |
|
643 | test-proc-extra() {
|
644 |
|
645 | # extra word
|
646 | _ysh-error-X 3 '
|
647 | proc myproc () {
|
648 | echo hi
|
649 | }
|
650 | myproc foo
|
651 | '
|
652 |
|
653 | # extra positional
|
654 | _ysh-error-X 3 '
|
655 | proc myproc (w) {
|
656 | echo hi
|
657 | }
|
658 | myproc foo (42)
|
659 | '
|
660 |
|
661 | # extra named
|
662 | _ysh-error-X 3 '
|
663 | proc myproc (w; p) {
|
664 | echo hi
|
665 | }
|
666 | myproc foo (42; named=1)
|
667 | '
|
668 |
|
669 | # extra block. TODO: error is about typed args
|
670 | _ysh-error-X 3 '
|
671 | proc myproc (w; p; n) {
|
672 | echo hi
|
673 | }
|
674 | myproc foo (42; n=1) { echo hi }
|
675 | '
|
676 | }
|
677 |
|
678 |
|
679 | test-func-defaults() {
|
680 | _ysh-error-X 1 'func f(a=ZZ) { echo }'
|
681 | _ysh-error-X 1 'func f(a; named=YY) { echo }'
|
682 |
|
683 | _ysh-expr-error 'func f(a=[]) { echo }'
|
684 | _ysh-expr-error 'func f(; d={a:3}) { echo }'
|
685 | }
|
686 |
|
687 | test-func-missing() {
|
688 | _ysh-expr-error '
|
689 | func f(x, y) {
|
690 | echo "$x $y"
|
691 | }
|
692 | call f(1)
|
693 | '
|
694 |
|
695 | _ysh-expr-error '
|
696 | func f(x, y; z) {
|
697 | echo "$x $y"
|
698 | }
|
699 | call f(3, 4)
|
700 | '
|
701 |
|
702 | }
|
703 |
|
704 | test-func-extra() {
|
705 | _ysh-expr-error '
|
706 | func f() {
|
707 | echo "$x $y"
|
708 | }
|
709 | call f(42) # extra pos
|
710 | '
|
711 |
|
712 | _ysh-expr-error '
|
713 | func f() {
|
714 | echo "$x $y"
|
715 | }
|
716 | call f(; x=32) # extra named
|
717 | '
|
718 | }
|
719 |
|
720 | test-func-passing() {
|
721 | # rest can't have default -- parse error
|
722 | _ysh-error-X 2 '
|
723 | func f(...rest=3) {
|
724 | return (42)
|
725 | }
|
726 | '
|
727 |
|
728 | _ysh-expr-error '
|
729 | func f(a, b) {
|
730 | echo "$a -- $b"
|
731 | }
|
732 | = f()
|
733 | '
|
734 |
|
735 | _ysh-expr-error '
|
736 | func f(a, b) {
|
737 | echo "$a -- $b"
|
738 | }
|
739 | = f(...[1, 2])
|
740 | = f(...3)
|
741 | '
|
742 |
|
743 | # rest args and splat
|
744 | _ysh-should-run '
|
745 | func f(a, ...b) {
|
746 | echo $a - @b -
|
747 | }
|
748 | = f(1, 2, 3)
|
749 |
|
750 | var x = [4, 5, 6]
|
751 | = f(...x)
|
752 | '
|
753 |
|
754 | # Named splat
|
755 | _ysh-should-run '
|
756 | func f(p ; a, b) {
|
757 | echo "$p ; $a $b"
|
758 | }
|
759 | var kwargs = {a: 42, b: 43, c: 44}
|
760 | = f(99; ...kwargs)
|
761 | '
|
762 | }
|
763 |
|
764 | test-read-builtin() {
|
765 | # no typed args
|
766 | _ysh-error-X 2 'echo hi | read (&x)'
|
767 | _ysh-error-X 2 'echo hi | read --all x y'
|
768 | _ysh-error-X 2 'echo hi | read --line x y'
|
769 | }
|
770 |
|
771 | test-equality() {
|
772 | _ysh-expr-error '
|
773 | = ^[42] === ^[43]
|
774 | '
|
775 |
|
776 | _ysh-expr-error '
|
777 | = ^(echo hi) === ^(echo yo)
|
778 | '
|
779 |
|
780 | return
|
781 |
|
782 | # Hm it's kind of weird you can do this -- it's False
|
783 | _ysh-expr-error '
|
784 | = ^[42] === "hi"
|
785 | '
|
786 | }
|
787 |
|
788 | test-float-equality() {
|
789 | _ysh-expr-error '
|
790 | var x = 1
|
791 | pp test_ (42.0 === x)'
|
792 |
|
793 | _ysh-expr-error 'pp test_ (2.0 === 1.0)'
|
794 | }
|
795 |
|
796 | test-place() {
|
797 | _ysh-expr-error '
|
798 | var a = null
|
799 | var p = &a
|
800 | call p->setValue() # 1 arg
|
801 | '
|
802 |
|
803 | _ysh-expr-error '
|
804 | var a = null
|
805 | var p = &a
|
806 | call p->setValue(3, 4)
|
807 | '
|
808 |
|
809 | _ysh-error-1 '
|
810 | func f() {
|
811 | var s = "foo"
|
812 | return (&s)
|
813 |
|
814 | }
|
815 | var p = f()
|
816 | call p->setValue(3)
|
817 | '
|
818 |
|
819 | }
|
820 |
|
821 | test-json() {
|
822 | _ysh-expr-error 'json write'
|
823 | _ysh-expr-error 'json write (42, 43)'
|
824 |
|
825 | _ysh-error-X 2 'json read zz'
|
826 | _ysh-error-X 2 'json read yy zz'
|
827 | _ysh-error-X 3 'json read (&x, 43)'
|
828 | }
|
829 |
|
830 | # For decoding errors, see data_lang/j8-errors.sh
|
831 |
|
832 | test-error-builtin() {
|
833 |
|
834 | _ysh-error-X 2 'error '
|
835 | _ysh-error-X 2 'error --'
|
836 |
|
837 | # These are OK
|
838 | _ysh-error-X 10 'error -- oops'
|
839 | _ysh-error-X 10 'error oops'
|
840 |
|
841 | _ysh-error-X 99 'error oops (code=99)'
|
842 | }
|
843 |
|
844 | test-fat-arrow() {
|
845 | #_ysh-should-run '= "str" -> upper()'
|
846 | _ysh-should-run '= "str" => upper()'
|
847 |
|
848 | _ysh-expr-error '= "str" -> bad()'
|
849 |
|
850 | # We get 'Undefined variable' error because of the fallback, could make it better
|
851 | _ysh-error-X 1 '= "str" => bad()'
|
852 |
|
853 | _ysh-should-run '= ["3", "4"] => join("/")'
|
854 |
|
855 | # Good error message for method chaining
|
856 | _ysh-expr-error '= "badstring" => join("/")'
|
857 |
|
858 |
|
859 | # float has no ExactlyEqual
|
860 | _ysh-error-X 3 "= [1.0, 2.0] => indexOf(3.14)"
|
861 |
|
862 | # Invalid type
|
863 | _ysh-expr-error '
|
864 | var myint = 42
|
865 | = "badstring" => myint("/")
|
866 | '
|
867 | }
|
868 |
|
869 | test-method-type-errors() {
|
870 | _ysh-expr-error '= "hi" => search(42)'
|
871 | _ysh-expr-error '= "hi" => leftMatch(42)'
|
872 | _ysh-expr-error "var m = 'hi' => leftMatch(/'hi'/); = m => group(3.14)"
|
873 | }
|
874 |
|
875 | test-str-replace() {
|
876 | # Some ad hoc tests - spec tests cover this
|
877 | if false; then
|
878 | _ysh-should-run '= "hi" => replace("i", "b")'
|
879 | _ysh-should-run '= "hi" => replace(/[a-z]/, "b")'
|
880 | _ysh-should-run '= "hi" => replace(/[a-z]/, "b", count=1)'
|
881 | _ysh-should-run '= "foo42" => replace(/<capture d+>/, ^"hi $1")'
|
882 | _ysh-should-run '= "foo42" => replace(/<capture d+ as num>/, ^"hi $num")'
|
883 | _ysh-should-run '= "foo42" => replace(/<capture d+ as num>/, ^"hi ${num}")'
|
884 | _ysh-should-run '= "foo42" => replace(/<capture d+ as num>/, ^"hi $[num]")'
|
885 | # test out globals - is this desirable?
|
886 | _ysh-should-run '= "foo42" => replace(/<capture d+ as num>/, ^["hi $[num] $PATH"])'
|
887 | # -1 is replace all
|
888 | _ysh-should-run '= "foo" => replace("o", "x", count=-1)'
|
889 | _ysh-should-run '= "foo" => replace("o", "x", count=-2)'
|
890 | fi
|
891 | # Replace empty string? Weird Python behavior
|
892 | _ysh-should-run '= "foo" => replace("", "-")'
|
893 | _ysh-should-run '= "foo" => replace("", "-", count=2)'
|
894 |
|
895 | # Use Expr with string
|
896 | _ysh-should-run '= "foo" => replace("o", ^"-")'
|
897 | # $0 is regular $0 here
|
898 | _ysh-should-run '= "foo" => replace("o", ^"-$0")'
|
899 |
|
900 | # Hm $0 isn't set?
|
901 | _ysh-should-run '= "foo" => replace(/[o]/, ^"-$0")'
|
902 | # Here $1 is set
|
903 | _ysh-should-run '= "foo" => replace(/<capture [o]>/, ^"-$1")'
|
904 | _ysh-should-run '= "foo" => replace(/<capture [o] as letter>/, ^"-$letter")'
|
905 |
|
906 | # Invalid arguments
|
907 | _ysh-expr-error '= "foo" => replace(42, "x")'
|
908 | _ysh-expr-error '= "foo" => replace("x", 42)'
|
909 |
|
910 | # Invalid evaluation
|
911 | _ysh-expr-error '= "foo" => replace("x", ^[42])'
|
912 | }
|
913 |
|
914 | test-remainder() {
|
915 | # second number can't be negative
|
916 | _ysh-expr-error '= 5 % -3'
|
917 | _ysh-expr-error 'var x = 5; setvar x %= -3'
|
918 | }
|
919 |
|
920 | test-append-usage-error() {
|
921 | _ysh-should-run 'append x ([])'
|
922 |
|
923 | _ysh-expr-error 'append'
|
924 |
|
925 | _ysh-expr-error 'append x' # Too few
|
926 |
|
927 | _ysh-expr-error 'append x ([], [])' # Too many
|
928 | }
|
929 |
|
930 | # Bad error location
|
931 | test-try-usage-error() {
|
932 | _ysh-error-X 2 '
|
933 | var s = "README"
|
934 | case (s) {
|
935 | README { echo hi }
|
936 | }
|
937 | echo hi
|
938 |
|
939 | try myproc
|
940 | if (_status !== 0) {
|
941 | echo failed
|
942 | }
|
943 | '
|
944 | }
|
945 |
|
946 | test-trim-utf8-error() {
|
947 | _ysh-error-here-X 3 << 'EOF'
|
948 | var badUtf = b'\yF9'
|
949 |
|
950 | # error is missed
|
951 | call " a$[badUtf]b " => trim()
|
952 | echo status=$_status
|
953 |
|
954 | # error is found
|
955 | call "$[badUtf]b " => trim()
|
956 | EOF
|
957 | }
|
958 |
|
959 | test-setglobal() {
|
960 | _ysh-should-run '
|
961 | var a = [0]
|
962 | setglobal a[1-1] = 42
|
963 | pp test_ (a)
|
964 | '
|
965 |
|
966 | _ysh-expr-error '
|
967 | var a = [0]
|
968 | setglobal a[a.bad] = 42
|
969 | pp test_ (a)
|
970 | '
|
971 |
|
972 | _ysh-should-run '
|
973 | var d = {e:{f:0}}
|
974 | setglobal d.e.f = 42
|
975 | pp test_ (d)
|
976 | setglobal d.e.f += 1
|
977 | pp test_ (d)
|
978 | '
|
979 | }
|
980 |
|
981 | test-assert() {
|
982 | _ysh-expr-error 'assert [0.0]'
|
983 | _ysh-expr-error 'assert [3 > 4]'
|
984 |
|
985 | _ysh-expr-error 'assert (0)'
|
986 | _ysh-expr-error 'assert (null === 42)'
|
987 |
|
988 | _ysh-expr-error 'assert [null === 42]'
|
989 |
|
990 | # One is long
|
991 | _ysh-expr-error 'assert [null === list(1 .. 50)]'
|
992 |
|
993 | # Both are long
|
994 | _ysh-expr-error 'assert [{k: list(3 .. 40)} === list(1 .. 50)]'
|
995 | }
|
996 |
|
997 | test-pp() {
|
998 | _ysh-expr-error 'pp (42/0)'
|
999 |
|
1000 | # Multiple lines
|
1001 | _ysh-should-run 'pp [42
|
1002 | /0]'
|
1003 |
|
1004 | _ysh-expr-error 'pp [5, 6]'
|
1005 |
|
1006 | _ysh-should-run 'pp (42)'
|
1007 | _ysh-should-run 'var x = 42; pp (x)'
|
1008 | _ysh-should-run '
|
1009 | var x = 42;
|
1010 | pp [x]'
|
1011 |
|
1012 | _ysh-should-run '
|
1013 | var x = list(1 .. 50);
|
1014 | pp [x]'
|
1015 | }
|
1016 |
|
1017 | test-module() {
|
1018 | # no args
|
1019 | _ysh-error-X 2 'use spec/testdata/module2/util.ysh; util'
|
1020 |
|
1021 | # bad arg
|
1022 | _ysh-error-X 2 'use spec/testdata/module2/util.ysh; util zz'
|
1023 |
|
1024 | # proc with bad args
|
1025 | _ysh-error-X 3 'use spec/testdata/module2/util2.ysh; util2 echo-args'
|
1026 |
|
1027 | # malformed Obj
|
1028 | _ysh-error-X 3 'use spec/testdata/module2/util2.ysh; util2 badObj otherproc'
|
1029 | }
|
1030 |
|
1031 | test-required-blocks() {
|
1032 |
|
1033 | # These are procs, which normally give usage errors
|
1034 | # The usage error prints the builtin name
|
1035 | #
|
1036 | # Funcs give you type errors though? Is that inconsistent?
|
1037 |
|
1038 | _ysh-error-X 2 'shvar'
|
1039 | _ysh-error-X 2 'push-registers'
|
1040 |
|
1041 | _ysh-error-X 2 'redir'
|
1042 | _ysh-error-X 2 'redir (42)'
|
1043 | _ysh-error-X 2 'hay eval :myvar'
|
1044 | _ysh-error-X 2 'hay eval :myvar (42)'
|
1045 | _ysh-error-X 2 'try'
|
1046 | _ysh-error-X 2 'ctx push ({})'
|
1047 |
|
1048 | _ysh-error-X 2 'fork'
|
1049 | _ysh-error-X 2 'forkwait'
|
1050 |
|
1051 | # OK this is a type error
|
1052 | _ysh-error-X 3 'forkwait ( ; ; 42)'
|
1053 |
|
1054 | _ysh-error-X 2 'haynode Foo'
|
1055 |
|
1056 | # Hm this isn't a usage error
|
1057 | _ysh-error-X 3 'haynode Foo (42)'
|
1058 |
|
1059 | # This neither
|
1060 | _ysh-error-X 3 'haynode Foo ( ; ; 42)'
|
1061 |
|
1062 | _ysh-should-run 'haynode Foo a { echo hi }'
|
1063 | }
|
1064 |
|
1065 | soil-run-py() {
|
1066 | run-test-funcs
|
1067 | }
|
1068 |
|
1069 | soil-run-cpp() {
|
1070 | local ysh=_bin/cxx-asan/ysh
|
1071 | ninja $ysh
|
1072 | YSH=$ysh run-test-funcs
|
1073 | }
|
1074 |
|
1075 | run-for-release() {
|
1076 | run-other-suite-for-release ysh-runtime-errors run-test-funcs
|
1077 | }
|
1078 |
|
1079 | "$@"
|