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

92 lines, 42 significant
1#!/usr/bin/env bash
2#
3# Usage:
4# data_lang/json-errors.sh <function name>
5
6# NOTE: No set -o errexit, etc.
7
8source test/common.sh # $OSH
9source test/sh-assert.sh # banner, _assert-sh-status
10
11# Test JSON in OSH. Should be the same in YSH.
12#OSH=${OSH:-bin/osh}
13
14_error-case-X() {
15 local expected_status=$1
16 shift
17
18 local message=$0
19 _assert-sh-status $expected_status $OSH "$message" \
20 -c "$@"
21}
22
23_expr-error-case() {
24 ### Expect status 3
25 _error-case-X 3 "$@"
26}
27
28#
29# Cases
30#
31
32test-parse-errors() {
33 #echo OSH=$OSH
34 #set +o errexit
35
36 # Unexpected EOF
37 _error-case-X 1 'echo "" | json read'
38
39 # Unexpected token
40 _error-case-X 1 'echo { | json read'
41
42 # Invalid token
43 _error-case-X 1 'echo + | json read'
44
45 # TYG8 token, not JSON8 token
46 _error-case-X 1 'echo "(" | json read'
47}
48
49test-lex-errors() {
50 # Unclosed quote
51 _error-case-X 1 'echo [\" | json read'
52
53 # EOL in middle of string
54 _error-case-X 1 'echo -n [\" | json read'
55
56 # Invalid unicode
57
58 json=$'"\xce"' # part of mu = \u03bc
59 echo "json=$json"
60 json=${json//'"'/'\"'} # shell escape
61 _error-case-X 1 $'echo -n '$json' | json read'
62}
63
64test-encode() {
65 _error-case-X 1 'var d = {}; setvar d.k = d; json write (d)'
66
67 _error-case-X 1 'var L = []; call L->append(L); json write (L)'
68
69 # This should fail!
70 # But not pp line (L)
71 _error-case-X 1 'var L = []; call L->append(/d+/); j8 write (L)'
72}
73
74#
75# Entry points
76#
77
78soil-run-py() {
79 run-test-funcs
80}
81
82soil-run-cpp() {
83 local osh=_bin/cxx-asan/osh
84 ninja $osh
85 OSH=$osh run-test-funcs
86}
87
88run-for-release() {
89 run-other-suite-for-release json-errors run-test-funcs
90}
91
92"$@"