From: Christian Babeux Date: Tue, 9 Apr 2013 01:15:06 +0000 (-0400) Subject: Add testing infrastructure X-Git-Url: https://git.efficios.com/?a=commitdiff_plain;h=5a8b59500faaa8cd13a69d201cdfbad2ee1c5d98;p=ctf-testsuite.git Add testing infrastructure Add the testing infrastructure using the TAP test format and the Perl prove utility as the tests runner. --- diff --git a/README b/README new file mode 100644 index 0000000..fb37649 --- /dev/null +++ b/README @@ -0,0 +1,7 @@ +ctf-testsuite +============= + +The Common Trace Format (CTF) testsuite is intended to validate the conformance of CTF readers. + +To run the testsuite simply export CTF_READER_BIN="$YOUR_CTF_READER" and use the run.sh script found in the tests/ folder. + diff --git a/README.md b/README.md deleted file mode 100644 index b8b057e..0000000 --- a/README.md +++ /dev/null @@ -1,4 +0,0 @@ -ctf-testsuite -============= - -The Common Trace Format (CTF) testsuite is intended to validate the conformance of CTF readers. \ No newline at end of file diff --git a/tests/fail/list b/tests/fail/list new file mode 100644 index 0000000..26d25f9 --- /dev/null +++ b/tests/fail/list @@ -0,0 +1,26 @@ +./test.sh types01/ +./test.sh types02/ +./test.sh types03/ +./test.sh types04/ +./test.sh types05/ +./test.sh types06/ +./test.sh types07/ +./test.sh types08/ +./test.sh types09/ +./test.sh types10/ +./test.sh types11/ +./test.sh types12/ +./test.sh types13/ +./test.sh types14/ +./test.sh types15/ +./test.sh types16/ +./test.sh types17/ +./test.sh types18/ +./test.sh types19/ +./test.sh types20/ +./test.sh types21/ +./test.sh types22/ +./test.sh types23/ +./test.sh types24/ +./test.sh types25/ +./test.sh types26/ diff --git a/tests/fail/run.sh b/tests/fail/run.sh new file mode 100755 index 0000000..d43ea19 --- /dev/null +++ b/tests/fail/run.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +prove --exec '' --merge - < list diff --git a/tests/fail/test.sh b/tests/fail/test.sh new file mode 100755 index 0000000..d3a61e0 --- /dev/null +++ b/tests/fail/test.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# +# Copyright (C) - 2013 Christian Babeux +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License, version 2 only, as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 51 +# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +CURDIR=$(dirname $0) +UTILS_DIR=$CURDIR/../../utils/ + +source $UTILS_DIR/tap/tap.sh + +NUM_TESTS=1 + +plan_tests $NUM_TESTS + +$CTF_READER_BIN $CTF_READER_OPTS $1 >/dev/null 2>&1 + +result=$? + +is $result 1 $1 diff --git a/tests/fail/types01/metadata b/tests/fail/types01/metadata new file mode 100644 index 0000000..f734c50 --- /dev/null +++ b/tests/fail/types01/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: size = 0 */ +typealias integer { size = 0; align = 8; signed = false; } := uint0_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types02/metadata b/tests/fail/types02/metadata new file mode 100644 index 0000000..64caaec --- /dev/null +++ b/tests/fail/types02/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: Alignment values must be power of two. */ +typealias integer { size = 8; align = 17; signed = false; } := uint0_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types03/metadata b/tests/fail/types03/metadata new file mode 100644 index 0000000..66fdd53 --- /dev/null +++ b/tests/fail/types03/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: invalid byte ordering. */ +typealias integer { size = 8; align = 8; signed = false; byte_order = iroquois; } := uint8_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types04/metadata b/tests/fail/types04/metadata new file mode 100644 index 0000000..c3939db --- /dev/null +++ b/tests/fail/types04/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: invalid signed. */ +typealias integer { size = 8; align = 8; signed = svp; } := uint8_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types05/metadata b/tests/fail/types05/metadata new file mode 100644 index 0000000..6d87f56 --- /dev/null +++ b/tests/fail/types05/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: negative size. */ +typealias integer { size = -8; align = 8; signed = false; } := uint8_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types06/metadata b/tests/fail/types06/metadata new file mode 100644 index 0000000..2d60c33 --- /dev/null +++ b/tests/fail/types06/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: negative align. */ +typealias integer { size = 8; align = -8; signed = false; } := uint8_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types07/metadata b/tests/fail/types07/metadata new file mode 100644 index 0000000..4b97a9c --- /dev/null +++ b/tests/fail/types07/metadata @@ -0,0 +1,16 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + + +/* FAIL: size as string. */ +typealias integer { size = "8"; align = 8; signed = false; } := uint8_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types08/metadata b/tests/fail/types08/metadata new file mode 100644 index 0000000..c2fd4af --- /dev/null +++ b/tests/fail/types08/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: align as string. */ +typealias integer { size = 8; align = "8"; signed = false; } := uint8_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types09/metadata b/tests/fail/types09/metadata new file mode 100644 index 0000000..7f5f142 --- /dev/null +++ b/tests/fail/types09/metadata @@ -0,0 +1,16 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + + +/* FAIL: signed as string. */ +typealias integer { size = 8; align = 8; signed = "false"; } := uint8_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types10/metadata b/tests/fail/types10/metadata new file mode 100644 index 0000000..86b6496 --- /dev/null +++ b/tests/fail/types10/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: name is a keyword. */ +typealias integer { size = 8; align = 8; signed = "false"; } := trace; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types11/metadata b/tests/fail/types11/metadata new file mode 100644 index 0000000..6ac5b82 --- /dev/null +++ b/tests/fail/types11/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: missing size. */ +typealias integer { align = 8; signed = "false"; } := uint8_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types12/metadata b/tests/fail/types12/metadata new file mode 100644 index 0000000..8057394 --- /dev/null +++ b/tests/fail/types12/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: invalid kind. */ +typealias entier { size = 8; align = 8; signed = false; } := uint8_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types13/metadata b/tests/fail/types13/metadata new file mode 100644 index 0000000..d379426 --- /dev/null +++ b/tests/fail/types13/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: base as a string. */ +typealias integer { size = 8; align = 8; signed = false; base = "decimal"; } := uint8_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types14/metadata b/tests/fail/types14/metadata new file mode 100644 index 0000000..051c230 --- /dev/null +++ b/tests/fail/types14/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: invalid base. */ +typealias integer { size = 8; align = 8; signed = false; base = orthogonal; } := uint8_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types15/metadata b/tests/fail/types15/metadata new file mode 100644 index 0000000..6343188 --- /dev/null +++ b/tests/fail/types15/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: invalid encoding. */ +typealias integer { size = 8; align = 8; signed = false; encoding = true; } := uint8_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types16/metadata b/tests/fail/types16/metadata new file mode 100644 index 0000000..4eeb883 --- /dev/null +++ b/tests/fail/types16/metadata @@ -0,0 +1,15 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: encoding as a string. */ +typealias integer { size = 8; align = 8; signed = false; encoding = "ascii"; } := uint8_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; diff --git a/tests/fail/types17/metadata b/tests/fail/types17/metadata new file mode 100644 index 0000000..abeee11 --- /dev/null +++ b/tests/fail/types17/metadata @@ -0,0 +1,19 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; +typealias integer { size = 8; align = 8; signed = false; } := uint8_t; + +/* FAIL: out of range */ +enum dummy : uint8_t { + x = 1024 +}; +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; + + diff --git a/tests/fail/types18/metadata b/tests/fail/types18/metadata new file mode 100644 index 0000000..3477e20 --- /dev/null +++ b/tests/fail/types18/metadata @@ -0,0 +1,18 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: out of range */ +enum dummy : uint32_t { + x = -1 +}; +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; + + diff --git a/tests/fail/types19/metadata b/tests/fail/types19/metadata new file mode 100644 index 0000000..6d88bad --- /dev/null +++ b/tests/fail/types19/metadata @@ -0,0 +1,18 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: implicite type int, but undefined. */ +enum dummy { + x = 1024 +}; +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; + + diff --git a/tests/fail/types20/metadata b/tests/fail/types20/metadata new file mode 100644 index 0000000..7f7405c --- /dev/null +++ b/tests/fail/types20/metadata @@ -0,0 +1,19 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: type int, but undefined. */ +struct dummy { + int xxx; +}; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; + + diff --git a/tests/fail/types21/metadata b/tests/fail/types21/metadata new file mode 100644 index 0000000..9933b4e --- /dev/null +++ b/tests/fail/types21/metadata @@ -0,0 +1,20 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: duplicate name */ +struct dummy { + uint32_t xxx; + uint32_t xxx; +}; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; + + diff --git a/tests/fail/types22/metadata b/tests/fail/types22/metadata new file mode 100644 index 0000000..eb05c32 --- /dev/null +++ b/tests/fail/types22/metadata @@ -0,0 +1,23 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: duplicate name. */ +struct a { + uint32_t xxx; +}; + +struct a { + uint32_t xxx; +}; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; + + diff --git a/tests/fail/types23/metadata b/tests/fail/types23/metadata new file mode 100644 index 0000000..326f730 --- /dev/null +++ b/tests/fail/types23/metadata @@ -0,0 +1,17 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: duplicate name. */ +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; + + diff --git a/tests/fail/types24/metadata b/tests/fail/types24/metadata new file mode 100644 index 0000000..666933a --- /dev/null +++ b/tests/fail/types24/metadata @@ -0,0 +1,20 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: keyword as a field. */ +struct a { + uint32_t trace; +}; + + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; + + diff --git a/tests/fail/types25/metadata b/tests/fail/types25/metadata new file mode 100644 index 0000000..5ca0fb5 --- /dev/null +++ b/tests/fail/types25/metadata @@ -0,0 +1,20 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: keyword as a field. */ +struct a { + uint32_t x; +} align(-1); + + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; + + diff --git a/tests/fail/types26/metadata b/tests/fail/types26/metadata new file mode 100644 index 0000000..3d1b760 --- /dev/null +++ b/tests/fail/types26/metadata @@ -0,0 +1,17 @@ +/* CTF 1.8 */ + +typealias integer { size = 32; align = 8; signed = false; } := uint32_t; + +/* FAIL: negative array. */ +typedef uint32_t name[-1]; + +trace { + major = 2; + minor = 1; + byte_order = le; + packet.header := struct { + uint32_t magic; + }; +}; + + diff --git a/tests/run.sh b/tests/run.sh new file mode 100755 index 0000000..8556098 --- /dev/null +++ b/tests/run.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +#pass/run.sh + +cd fail/ +./run.sh diff --git a/utils/tap/tap.sh b/utils/tap/tap.sh new file mode 100755 index 0000000..24ac1aa --- /dev/null +++ b/utils/tap/tap.sh @@ -0,0 +1,456 @@ +#!/bin/bash +# +# Copyright 2010 Patrick LeBoutillier +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +_version='1.01' + +_plan_set=0 +_no_plan=0 +_skip_all=0 +_test_died=0 +_expected_tests=0 +_executed_tests=0 +_failed_tests=0 +TODO= + + +usage(){ + cat <<'USAGE' +tap-functions: A TAP-producing BASH library + +PLAN: + plan_no_plan + plan_skip_all [REASON] + plan_tests NB_TESTS + +TEST: + ok RESULT [NAME] + okx COMMAND + is RESULT EXPECTED [NAME] + isnt RESULT EXPECTED [NAME] + like RESULT PATTERN [NAME] + unlike RESULT PATTERN [NAME] + pass [NAME] + fail [NAME] + +SKIP: + skip [CONDITION] [REASON] [NB_TESTS=1] + + skip $feature_not_present "feature not present" 2 || { + is $a "a" + is $b "b" + } + +TODO: + Specify TODO mode by setting $TODO: + TODO="not implemented yet" + ok $result "some not implemented test" + unset TODO + +OTHER: + diag MSG + +EXAMPLE: + #!/bin/bash + + . tap-functions + + plan_tests 7 + + me=$USER + is $USER $me "I am myself" + like $HOME $me "My home is mine" + like "`id`" $me "My id matches myself" + + /bin/ls $HOME 1>&2 + ok $? "/bin/ls $HOME" + # Same thing using okx shortcut + okx /bin/ls $HOME + + [[ "`id -u`" != "0" ]] + i_am_not_root=$? + skip $i_am_not_root "Must be root" || { + okx ls /root + } + + TODO="figure out how to become root..." + okx [ "$HOME" == "/root" ] + unset TODO +USAGE + exit +} + +opt= +set_u= +while getopts ":sx" opt ; do + case $_opt in + u) set_u=1 ;; + *) usage ;; + esac +done +shift $(( OPTIND - 1 )) +# Don't allow uninitialized variables if requested +[[ -n "$set_u" ]] && set -u +unset opt set_u + +# Used to call _cleanup on shell exit +trap _exit EXIT + + +plan_no_plan(){ + (( _plan_set != 0 )) && "You tried to plan twice!" + + _plan_set=1 + _no_plan=1 + + return 0 +} + + +plan_skip_all(){ + local reason=${1:-''} + + (( _plan_set != 0 )) && _die "You tried to plan twice!" + + _print_plan 0 "Skip $reason" + + _skip_all=1 + _plan_set=1 + _exit 0 + + return 0 +} + +plan_tests(){ + local tests=${1:?} + + (( _plan_set != 0 )) && _die "You tried to plan twice!" + (( tests == 0 )) && _die "You said to run 0 tests! You've got to run something." + + _print_plan $tests + _expected_tests=$tests + _plan_set=1 + + return $tests +} + + +_print_plan(){ + local tests=${1:?} + local directive=${2:-''} + + echo -n "1..$tests" + [[ -n "$directive" ]] && echo -n " # $directive" + echo +} + + +pass(){ + local name=$1 + ok 0 "$name" +} + + +fail(){ + local name=$1 + ok 1 "$name" +} + +# This is the workhorse method that actually +# prints the tests result. +ok(){ + local result=${1:?} + local name=${2:-''} + + (( _plan_set == 0 )) && _die "You tried to run a test without a plan! Gotta have a plan." + + _executed_tests=$(( $_executed_tests + 1 )) + + if [[ -n "$name" ]] ; then + if _matches "$name" "^[0-9]+$" ; then + diag " You named your test '$name'. You shouldn't use numbers for your test names." + diag " Very confusing." + fi + fi + + if (( result != 0 )) ; then + echo -n "not " + _failed_tests=$(( _failed_tests + 1 )) + fi + echo -n "ok $_executed_tests" + + if [[ -n "$name" ]] ; then + local ename=${name//\#/\\#} + echo -n " - $ename" + fi + + if [[ -n "$TODO" ]] ; then + echo -n " # TODO $TODO" ; + if (( result != 0 )) ; then + _failed_tests=$(( _failed_tests - 1 )) + fi + fi + + echo + if (( result != 0 )) ; then + local file='tap-functions' + local func= + local line= + + local i=0 + local bt=$(caller $i) + while _matches "$bt" "tap-functions$" ; do + i=$(( $i + 1 )) + bt=$(caller $i) + done + local backtrace= + eval $(caller $i | (read line func file ; echo "backtrace=\"$file:$func() at line $line.\"")) + + local t= + [[ -n "$TODO" ]] && t="(TODO) " + + if [[ -n "$name" ]] ; then + diag " Failed ${t}test '$name'" + diag " in $backtrace" + else + diag " Failed ${t}test in $backtrace" + fi + fi + + return $result +} + + +okx(){ + local command="$@" + + local line= + diag "Output of '$command':" + "$@" | while read line ; do + diag "$line" + done + ok ${PIPESTATUS[0]} "$command" +} + + +_equals(){ + local result=${1:?} + local expected=${2:?} + + if [[ "$result" == "$expected" ]] ; then + return 0 + else + return 1 + fi +} + + +# Thanks to Aaron Kangas for the patch to allow regexp matching +# under bash < 3. + _bash_major_version=${BASH_VERSION%%.*} +_matches(){ + local result=${1:?} + local pattern=${2:?} + + if [[ -z "$result" || -z "$pattern" ]] ; then + return 1 + else + if (( _bash_major_version >= 3 )) ; then + [[ "$result" =~ "$pattern" ]] + else + echo "$result" | egrep -q "$pattern" + fi + fi +} + + +_is_diag(){ + local result=${1:?} + local expected=${2:?} + + diag " got: '$result'" + diag " expected: '$expected'" +} + + +is(){ + local result=${1:?} + local expected=${2:?} + local name=${3:-''} + + _equals "$result" "$expected" + (( $? == 0 )) + ok $? "$name" + local r=$? + (( r != 0 )) && _is_diag "$result" "$expected" + return $r +} + + +isnt(){ + local result=${1:?} + local expected=${2:?} + local name=${3:-''} + + _equals "$result" "$expected" + (( $? != 0 )) + ok $? "$name" + local r=$? + (( r != 0 )) && _is_diag "$result" "$expected" + return $r +} + + +like(){ + local result=${1:?} + local pattern=${2:?} + local name=${3:-''} + + _matches "$result" "$pattern" + (( $? == 0 )) + ok $? "$name" + local r=$? + (( r != 0 )) && diag " '$result' doesn't match '$pattern'" + return $r +} + + +unlike(){ + local result=${1:?} + local pattern=${2:?} + local name=${3:-''} + + _matches "$result" "$pattern" + (( $? != 0 )) + ok $? "$name" + local r=$? + (( r != 0 )) && diag " '$result' matches '$pattern'" + return $r +} + + +skip(){ + local condition=${1:?} + local reason=${2:-''} + local n=${3:-1} + + if (( condition == 0 )) ; then + local i= + for (( i=0 ; i<$n ; i++ )) ; do + _executed_tests=$(( _executed_tests + 1 )) + echo "ok $_executed_tests # skip: $reason" + done + return 0 + else + return + fi +} + + +diag(){ + local msg=${1:?} + + if [[ -n "$msg" ]] ; then + echo "# $msg" + fi + + return 1 +} + + +_die(){ + local reason=${1:-''} + + echo "$reason" >&2 + _test_died=1 + _exit 255 +} + + +BAIL_OUT(){ + local reason=${1:-''} + + echo "Bail out! $reason" >&2 + _exit 255 +} + + +_cleanup(){ + local rc=0 + + if (( _plan_set == 0 )) ; then + diag "Looks like your test died before it could output anything." + return $rc + fi + + if (( _test_died != 0 )) ; then + diag "Looks like your test died just after $_executed_tests." + return $rc + fi + + if (( _skip_all == 0 && _no_plan != 0 )) ; then + _print_plan $_executed_tests + fi + + local s= + if (( _no_plan == 0 && _expected_tests < _executed_tests )) ; then + s= ; (( _expected_tests > 1 )) && s=s + local extra=$(( _executed_tests - _expected_tests )) + diag "Looks like you planned $_expected_tests test$s but ran $extra extra." + rc=1 ; + fi + + if (( _no_plan == 0 && _expected_tests > _executed_tests )) ; then + s= ; (( _expected_tests > 1 )) && s=s + diag "Looks like you planned $_expected_tests test$s but only ran $_executed_tests." + fi + + if (( _failed_tests > 0 )) ; then + s= ; (( _failed_tests > 1 )) && s=s + diag "Looks like you failed $_failed_tests test$s of $_executed_tests." + fi + + return $rc +} + + +_exit_status(){ + if (( _no_plan != 0 || _plan_set == 0 )) ; then + return $_failed_tests + fi + + if (( _expected_tests < _executed_tests )) ; then + return $(( _executed_tests - _expected_tests )) + fi + + return $(( _failed_tests + ( _expected_tests - _executed_tests ))) +} + + +_exit(){ + local rc=${1:-''} + if [[ -z "$rc" ]] ; then + _exit_status + rc=$? + fi + + _cleanup + local alt_rc=$? + (( alt_rc != 0 )) && rc=$alt_rc + trap - EXIT + exit $rc +}