You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 line
8.4KB

  1. Option Explicit
  2. Const ForReading = 1
  3. Const TristateTrue = -1
  4. Dim fso:Set fso = CreateObject("Scripting.FileSystemObject")
  5. Dim scriptDir: scriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
  6. Dim repoRoot: repoRoot = fso.GetParentFolderName(scriptDir)
  7. Dim sourcePath: sourcePath = fso.BuildPath(repoRoot, "ImportService\\TrackingDataImport.vbs")
  8. Dim objFSO
  9. Dim DataDirectory
  10. Dim functionNames
  11. functionNames = Array( _
  12. "PadLeft", _
  13. "CheckForFiles", _
  14. "Truncate", _
  15. "PadString", _
  16. "CleanNull", _
  17. "Assign", _
  18. "Choice", _
  19. "CompressArray", _
  20. "TrimLeadingZeros", _
  21. "PushNonEmptyToBottom", _
  22. "GetState", _
  23. "GetCityFromLine" _
  24. )
  25. LoadFunctions sourcePath, functionNames
  26. Dim passed: passed = 0
  27. Dim failed: failed = 0
  28. Dim q: q = Chr(34)
  29. AssertEqual Truncate("abcdef", 3), q & "abc" & q & ",", "Truncate trims and quotes"
  30. AssertEqual Truncate("ab", 3), q & "ab" & q & ",", "Truncate short string"
  31. AssertEqual PadLeft("7", 3, "0"), "007", "PadLeft pads"
  32. AssertEqual PadLeft("1234", 3, "0"), "1234", "PadLeft no pad when longer"
  33. AssertEqual PadString("ab", 4), "ab ", "PadString right pads"
  34. AssertEqual PadString("abcd", 4), "abcd", "PadString same length"
  35. AssertEqual PadString(Null, 3), " ", "PadString null"
  36. AssertEqual CleanNull(Null), "", "CleanNull null"
  37. AssertEqual CleanNull("x"), "x", "CleanNull value"
  38. Dim arr
  39. arr = Array("", "a", "", "b")
  40. arr = CompressArray(arr)
  41. AssertArrayEqual arr, Array("a", "b", "", ""), "CompressArray moves blanks to end"
  42. AssertEqual TrimLeadingZeros("000123"), "123", "TrimLeadingZeros removes leading zeros"
  43. AssertEqual TrimLeadingZeros("0000"), "", "TrimLeadingZeros all zeros"
  44. Dim arr2
  45. arr2 = Array("a", "", "b", "")
  46. PushNonEmptyToBottom arr2
  47. AssertArrayEqual arr2, Array("", "", "a", "b"), "PushNonEmptyToBottom"
  48. AssertEqual GetState("Lansing, MI 48906"), "MI", "GetState matches state"
  49. AssertEqual GetState("No match"), "", "GetState no match"
  50. AssertEqual GetCityFromLine("Lansing, MI 48906"), "Lansing", "GetCityFromLine with comma"
  51. AssertEqual GetCityFromLine("NoComma"), "NoComma", "GetCityFromLine no comma"
  52. AssertEqual GetCityFromLine(Null), "", "GetCityFromLine null"
  53. Dim tempDir
  54. tempDir = fso.BuildPath(repoRoot, "Tests\\_tmp_checkforfiles")
  55. If Not fso.FolderExists(tempDir) Then
  56. fso.CreateFolder tempDir
  57. End If
  58. Set objFSO = fso
  59. DataDirectory = tempDir
  60. On Error Resume Next
  61. Dim checkResult
  62. checkResult = CheckForFiles()
  63. If Err.Number = 0 Then
  64. passed = passed + 1
  65. WScript.Echo "PASS: CheckForFiles runs without error (empty dir)"
  66. Else
  67. failed = failed + 1
  68. WScript.Echo "FAIL: CheckForFiles error " & Err.Number & " - " & Err.Description
  69. Err.Clear
  70. End If
  71. On Error GoTo 0
  72. WScript.Echo ""
  73. WScript.Echo "Passed: " & passed
  74. WScript.Echo "Failed: " & failed
  75. If failed > 0 Then
  76. WScript.Quit 1
  77. End If
  78. Sub LoadFunctions(ByVal filePath, ByVal names)
  79. Dim fileText, lines, i, line
  80. Dim capturing: capturing = False
  81. Dim endKeyword: endKeyword = ""
  82. Dim block: block = ""
  83. If Not fso.FileExists(filePath) Then
  84. WScript.Echo "FAIL: Source file not found: " & filePath
  85. WScript.Quit 1
  86. End If
  87. With fso.OpenTextFile(filePath, ForReading, False, TristateTrue)
  88. fileText = .ReadAll
  89. .Close
  90. End With
  91. ' Normalize line endings and remove nulls/BOM that can appear in UTF-16 reads
  92. fileText = Replace(fileText, ChrW(&HFEFF), "")
  93. fileText = Replace(fileText, Chr(0), "")
  94. fileText = Replace(fileText, vbTab, " ")
  95. fileText = Replace(fileText, vbCrLf, vbLf)
  96. fileText = Replace(fileText, vbCr, vbLf)
  97. lines = Split(fileText, vbLf)
  98. For i = 0 To UBound(lines)
  99. line = lines(i)
  100. If Not capturing Then
  101. Dim nameIndex
  102. For nameIndex = 0 To UBound(names)
  103. Dim kind
  104. If TryStartLine(line, names(nameIndex), kind) Then
  105. capturing = True
  106. endKeyword = "End " & kind
  107. block = block & line & vbCrLf
  108. Exit For
  109. End If
  110. Next
  111. Else
  112. block = block & line & vbCrLf
  113. If LCase(Trim(line)) = LCase(endKeyword) Then
  114. capturing = False
  115. block = block & vbCrLf
  116. End If
  117. End If
  118. Next
  119. If Len(block) = 0 Then
  120. ' Fallback: simple text scan for each function/sub
  121. Dim nameIndex2
  122. For nameIndex2 = 0 To UBound(names)
  123. Dim extracted
  124. extracted = ExtractBlock(fileText, names(nameIndex2))
  125. If Len(extracted) > 0 Then
  126. block = block & extracted & vbCrLf & vbCrLf
  127. End If
  128. Next
  129. End If
  130. If Len(block) = 0 Then
  131. WScript.Echo "FAIL: No functions loaded from " & filePath
  132. WScript.Echo "Debug: file length=" & Len(fileText)
  133. WScript.Quit 1
  134. End If
  135. ExecuteGlobal block
  136. End Sub
  137. Function ExtractBlock(ByVal fileText, ByVal name)
  138. Dim lowerText, lowerName, startPos, endPos, kind, endToken
  139. lowerText = LCase(fileText)
  140. lowerName = LCase(name)
  141. startPos = FindDeclPos(lowerText, lowerName, "function")
  142. kind = "Function"
  143. If startPos = 0 Then
  144. startPos = FindDeclPos(lowerText, lowerName, "sub")
  145. kind = "Sub"
  146. End If
  147. If startPos = 0 Then
  148. ExtractBlock = ""
  149. Exit Function
  150. End If
  151. endToken = "end " & LCase(kind)
  152. endPos = InStr(startPos, lowerText, endToken)
  153. If endPos = 0 Then
  154. ExtractBlock = ""
  155. Exit Function
  156. End If
  157. endPos = endPos + Len(endToken) - 1
  158. ExtractBlock = Mid(fileText, startPos, endPos - startPos + 1)
  159. End Function
  160. Function FindDeclPos(ByVal lowerText, ByVal lowerName, ByVal keyword)
  161. Dim pos, idx, ch, nameLen, nextChar
  162. nameLen = Len(lowerName)
  163. pos = InStr(1, lowerText, keyword)
  164. Do While pos > 0
  165. idx = pos + Len(keyword)
  166. Do While idx <= Len(lowerText)
  167. ch = Mid(lowerText, idx, 1)
  168. If IsWs(ch) Then
  169. idx = idx + 1
  170. Else
  171. Exit Do
  172. End If
  173. Loop
  174. If LCase(Mid(lowerText, idx, nameLen)) = lowerName Then
  175. nextChar = Mid(lowerText, idx + nameLen, 1)
  176. If nextChar = "" Or Not IsNameChar(nextChar) Then
  177. FindDeclPos = pos
  178. Exit Function
  179. End If
  180. End If
  181. pos = InStr(pos + 1, lowerText, keyword)
  182. Loop
  183. FindDeclPos = 0
  184. End Function
  185. Function IsWs(ByVal ch)
  186. IsWs = (ch = " " Or ch = vbTab Or ch = vbLf Or ch = vbCr Or AscW(ch) = 160)
  187. End Function
  188. Function IsNameChar(ByVal ch)
  189. Dim code
  190. code = AscW(ch)
  191. IsNameChar = (code >= 48 And code <= 57) Or (code >= 65 And code <= 90) Or (code >= 97 And code <= 122) Or ch = "_"
  192. End Function
  193. Function TryStartLine(ByVal line, ByVal name, ByRef kind)
  194. Dim lowerLine, lowerName, pos
  195. lowerLine = LCase(line)
  196. lowerName = LCase(name)
  197. pos = FindDeclPos(lowerLine, lowerName, "function")
  198. If pos > 0 Then
  199. kind = "Function"
  200. TryStartLine = True
  201. Exit Function
  202. End If
  203. pos = FindDeclPos(lowerLine, lowerName, "sub")
  204. If pos > 0 Then
  205. kind = "Sub"
  206. TryStartLine = True
  207. Exit Function
  208. End If
  209. TryStartLine = False
  210. End Function
  211. Sub AssertEqual(ByVal actual, ByVal expected, ByVal testName)
  212. If actual = expected Then
  213. passed = passed + 1
  214. WScript.Echo "PASS: " & testName
  215. Else
  216. failed = failed + 1
  217. WScript.Echo "FAIL: " & testName & " | expected=" & FormatValue(expected) & " actual=" & FormatValue(actual)
  218. End If
  219. End Sub
  220. Sub AssertArrayEqual(ByVal actual, ByVal expected, ByVal testName)
  221. Dim ok: ok = True
  222. Dim i
  223. If (UBound(actual) <> UBound(expected)) Then
  224. ok = False
  225. Else
  226. For i = 0 To UBound(actual)
  227. If actual(i) <> expected(i) Then
  228. ok = False
  229. Exit For
  230. End If
  231. Next
  232. End If
  233. If ok Then
  234. passed = passed + 1
  235. WScript.Echo "PASS: " & testName
  236. Else
  237. failed = failed + 1
  238. WScript.Echo "FAIL: " & testName & " | expected=" & ArrayToString(expected) & " actual=" & ArrayToString(actual)
  239. End If
  240. End Sub
  241. Function ArrayToString(ByVal arr)
  242. Dim i, s
  243. s = "["
  244. For i = 0 To UBound(arr)
  245. If i > 0 Then s = s & ", "
  246. s = s & FormatValue(arr(i))
  247. Next
  248. s = s & "]"
  249. ArrayToString = s
  250. End Function
  251. Function FormatValue(ByVal v)
  252. If IsNull(v) Then
  253. FormatValue = "<null>"
  254. Else
  255. FormatValue = "\"" & CStr(v) & "\""
  256. End If
  257. End Function

Powered by TurnKey Linux.