Skip to content

Instantly share code, notes, and snippets.

@rocky
Created May 15, 2011 23:35
Show Gist options
  • Save rocky/973684 to your computer and use it in GitHub Desktop.
Save rocky/973684 to your computer and use it in GitHub Desktop.
Changes to Kate Ward's shunit2 to handle multiple files and aggregated tests
/usr/bin/zsh /src/external-vcs/zshdb/test/unit/shunit2 test-action.sh test-break.sh test-cmd-complete.sh test-columns.sh test-dbg-opts.sh test-eval.sh test-examine.sh test-file.sh test-filecache.sh test-fns.sh test-get-sourceline.sh test-journal.sh test-lib-shell.sh test-msg.sh test-pre.sh test-run.sh test-tty.sh
test_action
test_breakpoint
test_cmd_complete
test_columnized
test_dbg_opts
test_eval_subst
test_examine
test_file_glob_filename
test_file_adjust_filename
test_file_resolve_expand_filename
test_filecache_readin_maxline
test_filecache_check_line
test_fns_copies
test_fns_esc_dq_function
test_fns_esc_dbg_enter_return
test_fns_defined
test_fns_parse_linespec
test_fns_set_q
test_fns_split
test_get_source_line
test_get_source_line_with_spaces
test_journal
test_lib_shell_filter_typeset
test_msg
test_msg_undefined
test_pre_expand_filename
test_pre_do_show_version
test_run_not_running
test_tty
Ran 29 tests. 185 assert(s): 185 passed, 0 failed, 0 skipped.
OK
diff --git a/test/unit/shunit2 b/test/unit/shunit2
index 9ec6c88..c7af932 100644
--- a/test/unit/shunit2
+++ b/test/unit/shunit2
@@ -815,11 +815,15 @@ _shunit_generateReport()
&& _shunit_ok_=${SHUNIT_FALSE}
echo
- if [ ${__shunit_testsTotal} -eq 1 ]; then
- echo "Ran ${__shunit_testsTotal} test."
+ if [ -z $__shunit_terse ] ; then
+ _shunit_assert_stat=" $__shunit_assertsTotal assert(s): $__shunit_assertsPassed passed, $__shunit_assertsFailed failed, $__shunit_assertsSkipped skipped."
else
- echo "Ran ${__shunit_testsTotal} tests."
+ _shunit_assert_stat=''
fi
+ if [ ${__shunit_testsTotal} -ne 1 ]; then
+ plural='s'
+ fi
+ echo "Ran ${__shunit_testsTotal} test${plural}.${_shunit_assert_stat}"
_shunit_failures_=''
_shunit_skipped_=''
@@ -843,7 +847,7 @@ _shunit_generateReport()
echo ${_shunit_msg_}
__shunit_reportGenerated=${SHUNIT_TRUE}
- unset _shunit_failures_ _shunit_msg_ _shunit_ok_ _shunit_skipped_
+ unset _shunit_failures_ _shunit_msg_ _shunit_ok_ _shunit_skipped_ _shunit_assert_stat
}
# Test for whether a function should be skipped.
$ make check
make shunit2
make[1]: Entering directory `/src/external-vcs/zshdb/test/unit'
make[1]: Nothing to be done for `shunit2'.
make[1]: Leaving directory `/src/external-vcs/zshdb/test/unit'
make check-TESTS
make[1]: Entering directory `/src/external-vcs/zshdb/test/unit'
test_action
Ran 1 test.
OK
PASS: test-action.sh
test_alias
Ran 1 test.
OK
PASS: test-alias.sh
test_breakpoint
Ran 1 test.
OK
PASS: test-break.sh
test_columnized
Ran 1 test.
OK
PASS: test-columns.sh
test_opts
Ran 1 test.
OK
PASS: test-dbg-opts.sh
test_examine
Ran 1 test.
OK
PASS: test-examine.sh
test_eval_subst
Ran 1 test.
OK
PASS: test-eval.sh
test_glob_filename
test_adjust_filename
test_resolve_expand_filename
Ran 3 tests.
OK
PASS: test-file.sh
test_readin_maxline
test_check_line
Ran 2 tests.
OK
PASS: test-filecache.sh
test_copies
test_esc_dq_function
test_esc_dbg_enter_return
test_defined
test_parse_linespec
test_set_q
test_split
Ran 7 tests.
OK
PASS: test-fns.sh
test_frame
test_frame_adjust
->0 in file `test-frame.sh' at line 30
(test-frame.sh:30):
->1 test_frame_adjust called from file `shunit2' at line 973
(shunit2:973):
->2 (eval) called from file `shunit2' at line 973
(shunit2:973):
->3 _shunit_execSuite called from file `shunit2' at line 1101
(shunit2:1101):
->4 shunit2 called from file `test-frame.sh' at line 79
(test-frame.sh:79):
->3 _shunit_execSuite called from file `shunit2' at line 1101
(shunit2:1101):
->2 (eval) called from file `shunit2' at line 973
(shunit2:973):
Ran 2 tests.
OK
PASS: test-frame.sh
test_get_source_line
test_get_source_line_with_spaces
Ran 2 tests.
OK
PASS: test-get-sourceline.sh
test_journal
Ran 1 test.
OK
PASS: test-journal.sh
test_lib_filter_typeset
Ran 1 test.
OK
PASS: test-lib-shell.sh
test_msg
test_undefined
Ran 2 tests.
OK
PASS: test-msg.sh
test_expand_filename
test_do_show_version
Ran 2 tests.
OK
PASS: test-pre.sh
test_not_running
Ran 1 test.
OK
PASS: test-run.sh
test_is_unsetopt
test_create_unsetopt
test_save_restore_IFS_PS4
test_save_restore_ksharrays
test_save_restore_shwordsplit
Ran 5 tests.
OK
PASS: test-save-restore.sh
test_help_set_onoff
Ran 1 test.
OK
PASS: test-setshow.sh
test_sort
Ran 1 test.
OK
PASS: test-sort.sh
test_tty
Ran 1 test.
OK
PASS: test-tty.sh
test_is_alias
test_is_function
test_is_integer
Ran 3 tests.
OK
PASS: test-validate.sh
===================
All 22 tests passed
===================
--- a/test/unit/shunit2
+++ b/test/unit/shunit2
@@ -31,11 +31,12 @@ _shunit_fatal() { echo "shunit2:FATAL $@" >&2; exit ${SHUNIT_ERROR}; }
# specific shell checks
if [ -n "${ZSH_VERSION:-}" ]; then
setopt |grep "^shwordsplit$" >/dev/null
if [ $? -ne ${SHUNIT_TRUE} ]; then
_shunit_fatal 'zsh shwordsplit option is required for proper operation'
fi
- if [ -z "${SHUNIT_PARENT:-}" ]; then
+ if [ -z "${SHUNIT_PARENT:-}" ] && [ $# -eq 0 ] ; then
_shunit_fatal "zsh does not pass \$0 through properly. please declare \
\"SHUNIT_PARENT=\$0\" before calling shUnit2"
fi
@@ -814,12 +815,16 @@ _shunit_generateReport()
-o ${__shunit_testSuccess} -eq ${SHUNIT_FALSE} ] \
&& _shunit_ok_=${SHUNIT_FALSE}
- echo
- if [ ${__shunit_testsTotal} -eq 1 ]; then
- echo "Ran ${__shunit_testsTotal} test."
+ if [ -z $__shunit_terse ] ; then
+ _shunit_assert_stat=" $__shunit_assertsTotal assert(s): $__shunit_assertsPassed passed, $__shunit_assertsFailed failed, $__shunit_assertsSkipped skipped."
else
- echo "Ran ${__shunit_testsTotal} tests."
+ echo
+ _shunit_assert_stat=''
fi
+ if [ ${__shunit_testsTotal} -ne 1 ]; then
+ plural='s'
+ fi
+ echo "Ran ${__shunit_testsTotal} test${plural}.${_shunit_assert_stat}"
_shunit_failures_=''
_shunit_skipped_=''
@@ -839,11 +844,10 @@ _shunit_generateReport()
_shunit_msg_="${_shunit_msg_})"
fi
- echo
echo ${_shunit_msg_}
__shunit_reportGenerated=${SHUNIT_TRUE}
- unset _shunit_failures_ _shunit_msg_ _shunit_ok_ _shunit_skipped_
+ unset _shunit_failures_ _shunit_msg_ _shunit_ok_ _shunit_skipped_ _shunit_assert_stat
}
# Test for whether a function should be skipped.
@@ -975,21 +979,35 @@ _shunit_extractTestFunctions()
unset _shunit_regex_ _shunit_script_
}
+_shunit_runTests()
+{
+ # execute the oneTimeSetUp function (if it exists)
+ oneTimeSetUp
+
+ # execute the suite function defined in the parent test script
+ # deprecated as of 2.1.0
+ suite
+
+ # if no suite function was defined, dynamically build a list of functions
+ if [ -z "${__shunit_suite}" ]; then
+ shunit_funcs_=`_shunit_extractTestFunctions "${__shunit_script}"`
+ for shunit_func_ in ${shunit_funcs_}; do
+ suite_addTest ${shunit_func_}
+ done
+ fi
+ unset shunit_func_ shunit_funcs_
+
+ # execute the tests
+ _shunit_execSuite
+
+ # execute the oneTimeTearDown function (if it exists)
+ oneTimeTearDown
+}
+
#------------------------------------------------------------------------------
# main
#
-# determine the operating mode
-if [ $# -eq 0 ]; then
- __shunit_script=${__SHUNIT_PARENT}
- __shunit_mode=${__SHUNIT_MODE_SOURCED}
-else
- __shunit_script=$1
- [ -r "${__shunit_script}" ] || \
- _shunit_fatal "unable to read from ${__shunit_script}"
- __shunit_mode=${__SHUNIT_MODE_STANDALONE}
-fi
-
# create a temporary storage location
__shunit_tmpDir=`_shunit_mktempDir`
@@ -1013,32 +1031,21 @@ PATH="${__shunit_tmpDir}:${PATH}"
noexec 2>/dev/null || _shunit_fatal \
'please declare TMPDIR with path on partition with exec permission'
-# we must manually source the tests in standalone mode
-if [ "${__shunit_mode}" = "${__SHUNIT_MODE_STANDALONE}" ]; then
- . "`_shunit_prepForSourcing \"${__shunit_script}\"`"
-fi
-
-# execute the oneTimeSetUp function (if it exists)
-oneTimeSetUp
-
-# execute the suite function defined in the parent test script
-# deprecated as of 2.1.0
-suite
-
-# if no suite function was defined, dynamically build a list of functions
-if [ -z "${__shunit_suite}" ]; then
- shunit_funcs_=`_shunit_extractTestFunctions "${__shunit_script}"`
- for shunit_func_ in ${shunit_funcs_}; do
- suite_addTest ${shunit_func_}
+# determine the operating mode
+if [ $# -eq 0 ]; then
+ __shunit_script=${__SHUNIT_PARENT}
+ __shunit_mode=${__SHUNIT_MODE_SOURCED}
+ _shunit_runTests
+else
+ __shunit_mode=${__SHUNIT_MODE_STANDALONE}
+ for __shunit_script in $@ ; do
+ __shunit_suite=''
+ [ -r "${__shunit_script}" ] || \
+ _shunit_fatal "unable to read from ${__shunit_script}"
+ . "`_shunit_prepForSourcing \"${__shunit_script}\"`"
+ _shunit_runTests
done
fi
-unset shunit_func_ shunit_funcs_
-
-# execute the tests
-_shunit_execSuite
-
-# execute the oneTimeTearDown function (if it exists)
-oneTimeTearDown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment