mirror of
https://github.com/jimeh/stub.sh.git
synced 2026-02-19 13:46:40 +00:00
Add stub_called_at_least_times function
This commit is contained in:
16
stub.sh
16
stub.sh
@@ -150,6 +150,22 @@ stub_called_times() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out of stub has been called at least the given number of times.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Minimum required number of times stub has been called.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called at least the given number of
|
||||||
|
# times, otherwise 1 (error) is returned.
|
||||||
|
stub_called_at_least_times() {
|
||||||
|
if [ "$(stub_called_times "$1")" -lt "$2" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# Public: Restore the original command/function that was stubbed.
|
# Public: Restore the original command/function that was stubbed.
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
|
|||||||
32
test/stub_called_at_least_times-test.sh
Executable file
32
test/stub_called_at_least_times-test.sh
Executable file
@@ -0,0 +1,32 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_at_least_times() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Setup
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname
|
||||||
|
|
||||||
|
# Returns 0 when stub called at least given number of times.
|
||||||
|
assert_raises 'stub_called_at_least_times "uname" 0' 0
|
||||||
|
assert_raises 'stub_called_at_least_times "uname" 1' 0
|
||||||
|
assert_raises 'stub_called_at_least_times "uname" 2' 0
|
||||||
|
|
||||||
|
# Returns 1 when stub has been called less than given number of times.
|
||||||
|
assert_raises 'stub_called_at_least_times "uname" 3' 1
|
||||||
|
assert_raises 'stub_called_at_least_times "uname" 4' 1
|
||||||
|
assert_raises 'stub_called_at_least_times "uname" 5' 1
|
||||||
|
|
||||||
|
# Behaves as if stub has not been called when the stub doesn't exist.
|
||||||
|
assert_raises 'stub_called_at_least_times "top" 0' 0
|
||||||
|
assert_raises 'stub_called_at_least_times "top" 1' 1
|
||||||
|
|
||||||
|
# Teardown
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_at_least_times()"
|
||||||
Reference in New Issue
Block a user