| 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 | 
 | 
| 370 | test-float-convert() {
 | 
| 371 |   _ysh-expr-error '= float({})'
 | 
| 372 |   _ysh-expr-error '= float([])'
 | 
| 373 |   _ysh-expr-error '= float("foo")'
 | 
| 374 |   _ysh-expr-error '= float(len)'
 | 
| 375 |   _ysh-expr-error '= float("foo"->startswith)'
 | 
| 376 | }
 | 
| 377 | 
 | 
| 378 | test-str-convert() {
 | 
| 379 |   _ysh-expr-error '= str({})'
 | 
| 380 |   _ysh-expr-error '= str([])'
 | 
| 381 |   _ysh-expr-error '= str(len)'
 | 
| 382 |   _ysh-expr-error '= str("foo"->startswith)'
 | 
| 383 | }
 | 
| 384 | 
 | 
| 385 | test-list-convert() {
 | 
| 386 |   _ysh-expr-error '= list(1)'
 | 
| 387 |   _ysh-expr-error '= list(len)'
 | 
| 388 |   _ysh-expr-error '= list("foo"->startswith)'
 | 
| 389 | }
 | 
| 390 | 
 | 
| 391 | test-dict-convert() {
 | 
| 392 |   _ysh-expr-error '= dict(1)'
 | 
| 393 |   _ysh-expr-error '= dict("foo")'
 | 
| 394 |   _ysh-expr-error '= dict(len)'
 | 
| 395 |   _ysh-expr-error '= dict("foo"->startswith)'
 | 
| 396 |   _ysh-expr-error '= dict([["too", "many", "parts"]])'
 | 
| 397 | }
 | 
| 398 | 
 | 
| 399 | test-proc-error-locs() {
 | 
| 400 | 
 | 
| 401 |   # positional
 | 
| 402 |   _ysh-expr-error '
 | 
| 403 |   var d = [1]
 | 
| 404 | 
 | 
| 405 |   func f(a=1, x=d[2]) {
 | 
| 406 |     echo hi
 | 
| 407 |   }
 | 
| 408 |   '
 | 
| 409 | 
 | 
| 410 |   _ysh-expr-error '
 | 
| 411 |   var d = [1]
 | 
| 412 | 
 | 
| 413 |   func f(; n=1, m=d[2]) {
 | 
| 414 |     echo hi
 | 
| 415 |   }
 | 
| 416 |   '
 | 
| 417 | }
 | 
| 418 | 
 | 
| 419 | test-func-error-locs() {
 | 
| 420 |   # free funcs
 | 
| 421 |   _ysh-expr-error '= join(["foo", "bar"], " ", 99)' # too many args
 | 
| 422 |   _ysh-expr-error '= int()' # not enough args
 | 
| 423 |   _ysh-expr-error '= str({})' # wrong type
 | 
| 424 | 
 | 
| 425 |   # bound funcs
 | 
| 426 |   _ysh-expr-error '= "foo"->startswith("f", "o")' # too many args
 | 
| 427 |   _ysh-expr-error '= "foo"->startswith()' # not enough args
 | 
| 428 |   _ysh-expr-error '= "foo"->startswith(1)' # wrong type
 | 
| 429 | 
 | 
| 430 |   _ysh-expr-error '
 | 
| 431 |   func f(x) {
 | 
| 432 |      return (x)
 | 
| 433 |   }
 | 
| 434 |   = f()
 | 
| 435 |   '
 | 
| 436 | }
 | 
| 437 | 
 | 
| 438 | test-var-decl() {
 | 
| 439 |   _ysh-expr-error 'var x, y = 1, 2, 3'
 | 
| 440 |   _ysh-expr-error 'setvar x, y = 1, 2, 3'
 | 
| 441 | }
 | 
| 442 | 
 | 
| 443 | test-const-decl() {
 | 
| 444 |   _ysh-error-1 'const x = {}; const x = {};'
 | 
| 445 |   _ysh-error-1 'const x; const x;'
 | 
| 446 | }
 | 
| 447 | 
 | 
| 448 | test-proc-defaults() {
 | 
| 449 |   
 | 
| 450 |   # should be string
 | 
| 451 |   _ysh-expr-error 'proc p(word=42) { echo }'
 | 
| 452 |   _ysh-expr-error 'proc p(word=null) { echo }'
 | 
| 453 | 
 | 
| 454 |   # should be ^() or null
 | 
| 455 |   _ysh-expr-error 'proc p( ; ; ; block="str") { echo }'
 | 
| 456 |   _ysh-expr-error 'proc p( ; ; ; block=[]) { echo }'
 | 
| 457 | 
 | 
| 458 |   _ysh-should-run 'proc p( ; ; ; block=^(echo hi)) { true }'
 | 
| 459 |   _ysh-should-run 'proc p( ; ; ; block=null) { true }'
 | 
| 460 | 
 | 
| 461 |   # divide by zero
 | 
| 462 |   _ysh-expr-error 'proc p(word; t=42/0) { echo }'
 | 
| 463 | 
 | 
| 464 |   _ysh-error-X 1 'proc p(word; t=f()) { echo }'
 | 
| 465 | 
 | 
| 466 |   _ysh-error-X 1 'proc p(word; t=42; named=undef) { echo }'
 | 
| 467 | 
 | 
| 468 |   _ysh-error-X 1 'proc p(word; t=42; named=43; block=ZZ) { echo }'
 | 
| 469 | 
 | 
| 470 |   _ysh-should-run '
 | 
| 471 |   proc p(word="yo"; t=42; named=43; block=null) {
 | 
| 472 |     #echo $word $t $named $block
 | 
| 473 |     echo $word $t $block
 | 
| 474 |   }
 | 
| 475 |   p
 | 
| 476 |   '
 | 
| 477 | }
 | 
| 478 | 
 | 
| 479 | test-proc-passing() {
 | 
| 480 |   # Too few words
 | 
| 481 |   _ysh-error-X 3 '
 | 
| 482 |   proc p(a, b) { echo }
 | 
| 483 |   p a
 | 
| 484 |   '
 | 
| 485 | 
 | 
| 486 |   # Too many words
 | 
| 487 |   _ysh-error-X 3 '
 | 
| 488 |   proc p(a, b) { echo }
 | 
| 489 |   p AA b c DD
 | 
| 490 |   '
 | 
| 491 | 
 | 
| 492 |   # Too few typed
 | 
| 493 |   _ysh-error-X 3 '
 | 
| 494 |   proc p( ; a, b) { echo }
 | 
| 495 |   p (42)
 | 
| 496 |   '
 | 
| 497 | 
 | 
| 498 |   # Too many words
 | 
| 499 |   _ysh-error-X 3 '
 | 
| 500 |   proc p( ; a, b) { echo }
 | 
| 501 |   p (42, 43, 44, 45)
 | 
| 502 |   '
 | 
| 503 | 
 | 
| 504 |   _ysh-expr-error '
 | 
| 505 |   proc p(; a, b) {
 | 
| 506 |     echo $a - $b -
 | 
| 507 |   }
 | 
| 508 |   p (...[1, 2])
 | 
| 509 |   p (...3)
 | 
| 510 |   '
 | 
| 511 | 
 | 
| 512 |   # positional: rest args and spread
 | 
| 513 |   _ysh-should-run '
 | 
| 514 |   proc p(; a, ...b) {
 | 
| 515 |     echo $a - @b -
 | 
| 516 |   }
 | 
| 517 |   p (1, 2, 3)
 | 
| 518 | 
 | 
| 519 |   var x = [4, 5, 6]
 | 
| 520 |   p (...x)
 | 
| 521 |   '
 | 
| 522 | 
 | 
| 523 |   # named: splat
 | 
| 524 |   _ysh-should-run '
 | 
| 525 |   proc myproc (; p ; a, b) {
 | 
| 526 |     echo "$p ; $a $b"
 | 
| 527 |   }
 | 
| 528 |   var kwargs = {a: 42, b: 43}
 | 
| 529 |   myproc (99; ...kwargs)
 | 
| 530 |   '
 | 
| 531 | 
 | 
| 532 |   # named: rest args
 | 
| 533 |   _ysh-should-run '
 | 
| 534 |   proc myproc (; p ; a, b, ...named) {
 | 
| 535 |     = p
 | 
| 536 |     = a
 | 
| 537 |     = b
 | 
| 538 |     = named
 | 
| 539 |   }
 | 
| 540 |   var kwargs = {a: 42, b: 43, c:44}
 | 
| 541 |   myproc (99; ...kwargs)
 | 
| 542 |   '
 | 
| 543 | }
 | 
