1 | # test/sh-assert.sh
|
2 |
|
3 | section-banner() {
|
4 | echo
|
5 | echo '///'
|
6 | echo "/// $1"
|
7 | echo '///'
|
8 | echo
|
9 | }
|
10 |
|
11 | case-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-1() {
|
55 | _osh-error-X 1 "$@"
|
56 | }
|
57 |
|
58 | _osh-error-2() {
|
59 | _osh-error-X 2 "$@"
|
60 | }
|
61 |
|
62 | _ysh-error-X() {
|
63 | local expected_status=$1
|
64 | shift
|
65 |
|
66 | local message=$0
|
67 | _assert-sh-status $expected_status $YSH "$message" \
|
68 | -c "$@"
|
69 | }
|
70 |
|
71 | _ysh-error-here-X() {
|
72 | _ysh-error-X $1 "$(cat)"
|
73 | }
|
74 |
|
75 | _ysh-error-1() {
|
76 | ### Expect status 1
|
77 | _ysh-error-X 1 "$@"
|
78 | }
|
79 |
|
80 | _ysh-error-2() {
|
81 | ### Expect status 2
|
82 | _ysh-error-X 2 "$@"
|
83 | }
|
84 |
|
85 | _ysh-expr-error() {
|
86 | ### Expect status 3
|
87 | _ysh-error-X 3 "$@"
|
88 | }
|
89 |
|
90 | _osh-should-run() {
|
91 | local message="Should run under $OSH"
|
92 | _assert-sh-status 0 $OSH "$message" \
|
93 | -c "$@"
|
94 | }
|
95 |
|
96 | _ysh-should-run() {
|
97 | local message="Should run under $YSH"
|
98 | _assert-sh-status 0 $YSH "$message" \
|
99 | -c "$@"
|
100 | }
|
101 |
|
102 | #
|
103 | # Parse errors - pass -n -c, instead of -c
|
104 | #
|
105 |
|
106 | _osh-should-parse() {
|
107 | local message="Should parse with $OSH"
|
108 | _assert-sh-status 0 $OSH "$message" \
|
109 | -n -c "$@"
|
110 | }
|
111 |
|
112 | _osh-parse-error() {
|
113 | ### Pass a string that should NOT parse as $1
|
114 | local message="Should NOT parse with $OSH"
|
115 | _assert-sh-status 2 $OSH "$message" \
|
116 | -n -c "$@"
|
117 | }
|
118 |
|
119 | _ysh-should-parse() {
|
120 | local message="Should parse with $YSH"
|
121 | _assert-sh-status 0 $YSH "$message" \
|
122 | -n -c "$@"
|
123 | }
|
124 |
|
125 | _ysh-parse-error() {
|
126 | local message="Should NOT parse with $YSH"
|
127 | _assert-sh-status 2 $YSH "$message" \
|
128 | -n -c "$@"
|
129 | }
|
130 |
|
131 | #
|
132 | # Here doc variants of parse errors, so we can write single quoted strings in a
|
133 | # more readable way
|
134 | #
|
135 |
|
136 | _osh-should-parse-here() {
|
137 | _osh-should-parse "$(cat)"
|
138 | }
|
139 |
|
140 | _osh-parse-error-here() {
|
141 | _osh-parse-error "$(cat)"
|
142 | }
|
143 |
|
144 | _ysh-should-parse-here() {
|
145 | _ysh-should-parse "$(cat)"
|
146 | }
|
147 |
|
148 | _ysh-parse-error-here() {
|
149 | _ysh-parse-error "$(cat)"
|
150 | }
|
151 |
|