gambas-source-code/comp/src/gb.test/.src/TestSuite/UnitTest.class

163 lines
3.9 KiB
Text
Raw Normal View History

' Gambas class file
Export
''' The static procedure Unittest.Main() starts test(s).
'' Runs all tests in all testcontainers and prints the result to the console.
'' With UnitTest.Main(NameTestModule) the tests can be restricted to only those of a single test module.
'' With UnitTest.Main(NameTestModule, NameProcedure) only a single test can be accomplished.
Static Public Sub Main(Optional NameTestModule As String, Optional NameProcedure As String, Optional DoSelfTest As Boolean)
'Track.Initialize()
RunTests(NameTestModule, NameProcedure, DoSelfTest)
PrintSummary()
End
Static Private Sub PrintSummary()
Dim bSuccess As Boolean = True
If Track.NOKs.Count > 0 Then Print "# Failed tests: " & PrintNumbers(Track.NOKs)
'Plan
If Track.Plan > 0 Then
Print "1.." & Track.Plan
Else
Print "0..0"
Endif
If Track.NOKs.Count > 0 Or If Track.Counter = 0 Then
bSuccess = False
Endif
If Track.Plan <> Track.Counter Then
bSuccess = False
Print "# Plan was " & Track.Plan & " but assertions counted was " & Track.Counter
Endif
If bSuccess = True Then
Print "# ------- Success! -------"
Else
Print "# ------- No success! -------"
Endif
Track.Reset()
End
Static Private Function PrintNumbers(Numbers As Long[]) As String
Dim Number As Long
Dim s As New String[]
For Each Number In Numbers
s.Add(Number)
Next
Return s.Join(",")
End
'
'' Run all tests, optional limited by Container or TestCaseName. Track contains .
Static Private Function RunTests(SingleTestModule As String, Optional NameProcedure As String, Optional DoSelfTest As Boolean)
Dim sTestModule As String
Dim TestModule As Class
Dim Suite As New TestSuite
'FIXME: If included as component then TestContainers can only be loaded if they contain the magic word Export
For Each sTestModule In GetAllTestTestModules(SingleTestModule, DoSelfTest)
TestModule = Class.Load(sTestModule)
Suite.AddAllTestCases(TestModule, NameProcedure)
Next
Suite.Run()
End
Static Function GetAllTestTestModules(Optional SingleTestModule As String, Optional DoSelfTest As Boolean) As String[]
Dim TestClass As Class
Dim TestModuleNames As New String[]
Dim sNames As New String[]
Dim sName As String
If Exist(".../.test")
sNames = Split(File.Load(".../.test"), gb.Lf, Null, True)
Endif
Assert sNames
sNames.Sort
For Each sName In sNames
GoSub AddClass
Next
Goto Done
AddClass:
TestClass = Class.Load(sName)
If TestClass Then
'sName = Class.Stat(sName).Name
'Atention: Class.Stat(sName).Name does not work if included as component
'Then this creates the error:
'Bail out! Error in Unittest->GetAllTestModuleNames: Unknown symbol 'Stat' in class 'Class'
If Not TestModuleNames.Exist(sName) Then
If testclass.Symbols.Exist("ThisIsAnUnitTestSelfTest")
If DoSelfTest = True Then
GoSub AddTestModule
Endif
Else
GoSub AddTestModule
Endif
Endif
Endif
Return
AddTestModule:
If SingleTestModule = Null Then
TestModuleNames.Add(sName)
Else
If Lower(sName) = Lower(SingleTestModule) Then
TestModuleNames.Add(sName)
Endif
Endif
Return
Done:
TestModuleNames.Sort
' Print "# These TestContainer will be executed:\n#\n# " & TestModuleNames.Join("\n# ") & "\n"
Return TestModuleNames
Catch
Print "Bail out! Error in Unittest->GetAllTestModuleNames: " & Error.Text
Print "# ------- No success! -------"
Quit
End
' Private Function CaseNames_Read() As String[]
'
' Return $CaseNames
'
' End
'
' '' Returns the classname of the TestContainer
' Private Function Name_Read() As String
'
' Return Object.Class(Me).Name
'
' End