feat: Extended base tools for testing and made a basic test
This commit is contained in:
parent
4d27cea46b
commit
402b1614b5
1 changed files with 86 additions and 51 deletions
|
|
@ -9,7 +9,11 @@ exit 2
|
|||
###################
|
||||
|
||||
executable="../../src/42sh"
|
||||
errors_count=0 # TODO take into account
|
||||
errors_count=0
|
||||
total_tests=0
|
||||
tmp_script="/tmp/test_script.sh"
|
||||
output="/tmp/42sh_tests.output"
|
||||
ref_output="/tmp/42sh_tests_ref.output"
|
||||
|
||||
|
||||
|
||||
|
|
@ -96,40 +100,91 @@ On_IWhite='\033[0;107m' # White
|
|||
# Wrappers #
|
||||
##################
|
||||
|
||||
# TODO output handling
|
||||
|
||||
# @arg test command
|
||||
# @arg actual code
|
||||
# @arg ref code
|
||||
check_result() {
|
||||
|
||||
command="$1"
|
||||
actual_code="$2"
|
||||
ref_code="$3"
|
||||
|
||||
test_failed=0
|
||||
|
||||
# Check return code
|
||||
if [[ "$actual_code" -ne "$ref_code" ]]; then
|
||||
echo $BRed "FAILED" $Color_off
|
||||
echo $Blue ' ' "on '$2'" $Color_off
|
||||
echo ' ' "Expected code $ref_code but got $actual_code"
|
||||
test_failed=1
|
||||
# Check output
|
||||
elif [ diff $output $ref_output > /dev/null ]; then
|
||||
echo $BRed "FAILED" $Color_off
|
||||
echo $Blue ' ' "on '$2'" $Color_off
|
||||
echo ' ' "Output is not the one expected"
|
||||
test_failed=1
|
||||
else
|
||||
echo $Blue OK $Colors_off
|
||||
fi
|
||||
|
||||
if [[ "$test_failed" -eq 1 ]]; then
|
||||
((errors_count++))
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
|
||||
# @arg test name
|
||||
# @arg input string
|
||||
# @arg expected output
|
||||
# @arg expected return code
|
||||
test_cmd() {
|
||||
test_str() {
|
||||
|
||||
# Check input
|
||||
if [[ -z "$1" || -z "$2" || -z "$3" || -z "$4"]]; then
|
||||
echo $BRed "\n\n" "Issue in the testsuite: test_cmd: One or more argument is empty" $Color_off
|
||||
if [[ -z "$1" || -z "$2" ]]; then
|
||||
echo $BRed "\n\n" "Issue in the testsuite: test_str: One or more argument is empty" $Color_off
|
||||
exit 2
|
||||
fi
|
||||
|
||||
echo $BBlue "=== $1 ===" $Color_off
|
||||
|
||||
$executable -c "$2"
|
||||
actual_code=$?
|
||||
echo "$2" > $tmp_script
|
||||
|
||||
if [[ "$actual_code" -ne "$4" ]]; then
|
||||
echo $BRed "Test failed: expected code $4 but got $actual_code" $Color_off
|
||||
fi
|
||||
# Arg
|
||||
echo -n "\n= [ARG] "
|
||||
$executable -c "$2" &> $output
|
||||
actual_code=$?
|
||||
$ref_executable -c "$2" &> $ref_output
|
||||
ref_code=$?
|
||||
((total_tests++))
|
||||
check_result "$2" "$actual_code" "$ref_code"
|
||||
|
||||
# Script
|
||||
echo -n "\n= [SCRIPT] "
|
||||
$executable "$tmp_script" &> $output
|
||||
actual_code=$?
|
||||
$ref_executable "$tmp_script" &> $ref_output
|
||||
ref_code=$?
|
||||
((total_tests++))
|
||||
check_result "$2" "$actual_code" "$ref_code"
|
||||
|
||||
# Stdin
|
||||
echo -n "\n= [STDIN] "
|
||||
$executable < "$tmp_script" &> $output
|
||||
actual_code=$?
|
||||
$ref_executable < "$2" &> $ref_output
|
||||
ref_code=$?
|
||||
((total_tests++))
|
||||
check_result "$tmp_script" "$actual_code" "$ref_code"
|
||||
|
||||
echo $BBlue "===================" $Color_off "\n"
|
||||
}
|
||||
|
||||
# @arg test name
|
||||
# @arg input script
|
||||
# @arg expected output
|
||||
# @arg expected return code
|
||||
test_script() {
|
||||
|
||||
# Check input
|
||||
if [[ -z "$1" || -z "$2" || -z "$3" || -z "$4"]]; then
|
||||
if [[ -z "$1" || -z "$2" ]]; then
|
||||
echo $BRed "\n\n" "Issue in the testsuite: test_script: One or more argument is empty" $Color_off
|
||||
exit 2
|
||||
fi
|
||||
|
|
@ -138,52 +193,32 @@ test_script() {
|
|||
exit 2
|
||||
fi
|
||||
|
||||
|
||||
echo $BBlue "=== $1 ===" $Color_off
|
||||
|
||||
$executable "$2"
|
||||
# Script
|
||||
echo -n "\n= [SCRIPT] "
|
||||
$executable "$tmp_script" &> $output
|
||||
actual_code=$?
|
||||
$ref_executable "$tmp_script" &> $ref_output
|
||||
ref_code=$?
|
||||
((total_tests++))
|
||||
check_result "$2" "$actual_code" "$ref_code"
|
||||
|
||||
if [[ "$actual_code" -ne "$4" ]]; then
|
||||
echo $BRed "Test failed: expected code $4 but got $actual_code" $Color_off
|
||||
fi
|
||||
# Stdin
|
||||
echo -n "\n= [STDIN] "
|
||||
$executable < "$tmp_script" &> $output
|
||||
actual_code=$?
|
||||
$ref_executable < "$2" &> $ref_output
|
||||
ref_code=$?
|
||||
((total_tests++))
|
||||
check_result "$tmp_script" "$actual_code" "$ref_code"
|
||||
|
||||
echo $BBlue "===================" $Color_off "\n"
|
||||
}
|
||||
|
||||
# @arg test name
|
||||
# @arg input file
|
||||
# @arg expected output
|
||||
# @arg expected return code
|
||||
test_stdin() {
|
||||
# Check input
|
||||
if [[ -z "$1" || -z "$2" || -z "$3" || -z "$4"]]; then
|
||||
echo $BRed "\n\n" "Issue in the testsuite: test_stdin: One or more argument is empty" $Color_off
|
||||
exit 2
|
||||
fi
|
||||
if [[ ! -f "$2" ]]; then
|
||||
echo $BRed "\n\n" "Issue in the testsuite: test_stdin: Second argument is not a file" $Color_off
|
||||
exit 2
|
||||
fi
|
||||
|
||||
|
||||
echo $BBlue "===== $1 =====" $Color_off
|
||||
|
||||
$executable < "$2"
|
||||
actual_code=$?
|
||||
|
||||
if [[ "$actual_code" -ne "$4" ]]; then
|
||||
echo $BRed "Test failed: expected code $4 but got $actual_code" $Color_off
|
||||
fi
|
||||
|
||||
echo $BBlue "===================" $Color_off "\n"
|
||||
|
||||
}
|
||||
|
||||
|
||||
# ***********************************************************
|
||||
#################
|
||||
# Tests #
|
||||
#################
|
||||
|
||||
|
||||
test_str "Hello" "echo Hello there"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue