| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 1 | #!/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 | |
| 22 | if 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) |
| 28 | else |
| 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") |
| 34 | fi |
| 35 | |
| 36 | log_header() { |
| 37 | printf "\n${BOLD}${MAGENTA}========== %s ==========${NORMAL}\n" "$@" >&2 |
| 38 | } |
| 39 | |
| 40 | log_success() { |
| 41 | printf "${GREEN}✔ %s${NORMAL}\n" "$@" >&2 |
| 42 | } |
| 43 | |
| 44 | log_failure() { |
| 45 | printf "${RED}✖ %s${NORMAL}\n" "$@" >&2 |
| 46 | } |
| 47 | |
| 48 | |
| 49 | assert_eq() { |
| 50 | local expected="$1" |
| 51 | local actual="$2" |
| 52 | local msg="${3-}" |
| 53 | |
| 54 | if [ "$expected" == "$actual" ]; then |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 55 | [ "${#msg}" -gt 0 ] && log_success "$msg" || true |
| 56 | ((PASSED++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 57 | return 0 |
| 58 | else |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 59 | [ "${#msg}" -gt 0 ] && log_failure "$expected != $actual :: $msg is not the case" || true |
| 60 | ((ERRORS++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 61 | return 1 |
| 62 | fi |
| 63 | } |
| 64 | |
| 65 | assert_not_eq() { |
| 66 | local expected="$1" |
| 67 | local actual="$2" |
| 68 | local msg="${3-}" |
| 69 | |
| 70 | if [ ! "$expected" == "$actual" ]; then |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 71 | [ "${#msg}" -gt 0 ] && log_success "$msg" || true |
| 72 | ((PASSED++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 73 | return 0 |
| 74 | else |
| 75 | [ "${#msg}" -gt 0 ] && log_failure "$expected != $actual :: $msg" || true |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 76 | ((ERRORS++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 77 | return 1 |
| 78 | fi |
| 79 | } |
| 80 | |
| 81 | assert_true() { |
| 82 | local actual="$1" |
| 83 | local msg="${3-}" |
| 84 | |
| 85 | assert_eq true "$actual" "$msg" |
| 86 | return "$?" |
| 87 | } |
| 88 | |
| 89 | assert_false() { |
| 90 | local actual="$1" |
| 91 | local msg="${3-}" |
| 92 | |
| 93 | assert_eq false "$actual" "$msg" |
| 94 | return "$?" |
| 95 | } |
| 96 | |
| 97 | assert_array_eq() { |
| 98 | |
| 99 | declare -a expected=("${!1-}") |
| 100 | # echo "AAE ${expected[@]}" |
| 101 | |
| 102 | declare -a actual=("${!2}") |
| 103 | # echo "AAE ${actual[@]}" |
| 104 | |
| 105 | local msg="${3-}" |
| 106 | |
| 107 | local return_code=0 |
| 108 | if [ ! "${#expected[@]}" == "${#actual[@]}" ]; then |
| 109 | return_code=1 |
| 110 | fi |
| 111 | |
| 112 | local i |
| 113 | for (( i=1; i < ${#expected[@]} + 1; i+=1 )); do |
| 114 | if [ ! "${expected[$i-1]}" == "${actual[$i-1]}" ]; then |
| 115 | return_code=1 |
| 116 | break |
| 117 | fi |
| 118 | done |
| 119 | |
| 120 | if [ "$return_code" == 1 ]; then |
| 121 | [ "${#msg}" -gt 0 ] && log_failure "(${expected[*]}) != (${actual[*]}) :: $msg" || true |
| 122 | fi |
| 123 | |
| 124 | return "$return_code" |
| 125 | } |
| 126 | |
| 127 | assert_array_not_eq() { |
| 128 | |
| 129 | declare -a expected=("${!1-}") |
| 130 | declare -a actual=("${!2}") |
| 131 | |
| 132 | local msg="${3-}" |
| 133 | |
| 134 | local return_code=1 |
| 135 | if [ ! "${#expected[@]}" == "${#actual[@]}" ]; then |
| 136 | return_code=0 |
| 137 | fi |
| 138 | |
| 139 | local i |
| 140 | for (( i=1; i < ${#expected[@]} + 1; i+=1 )); do |
| 141 | if [ ! "${expected[$i-1]}" == "${actual[$i-1]}" ]; then |
| 142 | return_code=0 |
| 143 | break |
| 144 | fi |
| 145 | done |
| 146 | |
| 147 | if [ "$return_code" == 1 ]; then |
| 148 | [ "${#msg}" -gt 0 ] && log_failure "(${expected[*]}) == (${actual[*]}) :: $msg" || true |
| 149 | fi |
| 150 | |
| 151 | return "$return_code" |
| 152 | } |
| 153 | |
| 154 | assert_empty() { |
| 155 | local actual=$1 |
| 156 | local msg="${2-}" |
| 157 | |
| 158 | assert_eq "" "$actual" "$msg" |
| 159 | return "$?" |
| 160 | } |
| 161 | |
| 162 | assert_not_empty() { |
| 163 | local actual=$1 |
| 164 | local msg="${2-}" |
| 165 | |
| 166 | assert_not_eq "" "$actual" "$msg" |
| 167 | return "$?" |
| 168 | } |
| 169 | |
| 170 | assert_contain() { |
| 171 | local haystack="$1" |
| 172 | local needle="${2-}" |
| 173 | local msg="${3-}" |
| 174 | |
| 175 | if [ -z "${needle:+x}" ]; then |
| 176 | return 0; |
| 177 | fi |
| 178 | |
| 179 | if [ -z "${haystack##*$needle*}" ]; then |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 180 | ((PASSED++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 181 | return 0 |
| 182 | else |
| 183 | [ "${#msg}" -gt 0 ] && log_failure "$haystack doesn't contain $needle :: $msg" || true |
| 184 | return 1 |
| 185 | fi |
| 186 | } |
| 187 | |
| 188 | assert_not_contain() { |
| 189 | local haystack="$1" |
| 190 | local needle="${2-}" |
| 191 | local msg="${3-}" |
| 192 | |
| 193 | if [ -z "${needle:+x}" ]; then |
| 194 | return 0; |
| 195 | fi |
| 196 | |
| 197 | if [ "${haystack##*$needle*}" ]; then |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 198 | [ "${#msg}" -gt 0 ] && log_success "$msg" || true |
| 199 | ((PASSED++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 200 | return 0 |
| 201 | else |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 202 | [ "${#msg}" -gt 0 ] && log_failure "$haystack not contains $needle :: $msg" || true |
| 203 | ((ERRORS++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 204 | return 1 |
| 205 | fi |
| 206 | } |
| 207 | |
| 208 | assert_gt() { |
| 209 | local first="$1" |
| 210 | local second="$2" |
| 211 | local msg="${3-}" |
| 212 | |
| 213 | if [[ "$first" -gt "$second" ]]; then |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 214 | [ "${#msg}" -gt 0 ] && log_success "$msg" || true |
| 215 | ((PASSED++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 216 | return 0 |
| 217 | else |
| 218 | [ "${#msg}" -gt 0 ] && log_failure "$first > $second :: $msg" || true |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 219 | ((ERRORS++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 220 | return 1 |
| 221 | fi |
| 222 | } |
| 223 | |
| 224 | assert_ge() { |
| 225 | local first="$1" |
| 226 | local second="$2" |
| 227 | local msg="${3-}" |
| 228 | |
| 229 | if [[ "$first" -ge "$second" ]]; then |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 230 | [ "${#msg}" -gt 0 ] && log_success "$msg" || true |
| 231 | ((PASSED++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 232 | return 0 |
| 233 | else |
| 234 | [ "${#msg}" -gt 0 ] && log_failure "$first >= $second :: $msg" || true |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 235 | ((ERRORS++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 236 | return 1 |
| 237 | fi |
| 238 | } |
| 239 | |
| 240 | assert_lt() { |
| 241 | local first="$1" |
| 242 | local second="$2" |
| 243 | local msg="${3-}" |
| 244 | |
| 245 | if [[ "$first" -lt "$second" ]]; then |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 246 | [ "${#msg}" -gt 0 ] && log_success "$msg" || true |
| 247 | ((PASSED++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 248 | return 0 |
| 249 | else |
| 250 | [ "${#msg}" -gt 0 ] && log_failure "$first < $second :: $msg" || true |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 251 | ((ERRORS++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 252 | return 1 |
| 253 | fi |
| 254 | } |
| 255 | |
| 256 | assert_le() { |
| 257 | local first="$1" |
| 258 | local second="$2" |
| 259 | local msg="${3-}" |
| 260 | |
| 261 | if [[ "$first" -le "$second" ]]; then |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 262 | [ "${#msg}" -gt 0 ] && log_success "$msg" || true |
| 263 | ((PASSED++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 264 | return 0 |
| 265 | else |
| 266 | [ "${#msg}" -gt 0 ] && log_failure "$first <= $second :: $msg" || true |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 267 | ((ERRORS++)) |
| Marc Kupietz | ab0a733 | 2024-04-26 13:25:16 +0200 | [diff] [blame] | 268 | return 1 |
| 269 | fi |
| 270 | } |
| Marc Kupietz | ed3cc3a | 2024-04-27 16:05:49 +0200 | [diff] [blame] | 271 | |
| 272 | exit_with_test_summary() { |
| 273 | if [ "$ERRORS" -gt 0 ]; then |
| 274 | log_failure "There were $ERRORS errors" |
| 275 | else |
| 276 | log_success "All $PASSED tests passed" |
| 277 | fi |
| 278 | exit $ERRORS |
| 279 | } |