From 5c31c3e4eb36cccaaf72d0f0582beed98f6665e0 Mon Sep 17 00:00:00 2001 From: Joseph Snyder Date: Mon, 29 Sep 2014 14:52:10 -0400 Subject: CTest: Add code coverage parser for Pascal/Delphi Add a class to parse the HTML output of the Delphi-code-coverage tool http://code.google.com/p/delphi-code-coverage/ Add a test for the new parser. --- Source/CMakeLists.txt | 1 + Source/CTest/cmCTestCoverageHandler.cxx | 36 +++ Source/CTest/cmCTestCoverageHandler.h | 3 + Source/CTest/cmParseDelphiCoverage.cxx | 253 +++++++++++++++++++++ Source/CTest/cmParseDelphiCoverage.h | 46 ++++ Tests/CMakeLists.txt | 17 ++ Tests/DelphiCoverage/DartConfiguration.tcl.in | 8 + Tests/DelphiCoverage/UTCovTest(UTCovTest.pas).html | 117 ++++++++++ Tests/DelphiCoverage/src/UTCovTest.pas | 75 ++++++ 9 files changed, 556 insertions(+) create mode 100644 Source/CTest/cmParseDelphiCoverage.cxx create mode 100644 Source/CTest/cmParseDelphiCoverage.h create mode 100644 Tests/DelphiCoverage/DartConfiguration.tcl.in create mode 100644 Tests/DelphiCoverage/UTCovTest(UTCovTest.pas).html create mode 100644 Tests/DelphiCoverage/src/UTCovTest.pas diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index c3f77f4..1472264 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -524,6 +524,7 @@ set(CTEST_SRCS cmCTest.cxx CTest/cmParseJacocoCoverage.cxx CTest/cmParsePHPCoverage.cxx CTest/cmParseCoberturaCoverage.cxx + CTest/cmParseDelphiCoverage.cxx CTest/cmCTestEmptyBinaryDirectoryCommand.cxx CTest/cmCTestGenericHandler.cxx CTest/cmCTestHandlerCommand.cxx diff --git a/Source/CTest/cmCTestCoverageHandler.cxx b/Source/CTest/cmCTestCoverageHandler.cxx index 76f6584..d921705 100644 --- a/Source/CTest/cmCTestCoverageHandler.cxx +++ b/Source/CTest/cmCTestCoverageHandler.cxx @@ -15,6 +15,7 @@ #include "cmParseGTMCoverage.h" #include "cmParseCacheCoverage.h" #include "cmParseJacocoCoverage.h" +#include "cmParseDelphiCoverage.h" #include "cmCTest.h" #include "cmake.h" #include "cmMakefile.h" @@ -423,6 +424,12 @@ int cmCTestCoverageHandler::ProcessHandler() return error; } + file_count += this->HandleDelphiCoverage(&cont); + error = cont.Error; + if ( file_count < 0 ) + { + return error; + } std::set uncovered = this->FindUncoveredFiles(&cont); if ( file_count == 0 ) @@ -910,7 +917,36 @@ int cmCTestCoverageHandler::HandleJacocoCoverage( return static_cast(cont->TotalCoverage.size()); } +//---------------------------------------------------------------------- +int cmCTestCoverageHandler::HandleDelphiCoverage( + cmCTestCoverageHandlerContainer* cont) +{ + cmParseDelphiCoverage cov = + cmParseDelphiCoverage(*cont, this->CTest); + cmsys::Glob g; + std::vector files; + g.SetRecurse(true); + + std::string BinDir + = this->CTest->GetBinaryDir(); + std::string coverageFile = BinDir+ "/*.html"; + g.FindFiles(coverageFile); + files=g.GetFiles(); + if (files.size() > 0) + { + cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, + "Found Delphi HTML Files, Performing Coverage" << std::endl); + cov.LoadCoverageData(files); + } + else + { + cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, + " Cannot find Delphi coverage files: " << coverageFile + << std::endl); + } + return static_cast(cont->TotalCoverage.size()); +} //---------------------------------------------------------------------- int cmCTestCoverageHandler::HandleGCovCoverage( cmCTestCoverageHandlerContainer* cont) diff --git a/Source/CTest/cmCTestCoverageHandler.h b/Source/CTest/cmCTestCoverageHandler.h index d0f274c..01c5d7f 100644 --- a/Source/CTest/cmCTestCoverageHandler.h +++ b/Source/CTest/cmCTestCoverageHandler.h @@ -84,6 +84,9 @@ private: //! Handle coverage for Jacoco int HandleJacocoCoverage(cmCTestCoverageHandlerContainer* cont); + //! Handle coverage for Delphi (Pascal) + int HandleDelphiCoverage(cmCTestCoverageHandlerContainer* cont); + //! Handle coverage using Bullseye int HandleBullseyeCoverage(cmCTestCoverageHandlerContainer* cont); int RunBullseyeSourceSummary(cmCTestCoverageHandlerContainer* cont); diff --git a/Source/CTest/cmParseDelphiCoverage.cxx b/Source/CTest/cmParseDelphiCoverage.cxx new file mode 100644 index 0000000..ad71c85 --- /dev/null +++ b/Source/CTest/cmParseDelphiCoverage.cxx @@ -0,0 +1,253 @@ +#include "cmStandardIncludes.h" +#include +#include +#include "cmSystemTools.h" +#include "cmXMLParser.h" +#include "cmParseDelphiCoverage.h" +#include +#include +#include + + +class cmParseDelphiCoverage::HTMLParser +{ +public: + typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector + FileLinesType; + HTMLParser(cmCTest* ctest, cmCTestCoverageHandlerContainer& cont) + : CTest(ctest), Coverage(cont) + { + } + + virtual ~HTMLParser() + { + } + + bool initializeDelphiFile(const std::string filename, + cmParseDelphiCoverage::HTMLParser::FileLinesType &coverageVector) + { + std::string line; + size_t comPos; + size_t semiPos; + bool blockComFlag= false; + bool lineComFlag= false; + std::vector beginSet; + cmsys::ifstream in(filename.c_str()); + if(!in) + { + return false; + } + while(cmSystemTools::GetLineFromStream(in, line)) + { + lineComFlag=false; + // Unique cases found in lines. + size_t beginPos = line.find("begin"); + + //Check that the begin is the first non-space string on the line + if( (beginPos == line.find_first_not_of(' ')) && beginPos != line.npos ) + { + beginSet.push_back("begin"); + coverageVector.push_back(-1); + continue; + } + else if(line.find('{') != line.npos) + { + blockComFlag=true; + } + else if(line.find('}') != line.npos) + { + blockComFlag=false; + coverageVector.push_back(-1); + continue; + } + else if((line.find("end;") != line.npos) + && (beginSet.size() > 0)) + { + beginSet.pop_back(); + coverageVector.push_back(-1); + continue; + } + + // This checks for comments after lines of code, finding the + // comment symbol after the ending semicolon. + comPos = line.find("//"); + if(comPos != line.npos) + { + semiPos= line.find(';'); + if(comPos < semiPos) + { + lineComFlag=true; + } + } + //Based up what was found, add a line to the coverageVector + if((beginSet.size() > 0) && line != "" && !blockComFlag + && !lineComFlag) + { + coverageVector.push_back(0); + } + else + { + coverageVector.push_back(-1); + } + } + return true; + } + bool ParseFile(const char* file) + { + std::string line=file; + std::string lineresult; + std::string lastroutine; + std::string filename; + std::string filelineoffset; + size_t afterLineNum = 0; + size_t lastoffset = 0; + size_t endcovpos = 0; + size_t endnamepos = 0; + size_t pos = 0; + + /* + * This first 'while' section goes through the found HTML + * file name and attempts to capture the source file name + * which is set as part of the HTML file name: the name of + * the file is found in parenthesis '()' + * + * See test HTML file name: UTCovTest(UTCovTest.pas).html. + * + * Find the text inside each pair of parenthesis and check + * to see if it ends in '.pas'. If it can't be found, + * exit the function. + */ + while(true) + { + lastoffset = line.find('(',pos); + if(lastoffset==line.npos) + { + cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT, + endnamepos << "File not found " << lastoffset << std::endl); + return false; + } + endnamepos = line.find(')',lastoffset); + filename = line.substr(lastoffset+1, + (endnamepos-1)-lastoffset); + if(filename.find(".pas") != filename.npos) + { + cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT, + "Coverage found for file: " << filename << std::endl); + break; + } + pos = lastoffset+1; + endnamepos = 0; + lastoffset =0; + } + /* + * Glob through the source directory for the + * file found above + */ + cmsys::Glob gl; + gl.RecurseOn(); + gl.RecurseThroughSymlinksOff(); + std::string glob = Coverage.SourceDir + "*/" + filename; + gl.FindFiles(glob); + std::vector const& files = gl.GetFiles(); + if(files.size() == 0) + { + /* + * If that doesn't find any matching files + * return a failure. + */ + cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT, + "Unable to find file matching" << glob << std::endl); + return false; + } + FileLinesType& coverageVector = + this->Coverage.TotalCoverage[files[0]]; + + /* + * Initialize the file to have all code between 'begin' and + * 'end' tags marked as executable + */ + + this->initializeDelphiFile(files[0],coverageVector); + + cmsys::ifstream in(file); + if(!in) + { + return false; + } + + /* + * Now read the HTML file, looking for the lines that have an + * "inline" in it. Then parse out the "class" value of that + * line to determine if the line is executed or not. + * + * Sample HTML line: + * + * 47
+    *     CheckEquals(1,2-1);
+ * + */ + + while( cmSystemTools::GetLineFromStream(in, line)) + { + if(line.find("inline") == line.npos) + { + continue; + } + + lastoffset = line.find("class="); + endcovpos = line.find(">",lastoffset); + lineresult = line.substr(lastoffset+7,(endcovpos-8)-lastoffset); + + if(lineresult == "covered") + { + afterLineNum = line.find('<',endcovpos+5); + filelineoffset= line.substr(endcovpos+5, + afterLineNum-(endcovpos+5)); + coverageVector[atoi(filelineoffset.c_str())-1] = 1; + } + } + return true; + } + + + private: + cmCTest* CTest; + cmCTestCoverageHandlerContainer& Coverage; +}; + +cmParseDelphiCoverage::cmParseDelphiCoverage( + cmCTestCoverageHandlerContainer& cont, cmCTest* ctest) + :Coverage(cont), CTest(ctest) + { + } + +bool cmParseDelphiCoverage::LoadCoverageData( + const std::vector files) + { + size_t i; + std::string path; + size_t numf = files.size(); + for (i = 0; i < numf; i++) + { + path = files[i]; + + cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT, + "Reading HTML File " << path << std::endl); + if(cmSystemTools::GetFilenameLastExtension(path) == ".html") + { + if(!this->ReadDelphiHTML(path.c_str())) + { + return false; + } + } + } + return true; + }; + +bool cmParseDelphiCoverage::ReadDelphiHTML(const char* file) + { + cmParseDelphiCoverage::HTMLParser + parser(this->CTest, this->Coverage); + parser.ParseFile(file); + return true; + }; diff --git a/Source/CTest/cmParseDelphiCoverage.h b/Source/CTest/cmParseDelphiCoverage.h new file mode 100644 index 0000000..018340b --- /dev/null +++ b/Source/CTest/cmParseDelphiCoverage.h @@ -0,0 +1,46 @@ +/*============================================================================ + CMake - Cross Platform Makefile Generator + Copyright 2000-2009 Kitware, Inc. + + Distributed under the OSI-approved BSD License (the "License"); + see accompanying file Copyright.txt for details. + + This software is distributed WITHOUT ANY WARRANTY; without even the + implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the License for more information. +============================================================================*/ + +#ifndef cmParseDelphiCoverage_h +#define cmParseDelphiCoverage_h + +#include "cmStandardIncludes.h" +#include "cmCTestCoverageHandler.h" + + +/** \class cmParseDelphiCoverage + * \brief Parse Delphi coverage information + * + * This class is used to parse Delphi(Pascal) coverage information + * generated by the Delphi-Code-Coverage tool + * + * https://code.google.com/p/delphi-code-coverage/ + */ + +class cmParseDelphiCoverage + { + public: + cmParseDelphiCoverage(cmCTestCoverageHandlerContainer& cont, + cmCTest* ctest); + bool LoadCoverageData(const std::vector files); + bool ReadDelphiHTML(const char* file); + // Read a single HTML file from output + bool ReadHTMLFile(const char* f); + + + protected: + + class HTMLParser; + cmCTestCoverageHandlerContainer& Coverage; + cmCTest* CTest; + }; +#endif diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 0b4aef7..ab56130 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -2310,6 +2310,23 @@ ${CMake_BINARY_DIR}/bin/cmake -DDIR=dev -P ${CMake_SOURCE_DIR}/Utilities/Release "Process file.*CoverageTest.java.*Total LOC:.*17.*Percentage Coverage: 76.47*" ENVIRONMENT COVFILE=) + # test coverage for Delphi-code-Coverage + configure_file( + "${CMake_SOURCE_DIR}/Tests/DelphiCoverage/DartConfiguration.tcl.in" + "${CMake_BINARY_DIR}/Testing/DelphiCoverage/DartConfiguration.tcl") + file(COPY "${CMake_SOURCE_DIR}/Tests/DelphiCoverage/src" + DESTINATION "${CMake_BINARY_DIR}/Testing/DelphiCoverage") + file(COPY "${CMake_SOURCE_DIR}/Tests/DelphiCoverage/UTCovTest(UTCovTest.pas).html" + DESTINATION "${CMake_BINARY_DIR}/Testing/DelphiCoverage") + add_test(NAME CTestDelphiCoverage + COMMAND cmake -E chdir + ${CMake_BINARY_DIR}/Testing/DelphiCoverage + $ -T Coverage --debug) + set_tests_properties(CTestDelphiCoverage PROPERTIES + PASS_REGULAR_EXPRESSION + "Process file.*UTCovTest.pas.*Total LOC:.*20.*Percentage Coverage: 95.*" + ENVIRONMENT COVFILE=) + function(add_config_tests cfg) set(base "${CMake_BINARY_DIR}/Tests/CTestConfig") diff --git a/Tests/DelphiCoverage/DartConfiguration.tcl.in b/Tests/DelphiCoverage/DartConfiguration.tcl.in new file mode 100644 index 0000000..4edcea6 --- /dev/null +++ b/Tests/DelphiCoverage/DartConfiguration.tcl.in @@ -0,0 +1,8 @@ +# This file is configured by CMake automatically as DartConfiguration.tcl +# If you choose not to use CMake, this file may be hand configured, by +# filling in the required variables. + + +# Configuration directories and files +SourceDirectory: ${CMake_BINARY_DIR}/Testing/DelphiCoverage +BuildDirectory: ${CMake_BINARY_DIR}/Testing/DelphiCoverage diff --git a/Tests/DelphiCoverage/UTCovTest(UTCovTest.pas).html b/Tests/DelphiCoverage/UTCovTest(UTCovTest.pas).html new file mode 100644 index 0000000..9caaea3 --- /dev/null +++ b/Tests/DelphiCoverage/UTCovTest(UTCovTest.pas).html @@ -0,0 +1,117 @@ + + + + + Delphi CodeCoverage Coverage Report + + + +

