mirror of
https://github.com/jimeh/stub.sh.git
synced 2026-02-19 05:36:39 +00:00
Change behavior of stub_called_times
It now only ever returns the the number of times the stub has been called. To assert if it has been called exactly X number of times, use `stub_called_exact_times` instead.
This commit is contained in:
26
stub.sh
26
stub.sh
@@ -161,7 +161,6 @@ stub_called_with() {
|
||||
# it doesn't match..
|
||||
stub_called_times() {
|
||||
local cmd="$1"
|
||||
local expected="$2"
|
||||
|
||||
local index="$(__stub_index "$1")"
|
||||
local count=0
|
||||
@@ -170,12 +169,23 @@ stub_called_times() {
|
||||
eval "count=\"\${#STUB_${index}_CALLS[@]}\""
|
||||
fi
|
||||
|
||||
if [ -n "$expected" ]; then
|
||||
if [ "$expected" != "$count" ]; then
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
echo $count
|
||||
echo $count
|
||||
}
|
||||
|
||||
|
||||
# Public: Find out if stub has been called exactly the given number of times
|
||||
# with specified arguments.
|
||||
#
|
||||
# Arguments:
|
||||
# - $1: Name of stubbed command.
|
||||
# - $2: Exact number of times stub has been called.
|
||||
#
|
||||
# Echoes nothing.
|
||||
# Returns 0 (success) if stub has been called at least the given number of
|
||||
# times with specified arguments, otherwise 1 (error) is returned.
|
||||
stub_called_exactly_times() {
|
||||
if [ "$(stub_called_times "$1")" != "$2" ]; then
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -248,7 +258,7 @@ stub_called_with_times() {
|
||||
#
|
||||
# Arguments:
|
||||
# - $1: Name of stubbed command.
|
||||
# - $2: Minimum required number of times stub has been called.
|
||||
# - $2: Exact number of times stub has been called.
|
||||
# - $@: All additional arguments are used to specify what stub was called
|
||||
# with.
|
||||
#
|
||||
|
||||
32
test/stub_called_exactly_times-test.sh
Executable file
32
test/stub_called_exactly_times-test.sh
Executable file
@@ -0,0 +1,32 @@
|
||||
#! /usr/bin/env bash
|
||||
source "test-helper.sh"
|
||||
|
||||
#
|
||||
# stub_called_exactly_times() tests.
|
||||
#
|
||||
|
||||
# Setup.
|
||||
stub "uname"
|
||||
uname
|
||||
uname -r
|
||||
|
||||
|
||||
# Returns 0 when stub called exactly given number of times.
|
||||
assert_raises 'stub_called_exactly_times "uname" 2' 0
|
||||
|
||||
# Returns 1 when stub has not been called the exact given number of times.
|
||||
assert_raises 'stub_called_exactly_times "uname" 4' 1
|
||||
assert_raises 'stub_called_exactly_times "uname" 3' 1
|
||||
assert_raises 'stub_called_exactly_times "uname" 1' 1
|
||||
assert_raises 'stub_called_exactly_times "uname" 0' 1
|
||||
|
||||
# Behaves as if stub has not been called when the stub doesn't exist.
|
||||
assert_raises 'stub_called_exactly_times "top" 0' 0
|
||||
assert_raises 'stub_called_exactly_times "top" 1' 1
|
||||
|
||||
# Teardown.
|
||||
restore "uname"
|
||||
|
||||
|
||||
# End of tests.
|
||||
assert_end "stub_called_times()"
|
||||
@@ -29,14 +29,6 @@ stub "uname"
|
||||
assert 'stub_called_times "uname"' "0"
|
||||
restore "uname"
|
||||
|
||||
# When given a second argument, asserts stub called X number of times.
|
||||
stub "uname"
|
||||
uname
|
||||
assert_raises 'stub_called_times "uname" 1' 0
|
||||
assert 'stub_called_times "uname" 1' ""
|
||||
assert_raises 'stub_called_times "uname" 3' 1
|
||||
assert 'stub_called_times "uname" 3' ""
|
||||
restore "uname"
|
||||
|
||||
# End of tests.
|
||||
assert_end "stub_called_times()"
|
||||
|
||||
@@ -12,7 +12,7 @@ uname -r
|
||||
uname -r
|
||||
uname -r -a
|
||||
|
||||
# Returns 0 when stub called exactly given number of times
|
||||
# Returns 0 when stub called exactly given number of times.
|
||||
assert_raises 'stub_called_with_exactly_times "uname" 2 -r' 0
|
||||
|
||||
# Returns 1 when stub has not been called the exact given number of times.
|
||||
|
||||
Reference in New Issue
Block a user