%
'********************************************************************
' Name: ASPUnit.asp
'
' Purpose: Contains the main ASPUnit classes
'********************************************************************
Class TestSuite
Private m_oTestCases
Private Sub Class_Initialize()
Set m_oTestCases = Server.CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
Set m_oTestCases = Nothing
End Sub
Public Sub AddTestCase(oTestContainer, sTestMethod)
Dim oTestCase
Set oTestCase = New TestCase
Set oTestCase.TestContainer = oTestContainer
oTestCase.TestMethod = sTestMethod
m_oTestCases.Add oTestCase, oTestCase
End Sub
Public Sub AddAllTestCases(oTestContainer)
Dim oTestCase, sTestMethod
For Each sTestMethod In oTestContainer.TestCaseNames()
AddTestCase oTestContainer, sTestMethod
Next
End Sub
Public Function Count()
Count = m_oTestCases.Count
End Function
Public Sub Run(oTestResult)
Dim oTestCase
For Each oTestCase In m_oTestCases.Items
oTestCase.Run oTestResult
Next
End Sub
End Class
Class TestCase
Private m_oTestContainer
Private m_sTestMethod
Public Property Get TestContainer()
Set TestContainer = m_oTestContainer
End Property
Public Property Set TestContainer(oTestContainer)
Set m_oTestContainer = oTestContainer
End Property
Public Property Get TestMethod()
TestMethod = m_sTestMethod
End Property
Public Property Let TestMethod(sTestMethod)
m_sTestMethod = sTestMethod
End Property
Public Sub Run(oTestResult)
Dim iOldFailureCount
Dim iOldErrorCount
iOldFailureCount = oTestResult.Failures.Count
iOldErrorCount = oTestResult.Errors.Count
On Error Resume Next
oTestResult.StartTest Me
m_oTestContainer.SetUp()
If (Err.Number <> 0) Then
oTestResult.AddError Err
Else
' Response.Write("m_oTestContainer." & m_sTestMethod & "(oTestResult)
")
Execute("m_oTestContainer." & m_sTestMethod & "(oTestResult)")
If (Err.Number <> 0) Then
' Response.Write(Err.Description & "
")
oTestResult.AddError Err
End If
End If
Err.Clear()
m_oTestContainer.TearDown()
If (Err.Number <> 0) Then
oTestResult.AddError Err
End If
'Log success if no failures or errors occurred
If oTestResult.Failures.Count = iOldFailureCount And oTestResult.Errors.Count = iOldErrorCount Then
oTestResult.AddSuccess
End If
oTestResult.EndTest
On Error Goto 0
End Sub
End Class
Class TestResult
Private m_dicErrors
Private m_dicFailures
Private m_dicSuccesses
Private m_dicObservers
Private m_iRunTests
Private m_oCurrentTestCase
Private Sub Class_Initialize
Set m_dicErrors = Server.CreateObject("Scripting.Dictionary")
Set m_dicFailures = Server.CreateObject("Scripting.Dictionary")
Set m_dicSuccesses = Server.CreateObject("Scripting.Dictionary")
Set m_dicObservers = Server.CreateObject("Scripting.Dictionary")
m_iRunTests = 0
End Sub
Private Sub Class_Terminate
Set m_dicErrors = Nothing
Set m_dicFailures = Nothing
Set m_dicSuccesses = Nothing
Set m_dicObservers = Nothing
Set m_oCurrentTestCase = Nothing
End Sub
Public Property Get Errors()
Set Errors = m_dicErrors
End Property
Public Property Get Failures()
Set Failures = m_dicFailures
End Property
Public Property Get Successes()
Set Successes = m_dicSuccesses
End Property
Public Property Get RunTests()
RunTests = m_iRunTests
End Property
Public Sub StartTest(oTestCase)
Set m_oCurrentTestCase = oTestCase
Dim oObserver
For Each oObserver In m_dicObservers.Items
oObserver.OnStartTest
Next
End Sub
Public Sub EndTest()
m_iRunTests = m_iRunTests + 1
Dim oObserver
For Each oObserver In m_dicObservers.Items
oObserver.OnEndTest
Next
End Sub
Public Sub AddObserver(oObserver)
m_dicObservers.Add oOserver, oObserver
End Sub
Public Function AddError(oError)
Dim oTestError
Set oTestError = New TestError
oTestError.Initialize m_oCurrentTestCase, oError.Number, oError.Source, oError.Description
m_dicErrors.Add oTestError, oTestError
Dim oObserver
For Each oObserver In m_dicObservers.Items
oObserver.OnError
Next
Set AddError = oTestError
End Function
Public Function AddFailure(sMessage)
Dim oTestError
Set oTestError = New TestError
oTestError.Initialize m_oCurrentTestCase, 0, " ", sMessage
m_dicFailures.Add oTestError, oTestError
Dim oObserver
For Each oObserver In m_dicObservers.Items
oObserver.OnFailure
Next
Set AddFailure = oTestError
End Function
Public Function AddSuccess
Dim oTestError
Set oTestError = New TestError
oTestError.Initialize m_oCurrentTestCase, 0, " ", "Test completed without failures"
m_dicSuccesses.Add oTestError, oTestError
Dim oObserver
For Each oObserver In m_dicObservers.Items
oObserver.OnSuccess
Next
End Function
Public Sub Assert(bCondition, sMessage)
If Not bCondition Then
AddFailure sMessage
End If
End Sub
Public Sub AssertEquals(vExpected, vActual, sMessage)
If vExpected <> vActual Then
AddFailure NotEqualsMessage(sMessage, vExpected, vActual)
End If
End Sub
'Build a message about a failed equality check
Function NotEqualsMessage(sMessage, vExpected, vActual)
'NotEqualsMessage = sMessage & " expected: " & CStr(vExpected) & " but was: " & CStr(vActual)
NotEqualsMessage = sMessage & "
" &_
"
Expected | (" & typename(vExpected) & ") [" & CStr(vExpected) & "] |
---|---|
Actual | (" & typename(vActual) & ") [" & CStr(vActual) & "] |