OILS / data_lang / j8-errors.sh View on Github | oilshell.org

218 lines, 74 significant
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
8source test/common.sh # $OSH, $YSH
9source 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
29test-line-numbers() {
30 # J8 string
31 _osh-error-here-X 1 << 'EOF'
32echo '
33{
34 "a": 99,
35 "foo\z": 42
36}
37' | json read
38EOF
39
40 # JSON
41 _osh-error-here-X 1 << 'EOF'
42echo '
43{
44 "foo": 42 oops
45}
46' | json read
47EOF
48
49 # J8 Lines
50 _ysh-error-here-X 4 << 'EOF'
51proc p {
52 echo unquoted
53 echo
54 echo
55 echo ' "hi" oops' # line 4 error
56}
57
58write -- @(p)
59EOF
60}
61
62test-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'
77echo '{}[ ' | json read
78EOF
79
80}
81
82test-ascii-control() {
83 # Disallowed ASCII control chars OUTSIDE string
84 _osh-error-here-X 1 << 'EOF'
85echo $'\x02' | json read
86EOF
87 # JSON
88 _ysh-error-here-X 1 << 'EOF'
89echo $'"foo \x01 "' | json read
90pp line (_reply)
91EOF
92 # J8
93 _ysh-error-here-X 1 << 'EOF'
94var invalid = b'\y01'
95echo $["u'foo" ++ invalid ++ "'"] | json8 read
96pp line (_reply)
97EOF
98}
99
100test-str-unclosed-quote() {
101 # JSON
102 _osh-error-here-X 1 << 'EOF'
103echo -n '["' | json read
104EOF
105 # J8
106 _osh-error-here-X 1 << 'EOF'
107echo -n "[b'" | json8 read
108EOF
109}
110
111test-str-bad-escape() {
112 # Invalid string escape JSON
113 _ysh-error-here-X 1 << 'EOF'
114echo '"hi \z bye"' | json read
115EOF
116 _ysh-error-here-X 1 << 'EOF'
117var invalid = r'\z'
118echo $["u'hi" ++ invalid ++ "bye'"] | json8 read
119EOF
120return
121}
122
123test-str-invalid-utf8() {
124 # JSON
125 _ysh-error-here-X 1 << 'EOF'
126# part of mu = \u03bc
127echo $' "\xce" ' | json read
128EOF
129 # J8
130 _ysh-error-here-X 1 << 'EOF'
131var invalid = b'\yce'
132echo $["u'" ++ invalid ++ "'"] | json8 read
133EOF
134}
135
136test-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
146test-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
154test-j8-lines() {
155 _ysh-should-run-here <<'EOF'
156write @(echo ' "json\tstring" '; echo; echo " b'j8' "; echo ' unquoted ';)
157EOF
158 #return
159
160 # quotes that don't match - in expression mode
161 _ysh-error-here-X 4 <<'EOF'
162var lines = @(
163 echo '"unbalanced'
164)
165pp line (lines)
166EOF
167
168 # error in word language
169 _ysh-error-here-X 4 <<'EOF'
170write @(echo '"unbalanced')
171EOF
172
173 # can't have two strings on a line
174 _ysh-error-here-X 4 <<'EOF'
175write @(echo '"json" "nope"')
176EOF
177
178 _ysh-error-here-X 4 <<'EOF'
179write @(echo '"json" unquoted')
180EOF
181
182 # syntax error inside quotes
183 _ysh-error-here-X 4 <<'EOF'
184write @(echo '"hello \z"')
185EOF
186
187 # unquoted line must be valid UTF-8
188 _ysh-error-here-X 4 <<'EOF'
189write @(echo $'foo \xff-bar spam')
190EOF
191
192 # unquoted line can't have ASCII control chars
193 _ysh-error-here-X 4 <<'EOF'
194write @(echo $'foo \x01-bar spam')
195EOF
196}
197
198
199
200#
201# Entry points
202#
203
204soil-run-py() {
205 run-test-funcs
206}
207
208soil-run-cpp() {
209 local osh=_bin/cxx-asan/osh
210 ninja $osh
211 OSH=$osh run-test-funcs
212}
213
214run-for-release() {
215 run-other-suite-for-release j8-errors run-test-funcs
216}
217
218"$@"