mirror of
https://github.com/jimeh/stub.sh.git
synced 2026-02-19 13:46:40 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 78f31d9bc9 | |||
| e5382ea2dc | |||
| 9a34d8fa90 | |||
| 8c18d4075c | |||
| 407fefcb34 | |||
| ef607e8eec | |||
| 49ec034829 | |||
| 5ac835a822 | |||
| 51f6c8776e |
70
README.md
70
README.md
@@ -63,14 +63,14 @@ Asserting stub has been called X times:
|
|||||||
source "stub.sh"
|
source "stub.sh"
|
||||||
my-uname() { uname; }
|
my-uname() { uname; }
|
||||||
stub_and_echo uname "FooBar"
|
stub_and_echo uname "FooBar"
|
||||||
stub_called_times uname #=> 0
|
stub_called_times uname #=> 0
|
||||||
stub_called_times uname 2 # returns 1 (error)
|
stub_called_exactly_times uname 2 # returns 1 (error)
|
||||||
my-uname #=> FooBar
|
my-uname #=> FooBar
|
||||||
stub_called_times uname #=> 1
|
stub_called_times uname #=> 1
|
||||||
stub_called_times uname 2 # returns 1 (error)
|
stub_called_exactly_times uname 2 # returns 1 (error)
|
||||||
my-uname #=> FooBar
|
my-uname #=> FooBar
|
||||||
stub_called_times uname #=> 2
|
stub_called_times uname #=> 2
|
||||||
stub_called_times uname 2 # returns 0 (success)
|
stub_called_exactly_times uname 2 # returns 0 (success)
|
||||||
restore uname
|
restore uname
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -80,35 +80,54 @@ Asserting stub has been called with specific attributes:
|
|||||||
source "stub.sh"
|
source "stub.sh"
|
||||||
my-uname() { uname $@; }
|
my-uname() { uname $@; }
|
||||||
stub_and_echo uname "FooBar"
|
stub_and_echo uname "FooBar"
|
||||||
stub_called_with uname -r # returns 1 (error)
|
|
||||||
my-uname -r -a #=> FooBar
|
|
||||||
stub_called_with uname -r -a # returns 1 (error)
|
stub_called_with uname -r -a # returns 1 (error)
|
||||||
stub_called_with uname -r # returns 1 (error)
|
stub_called_with uname -r # returns 1 (error)
|
||||||
|
my-uname -r -a #=> FooBar
|
||||||
|
stub_called_with uname -r -a # returns 0 (success)
|
||||||
|
stub_called_with uname -r # returns 1 (error)
|
||||||
my-uname -r #=> FooBar
|
my-uname -r #=> FooBar
|
||||||
|
stub_called_with uname -r -a # returns 0 (success)
|
||||||
stub_called_with uname -r # returns 0 (success)
|
stub_called_with uname -r # returns 0 (success)
|
||||||
restore uname
|
restore uname
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Asserting stub has been called X number of times with specific attributes:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
source "stub.sh"
|
||||||
|
my-uname() { uname $@; }
|
||||||
|
stub_and_echo uname "FooBar"
|
||||||
|
stub_called_with_times uname -r #=> 0
|
||||||
|
stub_called_with_exactly_times uname 2 -r # returns 1 (error)
|
||||||
|
my-uname -r #=> FooBar
|
||||||
|
stub_called_with_times uname -r #=> 1
|
||||||
|
stub_called_with_exactly_times uname 2 -r # returns 1 (error)
|
||||||
|
my-uname -r #=> FooBar
|
||||||
|
stub_called_with_times uname -r #=> 2
|
||||||
|
stub_called_with_exactly_times uname 2 -r # returns 0 (success)
|
||||||
|
stub_called_with_times uname -r -a #=> 0
|
||||||
|
restore uname
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Function Reference
|
## Function Reference
|
||||||
|
|
||||||
### Stubbing and Restoring
|
### Stubbing and Restoring
|
||||||
|
|
||||||
- **`stub`**: Basic stubbing command. Will echo a default message to STDOUT.
|
- **`stub`**: Basic stubbing command.
|
||||||
Arguments:
|
|
||||||
- `$1`: Name of command to stub
|
- `$1`: Name of command to stub
|
||||||
- `$2`: When set to "STDERR", echo to STDERR instead of STDOUT (optional).
|
- `$2`: (optional) When set to "STDERR" or "STDOUT", will echo a default
|
||||||
|
message to specified output. If no output is specified, nothing is
|
||||||
|
echoed.
|
||||||
- **`stub_and_echo`**: Stub given command and echo a custom string to STDOUT.
|
- **`stub_and_echo`**: Stub given command and echo a custom string to STDOUT.
|
||||||
Arguments:
|
|
||||||
- `$1`: Name of command to stub.
|
- `$1`: Name of command to stub.
|
||||||
- `$2`: String to echo when stub is called.
|
- `$2`: String to echo when stub is called.
|
||||||
- `$3`: When set to "STDERR", echo to STDERR instead of STDOUT (optional).
|
- `$3`: (optional) When set to "STDERR", echo to STDERR instead of STDOUT.
|
||||||
- **`stub_and_eval`**: Stub given command and execute custom commands via eval.
|
- **`stub_and_eval`**: Stub given command and execute custom commands via
|
||||||
Arguments:
|
eval.
|
||||||
- `$1`: Name of command to stub.
|
- `$1`: Name of command to stub.
|
||||||
- `$2`: String to eval when stub is called.
|
- `$2`: String to eval when stub is called.
|
||||||
- **`restore`**: Restores use of original binary/function that was stubbed.
|
- **`restore`**: Restores use of original binary/function that was stubbed.
|
||||||
Arguments:
|
|
||||||
- `$1`: Name of command to restore.
|
- `$1`: Name of command to restore.
|
||||||
|
|
||||||
### Stub Called Validation
|
### Stub Called Validation
|
||||||
@@ -136,26 +155,31 @@ restore uname
|
|||||||
- **`stub_called_with`**: Check if given stub has been called with specified
|
- **`stub_called_with`**: Check if given stub has been called with specified
|
||||||
arguments.
|
arguments.
|
||||||
- `$1`: Name of stub to check.
|
- `$1`: Name of stub to check.
|
||||||
- `$@`: All additional arguments are used to specify what stub was with.
|
- `$@`: Any/all additional arguments are used to specify what stub was
|
||||||
|
called with.
|
||||||
- **`stub_called_with_times`**: Find out how many times a stub has been
|
- **`stub_called_with_times`**: Find out how many times a stub has been
|
||||||
called with specified arguments.
|
called with specified arguments.
|
||||||
- `$1`: Name of stub to check.
|
- `$1`: Name of stub to check.
|
||||||
- `$@`: All additional arguments are used to specify what stub was with.
|
- `$@`: Any/all additional arguments are used to specify what stub was
|
||||||
|
called with.
|
||||||
- **`stub_called_with_exactly_times`**: Validate that stub has been called
|
- **`stub_called_with_exactly_times`**: Validate that stub has been called
|
||||||
with specified arguments exactly given number of times.
|
with specified arguments exactly given number of times.
|
||||||
- `$1`: Name of stub to check.
|
- `$1`: Name of stub to check.
|
||||||
- `$2`: Exact number of times stub should have been called.
|
- `$2`: Exact number of times stub should have been called.
|
||||||
- `$@`: All additional arguments are used to specify what stub was with.
|
- `$@`: Any/all additional arguments are used to specify what stub was
|
||||||
|
called with.
|
||||||
- **`stub_called_with_at_least_times`**: Validate that stub has been called
|
- **`stub_called_with_at_least_times`**: Validate that stub has been called
|
||||||
with specified arguments at least X number of times.
|
with specified arguments at least X number of times.
|
||||||
- `$1`: Name of stub to check.
|
- `$1`: Name of stub to check.
|
||||||
- `$2`: Minimum number of times stub should have been called.
|
- `$2`: Minimum number of times stub should have been called.
|
||||||
- `$@`: All additional arguments are used to specify what stub was with.
|
- `$@`: Any/all additional arguments are used to specify what stub was
|
||||||
|
called with.
|
||||||
- **`stub_called_with_at_most_times`**: Validate that stub has been called
|
- **`stub_called_with_at_most_times`**: Validate that stub has been called
|
||||||
with specified arguments no more than X number of times.
|
with specified arguments no more than X number of times.
|
||||||
- `$1`: Name of stub to check.
|
- `$1`: Name of stub to check.
|
||||||
- `$2`: Maximum number of times stub should have been called.
|
- `$2`: Maximum number of times stub should have been called.
|
||||||
- `$@`: All additional arguments are used to specify what stub was with.
|
- `$@`: Any/all additional arguments are used to specify what stub was
|
||||||
|
called with.
|
||||||
|
|
||||||
|
|
||||||
## Todo
|
## Todo
|
||||||
|
|||||||
39
stub.sh
39
stub.sh
@@ -1,5 +1,5 @@
|
|||||||
# !/usr/bin/env bash
|
# !/usr/bin/env bash
|
||||||
# stub.sh 1.0.0 - stubbing helpers for simplifying bash script tests.
|
# stub.sh 1.0.1 - stubbing helpers for simplifying bash script tests.
|
||||||
# https://github.com/jimeh/stub.sh
|
# https://github.com/jimeh/stub.sh
|
||||||
#
|
#
|
||||||
# (The MIT License)
|
# (The MIT License)
|
||||||
@@ -26,12 +26,12 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
# Public: Stub given command, echoing a default stub message.
|
# Public: Stub given command.
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of command to stub.
|
# - $1: Name of command to stub.
|
||||||
# - $2: When set to "STDERR", echo to STDERR instead of STDOUT.
|
# - $2: (optional) When set to "STDOUT", echo a default message to STDOUT.
|
||||||
# When set to "null", all output is redirected to /dev/null.
|
# When set to "STDERR", echo default message to STDERR.
|
||||||
#
|
#
|
||||||
# Echoes nothing.
|
# Echoes nothing.
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
@@ -49,7 +49,7 @@ stub() {
|
|||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of command to stub.
|
# - $1: Name of command to stub.
|
||||||
# - $2: String to echo when stub is called.
|
# - $2: String to echo when stub is called.
|
||||||
# - $3: When set to "STDERR", echo to STDERR instead of STDOUT.
|
# - $3: (optional) When set to "STDERR", echo to STDERR instead of STDOUT.
|
||||||
# When set to "null", all output is redirected to /dev/null.
|
# When set to "null", all output is redirected to /dev/null.
|
||||||
#
|
#
|
||||||
# Echoes nothing.
|
# Echoes nothing.
|
||||||
@@ -120,11 +120,11 @@ stub_called() {
|
|||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
# - $@: All additional arguments are used to specify what stub was called
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
# with.
|
# called with.
|
||||||
#
|
#
|
||||||
# Examples:
|
# Examples:
|
||||||
# stub "uname"
|
# stub uname
|
||||||
# uname
|
# uname
|
||||||
# uname -r -a
|
# uname -r -a
|
||||||
# stub_called_with uname # Returns 0 (success).
|
# stub_called_with uname # Returns 0 (success).
|
||||||
@@ -148,13 +148,6 @@ stub_called_with() {
|
|||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
# - $2: When specified, will check if stub was called exactly the given
|
|
||||||
# number of times (optional).
|
|
||||||
#
|
|
||||||
# Examples:
|
|
||||||
# stub_called_times "uname" # Echoes "2" if stub has been called twice.
|
|
||||||
# stub_called_times "uname" 2 # Returns 0 (success).
|
|
||||||
# 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.
|
||||||
@@ -230,8 +223,8 @@ stub_called_at_most_times() {
|
|||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
# - $@: All additional arguments are used to specify what stub was called
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
# with.
|
# called with.
|
||||||
#
|
#
|
||||||
# Echoes number of times stub has been called with given arguments.
|
# Echoes number of times stub has been called with given arguments.
|
||||||
# Return 0 (success).
|
# Return 0 (success).
|
||||||
@@ -261,8 +254,8 @@ stub_called_with_times() {
|
|||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
# - $2: Exact number of times stub has been called.
|
# - $2: Exact number of times stub has been called.
|
||||||
# - $@: All additional arguments are used to specify what stub was called
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
# with.
|
# called with.
|
||||||
#
|
#
|
||||||
# Echoes nothing.
|
# Echoes nothing.
|
||||||
# Returns 0 (success) if stub has been called at least the given number of
|
# Returns 0 (success) if stub has been called at least the given number of
|
||||||
@@ -284,8 +277,8 @@ stub_called_with_exactly_times() {
|
|||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
# - $2: Minimum required number of times stub has been called.
|
# - $2: Minimum required number of times stub has been called.
|
||||||
# - $@: All additional arguments are used to specify what stub was called
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
# with.
|
# called with.
|
||||||
#
|
#
|
||||||
# Echoes nothing.
|
# Echoes nothing.
|
||||||
# Returns 0 (success) if stub has been called at least the given number of
|
# Returns 0 (success) if stub has been called at least the given number of
|
||||||
@@ -307,8 +300,8 @@ stub_called_with_at_least_times() {
|
|||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
# - $2: Maximum allowed number of times stub has been called.
|
# - $2: Maximum allowed number of times stub has been called.
|
||||||
# - $@: All additional arguments are used to specify what stub was called
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
# with.
|
# called with.
|
||||||
#
|
#
|
||||||
# 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
|
||||||
|
|||||||
Reference in New Issue
Block a user