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