Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

8 місяці тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <%
  2. '********************************************************************
  3. ' Name: ASPUnit.asp
  4. '
  5. ' Purpose: Contains the main ASPUnit classes
  6. '********************************************************************
  7. Class TestSuite
  8. Private m_oTestCases
  9. Private Sub Class_Initialize()
  10. Set m_oTestCases = Server.CreateObject("Scripting.Dictionary")
  11. End Sub
  12. Private Sub Class_Terminate()
  13. Set m_oTestCases = Nothing
  14. End Sub
  15. Public Sub AddTestCase(oTestContainer, sTestMethod)
  16. Dim oTestCase
  17. Set oTestCase = New TestCase
  18. Set oTestCase.TestContainer = oTestContainer
  19. oTestCase.TestMethod = sTestMethod
  20. m_oTestCases.Add oTestCase, oTestCase
  21. End Sub
  22. Public Sub AddAllTestCases(oTestContainer)
  23. Dim oTestCase, sTestMethod
  24. For Each sTestMethod In oTestContainer.TestCaseNames()
  25. AddTestCase oTestContainer, sTestMethod
  26. Next
  27. End Sub
  28. Public Function Count()
  29. Count = m_oTestCases.Count
  30. End Function
  31. Public Sub Run(oTestResult)
  32. Dim oTestCase
  33. For Each oTestCase In m_oTestCases.Items
  34. oTestCase.Run oTestResult
  35. Next
  36. End Sub
  37. End Class
  38. Class TestCase
  39. Private m_oTestContainer
  40. Private m_sTestMethod
  41. Public Property Get TestContainer()
  42. Set TestContainer = m_oTestContainer
  43. End Property
  44. Public Property Set TestContainer(oTestContainer)
  45. Set m_oTestContainer = oTestContainer
  46. End Property
  47. Public Property Get TestMethod()
  48. TestMethod = m_sTestMethod
  49. End Property
  50. Public Property Let TestMethod(sTestMethod)
  51. m_sTestMethod = sTestMethod
  52. End Property
  53. Public Sub Run(oTestResult)
  54. Dim iOldFailureCount
  55. Dim iOldErrorCount
  56. iOldFailureCount = oTestResult.Failures.Count
  57. iOldErrorCount = oTestResult.Errors.Count
  58. On Error Resume Next
  59. oTestResult.StartTest Me
  60. m_oTestContainer.SetUp()
  61. If (Err.Number <> 0) Then
  62. oTestResult.AddError Err
  63. Else
  64. ' Response.Write("m_oTestContainer." & m_sTestMethod & "(oTestResult)<br>")
  65. Execute("m_oTestContainer." & m_sTestMethod & "(oTestResult)")
  66. If (Err.Number <> 0) Then
  67. ' Response.Write(Err.Description & "<br>")
  68. oTestResult.AddError Err
  69. End If
  70. End If
  71. Err.Clear()
  72. m_oTestContainer.TearDown()
  73. If (Err.Number <> 0) Then
  74. oTestResult.AddError Err
  75. End If
  76. 'Log success if no failures or errors occurred
  77. If oTestResult.Failures.Count = iOldFailureCount And oTestResult.Errors.Count = iOldErrorCount Then
  78. oTestResult.AddSuccess
  79. End If
  80. oTestResult.EndTest
  81. On Error Goto 0
  82. End Sub
  83. End Class
  84. Class TestResult
  85. Private m_dicErrors
  86. Private m_dicFailures
  87. Private m_dicSuccesses
  88. Private m_dicObservers
  89. Private m_iRunTests
  90. Private m_oCurrentTestCase
  91. Private Sub Class_Initialize
  92. Set m_dicErrors = Server.CreateObject("Scripting.Dictionary")
  93. Set m_dicFailures = Server.CreateObject("Scripting.Dictionary")
  94. Set m_dicSuccesses = Server.CreateObject("Scripting.Dictionary")
  95. Set m_dicObservers = Server.CreateObject("Scripting.Dictionary")
  96. m_iRunTests = 0
  97. End Sub
  98. Private Sub Class_Terminate
  99. Set m_dicErrors = Nothing
  100. Set m_dicFailures = Nothing
  101. Set m_dicSuccesses = Nothing
  102. Set m_dicObservers = Nothing
  103. Set m_oCurrentTestCase = Nothing
  104. End Sub
  105. Public Property Get Errors()
  106. Set Errors = m_dicErrors
  107. End Property
  108. Public Property Get Failures()
  109. Set Failures = m_dicFailures
  110. End Property
  111. Public Property Get Successes()
  112. Set Successes = m_dicSuccesses
  113. End Property
  114. Public Property Get RunTests()
  115. RunTests = m_iRunTests
  116. End Property
  117. Public Sub StartTest(oTestCase)
  118. Set m_oCurrentTestCase = oTestCase
  119. Dim oObserver
  120. For Each oObserver In m_dicObservers.Items
  121. oObserver.OnStartTest
  122. Next
  123. End Sub
  124. Public Sub EndTest()
  125. m_iRunTests = m_iRunTests + 1
  126. Dim oObserver
  127. For Each oObserver In m_dicObservers.Items
  128. oObserver.OnEndTest
  129. Next
  130. End Sub
  131. Public Sub AddObserver(oObserver)
  132. m_dicObservers.Add oOserver, oObserver
  133. End Sub
  134. Public Function AddError(oError)
  135. Dim oTestError
  136. Set oTestError = New TestError
  137. oTestError.Initialize m_oCurrentTestCase, oError.Number, oError.Source, oError.Description
  138. m_dicErrors.Add oTestError, oTestError
  139. Dim oObserver
  140. For Each oObserver In m_dicObservers.Items
  141. oObserver.OnError
  142. Next
  143. Set AddError = oTestError
  144. End Function
  145. Public Function AddFailure(sMessage)
  146. Dim oTestError
  147. Set oTestError = New TestError
  148. oTestError.Initialize m_oCurrentTestCase, 0, " ", sMessage
  149. m_dicFailures.Add oTestError, oTestError
  150. Dim oObserver
  151. For Each oObserver In m_dicObservers.Items
  152. oObserver.OnFailure
  153. Next
  154. Set AddFailure = oTestError
  155. End Function
  156. Public Function AddSuccess
  157. Dim oTestError
  158. Set oTestError = New TestError
  159. oTestError.Initialize m_oCurrentTestCase, 0, " ", "Test completed without failures"
  160. m_dicSuccesses.Add oTestError, oTestError
  161. Dim oObserver
  162. For Each oObserver In m_dicObservers.Items
  163. oObserver.OnSuccess
  164. Next
  165. End Function
  166. Public Sub Assert(bCondition, sMessage)
  167. If Not bCondition Then
  168. AddFailure sMessage
  169. End If
  170. End Sub
  171. Public Sub AssertEquals(vExpected, vActual, sMessage)
  172. If vExpected <> vActual Then
  173. AddFailure NotEqualsMessage(sMessage, vExpected, vActual)
  174. End If
  175. End Sub
  176. 'Build a message about a failed equality check
  177. Function NotEqualsMessage(sMessage, vExpected, vActual)
  178. 'NotEqualsMessage = sMessage & " expected: " & CStr(vExpected) & " but was: " & CStr(vActual)
  179. NotEqualsMessage = sMessage & "<br>" &_
  180. "<table><tr><th class='expected'>Expected</th><td class='expected'><span class='left-delim'>(" & typename(vExpected) & ") [</span>" & CStr(vExpected) & "<span class='right-delim'>]</span></td></tr><tr><th class='actual'>Actual</th><td class='actual'><span class='left-delim'>(" & typename(vActual) & ") [</span>" & CStr(vActual) & "<span class='right-delim'>]</span></td></tr></table>"
  181. End Function
  182. Public Sub AssertNotEquals(vExpected, vActual, sMessage)
  183. If vExpected = vActual Then
  184. AddFailure sMessage & " expected: " & CStr(vExpected) & " and actual: " & CStr(vActual) & " should not be equal."
  185. End If
  186. End Sub
  187. Public Sub AssertExists(vVariable, sMessage)
  188. If IsObject(vVariable) Then
  189. If (vVariable Is Nothing) Then
  190. AddFailure sMessage & " - Variable of type " & TypeName(vVariable) & " is Nothing."
  191. End If
  192. ElseIf IsEmpty(vVariable) Then
  193. AddFailure sMessage & " - Variable " & TypeName(vVariable) & " is Empty (Uninitialized)."
  194. End If
  195. End Sub
  196. '---------------------------------------------------------------------------------------------------------------------
  197. ' CUSTOM ASSERTIONS
  198. '---------------------------------------------------------------------------------------------------------------------
  199. Public Sub AssertFalse(bCondition, sMessage)
  200. If bCondition then
  201. AddFailure sMessage
  202. End If
  203. End Sub
  204. Public Sub AssertEqual(vExpected, vActual, sMessage)
  205. AssertEquals vExpected, vActual, sMessage
  206. End Sub
  207. Public Sub AssertNotEqual(vExpected, vActual, sMessage)
  208. AssertNotEqual vExpected, vActual, sMessage
  209. End Sub
  210. Public Sub AssertNotExists(vVariable, sMessage)
  211. If IsObject(vVariable) then
  212. If (vVariable Is Not Nothing) then
  213. AddFailure sMessage & " - Variable of type " & TypeName(vVariable) & " should be Nothing."
  214. End If
  215. ElseIf Not IsEmpty(vVariable) then
  216. AddFailure sMessage & " - Variable " & TypeName(vVariable) & " should be Empty (Uninitialized)."
  217. End If
  218. End Sub
  219. 'Ensures (obj1 Is obj2) = true
  220. Public Sub AssertSame(obj1, obj2, sMessage)
  221. Assert (obj1 Is obj2), sMessage
  222. End Sub
  223. 'Ensures (obj1 Is obj2) = false
  224. Public Sub AssertDifferent(obj1, obj2, sMessage)
  225. Assert (not (obj1 Is obj2)), sMessage
  226. End Sub
  227. 'Forces a test failure
  228. Public Sub Fail(sMessage)
  229. AddFailure "Forced Failure: " & sMessage
  230. End Sub
  231. 'Flags a test as needing implementation, otherwise an empty test will silently pass
  232. Public Sub NotImplemented
  233. AddFailure "Test not implemented."
  234. End Sub
  235. Public Sub AssertType(sTypeName, vVariable, sMessage)
  236. AssertEqual sTypeName, typename(vVariable), sMessage
  237. End Sub
  238. End Class
  239. Class TestError
  240. Private m_oTestCase
  241. Private m_lErrNumber
  242. Private m_sSource
  243. Private m_sDescription
  244. Public Sub Initialize(oTestCase, lErrNumber, sSource, sDescription)
  245. Set m_oTestCase = oTestCase
  246. m_lErrNumber = lErrNumber
  247. m_sSource = sSource
  248. m_sDescription = sDescription
  249. End Sub
  250. Public Property Get TestCase
  251. Set TestCase = m_oTestCase
  252. End Property
  253. Public Property Get ErrNumber
  254. ErrNumber = m_lErrNumber
  255. End Property
  256. Public Property Get Source
  257. Source = m_sSource
  258. End Property
  259. Public Property Get Description
  260. Description = m_sDescription
  261. End Property
  262. End Class
  263. %>

Powered by TurnKey Linux.