feat: Extended base tools for testing and made a basic test

This commit is contained in:
Gu://em_ 2026-01-21 19:17:29 +01:00
parent 4d27cea46b
commit 402b1614b5

View file

@ -9,7 +9,11 @@ exit 2
################### ###################
executable="../../src/42sh" 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 # # 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 test name
# @arg input string # @arg input string
# @arg expected output test_str() {
# @arg expected return code
test_cmd() {
# Check input # 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_cmd: One or more argument is empty" $Color_off echo $BRed "\n\n" "Issue in the testsuite: test_str: One or more argument is empty" $Color_off
exit 2 exit 2
fi fi
echo $BBlue "=== $1 ===" $Color_off echo $BBlue "=== $1 ===" $Color_off
$executable -c "$2" echo "$2" > $tmp_script
actual_code=$?
if [[ "$actual_code" -ne "$4" ]]; then # Arg
echo $BRed "Test failed: expected code $4 but got $actual_code" $Color_off echo -n "\n= [ARG] "
fi $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" echo $BBlue "===================" $Color_off "\n"
} }
# @arg test name # @arg test name
# @arg input script # @arg input script
# @arg expected output
# @arg expected return code
test_script() { test_script() {
# Check input # 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 echo $BRed "\n\n" "Issue in the testsuite: test_script: One or more argument is empty" $Color_off
exit 2 exit 2
fi fi
@ -138,52 +193,32 @@ test_script() {
exit 2 exit 2
fi fi
echo $BBlue "=== $1 ===" $Color_off echo $BBlue "=== $1 ===" $Color_off
$executable "$2" # Script
echo -n "\n= [SCRIPT] "
$executable "$tmp_script" &> $output
actual_code=$? 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 # Stdin
echo $BRed "Test failed: expected code $4 but got $actual_code" $Color_off echo -n "\n= [STDIN] "
fi $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" 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 # # Tests #
################# #################
test_str "Hello" "echo Hello there"