Snippets

Sean Bamforth Tail Test - Console mode logging.

Created by Sean Bamforth last modified
External_Function WinAPI_GetCurrentProcessId "GetCurrentProcessId" Kernel32.Dll Returns Integer
External_Function WinAPI_EnumProcessModules "EnumProcessModules" Psapi.Dll Handle hProcess Address lphModule Integer cb Pointer lpcbNeeded Returns Integer
External_Function WinAPI_CloseHandle "CloseHandle" Kernel32.Dll Handle hObject Returns Integer
External_Function WinAPI_OpenProcess "OpenProcess" Kernel32.Dll DWord dwDesiredAccess Boolean bInheritHandle DWord dwProcessId Returns Handle

Define PROCESS_TERMINATE         for |CI$0001
Define PROCESS_CREATE_THREAD     for |CI$0002
Define PROCESS_SET_SESSIONID     for |CI$0004
Define PROCESS_VM_OPERATION      for |CI$0008
Define PROCESS_VM_READ           for |CI$0010
Define PROCESS_VM_WRITE          for |CI$0020
Define PROCESS_DUP_HANDLE        for |CI$0040
Define PROCESS_CREATE_PROCESS    for |CI$0080
Define PROCESS_SET_QUOTA         for |CI$0100
Define PROCESS_SET_INFORMATION   for |CI$0200
Define PROCESS_QUERY_INFORMATION for |CI$0400
Define PROCESS_ALL_ACCESS        for (STANDARD_RIGHTS_REQUIRED ior SYNCHRONIZE ior |CI$FFFF)

External_Function FindWindowEx "FindWindowExA" user32.dll ;
    Handle hWndParent ;
    Handle hWndChildAfter ;
    Pointer lpszClass ;
    Pointer lpszWindow ;
    Returns Handle

Function HandleFor Global String sSearch Returns Handle 
    Handle hwnd
    String sData 
    Integer iVoid 
    Move (lowercase(sSearch)) to sSearch 
    
    Move (FindWindowEx(0, 0, "", "")) to hwnd
    While (hwnd)
        ZeroString 100 to sData
        Move (GetWindowText(hwnd,(AddressOf(sData)),100)) to ivoid
        Move (Lowercase(Trim(cString(sData)))) to sData
        If (pos(sSearch,sData)) Begin 
            Function_Return hWnd 
        End

        Move (FindWindowEx(0, hWnd, "", "")) to hwnd
    Loop
    Function_Return 0 
End_Function


