| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # Usage:
|
| 4 | # data_lang/j8-errors.sh <function name>
|
| 5 |
|
| 6 | # NOTE: No set -o errexit, etc.
|
| 7 |
|
| 8 | source test/common.sh # $OSH, $YSH
|
| 9 | source test/sh-assert.sh # banner, _assert-sh-status
|
| 10 |
|
| 11 | _error-case-X() {
|
| 12 | local expected_status=$1
|
| 13 | shift
|
| 14 |
|
| 15 | local message=$0
|
| 16 | _assert-sh-status $expected_status $OSH "$message" \
|
| 17 | -c "$@"
|
| 18 | }
|
| 19 |
|
| 20 | _expr-error-case() {
|
| 21 | ### Expect status 3
|
| 22 | _error-case-X 3 "$@"
|
| 23 | }
|
| 24 |
|
| 25 | #
|
| 26 | # Cases
|
| 27 | #
|
| 28 |
|
| 29 | test-line-numbers() {
|
| 30 | # J8 string
|
| 31 | _osh-error-here-X 1 << 'EOF'
|
| 32 | echo '
|
| 33 | {
|
| 34 | "a": 99,
|
| 35 | "foo\z": 42
|
| 36 | }
|
| 37 | ' | json read
|
| 38 | EOF
|
| 39 |
|
| 40 | # JSON
|
| 41 | _osh-error-here-X 1 << 'EOF'
|
| 42 | echo '
|
| 43 | {
|
| 44 | "foo": 42 oops
|
| 45 | }
|
| 46 | ' | json read
|
| 47 | EOF
|
| 48 |
|
| 49 | # J8 Lines
|
| 50 | _ysh-error-here-X 4 << 'EOF'
|
| 51 | proc p {
|
| 52 | echo unquoted
|
| 53 | echo
|
| 54 | echo
|
| 55 | echo ' "hi" oops' # line 4 error
|
| 56 | }
|
| 57 |
|
| 58 | write -- @(p)
|
| 59 | EOF
|
| 60 | }
|
| 61 |
|
| 62 | test-parse-errors() {
|
| 63 | # Unexpected EOF
|
| 64 | _error-case-X 1 'echo "" | json read'
|
| 65 |
|
| 66 | # Unexpected token
|
| 67 | _error-case-X 1 'echo { | json read'
|
| 68 |
|
| 69 | # Invalid token
|
| 70 | _error-case-X 1 'echo + | json read'
|
| 71 |
|
| 72 | # NIL8 token, not JSON8 token
|
| 73 | _error-case-X 1 'echo "(" | json read'
|
| 74 |
|
| 75 | # Extra input after valid message
|
| 76 | _ysh-error-here-X 1 << 'EOF'
|
| 77 | echo '{}[ ' | json read
|
| 78 | EOF
|
| 79 |
|
| 80 | }
|
| 81 |
|
| 82 | test-ascii-control() {
|
| 83 | # Disallowed ASCII control chars OUTSIDE string
|
| 84 | _osh-error-here-X 1 << 'EOF'
|
| 85 | echo $'\x02' | json read
|
| 86 | EOF
|
| 87 | # JSON
|
| 88 | _ysh-error-here-X 1 << 'EOF'
|
| 89 | echo $'"foo \x01 "' | json read
|
| 90 | pp line (_reply)
|
| 91 | EOF
|
| 92 | # J8
|
| 93 | _ysh-error-here-X 1 << 'EOF'
|
| 94 | var invalid = b'\y01'
|
| 95 | echo $["u'foo" ++ invalid ++ "'"] | json8 read
|
| 96 | pp line (_reply)
|
| 97 | EOF
|
| 98 | }
|
| 99 |
|
| 100 | test-str-unclosed-quote() {
|
| 101 | # JSON
|
| 102 | _osh-error-here-X 1 << 'EOF'
|
| 103 | echo -n '["' | json read
|
| 104 | EOF
|
| 105 | # J8
|
| 106 | _osh-error-here-X 1 << 'EOF'
|
| 107 | echo -n "[b'" | json8 read
|
| 108 | EOF
|
| 109 | }
|
| 110 |
|
| 111 | test-str-bad-escape() {
|
| 112 | # Invalid string escape JSON
|
| 113 | _ysh-error-here-X 1 << 'EOF'
|
| 114 | echo '"hi \z bye"' | json read
|
| 115 | EOF
|
| 116 | _ysh-error-here-X 1 << 'EOF'
|
| 117 | var invalid = r'\z'
|
| 118 | echo $["u'hi" ++ invalid ++ "bye'"] | json8 read
|
| 119 | EOF
|
| 120 | return
|
| 121 | }
|
| 122 |
|
| 123 | test-str-invalid-utf8() {
|
| 124 | # JSON
|
| 125 | _ysh-error-here-X 1 << 'EOF'
|
| 126 | # part of mu = \u03bc
|
| 127 | echo $' "\xce" ' | json read
|
| 128 | EOF
|
| 129 | # J8
|
| 130 | _ysh-error-here-X 1 << 'EOF'
|
| 131 | var invalid = b'\yce'
|
| 132 | echo $["u'" ++ invalid ++ "'"] | json8 read
|
| 133 | EOF
|
| 134 | }
|
| 135 |
|
| 136 | test-encode() {
|
| 137 | _error-case-X 1 'var d = {}; setvar d.k = d; json write (d)'
|
| 138 |
|
| 139 | _error-case-X 1 'var L = []; call L->append(L); json write (L)'
|
| 140 |
|
| 141 | # This should fail!
|
| 142 | # But not pp line (L)
|
| 143 | _error-case-X 1 'var L = []; call L->append(/d+/); j8 write (L)'
|
| 144 | }
|
| 145 |
|
| 146 | test-cpython() {
|
| 147 | # control char is error in Python
|
| 148 | echo $'"foo \x01 "' \
|
| 149 | | python3 -c 'import json, sys; json.loads(sys.stdin.read())' || true
|
| 150 | echo $' \x01' \
|
| 151 | | python3 -c 'import json, sys; json.loads(sys.stdin.read())' || true
|
| 152 | }
|
| 153 |
|
| 154 | test-j8-lines() {
|
| 155 | _ysh-should-run-here <<'EOF'
|
| 156 | write @(echo ' "json\tstring" '; echo; echo " b'j8' "; echo ' unquoted ';)
|
| 157 | EOF
|
| 158 | #return
|
| 159 |
|
| 160 | # quotes that don't match - in expression mode
|
| 161 | _ysh-error-here-X 4 <<'EOF'
|
| 162 | var lines = @(
|
| 163 | echo '"unbalanced'
|
| 164 | )
|
| 165 | pp line (lines)
|
| 166 | EOF
|
| 167 |
|
| 168 | # error in word language
|
| 169 | _ysh-error-here-X 4 <<'EOF'
|
| 170 | write @(echo '"unbalanced')
|
| 171 | EOF
|
| 172 |
|
| 173 | # can't have two strings on a line
|
| 174 | _ysh-error-here-X 4 <<'EOF'
|
| 175 | write @(echo '"json" "nope"')
|
| 176 | EOF
|
| 177 |
|
| 178 | _ysh-error-here-X 4 <<'EOF'
|
| 179 | write @(echo '"json" unquoted')
|
| 180 | EOF
|
| 181 |
|
| 182 | # syntax error inside quotes
|
| 183 | _ysh-error-here-X 4 <<'EOF'
|
| 184 | write @(echo '"hello \z"')
|
| 185 | EOF
|
| 186 |
|
| 187 | # unquoted line must be valid UTF-8
|
| 188 | _ysh-error-here-X 4 <<'EOF'
|
| 189 | write @(echo $'foo \xff-bar spam')
|
| 190 | EOF
|
| 191 |
|
| 192 | # unquoted line can't have ASCII control chars
|
| 193 | _ysh-error-here-X 4 <<'EOF'
|
| 194 | write @(echo $'foo \x01-bar spam')
|
| 195 | EOF
|
| 196 | }
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 | #
|
| 201 | # Entry points
|
| 202 | #
|
| 203 |
|
| 204 | soil-run-py() {
|
| 205 | run-test-funcs
|
| 206 | }
|
| 207 |
|
| 208 | soil-run-cpp() {
|
| 209 | local osh=_bin/cxx-asan/osh
|
| 210 | ninja $osh
|
| 211 | OSH=$osh run-test-funcs
|
| 212 | }
|
| 213 |
|
| 214 | run-for-release() {
|
| 215 | run-other-suite-for-release j8-errors run-test-funcs
|
| 216 | }
|
| 217 |
|
| 218 | "$@"
|