-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtest_doubles.sh
More file actions
157 lines (132 loc) · 3.95 KB
/
test_doubles.sh
File metadata and controls
157 lines (132 loc) · 3.95 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
#!/usr/bin/env bash
MOCKED_FUNCTIONS=()
function unmock() {
local command=$1
for i in "${!MOCKED_FUNCTIONS[@]}"; do
if [[ "${MOCKED_FUNCTIONS[$i]}" == "$command" ]]; then
unset "MOCKED_FUNCTIONS[$i]"
unset -f "$command"
local variable
variable="$(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 mock() {
local command=$1
shift
if [[ $# -gt 0 ]]; then
eval "function $command() { $* ; }"
else
eval "function $command() { echo \"$($CAT)\" ; }"
fi
export -f "${command?}"
MOCKED_FUNCTIONS+=("$command")
}
function spy() {
local command=$1
local variable
variable="$(helper::normalize_variable_name "$command")"
local times_file params_file
local test_id="${BASHUNIT_CURRENT_TEST_ID:-global}"
times_file=$(temp_file "${test_id}_${variable}_times")
params_file=$(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+=\"\$(printf '%q' \"\$arg\")$'\\x1f'\"
done
serialized=\${serialized%$'\\x1f'}
printf '%s|%s\\n' \"\$raw\" \"\$serialized\" >> '$params_file'
local _c=\$(cat '$times_file')
_c=\$((_c+1))
echo \"\$_c\" > '$times_file'
}"
export -f "${command?}"
MOCKED_FUNCTIONS+=("$command")
}
function assert_have_been_called() {
local command=$1
local variable
variable="$(helper::normalize_variable_name "$command")"
local file_var="${variable}_times_file"
local times=0
if [[ -f "${!file_var-}" ]]; then
times=$(cat "${!file_var}")
fi
local label="${2:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"
if [[ $times -eq 0 ]]; then
state::add_assertions_failed
console_results::print_failed_test "${label}" "${command}" "to have been called" "once"
return
fi
state::add_assertions_passed
}
function assert_have_been_called_with() {
local command=$1
shift
local index=""
local number_pattern='^[0-9]+$'
if [[ ${!#} =~ $number_pattern ]]; then
index=${!#}
set -- "${@:1:$#-1}"
fi
local expected="$*"
local variable
variable="$(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}")
else
line=$(tail -n 1 "${!file_var}")
fi
fi
local raw
raw=$(echo "$line" | cut -d'|' -f1)
if [[ "$expected" != "$raw" ]]; then
state::add_assertions_failed
console_results::print_failed_test "$(helper::normalize_test_function_name \
"${FUNCNAME[1]}")" "$expected" "but got " "$raw"
return
fi
state::add_assertions_passed
}
function assert_have_been_called_times() {
local expected_count=$1
local command=$2
local variable
variable="$(helper::normalize_variable_name "$command")"
local file_var="${variable}_times_file"
local times=0
if [[ -f "${!file_var-}" ]]; then
times=$(cat "${!file_var}")
fi
local label="${3:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"
if [[ $times -ne $expected_count ]]; then
state::add_assertions_failed
console_results::print_failed_test "${label}" "${command}" \
"to have been called" "${expected_count} times" \
"actual" "${times} times"
return
fi
state::add_assertions_passed
}
function assert_not_called() {
local command=$1
local label="${2:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"
assert_have_been_called_times 0 "$command" "$label"
}