[aklug] bash assert function?

From: Christopher Howard <christopher.howard@frigidcode.com>
Date: Wed May 23 2012 - 14:25:22 AKDT

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hey guys, my bash is a bit rusty, but I'm trying to use a bash script
as a unit test in one of my software packages. I want to make a little
"assert" function like so:

code:
- --------
# Params:
# 1) LINENO
# ...) expression to test
function assert
{
    [ $# -gt 1 ] || die "assert requires at least two parameters"
    lineno=$1
    shift 1
    if ! [ $@ ] ; then
        echo "assert test failed at line $lineno, test expression $@"
        return 1
    else
        return 0
    fi
}
- --------

The idea being to do asserts like:

code:
- --------
results=`some_test_command -f foo`
assert $LINENO "$results" = "string value" || die
- --------

However, this chokes if I try something like:

code:
- --------
assert $LINENO "a b" = "a b"
# test inside assert() chokes on having "too many arguments"
- --------

I suppose I could do the test outside of the assert function, and just
pass in the results, but that would be kind of lame because then the
assert function wouldn't be able to echo the test expression.

Any helpful thoughts?

- --
frigidcode.com
indicium.us
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJPvWPSAAoJEI2DxlFxTtgdBGAH/2Up5XIUW17PSUZ17TStr1ua
VFeIzN6c8rTv1qlgw5bVGe5la0lDa7nYz9uOt5UEfsJ3chXErxfgExnEf0eYqyKD
vtyZlU1Ilz91XiAIhH+uonKm4FWOO2atEtHkUrIg18Soyd6sRI7CRGMlPmd8nreZ
jFdoU8dm74fBxW8/ouT9buD2Ig/ZLIznxvUzXBeJizBsORTsa4hPkO9nsUUISoZC
KFYDoyS+A+J/YFq5biUygOL4FObgreVvVMRIY6ba8XjTb9ZFvP9ckY29ffLsWjmp
dV1Jw3aH0bw6LdoxpfF3/BobZtxmOy7HT985gMFH4/zrq5S/1sNAV8mxSSCi3k0=
=Z9PW
-----END PGP SIGNATURE-----
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Wed May 23 14:21:22 2012

This archive was generated by hypermail 2.1.8 : Wed May 23 2012 - 14:21:22 AKDT