Added `be' support as an expectation handler

E.g. "to be silent".
stderr
Mike Gerwitz 2014-05-12 23:51:08 -04:00
parent dae4b2e255
commit bfc9971020
3 changed files with 70 additions and 7 deletions

View File

@ -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--succeed() { test "$1" -eq 0; }
_expect--fail() { test "$1" -ne 0; } _expect--fail() { test "$1" -ne 0; }

View File

@ -32,6 +32,9 @@ __INC_SPEC=1
source specstack source specstack
source expect-core 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 # 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 [ $# -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-assert-follow :expect to $(caller)
_sstack-pop _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" local -r assert="_expect--$type"
type "$assert" &>/dev/null \ type "$assert" &>/dev/null \
|| _bail "unknown expectation: \`$type'" $__spec_caller || _bail "unknown expectation: \`$type'" $__spec_caller
@ -172,10 +186,22 @@ to()
# output file, and all remaining arguments are said remainder clause; the # output file, and all remaining arguments are said remainder clause; the
# shift argument allows the implementation to vary without breaking BC so # shift argument allows the implementation to vary without breaking BC so
# long as the meaning of the shifted arguments do not change # long as the meaning of the shifted arguments do not change
$assert "$__spec_rexit" 3 "$__spec_errpath" "$@" <<< "$__spec_result" \ $assert "$__spec_rexit" $__SHIFTN "$__spec_errpath" "$@" \
|| fail "$expect_full" <<< "$__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 "$@"
} }

View File

@ -291,5 +291,34 @@ describe expect
'; to succeed '; to succeed
end end
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 end