summaryrefslogtreecommitdiffstats
path: root/Source/CTest/cmParseBlanketJSCoverage.cxx
diff options
context:
space:
mode:
authorJoseph Snyder <joe.snyder@kitware.com>2014-10-23 21:03:30 (GMT)
committerBrad King <brad.king@kitware.com>2014-10-29 15:27:11 (GMT)
commit220e81345b68ed3ad15ef294f6de653fcd1a874c (patch)
tree759f57741cb0ba02e764515142284d6bf99ae8a1 /Source/CTest/cmParseBlanketJSCoverage.cxx
parenta2456e157223255f0e2a71f2ddd510510e42b9e4 (diff)
downloadCMake-220e81345b68ed3ad15ef294f6de653fcd1a874c.zip
CMake-220e81345b68ed3ad15ef294f6de653fcd1a874c.tar.gz
CMake-220e81345b68ed3ad15ef294f6de653fcd1a874c.tar.bz2
CTest: Add Javascript coverage parser
Add a coverage parser for the Blanket.js library using the JSON output of the mocha.js test runner. Add a test for the new parser.
Diffstat (limited to 'Source/CTest/cmParseBlanketJSCoverage.cxx')
-rw-r--r--Source/CTest/cmParseBlanketJSCoverage.cxx166
1 files changed, 166 insertions, 0 deletions
diff --git a/Source/CTest/cmParseBlanketJSCoverage.cxx b/Source/CTest/cmParseBlanketJSCoverage.cxx
new file mode 100644
index 0000000..5f4a708
--- /dev/null
+++ b/Source/CTest/cmParseBlanketJSCoverage.cxx
@@ -0,0 +1,166 @@
+/*============================================================================
+ 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.
+============================================================================*/
+#include "cmStandardIncludes.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include "cmSystemTools.h"
+#include "cmParseBlanketJSCoverage.h"
+#include <cmsys/Directory.hxx>
+#include <cmsys/Glob.hxx>
+#include <cmsys/FStream.hxx>
+
+
+class cmParseBlanketJSCoverage::JSONParser
+ {
+public:
+ typedef cmCTestCoverageHandlerContainer::
+ SingleFileCoverageVector FileLinesType;
+ JSONParser(cmCTestCoverageHandlerContainer& cont)
+ : Coverage(cont)
+ {
+ }
+
+ virtual ~JSONParser()
+ {
+ }
+
+ std::string getValue(std::string line, int type)
+ {
+ size_t begIndex;
+ size_t endIndex;
+ endIndex = line.rfind(',');
+ begIndex = line.find_first_of(':');
+ if(type == 0)
+ {
+ // A unique substring to remove the extra characters
+ // around the files name in the JSON (extra " and ,)
+ std::string foundFileName =
+ line.substr(begIndex+3,endIndex-(begIndex+4));
+ return foundFileName;
+ }
+ else
+ {
+ return line.substr(begIndex,line.npos);
+ }
+ }
+ bool ParseFile(std::string file)
+ {
+ FileLinesType localCoverageVector;
+ std::string filename;
+ bool foundFile = false;
+ bool inSource = false;
+ std::string covResult;
+ std::string line;
+
+ cmsys::ifstream in(file.c_str());
+ if(!in)
+ {
+ return false;
+ }
+ while( cmSystemTools::GetLineFromStream(in, line))
+ {
+ if(line.find("filename") != line.npos)
+ {
+ if(foundFile)
+ {
+ /*
+ * Upon finding a second file name, generate a
+ * vector within the total coverage to capture the
+ * information in the local vector
+ */
+ FileLinesType& CoverageVector =
+ this->Coverage.TotalCoverage[filename.c_str()];
+ CoverageVector = localCoverageVector;
+ localCoverageVector.clear();
+ foundFile=false;
+ }
+ foundFile= true;
+ inSource = false;
+ filename = getValue(line,0).c_str();
+ }
+ else if((line.find("coverage") != line.npos) && foundFile && inSource )
+ {
+ /*
+ * two types of "coverage" in the JSON structure
+ *
+ * The coverage result over the file or set of files
+ * and the coverage for each individual line
+ *
+ * FoundFile and foundSource ensure that
+ * only the value of the line coverage is captured
+ */
+ std::string result = getValue(line,1).c_str();
+ result = result.substr(2,result.npos);
+ if(result == "\"\"")
+ {
+ // Empty quotation marks indicate that the
+ // line is not executable
+ localCoverageVector.push_back(-1);
+ }
+ else
+ {
+ // Else, it contains the number of time executed
+ localCoverageVector.push_back(atoi(result.c_str()));
+ }
+ }
+ else if(line.find("source") != line.npos)
+ {
+ inSource=true;
+ }
+ }
+
+ // On exit, capture end of last file covered.
+ FileLinesType& CoverageVector =
+ this->Coverage.TotalCoverage[filename.c_str()];
+ CoverageVector = localCoverageVector;
+ foundFile=false;
+ localCoverageVector.clear();
+ return true;
+ }
+private:
+ cmCTestCoverageHandlerContainer& Coverage;
+};
+
+cmParseBlanketJSCoverage::cmParseBlanketJSCoverage(
+ cmCTestCoverageHandlerContainer& cont, cmCTest* ctest)
+ :Coverage(cont), CTest(ctest)
+ {
+ }
+
+bool cmParseBlanketJSCoverage::LoadCoverageData(std::vector<std::string> files)
+ {
+ size_t i=0;
+ std::string path;
+ cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
+ "Found " << files.size() <<" Files" << std::endl);
+ for(i=0;i<files.size();i++)
+ {
+ cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
+ "Reading JSON File " << files[i] << std::endl);
+
+ if(!this->ReadJSONFile(files[i]))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+bool cmParseBlanketJSCoverage::ReadJSONFile(std::string file)
+ {
+ cmParseBlanketJSCoverage::JSONParser parser
+ (this->Coverage);
+ cmCTestLog(this->CTest,HANDLER_VERBOSE_OUTPUT,
+ "Parsing " << file << std::endl);
+ parser.ParseFile(file);
+ return true;
+ }