From bfc99710209c43349df255571e9c53075b30ffbc Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 12 May 2014 23:51:08 -0400 Subject: [PATCH] Added `be' support as an expectation handler E.g. "to be silent". --- src/expect-core | 8 ++++++++ src/spec | 40 +++++++++++++++++++++++++++++++++------- test/test-spec | 29 +++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/src/expect-core b/src/expect-core index 1d24fb5..5f2adfa 100644 --- a/src/expect-core +++ b/src/expect-core @@ -56,6 +56,14 @@ __chk-nshift() } +## +# Purely gramatical to make certain expectations flow more naturally when +# spoken +# +# For example, "to be silent". +_expect--be() { _proxy-to "$@"; } + + _expect--succeed() { test "$1" -eq 0; } _expect--fail() { test "$1" -ne 0; } diff --git a/src/spec b/src/spec index 5325cf2..7449a9f 100644 --- a/src/spec +++ b/src/spec @@ -32,6 +32,9 @@ __INC_SPEC=1 source specstack source expect-core +# number of internal arguments before remainder clause +declare -ir __SHIFTN=3 + ## # Attempts to make tempoary path in /dev/shm, falling back to default @@ -156,13 +159,24 @@ to() [ $# -gt 0 ] || _bail "missing assertion string for \`to'" $__spec_caller - local -r expect_full="$*" - local -r type="$1" - shift - _sstack-assert-follow :expect to $(caller) _sstack-pop + _handle-to "$@" || fail "$*" + + __spec_caller= +} + + +## +# Perform expectation assertion by invoking expectation handler +# +# Will throw an error if the handler cannot be found. +_handle-to() +{ + local -r type="$1" + shift + local -r assert="_expect--$type" type "$assert" &>/dev/null \ || _bail "unknown expectation: \`$type'" $__spec_caller @@ -172,10 +186,22 @@ to() # output file, and all remaining arguments are said remainder clause; the # shift argument allows the implementation to vary without breaking BC so # long as the meaning of the shifted arguments do not change - $assert "$__spec_rexit" 3 "$__spec_errpath" "$@" <<< "$__spec_result" \ - || fail "$expect_full" + $assert "$__spec_rexit" $__SHIFTN "$__spec_errpath" "$@" \ + <<< "$__spec_result" +} - __spec_caller= + +## +# Proxies remainder clause to an expectation handler +# +# This is different than simply invoking `_handle-to` in that the former +# expects to be called with arguments from an existing handler call (that +# is, in need of shifting to access the remainder clause), where as the +# latter assumes that its arguments are *only* the intended remainder clause +_proxy-to() +{ + shift $__SHIFTN + _handle-to "$@" } diff --git a/test/test-spec b/test/test-spec index a25525a..8873f8f 100644 --- a/test/test-spec +++ b/test/test-spec @@ -291,5 +291,34 @@ describe expect '; to succeed end end + + + describe be + it cannot stand alone + expect test-run <<< ' + describe foo + it should fail + expect true + to be + end + end + '; to fail + end + + it processes arguments as if it did not exist + expect test-run <<< ' + expected="foo bar baz" + _expect--awesome() { shift "$2"; test "$*" == "$expected"; } + + describe foo + it should succeed + expect true + # ignore the fact that this sounds awkward + to be awesome $expected + end + end + '; to succeed + end + end end