home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / sregexpf / test.vbs < prev    next >
Encoding:
Text File  |  1998-07-15  |  1.9 KB  |  69 lines

  1.     On Error Resume next
  2.     Dim sLine, sExpr, sText, sSuccess, sReplace, sReplaced, arrLine
  3.     Dim sTab
  4.  
  5.     ' Create object and initialize variables
  6.     sTab   = Chr(9)
  7.     set re = CreateObject("sgRegExp.RegExp")
  8.     Set fs = CreateObject("Scripting.FileSystemObject")
  9.     Set ts = fs.OpenTextFile("tests.txt", 1, False)    
  10.  
  11.     ' For each line in the test file
  12.     do while not ts.AtEndOfStream
  13.  
  14.         ' Get next line and ensure that it has valid syntax
  15.         nLine = ts.Line
  16.         sLine = ts.ReadLine
  17.         arrLine = Split(sLine, sTab, 5)
  18.         if (UBound(arrLine) <> 4) then
  19.             WScript.Echo "Error at line(" + CStr(nLine) + "): match that supposed to succeed has failed"
  20.             WScript.Quit(0)
  21.         end if
  22.         sExpr     = arrLine(0)
  23.         sText     = arrLine(1)
  24.         sSuccess  = arrLine(2)
  25.         sReplace  = arrLine(3)
  26.         sReplaced = arrLine(4)
  27.  
  28.         if sText = "-" then sText = ""
  29.         if sReplace = "-" then sReplace = ""
  30.         if sReplaced = "-" then sReplaced = ""
  31.  
  32.         ' Test regular expression
  33.         re.Expression = sExpr
  34.         bMatched = re.Match(sText)
  35.         if bMatched then
  36.             if sSuccess = "n" then
  37.                 ErrorEcho "Error at line(" + CStr(nLine) + "): match that supposed to fail has succeeded"
  38.                 WScript.Quit(0)
  39.             end if
  40.  
  41.             s = re.ReplaceString(sReplace)
  42.             if (s <> sReplaced) then
  43.                 ErrorEcho  "Error at line(" + CStr(nLine) + "): ReplaceString returned '" + s + "' and expected replacement was '" + sReplace + "'"
  44.                 WScript.Quit(0)
  45.             end if
  46.         else
  47.             if sSuccess = "y" then
  48.                 ErrorEcho  "Error at line(" + CStr(nLine) + "): match that supposed to succeed has failed"
  49.                 WScript.Quit(0)
  50.             end if
  51.         end if
  52.         WScript.Echo "Passed test at line(" + CStr(nLine) + "): " + sLine
  53.     loop
  54.     WScript.Echo ""
  55.     WScript.Echo "ALL TESTS PASSED!"
  56.  
  57.     set re = Nothing
  58.     Set fs = Nothing
  59.     Set ts = Nothing
  60.     WScript.Quit(0)
  61.  
  62. public sub ErrorEcho(sErr)
  63.     WScript.Echo sErr
  64.     for i = 0 to UBound(arrLine)
  65.         WScript.Echo "|" + arrLine(i) + "|"
  66.     next
  67.  
  68. end sub
  69.