diff --git a/src/expect-core b/src/expect-core index b2d2f49..ee40002 100644 --- a/src/expect-core +++ b/src/expect-core @@ -66,3 +66,8 @@ _expect--be() { _proxy-to "$@"; } _expect--succeed() { test "$1" -eq 0; } _expect--fail() { test "$1" -ne 0; } + +## +# Inverts the result of an expectation represented by the remainder clause +_expect--not() { ! _proxy-to "$@"; } + diff --git a/src/spec b/src/spec index 8cfda52..9913f11 100644 --- a/src/spec +++ b/src/spec @@ -162,7 +162,8 @@ to() _sstack-assert-follow :expect to $(caller) _sstack-pop - _handle-to "$@" || fail "$*" + __handle-to "$__spec_rexit" $__SHIFTN "$__spec_errpath" "$@" \ + || fail "$*" __spec_caller= } @@ -171,9 +172,18 @@ to() ## # Perform expectation assertion by invoking expectation handler # -# Will throw an error if the handler cannot be found. -_handle-to() +# Will throw an error if the handler cannot be found. Arguments are expected +# to be of the following form: +# +# <...N> <...remainder clause> +# +__handle-to() { + local -ri rexit="$1" + local -ri shiftn="$2" + local -r errpath="$( [ $shiftn -gt 2 ] && echo "$3" )" + shift "$shiftn" + local -r type="$1" shift @@ -186,23 +196,16 @@ _handle-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" $__SHIFTN "$__spec_errpath" "$@" \ + $assert $rexit $__SHIFTN "$errpath" "$@" \ < <( echo -n "$__spec_result" ) } ## -# Proxies remainder clause to an expectation handler +# Alias for _handle-to # -# 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 "$2" - _handle-to "$@" -} +# Shows intent to proxy a call and allows proxy implementation to vary. +_proxy-to() { __handle-to "$@"; } ## diff --git a/test/test-expect-core b/test/test-expect-core index bd06b78..a57f786 100644 --- a/test/test-expect-core +++ b/test/test-expect-core @@ -63,6 +63,19 @@ describe fail end +describe not + it will invert the result of an expectation + # exit code of 1, so normally `succeed' would fail + expect _expect--not 1 2 succeed + to succeed + + # exit code of 0, so normally `succeed' would succeed + expect _expect--not 0 2 succeed + to fail + end +end + + describe output it will default to asserting against stdout expect _expect--output 0 2 "test string" <<< "test string"