-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtest_doubles.sh
More file actions
149 lines (126 loc) · 3.85 KB
/
test_doubles.sh
File metadata and controls
149 lines (126 loc) · 3.85 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
#!/usr/bin/env bash
declare -a 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() {
echo \"\$*\" >> '$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 has been called" "once"
return
fi
state::add_assertions_passed
}
function assert_have_been_called_with() {
local expected=$1
local command=$2
local third_arg="${3:-}"
local fourth_arg="${4:-}"
local index=""
local label=""
if [[ -n $third_arg && $third_arg =~ ^[0-9]+$ ]]; then
index=$third_arg
label="${fourth_arg:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"
else
label="${third_arg:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"
index="$fourth_arg"
fi
local variable
variable="$(helper::normalize_variable_name "$command")"
local file_var="${variable}_params_file"
local params=""
if [[ -f "${!file_var-}" ]]; then
if [[ -n $index ]]; then
params=$(sed -n "${index}p" "${!file_var}")
else
params=$(tail -n 1 "${!file_var}")
fi
fi
if [[ "$expected" != "$params" ]]; then
state::add_assertions_failed
console_results::print_failed_test "${label}" "${expected}" "but got " "$params"
return
fi
state::add_assertions_passed
}
function assert_have_been_called_times() {
local expected=$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 ]]; then
state::add_assertions_failed
console_results::print_failed_test "${label}" "${command}" \
"to has been called" "${expected} 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"
}