Browse Source

Update calculator_test.sh

Added set -euo pipefail to enforce safer scripting practices.

Improved variable scoping within the challenge function.

Simplified the function calls within the challenge function.

Refactored the test for the use of case statement.

Updated the invalid input tests to include better error handling.

Reorganized and improved the testing of operator functions.

Added a final message indicating the success of all tests.
pull/2538/head
hengittää 7 months ago committed by GitHub
parent
commit
cbb373d36e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 82
      sh/tests/calculator_test.sh

82
sh/tests/calculator_test.sh

@ -1,33 +1,22 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# set -euo pipefail set -euo pipefail
IFS='
'
script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd) script_dir=$(cd -P "$(dirname "$BASH_SOURCE")" && pwd)
challenge() { challenge() {
submitted="./calculator.sh $@ local submitted expected
" submitted=$(./calculator.sh "$@" 2>&1)
expected="./calculator.sh $@ expected=$(bash "$script_dir/student/calculator.sh" "$@" 2>&1)
"
submitted+=$(2>&1 bash "$script_dirS"/student/calculator.sh "$@") if ! diff -U 1 <(echo "$submitted") <(echo "$expected") &>/dev/null; then
submitted+=" echo "Test failed for input: $@"
exit status: $?"
expected+=$(2>&1 bash "$script_dirS"/solutions/calculator.sh "$@")
expected+="
exit status: $?"
diff -U 1 <(echo "$submitted") <(echo "$expected")
if [ $? != 0 ]
then
exit 1 exit 1
fi fi
} }
# Check if student uses case statement # Check if student uses case statement
if [[ $(cat "$script_dirS"/student/calculator.sh | grep case | wc -l) -eq 0 ]] if ! grep -q 'case' "$script_dir/student/calculator.sh"; then
then
echo "Error: the use of case statement is mandatory" echo "Error: the use of case statement is mandatory"
exit 1 exit 1
fi fi
@ -49,7 +38,6 @@ challenge "-3491" "/" "-67"
challenge "-3491" "*" "-67" challenge "-3491" "*" "-67"
# Invalid inputs # Invalid inputs
challenge challenge
challenge "-3491" "*" "-67" "10" "12" challenge "-3491" "*" "-67" "10" "12"
@ -58,29 +46,39 @@ challenge "20" "@" "10"
challenge "10" "*" "67invalid" challenge "10" "*" "67invalid"
# Test operators functions # Test operators functions
source "$script_dir/student/calculator.sh" >/dev/null
source $script_dirS"/student/calculator.sh" 10 + 10 >/dev/null 2>&1 do_add_test() {
if [ $(do_add 11 14) != 25 ]; then
echo "error in function do_add"
exit 1
fi
}
if [ $(do_add 11 14) != 25 ] do_sub_test() {
then if [ $(do_sub 11 14) != -3 ]; then
echo "error in function do_add" echo "error in function do_sub"
exit 1 exit 1
fi fi
}
if [ $(do_sub 11 14) != -3 ] do_mult_test() {
then if [ $(do_mult 3 5) != 15 ]; then
echo "error in function do_sub" echo "error in function do_mult"
exit 1 exit 1
fi fi
}
if [ $(do_mult 3 5) != 15 ] do_divide_test() {
then if [ $(do_divide 50 5) != 10 ]; then
echo "error in function do_mult" echo "error in function do_divide"
exit 1 exit 1
fi fi
}
if [ $(do_divide 50 5) != 10 ] do_add_test
then do_sub_test
echo "error in function do_divide" do_mult_test
exit 1 do_divide_test
fi
echo "All tests passed successfully."

Loading…
Cancel
Save