Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

automate error collection for assertions and expectations

  • Loading branch information...
commit 0d698d4f994400ca2486c4e6b2b36cbbac81dd4f 1 parent 79ef31b
@falconindy authored
Showing with 42 additions and 39 deletions.
  1. +21 −31 apron-assert
  2. +5 −4 apron-mock
  3. +16 −4 apron-testcase
View
52 apron-assert
@@ -1,70 +1,60 @@
#!/bin/bash
#
-# TODO: figure out an error collection mechanism?
+# these are written strangely because all APRON_assert_*
+# functions are run with an error trap. We want to make
+# sure the trap is triggered on error.
#
#
# Numeric assertions
#
APRON_assert_eq() {
- if ! (( $1 == $2 )); then
- printf 'Assertion failed: %d == %d\n' "$1" "$2"
- return 1
- fi
+ (( $1 == $2 ))
+ (( $? )) && printf 'Assertion failed: %d == %d\n' "$1" "$2"
}
APRON_assert_ne() {
- if ! (( $1 != $2 )); then
- printf 'Assertion failed: %d != %d\n' "$1" "$2"
- return 1
- fi
+ (( $1 != $2 ))
+ (( $? )) && printf 'Assertion failed: %d != %d\n' "$1" "$2"
}
APRON_assert_lt() {
- if ! (( $1 < $2 )); then
- printf 'Assertion failed: %d < %d\n' "$1" "$2"
- return 1
- fi
+ (( $1 < $2 ))
+ (( $? )) && printf 'Assertion failed: %d < %d\n' "$1" "$2"
}
APRON_assert_le() {
- if ! (( $1 <= $2 )); then
- printf 'Assertion failed: %d <= %d\n' "$1" "$2"
- return 1
- fi
+ (( $1 <= $2 ))
+ (( $? )) && printf 'Assertion failed: %d <= %d\n' "$1" "$2"
}
APRON_assert_gt() {
- if ! (( $1 > $2 )); then
- printf 'Assertion failed: %d > %d\n' "$1" "$2"
- return 1
- fi
+ (( $1 > $2 ))
+ (( $? )) && printf 'Assertion failed: %d > %d\n' "$1" "$2"
}
APRON_assert_ge() {
- if ! (( $1 >= $2 )); then
- printf 'Assertion failed: %d >= %d\n' "$1" "$2"
- return 1
- fi
+ (( $1 >= $2 ))
+ (( $? )) && printf 'Assertion failed: %d >= %d\n' "$1" "$2"
}
#
# String based assertions
#
APRON_assert_streq() {
- if [[ $1 != "$2" ]]; then
- printf 'Assertion failed: "%s" != "%s"\n' "$1" "$2"
- return 1
- fi
+ [[ $1 == "$2" ]]
+ (( $? )) && printf 'Assertion failed: "%s" != "%s"\n' "$1" "$2"
}
APRON_assert_in() {
for i in "${@:2}"; do
- [[ $1 = "$i" ]] && return
+ if [[ $1 = "$i" ]]; then
+ return 0
+ fi
done
+ false
printf 'Assertion failed: "%s" not found in: %s\n' "$1" "${*:2}"
- return 1
}
# vim: set ft=sh et ts=2 sw=2:
View
9 apron-mock
@@ -166,9 +166,10 @@ APRON_replay() {
exec {APRON_expect_fd}>>"$expectations_file"
- # invoke the user command, collecting expectations
- "$@"
- APRON_function_return=$?
+ # invoke the user command, collecting expectations.
+ # HACK: mask the return value so we avoid the error trap
+ APRON_function_return=0
+ "$@" || APRON_function_return=$?
_APRON_verify_expectations "$expectations_file"
local r=$?
@@ -183,7 +184,7 @@ APRON_replay() {
unset APRON_expectations
declare -Ag APRON_expectations=()
- return $r
+ (( r == 0 ))
}
#
View
20 apron-testcase
@@ -19,9 +19,16 @@ APRON_run_test() {
_APRON_push_PATH
fi
+ __r=0
+ trap '_APRON_fn_is_traced "$FUNCNAME" && (( ++__r ))' ERR
+ set -o errtrace
+
"$1" &>"$APRON_scratchdir/$APRON_testname.OUTPUT"
- APRON_tests_run["$1"]=$?
+ set +o errtrace
+ trap -- ERR
+
+ APRON_tests_run["$1"]=$__r
}
APRON_load() {
@@ -76,7 +83,6 @@ _APRON_unload() {
_APRON_pop_PATH
APRON_unregister_all
unset -f command_not_found_handle
- unset -v "${!APRON_[@]}" "${!_APRON[@]}"
APRON_state=$APRON_state_disabled
# must be run after we restore the PATH
@@ -88,7 +94,8 @@ _APRON_unload() {
_APRON_report() {
local t
- printf '==> Test results for %s:\n' "$APRON_testsuite"
+ printf '==> Test results for %s (%d tests):\n' \
+ "$APRON_testsuite" "${#APRON_tests_run[*]}"
# loop through twice so that failures are always
# last and stand out.
@@ -101,8 +108,9 @@ _APRON_report() {
for t in "${!APRON_tests_run[@]}"; do
if (( APRON_tests_run["$t"] != 0 )); then
- cat "$APRON_scratchdir/${t#TEST_}.OUTPUT"
_APRON_test_fail "${t#TEST_}"
+ cat "$APRON_scratchdir/${t#TEST_}.OUTPUT"
+ echo
(( ++_APRON_failcount ))
fi
done
@@ -116,6 +124,10 @@ _APRON_test_fail() {
printf ' -> %-69s \e[1;37m[\e[1;31m%4s\e[1;37m]\e[0m\n' "$1" 'FAIL'
}
+_APRON_fn_is_traced() {
+ [[ $1 = APRON_@(assert_*|replay) ]]
+}
+
declare -A APRON_tests_run
# vim: set ft=sh et ts=2 sw=2:
Please sign in to comment.
Something went wrong with that request. Please try again.