forked from ztombol/bats-support
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
source "$(dirname "${BASH_SOURCE[0]}")/src/output.bash" | ||
source "$(dirname "${BASH_SOURCE[0]}")/src/error.bash" | ||
source "$(dirname "${BASH_SOURCE[0]}")/src/lang.bash" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# | ||
# bats-util - Various auxiliary functions for Bats | ||
# | ||
# Written in 2016 by Zoltan Tombol <zoltan dot tombol at gmail dot com> | ||
# | ||
# To the extent possible under law, the author(s) have dedicated all | ||
# copyright and related and neighboring rights to this software to the | ||
# public domain worldwide. This software is distributed without any | ||
# warranty. | ||
# | ||
# You should have received a copy of the CC0 Public Domain Dedication | ||
# along with this software. If not, see | ||
# <http://creativecommons.org/publicdomain/zero/1.0/>. | ||
# | ||
|
||
# | ||
# lang.bash | ||
# --------- | ||
# | ||
# Bash language and execution related functions. Used by public helper | ||
# functions. | ||
# | ||
|
||
# Check whether the calling function was called from a given function. | ||
# | ||
# By default, direct invocation is checked. The function succeeds if the | ||
# calling function was called directly from the given function. In other | ||
# words, if the given function is the next element on the call stack. | ||
# | ||
# When `--indirect' is specified, indirect invocation is checked. The | ||
# function succeeds if the calling function was called from the given | ||
# function with any number of intermediate calls. In other words, if the | ||
# given function can be found somewhere on the call stack. | ||
# | ||
# Direct invocation is a form of indirect invocation with zero | ||
# intermediate calls. | ||
# | ||
# Globals: | ||
# FUNCNAME | ||
# Options: | ||
# -i, --indirect - check indirect invocation | ||
# Arguments: | ||
# $1 - calling function's name | ||
# Returns: | ||
# 0 - current function was called from the given function | ||
# 1 - otherwise | ||
batslib_is_caller() { | ||
local -i is_mode_direct=1 | ||
|
||
# Handle options. | ||
while (( $# > 0 )); do | ||
case "$1" in | ||
-i|--indirect) is_mode_direct=0; shift ;; | ||
--) shift; break ;; | ||
*) break ;; | ||
esac | ||
done | ||
|
||
# Arguments. | ||
local -r func="$1" | ||
|
||
# Check call stack. | ||
if (( is_mode_direct )); then | ||
[[ $func == "${FUNCNAME[2]}" ]] && return 0 | ||
else | ||
local -i depth | ||
for (( depth=2; depth<${#FUNCNAME[@]}; ++depth )); do | ||
[[ $func == "${FUNCNAME[$depth]}" ]] && return 0 | ||
done | ||
fi | ||
|
||
return 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/usr/bin/env bats | ||
|
||
load 'test_helper' | ||
|
||
|
||
# Test functions | ||
test_func_lvl_2() { | ||
test_func_lvl_1 "$@" | ||
} | ||
|
||
test_func_lvl_1() { | ||
test_func_lvl_0 "$@" | ||
} | ||
|
||
test_func_lvl_0() { | ||
batslib_is_caller "$@" | ||
} | ||
|
||
|
||
# | ||
# Direct invocation | ||
# | ||
|
||
# Interface | ||
@test 'batslib_is_caller() <function>: returns 0 if the current function was called directly from <function>' { | ||
run test_func_lvl_1 test_func_lvl_1 | ||
[ "$status" -eq 0 ] | ||
[ "${#lines[@]}" -eq 0 ] | ||
} | ||
|
||
@test 'batslib_is_caller() <function>: returns 1 if the current function was not called directly from <function>' { | ||
run test_func_lvl_0 test_func_lvl_1 | ||
[ "$status" -eq 1 ] | ||
[ "${#lines[@]}" -eq 0 ] | ||
} | ||
|
||
# Correctness | ||
@test 'batslib_is_caller() <function>: the current function does not appear on the call stack' { | ||
run test_func_lvl_0 test_func_lvl_0 | ||
[ "$status" -eq 1 ] | ||
[ "${#lines[@]}" -eq 0 ] | ||
} | ||
|
||
|
||
# | ||
# Indirect invocation | ||
# | ||
|
||
# Options | ||
test_i_indirect() { | ||
run test_func_lvl_2 "$@" | ||
[ "$status" -eq 0 ] | ||
[ "${#lines[@]}" -eq 0 ] | ||
} | ||
|
||
@test 'batslib_is_caller() -i <function>: enables indirect checking' { | ||
test_i_indirect -i test_func_lvl_2 | ||
} | ||
|
||
@test 'batslib_is_caller() --indirect <function>: enables indirect checking' { | ||
test_i_indirect --indirect test_func_lvl_2 | ||
} | ||
|
||
# Interface | ||
@test 'batslib_is_caller() --indirect <function>: returns 0 if the current function was called indirectly from <function>' { | ||
run test_func_lvl_2 --indirect test_func_lvl_2 | ||
[ "$status" -eq 0 ] | ||
[ "${#lines[@]}" -eq 0 ] | ||
} | ||
|
||
@test 'batslib_is_caller() --indirect <function>: returns 1 if the current function was not called indirectly from <function>' { | ||
run test_func_lvl_1 --indirect test_func_lvl_2 | ||
[ "$status" -eq 1 ] | ||
[ "${#lines[@]}" -eq 0 ] | ||
} | ||
|
||
# Correctness | ||
@test 'batslib_is_caller() --indirect <function>: direct invocation is a special case of indirect invocation with zero intermediate calls' { | ||
run test_func_lvl_1 --indirect test_func_lvl_1 | ||
[ "$status" -eq 0 ] | ||
[ "${#lines[@]}" -eq 0 ] | ||
} | ||
|
||
@test 'batslib_is_caller() --indirect <function>: the current function does not appear on the call stack' { | ||
run test_func_lvl_0 --indirect test_func_lvl_0 | ||
[ "$status" -eq 1 ] | ||
[ "${#lines[@]}" -eq 0 ] | ||
} |