Download Custom Testing Class (vbs file)

  1. '**********************************************************
  2. 'FILE: cTesting.vbs
  3. 'VERSION: 1.0
  4. 'CREATED: 05/10/2004
  5. 'UPDATED:   05/13/2004
  6. 'AUTHOR:    Scott Greenberg
  7. 'COMPANY:   SG Technology
  8. 'WEBSITE: http://gogogadgetscott.info
  9. 'DECRIPTION: A custom VBScript testing class
  10. 'COMMENT: N/A
  11. 'Copyright© 2004. SG Technology. All rights reserved.
  12. '**********************************************************
  13. '=====================================
  14. 'Class Usage
  15. '=====================================
  16. sTestFolder = "D:\Programming\Scripts\VBScripting\classes"
  17. 'Include sTestFolder & "\cTesting.vbs"
  18. 'SwitchCScript()
  19. 'Example()
  20. Sub Include(sScriptPath)
  21.   Dim m_FSO, m_oFile
  22.   Set m_FSO = CreateObject ("Scripting.Filesystemobject")
  23.   If m_FSO.FileExists (sScriptPath) Then
  24.     Set m_oFile = m_FSO.OpenTextFile (sScriptPath)
  25.     ExecuteGlobal m_oFile.ReadAll ()
  26.     m_oFile.Close
  27.     Set m_oFile = Nothing
  28.   End If
  29.   Set m_FSO = Nothing
  30. End Sub
  31. Sub Example()
  32.   'Create New intencses of classes
  33.   Set timer1 = New cTimer
  34.   Set Out = New cOutput
  35.   'Clear any prior output
  36.   Out.Clear
  37.   MsgBox "Start"
  38.   'Run a few tests With timer
  39.   With timer1
  40.     .StartTimer()
  41.     WScript.Sleep 1000
  42.     .StopTimer()
  43.     'Print results
  44.     Out.WriteLine timer1.Elapsed()
  45.     .StartTimer()
  46.     WScript.Sleep 2000
  47.     .StopTimer()
  48.     Out.WriteLine .Elapsed()
  49.   End With
  50.   'Create header file
  51.   Set Head = New cOutput
  52.   With Head
  53.     .File = sTestFolder & "\head.txt"
  54.     .Clear
  55.     .WriteLine "VBScript Custom Testing Class"
  56.     .WriteLine "By: SG Technology"
  57.   End With
  58.   'Create footer file
  59.   Set Foot = New cOutput
  60.   With Foot
  61.     .File = sTestFolder & "\foot.txt"
  62.     .Clear
  63.     .WriteLine "VBScript Custom Testing Class"
  64.   End With
  65.   'Apply header And footer To output file
  66.   Out.Header = Head.File
  67.   Out.Footer = Foot.File
  68.   'Show output file
  69.   Out.View
  70.   'Delete header And footer files
  71.   Head.Delete
  72.   Foot.Delete
  73.   MsgBox "Done"
  74. End Sub
  75. '=====================================
  76. 'Execute options
  77. '=====================================
  78. 'Switch To debuging
  79. Sub debug()
  80.   sCmd = "cscript.exe """ & WScript.scriptfullname & """ //X " & GetArgs
  81.   Execute sCmd
  82. End Sub
  83. Sub SwitchCScript()
  84.   sCmd = "cscript.exe """ & WScript.scriptfullname & """ " & GetArgs
  85.   Execute sCmd
  86. End Sub
  87. Private Function GetArgs()
  88.   Set oArgs = WScript.Arguments
  89.   For i = 0 To oArgs.Count - 1
  90.     GetArgs = GetArgs & " " & oArgs(i)
  91.   Next
  92. End Function
  93. Private Sub Execute(sCmd)
  94.   Set Sh = CreateObject ("WScript.Shell")
  95.   If (InStr (LCase (WScript.FullName), "wscript") <> 0) Then
  96.     Sh.Run sCmd, 1, False
  97.     WScript.Quit -1
  98.   End If
  99. End Sub
  100. '=====================================
  101. 'Timer class
  102. '=====================================
  103. Class cTimer
  104.   Private timStart, timElapsed, nFrequency, m_bRunning
  105.   Private Sub Class_Initialize()
  106.     nFrequency = 0
  107.   End Sub
  108.   'Starts the Counter
  109.   Public Sub StartTimer()
  110.     timElapsed = 0 'Reset Counter
  111.     m_bRunning = True
  112.     timStart = Timer
  113.   End Sub
  114.   'Ends the Counter
  115.   Public Sub StopTimer()
  116.     timElapsed = GetElapsedTime 'Store this Value
  117.     m_bRunning = False
  118.   End Sub
  119.   'Returns Elapsed time
  120.   Public Property Get Elapsed()
  121.     If m_bRunning Then
  122.       Elapsed = GetElapsedTime 'Return Intermediate Time
  123.     Else
  124.       Elapsed = timElapsed 'Return last counter's timing
  125.     End If
  126.   End Property
  127.   'Calculates the Elapsed Time
  128.   Private Function GetElapsedTime()
  129.     timEnd = Timer
  130.     GetElapsedTime = timEnd - timStart
  131.   End Function
  132. End Class
  133. '=====================================
  134. 'Output class
  135. '=====================================
  136. Class cOutput
  137.   Private m_FSO, m_sOutFile, m_fOutput, m_bRunning, _
  138.   m_sHeader, m_sFooter, m_bWritten
  139.   Private Sub Class_Initialize()
  140.     Set m_FSO = CreateObject ("Scripting.FileSystemObject")
  141.   End Sub
  142.   Private Sub OpenFile(iomode)
  143.     Close ()
  144.     Set m_fOutput = m_FSO.OpenTextFile(Me.File, iomode, True )
  145.   End Sub
  146.   'Close output file handle
  147.   Private Sub Close ()
  148.     If IsEmpty (m_sOutFile) And IsObject (m_fOutput) Then
  149.       m_fOutput.Close
  150.     End If
  151.     Set m_fOutput = Nothing
  152.   End Sub
  153.   'Apply header And footer
  154.   Private Sub WriteFile()
  155.     If m_sHeader <> "" Then
  156.       OpenFile 1 'Open For reading
  157.       sTemp = m_fOutput.ReadAll
  158.       Clear()
  159.       m_fOutput.Write m_FSO.OpenTextFile (m_sHeader).ReadAll
  160.       m_fOutput.WriteLine ""
  161.       m_fOutput.Write sTemp
  162.     End If
  163.     If m_sFooter <> "" Then
  164.       m_fOutput.Write m_FSO.OpenTextFile (m_sFooter).ReadAll
  165.     End If
  166.     m_bWritten = True
  167.   End Sub
  168.   'Close everything
  169.   Private Sub Class_Terminate()
  170.     If m_bWritten = False Then WriteFile()
  171.     Close ()
  172.     Set m_FSO = Nothing
  173.   End Sub
  174.   'Class Property 's
  175.   Public Property Let File(sFilePath)
  176.     m_sOutFile = sFilePath
  177.   End Property
  178.   Public Property Get File()
  179.     If IsEmpty (m_sOutFile) Then
  180.       m_sOutFile = m_FSO.GetParentFolderName (WScript.ScriptFullName ) & _
  181.       "\Testing.txt"
  182.     End If
  183.     File = m_sOutFile
  184.   End Property
  185.   'File To be append To top of output file
  186.   'i.e. head of html document
  187.   Public Property Let Header(sFilePath)
  188.     If m_FSO.FileExists (sFilePath) Then
  189.       m_sHeader = sFilePath
  190.     End If
  191.   End Property
  192.   Public Property Get Header()
  193.     Header = m_sHeader
  194.   End Property
  195.   'File To be append To bottom of output file
  196.   Public Property Let Footer(sFilePath)
  197.     If m_FSO.FileExists (sFilePath) Then
  198.       m_sFooter = sFilePath
  199.     End If
  200.   End Property
  201.   Public Property Get Footer()
  202.     Footer = m_sFooter
  203.   End Property
  204.   'Public functions
  205.   Public Sub WriteLine(sLine)
  206.     If IsObject (m_fOutput) = False Then
  207.       OpenFile 8 'Open For appending
  208.     End If
  209.     m_fOutput.WriteLine sLine
  210.     m_bWritten = False
  211.   End Sub
  212.   'Clear any previous contents of output file
  213.   Public Sub Clear()
  214.     OpenFile 2 'Open For writing
  215.   End Sub
  216.   'View output file
  217.   'Note: Header And footer will be duplicate If any
  218.   'WriteLine Is invoked after View
  219.   Public Sub View()
  220.     WriteFile()
  221.     Close ()
  222.     Dim Sh
  223.     Set Sh = WScript.CreateObject ("WScript.Shell")
  224.     Sh.Run """" & m_sOutFile & """"
  225.     Set Sh = Nothing
  226.   End Sub
  227.   Public Sub Delete()
  228.     Close ()
  229.     m_FSO.DeleteFile m_sOutFile
  230.   End Sub
  231. End Class

Download Custom Testing Class (vbs file)