mirror of
https://github.com/jimeh/stub.sh.git
synced 2026-02-19 05:36:39 +00:00
Add stub_called_at_most_times function
This commit is contained in:
17
stub.sh
17
stub.sh
@@ -166,6 +166,23 @@ stub_called_at_least_times() {
|
||||
}
|
||||
|
||||
|
||||
# Public: Find out of stub has been called no more than 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 no more than the given number of
|
||||
# times, otherwise 1 (error) is returned.
|
||||
stub_called_at_most_times() {
|
||||
if [ "$(stub_called_times "$1")" -gt "$2" ]; then
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# Public: Restore the original command/function that was stubbed.
|
||||
#
|
||||
# Arguments:
|
||||
|
||||
33
test/stub_called_at_most_times-test.sh
Executable file
33
test/stub_called_at_most_times-test.sh
Executable file
@@ -0,0 +1,33 @@
|
||||
#! /usr/bin/env bash
|
||||
source "test-helper.sh"
|
||||
|
||||
#
|
||||
# stub_called_at_most_times() tests.
|
||||
#
|
||||
|
||||
# Setup
|
||||
stub "uname"
|
||||
uname
|
||||
uname
|
||||
uname
|
||||
|
||||
# Returns 0 when stub called no more than given number of times.
|
||||
assert_raises 'stub_called_at_most_times "uname" 5' 0
|
||||
assert_raises 'stub_called_at_most_times "uname" 4' 0
|
||||
assert_raises 'stub_called_at_most_times "uname" 3' 0
|
||||
|
||||
# Returns 1 when stub has been called more than given number of times.
|
||||
assert_raises 'stub_called_at_most_times "uname" 2' 1
|
||||
assert_raises 'stub_called_at_most_times "uname" 1' 1
|
||||
assert_raises 'stub_called_at_most_times "uname" 0' 1
|
||||
|
||||
# Behaves as if stub has not been called when the stub doesn't exist.
|
||||
assert_raises 'stub_called_at_most_times "top" 0' 0
|
||||
assert_raises 'stub_called_at_most_times "top" 1' 0
|
||||
|
||||
# Teardown
|
||||
restore "uname"
|
||||
|
||||
|
||||
# End of tests.
|
||||
assert_end "stub_called_at_most_times()"
|
||||
Reference in New Issue
Block a user