Fixed PREFIX in Makefile, added several tests in tests/

This commit is contained in:
2013-10-09 22:42:16 -04:00
parent 034e79e9bf
commit 77d48c3425
4 changed files with 162 additions and 18 deletions

58
tests/test_clean_state.sh Normal file
View File

@@ -0,0 +1,58 @@
source $(dirname ${BASH_SOURCE})/../cmdarg.sh
function shunittest_clean_state()
{
# Tests that cmdarg_purge ensures an empty config state
function parse1()
{
cmdarg 'a:' 'a' 'some arg'
cmdarg 'b' 'b' 'some arg'
cmdarg_parse "$@"
}
function parse2()
{
cmdarg_purge
cmdarg_parse "$@"
}
# This cleans the state from shunit
cmdarg_purge
parse1 -a 3 -b
parse2
if [[ "${cmdarg_cfg['a']}" == "" ]]; then
return 0
else
cmdarg_dump
return 1
fi
}
function shunittest_clean_state_subshells()
{
# Ensures that, when subsequent cmdarg invocations occur in subshells,
# that the initial state is empty even without having called cmdarg_purge
# This is just here to clean the state from shunit
cmdarg_purge
function parse1()
{
cmdarg 'a:' 'a' 'some arg'
cmdarg 'b' 'b' 'some arg'
cmdarg_parse "$@"
}
function parse2()
{
cmdarg_parse "$@"
}
(parse1 -a 3 -b)
(parse2)
if [[ "${cmdarg_cfg['a']}" == "" ]]; then
return 0
else
cmdarg_dump
return 1
fi
}

74
tests/test_types.sh Normal file
View File

@@ -0,0 +1,74 @@
source $(dirname ${BASH_SOURCE})/../cmdarg.sh
function shunittest_array_undefined()
{
# Tests that cmdarg and cmdarg_parse return an error when an array
# is undefined
cmdarg_purge
err=$(cmdarg 'a:[]' 'missingarray' 2>&1)
if [[ $? -eq 0 ]]; then
echo "cmdarg fails to throw an error for undefined array variables"
else
echo "$err" | grep "Array variable missingarray does not exist" >/dev/null
if [[ $? -ne 0 ]]; then
echo "cmdarg does not report errors on stderr for undefined arrays"
echo "$err"
return 1
fi
fi
return 0
}
function shunittest_array_values
{
cmdarg_purge
declare -a array
cmdarg 'a:[]' 'array'
cmdarg_parse -a a -a b -a c
if [[ "${array[@]}" != "a b c" ]]; then
echo "Array does not contain expected arguments"
cmdarg_dump >&2
return 1
fi
return $?
}
function shunittest_hash_undefined()
{
# Tests that cmdarg and cmdarg_parse return an error when an array
# is undefined
cmdarg_purge
err=$(cmdarg 'a:{}' 'missingarray' 2>&1)
if [[ $? -eq 0 ]]; then
echo "cmdarg fails to throw an error for undefined hash variables"
else
echo "$err" | grep "Hash variable missingarray does not exist" >/dev/null
if [[ $? -ne 0 ]]; then
echo "cmdarg does not report errors on stderr for undefined hashes"
echo "$err"
return 1
fi
fi
return 0
}
function shunittest_hash_values
{
cmdarg_purge
declare -A hash
cmdarg 'H:{}' 'hash'
cmdarg_parse -H a=1 -H b=2 -H c=3
base="a=1 b=2 c=3"
cmp=""
for k in a b c
do
cmp="$cmp ${k}=${hash[$k]}"
done
cmp=$(echo "$cmp" | sed s/'^\s*'//)
if [[ "$cmp" != "$base" ]]; then
echo "Hash does not contain expected arguments ($cmp vs $base)"
cmdarg_dump >&2
return 1
fi
return $?
}