2020-04-27 12:01:47 +02:00
|
|
|
' Gambas module file
|
2019-11-15 22:33:54 +01:00
|
|
|
|
|
|
|
Export
|
|
|
|
|
2020-05-07 12:57:55 +02:00
|
|
|
'' Special value for unspecified test plan
|
|
|
|
Public Const NO_PLAN As Integer = -1
|
|
|
|
|
|
|
|
Property Read _Printer As TapPrinter
|
2020-05-02 18:35:48 +02:00
|
|
|
Property Read _Finished As Boolean
|
2020-04-27 12:01:47 +02:00
|
|
|
Property _Next As TestAssertion
|
|
|
|
|
|
|
|
Private $hPrinter As New TapPrinter
|
|
|
|
Private $hNext As TestAssertion
|
|
|
|
|
2020-05-13 11:14:19 +02:00
|
|
|
'' The static procedure Test.Main() starts tests. By default it runs all testmethods in all
|
2020-05-12 00:49:33 +02:00
|
|
|
'' testmodules ordered by name and prints the result to the console.
|
2020-05-13 11:14:19 +02:00
|
|
|
''
|
|
|
|
'' The optional string Tests ca be used to define a TestSuite which can be used to choose any
|
2020-05-12 00:49:33 +02:00
|
|
|
'' combination of testmodule and testmethod, separated by comma.
|
2020-05-13 11:14:19 +02:00
|
|
|
''
|
|
|
|
'' A testmodule is a special class with the ending test. It must contain at minimum one testmethod.
|
|
|
|
''
|
|
|
|
'' A testmethod is a public sub in a testmodule whose name is not
|
2020-05-11 22:26:08 +02:00
|
|
|
'' "_Setup", "_Teardown", "_SetupEach" or "_TeardownEach".
|
2020-05-13 11:14:19 +02:00
|
|
|
''
|
|
|
|
'' A testmodule can be choosen by it's name, a testmethod by it's testmodules'
|
2020-05-12 00:49:33 +02:00
|
|
|
'' name followed by a dot and the name of the testmethod.
|
2020-05-13 11:14:19 +02:00
|
|
|
''
|
|
|
|
'' Example: Test.Main("TestWWW, TestMail.Send, TestMail.Receive")
|
|
|
|
''
|
2020-05-12 00:49:33 +02:00
|
|
|
'' runs all testmethods in TestWWW and the testmethods Send and Receive of the testmodule TestMail.
|
2020-05-13 11:14:19 +02:00
|
|
|
''
|
|
|
|
'' Public methods called "_Setup", "_Teardown", "_SetupEach" or "_TeardownEach" can be used to create a testfixture,
|
2020-05-02 11:53:30 +02:00
|
|
|
'' which defines a special environment for the tests.
|
2019-11-15 22:33:54 +01:00
|
|
|
|
2020-04-25 05:22:10 +02:00
|
|
|
Public Sub Main(Optional Tests As String)
|
2019-11-15 22:33:54 +01:00
|
|
|
|
2020-05-02 18:35:48 +02:00
|
|
|
Test._Reset() ' only if you run this Main multiple times per process, which you shouldn't
|
2020-04-25 05:22:10 +02:00
|
|
|
RunTests(Tests)
|
|
|
|
PrintSummary()
|
2020-05-07 13:01:31 +02:00
|
|
|
Quit ExitCode()
|
2019-11-15 22:33:54 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-24 17:59:14 +01:00
|
|
|
Private Sub PrintSummary()
|
|
|
|
|
2020-05-07 12:57:55 +02:00
|
|
|
With $hPrinter.Session
|
|
|
|
Test._Print(gb.Lf) ' better readability for humans
|
2020-05-01 18:59:20 +02:00
|
|
|
Test.Note(Subst$(("Ran: '&1' "), .Summary.Description))
|
2020-04-27 12:01:47 +02:00
|
|
|
If .TestsRun <> .Plan Then Test.Note(Subst$(("Planned &1 tests but ran &2"), .Plan, .TestsRun))
|
2020-05-01 15:57:53 +02:00
|
|
|
Test.Note(gb.Lf)
|
|
|
|
|
2020-05-01 18:59:20 +02:00
|
|
|
ShowTestCollection(("&1 skipped:"), FindSkips(.Summary.Subtests, ""))
|
|
|
|
ShowTestCollection(("&1 todo:"), FindTodos(.Summary.Subtests, ""))
|
|
|
|
ShowTestCollection(("&1 bonus:"), FindBonus(.Summary.Subtests, ""))
|
2020-05-07 12:57:55 +02:00
|
|
|
|
2020-05-11 23:28:42 +02:00
|
|
|
If Not .Summary.Success Then ShowTestCollection(("&1 tests failed:"), FindFailures(.Summary.Subtests, ""))
|
2020-05-01 18:59:20 +02:00
|
|
|
Test.Note(IIf(.Summary.Success, "PASSED", "FAILED"))
|
2020-02-24 17:59:14 +01:00
|
|
|
End With
|
2019-11-15 22:33:54 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-05-07 13:01:31 +02:00
|
|
|
Private Function ExitCode() As Integer
|
|
|
|
|
|
|
|
Return IIf($hPrinter.Session.Summary.Success, 0, 1)
|
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-05-01 15:57:53 +02:00
|
|
|
'' Prints a Collection[] of tests as returned by FindFailures, FindSkips, FindTodos.
|
|
|
|
'' _Description_ can contain '&1' which is substituted for _TestCollection_.Count.
|
|
|
|
Private Sub ShowTestCollection(Description As String, TestCollection As Collection[])
|
|
|
|
|
|
|
|
Dim cTest As Collection
|
|
|
|
|
|
|
|
If TestCollection.Count Then
|
|
|
|
Test.Note(Subst$(Description, TestCollection.Count))
|
|
|
|
For Each cTest In TestCollection
|
|
|
|
Dim hTest As TestAssertion = cTest!Assertion
|
|
|
|
Test.Note(Subst$(("&2: &1 -- &3 &4"), cTest["Path"], hTest.Id, hTest.Description, IIf(hTest.Comment, "# " & hTest.Comment, "")))
|
|
|
|
Next
|
|
|
|
Test.Note(gb.Lf)
|
|
|
|
Endif
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Private Function FindFailures(Tests As TestAssertion[], Prefix As String) As Collection[]
|
2020-04-25 05:22:10 +02:00
|
|
|
|
|
|
|
Dim hTest As TestAssertion
|
|
|
|
Dim sName As String
|
2020-05-01 15:57:53 +02:00
|
|
|
Dim aRet As New Collection[]
|
2020-04-25 05:22:10 +02:00
|
|
|
|
|
|
|
For Each hTest In Tests
|
|
|
|
sName = Prefix &/ hTest.Description
|
|
|
|
' Only show the deepest subtests that caused failures.
|
2020-05-01 15:57:53 +02:00
|
|
|
If Not hTest.Success And If Not hTest.Subtests.Count Then aRet.Add(["Path": Prefix, "Assertion": hTest])
|
|
|
|
aRet.Insert(FindFailures(hTest.Subtests, sName))
|
|
|
|
Next
|
|
|
|
Return aRet
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Private Function FindSkips(Tests As TestAssertion[], Prefix As String) As Collection[]
|
|
|
|
|
|
|
|
Dim hTest As TestAssertion
|
|
|
|
Dim sName As String
|
|
|
|
Dim aRet As New Collection[]
|
|
|
|
|
|
|
|
For Each hTest In Tests
|
|
|
|
sName = Prefix &/ hTest.Description
|
2020-05-07 12:57:55 +02:00
|
|
|
If hTest.Directive = TestAssertion.SKIP Then aRet.Add(["Path": Prefix, "Assertion": hTest])
|
2020-05-01 15:57:53 +02:00
|
|
|
aRet.Insert(FindSkips(hTest.Subtests, sName))
|
|
|
|
Next
|
|
|
|
Return aRet
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Private Function FindTodos(Tests As TestAssertion[], Prefix As String) As Collection[]
|
|
|
|
|
|
|
|
Dim hTest As TestAssertion
|
|
|
|
Dim sName As String
|
|
|
|
Dim aRet As New Collection[]
|
|
|
|
|
|
|
|
For Each hTest In Tests
|
|
|
|
sName = Prefix &/ hTest.Description
|
2020-05-07 12:57:55 +02:00
|
|
|
If hTest.Directive = TestAssertion.TODO And If Not hTest.Ok Then aRet.Add(["Path": Prefix, "Assertion": hTest])
|
2020-05-01 15:57:53 +02:00
|
|
|
aRet.Insert(FindTodos(hTest.Subtests, sName))
|
|
|
|
Next
|
|
|
|
Return aRet
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Private Function FindBonus(Tests As TestAssertion[], Prefix As String) As Collection[]
|
|
|
|
|
|
|
|
Dim hTest As TestAssertion
|
|
|
|
Dim sName As String
|
|
|
|
Dim aRet As New Collection[]
|
|
|
|
|
|
|
|
For Each hTest In Tests
|
|
|
|
sName = Prefix &/ hTest.Description
|
2020-05-07 12:57:55 +02:00
|
|
|
If hTest.Directive = TestAssertion.TODO And If hTest.Ok Then aRet.Add(["Path": Prefix, "Assertion": hTest])
|
2020-05-01 15:57:53 +02:00
|
|
|
aRet.Insert(FindBonus(hTest.Subtests, sName))
|
2020-04-25 05:22:10 +02:00
|
|
|
Next
|
2020-05-01 15:57:53 +02:00
|
|
|
Return aRet
|
2020-04-25 05:22:10 +02:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-05-12 00:49:33 +02:00
|
|
|
'' Run all tests, optional limited by module or method name.
|
2019-11-15 22:33:54 +01:00
|
|
|
|
2020-04-25 05:22:10 +02:00
|
|
|
Private Function RunTests(Tests As String)
|
2019-11-15 22:33:54 +01:00
|
|
|
|
2020-04-08 12:39:55 +02:00
|
|
|
Dim aTestCommands As TestCommand[]
|
2020-02-23 12:38:53 +01:00
|
|
|
Dim sTestModule As String
|
|
|
|
Dim TestModule As Class
|
2019-11-15 22:33:54 +01:00
|
|
|
Dim Suite As New TestSuite
|
|
|
|
|
2020-04-08 12:39:55 +02:00
|
|
|
aTestCommands = TestCommand.ParseCommands(Tests)
|
|
|
|
|
2020-05-12 00:49:33 +02:00
|
|
|
For Each sTestModule In GetAllTestModules(aTestCommands)
|
2020-02-23 12:38:53 +01:00
|
|
|
TestModule = Class.Load(sTestModule)
|
2020-04-08 12:39:55 +02:00
|
|
|
Suite.AddAllTestCases(TestModule, aTestCommands)
|
2019-11-15 22:33:54 +01:00
|
|
|
Next
|
|
|
|
|
2020-05-07 12:57:55 +02:00
|
|
|
$hPrinter.Session.Summary.Description = Tests
|
2020-04-25 05:22:10 +02:00
|
|
|
Suite.Run()
|
2020-05-02 18:35:48 +02:00
|
|
|
If Not Test._Finished Then Test._Finish()
|
2020-02-27 20:28:49 +01:00
|
|
|
|
2019-11-15 22:33:54 +01:00
|
|
|
End
|
|
|
|
|
2020-05-12 00:49:33 +02:00
|
|
|
Private Function GetAllTestModules(Optional Commands As TestCommand[]) As String[]
|
2019-11-15 22:33:54 +01:00
|
|
|
|
|
|
|
Dim TestClass As Class
|
2020-02-23 12:38:53 +01:00
|
|
|
Dim TestModuleNames As New String[]
|
2019-11-15 22:33:54 +01:00
|
|
|
Dim sNames As New String[]
|
|
|
|
Dim sName As String
|
2020-04-08 12:39:55 +02:00
|
|
|
Dim Command As TestCommand
|
2019-11-15 22:33:54 +01:00
|
|
|
|
2020-05-11 23:28:42 +02:00
|
|
|
If Commands = Null Then
|
|
|
|
Commands = New TestCommand[]
|
|
|
|
Endif
|
|
|
|
|
2020-02-23 12:38:53 +01:00
|
|
|
If Exist(".../.test")
|
|
|
|
sNames = Split(File.Load(".../.test"), gb.Lf, Null, True)
|
|
|
|
Endif
|
2019-11-15 22:33:54 +01:00
|
|
|
|
|
|
|
Assert sNames
|
|
|
|
|
|
|
|
sNames.Sort
|
2020-04-25 11:38:52 +02:00
|
|
|
|
2019-11-15 22:33:54 +01:00
|
|
|
For Each sName In sNames
|
2020-04-07 15:03:13 +02:00
|
|
|
TestClass = Class.Load(sName)
|
|
|
|
If Not TestClass Then Error.Raise(Subst$(("Could not load test module '&1'"), sName))
|
|
|
|
If TestModuleNames.Exist(sName) Then Continue
|
2020-04-08 12:39:55 +02:00
|
|
|
If Commands.Count = 0 Then
|
|
|
|
'Add every Testmodule
|
2020-02-23 12:38:53 +01:00
|
|
|
TestModuleNames.Add(sName)
|
2020-04-07 15:03:13 +02:00
|
|
|
Else
|
2020-04-25 11:38:52 +02:00
|
|
|
'Add only testmodules whose names exist in Commands
|
2020-04-08 12:39:55 +02:00
|
|
|
For Each Command In Commands
|
2020-04-26 11:23:01 +02:00
|
|
|
If Not sNames.Exist(Command.ModuleName) Then
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.BailOut(Subst$(("There is no test called &1."), Command.ModuleName))
|
2020-04-26 11:23:01 +02:00
|
|
|
Endif
|
2020-04-08 12:39:55 +02:00
|
|
|
If Lower(Command.ModuleName) = Lower(sName) Then
|
|
|
|
TestModuleNames.Add(sName)
|
|
|
|
Endif
|
|
|
|
Next
|
2019-11-15 22:33:54 +01:00
|
|
|
Endif
|
2020-04-07 15:03:13 +02:00
|
|
|
Next
|
2019-11-15 22:33:54 +01:00
|
|
|
|
2020-02-23 12:38:53 +01:00
|
|
|
TestModuleNames.Sort
|
2020-04-25 11:38:52 +02:00
|
|
|
|
2020-02-23 12:38:53 +01:00
|
|
|
Return TestModuleNames
|
2019-11-15 22:33:54 +01:00
|
|
|
|
2020-01-04 14:48:51 +01:00
|
|
|
Catch
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.BailOut("Error in " & Error.Where & ": " & Error.Text)
|
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-05-13 11:14:19 +02:00
|
|
|
'' Returns an collection of the testmodules and their testmethods.
|
|
|
|
'' Key is the name of the testmodule, Value is a string array with the names of the testmethods it contains
|
|
|
|
|
|
|
|
Public Function AllTestsCollection() As Collection
|
|
|
|
|
|
|
|
Dim sModules As String[]
|
|
|
|
Dim sModule As String
|
|
|
|
Dim TestModule As Class
|
|
|
|
Dim Alltests As New Collection
|
2020-05-11 23:28:42 +02:00
|
|
|
|
2020-05-13 11:14:19 +02:00
|
|
|
sModules = GetAllTestModules(Null)
|
2020-05-11 23:28:42 +02:00
|
|
|
|
2020-05-13 11:14:19 +02:00
|
|
|
For Each sModule In sModules
|
|
|
|
TestModule = Class.Load(sModule)
|
|
|
|
Alltests.Add(TestSuite.GetTestsFromTestModule(Testmodule), TestModule.Name)
|
|
|
|
Next
|
2020-05-11 22:26:08 +02:00
|
|
|
|
2020-05-11 23:28:42 +02:00
|
|
|
Return Alltests
|
2020-05-11 22:26:08 +02:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-05-13 11:14:19 +02:00
|
|
|
'' Returns a Json string containing the testmodules and their testmethods.
|
|
|
|
|
|
|
|
Public Function AllTestsJson() As String
|
|
|
|
|
|
|
|
Dim tests As Collection
|
|
|
|
Dim asTests As String[]
|
|
|
|
Dim asLines As New String[]
|
|
|
|
Dim sBuf As String
|
|
|
|
|
|
|
|
tests = AllTestsCollection()
|
|
|
|
|
|
|
|
For Each tests
|
|
|
|
sBuf = "\"" & tests.Key & "\":"
|
|
|
|
asTests = tests[tests.Key]
|
|
|
|
sBuf &= "[" & asTests.Join(",", "\"") & "]"
|
|
|
|
asLines.Add(sBuf)
|
|
|
|
Next
|
|
|
|
|
|
|
|
|
|
|
|
Return "{" & asLines.Join(",") & "}"
|
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
' ------------------------------------------------- Test controls
|
|
|
|
|
2020-05-02 18:35:48 +02:00
|
|
|
'' Prints "Bail out!" and stops all testing immediately.
|
2020-04-27 12:01:47 +02:00
|
|
|
Public Sub BailOut(Optional Comment As String)
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
$hPrinter.BailOut(Comment)
|
2020-02-27 20:28:49 +01:00
|
|
|
Quit 1
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
End
|
2020-05-02 18:35:48 +02:00
|
|
|
'' Synonym for Note, prints Comment with leading #
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
Public Sub Diagnostic(Comment As String)
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
$hPrinter.Diagnostic(Comment)
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
End
|
2020-05-02 18:35:48 +02:00
|
|
|
'' Synonym for Diagnostic, prints Comment with leading #
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
Public Sub Note(Comment As String)
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
$hPrinter.Note(Comment)
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
End
|
2020-05-02 18:35:48 +02:00
|
|
|
'' Prints Line to Stdout
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-05-07 12:57:55 +02:00
|
|
|
Public Sub _Print({Line} As String)
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
$hPrinter.Print({Line})
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
End
|
|
|
|
|
2020-05-07 12:57:55 +02:00
|
|
|
Private Function _Printer_Read() As TapPrinter
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
Return $hPrinter
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
End
|
|
|
|
|
2020-05-02 18:35:48 +02:00
|
|
|
Private Function _Finished_Read() As Boolean
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
Return $hPrinter.Session.Finished
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
End
|
|
|
|
|
2020-05-02 18:35:48 +02:00
|
|
|
Public Sub _Reset()
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
$hPrinter = New TapPrinter As "Printer"
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
End
|
|
|
|
|
|
|
|
Private Function _Next_Read() As TestAssertion
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
If $hNext = Null Then
|
|
|
|
$hNext = New TestAssertion
|
|
|
|
Endif
|
|
|
|
Return $hNext
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
End
|
|
|
|
|
|
|
|
Private Sub _Next_Write(Value As TestAssertion)
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
$hNext = Value
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2019-11-15 22:33:54 +01:00
|
|
|
End
|
2020-04-27 12:01:47 +02:00
|
|
|
|
2020-05-02 18:35:48 +02:00
|
|
|
Public Sub _Subtest(Description As String, Optional Tests As Integer, Optional Comment As String)
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-05-07 12:57:55 +02:00
|
|
|
Test._Printer.Subtest(Description, Tests, Comment)
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
End
|
|
|
|
|
|
|
|
Public Sub Plan(Tests As Integer, Optional Comment As String)
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-05-07 12:57:55 +02:00
|
|
|
Test._Printer.Plan(Tests, Comment)
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
End
|
|
|
|
|
|
|
|
Public Sub SkipAll(Optional Comment As String)
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-05-07 12:57:55 +02:00
|
|
|
Test._Printer.SkipAll(Comment)
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
End
|
|
|
|
|
2020-05-02 18:35:48 +02:00
|
|
|
Public Sub _Finish()
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-05-07 12:57:55 +02:00
|
|
|
Test._Printer.Finish()
|
2020-05-13 11:14:19 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
End
|