Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EXTERNAL] Update calculator_test.sh #2538

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 40 additions & 42 deletions sh/tests/calculator_test.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
#!/usr/bin/env bash

# set -euo pipefail
IFS='
'
set -euo pipefail

script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd)
script_dir=$(cd -P "$(dirname "$BASH_SOURCE")" && pwd)

challenge() {
submitted="./calculator.sh $@
"
expected="./calculator.sh $@
"
submitted+=$(2>&1 bash "$script_dirS"/student/calculator.sh "$@")
submitted+="
exit status: $?"
expected+=$(2>&1 bash "$script_dirS"/solutions/calculator.sh "$@")
expected+="
exit status: $?"

diff -U 1 <(echo "$submitted") <(echo "$expected")
if [ $? != 0 ]
then
local submitted expected
submitted=$(./calculator.sh "$@" 2>&1)
expected=$(bash "$script_dir/student/calculator.sh" "$@" 2>&1)

if ! diff -U 1 <(echo "$submitted") <(echo "$expected") &>/dev/null; then
echo "Test failed for input: $@"
exit 1
fi
}

# Check if student uses case statement
if [[ $(cat "$script_dirS"/student/calculator.sh | grep case | wc -l) -eq 0 ]]
then
if ! grep -q 'case' "$script_dir/student/calculator.sh"; then
echo "Error: the use of case statement is mandatory"
exit 1
fi
Expand All @@ -49,7 +38,6 @@ challenge "-3491" "/" "-67"
challenge "-3491" "*" "-67"

# Invalid inputs

challenge
challenge "-3491" "*" "-67" "10" "12"

Expand All @@ -58,29 +46,39 @@ challenge "20" "@" "10"
challenge "10" "*" "67invalid"

# 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 ]
then
echo "error in function do_add"
exit 1
fi
do_sub_test() {
if [ $(do_sub 11 14) != -3 ]; then
echo "error in function do_sub"
exit 1
fi
}

if [ $(do_sub 11 14) != -3 ]
then
echo "error in function do_sub"
exit 1
fi
do_mult_test() {
if [ $(do_mult 3 5) != 15 ]; then
echo "error in function do_mult"
exit 1
fi
}

if [ $(do_mult 3 5) != 15 ]
then
echo "error in function do_mult"
exit 1
fi
do_divide_test() {
if [ $(do_divide 50 5) != 10 ]; then
echo "error in function do_divide"
exit 1
fi
}

if [ $(do_divide 50 5) != 10 ]
then
echo "error in function do_divide"
exit 1
fi
do_add_test
do_sub_test
do_mult_test
do_divide_test

echo "All tests passed successfully."