-
Notifications
You must be signed in to change notification settings - Fork 3
/
shpy-shunit2
105 lines (84 loc) · 2.64 KB
/
shpy-shunit2
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
#!/usr/bin/env sh
assertCallCount() {
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
echo 'Usage: assertCallCount [message] spy count'
echo
echo 'Assert the number of times the spy was invoked'
return 1
fi
if [ $# -eq 2 ]; then
assertEquals "$2" "$(getSpyCallCount "$1")"
else
assertEquals "$1" "$3" "$(getSpyCallCount "$2")"
fi
}
assertCalledWith() {
if [ $# -lt 1 ]; then
echo 'Usage: assertCalledWith spy [arg ...]'
echo
echo 'Assert the arguments for the first invocation of the spy'
echo
echo 'Subsequent calls will assert the second invocation, etc'
return 1
fi
# shellcheck disable=SC3043
local status_code actual_call current_spy_call
wasSpyCalledWith "$@"
status_code=$?
actual_call=$1
if [ $status_code -ne 0 ]; then
current_spy_call=$(( $(_shpyGetCurrentSpyCallIndex "$1") + 1 ))
if ! actual_call="$actual_call $(getArgsForCall "$1" "$current_spy_call")" 2>/dev/null; then
actual_call='not called'
fi
fi
examineNextSpyCall "$1"
assertTrue "expected:<$*> but was:<$actual_call>" $status_code
}
assertCalledWith_() {
if [ $# -lt 2 ]; then
echo 'Usage: assertCalledWith_ message spy [arg ...]'
echo
echo 'Assert the arguments for the first invocation of the spy'
echo 'When the assertion fails, the custom error message is used'
echo
echo 'Subsequent calls will assert the second invocation, etc'
return 1
fi
# shellcheck disable=SC3043
local message status_code
message=$1
shift
wasSpyCalledWith "$@"
status_code=$?
examineNextSpyCall "$1"
assertTrue "$message" $status_code
}
assertCalledOnceWith() {
if [ $# -lt 1 ]; then
echo 'Usage: assertCalledOnceWith spy [arg ...]'
echo
echo 'Assert the arguments of the first and only invocation of the spy'
return 1
fi
assertCallCount "$1" 1 && assertCalledWith "$@"
}
assertCalledOnceWith_() {
if [ $# -lt 2 ]; then
echo 'Usage: assertCalledOnceWith_ message spy [arg ...]'
echo
echo 'Assert the arguments of the first and only invocation of the spy'
echo 'When the assertion fails, the custom error message is used'
return 1
fi
assertCallCount "$1" "$2" 1 && assertCalledWith_ "$@"
}
assertNeverCalled() {
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
echo 'Usage: assertNeverCalled [message] spy'
echo
echo 'Assert the spy was never invoked with an optional error message'
return 1
fi
assertCallCount "$@" 0
}