From 752230d7288cd8efd7606f7c4032077590fbac0f Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Sat, 5 Mar 2022 12:30:59 +0100 Subject: tests: Add option parsing tests --- tests/001-missing_arguments.sh | 7 +++++-- tests/002-too_many_arguments.sh | 9 +++++++++ tests/003-unknown_option.sh | 9 +++++++++ tests/004-missing_argument_for_option.sh | 10 ++++++++++ tests/005-incorrect_buffer_size.sh | 11 +++++++++++ tests/006-incorrect_sector_size.sh | 11 +++++++++++ tests/Makefile | 8 +++++++- tests/assert.sh | 19 +++++++++++++++++++ 8 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 tests/002-too_many_arguments.sh create mode 100644 tests/003-unknown_option.sh create mode 100644 tests/004-missing_argument_for_option.sh create mode 100644 tests/005-incorrect_buffer_size.sh create mode 100644 tests/006-incorrect_sector_size.sh create mode 100644 tests/assert.sh (limited to 'tests') diff --git a/tests/001-missing_arguments.sh b/tests/001-missing_arguments.sh index baa7af1..8e00bf0 100644 --- a/tests/001-missing_arguments.sh +++ b/tests/001-missing_arguments.sh @@ -1,7 +1,10 @@ #!/bin/sh +source ./assert.sh + PROGRAM_EXEC="$1" -$PROGRAM_EXEC 1>/dev/null 2>&1 +assert_error "missing arguments" $PROGRAM_EXEC +assert_error "missing arguments" $PROGRAM_EXEC arg -[ $? -ne 0 ] && exit 0 || exit 1 +exit 0 diff --git a/tests/002-too_many_arguments.sh b/tests/002-too_many_arguments.sh new file mode 100644 index 0000000..b6a1990 --- /dev/null +++ b/tests/002-too_many_arguments.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +source ./assert.sh + +PROGRAM_EXEC="$1" + +assert_error "too many arguments" $PROGRAM_EXEC arg1 arg2 arg3 arg4 + +exit 0 diff --git a/tests/003-unknown_option.sh b/tests/003-unknown_option.sh new file mode 100644 index 0000000..919f125 --- /dev/null +++ b/tests/003-unknown_option.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +source ./assert.sh + +PROGRAM_EXEC="$1" + +assert_error "unknown option '-x'" $PROGRAM_EXEC -x ref out + +exit 0 diff --git a/tests/004-missing_argument_for_option.sh b/tests/004-missing_argument_for_option.sh new file mode 100644 index 0000000..071ea07 --- /dev/null +++ b/tests/004-missing_argument_for_option.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +source ./assert.sh + +PROGRAM_EXEC="$1" + +assert_error "missing argument for option '-b'" $PROGRAM_EXEC -b +assert_error "missing argument for option '-s'" $PROGRAM_EXEC -s + +exit 0 diff --git a/tests/005-incorrect_buffer_size.sh b/tests/005-incorrect_buffer_size.sh new file mode 100644 index 0000000..8c88b54 --- /dev/null +++ b/tests/005-incorrect_buffer_size.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +source ./assert.sh + +PROGRAM_EXEC="$1" + +assert_error "incorrect sector size" $PROGRAM_EXEC -s abc123 ref out +assert_error "sector size cannot be 0" $PROGRAM_EXEC -s 0 ref out +assert_error "sector size cannot larger than buffer size" $PROGRAM_EXEC -s 2 -b 1 ref out + +exit 0 diff --git a/tests/006-incorrect_sector_size.sh b/tests/006-incorrect_sector_size.sh new file mode 100644 index 0000000..484c09e --- /dev/null +++ b/tests/006-incorrect_sector_size.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +source ./assert.sh + +PROGRAM_EXEC="$1" + +assert_error "incorrect buffer size" $PROGRAM_EXEC -b abc123 ref out +assert_error "buffer size cannot be 0" $PROGRAM_EXEC -b 0 ref out +assert_error "buffer size is not multiple of sector size" $PROGRAM_EXEC -b 3 -s 2 ref out + +exit 0 diff --git a/tests/Makefile b/tests/Makefile index eb009c1..fb58ff6 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,4 +1,10 @@ PROGRAM_EXEC=../src/$(PROGRAM_NAME) all: - sh ./001-missing_arguments.sh $(PROGRAM_EXEC) + for t in [0-9]*.sh ; do \ + echo $$t; \ + sh ./$$t ../src/diff-dd; \ + # Testing of the return value must return true on the test failure. \ + # If the return value is zero, it means a successful test. \ + [ $$? -ne 0 ] && exit 1 || true; \ + done diff --git a/tests/assert.sh b/tests/assert.sh new file mode 100644 index 0000000..ba73f41 --- /dev/null +++ b/tests/assert.sh @@ -0,0 +1,19 @@ +function assert_error() +{ + expected_stderr="$1" + + shift 1 + actual_stderr="$("$@" 2>&1 1>/dev/null)" + retval=$? + is_stderr_expected="$(echo "$actual_stderr" | grep -i "$expected_stderr")" + + if [ $retval -eq 0 ]; then + echo "assert_error: Return value is $retval, expected != 0" + exit 1 + elif [ -z "$is_stderr_expected" ]; then + echo "assert_error: stderr does not contain expected string" + echo " actual: $actual_stderr" + echo " expected: $expected_stderr" + exit 1 + fi +} -- cgit v1.2.3