Skip to content

Commit

Permalink
feat: test case update in helloworld (#11)
Browse files Browse the repository at this point in the history
* feat: removed old files

* feat: test case update in helloworld
  • Loading branch information
GroophyLifefor authored Feb 20, 2024
1 parent f127176 commit 028ddbc
Showing 1 changed file with 71 additions and 24 deletions.
95 changes: 71 additions & 24 deletions exercises/practice/hello-world/HelloWorldTest.bat
Original file line number Diff line number Diff line change
@@ -1,41 +1,88 @@
@echo off
REM ---------------------------------------------------
REM testThatGreeterReturnsTheCorrectGreeting
REM Hello World Unit Test
REM ---------------------------------------------------

REM Initalize result variable
set "slug=HelloWorld"
set "result="
set "expected=Hello, World!"
:Main
REM Initalize result variable
set "slug=HelloWorld"

REM Run the program and store the output in the result variable
if exist %~dp0%slug%.bat (
REM If the file exists in the full path, run it from there
for /f "delims=" %%a in ('%~dp0%slug%.bat') do set result=%%a
CALL :Initialize

) else (
if exist %slug%.bat (
REM If the file exists in the wildcard search path, run it from there
for /f "delims=" %%a in ('%slug%.bat') do set result=%%a
REM --------------------
REM Test Case Start \/\/
REM --------------------
set "expected=Hello, World!"
set "if_success=Test passed"
set "if_failed=Test failed: Your output have to be 'Hello, World!'"
CALL :Assert

) else (
REM Errorlevel = 2: The system cannot find the file specified.
REM Indicates that the file cannot be found in specified location.
echo Could not find "%~dp0%slug%.bat" or "%slug%.bat"
exit /b 2
REM --------------------
REM Test Case End /\/\/\
REM --------------------

)
)
CALL :ResolveStatus
exit /b %errorlevel%
REM End of Main

REM ---------------------------------------------------
REM Assert [..Parameters(up to 9)]
REM ---------------------------------------------------
GOTO :End REM Prevents the code below from being executed
:Assert
set "stdout="

REM Run the program and capture the output then delete the file
CALL %slug%.bat %~1 %~2 %~3 %~4 %~5 %~6 %~7 %~8 %~9 > stdout.bin 2>&1
set /p stdout=<stdout.bin
del stdout.bin

REM Check if the result is correct
if "%result%" == "%expected%" (
if defined if_success (
echo %if_success%

REM Reset the variable to avoid duplicating the message.
set "if_success="
set "if_failed="
)

REM If the result is correct, exit with code 0
echo Test passed
set /a successCount+=1
exit /b 0
) else (
if defined if_failed (
echo %if_failed%

REM Reset the variable to avoid duplicating the message.
set "if_success="
set "if_failed="
)

REM If the result is incorrect, exit with code 1
echo Expected: %expected%
echo Actually: %result%
echo Test failed
set /a failCount+=1
exit /b 1
)
GOTO :EOF REM Go back to the line after the call to :Assert

:Initialize
REM It's for initialize, not about checking empty file
set "successCount=0"
set "failCount=0"
GOTO :EOF REM Go back to the line after the call to :CheckEmptyFile

:ResolveStatus
set "status="
if %failCount% gtr 0 (
REM status: Fail
REM message: The test failed.
exit /b 1

) else (
REM status: Pass
exit /b 0

)
GOTO :EOF REM Go back to the line after the call to :ExportResultAsJson

:End

0 comments on commit 028ddbc

Please sign in to comment.