OILS / test / sh-assert.sh View on Github | oilshell.org

166 lines, 113 significant
1# test/sh-assert.sh
2
3section-banner() {
4 echo
5 echo '///'
6 echo "/// $1"
7 echo '///'
8 echo
9}
10
11case-banner() {
12 echo
13 echo ===== CASE: "$@" =====
14 echo
15}
16
17_assert-sh-status() {
18 ### The most general assertion - EXIT on failure
19
20 local expected_status=$1
21 local sh=$2
22 local message=$3
23 shift 3
24
25 case-banner "$@"
26 echo
27
28 set +o errexit
29 $sh "$@"
30 local status=$?
31 set -o errexit
32
33 if test -z "${SH_ASSERT_DISABLE:-}"; then
34 if test "$status" != "$expected_status"; then
35 echo
36 die "$message: expected status $expected_status, got $status"
37 fi
38 fi
39}
40
41#
42# Assertions using _assert-sh-status
43#
44
45_osh-error-X() {
46 local expected_status=$1
47 shift
48
49 local message="Should FAIL under $OSH"
50 _assert-sh-status "$expected_status" "$OSH" "$message" \
51 -c "$@"
52}
53
54_osh-error-here-X() {
55 _osh-error-X $1 "$(cat)"
56}
57
58
59_osh-error-1() {
60 _osh-error-X 1 "$@"
61}
62
63_osh-error-2() {
64 _osh-error-X 2 "$@"
65}
66
67_ysh-error-X() {
68 local expected_status=$1
69 shift
70
71 local message=$0
72 _assert-sh-status $expected_status $YSH "$message" \
73 -c "$@"
74}
75
76_ysh-error-here-X() {
77 _ysh-error-X $1 "$(cat)"
78}
79
80_ysh-error-1() {
81 ### Expect status 1
82 _ysh-error-X 1 "$@"
83}
84
85_ysh-error-2() {
86 ### Expect status 2
87 _ysh-error-X 2 "$@"
88}
89
90_ysh-expr-error() {
91 ### Expect status 3
92 _ysh-error-X 3 "$@"
93}
94
95_osh-should-run() {
96 local message="Should run under $OSH"
97 _assert-sh-status 0 $OSH "$message" \
98 -c "$@"
99}
100
101_ysh-should-run() {
102 local message="Should run under $YSH"
103 _assert-sh-status 0 $YSH "$message" \
104 -c "$@"
105}
106
107#
108# Parse errors - pass -n -c, instead of -c
109#
110
111_osh-should-parse() {
112 local message="Should parse with $OSH"
113 _assert-sh-status 0 $OSH "$message" \
114 -n -c "$@"
115}
116
117_osh-parse-error() {
118 ### Pass a string that should NOT parse as $1
119 local message="Should NOT parse with $OSH"
120 _assert-sh-status 2 $OSH "$message" \
121 -n -c "$@"
122}
123
124_ysh-should-parse() {
125 local message="Should parse with $YSH"
126 _assert-sh-status 0 $YSH "$message" \
127 -n -c "$@"
128}
129
130_ysh-parse-error() {
131 local message="Should NOT parse with $YSH"
132 _assert-sh-status 2 $YSH "$message" \
133 -n -c "$@"
134}
135
136#
137# Here doc variants, so we can write single quoted strings in a
138# more readable way
139#
140
141_osh-should-parse-here() {
142 _osh-should-parse "$(cat)"
143}
144
145_osh-parse-error-here() {
146 _osh-parse-error "$(cat)"
147}
148
149_ysh-should-parse-here() {
150 _ysh-should-parse "$(cat)"
151}
152
153_ysh-parse-error-here() {
154 _ysh-parse-error "$(cat)"
155}
156
157_osh-should-run-here() {
158 _osh-should-run "$(cat)"
159}
160
161_ysh-should-run-here() {
162 _ysh-should-run "$(cat)"
163}
164
165
166