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

1144 lines, 457 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-int-convert() {
410 _ysh-expr-error '= int({})'
411 _ysh-expr-error '= int([])'
412 _ysh-expr-error '= int("foo")'
413 _ysh-expr-error '= int(len)'
414 _ysh-expr-error '= int("foo" => startsWith)'
415
416 _ysh-expr-error '= int(NAN)'
417 _ysh-expr-error '= int(-INFINITY)'
418}
419
420test-float-convert() {
421 _ysh-expr-error '= float({})'
422 _ysh-expr-error '= float([])'
423 _ysh-expr-error '= float("foo")'
424 _ysh-expr-error '= float(len)'
425 _ysh-expr-error '= float("foo"->startswith)'
426}
427
428test-str-convert() {
429 _ysh-expr-error '= str({})'
430 _ysh-expr-error '= str([])'
431 _ysh-expr-error '= str(len)'
432 _ysh-expr-error '= str("foo"->startswith)'
433}
434
435test-list-convert() {
436 _ysh-expr-error '= list(1)'
437 _ysh-expr-error '= list(len)'
438 _ysh-expr-error '= list("foo"->startswith)'
439}
440
441test-dict-convert() {
442 _ysh-expr-error '= dict(1)'
443 _ysh-expr-error '= dict("foo")'
444 _ysh-expr-error '= dict(len)'
445 _ysh-expr-error '= dict("foo"->startswith)'
446 _ysh-expr-error '= dict([["too", "many", "parts"]])'
447}
448
449test-proc-error-locs() {
450
451 # positional
452 _ysh-expr-error '
453 var d = [1]
454
455 func f(a=1, x=d[2]) {
456 echo hi
457 }
458 '
459
460 _ysh-expr-error '
461 var d = [1]
462
463 func f(; n=1, m=d[2]) {
464 echo hi
465 }
466 '
467}
468
469test-func-error-locs() {
470 # free funcs
471 _ysh-expr-error '= join(["foo", "bar"], " ", 99)' # too many args
472 _ysh-expr-error '= int()' # not enough args
473 _ysh-expr-error '= str({})' # wrong type
474
475 # bound funcs
476 _ysh-expr-error '= "foo"->startswith("f", "o")' # too many args
477 _ysh-expr-error '= "foo"->startswith()' # not enough args
478 _ysh-expr-error '= "foo"->startswith(1)' # wrong type
479
480 _ysh-expr-error '
481 func f(x) {
482 return (x)
483 }
484 = f()
485 '
486}
487
488test-attr-error-locs() {
489 _ysh-expr-error '= {}.key'
490 _ysh-expr-error '= {}->method'
491
492 _ysh-expr-error 'var obj = Object(null, {}); = obj.attr'
493 _ysh-expr-error 'var obj = Object(null, {}); = obj->method'
494
495}
496
497# TODO:
498test-error-loc-bugs() {
499 _ysh-expr-error '
500func id(x) {
501 return (x)
502}
503
504#pp test_ (id(len(42)))
505
506# This should point at ( in len, not id(
507pp test_ (len(id(42)))
508 '
509
510 _ysh-expr-error '
511var methods = {}
512
513# Should point at methods, not {}
514var o = Object(methods, {})
515 '
516}
517
518test-var-decl() {
519 _ysh-expr-error 'var x, y = 1, 2, 3'
520 _ysh-expr-error 'setvar x, y = 1, 2, 3'
521}
522
523test-const-decl() {
524 _ysh-error-1 'const x = {}; const x = {};'
525 _ysh-error-1 'const x; const x;'
526}
527
528test-proc-defaults() {
529
530 # should be string
531 _ysh-expr-error 'proc p(word=42) { echo }'
532 _ysh-expr-error 'proc p(word=null) { echo }'
533
534 # should be ^() or null
535 _ysh-expr-error 'proc p( ; ; ; block="str") { echo }'
536 _ysh-expr-error 'proc p( ; ; ; block=[]) { echo }'
537
538 _ysh-should-run 'proc p( ; ; ; block=^(echo hi)) { true }'
539 _ysh-should-run 'proc p( ; ; ; block=null) { true }'
540
541 # divide by zero
542 _ysh-expr-error 'proc p(word; t=42/0) { echo }'
543
544 _ysh-error-X 1 'proc p(word; t=f()) { echo }'
545
546 _ysh-error-X 1 'proc p(word; t=42; named=undef) { echo }'
547
548 _ysh-error-X 1 'proc p(word; t=42; named=43; block=ZZ) { echo }'
549
550 _ysh-should-run '
551 proc p(word="yo"; t=42; named=43; block=null) {
552 #echo $word $t $named $block
553 echo $word $t $block
554 }
555 p
556 '
557}
558
559test-proc-passing() {
560 # Too few words
561 _ysh-error-X 3 '
562 proc p(a, b) { echo }
563 p a
564 '
565
566 # Too many words
567 _ysh-error-X 3 '
568 proc p(a, b) { echo }
569 p AA b c DD
570 '
571
572 # Too few typed
573 _ysh-error-X 3 '
574 proc p( ; a, b) { echo }
575 p (42)
576 '
577
578 # Too many words
579 _ysh-error-X 3 '
580 proc p( ; a, b) { echo }
581 p (42, 43, 44, 45)
582 '
583
584 _ysh-expr-error '
585 proc p(; a, b) {
586 echo $a - $b -
587 }
588 p (...[1, 2])
589 p (...3)
590 '
591
592 # positional: rest args and spread
593 _ysh-should-run '
594 proc p(; a, ...b) {
595 echo $a - @b -
596 }
597 p (1, 2, 3)
598
599 var x = [4, 5, 6]
600 p (...x)
601 '
602
603 # named: splat
604 _ysh-should-run '
605 proc myproc (; p ; a, b) {
606 echo "$p ; $a $b"
607 }
608 var kwargs = {a: 42, b: 43}
609 myproc (99; ...kwargs)
610 '
611
612 # named: rest args
613 _ysh-should-run '
614 proc myproc (; p ; a, b, ...named) {
615 = p
616 = a
617 = b
618 = named
619 }
620 var kwargs = {a: 42, b: 43, c:44}
621 myproc (99; ...kwargs)
622 '
623}
624
625# TODO: improve locations for all of these
626test-proc-missing() {
627 # missing word param
628 _ysh-error-X 3 '
629 proc myproc (w) {
630 = w
631 }
632 myproc
633 '
634
635 # missing typed param
636 _ysh-error-X 3 '
637 proc myproc (w; t1, t2) {
638 = w
639 = t
640 }
641 myproc foo (42)
642 '
643
644 # missing named param
645 _ysh-error-X 3 '
646 proc myproc (; p ; a, b) {
647 echo "$p ; $a $b"
648 }
649 myproc (99, b=3)
650 '
651
652 # missing named param with semicolon
653 _ysh-error-X 3 '
654 proc myproc (; p ; a, b) {
655 echo "$p ; $a $b"
656 }
657 myproc (99; b=3)
658 '
659
660 # missing block param
661 _ysh-error-X 3 '
662 proc myproc (w; p ; a, b; block) {
663 = block
664 }
665 myproc foo (99, a=1, b=2)
666 '
667}
668
669test-proc-extra() {
670
671 # extra word
672 _ysh-error-X 3 '
673 proc myproc () {
674 echo hi
675 }
676 myproc foo
677 '
678
679 # extra positional
680 _ysh-error-X 3 '
681 proc myproc (w) {
682 echo hi
683 }
684 myproc foo (42)
685 '
686
687 # extra named
688 _ysh-error-X 3 '
689 proc myproc (w; p) {
690 echo hi
691 }
692 myproc foo (42; named=1)
693 '
694
695 # extra block. TODO: error is about typed args
696 _ysh-error-X 3 '
697 proc myproc (w; p; n) {
698 echo hi
699 }
700 myproc foo (42; n=1) { echo hi }
701 '
702}
703
704
705test-func-defaults() {
706 _ysh-error-X 1 'func f(a=ZZ) { echo }'
707 _ysh-error-X 1 'func f(a; named=YY) { echo }'
708
709 _ysh-expr-error 'func f(a=[]) { echo }'
710 _ysh-expr-error 'func f(; d={a:3}) { echo }'
711}
712
713test-func-missing() {
714 _ysh-expr-error '
715 func f(x, y) {
716 echo "$x $y"
717 }
718 call f(1)
719 '
720
721 _ysh-expr-error '
722 func f(x, y; z) {
723 echo "$x $y"
724 }
725 call f(3, 4)
726 '
727
728}
729
730test-func-extra() {
731 _ysh-expr-error '
732 func f() {
733 echo "$x $y"
734 }
735 call f(42) # extra pos
736 '
737
738 _ysh-expr-error '
739 func f() {
740 echo "$x $y"
741 }
742 call f(; x=32) # extra named
743 '
744}
745
746test-func-passing() {
747 # rest can't have default -- parse error
748 _ysh-error-X 2 '
749 func f(...rest=3) {
750 return (42)
751 }
752 '
753
754 _ysh-expr-error '
755 func f(a, b) {
756 echo "$a -- $b"
757 }
758 = f()
759 '
760
761 _ysh-expr-error '
762 func f(a, b) {
763 echo "$a -- $b"
764 }
765 = f(...[1, 2])
766 = f(...3)
767 '
768
769 # rest args and splat
770 _ysh-should-run '
771 func f(a, ...b) {
772 echo $a - @b -
773 }
774 = f(1, 2, 3)
775
776 var x = [4, 5, 6]
777 = f(...x)
778 '
779
780 # Named splat
781 _ysh-should-run '
782 func f(p ; a, b) {
783 echo "$p ; $a $b"
784 }
785 var kwargs = {a: 42, b: 43, c: 44}
786 = f(99; ...kwargs)
787 '
788}
789
790test-read-builtin() {
791 # no typed args
792 _ysh-error-X 2 'echo hi | read (&x)'
793 _ysh-error-X 2 'echo hi | read --all x y'
794 _ysh-error-X 2 'echo hi | read --line x y'
795}
796
797test-equality() {
798 _ysh-expr-error '
799 = ^[42] === ^[43]
800 '
801
802 _ysh-expr-error '
803 = ^(echo hi) === ^(echo yo)
804 '
805
806 return
807
808 # Hm it's kind of weird you can do this -- it's False
809 _ysh-expr-error '
810 = ^[42] === "hi"
811 '
812}
813
814test-float-equality() {
815 _ysh-expr-error '
816var x = 1
817pp test_ (42.0 === x)'
818
819 _ysh-expr-error 'pp test_ (2.0 === 1.0)'
820}
821
822test-place() {
823 _ysh-expr-error '
824 var a = null
825 var p = &a
826 call p->setValue() # 1 arg
827 '
828
829 _ysh-expr-error '
830 var a = null
831 var p = &a
832 call p->setValue(3, 4)
833 '
834
835 # DISABLED 2024-10, after implementing modules
836 if false; then
837 _ysh-error-1 '
838 func f() {
839 var s = "foo"
840 return (&s)
841
842 }
843 var p = f()
844 call p->setValue(3)
845 '
846 fi
847
848}
849
850test-json() {
851 _ysh-expr-error 'json write'
852 _ysh-expr-error 'json write (42, 43)'
853
854 _ysh-error-X 2 'json read zz'
855 _ysh-error-X 2 'json read yy zz'
856 _ysh-error-X 3 'json read (&x, 43)'
857}
858
859# For decoding errors, see data_lang/j8-errors.sh
860
861test-error-builtin() {
862
863 _ysh-error-X 2 'error '
864 _ysh-error-X 2 'error --'
865
866 # These are OK
867 _ysh-error-X 10 'error -- oops'
868 _ysh-error-X 10 'error oops'
869
870 _ysh-error-X 99 'error oops (code=99)'
871}
872
873test-fat-arrow() {
874 #_ysh-should-run '= "str" -> upper()'
875 _ysh-should-run '= "str" => upper()'
876
877 _ysh-expr-error '= "str" -> bad()'
878
879 # We get 'Undefined variable' error because of the fallback, could make it better
880 _ysh-error-X 1 '= "str" => bad()'
881
882 _ysh-should-run '= ["3", "4"] => join("/")'
883
884 # Good error message for method chaining
885 _ysh-expr-error '= "badstring" => join("/")'
886
887
888 # float has no ExactlyEqual
889 _ysh-error-X 3 "= [1.0, 2.0] => indexOf(3.14)"
890
891 # Invalid type
892 _ysh-expr-error '
893 var myint = 42
894 = "badstring" => myint("/")
895 '
896}
897
898test-method-type-errors() {
899 _ysh-expr-error '= "hi" => search(42)'
900 _ysh-expr-error '= "hi" => leftMatch(42)'
901 _ysh-expr-error "var m = 'hi' => leftMatch(/'hi'/); = m => group(3.14)"
902}
903
904test-str-replace() {
905 # Some ad hoc tests - spec tests cover this
906 if false; then
907 _ysh-should-run '= "hi" => replace("i", "b")'
908 _ysh-should-run '= "hi" => replace(/[a-z]/, "b")'
909 _ysh-should-run '= "hi" => replace(/[a-z]/, "b", count=1)'
910 _ysh-should-run '= "foo42" => replace(/<capture d+>/, ^"hi $1")'
911 _ysh-should-run '= "foo42" => replace(/<capture d+ as num>/, ^"hi $num")'
912 _ysh-should-run '= "foo42" => replace(/<capture d+ as num>/, ^"hi ${num}")'
913 _ysh-should-run '= "foo42" => replace(/<capture d+ as num>/, ^"hi $[num]")'
914 # test out globals - is this desirable?
915 _ysh-should-run '= "foo42" => replace(/<capture d+ as num>/, ^["hi $[num] $PATH"])'
916 # -1 is replace all
917 _ysh-should-run '= "foo" => replace("o", "x", count=-1)'
918 _ysh-should-run '= "foo" => replace("o", "x", count=-2)'
919 fi
920 # Replace empty string? Weird Python behavior
921 _ysh-should-run '= "foo" => replace("", "-")'
922 _ysh-should-run '= "foo" => replace("", "-", count=2)'
923
924 # Use Expr with string
925 _ysh-should-run '= "foo" => replace("o", ^"-")'
926 # $0 is regular $0 here
927 _ysh-should-run '= "foo" => replace("o", ^"-$0")'
928
929 # Hm $0 isn't set?
930 _ysh-should-run '= "foo" => replace(/[o]/, ^"-$0")'
931 # Here $1 is set
932 _ysh-should-run '= "foo" => replace(/<capture [o]>/, ^"-$1")'
933 _ysh-should-run '= "foo" => replace(/<capture [o] as letter>/, ^"-$letter")'
934
935 # Invalid arguments
936 _ysh-expr-error '= "foo" => replace(42, "x")'
937 _ysh-expr-error '= "foo" => replace("x", 42)'
938
939 # Invalid evaluation
940 _ysh-expr-error '= "foo" => replace("x", ^[42])'
941}
942
943test-remainder() {
944 # second number can't be negative
945 _ysh-expr-error '= 5 % -3'
946 _ysh-expr-error 'var x = 5; setvar x %= -3'
947}
948
949test-append-usage-error() {
950 _ysh-should-run 'append x ([])'
951
952 _ysh-expr-error 'append'
953
954 _ysh-expr-error 'append x' # Too few
955
956 _ysh-expr-error 'append x ([], [])' # Too many
957}
958
959test-try-usage-error() {
960 _ysh-error-X 2 '
961var s = "README"
962case (s) {
963 README { echo hi }
964}
965echo hi
966
967try myproc
968if (_status !== 0) {
969 echo failed
970}
971'
972}
973
974test-trim-utf8-error() {
975 _ysh-error-here-X 3 << 'EOF'
976 var badUtf = b'\yF9'
977
978 # error is missed
979 call " a$[badUtf]b " => trim()
980 echo status=$_status
981
982 # error is found
983 call "$[badUtf]b " => trim()
984EOF
985}
986
987test-setglobal() {
988 _ysh-should-run '
989var a = [0]
990setglobal a[1-1] = 42
991pp test_ (a)
992 '
993
994 _ysh-expr-error '
995var a = [0]
996setglobal a[a.bad] = 42
997pp test_ (a)
998 '
999
1000 _ysh-should-run '
1001var d = {e:{f:0}}
1002setglobal d.e.f = 42
1003pp test_ (d)
1004setglobal d.e.f += 1
1005pp test_ (d)
1006 '
1007}
1008
1009test-assert() {
1010 _ysh-expr-error 'assert [0.0]'
1011 _ysh-expr-error 'assert [3 > 4]'
1012
1013 _ysh-expr-error 'assert (0)'
1014 _ysh-expr-error 'assert (null === 42)'
1015
1016 _ysh-expr-error 'assert [null === 42]'
1017
1018 # One is long
1019 _ysh-expr-error 'assert [null === list(1 ..< 50)]'
1020
1021 # Both are long
1022 _ysh-expr-error 'assert [{k: list(3 ..< 40)} === list(1 ..< 50)]'
1023}
1024
1025test-pp() {
1026 _ysh-expr-error 'pp (42/0)'
1027
1028 # Multiple lines
1029 _ysh-should-run 'pp [42
1030/0]'
1031
1032 _ysh-expr-error 'pp [5, 6]'
1033
1034 _ysh-should-run 'pp (42)'
1035 _ysh-should-run 'var x = 42; pp (x)'
1036 _ysh-should-run '
1037var x = 42;
1038pp [x]'
1039
1040 _ysh-should-run '
1041var x = list(1 ..< 50);
1042pp [x]'
1043}
1044
1045test-module() {
1046 # no args
1047 _ysh-error-X 2 'use spec/testdata/module2/util.ysh; util'
1048
1049 # bad arg
1050 _ysh-error-X 2 'use spec/testdata/module2/util.ysh; util zz'
1051
1052 # proc with bad args
1053 _ysh-error-X 3 'use spec/testdata/module2/util2.ysh; util2 echo-args'
1054
1055 # malformed Obj
1056 _ysh-error-X 3 'use spec/testdata/module2/util2.ysh; util2 badObj otherproc'
1057}
1058
1059test-required-blocks() {
1060
1061 # These are procs, which normally give usage errors
1062 # The usage error prints the builtin name
1063 #
1064 # Funcs give you type errors though? Is that inconsistent?
1065
1066 _ysh-error-X 2 'shvar'
1067 _ysh-error-X 2 'push-registers'
1068
1069 _ysh-error-X 2 'redir'
1070 _ysh-error-X 2 'redir (42)'
1071 _ysh-error-X 2 'hay eval :myvar'
1072 _ysh-error-X 2 'hay eval :myvar (42)'
1073 _ysh-error-X 2 'try'
1074 _ysh-error-X 2 'ctx push ({})'
1075
1076 _ysh-error-X 2 'fork'
1077 _ysh-error-X 2 'forkwait'
1078
1079 # OK this is a type error
1080 _ysh-error-X 3 'forkwait ( ; ; 42)'
1081
1082 _ysh-error-X 2 'haynode Foo'
1083
1084 # Hm this isn't a usage error
1085 _ysh-error-X 3 'haynode Foo (42)'
1086
1087 # This neither
1088 _ysh-error-X 3 'haynode Foo ( ; ; 42)'
1089
1090 _ysh-should-run 'haynode Foo a { echo hi }'
1091}
1092
1093test-obj-methods() {
1094 _ysh-error-X 3 'var o = Object(null, {}); pp test_ (o[1])'
1095 _ysh-error-X 3 'var o = Str; pp test_ (Str[1])'
1096
1097 _ysh-error-X 3 'pp test_ (Bool[Bool])'
1098 _ysh-error-X 3 'pp test_ (Dict[Bool])'
1099 _ysh-error-X 3 'pp test_ (List[Str, Bool])'
1100
1101 # break invariants
1102 _ysh-error-X 3 'call propView(List)->erase("name"); pp test_ (List[Str])'
1103 _ysh-error-X 3 'call propView(Str)->erase("name"); pp test_ (List[Str])'
1104
1105 _ysh-error-X 3 'pp test_ (List[Str, 3])'
1106}
1107
1108test-int-overflow() {
1109 local pos='18446744073709551616'
1110 local neg='-18446744073709551616'
1111
1112 # arithmetic
1113
1114 # _ConvertToInt
1115 _ysh-error-1 "var s = '$pos'; = s % 2"
1116 _ysh-error-1 "var s = '$neg'; = s % 2"
1117
1118 # _ConvertToNumber
1119 _ysh-error-1 "var s = '$pos'; = s + 1"
1120 _ysh-error-1 "var s = '$neg'; = s + 1"
1121
1122 _ysh-error-1 "= '$pos' ~== 42"
1123 _ysh-error-1 "= '$neg' ~== 42"
1124
1125 # builtins
1126 _ysh-expr-error "= int('$pos')"
1127 _ysh-expr-error "= int('$neg')"
1128}
1129
1130soil-run-py() {
1131 run-test-funcs
1132}
1133
1134soil-run-cpp() {
1135 local ysh=_bin/cxx-asan/ysh
1136 ninja $ysh
1137 YSH=$ysh run-test-funcs
1138}
1139
1140run-for-release() {
1141 run-other-suite-for-release ysh-runtime-errors run-test-funcs
1142}
1143
1144"$@"