File: Tests.vb

package info (click to toggle)
mono-basic 3.8-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 26,600 kB
  • ctags: 6,969
  • sloc: cs: 34,086; xml: 26,150; makefile: 475; sh: 341
file content (298 lines) | stat: -rw-r--r-- 9,541 bytes parent folder | download | duplicates (2)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
' 
' Visual Basic.Net Compiler
' Copyright (C) 2004 - 2010 Rolf Bjarne Kvinge, RKvinge@novell.com
' 
' This library is free software; you can redistribute it and/or
' modify it under the terms of the GNU Lesser General Public
' License as published by the Free Software Foundation; either
' version 2.1 of the License, or (at your option) any later version.
' 
' This library is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
' Lesser General Public License for more details.
' 
' You should have received a copy of the GNU Lesser General Public
' License along with this library; if not, write to the Free Software
' Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
' 

Imports System.IO

''' <summary>
''' A list of tests.
''' </summary>
''' <remarks></remarks>
Public Class Tests
    Inherits Generic.Dictionary(Of String, Test)

    Private m_Filename As String
    Private m_ResultsFilename As String
    Private m_Document As XmlDocument
    Private m_VBCPath As String
    Private m_VBNCPath As String

    Public Sub Append(ByVal Test As Test)
        For i As Integer = 0 To Integer.MaxValue
            If ContainsKey(i.ToString()) = False Then
                Test.ID = i.ToString()
                Add(Test)
                Return
            End If
        Next
    End Sub

    Public Shadows Sub Add(ByVal Test As Test)
        MyBase.Add(Test.ID, Test)
    End Sub

    Public Sub Load(ByVal Filename As String)
        m_Filename = Filename
        m_ResultsFilename = Path.Combine(Path.GetDirectoryName(m_Filename), Path.GetFileNameWithoutExtension(m_Filename) + ".results" + Path.GetExtension(m_Filename))
        m_Document = New XmlDocument()
        m_Document.Load(Filename)
        For Each node As XmlNode In m_Document.SelectNodes("/rt/test")
            Dim test As New Test(Me, node)
            If Me.ContainsKey(test.ID) Then
                Debug.WriteLine(String.Format("Test list already contains the test with id {0}", test.ID))
            Else
                Add(test)
            End If
        Next
        If File.Exists(m_ResultsFilename) Then
            m_Document = New XmlDocument()
            m_Document.Load(m_ResultsFilename)
            For Each node As XmlNode In m_Document.SelectNodes("/rt/test")
                Dim id As String = node.Attributes("id").Value
                Dim test As Test = Nothing

                If Me.TryGetValue(id, test) Then
                    test.LoadResult(node)
                End If
            Next
        End If
    End Sub

    Private Shared Sub CreateBackup(ByVal fn As String)
        Dim path As String
        Dim name As String
        Dim ext As String
        Dim counter As Integer
        Dim filename As String

        If Not File.Exists(fn) Then Return

        counter = 0
        path = IO.Path.GetDirectoryName(fn)
        name = IO.Path.GetFileNameWithoutExtension(fn)
        ext = IO.Path.GetExtension(fn)
        Do
            counter += 1
            filename = IO.Path.Combine(path, name & "." & counter.ToString() & ext)
        Loop While IO.File.Exists(filename)
        IO.File.Copy(fn, filename, False)
    End Sub

    Private Sub CreateBackup()
        CreateBackup(m_Filename)
        CreateBackup(m_ResultsFilename)
    End Sub

    Public Sub Save()
        CreateBackup()
        Save(False)
        Save(True)
    End Sub

    Public Sub Save(ByVal results As Boolean)
        Dim settings As New XmlWriterSettings
        Dim writer As XmlWriter

        settings.CloseOutput = False
        settings.ConformanceLevel = ConformanceLevel.Document
        settings.Indent = True
        settings.IndentChars = vbTab
        settings.NewLineHandling = NewLineHandling.None
        settings.NewLineChars = vbLf
        settings.NewLineOnAttributes = False
        settings.OmitXmlDeclaration = False

        Using fs As New IO.StreamWriter(If(results, m_ResultsFilename, m_Filename), False, New System.Text.UTF8Encoding(False))
            fs.NewLine = settings.NewLineChars
            writer = XmlWriter.Create(fs, settings)

            writer.WriteStartElement("rt")
            For Each Test As Test In Me.Values
                Test.Save(writer, results)
            Next
            writer.WriteEndElement()
            writer.Close()
            fs.WriteLine()
        End Using
    End Sub

    ReadOnly Property GetGreenCount() As Integer
        Get
            Return Me.GetTestsCount(Test.Results.Success, Test.Results.Success)
        End Get
    End Property

    ReadOnly Property GetRedCount() As Integer
        Get
            Return Me.GetTestsCount(Test.Results.Failed, Test.Results.Failed)
        End Get
    End Property

    ReadOnly Property Filename() As String
        Get
            Return m_Filename
        End Get
    End Property

    Property VBCPath() As String
        Get
            Return m_VBCPath
        End Get
        Set(ByVal value As String)
            m_VBCPath = value
        End Set
    End Property

    Property VBNCPath() As String
        Get
            Return m_VBNCPath
        End Get
        Set(ByVal value As String)
            m_VBNCPath = value
        End Set
    End Property

    ''' <summary>
    ''' The count of tests run
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    ReadOnly Property TestsRun() As Integer
        Get
            Dim result As Integer
            For Each test As Test In Me.Values
                If test.Run Then result += 1
            Next
            Return result
        End Get
    End Property

    ''' <summary>
    ''' The count of successful tests
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    ReadOnly Property TestsSucceded() As Integer
        Get
            Dim result As Integer
            For Each test As Test In Me.Values
                If test.Run AndAlso test.Success = True Then result += 1
            Next
            Return result
        End Get
    End Property

    ''' <summary>
    ''' The total time all tests have been executing.
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    ReadOnly Property ExecutionTime() As TimeSpan
        Get
            Dim result As TimeSpan
            For Each test As Test In Me.Values
                If test.Run Then
                    result = result + test.TestDuration
                End If
            Next
            Return result
        End Get
    End Property

    ''' <summary>
    ''' The count of failed tests
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    ReadOnly Property TestsFailed() As Integer
        Get
            Dim result As Integer
            For Each test As Test In Me.Values
                If test.Run AndAlso test.Success = False Then result += 1
            Next
            Return result
        End Get
    End Property

    Sub GetTestsCount(ByVal result() As Integer)
        For Each t As Test In Me.Values
            result(t.Result) += 1
        Next
    End Sub

    Function GetTestsCount(ByVal MinResult As Test.Results, ByVal MaxResult As Test.Results) As Integer
        Dim result As Integer
        For Each t As Test In Me.Values
            If t.Result >= MinResult AndAlso t.Result <= MaxResult Then
                result += 1
            End If
        Next
        Return result
    End Function

    Function GetTestsMin(Optional ByVal MinResult As Test.Results = Test.Results.NotRun) As Tests
        If MinResult = Test.Results.NotRun Then Return Me
        Dim result As New Tests
        For Each test As Test In Me.Values
            If test.Result >= MinResult Then result.Add(test)
        Next
        Return result
    End Function

    Function GetTestsMax(Optional ByVal MaxResult As Test.Results = Test.Results.NotRun) As Tests
        Dim result As New Tests
        For Each test As Test In Me.Values
            If test.Result <= MaxResult Then result.Add(test)
        Next
        Return result
    End Function

    Function GetTestsRange(ByVal MinResult As Test.Results, ByVal MaxResult As Test.Results) As Tests
        Dim result As New Tests
        For Each test As Test In Me.Values
            If test.Result >= MinResult AndAlso test.Result <= MaxResult Then result.Add(test)
        Next
        Return result
    End Function

    ''' <summary>
    ''' Get all tests that have failed
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GetRedTests() As Tests
        Return GetTestsRange(Test.Results.Failed, Test.Results.Failed)
    End Function

    ''' <summary>
    ''' Get all tests that have reached at least level Results.Success
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GetGreenTests() As Tests
        Return GetTestsMin(Test.Results.Success)
    End Function

    Function GetNotRunTests() As Tests
        Return GetTestsMax(Test.Results.NotRun)
    End Function

    Function GetRunTests() As Tests
        Return GetTestsRange(Test.Results.Failed, Test.Results.Success)
    End Function
End Class