-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
116 lines (103 loc) · 2.77 KB
/
test.sh
File metadata and controls
116 lines (103 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env bash
if type test::equal > /dev/null 2>&1; then return; fi
set -euo pipefail
_load_dependencies() {
local script_dir=${BASH_SOURCE:-$0}
script_dir="$(dirname "$(realpath "$script_dir")")"
source "${script_dir}/ansi.sh"
}
if ! type ansi::out >/dev/null 2>&1; then
_load_dependencies
fi
unset -f _load_dependencies
test_lib_dir="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
test_pass=0
test_fail=0
test_total=0
test_name=""
test::name() {
test_name="$1"
}
test::equal() {
local actual="$1"
local expected="$2"
local msg="${3:-}"
test_total=$((test_total + 1))
if [[ "$actual" == "$expected" ]]; then
test_pass=$((test_pass + 1))
echo -e " ${GREEN}ok${RESET_ALL} ${test_total} ${msg}"
else
test_fail=$((test_fail + 1))
echo -e " ${RED}FAIL${RESET_ALL} ${test_total} ${msg}"
echo -e " expected: ${expected@Q}"
echo -e " actual: ${actual@Q}"
fi
}
test::not_equal() {
local actual="$1"
local expected="$2"
local msg="${3:-}"
test_total=$((test_total + 1))
if [[ "$actual" != "$expected" ]]; then
test_pass=$((test_pass + 1))
echo -e " ${GREEN}ok${RESET_ALL} ${test_total} ${msg}"
else
test_fail=$((test_fail + 1))
echo -e " ${RED}FAIL${RESET_ALL} ${test_total} ${msg}"
echo -e " expected anything other than: ${expected@Q}"
fi
}
test::match() {
local actual="$1"
local pattern="$2"
local msg="${3:-}"
test_total=$((test_total + 1))
if [[ "$actual" =~ $pattern ]]; then
test_pass=$((test_pass + 1))
echo -e " ${GREEN}ok${RESET_ALL} ${test_total} ${msg}"
else
test_fail=$((test_fail + 1))
echo -e " ${RED}FAIL${RESET_ALL} ${test_total} ${msg}"
echo -e " value: ${actual@Q}"
echo -e " pattern: ${pattern@Q}"
fi
}
test::contains() {
local haystack="$1"
local needle="$2"
local msg="${3:-}"
test_total=$((test_total + 1))
if echo "$haystack" | grep -qF -- "$needle"; then
test_pass=$((test_pass + 1))
echo -e " ${GREEN}ok${RESET_ALL} ${test_total} ${msg}"
else
test_fail=$((test_fail + 1))
echo -e " ${RED}FAIL${RESET_ALL} ${test_total} ${msg}"
echo -e " value: ${haystack@Q}"
echo -e " expected: ${needle@Q}"
fi
}
test::ok() {
local msg="${1:-}"
test_total=$((test_total + 1))
test_pass=$((test_pass + 1))
echo -e " ${GREEN}ok${RESET_ALL} ${test_total} ${msg}"
}
test::fail_() {
local msg="${1:-}"
test_total=$((test_total + 1))
test_fail=$((test_fail + 1))
echo -e " ${RED}FAIL${RESET_ALL} ${test_total} ${msg}"
}
test::done() {
echo ""
if [[ "$test_fail" -eq 0 ]]; then
echo -e "${GREEN}${BOLD}All ${test_pass} tests passed.${RESET_ALL}"
else
echo -e "${RED}${BOLD}${test_fail} of ${test_total} tests failed.${RESET_ALL}"
fi
if [[ "$test_fail" -gt 0 ]]; then
exit 1
fi
exit 0
}