OILS / test / ysh-runtime-errors.sh View on Github | oilshell.org

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