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

feat: test case update in helloworld #11

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion exercises/practice/hello-world/Greeter.bat

This file was deleted.

23 changes: 0 additions & 23 deletions exercises/practice/hello-world/GreeterTest.bat

This file was deleted.

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