/
mdlUnitTestLib.bas
152 lines (136 loc) · 4.73 KB
/
mdlUnitTestLib.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
' File: mdlTestLib.bas
' Includes the functions and subroutines about unit tests.
' This is a simple module that displays the test results in the
' "Intermediate Window" of Microsoft Access.
Option Compare Database
Option Explicit
' Function: GetPassMessage
' Returns the pass message to be the same
' for all test subroutines.
'
' Returns:
' the pass message
Public Function GetPassMessage() As String
GetPassMessage = "pass ... "
End Function
' Function: GetFailMessage
' Returns the fail message to be the same
' for all test subroutines.
'
' Returns:
' the fail message
Public Function GetFailMessage() As String
GetFailMessage = "FAIL ... "
End Function
' Sub: AssertAreEqual
' Tests if two values are equal.
'
' Parameters:
' testSubName - name of the test subroutine
' expected - expected value of any type
' actual - actual value of any type
Public Sub AssertAreEqual(ByVal testSubName As String, ByVal expected As Variant, ByVal actual As Variant)
Dim msg As String
If expected = actual Then
msg = GetPassMessage() & testSubName
Else
msg = GetFailMessage() & testSubName & " - "
msg = msg & " expected : [" & expected & "]" & vbCrlf
msg = msg & " actual : [" & actual & "]" & vbCrlf
End If
Debug.Print (msg)
End Sub
' Sub: AssertAreNotEqual
' Tests if two values are not equal.
'
' Parameters:
' testSubName - name of the test subroutine
' expected - expected value of any type
' actual - actual value of any type
Public Sub AssertAreNotEqual(ByVal testSubName As String, ByVal expected As Variant, ByVal actual As Variant)
Dim msg As String
If expected = actual Then
msg = GetFailMessage() & testSubName & " - "
msg = msg & " expected : [" & expected & "]" & vbCrlf
msg = msg & " actual : [" & actual & "]" & vbCrlf
Else
msg = GetPassMessage() & testSubName
End If
Debug.Print (msg)
End Sub
' Sub: AssertTrue
' Tests if the specified value is True.
'
' Parameters:
' testSubName - name of the test subroutine
' actual - actual value
Public Sub AssertTrue(ByVal testSubName As String, ByVal actual As Boolean)
Dim msg As String
If actual = True Then
msg = GetPassMessage() & testSubName
Else
msg = GetFailMessage() & testSubName & " - "
msg = msg & " expected : True" & vbCrlf
msg = msg & " actual : " & actual & vbCrlf
End If
Debug.Print (msg)
End Sub
' Sub: AssertFalse
' Tests if the specified value is False.
'
' Parameters:
' testSubName - name of the test subroutine
' actual - actual value
Public Sub AssertFalse(ByVal testSubName As String, ByVal actual As Boolean)
Dim msg As String
If actual = False Then
msg = GetPassMessage() & testSubName
Else
msg = GetFailMessage() & testSubName & " - "
msg = msg & " expected : False" & vbCrlf
msg = msg & " actual : " & actual & vbCrlf
End If
Debug.Print (msg)
End Sub
' Sub: AssertAreArraysEqual
' Tests if the contents of two arrays are equal.
'
' Parameters:
' testSubName - name of the test subroutine
' arrExpected - expected array of any type
' arrActual - actual array of any type
Public Sub AssertAreArraysEqual(ByVal testSubName As String, ByRef arrExpected() As Variant, ByRef arrActual() As Variant)
Dim msg As String
If LBound(arrExpected) = LBound(arrActual) Then
If UBound(arrExpected) = UBound(arrActual) Then
Dim bolAllEqual As Boolean
bolAllEqual = True
Dim i As Long
For i = LBound(arrExpected) To UBound(arrExpected)
If arrExpected(i) <> arrActual(i) Then
' same number of elements but at least one of them does not match
msg = GetFailMessage() & testSubName & " - "
msg = msg & " expected(" & i & ") : " & arrExpected(i) & vbCrlf
msg = msg & " actual(" & i & ") : " & arrActual(i) & vbCrlf
bolAllEqual = False
Exit For
End If
Next
If bolAllEqual = True Then
' same number of elements and all elements are equal
msg = GetPassMessage() & testSubName
End If
Else
' UBound of array do not match
msg = GetFailMessage() & testSubName & " - "
msg = msg & " expected UBound : " & UBound(arrExpected) & vbCrlf
msg = msg & " actual UBound : " & UBound(arrActual) & vbCrlf
End If
Else
' LBound of array do not match
msg = GetFailMessage() & testSubName & " - "
msg = msg & " expected LBound : " & LBound(arrExpected) & vbCrlf
msg = msg & " actual LBound : " & LBound(arrActual) & vbCrlf
End If
Debug.Print (msg)
End Sub