Coverage report for UTCovTest (C:\Users\joe.snyder\Work\OSEHRA\VistA\Packages\Order Entry Results Reporting\CPRS\Testing\Tests\UTCovTest.pas).

+

Generated at 10/3/2014 12:24:11 PM by DelphiCodeCoverage - an open source tool for Delphi Code Coverage.

+

Statistics for C:\Users\joe.snyder\Work\OSEHRA\VistA\Packages\Order Entry Results Reporting\CPRS\Testing\Tests\UTCovTest.pas

+
Number of lines covered19
Number of lines with code gen19
Line coverage100%
+

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
1
//---------------------------------------------------------------------------
2
// Copyright 2012 The Open Source Electronic Health Record Agent
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
//     http://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
//---------------------------------------------------------------------------
16
unit UTCovTest;
17
interface
18
uses UnitTest, TestFrameWork,SysUtils,Windows;
19
20
implementation
21
type
22
UTCovTestTests=class(TTestCase)
23
  public
24
  procedure SetUp; override;
25
  procedure TearDown; override;
26
27
  published
28
    procedure TestCov1;
29
    procedure TestCov2;
30
    procedure TestCov3;
31
  end;
32
33
procedure NotRun;
34
begin
35
    WriteLn('This line will never run');
36
end;
37
procedure UTCovTestTests.SetUp;
38
begin
39
end;
40
41
procedure UTCovTestTests.TearDown;
42
begin
43
end;
44
45
procedure UTCovTestTests.TestCov1;
46
begin
47
  {
48
  Block comment lines
49
  }