| 544 | 
 | 
| 545 | # TODO: improve locations for all of these
 | 
| 546 | test-proc-missing() {
 | 
| 547 |   # missing word param
 | 
| 548 |   _ysh-error-X 3 '
 | 
| 549 |   proc myproc (w) {
 | 
| 550 |     = w
 | 
| 551 |   }
 | 
| 552 |   myproc
 | 
| 553 |   '
 | 
| 554 | 
 | 
| 555 |   # missing typed param
 | 
| 556 |   _ysh-error-X 3 '
 | 
| 557 |   proc myproc (w; t1, t2) {
 | 
| 558 |     = w
 | 
| 559 |     = t
 | 
| 560 |   }
 | 
| 561 |   myproc foo (42)
 | 
| 562 |   '
 | 
| 563 | 
 | 
| 564 |   # missing named param
 | 
| 565 |   _ysh-error-X 3 '
 | 
| 566 |   proc myproc (; p ; a, b) {
 | 
| 567 |     echo "$p ; $a $b"
 | 
| 568 |   }
 | 
| 569 |   myproc (99, b=3)
 | 
| 570 |   '
 | 
| 571 | 
 | 
| 572 |   # missing named param with semicolon
 | 
| 573 |   _ysh-error-X 3 '
 | 
| 574 |   proc myproc (; p ; a, b) {
 | 
| 575 |     echo "$p ; $a $b"
 | 
| 576 |   }
 | 
| 577 |   myproc (99; b=3)
 | 
| 578 |   '
 | 
| 579 | 
 | 
| 580 |   # missing block param
 | 
| 581 |   _ysh-error-X 3 '
 | 
| 582 |   proc myproc (w; p ; a, b; block) {
 | 
| 583 |     = block
 | 
| 584 |   }
 | 
| 585 |   myproc foo (99, a=1, b=2)
 | 
| 586 |   '
 | 
| 587 | }
 | 
| 588 | 
 | 
| 589 | test-proc-extra() {
 | 
| 590 | 
 | 
| 591 |   # extra word
 | 
| 592 |   _ysh-error-X 3 '
 | 
| 593 |   proc myproc () {
 | 
| 594 |     echo hi
 | 
| 595 |   }
 | 
| 596 |   myproc foo
 | 
| 597 |   '
 | 
| 598 | 
 | 
| 599 |   # extra positional
 | 
| 600 |   _ysh-error-X 3 '
 | 
| 601 |   proc myproc (w) {
 | 
| 602 |     echo hi
 | 
| 603 |   }
 | 
| 604 |   myproc foo (42)
 | 
| 605 |   '
 | 
| 606 | 
 | 
| 607 |   # extra named
 | 
| 608 |   _ysh-error-X 3 '
 | 
| 609 |   proc myproc (w; p) {
 | 
| 610 |     echo hi
 | 
| 611 |   }
 | 
| 612 |   myproc foo (42; named=1)
 | 
| 613 |   '
 | 
| 614 | 
 | 
| 615 |   # extra block.  TODO: error is about typed args
 | 
| 616 |   _ysh-error-X 3 '
 | 
| 617 |   proc myproc (w; p; n) {
 | 
| 618 |     echo hi
 | 
| 619 |   }
 | 
| 620 |   myproc foo (42; n=1) { echo hi }
 | 
| 621 |   '
 | 
| 622 | }
 | 