Global_Variable Integer ghlogfile 
Object oSimpleLog is a cObject
    Property String psPrivate.LogName 
    Property Boolean pisShow True 
    Property Boolean pisColored True 
    
    Property String psError         "/e[0;31m"
    Property String psMessage       "/e[1;33m"
    Property String psWarning       "/e[0;35m"
    Property String psOK            "/e[0;32m"
    Property String psWhite         "/e[1;37m"
    Property String psBlue          "/e[1;34m"

    Procedure Set logfile String sName 
        String sFolder 
        Get psHome of (phoWorkspace(oApplication)) to sFolder 
        If ((Right(sFolder,1))<>"\") Append sFolder "\"
        Append sFolder sName 
        Set  psPrivate.LogName to sFolder 
    End_Procedure
    
    Function logfile Returns String 
        Function_Return ( psPrivate.LogName (Self))
    End_Function
    
    Procedure BaseLog String sColor String sText 
        Integer iCH 
        DateTime dtNow
    
        Move (CurrentDateTime()) to dtNow
        Move ("["+(String(dtNow)) +"] "+  sText) to sText 
        
        If ((pisColored(Self)) and (sColor<>""))  Move (sColor+sText+(psWhite(Self))) to sText 
        Move (Replaces("/e",sText,Character(27))) to sText 

        Get Seq_New_Channel to iCH 
        Append_Output channel iCH (logfile(Self)) 
        Writeln channel iCH sText 
        Close_Output channel iCH 
        Send Seq_Release_Channel iCH 
    End_Procedure
    
    Procedure log String sText 
        Send BaseLog "" sText 
    End_Procedure
    
    Procedure log_error String sText 
        Send BaseLog (psError(Self)) sText 
    End_Procedure
    
    Procedure log_ok String sText 
        Send BaseLog (psOK(Self)) sText 
    End_Procedure
    
    Procedure log_note String sText 
        Send BaseLog (psBlue(Self)) sText 
    End_Procedure
    
    Procedure log_yellow String stext 
        Send BaseLog (psMessage(Self)) sText 
    End_Procedure
    
    Move (Self) to ghlogfile
End_Object

Global_Variable Integer ghTest
Object oTailTest is a cObject 
    
    Move (Self) to ghTest
    Property Integer piErrorCount 
    
    Procedure DisplayError String sMessage 
        Set piErrorCount to ( (piErrorCount (Self))+1)
        Send log_error to ghlogfile sMessage
    End_Procedure    
    
    Procedure DisplayOK String sMessage 
        Send log_OK to ghlogfile sMessage     
    End_Procedure    
    
    Procedure DisplayMessage String sMessage 
        Send log_yellow to ghlogfile sMessage     
    End_Procedure   
    
    Procedure StartTesting
        Set piErrorCount to 0 
    End_Procedure
     
    Procedure doAssertEqual String sTest String sValue String sMessage 
        Move (Trim(sTest)) to sTest 
        Move (Trim(sMessage)) to sMessage 
        If (sTest<>sValue) Send DisplayError (SFormat("FAIL: "+sMessage,sTest,sValue))
        If (sTest=sValue) Send DisplayOK (SFormat(sMessage,sTest,sValue))
    End_Procedure
    
    Procedure doAssertContains String sTest String sValue String sMessage 
        Integer iPos 
        String sCRLF 
        String sReport 
        
        Move ((Character(13))+(character(10))) to sCRLF
        Move (Replaces(sCRLF,sValue," ")) to sValue 

        Move (Lowercase(sTest)) to sTest 
        Move (Lowercase(sValue)) to sValue
        
        If ((Length(sValue))>30) Begin 
            Move (Pos(sTest,sValue)) to iPos             
            Move (iPos-10) to iPos 
            If (iPos<1) Move 1 to iPos
            Move (Mid(sValue,30,iPos)) to sreport 
            If (iPos>1) Move ( "..."+sReport) to sReport
            If ((iPos+30) < (Length(sValue)) ) Move (sReport+"...") to sReport
        End
        Else Begin 
            Move sValue to sReport 
        End

        If (Pos(sTest,sValue)) Send DisplayOK (SFormat(sMessage,sTest,sReport))
        Else Send DisplayError (SFormat("FAIL: "+sMessage,sTest,sReport))
    End_Procedure
    
    Procedure doAssertNotContains String sTest String sValue String sMessage 
        Integer iPos 
        String sCRLF 
        String sReport 
        
        Move ((Character(13))+(character(10))) to sCRLF
        Move (Replaces(sCRLF,sValue," ")) to sValue 

        Move (Lowercase(sTest)) to sTest 
        Move (Lowercase(sValue)) to sValue
        
        If ((Length(sValue))>30) Begin 
            Move (Pos(sTest,sValue)) to iPos             
            Move (iPos-10) to iPos 
            If (iPos<1) Move 1 to iPos
            Move (Mid(sValue,30,iPos)) to sreport 
            If (iPos>1) Move ( "..."+sReport) to sReport
            If ((iPos+30) < (Length(sValue)) ) Move (sReport+"...") to sReport
        End
        Else Begin 
            Move sValue to sReport 
        End

        If (Pos(sTest,sValue)) Send DisplayError (SFormat("FAIL: "+sMessage,sTest,sReport))
        Else Send DisplayOK (SFormat(sMessage,sTest,sReport))
    End_Procedure
    
    
    Procedure doAssertNotEqual String sTest String sValue String sMessage 
        Move (Trim(sTest)) to sTest 
        Move (Trim(sValue)) to sTest 
        Move (Trim(sMessage)) to sMessage 

        If (sTest=sValue) Send DisplayError (SFormat("NE-FAIL: "+sMessage,trim(sTest),trim(sValue)))
        If (sTest<>sValue) Send DisplayOK (SFormat(sMessage,trim(sTest),trim(sValue)))
    End_Procedure

    Procedure DisplayLogFile 
        String sLogFile 
        String sProgName
        String sRunProg
        String sProgramPath
        
        Get psHome of (phoWorkspace(ghoApplication)) to sProgramPath
        If ((Right(sProgramPath,1))<>"\") Append sProgramPath "\"
        Move (GetApplicationName(ghoApplication)) to sProgName 
        Move (sProgName+".log") to sLogFile
        Set logfile of ghLogFile to sLogFile 
        
        Send log_note to ghLogFile sLogFile
        Send log_note to ghLogFile "----------------------------"
        
        If ((HandleFor("tail.exe"))=0) Begin 
            Move ("tail.exe "+'"'+(logfile(ghLogfile))+'"'+" -f") to sRunProg
            Runprogram Background (sRunProg)  
        End
    End_Procedure
    Send DisplayLogFile
    
End_Object

Procedure AssertEqual Global String sTest String sValue String sMessage 
    Send doAssertEqual to ghTest sTest sValue sMessage 
End_Procedure

Procedure AssertTrue Global String sValue String sMessage 
    Send doAssertEqual to ghTest sValue (True) sMessage 
End_Procedure

Procedure AssertFalse Global String sValue String sMessage 
    Send doAssertEqual to ghTest sValue (False) sMessage 
End_Procedure

Procedure AssertContains Global String sTest String sValue String sMessage 
    Send doAssertContains to ghTest sTest sValue sMessage 
End_Procedure

Procedure AssertNotContains Global String sTest String sValue String sMessage 
    Send doAssertNotContains to ghTest sTest sValue sMessage 
End_Procedure

Procedure AssertNotEqual Global String sTest String sValue String sMessage 
    Send doAssertNotEqual to ghTest sTest sValue sMessage 
End_Procedure

Procedure TestMessage Global String sMessage 
    Send DisplayMessage to ghTest sMessage 
End_Procedure

Procedure LogErrorMessage Global String sMessage String s1 String s2 String s3 String s4 String s5
    String sParam 
    Integer iArg 
    For iArg from 2 to Num_Arguments
        MoveStr iArg& to sParam
        Move (Replaces("%"+String(iArg-1),sMessage,sParam)) to sMessage
    Loop
    Send log_error to ghLogFile sMessage 
End_Procedure

Procedure OKMessage Global String sMessage String s1 String s2 String s3 String s4 String s5 String s6 string s7 String s8
    String sParam 
    Integer iArg 
    For iArg from 2 to Num_Arguments
        MoveStr iArg& to sParam
        Move (Replaces("%"+String(iArg-1),sMessage,sParam)) to sMessage
    Loop
    Send log_ok to ghLogFile sMessage 
End_Procedure

Procedure NoteMessage Global String sMessage 
    Send log_note to ghLogFile sMessage 
End_Procedure


Comments (0)