From 98c14c36e3616aa7c1102be6211e642bb9d44fb5 Mon Sep 17 00:00:00 2001 From: Eric Villard Date: Wed, 27 Apr 2016 11:08:58 +0200 Subject: [PATCH 1/5] add shpec assert function stub utility it allows to stub the assert function and get an expected result it is useful to test assert result when used in a function body (eq. in a matcher body) Signed-off-by: Eric Villard --- bin/shpec | 24 ++++++++++++++++++++++++ shpec/shpec_shpec.sh | 9 +++++++++ 2 files changed, 33 insertions(+) diff --git a/bin/shpec b/bin/shpec index 694383d..f2d9df5 100755 --- a/bin/shpec +++ b/bin/shpec @@ -42,6 +42,30 @@ stub_command() { unstub_command() { unset -f "$1"; } +get_function_declaration() +{ + if [ "${SHELL}" = "dash" ]; then + type "$1" | tail -n +2 + else + typeset -f "$1" + fi +} + +stub_assert() +{ + eval "_shpec____$(get_function_declaration assert)" + stub_command "assert" "$1" +} + +unstub_assert() +{ + unstub_command "assert" + local definition="$(get_function_declaration _shpec____assert)" + local reverted="${definition/_shpec____/}" + + eval "${reverted}" +} + it() { : $((_shpec_indent += 1)) : $((_shpec_examples += 1)) diff --git a/shpec/shpec_shpec.sh b/shpec/shpec_shpec.sh index 6f54d0d..eac77e9 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 "stubs shpec assert function" + local expected="assert double" + stub_assert "echo '${expected}'" + local result="$(assert)" + unstub_assert + + assert equal "${expected}" "${result}" + end end describe "testing files" From 7a0d2549bb1d46c30b7f281a525a1fe751263f9a Mon Sep 17 00:00:00 2001 From: Eric Villard Date: Wed, 27 Apr 2016 11:20:53 +0200 Subject: [PATCH 2/5] fix string replacement for sh and dash using sed Signed-off-by: Eric Villard --- bin/shpec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/shpec b/bin/shpec index f2d9df5..2351a6f 100755 --- a/bin/shpec +++ b/bin/shpec @@ -61,7 +61,7 @@ unstub_assert() { unstub_command "assert" local definition="$(get_function_declaration _shpec____assert)" - local reverted="${definition/_shpec____/}" + local reverted="$(echo "${definition}" | sed 's/_shpec____//')" eval "${reverted}" } From ce77361885187bf34d1e3c2188c36fad2859de48 Mon Sep 17 00:00:00 2001 From: Eric Villard Date: Tue, 3 May 2016 20:34:39 +0200 Subject: [PATCH 3/5] move backup of existing funcs to [un]stub_command Signed-off-by: Eric Villard --- bin/shpec | 27 +++++++++++---------------- shpec/shpec_shpec.sh | 6 +++--- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/bin/shpec b/bin/shpec index 2351a6f..9227710 100755 --- a/bin/shpec +++ b/bin/shpec @@ -36,11 +36,21 @@ 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="${func_decl/_shpec____/}" + eval "${reverted}" + } +} get_function_declaration() { @@ -51,21 +61,6 @@ get_function_declaration() fi } -stub_assert() -{ - eval "_shpec____$(get_function_declaration assert)" - stub_command "assert" "$1" -} - -unstub_assert() -{ - unstub_command "assert" - local definition="$(get_function_declaration _shpec____assert)" - local reverted="$(echo "${definition}" | sed 's/_shpec____//')" - - eval "${reverted}" -} - it() { : $((_shpec_indent += 1)) : $((_shpec_examples += 1)) diff --git a/shpec/shpec_shpec.sh b/shpec/shpec_shpec.sh index eac77e9..e6bdc00 100644 --- a/shpec/shpec_shpec.sh +++ b/shpec/shpec_shpec.sh @@ -87,11 +87,11 @@ line' unstub_command "curl" end - it "stubs shpec assert function" + it "prevents loosing stubbed function" local expected="assert double" - stub_assert "echo '${expected}'" + stub_command "assert" "echo '${expected}'" local result="$(assert)" - unstub_assert + unstub_command "assert" assert equal "${expected}" "${result}" end From 8ce62756a8411fa06595af4b5a057e68b6cb6377 Mon Sep 17 00:00:00 2001 From: Eric Villard Date: Tue, 3 May 2016 20:48:01 +0200 Subject: [PATCH 4/5] fix string replacement for sh for the second time... :( Signed-off-by: Eric Villard --- bin/shpec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/shpec b/bin/shpec index 9227710..83922bf 100755 --- a/bin/shpec +++ b/bin/shpec @@ -47,7 +47,7 @@ unstub_command() { unset -f "$1"; local func_decl="$(get_function_declaration _shpec____"$1")" [ -n "${func_decl}" ] && { - local reverted="${func_decl/_shpec____/}" + local reverted=""$(echo "${func_decl}" | sed 's/_shpec____//')"" eval "${reverted}" } } From b83175403cf1b53418c8508fdced283153ea588a Mon Sep 17 00:00:00 2001 From: Eric Villard Date: Tue, 3 May 2016 20:51:45 +0200 Subject: [PATCH 5/5] remove extra double quotes due to IDE autocomplete Signed-off-by: Eric Villard --- bin/shpec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/shpec b/bin/shpec index 83922bf..ec51db7 100755 --- a/bin/shpec +++ b/bin/shpec @@ -47,7 +47,7 @@ unstub_command() { unset -f "$1"; local func_decl="$(get_function_declaration _shpec____"$1")" [ -n "${func_decl}" ] && { - local reverted=""$(echo "${func_decl}" | sed 's/_shpec____//')"" + local reverted="$(echo "${func_decl}" | sed 's/_shpec____//')" eval "${reverted}" } }