| 623 | 
 | 
| 624 | 
 | 
| 625 | test-func-defaults() {
 | 
| 626 |   _ysh-error-X 1 'func f(a=ZZ) { echo }'
 | 
| 627 |   _ysh-error-X 1 'func f(a; named=YY) { echo }'
 | 
| 628 | 
 | 
| 629 |   _ysh-expr-error 'func f(a=[]) { echo }'
 | 
| 630 |   _ysh-expr-error 'func f(; d={a:3}) { echo }'
 | 
| 631 | }
 | 
| 632 | 
 | 
| 633 | test-func-missing() {
 | 
| 634 |   _ysh-expr-error '
 | 
| 635 |   func f(x, y) {
 | 
| 636 |     echo "$x $y"
 | 
| 637 |   }
 | 
| 638 |   call f(1)
 | 
| 639 |   '
 | 
| 640 | 
 | 
| 641 |   _ysh-expr-error '
 | 
| 642 |   func f(x, y; z) {
 | 
| 643 |     echo "$x $y"
 | 
| 644 |   }
 | 
| 645 |   call f(3, 4)
 | 
| 646 |   '
 | 
| 647 | 
 | 
| 648 | }
 | 
| 649 | 
 | 
| 650 | test-func-extra() {
 | 
| 651 |   _ysh-expr-error '
 | 
| 652 |   func f() {
 | 
| 653 |     echo "$x $y"
 | 
| 654 |   }
 | 
| 655 |   call f(42)  # extra pos
 | 
| 656 |   '
 | 
| 657 | 
 | 
| 658 |   _ysh-expr-error '
 | 
| 659 |   func f() {
 | 
| 660 |     echo "$x $y"
 | 
| 661 |   }
 | 
| 662 |   call f(; x=32)  # extra named
 | 
| 663 |   '
 | 
| 664 | }
 | 
| 665 | 
 | 
| 666 | test-func-passing() {
 | 
| 667 |   # rest can't have default -- parse error
 | 
| 668 |   _ysh-error-X 2 '
 | 
| 669 |   func f(...rest=3) {
 | 
| 670 |     return (42)
 | 
| 671 |   }
 | 
| 672 |   '
 | 
| 673 | 
 | 
| 674 |   _ysh-expr-error '
 | 
| 675 |   func f(a, b) {
 | 
| 676 |     echo "$a -- $b"
 | 
| 677 |   }
 | 
| 678 |   = f()
 | 
| 679 |   '
 | 
| 680 | 
 | 
| 681 |   _ysh-expr-error '
 | 
| 682 |   func f(a, b) {
 | 
| 683 |     echo "$a -- $b"
 | 
| 684 |   }
 | 
| 685 |   = f(...[1, 2])
 | 
| 686 |   = f(...3)
 | 
| 687 |   '
 | 
| 688 | 
 | 
| 689 |   # rest args and splat
 | 
| 690 |   _ysh-should-run '
 | 
| 691 |   func f(a, ...b) {
 | 
| 692 |     echo $a - @b -
 | 
| 693 |   }
 | 
| 694 |   = f(1, 2, 3)
 | 
| 695 | 
 | 
| 696 |   var x = [4, 5, 6]
 | 
| 697 |   = f(...x)
 | 
| 698 |   '
 | 
| 699 | 
 | 
| 700 |   # Named splat
 | 
| 701 |   _ysh-should-run '
 | 
| 702 |   func f(p ; a, b) {
 | 
| 703 |     echo "$p ; $a $b"
 | 
| 704 |   }
 | 
| 705 |   var kwargs = {a: 42, b: 43, c: 44}
 | 
| 706 |   = f(99; ...kwargs)
 | 
| 707 |   '
 | 
| 708 | }
 | 