50
  CheckEquals(1,2-1);
51
end;
52
53
procedure UTCovTestTests.TestCov2;
54
var
55
  i:DWORD;
56
begin
57
  for i := 0 to 1 do
58
    WriteLn( IntToStr(i));
59
  // Comment
60
  CheckEquals(i,2);
61
end;
62
63
procedure UTCovTestTests.TestCov3;
64
var
65
 i : DWORD;
66
begin
67
  i := 0;
68
  while i < 5 do
69
   i := i+1;
70
  CheckEquals(i,5);
71
end;
72
73
begin
74
  UnitTest.addSuite(UTCovTestTests.Suite);
75
end.
+ + diff --git a/Tests/DelphiCoverage/src/UTCovTest.pas b/Tests/DelphiCoverage/src/UTCovTest.pas new file mode 100644 index 0000000..66db3c0 --- /dev/null +++ b/Tests/DelphiCoverage/src/UTCovTest.pas @@ -0,0 +1,75 @@ +//--------------------------------------------------------------------------- +// Copyright 2012 The Open Source Electronic Health Record Agent +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//--------------------------------------------------------------------------- +unit UTCovTest; +interface +uses UnitTest, TestFrameWork,SysUtils,Windows; + +implementation +type +UTCovTestTests=class(TTestCase) + public + procedure SetUp; override; + procedure TearDown; override; + + published + procedure TestCov1; + procedure TestCov2; + procedure TestCov3; + end; + +procedure NotRun; +begin + WriteLn('This line will never run'); +end; +procedure UTCovTestTests.SetUp; +begin +end; + +procedure UTCovTestTests.TearDown; +begin +end; + +procedure UTCovTestTests.TestCov1; +begin + { + Block comment lines + } + CheckEquals(1,2-1); +end; + +procedure UTCovTestTests.TestCov2; +var + i:DWORD; +begin + for i := 0 to 1 do + WriteLn( IntToStr(i)); + // Comment + CheckEquals(i,2); +end; + +procedure UTCovTestTests.TestCov3; +var + i : DWORD; +begin + i := 0; + while i < 5 do + i := i+1; + CheckEquals(i,5); +end; + +begin + UnitTest.addSuite(UTCovTestTests.Suite); +end. \ No newline at end of file -- cgit v0.12