From 1cb809d8ef81931ea0b1f15619b7e830281f2556 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Fri, 17 Apr 2015 12:55:38 +1000 Subject: [PATCH] Add "passed" and "failed" functions Add two generic "passed" and "failed" functions to the unittest helper. Also keep a count of passed and failed tests. Later changes will use these functions to ensure they exit with a correct return code. Change-Id: I8574dcb1447b04fcda3d72df0bf8605cf7488d3c --- tests/unittest.sh | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/tests/unittest.sh b/tests/unittest.sh index 435cc3a58b..69f19b7dae 100644 --- a/tests/unittest.sh +++ b/tests/unittest.sh @@ -14,8 +14,30 @@ # we always start with no errors ERROR=0 +PASS=0 FAILED_FUNCS="" +function passed { + local lineno=$(caller 0 | awk '{print $1}') + local function=$(caller 0 | awk '{print $2}') + local msg="$1" + if [ -z "$msg" ]; then + msg="OK" + fi + PASS=$((PASS+1)) + echo $function:L$lineno $msg +} + +function failed { + local lineno=$(caller 0 | awk '{print $1}') + local function=$(caller 0 | awk '{print $2}') + local msg="$1" + FAILED_FUNCS+="$function:L$lineno\n" + echo "ERROR: $function:L$lineno!" + echo " $msg" + ERROR=$((ERROR+1)) +} + function assert_equal { local lineno=`caller 0 | awk '{print $1}'` local function=`caller 0 | awk '{print $2}'` @@ -24,16 +46,20 @@ function assert_equal { FAILED_FUNCS+="$function:L$lineno\n" echo "ERROR: $1 != $2 in $function:L$lineno!" echo " $msg" - ERROR=1 + ERROR=$((ERROR+1)) else + PASS=$((PASS+1)) echo "$function:L$lineno - ok" fi } function report_results { - if [[ $ERROR -eq 1 ]]; then - echo "Tests FAILED" - echo $FAILED_FUNCS + echo "$PASS Tests PASSED" + if [[ $ERROR -gt 1 ]]; then + echo + echo "The following $ERROR tests FAILED" + echo -e "$FAILED_FUNCS" + echo "---" exit 1 fi }