| 709 | 
 | 
| 710 | test-read-builtin() {
 | 
| 711 |   # no typed args
 | 
| 712 |   _ysh-error-X 2 'echo hi | read (&x)'
 | 
| 713 |   _ysh-error-X 2 'echo hi | read --all x y'
 | 
| 714 |   _ysh-error-X 2 'echo hi | read --line x y'
 | 
| 715 | }
 | 
| 716 | 
 | 
| 717 | test-equality() {
 | 
| 718 |   _ysh-expr-error '
 | 
| 719 |   = ^[42] === ^[43]
 | 
| 720 |   '
 | 
| 721 | 
 | 
| 722 |   _ysh-expr-error '
 | 
| 723 |   = ^(echo hi) === ^(echo yo)
 | 
| 724 |   '
 | 
| 725 | 
 | 
| 726 |   return
 | 
| 727 | 
 | 
| 728 |   # Hm it's kind of weird you can do this -- it's False
 | 
| 729 |   _ysh-expr-error '
 | 
| 730 |   = ^[42] === "hi"
 | 
| 731 |   '
 | 
| 732 | }
 | 
| 733 | 
 | 
| 734 | test-place() {
 | 
| 735 |   _ysh-expr-error '
 | 
| 736 |   var a = null
 | 
| 737 |   var p = &a
 | 
| 738 |   call p->setValue()  # 1 arg
 | 
| 739 |   '
 | 
| 740 | 
 | 
| 741 |   _ysh-expr-error '
 | 
| 742 |   var a = null
 | 
| 743 |   var p = &a
 | 
| 744 |   call p->setValue(3, 4)
 | 
| 745 |   '
 | 
| 746 | 
 | 
| 747 |   _ysh-error-1 '
 | 
| 748 |   func f() {
 | 
| 749 |     var s = "foo"
 | 
| 750 |     return (&s)
 | 
| 751 | 
 | 
| 752 |   }
 | 
| 753 |   var p = f()
 | 
| 754 |   call p->setValue(3)
 | 
| 755 |   '
 | 
| 756 | 
 | 
| 757 | }
 | 
| 758 | 
 | 
| 759 | test-json() {
 | 
| 760 |   _ysh-expr-error 'json write'
 | 
| 761 |   _ysh-expr-error 'json write (42, 43)'
 | 
| 762 | 
 | 
| 763 |   _ysh-error-X 2 'json read zz'
 | 
| 764 |   _ysh-error-X 2 'json read yy zz'
 | 
| 765 |   _ysh-error-X 3 'json read (&x, 43)'
 | 
| 766 | }
 | 
| 767 | 
 | 
| 768 | # For decoding errors, see data_lang/j8-errors.sh
 | 
| 769 | 
 | 
| 770 | test-error-builtin() {
 | 
| 771 | 
 | 
| 772 |   _ysh-error-X 2 'error '
 | 
| 773 |   _ysh-error-X 2 'error --'
 | 
| 774 | 
 | 
| 775 |   # These are OK
 | 
| 776 |   _ysh-error-X 10 'error -- oops'
 | 
| 777 |   _ysh-error-X 10 'error oops'
 | 
| 778 | 
 | 
| 779 |   _ysh-error-X 99 'error oops (code=99)'
 | 
| 780 | }
 | 
| 781 | 
 | 
