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

279 рядки
7.5KB

  1. Option Explicit
  2. Dim g_TestsRun
  3. Dim g_TestsPassed
  4. Dim g_Failures
  5. g_TestsRun = 0
  6. g_TestsPassed = 0
  7. Set g_Failures = CreateObject("Scripting.Dictionary")
  8. Dim ASSERTION_FAILED
  9. ASSERTION_FAILED = vbObjectError + 1999
  10. ' Runs the named zero-argument Sub/Function, treating any unhandled error
  11. ' (including one raised by a failed Assert*) as a failed test. This mirrors
  12. ' exactly the On Error Resume Next / Err.Number pattern already used in
  13. ' Public/Default.asp - an error raised in a called procedure with no error
  14. ' handling of its own is caught by the caller's active On Error Resume Next.
  15. Sub RunTest(ByVal testName, ByVal procName)
  16. g_TestsRun = g_TestsRun + 1
  17. On Error Resume Next
  18. Err.Clear
  19. Execute procName & "()"
  20. If Err.Number <> 0 Then
  21. RecordFailure testName, Err.Description
  22. Else
  23. g_TestsPassed = g_TestsPassed + 1
  24. End If
  25. On Error Goto 0
  26. End Sub
  27. Sub RecordFailure(ByVal testName, ByVal message)
  28. g_Failures.Add g_Failures.Count, testName & " -- " & message
  29. End Sub
  30. Sub PrintSummary()
  31. Dim failureKey
  32. WScript.Echo ""
  33. WScript.Echo "----------------------------------------"
  34. WScript.Echo g_TestsPassed & " / " & g_TestsRun & " tests passed"
  35. If g_Failures.Count > 0 Then
  36. WScript.Echo ""
  37. WScript.Echo "Failures:"
  38. For Each failureKey In g_Failures.Keys
  39. WScript.Echo " - " & g_Failures.Item(failureKey)
  40. Next
  41. End If
  42. WScript.Echo "----------------------------------------"
  43. End Sub
  44. Function HasFailures()
  45. HasFailures = (g_Failures.Count > 0)
  46. End Function
  47. ' --- Assertions ---
  48. Sub AssertEquals(ByVal expected, ByVal actual, ByVal message)
  49. If Not (expected = actual) Then
  50. Fail message & " (expected [" & CStr(expected) & "] but got [" & CStr(actual) & "])"
  51. End If
  52. End Sub
  53. Sub AssertTrue(ByVal condition, ByVal message)
  54. If Not condition Then
  55. Fail message & " (expected True)"
  56. End If
  57. End Sub
  58. Sub AssertFalse(ByVal condition, ByVal message)
  59. If condition Then
  60. Fail message & " (expected False)"
  61. End If
  62. End Sub
  63. Sub AssertIsNothing(ByVal value, ByVal message)
  64. If Not (value Is Nothing) Then
  65. Fail message & " (expected Nothing)"
  66. End If
  67. End Sub
  68. Sub AssertNotNothing(ByVal value, ByVal message)
  69. If value Is Nothing Then
  70. Fail message & " (expected an object, got Nothing)"
  71. End If
  72. End Sub
  73. Sub Fail(ByVal message)
  74. Err.Raise ASSERTION_FAILED, "Assert", message
  75. End Sub
  76. ' --- Loading Core/*.asp class files outside IIS ---
  77. ' These files are plain "<% Class ... End Class %>" blocks with no mixed HTML
  78. ' and (deliberately - see CLAUDE.md) no Option Explicit of their own, so
  79. ' stripping the ASP delimiters and running the remainder through ExecuteGlobal
  80. ' loads the same class definitions IIS would compile, without needing IIS.
  81. Sub IncludeAspClass(ByVal path)
  82. Dim raw
  83. Dim code
  84. Dim openPos
  85. Dim closePos
  86. raw = ReadFileText(path)
  87. openPos = InStr(raw, "<%")
  88. closePos = InStrRev(raw, "%>")
  89. If openPos = 0 Or closePos = 0 Or closePos <= openPos Then
  90. Err.Raise vbObjectError + 1998, "IncludeAspClass", "No <% %> block found in: " & path
  91. End If
  92. code = Mid(raw, openPos + 2, closePos - (openPos + 2))
  93. ExecuteGlobal code
  94. End Sub
  95. Sub IncludeVbsFile(ByVal path)
  96. ExecuteGlobal ReadFileText(path)
  97. End Sub
  98. Function ReadFileText(ByVal path)
  99. Dim fso
  100. Dim stream
  101. Set fso = CreateObject("Scripting.FileSystemObject")
  102. Set stream = fso.OpenTextFile(path, 1)
  103. ReadFileText = stream.ReadAll
  104. stream.Close
  105. End Function
  106. ' --- ASP intrinsic stand-ins ---
  107. ' Global variables literally named Server/Response so Core/*.asp's
  108. ' "Server.CreateObject(...)" / "Server.HTMLEncode(...)" / "Response.Status = ..."
  109. ' resolve to these instead of the real (IIS-only) intrinsics.
  110. Function RealCreateObject(ByVal progId)
  111. Set RealCreateObject = CreateObject(progId)
  112. End Function
  113. Function SimpleHtmlEncode(ByVal text)
  114. Dim result
  115. result = CStr(text)
  116. result = Replace(result, "&", "&amp;")
  117. result = Replace(result, "<", "&lt;")
  118. result = Replace(result, ">", "&gt;")
  119. result = Replace(result, """", "&quot;")
  120. SimpleHtmlEncode = result
  121. End Function
  122. Class FakeServer
  123. Public Function CreateObject(ByVal progId)
  124. Set CreateObject = RealCreateObject(progId)
  125. End Function
  126. Public Function HTMLEncode(ByVal text)
  127. HTMLEncode = SimpleHtmlEncode(text)
  128. End Function
  129. Public Function MapPath(ByVal relativePath)
  130. MapPath = "C:\fake-physical-root" & relativePath
  131. End Function
  132. End Class
  133. Class FakeResponse
  134. Public Status
  135. Public Buffer
  136. Public ContentType
  137. Private m_Output
  138. Private Sub Class_Initialize()
  139. Status = "200 OK"
  140. Buffer = False
  141. ContentType = ""
  142. m_Output = ""
  143. End Sub
  144. Public Sub Write(ByVal text)
  145. m_Output = m_Output & text
  146. End Sub
  147. Public Property Get Output()
  148. Output = m_Output
  149. End Property
  150. End Class
  151. ' A neutral dummy object for tests that just need "some object" to register
  152. ' as a service - not a stand-in for any real ASP intrinsic.
  153. Class FakeService
  154. Public Marker
  155. End Class
  156. ' A stand-in for the real ASP Request object that Core/HttpRequest.asp's
  157. ' Bind() expects - only implements what the current code paths touch.
  158. Class FakeAspRequest
  159. Private m_ServerVariables
  160. Private m_QueryString
  161. Private m_Form
  162. Private m_Cookies
  163. Private Sub Class_Initialize()
  164. Set m_ServerVariables = CreateObject("Scripting.Dictionary")
  165. Set m_QueryString = CreateObject("Scripting.Dictionary")
  166. Set m_Form = CreateObject("Scripting.Dictionary")
  167. Set m_Cookies = CreateObject("Scripting.Dictionary")
  168. End Sub
  169. Public Sub SetServerVariable(ByVal key, ByVal value)
  170. m_ServerVariables.Item(CStr(key)) = value
  171. End Sub
  172. Public Sub SetQueryString(ByVal key, ByVal value)
  173. m_QueryString.Item(CStr(key)) = value
  174. End Sub
  175. Public Sub SetForm(ByVal key, ByVal value)
  176. m_Form.Item(CStr(key)) = value
  177. End Sub
  178. Public Sub SetCookie(ByVal key, ByVal value)
  179. m_Cookies.Item(CStr(key)) = value
  180. End Sub
  181. Public Function ServerVariables(ByVal key)
  182. If m_ServerVariables.Exists(CStr(key)) Then
  183. ServerVariables = m_ServerVariables.Item(CStr(key))
  184. Else
  185. ServerVariables = ""
  186. End If
  187. End Function
  188. Public Function QueryString(ByVal key)
  189. If m_QueryString.Exists(CStr(key)) Then
  190. QueryString = m_QueryString.Item(CStr(key))
  191. Else
  192. QueryString = ""
  193. End If
  194. End Function
  195. Public Function Form(ByVal key)
  196. If m_Form.Exists(CStr(key)) Then
  197. Form = m_Form.Item(CStr(key))
  198. Else
  199. Form = ""
  200. End If
  201. End Function
  202. Public Function Cookies(ByVal key)
  203. If m_Cookies.Exists(CStr(key)) Then
  204. Cookies = m_Cookies.Item(CStr(key))
  205. Else
  206. Cookies = ""
  207. End If
  208. End Function
  209. Public Default Function Item(ByVal key)
  210. If m_QueryString.Exists(CStr(key)) Then
  211. Item = m_QueryString.Item(CStr(key))
  212. ElseIf m_Form.Exists(CStr(key)) Then
  213. Item = m_Form.Item(CStr(key))
  214. ElseIf m_Cookies.Exists(CStr(key)) Then
  215. Item = m_Cookies.Item(CStr(key))
  216. ElseIf m_ServerVariables.Exists(CStr(key)) Then
  217. Item = m_ServerVariables.Item(CStr(key))
  218. Else
  219. Item = ""
  220. End If
  221. End Function
  222. End Class
  223. Dim Server
  224. Dim Response
  225. Set Server = New FakeServer
  226. Set Response = New FakeResponse

Powered by TurnKey Linux.