Add a testing suite for CMD based on CMD scripts.
authorColin Finck <colin@reactos.org>
Sat, 17 May 2008 20:01:21 +0000 (20:01 +0000)
committerColin Finck <colin@reactos.org>
Sat, 17 May 2008 20:01:21 +0000 (20:01 +0000)
Of course, ReactOS' cmd doesn't work at all with the framework at the moment :-)

Some tests were taken from "seta_test.cmd" by Royce. (see file headers)

svn path=/trunk/; revision=33560

rostests/win32/cmd/script_testsuite/lib/testlib.cmd [new file with mode: 0644]
rostests/win32/cmd/script_testsuite/run.cmd [new file with mode: 0644]
rostests/win32/cmd/script_testsuite/tests/at.cmd [new file with mode: 0644]
rostests/win32/cmd/script_testsuite/tests/environment.cmd [new file with mode: 0644]
rostests/win32/cmd/script_testsuite/tests/if.cmd [new file with mode: 0644]
rostests/win32/cmd/script_testsuite/tests/redirect.cmd [new file with mode: 0644]
rostests/win32/cmd/script_testsuite/tests/set.cmd [new file with mode: 0644]

diff --git a/rostests/win32/cmd/script_testsuite/lib/testlib.cmd b/rostests/win32/cmd/script_testsuite/lib/testlib.cmd
new file mode 100644 (file)
index 0000000..5a917ab
--- /dev/null
@@ -0,0 +1,69 @@
+::
+:: PROJECT:     ReactOS CMD Testing Suite
+:: LICENSE:     GPL v2 or any later version
+:: FILE:        lib/testlib.cmd
+:: PURPOSE:     Library with functions available for all tests
+:: COPYRIGHT:   Copyright 2008 Colin Finck <mail@colinfinck.de>
+::
+
+:: Indicate that a test ran successfully
+:_successful
+set /a test_count+=1
+set /a successful_tests+=1
+goto :EOF
+
+
+:: Indicate that a test failed
+:: @param 1  Description of the test that failed
+:_failed
+set /a test_count+=1
+set /a failed_tests+=1
+echo Test "%~1" failed!
+goto :EOF
+
+
+:: Test whether a call succeeded
+:: @param 1  The test command to run and check
+:_test
+%~1
+
+if "%errorlevel%" == "0" (
+       call :_successful
+) else (
+       call :_failed "%~1"
+)
+goto :EOF
+
+
+:: Test whether a call failed
+:: @param 1  The test command to run and check
+:_testnot
+%~1
+
+if "%errorlevel%" == "0" (
+       call :_failed "%~1"
+) else (
+       call :_successful
+)
+goto :EOF
+
+
+:: Test the value of a variable
+:: @param 1  The variable to check (like %test%)
+:: @param 2  The variable name (like test)
+:: @param 3  The expected result (like 5)
+::           If this parameter wasn't given, _testvar checks if the variable is not ""
+:_testvar
+if "%~3" == "" (
+       set testvar_operator=not
+) else (
+       set testvar_operator=
+)
+
+if %testvar_operator% "%~1" == "%~3" (
+       call :_successful
+) else (
+       call :_failed "if %%~2%% == %~3, actual result was %~1"
+)
+
+goto :EOF
diff --git a/rostests/win32/cmd/script_testsuite/run.cmd b/rostests/win32/cmd/script_testsuite/run.cmd
new file mode 100644 (file)
index 0000000..57a6c1c
--- /dev/null
@@ -0,0 +1,43 @@
+::
+:: PROJECT:     ReactOS CMD Testing Suite
+:: LICENSE:     GPL v2 or any later version
+:: FILE:        run.cmd
+:: PURPOSE:     Runs the testing scripts
+:: COPYRIGHT:   Copyright 2008 Colin Finck <mail@colinfinck.de>
+::
+
+@echo off
+cls
+echo ReactOS CMD Testing Suite
+echo ==========================
+echo.
+
+:: Preparations
+set failed_tests=0
+set successful_tests=0
+set test_count=0
+
+if exist "temp\." (
+       rmdir /s /q "temp"
+)
+
+mkdir "temp"
+
+:: Run the tests
+call :_runtest at
+call :_runtest environment
+call :_runtest if
+call :_runtest redirect
+call :_runtest set
+
+:: Print the summary and clean up
+echo Executed %test_count% tests, %successful_tests% successful, %failed_tests% failed
+rmdir /s /q "temp"
+goto :EOF
+
+:: Functions
+:_runtest
+type "tests\%~1.cmd" > "temp\%~1.cmd"
+type "lib\testlib.cmd" >> "temp\%~1.cmd"
+call "temp\%~1.cmd"
+goto :EOF
diff --git a/rostests/win32/cmd/script_testsuite/tests/at.cmd b/rostests/win32/cmd/script_testsuite/tests/at.cmd
new file mode 100644 (file)
index 0000000..cf32abd
--- /dev/null
@@ -0,0 +1,20 @@
+::
+:: PROJECT:     ReactOS CMD Testing Suite
+:: LICENSE:     GPL v2 or any later version
+:: FILE:        tests/at.cmd
+:: PURPOSE:     Tests for the correct parsing of the "@" character
+:: COPYRIGHT:   Copyright 2008 Colin Finck <mail@colinfinck.de>
+::
+
+:: Calling a command should work with any number of "@" characters prepended
+call :_test "@echo Test >nul"
+call :_test "@@echo Test >nul"
+
+:: Files with an "@" sign at the beginning should work as well
+echo @^@echo Test > "temp\@file.cmd"
+call :_test "call temp\@file.cmd >nul"
+
+echo ^@echo Test > "temp\@file.cmd"
+call :_test "call temp\@file.cmd >nul"
+
+goto :EOF
diff --git a/rostests/win32/cmd/script_testsuite/tests/environment.cmd b/rostests/win32/cmd/script_testsuite/tests/environment.cmd
new file mode 100644 (file)
index 0000000..fc552cc
--- /dev/null
@@ -0,0 +1,47 @@
+::
+:: PROJECT:     ReactOS CMD Testing Suite
+:: LICENSE:     GPL v2 or any later version
+:: FILE:        tests/environment.cmd
+:: PURPOSE:     Tests for the environment (like automatically set variables)
+:: COPYRIGHT:   Copyright 2008 Colin Finck <mail@colinfinck.de>
+::
+
+:: ~0 contains the called path to the current script file
+call :_testvar %~0 ~0
+
+:: ~d0 contains the drive of the current script file
+call :_testvar %~d0 ~d0
+
+:: ~p0 contains the path of the current script file without the drive letter
+call :_testvar %~p0 ~p0
+
+:: ~dp0 contains the path to the current script file with the drive letter
+call :_testvar %~dp0 ~dp0
+
+:: ~dpf0 contains the full path to the current script file
+call :_testvar %~dpf0 ~dpf0
+
+:: ~n0 contains the name of the current script file without extension
+call :_testvar %~n0 ~n0
+
+:: ~x0 contains the extension of the current script file
+call :_testvar %~x0 ~x0
+
+:: ~a0 contains the attributes of the current script file
+call :_testvar %~a0 ~a0
+
+:: ~t0 contains the date and time of the current script file
+call :_testvar "%~t0" ~t0
+
+:: ~z0 contains the file size of the current script file in bytes
+call :_testvar %~z0 ~z0
+
+:: ~s0 and ~spf0 contain the short path to the current script file
+call :_testvar %~s0 ~s0
+call :_testvar %~s0 ~s0 %~spf0%
+
+:: Now try to verify that the information is valid
+set test_path=%~d0%~p0%~n0%~x0
+call :_testvar %test_path% test_path %~dpf0
+
+goto :EOF
diff --git a/rostests/win32/cmd/script_testsuite/tests/if.cmd b/rostests/win32/cmd/script_testsuite/tests/if.cmd
new file mode 100644 (file)
index 0000000..33f6aa5
--- /dev/null
@@ -0,0 +1,26 @@
+::
+:: PROJECT:     ReactOS CMD Testing Suite
+:: LICENSE:     GPL v2 or any later version
+:: FILE:        tests/if.cmd
+:: PURPOSE:     Tests for the "if" command
+:: COPYRIGHT:   Copyright 2005 Royce Mitchell III
+::              Copyright 2008 Colin Finck <mail@colinfinck.de>
+::
+
+:: Bugs in the if code
+if not "=="=="==" call :_failed "if not "=="=="==""
+if "=="=="==" call :_successful
+
+if "1"=="2" (
+       call :_failed "if "1"=="2""
+) else (
+       call :_successful
+)
+
+if not "1"=="1" (
+       call :_failed "if "1"=="1""
+) else (
+       call :_successful
+)
+
+goto :EOF
diff --git a/rostests/win32/cmd/script_testsuite/tests/redirect.cmd b/rostests/win32/cmd/script_testsuite/tests/redirect.cmd
new file mode 100644 (file)
index 0000000..32c0eed
--- /dev/null
@@ -0,0 +1,28 @@
+::
+:: PROJECT:     ReactOS CMD Testing Suite
+:: LICENSE:     GPL v2 or any later version
+:: FILE:        tests/redirect.cmd
+:: PURPOSE:     Tests for redirections
+:: COPYRIGHT:   Copyright 2008 Colin Finck <mail@colinfinck.de>
+::
+
+:: One redirect, the file must exist
+call :_test "echo moo > temp\redirect_temp.txt"
+call :_test "type temp\redirect_temp.txt >nul"
+call :_test "find "moo" temp\redirect_temp.txt >nul"
+
+:: Add the string yet another time to the file
+call :_test "echo moo >> temp\redirect_temp.txt"
+
+set moo_temp=0
+
+:: Count the moo's in the file (must be 2 now)
+:: No idea why so many percent signs are necessary here :-)
+call :_test "for /f "usebackq" %%%%i in (`findstr moo temp\redirect_temp.txt`) do set /a moo_temp+=1"
+call :_testvar %moo_temp% moo_temp 2
+
+:: Two redirects, the file in the middle mustn't exist after this call
+call :_test "echo moo > temp\redirect_temp2.txt > nul"
+call :_testnot "type temp\redirect_temp2.txt 2>nul"
+
+goto :EOF
diff --git a/rostests/win32/cmd/script_testsuite/tests/set.cmd b/rostests/win32/cmd/script_testsuite/tests/set.cmd
new file mode 100644 (file)
index 0000000..4e292bb
--- /dev/null
@@ -0,0 +1,104 @@
+::
+:: PROJECT:     ReactOS CMD Testing Suite
+:: LICENSE:     GPL v2 or any later version
+:: FILE:        tests/set.cmd
+:: PURPOSE:     Tests for the "set" command
+:: COPYRIGHT:   Copyright 2005 Royce Mitchell III
+::              Copyright 2008 Colin Finck <mail@colinfinck.de>
+::
+
+:: Test the /A parameter
+call :_test "set /a a=1"
+call :_testvar %a% a 1
+
+call :_test "set /a b=a"
+call :_testvar %b% b 1
+
+call :_test "set /a a=!5"
+call :_testvar %a% a 0
+
+call :_test "set /a a=!a"
+call :_testvar %a% a 1
+
+call :_test "set /a a=~5"
+call :_testvar %a% a -6
+
+call :_test "set /a a=5,a=-a"
+call :_testvar %a% a -5
+
+call :_test "set /a a=5*7"
+call :_testvar %a% a 35
+
+call :_test "set /a a=2000/10"
+call :_testvar %a% a 200
+
+call :_test "set /a a=42%%%%9"
+call :_testvar %a% a 6
+
+call :_test "set /a a=5%%2"
+call :_testvar %a% a 5
+
+call :_test "set /a a=42%13"
+call :_testvar %a% a 423
+
+call :_test "set /a a=7+9"
+call :_testvar %a% a 16
+
+call :_test "set /a a=9-7"
+call :_testvar %a% a 2
+
+set /a a=9^<^<2
+call :_testvar %a% a 36
+
+set /a a=36^>^>2
+call :_testvar %a% a 9
+
+set /a a=42^&9
+call :_testvar %a% a 8
+
+set /a a=32^9
+call :_testvar %a% a 329
+
+set /a a=32^^9
+call :_testvar %a% a 41
+
+set /a a=10^|22
+call :_testvar %a% a 30
+
+call :_test "set /a a=2,a*=3"
+call :_testvar %a% a 6
+
+call :_test "set /a a=11,a/=2"
+call :_testvar %a% a 5
+
+call :_test "set /a a=42,a%%%%=9"
+call :_testvar %a% a 6
+
+call :_test "set /a a=7,a+=9"
+call :_testvar %a% a 16
+
+call :_test "set /a a=9,a-=7"
+call :_testvar %a% a 2
+
+set /a a=42,a^&=9
+call :_testvar %a% a 8
+
+set /a a=32,a^^=9
+call :_testvar %a% a 41
+
+set /a a=10,a^|=22
+call :_testvar %a% a 30
+
+set /a a=9,a^<^<=2
+call :_testvar %a% a 36
+
+set /a a=36,a^>^>=2
+call :_testvar %a% a 9
+
+call :_test "set /a a=1,2"
+call :_testvar %a% a 1
+
+call :_test "set /a a=(a=1,a+2)"
+call :_testvar %a% a 3
+
+goto :EOF