| 782 | test-fat-arrow() {
 | 
| 783 |   #_ysh-should-run '= "str" -> upper()'
 | 
| 784 |   _ysh-should-run '= "str" => upper()'
 | 
| 785 | 
 | 
| 786 |   _ysh-expr-error '= "str" -> bad()'
 | 
| 787 | 
 | 
| 788 |   # We get 'Undefined variable' error because of the fallback, could make it better
 | 
| 789 |   _ysh-error-X 1 '= "str" => bad()'
 | 
| 790 | 
 | 
| 791 |   _ysh-should-run '= ["3", "4"] => join("/")'
 | 
| 792 | 
 | 
| 793 |   # Good error message for method chaining
 | 
| 794 |   _ysh-expr-error '= "badstring" => join("/")'
 | 
| 795 | 
 | 
| 796 | 
 | 
| 797 |   # float has no ExactlyEqual
 | 
| 798 |   _ysh-error-X 3 "= [1.0, 2.0] => indexOf(3.14)"
 | 
| 799 | 
 | 
| 800 |   # Invalid type
 | 
| 801 |   _ysh-expr-error '
 | 
| 802 |   var myint = 42
 | 
| 803 |   = "badstring" => myint("/")
 | 
| 804 |   '
 | 
| 805 | }
 | 
| 806 | 
 | 
| 807 | test-method-type-errors() {
 | 
| 808 |    _ysh-expr-error '= "hi" => search(42)'
 | 
| 809 |    _ysh-expr-error '= "hi" => leftMatch(42)'
 | 
| 810 |    _ysh-expr-error "var m = 'hi' => leftMatch(/'hi'/); = m => group(3.14)"
 | 
| 811 | }
 | 
| 812 | 
 | 
| 813 | test-str-replace() {
 | 
| 814 |    # Some ad hoc tests - spec tests cover this
 | 
| 815 |    if false; then
 | 
| 816 |      _ysh-should-run '= "hi" => replace("i", "b")'
 | 
| 817 |      _ysh-should-run '= "hi" => replace(/[a-z]/, "b")'
 | 
| 818 |      _ysh-should-run '= "hi" => replace(/[a-z]/, "b", count=1)'
 | 
| 819 |      _ysh-should-run '= "foo42" => replace(/<capture d+>/, ^"hi $1")'
 | 
| 820 |      _ysh-should-run '= "foo42" => replace(/<capture d+ as num>/, ^"hi $num")'
 | 
| 821 |      _ysh-should-run '= "foo42" => replace(/<capture d+ as num>/, ^"hi ${num}")'
 | 
| 822 |      _ysh-should-run '= "foo42" => replace(/<capture d+ as num>/, ^"hi $[num]")'
 | 
| 823 |      # test out globals - is this desirable?
 | 
| 824 |      _ysh-should-run '= "foo42" => replace(/<capture d+ as num>/, ^["hi $[num] $PATH"])'
 | 
| 825 |      # -1 is replace all
 | 
| 826 |      _ysh-should-run '= "foo" => replace("o", "x", count=-1)'
 | 
| 827 |      _ysh-should-run '= "foo" => replace("o", "x", count=-2)'
 | 
| 828 |    fi
 | 
| 829 |    # Replace empty string?  Weird Python behavior
 | 
| 830 |    _ysh-should-run '= "foo" => replace("", "-")'
 | 
| 831 |    _ysh-should-run '= "foo" => replace("", "-", count=2)'
 | 
| 832 | 
 | 
| 833 |    # Use Expr with string
 | 
| 834 |    _ysh-should-run '= "foo" => replace("o", ^"-")'
 | 
| 835 |    # $0 is regular $0 here
 | 
| 836 |    _ysh-should-run '= "foo" => replace("o", ^"-$0")'
 | 
| 837 | 
 | 
| 838 |    # Hm $0 isn't set?
 | 
| 839 |    _ysh-should-run '= "foo" => replace(/[o]/, ^"-$0")'
 | 
| 840 |    # Here $1 is set
 | 
| 841 |    _ysh-should-run '= "foo" => replace(/<capture [o]>/, ^"-$1")'
 | 
| 842 |    _ysh-should-run '= "foo" => replace(/<capture [o] as letter>/, ^"-$letter")'
 | 
| 843 | 
 | 
| 844 |    # Invalid arguments
 | 
| 845 |    _ysh-expr-error '= "foo" => replace(42, "x")'
 | 
| 846 |    _ysh-expr-error '= "foo" => replace("x", 42)'
 | 
| 847 | 
 | 
| 848 |    # Invalid evaluation
 | 
| 849 |    _ysh-expr-error '= "foo" => replace("x", ^[42])'
 | 
| 850 | }
 | 
