Visual C++ Macro for easyly adding test

Blood Chen
2001-07-06
2001-07-10
  • Blood Chen

    Blood Chen - 2001-07-06

    I have made 2 Macro to add a test Class and test functions easyly. You just specify class name and the macro create test framework for you. I find it very useful to add test.And I would like to contribute it to everyone, who is using CppUnit.
    Can I put it in the project? How?

     
    • Baptiste Lepilleur

      Posting to the list. This is were CppUnit development discussions take place.

      Baptiste.

       
    • Blood Chen

      Blood Chen - 2001-07-09

      'Made by bloodchen
      'bloodchen@hotmail.com
      Sub NewTestClass
          On Error Resume Next
          dim proj_path,ext,pos,proj_dir,MyCppFile,MyCppName,MyHFile,MyHName,ClassName,HText,CPPText
          proj_path = ActiveProject.fullname
          ext = ""
          pos = len (proj_path)
          Do While ext <> "\&quot;
          ext = Mid(proj_path, pos, 1)
          pos = pos -1
          Loop
          proj_dir = left(proj_path, pos+1)
          ClassName=InputBox("Enter the class name:", "Class Name")
          if ActiveProject.Type <> "Build" then
              MsgBox "This project is not valid. Ending macro."
              Exit Sub
          end if
          if (len(ClassName) <= 0) then
            MsgBox "Invalid class name. Ending macro."
            Exit Sub
          end if

      '    ClassName="CTest"
          MyCppName=proj_dir+ClassName+".cpp"
          MyHName=proj_dir+ClassName+".h"
          ActiveProject.AddFile MyCppName
          ActiveProject.AddFile MyHName
          Documents.Add "Text"
          ActiveDocument.Selection.StartOfDocument

          HText=    "#ifndef "+ClassName+"DEF"+VbCrLf& _
                  "#define "+ClassName+"DEF"+VbCrLf& _
                  ""+VbCrLf& _
                  "#include <cppunit\testcase.h>"+VbCrLf& _
                  "#include <cppunit\extensions\HelperMacros.h>"+VbCrLf& _
                  "class "+ClassName+":public CppUnit::TestCase"+VbCrLf& _
                  "{"+VbCrLf& _
                  "        CPPUNIT_TEST_SUITE( "+ClassName+"  );"+VbCrLf& _
                  "        CPPUNIT_TEST_SUITE_END();"+VbCrLf& _
                  "public:"+VbCrLf& _
                  "        "+ClassName+"();"+VbCrLf& _
                  "        virtual ~"+ClassName+"();"+VbCrLf& _
                  "};"+VbCrLf& _
                  "#endif"+VbCrLf
          ActiveDocument.Selection = HText
          ActiveDocument.Save MyHName
      '    WriteFile MyHName,HText
          Documents.Add "Text"
          ActiveDocument.Selection.StartOfDocument
          CPPText =    "#include "+chr(34)+"stdafx.h"+chr(34)+VbCrLf& _
                      "#include "+chr(34)+ClassName+".h"+chr(34)+VbCrLf& _
                      ""+VbCrLf& _
                      ""+VbCrLf& _
                      "CPPUNIT_TEST_SUITE_REGISTRATION( "+ClassName+ "  );"+VbCrLf& _
                      ""+VbCrLf& _
                      ""+VbCrLf& _
                      ClassName+"::"+ClassName+"()"+VbCrLf& _
                      "{"+VbCrLf& _
                      "}"+VbCrLf& _
                      ""+VbCrLf& _
                      ""+VbCrLf& _
                      ClassName+"::~"+ClassName+"()"+VbCrLf& _
                      "{"+VbCrLf& _
                      "}"
      '    WriteFile MyCppName,CPPText
          ActiveDocument.Selection = CPPText
          ActiveDocument.Save MyCppName
             
      End Sub

      Sub ToggleHandCPP()
          'DESCRIPTION: Opens the .cpp or .h file for the current document.
          'Toggles between the .cpp & .h file
          ext = ActiveDocument.FullName
          If ext = "" Then
              msgbox ("Error, not a .cpp or .h file")
              exit sub
          End If
          DocName = UCase(ext)

          If Right(DocName,4) = ".CPP" Then
              fn = left(DocName, Len(DocName)-3) & "h"
          ElseIf Right(DocName,2) = ".H" Then
              fn = Left(DocName, Len(DocName)-1) & "cpp"
          Else
              msgbox ("Error, not a .cpp or a .h file")
              exit sub
          End If
          'msgbox (fn)
          on error resume next
          Documents.Open (fn)
      End Sub

      Sub ADDTestMethod()
          strHpt = ActiveDocument.FullName
          if right(strHpt,3) = "CPP" Or right (strHpt,3) = "cpp" Then
              ActiveDocument.Selection.SelectLine
              strText = ActiveDocument.Selection.Text
              if (Instr(strText, "::" ) = 0) Then
                  MsgBox("Line not valid !!")
                  Exit Sub
              End If
              else exit sub
          end if

              pos = Instr(strText, "::")
              strName = Right(strText, (Len(strText) - (pos+1)))
              pos = Instr(strName,"(")
              strName = Left(strName,pos-1)
              strClass = Left(strText,pos - 1)
              while (instr(strClass, " ") > 0)
                  pos = instr(strClass, " ")
                  strTyp = strTyp & Left(strClass, pos)
                  strClass = Right(strClass, Len(strClass) - (pos) )
              wend
          ToggleHandCPP

          ActiveDocument.Selection.SelectAll
          strHead = ActiveDocument.Selection.Text

          if (instr(strHead,strClass)  = 0) Then
                  MsgBox(" Can't find class " & strClass & " !!")
                  ToggleHandCPP
                  Exit Sub
              End If
          ActiveDocument.Selection.EndOfDocument
          lineBottom = ActiveDocument.Selection.CurrentLine

          ActiveDocument.Selection.StartOfDocument
          ActiveDocument.Selection.StartOfLine
          ActiveDocument.Selection.SelectLine
          strLine = ActiveDocument.Selection.Text
          while (instr(strLine, strName) = 0  And ActiveDocument.Selection.CurrentLine <> lineBottom)
                  ActiveDocument.Selection.StartOfLine
                  ActiveDocument.Selection.LineDown dsMove
                  ActiveDocument.Selection.SelectLine
                  strLine = ActiveDocument.Selection.Text
          Wend
          if (ActiveDocument.Selection.CurrentLine < lineBottom) Then
                 if( instr(strLine, "CPPUNIT_TEST" ) <> 0 )Then
                 ToggleHandCPP
                 Exit Sub
                 end if
          End If

          ActiveDocument.Selection.StartOfDocument
          ActiveDocument.Selection.StartOfLine
          ActiveDocument.Selection.SelectLine
          strLine = ActiveDocument.Selection.Text
          while (instr(strLine, "    CPPUNIT_TEST_SUITE_END();" ) = 0  And ActiveDocument.Selection.CurrentLine <> lineBottom)
                  ActiveDocument.Selection.StartOfLine
                  ActiveDocument.Selection.LineDown dsMove
                  ActiveDocument.Selection.SelectLine
                  strLine = ActiveDocument.Selection.Text
          Wend
          if (ActiveDocument.Selection.CurrentLine < lineBottom) Then
              ActiveDocument.Selection.EndOfLine
              ActiveDocument.Selection.LineUp
              ActiveDocument.Selection.EndOfLine
              ActiveDocument.Selection.NewLine
              ActiveDocument.Selection = "CPPUNIT_TEST( "&strName&" );"
          else
              MsgBox("CPPUNIT_TEST_SUITE_END not found")
          end if
          ToggleHandCPP

      End Sub

       
      • Baptiste Lepilleur

        Could you post this as an attachement to the mailing list (not the forum), with a short doc indicating how to use it.

        Thanks,
        Baptiste

         

Log in to post a comment.