gambas-source-code/.src/TestSuite/TestResult.class

280 lines
7.9 KiB
Text
Raw Normal View History

2015-01-29 15:02:26 +01:00
' Gambas class file
2016-09-19 23:42:46 +02:00
Export
2015-10-31 09:22:52 +01:00
''' The TestResult object collects the results from executing test cases. It is an
''' instance of the Collecting Parameter pattern. When new failures or errors
''' are added to the TestResult or if a test case is started or finished, the
''' TestResult generates events to notify its event handlers about what has happened.
'''
''' The test framework distinguishes between failures and errors.
''' A failure is anticipated and checked for with assertions. Errors are
''' unanticipated problems signified by exceptions that are not generated
''' by the framework. TestResult includes a set of Assert methods that simplify
''' the checking of test assertions.
2016-09-19 23:42:46 +02:00
'' Gets the number of run tests.
Property Read RunTests As Integer
'' Returns a collection of failures
Property Read Failures As TestErrors
'' Returns a collection of errors
Property Read Errors As TestErrors
'' Set and Returns parameter collection
Property Parameters As TestParameters
2015-10-31 09:22:52 +01:00
'' Member Variables
2015-01-30 07:46:43 +01:00
Private $colErrors As TestErrors
Private $colFailures As TestErrors
Private $colParameters As TestParameters
Private $iRunTests As Integer
Private $oCurrentTestCase As ITestCase
2015-01-29 15:02:26 +01:00
2015-10-31 09:22:52 +01:00
'' Events
2015-01-29 15:02:26 +01:00
Event AfterStartTest(oTestCase As ITestCase)
Event AfterEndTest()
Event AfterAddError(oError As TestError)
Event AfterAddFailure(oError As TestError)
Event AfterAddTrace(sMessage As String)
2015-10-31 09:22:52 +01:00
'' Initialize Variables
2015-01-30 07:46:43 +01:00
Public Sub _new()
2015-01-29 15:02:26 +01:00
2015-01-30 07:46:43 +01:00
$colErrors = New TestErrors
$colFailures = New TestErrors
$iRunTests = 0
2015-01-29 15:02:26 +01:00
End Sub
2015-10-31 09:22:52 +01:00
'' Returns whether the entire test was successful or not.
2015-01-29 15:02:26 +01:00
Property Read WasSuccessful As Boolean
2015-10-31 09:22:52 +01:00
'' Informs the result that a test will be started.
2015-01-29 15:02:26 +01:00
Public Sub StartTest(oTestCase As ITestCase)
2015-01-30 07:46:43 +01:00
$oCurrentTestCase = oTestCase
2015-01-29 15:02:26 +01:00
Raise AfterStartTest(oTestCase)
End Sub
2015-10-31 09:22:52 +01:00
'' Informs the result that a test is completed.
2015-01-29 15:02:26 +01:00
Public Sub EndTest()
2015-01-30 07:46:43 +01:00
$oCurrentTestCase = Null
$iRunTests = $iRunTests + 1
2015-01-29 15:02:26 +01:00
Raise AfterEndTest
End Sub
2015-10-31 09:22:52 +01:00
'' Adds a failure to the collection of failures.
2015-01-29 15:02:26 +01:00
Public Sub AddFailure(sDescription As String)
Dim oNewError As TestError
Dim source As String
source = Object.Class($oCurrentTestCase.TestContainer).Name &
"." & $oCurrentTestCase.Name
2015-01-29 15:02:26 +01:00
'Failures use Error number 0
oNewError = $colFailures.Add($oCurrentTestCase, 0, source, sDescription)
2015-01-29 15:02:26 +01:00
Raise AfterAddFailure(oNewError)
oNewError = Null
End Sub
2015-10-31 09:22:52 +01:00
'' Adds a error to the collection of errors.
2015-01-29 15:02:26 +01:00
Public Sub AddError(lNumber As Long, sSource As String, sDescription As String)
Dim oNewError As TestError
2015-01-30 07:46:43 +01:00
oNewError = $colErrors.Add($oCurrentTestCase, lNumber, sSource, sDescription)
2015-01-29 15:02:26 +01:00
Raise AfterAddError(oNewError)
oNewError = Null
End Sub
2015-10-31 09:22:52 +01:00
'' Add trace message
2015-01-29 15:02:26 +01:00
Public Sub AddTrace(sMessage As String)
Raise AfterAddTrace(sMessage)
End Sub
2015-10-31 09:22:52 +01:00
'' Asserts that a condition is true. If it isn't it raises a failure with the given message.
'' bCondition: condition to be asserted
'' sMessage: optional message describing the asserted condition
2015-01-29 15:02:26 +01:00
Public Sub Assert(bCondition As Boolean, Optional sMessage As String)
If Not bCondition Then
AddFailure(sMessage)
End If
End Sub
2015-10-31 09:22:52 +01:00
'' Asserts that the expected string equals the actual string.
'' sExpected: the expected value
'' sActual: the actual value
'' sMessage: optional message describing the asserted condition
2015-01-29 15:02:26 +01:00
Public Sub AssertEqualsString(sExpected As String, sActual As String, Optional sMessage As String)
If sExpected <> sActual Then
AddFailure(NotEqualsMessage(sMessage, sExpected, sActual))
End If
End Sub
2015-10-31 09:22:52 +01:00
'' Asserts that the expected long value equals the actual long value.
'' lExpected: the expected value
'' lActual: the actual value
'' sMessage: optional message describing the asserted condition
2015-01-29 15:02:26 +01:00
Public Sub AssertEqualsLong(lExpected As Long, lActual As Long, Optional sMessage As String)
If lExpected <> lActual Then
AddFailure(NotEqualsMessage(sMessage, lExpected, lActual))
End If
End Sub
2015-10-31 09:22:52 +01:00
'' Asserts that the expected Float value equals the actual Float value with delta precision.
'' dExpected: the expected value
'' dActual: the actual value
'' dDelta: tolerated precision
'' sMessage: optional message describing the asserted condition
2015-01-29 15:02:26 +01:00
Public Sub AssertEqualsFloat(dExpected As Float, dActual As Float, dDelta As Float, Optional sMessage As String)
If (Abs(dExpected - dActual) > dDelta) Then
AddFailure(NotEqualsMessage(sMessage, dExpected, dActual))
End If
End Sub
2015-10-31 09:22:52 +01:00
'' Asserts that the expected variant equals the actual variant.
'' vExpected: the expected value
'' vActual: the actual value
'' sMessage: optional message describing the asserted condition
2015-01-29 15:02:26 +01:00
Public Sub AssertEqualsVariant(vExpected As Variant, vActual As Variant, Optional sMessage As String)
If vExpected <> vActual Then
AddFailure(NotEqualsMessage(sMessage, vExpected, vActual))
End If
End Sub
2015-10-31 09:22:52 +01:00
'' Asserts that an object is not nothing
'' oObject: object reference
'' sMessage: the detail message to record if this assertion fails
2015-01-29 15:02:26 +01:00
Public Sub AssertExists(oObject As Object, Optional sMessage As String)
If Not oObject Then
AddFailure(sMessage & " - Object of type " & oObject.TypeOf & " is Nothing.")
End If
End Sub
2015-10-31 09:22:52 +01:00
'' Asserts that the expected object equals the actual object.
'' oExpected: expected object reference
'' oActual: actual object reference
'' sMessage: the detail message to record if this assertion fails
2015-01-29 15:02:26 +01:00
Public Sub AssertEqualsObject(oExpected As Object, oActual As Object, Optional sMessage As String)
If Not (oExpected = OActual) Then
AddFailure(sMessage & " - Object [" & oExpected.name & "] does not equal Object [" & oActual.name & "].")
End If
End Sub
2015-10-31 09:22:52 +01:00
'' Asserts that a variant is not empty
'' vVariant: variant to evaluate
'' sMessage: the detail message to record if this assertion fails
2015-01-29 15:02:26 +01:00
Public Sub AssertNotEmpty(vVariant As Variant, Optional sMessage As String)
If (vVariant = Null) Then
AddFailure(sMessage & " - Variant is empty.")
End If
End Sub
'' Asserts that a variant is empty
'' vVariant: variant to evaluate
'' sMessage: the detail message to record if this assertion fails
Public Sub AssertEmpty(vVariant As Variant, Optional sMessage As String)
If (vVariant <> Null) Then
AddFailure(sMessage & " - Variant is not empty.")
2015-01-29 15:02:26 +01:00
End If
End Sub
2015-10-31 09:22:52 +01:00
'' Asserts that a variant is not null
'' vVariant: variant to evaluate
'' sMessage: the detail message to record if this assertion fails
2015-01-29 15:02:26 +01:00
Public Sub AssertNotNull(vVariant As Variant, Optional sMessage As String)
If (IsNull(vVariant)) Then
AddFailure(sMessage & " - Variant is Null.")
End If
End Sub
2015-10-31 09:22:52 +01:00
'' Asserts that an error was thrown
'' lError: error number to compare
2015-01-29 15:02:26 +01:00
Public Sub AssertEqualsError(oError As Error, Optional lError As Long, Optional sMessage As String)
If (lError = 0 And oError.Number = 0) Then
AddFailure(sMessage & " - Expected Error did not occur.")
Else If (lError <> 0 And oError.Number <> lError) Then
AddFailure(sMessage &
" - Expected Error (" & lError & ") but Error (" &
oError.Number & ") was thrown instead. Description: " &
oError.Description)
End If
Error.Clear
2015-01-29 15:02:26 +01:00
End Sub
2015-10-31 09:22:52 +01:00
'' Build a message about a failed equality check
2015-01-29 15:02:26 +01:00
Private Function NotEqualsMessage(sMessage As String, sExpected As Variant, sActual As Variant) As String
Return sMessage & " expected: " & CString(sExpected) & " but was: " & CStr(sActual)
End Function
Private Function WasSuccessful_Read() As Boolean
2015-01-30 07:46:43 +01:00
Return (($colErrors.Count = 0) And ($colFailures.Count = 0))
2015-01-29 15:02:26 +01:00
End
Private Function RunTests_Read() As Integer
2015-01-30 07:46:43 +01:00
Return $iRunTests
2015-01-29 15:02:26 +01:00
End
Private Function Failures_Read() As TestErrors
2015-01-30 07:46:43 +01:00
Return $colFailures
2015-01-29 15:02:26 +01:00
End
Private Function Errors_Read() As TestErrors
2015-01-30 07:46:43 +01:00
'Errors = $colErrors
Return $colErrors
2015-01-29 15:02:26 +01:00
End
Private Function Parameters_Read() As TestParameters
2015-01-30 07:46:43 +01:00
'Parameters = $colParameters
Return $colParameters
2015-01-29 15:02:26 +01:00
End
Private Sub Parameters_Write(Value As TestParameters)
2015-01-30 07:46:43 +01:00
$colParameters = Value
2015-01-29 15:02:26 +01:00
End