diff options
Diffstat (limited to 'tests/cond.tests')
-rwxr-xr-x | tests/cond.tests | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/tests/cond.tests b/tests/cond.tests new file mode 100755 index 0000000..3abfa9d --- /dev/null +++ b/tests/cond.tests @@ -0,0 +1,159 @@ +# +# the test/[ code is tested elsewhere, and the [[...]] just uses the same +# code. this tests the special features of [[...]] +# +TDIR=/usr/homes/chet + +# this one is straight out of the ksh88 book +[[ foo > bar && $PWD -ef . ]] +echo returns: $? + +# [[ x ]] is equivalent to [[ -n x ]] +[[ x ]] +echo returns: $? + +# [[ ! x ]] is equivalent to [[ ! -n x ]] +[[ ! x ]] +echo returns: $? + +# ! binds tighter than test/[ -- it binds to a term, not an expression +[[ ! x || x ]] +echo returns: $? + +# parenthesized terms didn't work right until post-2.04 +[[ a ]] +echo returns: $? + +[[ (a) ]] +echo returns: $? + +[[ -n a ]] +echo returns: $? + +[[ (-n a) ]] +echo returns: $? + +# unset variables don't need to be quoted +[[ -n $UNSET ]] +echo returns: $? + +[[ -z $UNSET ]] +echo returns: $? + +# the ==/= and != operators do pattern matching +[[ $TDIR == /usr/homes/* ]] +echo returns: $? + +# ...but you can quote any part of the pattern to have it matched as a string +[[ $TDIR == /usr/homes/\* ]] +echo returns: $? + +[[ $TDIR == '/usr/homes/*' ]] +echo returns: $? + +# if the first part of && fails, the second is not executed +[[ -n $UNSET && $UNSET == foo ]] +echo returns: $? + +[[ -z $UNSET && $UNSET == foo ]] +echo returns: $? + +# if the first part of || succeeds, the second is not executed +[[ -z $UNSET || -d $PWD ]] +echo returns: $? + +# if the rhs were executed, it would be an error +[[ -n $TDIR || $HOME -ef ${H*} ]] +echo returns: $? + +[[ -n $TDIR && -z $UNSET || $HOME -ef ${H*} ]] +echo returns: $? + +# && has a higher parsing precedence than || +[[ -n $TDIR && -n $UNSET || $TDIR -ef . ]] +echo returns: $? + +# ...but expressions in parentheses may be used to override precedence rules +[[ -n $TDIR || -n $UNSET && $PWD -ef xyz ]] +echo returns: $? + +[[ ( -n $TDIR || -n $UNSET ) && $PWD -ef xyz ]] +echo returns: $? + +# some arithmetic tests for completeness -- see what happens with missing +# operands, bad expressions, makes sure arguments are evaluated as +# arithmetic expressions, etc. + +unset IVAR A +[[ 7 -gt $IVAR ]] +echo returns: $? + +[[ $IVAR -gt 7 ]] +echo returns: $? + +IVAR=4 +[[ $IVAR -gt 7 ]] +echo returns: $? + +[[ 7 -eq 4+3 ]] +echo returns: $? + +[[ 7 -eq 4+ ]] +echo returns: $? + +IVAR=4+3 +[[ $IVAR -eq 7 ]] +echo returns: $? + +A=7 +[[ $IVAR -eq A ]] +echo returns: $? + +unset IVAR A + +# more pattern matching tests + +[[ $filename == *.c ]] +echo returns: $? + +filename=patmatch.c + +[[ $filename == *.c ]] +echo returns: $? + +# the extended globbing features may be used when matching patterns +shopt -s extglob + +arg=-7 + +[[ $arg == -+([0-9]) ]] +echo returns: $? + +arg=-H + +[[ $arg == -+([0-9]) ]] +echo returns: $? + +arg=+4 +[[ $arg == ++([0-9]) ]] +echo returns: $? + +# make sure the null string is never matched if the string is not null +STR=file.c +PAT= + +if [[ $STR = $PAT ]]; then + echo oops +fi + +# but that if the string is null, a null pattern is matched correctly +STR= +PAT= + +if [[ $STR = $PAT ]]; then + echo ok +fi + +# bug in all versions up to and including bash-2.05b +if [[ "123abc" == *?(a)bc ]]; then echo ok 42; else echo bad 42; fi +if [[ "123abc" == *?(a)bc ]]; then echo ok 43; else echo bad 43; fi |