From 13a571b9a3cfa771590e86e1fe92e7dc2477c75c Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 19 Mar 2014 23:28:07 +0000 Subject: [PATCH] Add stub_called_at_most_times function --- stub.sh | 17 +++++++++++++ test/stub_called_at_most_times-test.sh | 33 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100755 test/stub_called_at_most_times-test.sh diff --git a/stub.sh b/stub.sh index bab21ed..0ebef48 100644 --- a/stub.sh +++ b/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: diff --git a/test/stub_called_at_most_times-test.sh b/test/stub_called_at_most_times-test.sh new file mode 100755 index 0000000..067cfb8 --- /dev/null +++ b/test/stub_called_at_most_times-test.sh @@ -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()"