|
10 | 10 | ## Function list based on:
|
11 | 11 | ## http://junit.sourceforge.net/javadoc/org/junit/Assert.html
|
12 | 12 | ## Log methods : inspired by
|
13 |
| -## - https://natelandau.com/bash-scripting-utilities/ |
| 13 | +## - https://natelandau.com/bash-scripting-utilities/ |
14 | 14 | ## author: Mark Torok
|
15 | 15 | ##
|
16 | 16 | ## date: 07. Dec. 2016
|
|
22 | 22 | # Note: from https://github.com/torokmark/assert.sh/blob/main/assert.sh.
|
23 | 23 |
|
24 | 24 | if command -v tput &>/dev/null && tty -s; then
|
25 |
| - RED=$(tput setaf 1) |
26 |
| - GREEN=$(tput setaf 2) |
27 |
| - MAGENTA=$(tput setaf 5) |
28 |
| - NORMAL=$(tput sgr0) |
29 |
| - BOLD=$(tput bold) |
| 25 | + RED=$(tput setaf 1) |
| 26 | + GREEN=$(tput setaf 2) |
| 27 | + MAGENTA=$(tput setaf 5) |
| 28 | + NORMAL=$(tput sgr0) |
| 29 | + BOLD=$(tput bold) |
30 | 30 | else
|
31 |
| - RED=$(echo -en "\e[31m") |
32 |
| - GREEN=$(echo -en "\e[32m") |
33 |
| - MAGENTA=$(echo -en "\e[35m") |
34 |
| - NORMAL=$(echo -en "\e[00m") |
35 |
| - BOLD=$(echo -en "\e[01m") |
| 31 | + RED=$(echo -en "\e[31m") |
| 32 | + GREEN=$(echo -en "\e[32m") |
| 33 | + MAGENTA=$(echo -en "\e[35m") |
| 34 | + NORMAL=$(echo -en "\e[00m") |
| 35 | + BOLD=$(echo -en "\e[01m") |
36 | 36 | fi
|
37 | 37 |
|
38 | 38 | log_header() {
|
39 |
| - printf "\n${BOLD}${MAGENTA}========== %s ==========${NORMAL}\n" "$@" >&2 |
| 39 | + printf "\n${BOLD}${MAGENTA}========== %s ==========${NORMAL}\n" "$@" >&2 |
40 | 40 | }
|
41 | 41 |
|
42 | 42 | log_success() {
|
43 |
| - printf "${GREEN}✔ %s${NORMAL}\n" "$@" >&2 |
| 43 | + printf "${GREEN}✔ %s${NORMAL}\n" "$@" >&2 |
44 | 44 | }
|
45 | 45 |
|
46 | 46 | log_failure() {
|
47 |
| - printf "${RED}✖ %s${NORMAL}\n" "$@" >&2 |
| 47 | + printf "${RED}✖ %s${NORMAL}\n" "$@" >&2 |
48 | 48 | }
|
49 | 49 |
|
50 |
| - |
51 | 50 | assert_eq() {
|
52 |
| - local expected="$1" |
53 |
| - local actual="$2" |
54 |
| - local msg="${3-}" |
55 |
| - |
56 |
| - if [ "$expected" == "$actual" ]; then |
57 |
| - return 0 |
58 |
| - else |
59 |
| - [ "${#msg}" -gt 0 ] && log_failure "$expected == $actual :: $msg" || true |
60 |
| - return 1 |
61 |
| - fi |
| 51 | + local expected="$1" |
| 52 | + local actual="$2" |
| 53 | + local msg="${3-}" |
| 54 | + |
| 55 | + if [ "$expected" == "$actual" ]; then |
| 56 | + return 0 |
| 57 | + else |
| 58 | + ([ "${#msg}" -gt 0 ] && log_failure "$expected == $actual :: $msg") || true |
| 59 | + return 1 |
| 60 | + fi |
62 | 61 | }
|
63 | 62 |
|
64 | 63 | assert_not_eq() {
|
65 |
| - local expected="$1" |
66 |
| - local actual="$2" |
67 |
| - local msg="${3-}" |
68 |
| - |
69 |
| - if [ ! "$expected" == "$actual" ]; then |
70 |
| - return 0 |
71 |
| - else |
72 |
| - [ "${#msg}" -gt 0 ] && log_failure "$expected != $actual :: $msg" || true |
73 |
| - return 1 |
74 |
| - fi |
| 64 | + local expected="$1" |
| 65 | + local actual="$2" |
| 66 | + local msg="${3-}" |
| 67 | + |
| 68 | + if [ ! "$expected" == "$actual" ]; then |
| 69 | + return 0 |
| 70 | + else |
| 71 | + ([ "${#msg}" -gt 0 ] && log_failure "$expected != $actual :: $msg") || true |
| 72 | + return 1 |
| 73 | + fi |
75 | 74 | }
|
76 | 75 |
|
77 | 76 | assert_true() {
|
78 |
| - local actual="$1" |
79 |
| - local msg="${2-}" |
| 77 | + local actual="$1" |
| 78 | + local msg="${2-}" |
80 | 79 |
|
81 |
| - assert_eq true "$actual" "$msg" |
82 |
| - return "$?" |
| 80 | + assert_eq true "$actual" "$msg" |
| 81 | + return "$?" |
83 | 82 | }
|
84 | 83 |
|
85 | 84 | assert_false() {
|
86 |
| - local actual="$1" |
87 |
| - local msg="${2-}" |
| 85 | + local actual="$1" |
| 86 | + local msg="${2-}" |
88 | 87 |
|
89 |
| - assert_eq false "$actual" "$msg" |
90 |
| - return "$?" |
| 88 | + assert_eq false "$actual" "$msg" |
| 89 | + return "$?" |
91 | 90 | }
|
92 | 91 |
|
93 | 92 | assert_array_eq() {
|
| 93 | + # There is a bug in a shellcheck type check that bleeds across function |
| 94 | + # scope so these variables need to have a different name. |
| 95 | + # See: https://github.com/koalaman/shellcheck/issues/1225 |
| 96 | + declare -a expecteda=("${!1-}") |
| 97 | + declare -a actuala=("${!2}") |
94 | 98 |
|
95 |
| - declare -a expected=("${!1-}") |
96 |
| - # echo "AAE ${expected[@]}" |
97 |
| - |
98 |
| - declare -a actual=("${!2}") |
99 |
| - # echo "AAE ${actual[@]}" |
| 99 | + local msg="${3-}" |
100 | 100 |
|
101 |
| - local msg="${3-}" |
| 101 | + local return_code=0 |
| 102 | + if [ ! "${#expecteda[@]}" == "${#actuala[@]}" ]; then |
| 103 | + return_code=1 |
| 104 | + fi |
102 | 105 |
|
103 |
| - local return_code=0 |
104 |
| - if [ ! "${#expected[@]}" == "${#actual[@]}" ]; then |
105 |
| - return_code=1 |
106 |
| - fi |
| 106 | + local i |
| 107 | + for ((i = 1; i < ${#expecteda[@]} + 1; i += 1)); do |
| 108 | + if [ ! "${expecteda[$i - 1]}" == "${actuala[$i - 1]}" ]; then |
| 109 | + return_code=1 |
| 110 | + break |
| 111 | + fi |
| 112 | + done |
107 | 113 |
|
108 |
| - local i |
109 |
| - for (( i=1; i < ${#expected[@]} + 1; i+=1 )); do |
110 |
| - if [ ! "${expected[$i-1]}" == "${actual[$i-1]}" ]; then |
111 |
| - return_code=1 |
112 |
| - break |
| 114 | + if [ "$return_code" == 1 ]; then |
| 115 | + ([ "${#msg}" -gt 0 ] && log_failure "(${expecteda[*]}) != (${actuala[*]}) :: $msg") || true |
113 | 116 | fi
|
114 |
| - done |
115 |
| - |
116 |
| - if [ "$return_code" == 1 ]; then |
117 |
| - [ "${#msg}" -gt 0 ] && log_failure "(${expected[*]}) != (${actual[*]}) :: $msg" || true |
118 |
| - fi |
119 | 117 |
|
120 |
| - return "$return_code" |
| 118 | + return "$return_code" |
121 | 119 | }
|
122 | 120 |
|
123 | 121 | assert_array_not_eq() {
|
| 122 | + # There is a bug in a shellcheck type check that bleeds across function |
| 123 | + # scope so these variables need to have a different name. |
| 124 | + # See: https://github.com/koalaman/shellcheck/issues/1225 |
| 125 | + declare -a expecteda=("${!1-}") |
| 126 | + declare -a actuala=("${!2}") |
124 | 127 |
|
125 |
| - declare -a expected=("${!1-}") |
126 |
| - declare -a actual=("${!2}") |
| 128 | + local msg="${3-}" |
127 | 129 |
|
128 |
| - local msg="${3-}" |
| 130 | + local return_code=1 |
| 131 | + if [ ! "${#expecteda[@]}" == "${#actuala[@]}" ]; then |
| 132 | + return_code=0 |
| 133 | + fi |
129 | 134 |
|
130 |
| - local return_code=1 |
131 |
| - if [ ! "${#expected[@]}" == "${#actual[@]}" ]; then |
132 |
| - return_code=0 |
133 |
| - fi |
| 135 | + local i |
| 136 | + for ((i = 1; i < ${#expecteda[@]} + 1; i += 1)); do |
| 137 | + if [ ! "${expecteda[$i - 1]}" == "${actuala[$i - 1]}" ]; then |
| 138 | + return_code=0 |
| 139 | + break |
| 140 | + fi |
| 141 | + done |
134 | 142 |
|
135 |
| - local i |
136 |
| - for (( i=1; i < ${#expected[@]} + 1; i+=1 )); do |
137 |
| - if [ ! "${expected[$i-1]}" == "${actual[$i-1]}" ]; then |
138 |
| - return_code=0 |
139 |
| - break |
| 143 | + if [ "$return_code" == 1 ]; then |
| 144 | + ([ "${#msg}" -gt 0 ] && log_failure "(${expecteda[*]}) == (${actuala[*]}) :: $msg") || true |
140 | 145 | fi
|
141 |
| - done |
142 | 146 |
|
143 |
| - if [ "$return_code" == 1 ]; then |
144 |
| - [ "${#msg}" -gt 0 ] && log_failure "(${expected[*]}) == (${actual[*]}) :: $msg" || true |
145 |
| - fi |
146 |
| - |
147 |
| - return "$return_code" |
| 147 | + return "$return_code" |
148 | 148 | }
|
149 | 149 |
|
150 | 150 | assert_empty() {
|
151 |
| - local actual=$1 |
152 |
| - local msg="${2-}" |
| 151 | + local actual=$1 |
| 152 | + local msg="${2-}" |
153 | 153 |
|
154 |
| - assert_eq "" "$actual" "$msg" |
155 |
| - return "$?" |
| 154 | + assert_eq "" "$actual" "$msg" |
| 155 | + return "$?" |
156 | 156 | }
|
157 | 157 |
|
158 | 158 | assert_not_empty() {
|
159 |
| - local actual=$1 |
160 |
| - local msg="${2-}" |
| 159 | + local actual=$1 |
| 160 | + local msg="${2-}" |
161 | 161 |
|
162 |
| - assert_not_eq "" "$actual" "$msg" |
163 |
| - return "$?" |
| 162 | + assert_not_eq "" "$actual" "$msg" |
| 163 | + return "$?" |
164 | 164 | }
|
165 | 165 |
|
166 | 166 | assert_contain() {
|
167 |
| - local haystack="$1" |
168 |
| - local needle="${2-}" |
169 |
| - local msg="${3-}" |
| 167 | + local haystack="$1" |
| 168 | + local needle="${2-}" |
| 169 | + local msg="${3-}" |
170 | 170 |
|
171 |
| - if [ -z "${needle:+x}" ]; then |
172 |
| - return 0; |
173 |
| - fi |
| 171 | + if [ -z "${needle:+x}" ]; then |
| 172 | + return 0 |
| 173 | + fi |
174 | 174 |
|
175 |
| - if [ -z "${haystack##*$needle*}" ]; then |
176 |
| - return 0 |
177 |
| - else |
178 |
| - [ "${#msg}" -gt 0 ] && log_failure "$haystack doesn't contain $needle :: $msg" || true |
179 |
| - return 1 |
180 |
| - fi |
| 175 | + # needle is used as a search pattern. |
| 176 | + # shellcheck disable=SC2295 |
| 177 | + if [ -z "${haystack##*$needle*}" ]; then |
| 178 | + return 0 |
| 179 | + else |
| 180 | + ([ "${#msg}" -gt 0 ] && log_failure "$haystack doesn't contain $needle :: $msg") || true |
| 181 | + return 1 |
| 182 | + fi |
181 | 183 | }
|
182 | 184 |
|
183 | 185 | assert_not_contain() {
|
184 |
| - local haystack="$1" |
185 |
| - local needle="${2-}" |
186 |
| - local msg="${3-}" |
| 186 | + local haystack="$1" |
| 187 | + local needle="${2-}" |
| 188 | + local msg="${3-}" |
187 | 189 |
|
188 |
| - if [ -z "${needle:+x}" ]; then |
189 |
| - return 0; |
190 |
| - fi |
| 190 | + if [ -z "${needle:+x}" ]; then |
| 191 | + return 0 |
| 192 | + fi |
191 | 193 |
|
192 |
| - if [ "${haystack##*$needle*}" ]; then |
193 |
| - return 0 |
194 |
| - else |
195 |
| - [ "${#msg}" -gt 0 ] && log_failure "$haystack contains $needle :: $msg" || true |
196 |
| - return 1 |
197 |
| - fi |
| 194 | + # needle is used as a search pattern. |
| 195 | + # shellcheck disable=SC2295 |
| 196 | + if [ "${haystack##*$needle*}" ]; then |
| 197 | + return 0 |
| 198 | + else |
| 199 | + ([ "${#msg}" -gt 0 ] && log_failure "$haystack contains $needle :: $msg") || true |
| 200 | + return 1 |
| 201 | + fi |
198 | 202 | }
|
199 | 203 |
|
200 | 204 | assert_gt() {
|
201 |
| - local first="$1" |
202 |
| - local second="$2" |
203 |
| - local msg="${3-}" |
204 |
| - |
205 |
| - if [[ "$first" -gt "$second" ]]; then |
206 |
| - return 0 |
207 |
| - else |
208 |
| - [ "${#msg}" -gt 0 ] && log_failure "$first > $second :: $msg" || true |
209 |
| - return 1 |
210 |
| - fi |
| 205 | + local first="$1" |
| 206 | + local second="$2" |
| 207 | + local msg="${3-}" |
| 208 | + |
| 209 | + if [[ "$first" -gt "$second" ]]; then |
| 210 | + return 0 |
| 211 | + else |
| 212 | + ([ "${#msg}" -gt 0 ] && log_failure "$first > $second :: $msg") || true |
| 213 | + return 1 |
| 214 | + fi |
211 | 215 | }
|
212 | 216 |
|
213 | 217 | assert_ge() {
|
214 |
| - local first="$1" |
215 |
| - local second="$2" |
216 |
| - local msg="${3-}" |
217 |
| - |
218 |
| - if [[ "$first" -ge "$second" ]]; then |
219 |
| - return 0 |
220 |
| - else |
221 |
| - [ "${#msg}" -gt 0 ] && log_failure "$first >= $second :: $msg" || true |
222 |
| - return 1 |
223 |
| - fi |
| 218 | + local first="$1" |
| 219 | + local second="$2" |
| 220 | + local msg="${3-}" |
| 221 | + |
| 222 | + if [[ "$first" -ge "$second" ]]; then |
| 223 | + return 0 |
| 224 | + else |
| 225 | + ([ "${#msg}" -gt 0 ] && log_failure "$first >= $second :: $msg") || true |
| 226 | + return 1 |
| 227 | + fi |
224 | 228 | }
|
225 | 229 |
|
226 | 230 | assert_lt() {
|
227 |
| - local first="$1" |
228 |
| - local second="$2" |
229 |
| - local msg="${3-}" |
230 |
| - |
231 |
| - if [[ "$first" -lt "$second" ]]; then |
232 |
| - return 0 |
233 |
| - else |
234 |
| - [ "${#msg}" -gt 0 ] && log_failure "$first < $second :: $msg" || true |
235 |
| - return 1 |
236 |
| - fi |
| 231 | + local first="$1" |
| 232 | + local second="$2" |
| 233 | + local msg="${3-}" |
| 234 | + |
| 235 | + if [[ "$first" -lt "$second" ]]; then |
| 236 | + return 0 |
| 237 | + else |
| 238 | + ([ "${#msg}" -gt 0 ] && log_failure "$first < $second :: $msg") || true |
| 239 | + return 1 |
| 240 | + fi |
237 | 241 | }
|
238 | 242 |
|
239 | 243 | assert_le() {
|
240 |
| - local first="$1" |
241 |
| - local second="$2" |
242 |
| - local msg="${3-}" |
243 |
| - |
244 |
| - if [[ "$first" -le "$second" ]]; then |
245 |
| - return 0 |
246 |
| - else |
247 |
| - [ "${#msg}" -gt 0 ] && log_failure "$first <= $second :: $msg" || true |
248 |
| - return 1 |
249 |
| - fi |
| 244 | + local first="$1" |
| 245 | + local second="$2" |
| 246 | + local msg="${3-}" |
| 247 | + |
| 248 | + if [[ "$first" -le "$second" ]]; then |
| 249 | + return 0 |
| 250 | + else |
| 251 | + ([ "${#msg}" -gt 0 ] && log_failure "$first <= $second :: $msg") || true |
| 252 | + return 1 |
| 253 | + fi |
250 | 254 | }
|
0 commit comments