From 98f59aafaf88328f6aee98efa0f563fb8bf91ebd Mon Sep 17 00:00:00 2001 From: Mahito OGURA Date: Mon, 11 May 2015 18:02:34 +0900 Subject: [PATCH] Fix function and test for 'trueorfalse'. The function's comment is written as follow, however the function accepts other values (ex. "e", "t", "T", "f", "F", etc...). --- Accepts as False: 0 no No NO false False FALSE Accepts as True: 1 yes Yes YES true True TRUE --- Moreover if testval mach True or False, the function exits without resetting xtrace. This patch fixes the issue and add test patterns. Change-Id: Ie48a859476faff22a4dfef466516e2d7d62ef0c0 Closes-bug: #1453687 --- functions-common | 16 +++++++++------- tests/test_truefalse.sh | 33 +++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/functions-common b/functions-common index 4d07c03cce..6d9a0fa6b0 100644 --- a/functions-common +++ b/functions-common @@ -51,14 +51,16 @@ TRACK_DEPENDS=${TRACK_DEPENDS:-False} function trueorfalse { local xtrace=$(set +o | grep xtrace) set +o xtrace - local default=$1 - local literal=$2 - local testval=${!literal:-} - [[ -z "$testval" ]] && { echo "$default"; return; } - [[ "0 no No NO false False FALSE" =~ "$testval" ]] && { echo "False"; return; } - [[ "1 yes Yes YES true True TRUE" =~ "$testval" ]] && { echo "True"; return; } - echo "$default" + local default=$1 + local testval=${!2:-} + + case "$testval" in + "1" | [yY]es | "YES" | [tT]rue | "TRUE" ) echo "True" ;; + "0" | [nN]o | "NO" | [fF]alse | "FALSE" ) echo "False" ;; + * ) echo "$default" ;; + esac + $xtrace } diff --git a/tests/test_truefalse.sh b/tests/test_truefalse.sh index e57948a407..ebd9650649 100755 --- a/tests/test_truefalse.sh +++ b/tests/test_truefalse.sh @@ -8,27 +8,36 @@ TOP=$(cd $(dirname "$0")/.. && pwd) source $TOP/functions source $TOP/tests/unittest.sh -function test_truefalse { +function test_trueorfalse { local one=1 local captrue=True local lowtrue=true - local abrevtrue=t + local uppertrue=TRUE + local capyes=Yes + local lowyes=yes + local upperyes=YES + + 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)" + done + done + local zero=0 local capfalse=False local lowfalse=false - local abrevfalse=f - for against in True False; do - for name in one captrue lowtrue abrevtrue; do - assert_equal "True" $(trueorfalse $against $name) "\$(trueorfalse $against $name)" - done - done - for against in True False; do - for name in zero capfalse lowfalse abrevfalse; do - assert_equal "False" $(trueorfalse $against $name) "\$(trueorfalse $against $name)" + local upperfalse=FALSE + local capno=No + local lowno=no + local upperno=NO + + 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)" done done } -test_truefalse +test_trueorfalse report_results