gambas-source-code/comp/src/gb.test/.src/TestSuite/Assert.module

254 lines
5.8 KiB
Text
Raw Normal View History

' Gambas module file
''' Assertions which print TAP.
Export
Private $bIntendedFailure As Boolean
Public Sub IntendedFailure()
$bIntendedFailure = True
End
Public Sub Ok(Result As Boolean, Optional Description As String) As Boolean
Dim bRes As Boolean
If $bIntendedFailure Then Result = Not Result
$bIntendedFailure = False
' Dim TheNext As TestAssertion
' just for debug
'TheNext = Test._Next
Test._Next.Ok = Result
Test._Next.Description = Description
bRes = Test.Printer.Assert(Test._Next).Ok
Test._Next = New TestAssertion
Return bRes
End
Public Sub Todo(Optional Comment As String)
Test._Next.Directive = Tap.TODO
Test._Next.Comment = Comment
End
Public Sub Skip(Optional Comment As String)
Test._Next.Directive = Tap.SKIP
Test._Next.Comment = Comment
Pass()
End
' -------------------- High-level test functions --------------------
Public Sub Pass(Optional Description As String) As Boolean
Return Ok(True, Description)
End
Public Sub Fail(Optional Description As String) As Boolean
Return Ok(False, Description)
End
Public Sub NotOk(Result As Boolean, Optional Description As String) As Boolean
Return Ok(Not Result, Description)
End
Public Sub Equals(Got As Variant, Expected As Variant, Optional Description As String) As Boolean
Dim bRes As Boolean
bRes = Ok(Got = Expected, Description)
If Not bRes Then
Test.Note(Subst$((" Got: &1"), Got))
Test.Note(Subst$((" Expected: &1"), Expected))
Endif
Return bRes
End
Public Sub Notequals(Got As Variant, UnExpected As Variant, Optional Description As String) As Boolean
Return Ok(Got <> UnExpected, Description)
End
Public Sub LessEqual(Got As Variant, Bound As Variant, Optional Description As String) As Boolean
Dim bRes As Boolean
bRes = Ok(Got <= Bound, Description)
If Not bRes Then
Test.Note(Subst$((" Got: &1"), Got))
Test.Note(Subst$((" Expected: <= &1"), Bound))
Endif
Return bRes
End
Public Sub Less(Got As Variant, Bound As Variant, Optional Description As String) As Boolean
Dim bRes As Boolean
bRes = Ok(Got < Bound, Description)
If Not bRes Then
Test.Note(Subst$((" Got: &1"), Got))
Test.Note(Subst$((" Expected: < &1"), Bound))
Endif
Return bRes
End
Public Sub GreaterEqual(Got As Variant, Bound As Variant, Optional Description As String) As Boolean
Dim bRes As Boolean
bRes = Ok(Got >= Bound, Description)
If Not bRes Then
Test.Note(Subst$((" Got: &1"), Got))
Test.Note(Subst$((" Expected: >= &1"), Bound))
Endif
Return bRes
End
Public Sub Greater(Got As Variant, Bound As Variant, Optional Description As String) As Boolean
Dim bRes As Boolean
bRes = Ok(Got > Bound, Description)
If Not bRes Then
Test.Note(Subst$((" Got: &1"), Got))
Test.Note(Subst$((" Expected: > &1"), Bound))
Endif
Return bRes
End
Public Sub Approximate(Got As Float, Expected As Float, Precision As Float, Optional Description As String) As Boolean
Return LessEqual(Abs(Got - Expected), Precision, Description)
End
Public Sub RelativeApproximate(Got As Float, Expected As Float, RelPrecision As Float, Optional Description As String) As Boolean
Return LessEqual(Abs((Got - Expected) / Expected), RelPrecision, Description)
End
Public Sub IsType(Got As Variant, Type As Integer, Optional Description As String) As Boolean
Return Equals(TypeOf(Got), Type, Description)
End
Public Sub Null(Got As Variant, Optional Description As String) As Boolean
Return Equals(Got, Null, Description)
End
Public Sub NotNull(Got As Variant, Optional Description As String) As Boolean
Return Notequals(Got, Null, Description)
End
Public Sub Like(Got As String, Pattern As String, Optional Description As String) As Boolean
Dim bRes As Boolean
bRes = Ok(Got Like Pattern, Description)
If Not bRes Then
Test.Note(Subst$((" Got: &1"), Got))
Test.Note(Subst$((" Expected: &1"), Pattern))
Endif
Return bRes
End
Public Sub Match(Got As String, Pattern As String, Optional Description As String) As Boolean
Dim bRes As Boolean
bRes = Ok(Got Match Pattern, Description)
If Not bRes Then
Test.Note(Subst$((" Got: &1"), Got))
Test.Note(Subst$((" Expected: &1"), Pattern))
Endif
Return bRes
End
Public Sub StringEquals(Got As String, Expected As String, Optional Description As String) As Boolean
Dim bRes As Boolean
Dim iPos As Integer
bRes = Equals(Got, Expected, Description)
If Not bRes Then
If Len(Got) <> Len(Expected) Then
Test.Note(Subst$(("Strings are of different lengths &1 and &2, respectively."), Len(Got), Len(Expected)))
Endif
For iPos = 1 To Min(Len(Got), Len(Expected))
If Mid$(Got, iPos, 1) <> Mid$(Expected, iPos, 1) Then Break
Next
Test.Note(Subst$(("Strings differ at position &1."), iPos))
Endif
Return bRes
End
Public Sub Error(Optional Description As String) As Boolean
Return Ok( Error , Description)
End
Public Sub ErrorCode(Code As Integer, Optional Description As String) As Boolean
Dim bRes As Boolean
If Not Error Then
bRes = Fail(Description)
Test.Note(("No error happened"))
Else
bRes = Equals(Error.Code, Code, Description)
If Not bRes Then
Test.Note(Subst$(("Error was: &1 (code: &2) at &3"), Error.Text, Error.Code, Error.Where))
Endif
Endif
Error.Clear()
Return bRes
End
Public Sub Noterror(Optional Description As String) As Boolean
Dim bRes As Boolean
bRes = Ok(Not Error , Description)
If Not bRes Then
Test.Note(Subst$(("Error was: &1 (code: &2) at &3"), Error.Text, Error.Code, Error.Where))
Endif
Error.Clear()
Return bRes
End