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