aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichał Łyszczek <michal.lyszczek@bofc.pl>2019-10-29 22:16:20 +0100
committerMichał Łyszczek <michal.lyszczek@bofc.pl>2019-10-29 22:16:20 +0100
commit9b1bdcd94db8cf5266e5b754227b5d8c2170673a (patch)
tree97d67dc1fb608feb3252fd9d55a7a68f5c23c152
parenta771456205242fffeac4b6c48e171db3f315629e (diff)
downloadmtest-9b1bdcd94db8cf5266e5b754227b5d8c2170673a.tar.gz
mtest-9b1bdcd94db8cf5266e5b754227b5d8c2170673a.tar.bz2
mtest-9b1bdcd94db8cf5266e5b754227b5d8c2170673a.zip
mtest.sh: add support for custom parameters in shell scripts
It is possible now to run mt_run test1 and add some params mt_run_named test1 to-customize tests Signed-off-by: Michał Łyszczek <michal.lyszczek@bofc.pl>
-rw-r--r--man/mt_run.328
-rwxr-xr-xmtest.sh33
2 files changed, 56 insertions, 5 deletions
diff --git a/man/mt_run.3 b/man/mt_run.3
index c748ecd..1675a2e 100644
--- a/man/mt_run.3
+++ b/man/mt_run.3
@@ -27,7 +27,10 @@ c/c++
.PP
shell
.PP
-.BI "mt_run <" function_name ">"
+.BI "mt_run <" function_name "> [" param1 "] [" ... "]"
+.br
+.BI "mt_run_named <" function_name "> <" test_name "> [" param1 "] \
+[" ... "]"
.SH DESCRIPTION
.PP
.BR mt_run (3)
@@ -63,6 +66,19 @@ works in the same way as they non-parameter counterpart, but
.I parameter
will be passed to
.IR function_name_param .
+When using shell functions, there is no
+.I param
+functions, but you can pass unspecified number of parameters to both
+.BR mt_run (3)
+and
+.BR mt_run_named (3).
+Since
+.I function_name
+is run via
+.B eval
+program, when you want to pass parameter with spaces, you need to use
+double quotes like
+.BR """'parameter\ with\ spaces'""" .
.PP
Optionally user can also set two function pointers
.I mt_prepare_test
@@ -123,6 +139,14 @@ shell
. ./mtest.sh
+ test_two()
+ {
+ a=$1
+ b=$2
+ c=$((a + b))
+ mt_fail "[ $b -eq $(( c - a )) ]"
+ }
+
test_one()
{
a=1
@@ -131,6 +155,8 @@ shell
}
mt_run test_one
+ mt_run test_two 5 3
+
mt_return
.EE
.SH "SEE ALSO"
diff --git a/mtest.sh b/mtest.sh
index fa6ad76..61af340 100755
--- a/mtest.sh
+++ b/mtest.sh
@@ -64,28 +64,51 @@ mt_current_test="none"
## ==========================================================================
-# run specified test
+# Runs specified test with optional custom parameters. Check info about
+# mt_run_named() to learn more.
#
# $1 - function name as a string - will be passed to eval
+# $@ - unspecified number of parameters to pass to function $1
## ==========================================================================
mt_run()
{
- mt_run_named $1 $1
+ function_name=$1
+ shift
+ mt_run_named $function_name $function_name $@
}
## ==========================================================================
-# run specified test with custom name to be printed during report
+# run specified test with custom name to be printed during report and
+# also pass optional arguments to test function.
#
# $1 - function name as a string - will be passed to eval
# $2 - test name, will be used instead of $1 in report
+# $@ - unspecified number of parameters to pass to function $1
+#
+# Consider:
+#
+# foo()
+# {
+# echo param1: $1 param2: $2 param3: $3
+# }
+#
+# mt_run_named_param foo test-name 42 "'string with space'" string
+#
+# will print
+# param1: 42 param2: string with space param3: string
+#
+# NOTE: due to the fact that function is called by `eval', when you
+# want to pass string with space, you need to use double quotation:
+# like this "''".
## ==========================================================================
mt_run_named()
{
+ function_name="$1"
mt_current_test="$2"
mt_test_status=0
mt_total_tests=$((mt_total_tests + 1))
@@ -95,7 +118,9 @@ mt_run_named()
mt_prepare_test
fi
- eval "$1"
+ shift
+ shift
+ eval $function_name $@
if type mt_cleanup_test > /dev/null 2>&1
then