forked from TypedDevs/bashunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_doubles.sh
More file actions
205 lines (171 loc) · 5.7 KB
/
test_doubles.sh
File metadata and controls
205 lines (171 loc) · 5.7 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env bash
declare -a _BASHUNIT_MOCKED_FUNCTIONS=()
function bashunit::unmock() {
local command=$1
if [ "${#_BASHUNIT_MOCKED_FUNCTIONS[@]}" -eq 0 ]; then
return
fi
local i
for i in "${!_BASHUNIT_MOCKED_FUNCTIONS[@]}"; do
if [ "${_BASHUNIT_MOCKED_FUNCTIONS[$i]:-}" = "$command" ]; then
unset "_BASHUNIT_MOCKED_FUNCTIONS[$i]"
unset -f "$command"
local variable
variable="$(bashunit::helper::normalize_variable_name "$command")"
local times_file_var="${variable}_times_file"
local params_file_var="${variable}_params_file"
[ -f "${!times_file_var-}" ] && rm -f "${!times_file_var}"
[ -f "${!params_file_var-}" ] && rm -f "${!params_file_var}"
unset "$times_file_var"
unset "$params_file_var"
break
fi
done
}
function bashunit::mock() {
local command=$1
shift
if [ $# -gt 0 ]; then
eval "function $command() { $* \"\$@\"; }"
else
eval "function $command() { builtin echo \"$($CAT)\" ; }"
fi
export -f "${command?}"
_BASHUNIT_MOCKED_FUNCTIONS[${#_BASHUNIT_MOCKED_FUNCTIONS[@]}]="$command"
}
function bashunit::spy() {
local command=$1
local variable
variable="$(bashunit::helper::normalize_variable_name "$command")"
local times_file params_file
local test_id="${BASHUNIT_CURRENT_TEST_ID:-global}"
times_file=$(bashunit::temp_file "${test_id}_${variable}_times")
params_file=$(bashunit::temp_file "${test_id}_${variable}_params")
echo 0 >"$times_file"
: >"$params_file"
export "${variable}_times_file"="$times_file"
export "${variable}_params_file"="$params_file"
eval "function $command() {
local raw=\"\$*\"
local serialized=\"\"
local arg
for arg in \"\$@\"; do
serialized=\"\$serialized\$(builtin printf '%q' \"\$arg\")$'\\x1f'\"
done
serialized=\${serialized%$'\\x1f'}
builtin printf '%s\x1e%s\\n' \"\$raw\" \"\$serialized\" >> '$params_file'
local _c
_c=\$(cat '$times_file' 2>/dev/null || builtin echo 0)
_c=\$((_c+1))
builtin echo \"\$_c\" > '$times_file'
}"
export -f "${command?}"
_BASHUNIT_MOCKED_FUNCTIONS[${#_BASHUNIT_MOCKED_FUNCTIONS[@]}]="$command"
}
function assert_have_been_called() {
local command=$1
local variable
variable="$(bashunit::helper::normalize_variable_name "$command")"
local file_var="${variable}_times_file"
local times=0
if [ -f "${!file_var-}" ]; then
times=$(cat "${!file_var}" 2>/dev/null || builtin echo 0)
fi
local label="${2:-$(bashunit::helper::normalize_test_function_name "${FUNCNAME[1]}")}"
if [ "$times" -eq 0 ]; then
bashunit::state::add_assertions_failed
bashunit::console_results::print_failed_test "${label}" "${command}" "to have been called" "once"
return
fi
bashunit::state::add_assertions_passed
}
function assert_have_been_called_with() {
local command=$1
shift
local index=""
if [ "$(echo "${!#}" | "$GREP" -cE '^[0-9]+$' || true)" -gt 0 ]; then
index=${!#}
set -- "${@:1:$#-1}"
fi
local expected="$*"
local variable
variable="$(bashunit::helper::normalize_variable_name "$command")"
local file_var="${variable}_params_file"
local line=""
if [ -f "${!file_var-}" ]; then
if [ -n "$index" ]; then
line=$(sed -n "${index}p" "${!file_var}" 2>/dev/null || true)
else
line=$(tail -n 1 "${!file_var}" 2>/dev/null || true)
fi
fi
local raw
IFS=$'\x1e' read -r raw _ <<<"$line" || true
if [ "$expected" != "$raw" ]; then
bashunit::state::add_assertions_failed
bashunit::console_results::print_failed_test "$(bashunit::helper::normalize_test_function_name \
"${FUNCNAME[1]}")" "$expected" "but got " "$raw"
return
fi
bashunit::state::add_assertions_passed
}
function assert_have_been_called_times() {
local expected_count=$1
local command=$2
local variable
variable="$(bashunit::helper::normalize_variable_name "$command")"
local file_var="${variable}_times_file"
local times=0
if [ -f "${!file_var-}" ]; then
times=$(cat "${!file_var}" 2>/dev/null || builtin echo 0)
fi
local label="${3:-$(bashunit::helper::normalize_test_function_name "${FUNCNAME[1]}")}"
if [ "$times" -ne "$expected_count" ]; then
bashunit::state::add_assertions_failed
bashunit::console_results::print_failed_test "${label}" "${command}" \
"to have been called" "${expected_count} times" \
"actual" "${times} times"
return
fi
bashunit::state::add_assertions_passed
}
function assert_have_been_called_nth_with() {
local nth=$1
local command=$2
shift 2
local expected="$*"
local variable
variable="$(bashunit::helper::normalize_variable_name "$command")"
local times_file_var="${variable}_times_file"
local file_var="${variable}_params_file"
local label
label="$(bashunit::helper::normalize_test_function_name "${FUNCNAME[1]}")"
local times=0
if [ -f "${!times_file_var-}" ]; then
times=$(cat "${!times_file_var}" 2>/dev/null || builtin echo 0)
fi
if [ "$nth" -gt "$times" ]; then
bashunit::state::add_assertions_failed
bashunit::console_results::print_failed_test "${label}" \
"expected call" "at index ${nth} but" "only called ${times} times"
return
fi
local line=""
if [ -f "${!file_var-}" ]; then
line=$(sed -n "${nth}p" "${!file_var}" 2>/dev/null || true)
fi
local raw
IFS=$'\x1e' read -r raw _ <<<"$line" || true
if [ "$expected" != "$raw" ]; then
bashunit::state::add_assertions_failed
bashunit::console_results::print_failed_test "${label}" \
"$expected" "but got " "$raw"
return
fi
bashunit::state::add_assertions_passed
}
function assert_not_called() {
local command=$1
local label="${2:-$(bashunit::helper::normalize_test_function_name "${FUNCNAME[1]}")}"
assert_have_been_called_times 0 "$command" "$label"
}