diff options
author | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2020-04-29 11:29:18 +0200 |
---|---|---|
committer | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2020-04-29 11:52:39 +0200 |
commit | 864dd863bf59d3182fc98fcf353dd730801af261 (patch) | |
tree | 35e699e9c76814c99192017b35e8bb5120373b20 | |
parent | 09cc79d77909a7f9b216f667b0ba551963f70719 (diff) | |
download | mtest-864dd863bf59d3182fc98fcf353dd730801af261.tar.gz mtest-864dd863bf59d3182fc98fcf353dd730801af261.tar.bz2 mtest-864dd863bf59d3182fc98fcf353dd730801af261.zip |
mtest.sh: mt_fail(sh) can now print custom error message
by calling mt_fail <expr> <error_message> user can now print
own error message when expr fails
Signed-off-by: Michał Łyszczek <michal.lyszczek@bofc.pl>
-rw-r--r-- | man/mt_assert.3 | 17 | ||||
-rw-r--r-- | man/mtest_overview.7 | 3 | ||||
-rwxr-xr-x | mtest.sh | 10 |
3 files changed, 24 insertions, 6 deletions
diff --git a/man/mt_assert.3 b/man/mt_assert.3 index dd3a358..0478902 100644 --- a/man/mt_assert.3 +++ b/man/mt_assert.3 @@ -16,7 +16,7 @@ c/c++ .PP shell .PP -.BI "mt_fail <" expression ">" +.BI "mt_fail <" expression "> [" error_message "]" .br .BI "mt_dfail <" command "> [" argumnet "] ..." .SH DESCRIPTION @@ -50,12 +50,23 @@ to eval. must be single argument, so it should be closed in "" to preserve spaces. If .I expression -is easy and does not required +is easy and does not require .B eval to process, one can use .BR mt_dfail (3) which passes all arguments directly to if statement -.B """if ! ${@}""" +.BR """if ! ${@}""" . +Normally when +.BR mt_fail (3) +fails, it prints +.B # assert +.I expression +on stderr, but you can instead print own message by passing second argument +to +.BR mt_fail (3). +This string will be directly passed to +.B echo +command. .SH EXAMPLE .PP Proper example can be found in diff --git a/man/mtest_overview.7 b/man/mtest_overview.7 index dbb2afc..501e937 100644 --- a/man/mtest_overview.7 +++ b/man/mtest_overview.7 @@ -39,7 +39,7 @@ For shell .PP .BI "mt_run <" function_name ">" .br -.BI "mt_fail <" expression ">" +.BI "mt_fail <" expression "> [" error_message "]" .br .BI "mt_dfail <" command "> [" argumnet "] ..." .br @@ -107,6 +107,7 @@ Example of using mt in posix shell a=$((a + 1)) mt_fail "[ $a -eq 2 ]" mt_dfail [ $a -eq 3 ] + mt_fail "[ $a -eq 4 ]" "a($a) is not equal to 4" } mt_run test_one @@ -122,17 +122,23 @@ mt_run_named() ## ========================================================================== # performs check on given command, if command returns error, current test -# will be marked as failed. +# will be marked as failed. If second argument is provided, this string +# will be used as error message instead of "$1". # # $1 - code to evaluate, simply passed to eval +# $2 - error message to print ## ========================================================================== mt_fail() { + error_msg="$1" + if [ $# -eq 2 ]; then + err_msg="$2" + fi mt_total_checks=$(( mt_total_checks + 1 )) if ! eval $1; then - echo "# assert $mt_current_test, '$1'" + echo "# assert $mt_current_test, '$err_msg'" mt_test_status=1 mt_checks_failed=$(( mt_checks_failed + 1 )) fi |