mirror of
https://github.com/jimeh/stub.sh.git
synced 2026-02-19 13:46:40 +00:00
Add stub_called_with and related functions
This commit is contained in:
228
stub.sh
228
stub.sh
@@ -72,9 +72,6 @@ stub_and_echo() {
|
|||||||
stub_and_eval() {
|
stub_and_eval() {
|
||||||
local cmd="$1"
|
local cmd="$1"
|
||||||
|
|
||||||
# Setup empty list of called stubs.
|
|
||||||
if [ -z "$STUB_CALLED_STUBS" ]; then STUB_CALLED_STUBS=(); fi
|
|
||||||
|
|
||||||
# Setup empty list of active stubs.
|
# Setup empty list of active stubs.
|
||||||
if [ -z "$STUB_ACTIVE_STUBS" ]; then STUB_ACTIVE_STUBS=(); fi
|
if [ -z "$STUB_ACTIVE_STUBS" ]; then STUB_ACTIVE_STUBS=(); fi
|
||||||
|
|
||||||
@@ -89,18 +86,17 @@ stub_and_eval() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Prepare stub index and call list for this stub.
|
||||||
|
__stub_register "$cmd"
|
||||||
|
|
||||||
# Keep track of what is currently stubbed to ensure restore only acts on
|
# Keep track of what is currently stubbed to ensure restore only acts on
|
||||||
# actual stubs.
|
# actual stubs.
|
||||||
if [[ " ${STUB_ACTIVE_STUBS[@]} " != *" $1 "* ]]; then
|
if [[ " ${STUB_ACTIVE_STUBS[@]} " != *" $cmd "* ]]; then
|
||||||
STUB_ACTIVE_STUBS+=("$cmd")
|
STUB_ACTIVE_STUBS+=("$cmd")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Remove stub from list of called stubs, as we are now creating a new stub
|
|
||||||
# which hasn't been called yet.
|
|
||||||
STUB_CALLED_STUBS=(${STUB_CALLED_STUBS[@]/$cmd/})
|
|
||||||
|
|
||||||
# Create the stub.
|
# Create the stub.
|
||||||
eval "$(echo -e "${cmd}() {\n __stub_call \"${cmd}\"\n $2\n}")"
|
eval "$(echo -e "${cmd}() {\n __stub_call \"${cmd}\" \$@\n $2\n}")"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -112,7 +108,35 @@ stub_and_eval() {
|
|||||||
# Echoes nothing.
|
# Echoes nothing.
|
||||||
# Returns 0 (success) is stub has been called, 1 (error) otherwise.
|
# Returns 0 (success) is stub has been called, 1 (error) otherwise.
|
||||||
stub_called() {
|
stub_called() {
|
||||||
if [[ " ${STUB_CALLED_STUBS[@]} " != *" $1 "* ]]; then
|
if [ "$(stub_called_times "$1")" -lt 1 ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called with specific arguments.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $@: All additional arguments are used to specify what stub was called
|
||||||
|
# with.
|
||||||
|
#
|
||||||
|
# Examples:
|
||||||
|
# stub "uname"
|
||||||
|
# uname
|
||||||
|
# uname -r -a
|
||||||
|
# stub_called_with uname # Returns 0 (success).
|
||||||
|
# stub_called_with uname -r # Returns 1 (error).
|
||||||
|
# stub_called_with uname -r -a # Returns 0 (success).
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if specified stub has been called with given arguments,
|
||||||
|
# otherwise returns 1 (error).
|
||||||
|
stub_called_with() {
|
||||||
|
local cmd="$1"
|
||||||
|
shift 1
|
||||||
|
|
||||||
|
if [ "$(stub_called_with_times "$cmd" $@)" -lt 1 ]; then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
@@ -127,8 +151,8 @@ stub_called() {
|
|||||||
#
|
#
|
||||||
# Examples:
|
# Examples:
|
||||||
# stub_called_times "uname" # Echoes "2" if stub has been called twice.
|
# stub_called_times "uname" # Echoes "2" if stub has been called twice.
|
||||||
# stub_called_times "uname" 2 # Returns value of 0 (success).
|
# stub_called_times "uname" 2 # Returns 0 (success).
|
||||||
# stub_called_times "uname" 3 # Returns value of 1 (error).
|
# stub_called_times "uname" 3 # Returns 1 (error).
|
||||||
#
|
#
|
||||||
# Echoes number of times stub has been called if $2 is not given, otherwise
|
# Echoes number of times stub has been called if $2 is not given, otherwise
|
||||||
# echoes nothing.
|
# echoes nothing.
|
||||||
@@ -136,16 +160,18 @@ stub_called() {
|
|||||||
# number of times the stub has been called. Otherwise 1 (error) is returned if
|
# number of times the stub has been called. Otherwise 1 (error) is returned if
|
||||||
# it doesn't match..
|
# it doesn't match..
|
||||||
stub_called_times() {
|
stub_called_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
local expected="$2"
|
||||||
|
|
||||||
|
local index="$(__stub_index "$1")"
|
||||||
local count=0
|
local count=0
|
||||||
|
|
||||||
for called in ${STUB_CALLED_STUBS[@]}; do
|
if [ -n "$index" ]; then
|
||||||
if [ "$called" == "$1" ]; then
|
eval "count=\"\${#STUB_${index}_CALLS[@]}\""
|
||||||
((count++))
|
fi
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
if [ -n "$2" ]; then
|
if [ -n "$expected" ]; then
|
||||||
if [ "$2" != "$count" ]; then
|
if [ "$expected" != "$count" ]; then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
@@ -154,7 +180,7 @@ stub_called_times() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Public: Find out of stub has been called at least the given number of times.
|
# Public: Find out if stub has been called at least the given number of times.
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
@@ -170,12 +196,12 @@ stub_called_at_least_times() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Public: Find out of stub has been called no more than the given number of
|
# Public: Find out if stub has been called no more than the given number of
|
||||||
# times.
|
# times.
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
# - $2: Minimum required number of times stub has been called.
|
# - $2: Maximum allowed number of times stub has been called.
|
||||||
#
|
#
|
||||||
# Echoes nothing.
|
# Echoes nothing.
|
||||||
# Returns 0 (success) if stub has been called no more than the given number of
|
# Returns 0 (success) if stub has been called no more than the given number of
|
||||||
@@ -187,6 +213,105 @@ stub_called_at_most_times() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out how many times a stub has been called with specific
|
||||||
|
# arguments.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $@: All additional arguments are used to specify what stub was called
|
||||||
|
# with.
|
||||||
|
#
|
||||||
|
# Echoes number of times stub has been called with given arguments.
|
||||||
|
# Return 0 (success).
|
||||||
|
stub_called_with_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
shift 1
|
||||||
|
local args="$@"
|
||||||
|
if [ "$args" == "" ]; then args="<none>"; fi
|
||||||
|
|
||||||
|
local count=0
|
||||||
|
local index="$(__stub_index "$cmd")"
|
||||||
|
if [ -n "$index" ]; then
|
||||||
|
eval "local calls=(\"\${STUB_${index}_CALLS[@]}\")"
|
||||||
|
for call in "${calls[@]}"; do
|
||||||
|
if [ "$call" == "$args" ]; then ((count++)); fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
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: Minimum required number of times stub has been called.
|
||||||
|
# - $@: All additional arguments are used to specify what stub was called
|
||||||
|
# with.
|
||||||
|
#
|
||||||
|
# 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_with_exactly_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
local count="$2"
|
||||||
|
shift 2
|
||||||
|
|
||||||
|
if [ "$(stub_called_with_times "$cmd" $@)" != "$count" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called at least the given number of times
|
||||||
|
# with specified arguments.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Minimum required number of times stub has been called.
|
||||||
|
# - $@: All additional arguments are used to specify what stub was called
|
||||||
|
# with.
|
||||||
|
#
|
||||||
|
# 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_with_at_least_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
local count="$2"
|
||||||
|
shift 2
|
||||||
|
|
||||||
|
if [ "$(stub_called_with_times "$cmd" $@)" -lt "$count" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called no more than the given number of
|
||||||
|
# times.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Maximum allowed number of times stub has been called.
|
||||||
|
# - $@: All additional arguments are used to specify what stub was called
|
||||||
|
# with.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called no more than the given number of
|
||||||
|
# times with specified arguments, otherwise 1 (error) is returned.
|
||||||
|
stub_called_with_at_most_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
local count="$2"
|
||||||
|
shift 2
|
||||||
|
|
||||||
|
if [ "$(stub_called_with_times "$cmd" $@)" -gt "$count" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# Public: Restore the original command/function that was stubbed.
|
# Public: Restore the original command/function that was stubbed.
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
@@ -228,5 +353,62 @@ restore() {
|
|||||||
# Private: Used to keep track of which stubs have been called and how many
|
# Private: Used to keep track of which stubs have been called and how many
|
||||||
# times.
|
# times.
|
||||||
__stub_call() {
|
__stub_call() {
|
||||||
STUB_CALLED_STUBS+=("$1")
|
local cmd="$1"
|
||||||
|
shift 1
|
||||||
|
local args="$@"
|
||||||
|
if [ "$args" == "" ]; then args="<none>"; fi
|
||||||
|
|
||||||
|
local index="$(__stub_index "$cmd")"
|
||||||
|
if [ -n "$index" ]; then
|
||||||
|
eval "STUB_${index}_CALLS+=(\"\$args\")"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Private: Get index value of stub. Required to access list of stub calls.
|
||||||
|
__stub_index() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
for item in ${STUB_INDEX[@]}; do
|
||||||
|
if [[ "$item" == "${cmd}="* ]]; then
|
||||||
|
local index="$item"
|
||||||
|
index="${index/${cmd}=/}"
|
||||||
|
echo "$index"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Private: Prepare for the creation of a new stub. Adds stub to index and
|
||||||
|
# sets up an empty call list.
|
||||||
|
__stub_register() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
if [ -z "$STUB_NEXT_INDEX" ]; then STUB_NEXT_INDEX=0; fi
|
||||||
|
if [ -z "$STUB_INDEX" ]; then STUB_INDEX=(); fi
|
||||||
|
|
||||||
|
# Clean up after any previous stub for the same command.
|
||||||
|
__stub_clean "$cmd"
|
||||||
|
|
||||||
|
# Add stub to index.
|
||||||
|
STUB_INDEX+=("${cmd}=${STUB_NEXT_INDEX}")
|
||||||
|
eval "STUB_${STUB_NEXT_INDEX}_CALLS=()"
|
||||||
|
|
||||||
|
# Increment stub count.
|
||||||
|
((STUB_NEXT_INDEX++))
|
||||||
|
}
|
||||||
|
|
||||||
|
# Private: Cleans out and removes a stub's call list, and removes stub from
|
||||||
|
# index.
|
||||||
|
__stub_clean() {
|
||||||
|
local cmd="$1"
|
||||||
|
local index="$(__stub_index "$cmd")"
|
||||||
|
|
||||||
|
# Remove all relevant details from any previously existing stub for the same
|
||||||
|
# command.
|
||||||
|
if [ -n "$index" ]; then
|
||||||
|
eval "unset STUB_${index}_CALLS"
|
||||||
|
STUB_INDEX=(${STUB_INDEX[@]/${cmd}=*/})
|
||||||
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
21
test/__stub_call-test.sh
Executable file
21
test/__stub_call-test.sh
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# __stub_call() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Adds call to stub call list.
|
||||||
|
STUB_INDEX=("uname=0")
|
||||||
|
STUB_0_CALLS=()
|
||||||
|
__stub_call "uname"
|
||||||
|
__stub_call "uname" -r
|
||||||
|
__stub_call "uname" -r -a
|
||||||
|
assert 'echo ${STUB_0_CALLS[@]}' "<none> -r -r -a"
|
||||||
|
assert 'echo ${STUB_0_CALLS[0]}' "<none>"
|
||||||
|
assert 'echo ${STUB_0_CALLS[1]}' "-r"
|
||||||
|
assert 'echo ${STUB_0_CALLS[2]}' "-r -a"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "__stub_call()"
|
||||||
19
test/__stub_clean-test.sh
Executable file
19
test/__stub_clean-test.sh
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# __stub_clean() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Removes unsets stub call list, removes stub from index
|
||||||
|
STUB_INDEX=("uname=0" "top=1")
|
||||||
|
STUB_0_CALLS=("<none>" "-r" "-r -a")
|
||||||
|
STUB_1_CALLS=("-h")
|
||||||
|
__stub_clean "uname"
|
||||||
|
assert 'echo ${STUB_INDEX[@]}' "top=1"
|
||||||
|
assert 'echo ${STUB_INDEX[0]}' "top=1"
|
||||||
|
assert 'echo ${STUB_0_CALLS[@]}' ""
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "__stub_clean()"
|
||||||
21
test/__stub_index-test.sh
Executable file
21
test/__stub_index-test.sh
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# __stub_index() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Echoes index of given stub.
|
||||||
|
STUB_INDEX=("uname=1" "top=3")
|
||||||
|
assert '__stub_index "uname"' "1"
|
||||||
|
assert '__stub_index "top"' "3"
|
||||||
|
unset STUB_INDEX
|
||||||
|
|
||||||
|
# Echoes nothing if stub is not in the index.
|
||||||
|
STUB_INDEX=("uname=1")
|
||||||
|
assert '__stub_index "top"' ""
|
||||||
|
unset STUB_INDEX
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "__stub_index()"
|
||||||
22
test/__stub_register-test.sh
Executable file
22
test/__stub_register-test.sh
Executable file
@@ -0,0 +1,22 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# __stub_register() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Sets up stub index, stub call list, and adds stub to index.
|
||||||
|
__stub_register "uname"
|
||||||
|
__stub_register "top"
|
||||||
|
assert 'echo ${STUB_INDEX[@]}' 'uname=0 top=1'
|
||||||
|
assert 'echo ${STUB_INDEX[0]}' 'uname=0'
|
||||||
|
assert 'echo ${STUB_INDEX[1]}' 'top=1'
|
||||||
|
assert 'echo $STUB_NEXT_INDEX' "2"
|
||||||
|
|
||||||
|
# Note: There seems to be no possible way to validate if a empty array
|
||||||
|
# variable has been set, as it appears to be empty/null/undefined whatever I
|
||||||
|
# try.
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "__stub_register()"
|
||||||
54
test/stub_called_with-test.sh
Executable file
54
test/stub_called_with-test.sh
Executable file
@@ -0,0 +1,54 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_with() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Returns 1 when stub doesn't exist.
|
||||||
|
assert_raises 'stub_called_with "top"' 1
|
||||||
|
|
||||||
|
# Returns 0 when stub has been called with given arguments.
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname -r
|
||||||
|
uname -r -a
|
||||||
|
assert_raises 'stub_called_with "uname"' 0
|
||||||
|
assert_raises 'stub_called_with "uname" -r' 0
|
||||||
|
assert_raises 'stub_called_with "uname" -r -a' 0
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
# Returns 1 when stub has not been called with given arguments.
|
||||||
|
stub "uname"
|
||||||
|
uname -r
|
||||||
|
assert_raises 'stub_called_with "uname"' 1
|
||||||
|
assert_raises 'stub_called_with "uname" -a' 1
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
# Only matches against exact argument lists.
|
||||||
|
stub "uname"
|
||||||
|
uname -r -a
|
||||||
|
assert_raises 'stub_called_with "uname" -r' 1
|
||||||
|
assert_raises 'stub_called_with "uname" -r -a' 0
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
# Call history is only reset when restubbing a command, not when restoring.
|
||||||
|
stub "uname"
|
||||||
|
uname -r
|
||||||
|
assert_raises 'stub_called_with "uname" -r' 0
|
||||||
|
restore "uname"
|
||||||
|
assert_raises 'stub_called_with "uname" -r' 0
|
||||||
|
stub "uname"
|
||||||
|
assert_raises 'stub_called_with "uname" -r' 1
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
# Handling of string arguments containing spaces.
|
||||||
|
stub "uname"
|
||||||
|
uname -r "foo bar"
|
||||||
|
assert_raises 'stub_called_with "uname" -r "foo bar"' 0
|
||||||
|
assert_raises 'stub_called_with "uname" -r foo bar' 0
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_with()"
|
||||||
34
test/stub_called_with_at_least_times-test.sh
Executable file
34
test/stub_called_with_at_least_times-test.sh
Executable file
@@ -0,0 +1,34 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_with_at_least_times() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Setup.
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname -r
|
||||||
|
uname -r
|
||||||
|
uname -r -a
|
||||||
|
|
||||||
|
# Retruns 0 when stub called with at least given number of times.
|
||||||
|
assert_raises 'stub_called_with_at_least_times "uname" 0 -r' 0
|
||||||
|
assert_raises 'stub_called_with_at_least_times "uname" 1 -r' 0
|
||||||
|
assert_raises 'stub_called_with_at_least_times "uname" 2 -r' 0
|
||||||
|
|
||||||
|
# Retruns 1 when stub called with less than given number of times.
|
||||||
|
assert_raises 'stub_called_with_at_least_times "uname" 3 -r' 1
|
||||||
|
assert_raises 'stub_called_with_at_least_times "uname" 4 -r' 1
|
||||||
|
assert_raises 'stub_called_with_at_least_times "uname" 5 -r' 1
|
||||||
|
|
||||||
|
# Behaves as if stub has not been called when the stub doesn't exist.
|
||||||
|
assert_raises 'stub_called_with_at_least_times "top" 0' 0
|
||||||
|
assert_raises 'stub_called_with_at_least_times "top" 1' 1
|
||||||
|
|
||||||
|
# Teardown.
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_with_at_least_times()"
|
||||||
35
test/stub_called_with_at_most_times-test.sh
Executable file
35
test/stub_called_with_at_most_times-test.sh
Executable file
@@ -0,0 +1,35 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_with_at_most_times() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Setup.
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname -r
|
||||||
|
uname -r
|
||||||
|
uname -r
|
||||||
|
uname -r -a
|
||||||
|
|
||||||
|
# Returns 0 when stub called no more than given number of times.
|
||||||
|
assert_raises 'stub_called_with_at_most_times "uname" 5 -r' 0
|
||||||
|
assert_raises 'stub_called_with_at_most_times "uname" 4 -r' 0
|
||||||
|
assert_raises 'stub_called_with_at_most_times "uname" 3 -r' 0
|
||||||
|
|
||||||
|
# Returns 1 when stub has been called more than given number of times.
|
||||||
|
assert_raises 'stub_called_with_at_most_times "uname" 2 -r' 1
|
||||||
|
assert_raises 'stub_called_with_at_most_times "uname" 1 -r' 1
|
||||||
|
assert_raises 'stub_called_with_at_most_times "uname" 0 -r' 1
|
||||||
|
|
||||||
|
# Behaves as if stub has not been called when the stub doesn't exist.
|
||||||
|
assert_raises 'stub_called_with_at_most_times "top" 0' 0
|
||||||
|
assert_raises 'stub_called_with_at_most_times "top" 1' 0
|
||||||
|
|
||||||
|
# Teardown.
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_with_at_most_times()"
|
||||||
33
test/stub_called_with_exactly_times-test.sh
Executable file
33
test/stub_called_with_exactly_times-test.sh
Executable file
@@ -0,0 +1,33 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_with_exactly_times() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Setup.
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname -r
|
||||||
|
uname -r
|
||||||
|
uname -r -a
|
||||||
|
|
||||||
|
# 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.
|
||||||
|
assert_raises 'stub_called_with_exactly_times "uname" 4 -r' 1
|
||||||
|
assert_raises 'stub_called_with_exactly_times "uname" 3 -r' 1
|
||||||
|
assert_raises 'stub_called_with_exactly_times "uname" 1 -r' 1
|
||||||
|
assert_raises 'stub_called_with_exactly_times "uname" 0 -r' 1
|
||||||
|
|
||||||
|
# Behaves as if stub has not been called when the stub doesn't exist.
|
||||||
|
assert_raises 'stub_called_with_exactly_times "top" 0' 0
|
||||||
|
assert_raises 'stub_called_with_exactly_times "top" 1' 1
|
||||||
|
|
||||||
|
# Teardown.
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_with_exactly_times()"
|
||||||
43
test/stub_called_with_times-test.sh
Executable file
43
test/stub_called_with_times-test.sh
Executable file
@@ -0,0 +1,43 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_with_times() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Echoes 0 when stub doesn't exist.
|
||||||
|
assert 'stub_called_with_times "cowabunga-dude"' "0"
|
||||||
|
|
||||||
|
# Echoes how many times a stub has been called with given arguments
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname -r
|
||||||
|
uname -r
|
||||||
|
uname -r -a
|
||||||
|
uname -r -a
|
||||||
|
uname -r -a
|
||||||
|
assert 'stub_called_with_times "uname"' "1"
|
||||||
|
assert 'stub_called_with_times "uname" -r' "2"
|
||||||
|
assert 'stub_called_with_times "uname" -r -a' "3"
|
||||||
|
assert 'stub_called_with_times "uname" -a' "0"
|
||||||
|
|
||||||
|
# Keeps track of identical argument calls to different stubs.
|
||||||
|
stub "top"
|
||||||
|
top
|
||||||
|
top
|
||||||
|
top -r
|
||||||
|
top -r
|
||||||
|
top -r
|
||||||
|
top -r -a
|
||||||
|
assert 'stub_called_with_times "top"' "2"
|
||||||
|
assert 'stub_called_with_times "top" -r' "3"
|
||||||
|
assert 'stub_called_with_times "top" -r -a' "1"
|
||||||
|
assert 'stub_called_with_times "top" -a' "0"
|
||||||
|
|
||||||
|
# Teardown.
|
||||||
|
restore "uname"
|
||||||
|
restore "top"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_with_times()"
|
||||||
Reference in New Issue
Block a user