From 165afa2377ee8eb6bad1b6cfb454a7de525a4498 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Mon, 25 May 2015 11:29:48 +1000 Subject: [PATCH] Fix msg argument to assert_equal I noticed this was taking an argument but not dealing with it. In general the functions were undocumented, so I added some terse usage. Also, the intent of the test-case was to expand the values before using them as the message; make sure this happens by using a temp variable. Change-Id: Ib317ad1e9dd2a5d2232b9c64541fe4a601a2b8da --- tests/test_truefalse.sh | 6 ++++-- tests/unittest.sh | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/tests/test_truefalse.sh b/tests/test_truefalse.sh index ebd9650649..2689589dc9 100755 --- a/tests/test_truefalse.sh +++ b/tests/test_truefalse.sh @@ -19,7 +19,8 @@ function test_trueorfalse { for default in True False; do for name in one captrue lowtrue uppertrue capyes lowyes upperyes; do - assert_equal "True" $(trueorfalse $default $name) "\$(trueorfalse $default $name)" + local msg="trueorfalse($default $name)" + assert_equal "True" $(trueorfalse $default $name) "$msg" done done @@ -33,7 +34,8 @@ function test_trueorfalse { for default in True False; do for name in zero capfalse lowfalse upperfalse capno lowno upperno; do - assert_equal "False" $(trueorfalse $default $name) "\$(trueorfalse $default $name)" + local msg="trueorfalse($default $name)" + assert_equal "False" $(trueorfalse $default $name) "$msg" done done } diff --git a/tests/unittest.sh b/tests/unittest.sh index 69f19b7dae..93aa5fc571 100644 --- a/tests/unittest.sh +++ b/tests/unittest.sh @@ -17,6 +17,8 @@ ERROR=0 PASS=0 FAILED_FUNCS="" +# pass a test, printing out MSG +# usage: passed message function passed { local lineno=$(caller 0 | awk '{print $1}') local function=$(caller 0 | awk '{print $2}') @@ -25,9 +27,11 @@ function passed { msg="OK" fi PASS=$((PASS+1)) - echo $function:L$lineno $msg + echo "PASS: $function:L$lineno $msg" } +# fail a test, printing out MSG +# usage: failed message function failed { local lineno=$(caller 0 | awk '{print $1}') local function=$(caller 0 | awk '{print $2}') @@ -38,10 +42,16 @@ function failed { ERROR=$((ERROR+1)) } +# assert string comparision of val1 equal val2, printing out msg +# usage: assert_equal val1 val2 msg function assert_equal { local lineno=`caller 0 | awk '{print $1}'` local function=`caller 0 | awk '{print $2}'` local msg=$3 + + if [ -z "$msg" ]; then + msg="OK" + fi if [[ "$1" != "$2" ]]; then FAILED_FUNCS+="$function:L$lineno\n" echo "ERROR: $1 != $2 in $function:L$lineno!" @@ -49,10 +59,13 @@ function assert_equal { ERROR=$((ERROR+1)) else PASS=$((PASS+1)) - echo "$function:L$lineno - ok" + echo "PASS: $function:L$lineno - $msg" fi } +# print a summary of passing and failing tests, exiting +# with an error if we have failed tests +# usage: report_results function report_results { echo "$PASS Tests PASSED" if [[ $ERROR -gt 1 ]]; then