Add stub_called_at_most_times function

This commit is contained in:
2014-03-19 23:28:07 +00:00
parent 32af661b2f
commit 13a571b9a3
2 changed files with 50 additions and 0 deletions

17
stub.sh
View File

@@ -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:

View 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()"