Skip to content

Commit 5190030

Browse files
r0cketdyneHarryVasanth
authored andcommitted
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.
1 parent 93ad192 commit 5190030

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed

sh/tests/calculator_test.sh

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
11
#!/usr/bin/env bash
22

3-
# set -euo pipefail
4-
IFS='
5-
'
3+
set -euo pipefail
64

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

97
challenge() {
10-
submitted="./calculator.sh $@
11-
"
12-
expected="./calculator.sh $@
13-
"
14-
submitted+=$(2>&1 bash "$script_dirS"/student/calculator.sh "$@")
15-
submitted+="
16-
exit status: $?"
17-
expected+=$(2>&1 bash "$script_dirS"/solutions/calculator.sh "$@")
18-
expected+="
19-
exit status: $?"
20-
21-
diff -U 1 <(echo "$submitted") <(echo "$expected")
22-
if [ $? != 0 ]
23-
then
8+
local submitted expected
9+
submitted=$(./calculator.sh "$@" 2>&1)
10+
expected=$(bash "$script_dir/student/calculator.sh" "$@" 2>&1)
11+
12+
if ! diff -U 1 <(echo "$submitted") <(echo "$expected") &>/dev/null; then
13+
echo "Test failed for input: $@"
2414
exit 1
2515
fi
2616
}
2717

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

5140
# Invalid inputs
52-
5341
challenge
5442
challenge "-3491" "*" "-67" "10" "12"
5543

@@ -58,29 +46,39 @@ challenge "20" "@" "10"
5846
challenge "10" "*" "67invalid"
5947

6048
# Test operators functions
49+
source "$script_dir/student/calculator.sh" >/dev/null
6150

62-
source $script_dirS"/student/calculator.sh" 10 + 10 >/dev/null 2>&1
51+
do_add_test() {
52+
if [ $(do_add 11 14) != 25 ]; then
53+
echo "error in function do_add"
54+
exit 1
55+
fi
56+
}
6357

64-
if [ $(do_add 11 14) != 25 ]
65-
then
66-
echo "error in function do_add"
67-
exit 1
68-
fi
58+
do_sub_test() {
59+
if [ $(do_sub 11 14) != -3 ]; then
60+
echo "error in function do_sub"
61+
exit 1
62+
fi
63+
}
6964

70-
if [ $(do_sub 11 14) != -3 ]
71-
then
72-
echo "error in function do_sub"
73-
exit 1
74-
fi
65+
do_mult_test() {
66+
if [ $(do_mult 3 5) != 15 ]; then
67+
echo "error in function do_mult"
68+
exit 1
69+
fi
70+
}
7571

76-
if [ $(do_mult 3 5) != 15 ]
77-
then
78-
echo "error in function do_mult"
79-
exit 1
80-
fi
72+
do_divide_test() {
73+
if [ $(do_divide 50 5) != 10 ]; then
74+
echo "error in function do_divide"
75+
exit 1
76+
fi
77+
}
8178

82-
if [ $(do_divide 50 5) != 10 ]
83-
then
84-
echo "error in function do_divide"
85-
exit 1
86-
fi
79+
do_add_test
80+
do_sub_test
81+
do_mult_test
82+
do_divide_test
83+
84+
echo "All tests passed successfully."

0 commit comments

Comments
 (0)