| 851 | 
 | 
| 852 | test-remainder() {
 | 
| 853 |   # second number can't be negative
 | 
| 854 |   _ysh-expr-error '= 5 % -3'
 | 
| 855 |   _ysh-expr-error 'var x = 5; setvar x %= -3'
 | 
| 856 | }
 | 
| 857 | 
 | 
| 858 | test-append-usage-error() {
 | 
| 859 |   _ysh-should-run 'append x ([])'
 | 
| 860 | 
 | 
| 861 |   _ysh-expr-error 'append'
 | 
| 862 | 
 | 
| 863 |   _ysh-expr-error 'append x'  # Too few
 | 
| 864 | 
 | 
| 865 |   _ysh-expr-error 'append x ([], [])'  # Too many
 | 
| 866 | }
 | 
| 867 | 
 | 
| 868 | # Bad error location
 | 
| 869 | test-try-usage-error() {
 | 
| 870 |   _ysh-expr-error '
 | 
| 871 | var s = "README"
 | 
| 872 | case (s) {
 | 
| 873 |   README { echo hi }
 | 
| 874 | }
 | 
| 875 | echo hi
 | 
| 876 | 
 | 
| 877 | try myproc
 | 
| 878 | if (_status !== 0) {
 | 
| 879 |   echo failed
 | 
| 880 | }
 | 
| 881 | '
 | 
| 882 | }
 | 
| 883 | 
 | 
| 884 | test-trim-utf8-error() {
 | 
| 885 |   _ysh-error-here-X 3 << 'EOF'
 | 
| 886 |   var badUtf = b'\yF9'
 | 
| 887 | 
 | 
| 888 |   # error is missed
 | 
| 889 |   call " a$[badUtf]b " => trim()
 | 
| 890 |   echo status=$_status
 | 
| 891 | 
 | 
| 892 |   # error is found
 | 
| 893 |   call "$[badUtf]b " => trim()
 | 
| 894 | EOF
 | 
| 895 | }
 | 
| 896 | 
 | 
| 897 | test-setglobal() {
 | 
| 898 |    _ysh-should-run '
 | 
| 899 | var a = [0]
 | 
| 900 | setglobal a[1-1] = 42
 | 
| 901 | pp line (a)
 | 
| 902 |    '
 | 
| 903 | 
 | 
| 904 |    _ysh-expr-error '
 | 
| 905 | var a = [0]
 | 
| 906 | setglobal a[a.bad] = 42
 | 
| 907 | pp line (a)
 | 
| 908 |    '
 | 
| 909 | 
 | 
| 910 |    _ysh-should-run '
 | 
| 911 | var d = {e:{f:0}}
 | 
| 912 | setglobal d.e.f = 42
 | 
| 913 | pp line (d)
 | 
| 914 | setglobal d.e.f += 1
 | 
| 915 | pp line (d)
 | 
| 916 |    '
 | 
| 917 | }
 | 
| 918 | 
 | 
| 919 | soil-run-py() {
 | 
| 920 |   run-test-funcs
 | 
| 921 | }
 | 
| 922 | 
 | 
| 923 | soil-run-cpp() {
 | 
| 924 |   local ysh=_bin/cxx-asan/ysh
 | 
| 925 |   ninja $ysh
 | 
| 926 |   YSH=$ysh run-test-funcs
 | 
| 927 | }
 | 
| 928 | 
 | 
| 929 | run-for-release() {
 | 
| 930 |   run-other-suite-for-release ysh-runtime-errors run-test-funcs
 | 
| 931 | }
 | 
| 932 | 
 | 
| 933 | "$@"
 |