5004f20609
[INTERPRETER] * NEW: Remove the now useless testing specific code. * NEW: 'gbx3 -T' now just loads the 'gb.test' component and calls Test.Main() passing it the '-T' option argument. [GB.TEST] * NEW: Move 'gb.test' sources in '/main/lib'. * NEW: 'gb.test' has now a part written in C that allows him to load project classes on demand.
137 lines
3.8 KiB
Text
137 lines
3.8 KiB
Text
' Gambas class file
|
|
|
|
''' The TestSuite class represents a suite of different tests to be run. The TestSuite contains
|
|
''' a part-whole hierarchy of objects that implement the ITest interface --
|
|
''' including TestCase objects and other TestSuite objects. Executing the Run method for the
|
|
''' TestSuite will execute all test cases that it contains. The TestSuite class also provides
|
|
''' methods for add all test cases contained in a TestModule object into the suite.
|
|
|
|
Property Read Tests As TestCase[]
|
|
Private $Tests As New TestCase[]
|
|
|
|
'' Runs all tests contained within the collection and collects the result in the Track parameter.
|
|
Public Sub Run()
|
|
|
|
Dim oTest As TestCase
|
|
Dim CurrentTestModule, LastTestModule As Class
|
|
Dim CurrentAction As String
|
|
|
|
Assert $Tests
|
|
|
|
$Tests.Sort()
|
|
|
|
Test.Plan($Tests.Count)
|
|
For Each oTest In $Tests
|
|
CurrentTestModule = oTest.TestModule
|
|
If LastTestModule Then
|
|
If LastTestModule <> CurrentTestModule Then
|
|
CurrentAction = LastTestModule.Name & ":TeardownTestModule"
|
|
StopTestModule(LastTestModule)
|
|
CurrentAction = CurrentTestModule.Name & ":SetupTestModule"
|
|
StartTestModule(CurrentTestModule)
|
|
Endif
|
|
Else
|
|
'Test.Note("-------------------- " & CurrentTestModule.Name)
|
|
CurrentAction = CurrentTestModule.Name & ":SetupTestModule"
|
|
StartTestModule(CurrentTestModule)
|
|
Endif
|
|
|
|
Test._Print(gb.Lf) 'better readability for humans
|
|
Test._Print(Subst$(("Test &1:&2"), oTest.TestModule.Name, oTest.Name))
|
|
Test._Subtest(Subst$("&1:&2", oTest.TestModule.Name, oTest.Name))
|
|
'Debug oTest.Name
|
|
oTest.Run()
|
|
If Not Test._Finished Then Test._Finish()
|
|
LastTestModule = CurrentTestModule
|
|
Next
|
|
|
|
If LastTestModule Then
|
|
CurrentAction = LastTestModule.Name & ":TeardownTestModule"
|
|
StopTestModule(LastTestModule)
|
|
Endif
|
|
|
|
Catch
|
|
Test.BailOut("Test Stopped with error \"" & Error.Text & "\" caused by " & CurrentAction & " in " & Error.Where & ".")
|
|
|
|
End Sub
|
|
|
|
Private Sub StartTestModule(TestModule As Class)
|
|
|
|
If TestModule.Symbols.Exist("_Setup") Then
|
|
Object.Call(TestModule, "_Setup")
|
|
Endif
|
|
|
|
End
|
|
|
|
Private Sub StopTestModule(TestModule As Class)
|
|
|
|
If TestModule.Symbols.Exist("_Teardown") Then
|
|
Object.Call(TestModule, "_Teardown")
|
|
Endif
|
|
|
|
End
|
|
|
|
'' Create a new test case and add it to the suite.
|
|
Public Function AddTestCase(sName As String, TestModule As Class)
|
|
|
|
Dim test As TestCase
|
|
|
|
Assert sName <> Null
|
|
Assert TestModule
|
|
|
|
test = New TestCase(sName, TestModule)
|
|
Assert $Tests
|
|
|
|
If Not $Tests.Exist(test) Then
|
|
$Tests.Add(test)
|
|
Endif
|
|
|
|
End
|
|
|
|
Static Public Function GetTestsFromTestModule(TestModule As Class) As String[]
|
|
|
|
Dim sSymbol As String
|
|
Dim NoTestSymbols As String[] = ["_Setup", "_SetupEach", "_Teardown", "_TeardownEach", "Plan"]
|
|
Dim asTests As New String[]
|
|
|
|
Assert TestModule
|
|
|
|
For Each sSymbol In TestModule.Symbols
|
|
If Not NoTestSymbols.Exist(sSymbol, gb.IgnoreCase) Then
|
|
asTests.Add(sSymbol)
|
|
Endif
|
|
Next
|
|
|
|
Return asTests
|
|
|
|
End
|
|
|
|
'' Create all test cases that are contained in the specified TestModule and add them to the suite.
|
|
Public Function AddAllTestCases(TestModule As Class)
|
|
|
|
Dim asTests As String[]
|
|
Dim i As Integer
|
|
Dim NameProcedure As String
|
|
|
|
Assert TestModule
|
|
|
|
asTests = GetTestsFromTestModule(TestModule)
|
|
asTests.Sort
|
|
|
|
'Assert asTests.Count > 0 Error "Failed: No tests in " & TestModule.Name
|
|
If asTests.Count = 0 Then
|
|
Assert.Todo("No tests in " & TestModule.Name)
|
|
Endif
|
|
|
|
For i = 0 To asTests.Count - 1
|
|
NameProcedure = CStr(asTests[i])
|
|
AddTestCase(NameProcedure, TestModule)
|
|
Next
|
|
|
|
End
|
|
|
|
Private Function Tests_Read() As TestCase[]
|
|
|
|
Return $Tests
|
|
|
|
End
|