2020-04-27 12:01:47 +02:00
|
|
|
' Gambas module file
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
''' Assertions which print TAP.
|
|
|
|
|
|
|
|
Export
|
|
|
|
|
2020-04-25 05:48:28 +02:00
|
|
|
Private $bIntendedFailure As Boolean
|
2020-04-09 08:09:53 +02:00
|
|
|
|
2020-02-24 14:55:00 +01:00
|
|
|
|
2020-04-25 05:48:28 +02:00
|
|
|
Public Sub IntendedFailure()
|
2020-02-23 15:54:16 +01:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
$bIntendedFailure = True
|
2020-02-23 15:54:16 +01:00
|
|
|
|
2020-02-24 17:52:35 +01:00
|
|
|
End
|
|
|
|
|
2020-02-23 15:54:16 +01:00
|
|
|
Public Sub Ok(Result As Boolean, Optional Description As String) As Boolean
|
|
|
|
|
2020-04-25 05:22:10 +02:00
|
|
|
Dim bRes As Boolean
|
2020-04-09 08:09:53 +02:00
|
|
|
|
2020-04-25 05:48:28 +02:00
|
|
|
If $bIntendedFailure Then Result = Not Result
|
|
|
|
$bIntendedFailure = False
|
2020-04-25 05:22:10 +02:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
' 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
|
2020-04-25 05:22:10 +02:00
|
|
|
Return bRes
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Public Sub Todo(Optional Comment As String)
|
2020-02-27 20:28:49 +01:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
Test._Next.Directive = Tap.TODO
|
|
|
|
Test._Next.Comment = Comment
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
Public Sub Skip(Optional Comment As String)
|
2020-02-24 14:11:10 +01:00
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
Test._Next.Directive = Tap.SKIP
|
|
|
|
Test._Next.Comment = Comment
|
|
|
|
Pass()
|
2020-02-27 20:28:49 +01:00
|
|
|
|
2020-02-24 17:52:35 +01:00
|
|
|
End
|
|
|
|
|
2020-02-23 15:54:16 +01:00
|
|
|
' -------------------- High-level test functions --------------------
|
|
|
|
|
2020-02-23 20:06:19 +01:00
|
|
|
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
|
|
|
|
|
2020-02-23 15:54:16 +01:00
|
|
|
Public Sub NotOk(Result As Boolean, Optional Description As String) As Boolean
|
|
|
|
|
|
|
|
Return Ok(Not Result, Description)
|
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-23 20:06:19 +01:00
|
|
|
Public Sub Equals(Got As Variant, Expected As Variant, Optional Description As String) As Boolean
|
2020-02-23 15:54:16 +01:00
|
|
|
|
2020-02-24 18:18:04 +01:00
|
|
|
Dim bRes As Boolean
|
|
|
|
|
|
|
|
bRes = Ok(Got = Expected, Description)
|
|
|
|
If Not bRes Then
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.Note(Subst$((" Got: &1"), Got))
|
|
|
|
Test.Note(Subst$((" Expected: &1"), Expected))
|
2020-02-24 18:18:04 +01:00
|
|
|
Endif
|
|
|
|
Return bRes
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
Public Sub Notequals(Got As Variant, UnExpected As Variant, Optional Description As String) As Boolean
|
2020-02-23 20:06:19 +01:00
|
|
|
|
|
|
|
Return Ok(Got <> UnExpected, Description)
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Public Sub LessEqual(Got As Variant, Bound As Variant, Optional Description As String) As Boolean
|
2020-02-23 15:54:16 +01:00
|
|
|
|
2020-02-24 18:18:04 +01:00
|
|
|
Dim bRes As Boolean
|
|
|
|
|
|
|
|
bRes = Ok(Got <= Bound, Description)
|
|
|
|
If Not bRes Then
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.Note(Subst$((" Got: &1"), Got))
|
|
|
|
Test.Note(Subst$((" Expected: <= &1"), Bound))
|
2020-02-24 18:18:04 +01:00
|
|
|
Endif
|
|
|
|
Return bRes
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-23 20:06:19 +01:00
|
|
|
Public Sub Less(Got As Variant, Bound As Variant, Optional Description As String) As Boolean
|
2020-02-23 15:54:16 +01:00
|
|
|
|
2020-02-24 18:18:04 +01:00
|
|
|
Dim bRes As Boolean
|
|
|
|
|
|
|
|
bRes = Ok(Got < Bound, Description)
|
|
|
|
If Not bRes Then
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.Note(Subst$((" Got: &1"), Got))
|
|
|
|
Test.Note(Subst$((" Expected: < &1"), Bound))
|
2020-02-24 18:18:04 +01:00
|
|
|
Endif
|
|
|
|
Return bRes
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-23 20:06:19 +01:00
|
|
|
Public Sub GreaterEqual(Got As Variant, Bound As Variant, Optional Description As String) As Boolean
|
2020-02-23 15:54:16 +01:00
|
|
|
|
2020-02-24 18:18:04 +01:00
|
|
|
Dim bRes As Boolean
|
|
|
|
|
|
|
|
bRes = Ok(Got >= Bound, Description)
|
|
|
|
If Not bRes Then
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.Note(Subst$((" Got: &1"), Got))
|
|
|
|
Test.Note(Subst$((" Expected: >= &1"), Bound))
|
2020-02-24 18:18:04 +01:00
|
|
|
Endif
|
|
|
|
Return bRes
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-23 20:06:19 +01:00
|
|
|
Public Sub Greater(Got As Variant, Bound As Variant, Optional Description As String) As Boolean
|
2020-02-23 15:54:16 +01:00
|
|
|
|
2020-02-24 18:18:04 +01:00
|
|
|
Dim bRes As Boolean
|
|
|
|
|
|
|
|
bRes = Ok(Got > Bound, Description)
|
|
|
|
If Not bRes Then
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.Note(Subst$((" Got: &1"), Got))
|
|
|
|
Test.Note(Subst$((" Expected: > &1"), Bound))
|
2020-02-24 18:18:04 +01:00
|
|
|
Endif
|
|
|
|
Return bRes
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-23 20:06:19 +01:00
|
|
|
Public Sub Approximate(Got As Float, Expected As Float, Precision As Float, Optional Description As String) As Boolean
|
2020-02-23 15:54:16 +01:00
|
|
|
|
2020-02-24 18:18:04 +01:00
|
|
|
Return LessEqual(Abs(Got - Expected), Precision, Description)
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-23 20:06:19 +01:00
|
|
|
Public Sub RelativeApproximate(Got As Float, Expected As Float, RelPrecision As Float, Optional Description As String) As Boolean
|
2020-02-23 15:54:16 +01:00
|
|
|
|
2020-02-24 18:18:04 +01:00
|
|
|
Return LessEqual(Abs((Got - Expected) / Expected), RelPrecision, Description)
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-23 20:06:19 +01:00
|
|
|
Public Sub IsType(Got As Variant, Type As Integer, Optional Description As String) As Boolean
|
2020-02-23 15:54:16 +01:00
|
|
|
|
2020-02-24 18:18:04 +01:00
|
|
|
Return Equals(TypeOf(Got), Type, Description)
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-23 20:06:19 +01:00
|
|
|
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
|
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
Return Notequals(Got, Null, Description)
|
2020-02-23 20:06:19 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Public Sub Like(Got As String, Pattern As String, Optional Description As String) As Boolean
|
2020-02-23 15:54:16 +01:00
|
|
|
|
2020-02-24 18:18:04 +01:00
|
|
|
Dim bRes As Boolean
|
|
|
|
|
|
|
|
bRes = Ok(Got Like Pattern, Description)
|
|
|
|
If Not bRes Then
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.Note(Subst$((" Got: &1"), Got))
|
|
|
|
Test.Note(Subst$((" Expected: &1"), Pattern))
|
2020-02-24 18:18:04 +01:00
|
|
|
Endif
|
|
|
|
Return bRes
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-23 20:06:19 +01:00
|
|
|
Public Sub Match(Got As String, Pattern As String, Optional Description As String) As Boolean
|
2020-02-23 15:54:16 +01:00
|
|
|
|
2020-02-24 18:18:04 +01:00
|
|
|
Dim bRes As Boolean
|
|
|
|
|
|
|
|
bRes = Ok(Got Match Pattern, Description)
|
|
|
|
If Not bRes Then
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.Note(Subst$((" Got: &1"), Got))
|
|
|
|
Test.Note(Subst$((" Expected: &1"), Pattern))
|
2020-02-24 18:18:04 +01:00
|
|
|
Endif
|
|
|
|
Return bRes
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-23 20:06:19 +01:00
|
|
|
Public Sub StringEquals(Got As String, Expected As String, Optional Description As String) As Boolean
|
2020-02-23 15:54:16 +01:00
|
|
|
|
|
|
|
Dim bRes As Boolean
|
|
|
|
Dim iPos As Integer
|
|
|
|
|
|
|
|
bRes = Equals(Got, Expected, Description)
|
|
|
|
If Not bRes Then
|
|
|
|
If Len(Got) <> Len(Expected) Then
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.Note(Subst$(("Strings are of different lengths &1 and &2, respectively."), Len(Got), Len(Expected)))
|
2020-02-23 15:54:16 +01:00
|
|
|
Endif
|
|
|
|
For iPos = 1 To Min(Len(Got), Len(Expected))
|
|
|
|
If Mid$(Got, iPos, 1) <> Mid$(Expected, iPos, 1) Then Break
|
|
|
|
Next
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.Note(Subst$(("Strings differ at position &1."), iPos))
|
2020-02-23 15:54:16 +01:00
|
|
|
Endif
|
|
|
|
Return bRes
|
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-02-23 20:06:19 +01:00
|
|
|
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)
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.Note(("No error happened"))
|
2020-02-23 20:06:19 +01:00
|
|
|
Else
|
2020-04-07 14:18:59 +02:00
|
|
|
bRes = Equals(Error.Code, Code, Description)
|
2020-02-23 20:06:19 +01:00
|
|
|
If Not bRes Then
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.Note(Subst$(("Error was: &1 (code: &2) at &3"), Error.Text, Error.Code, Error.Where))
|
2020-02-23 20:06:19 +01:00
|
|
|
Endif
|
|
|
|
Endif
|
|
|
|
|
|
|
|
Error.Clear()
|
|
|
|
Return bRes
|
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-04-27 12:01:47 +02:00
|
|
|
Public Sub Noterror(Optional Description As String) As Boolean
|
2020-02-23 20:06:19 +01:00
|
|
|
|
|
|
|
Dim bRes As Boolean
|
|
|
|
|
|
|
|
bRes = Ok(Not Error , Description)
|
|
|
|
If Not bRes Then
|
2020-04-27 12:01:47 +02:00
|
|
|
Test.Note(Subst$(("Error was: &1 (code: &2) at &3"), Error.Text, Error.Code, Error.Where))
|
2020-02-23 20:06:19 +01:00
|
|
|
Endif
|
|
|
|
|
|
|
|
Error.Clear()
|
|
|
|
Return bRes
|
|
|
|
|
|
|
|
End
|