-
Notifications
You must be signed in to change notification settings - Fork 54
/
TestSuite.cls
151 lines (128 loc) · 3.33 KB
/
TestSuite.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "TestSuite"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
''
' TestSuite v2.0.0-beta.3
' (c) Tim Hall - https://github.com/vba-tools/vba-test
'
' A collection of tests, with events and results
'
' @class TestSuite
' @author [email protected]
' @license MIT (https://opensource.org/licenses/MIT)
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '
Option Explicit
' --------------------------------------------- '
' Types, Events, and Properties
' --------------------------------------------- '
Public Enum TestResultType
Pass
Fail
Pending
Skipped
End Enum
Public Event BeforeEach(Test As TestCase)
Public Event Result(Test As TestCase)
Public Event AfterEach(Test As TestCase)
''
' (Optional) description of suite for display in runners
'
' @property Description
' @type String
''
Public Description As String
''
' @property Tests
' @type Collection<TestCase>
''
Public Tests As VBA.Collection
''
' Compute suite result from tests
'
' @property Result
' @type SpecResultType
''
Public Property Get Result() As TestResultType
Result = TestResultType.Pending
Dim Test As TestCase
For Each Test In Me.Tests
If Test.Result = TestResultType.Pass Then
Result = TestResultType.Pass
ElseIf Test.Result = TestResultType.Fail Then
Result = TestResultType.Fail
Exit For
End If
Next Test
End Property
''
' @property PassedTests
' @type Collection<TestCase>
''
Public Property Get PassedTests() As VBA.Collection
Set PassedTests = GetTestsByType(TestResultType.Pass)
End Property
''
' @property FailedTests
' @type Collection<TestCase>
''
Public Property Get FailedTests() As VBA.Collection
Set FailedTests = GetTestsByType(TestResultType.Fail)
End Property
''
' @property PendingTests
' @type Collection<TestCase>
''
Public Property Get PendingTests() As VBA.Collection
Set PendingTests = GetTestsByType(TestResultType.Pending)
End Property
''
' @property SkippedTests
' @type Collection<TestCase>
''
Public Property Get SkippedTests() As VBA.Collection
Set SkippedTests = GetTestsByType(TestResultType.Skipped)
End Property
' ============================================= '
' Public Methods
' ============================================= '
''
' Create a new test case with name
'
' @method Test
' @param {String} Name
' @returns {TestCase}
''
Public Function Test(Name As String) As TestCase
Dim Instance As New TestCase
Instance.Name = Name
Set Instance.Suite = Me
RaiseEvent BeforeEach(Instance)
Set Test = Instance
End Function
Public Sub TestComplete(Test As TestCase)
Tests.Add Test
RaiseEvent Result(Test)
RaiseEvent AfterEach(Test)
End Sub
' ============================================= '
' Private Functions
' ============================================= '
Private Function GetTestsByType(ResultType As TestResultType) As Collection
Dim Test As TestCase
Dim Filtered As New VBA.Collection
For Each Test In Me.Tests
If Test.Result = ResultType Then
Filtered.Add Test
End If
Next Test
Set GetTestsByType = Filtered
End Function
Private Sub Class_Initialize()
Set Tests = New VBA.Collection
End Sub