diff --git a/bin/shpec b/bin/shpec index 694383d..ec51db7 100755 --- a/bin/shpec +++ b/bin/shpec @@ -36,11 +36,30 @@ end_describe() { # any identifier as a function name. stub_command() { + local func_decl="$(get_function_declaration "$1")" + [ -n "${func_decl}" ] && eval "_shpec____${func_decl}" + _shpec_stub_body="${2:-:}" eval "$1() { ${_shpec_stub_body}; }" } -unstub_command() { unset -f "$1"; } +unstub_command() { + unset -f "$1"; + local func_decl="$(get_function_declaration _shpec____"$1")" + [ -n "${func_decl}" ] && { + local reverted="$(echo "${func_decl}" | sed 's/_shpec____//')" + eval "${reverted}" + } +} + +get_function_declaration() +{ + if [ "${SHELL}" = "dash" ]; then + type "$1" | tail -n +2 + else + typeset -f "$1" + fi +} it() { : $((_shpec_indent += 1)) diff --git a/shpec/shpec_shpec.sh b/shpec/shpec_shpec.sh index 6f54d0d..e6bdc00 100644 --- a/shpec/shpec_shpec.sh +++ b/shpec/shpec_shpec.sh @@ -86,6 +86,15 @@ line' assert equal "$(curl)" "stubbed body" unstub_command "curl" end + + it "prevents loosing stubbed function" + local expected="assert double" + stub_command "assert" "echo '${expected}'" + local result="$(assert)" + unstub_command "assert" + + assert equal "${expected}" "${result}" + end end describe "testing files"