1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
/*
* The MIT License
*
* Copyright (c) 2011 Bruno P. Kinoshita <http://www.kinoshita.eti.br>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Bruno P. Kinoshita <http://www.kinoshita.eti.br>
* @since 0.1
*/
#ifndef TAP_H_
#define TAP_H_
#include <list>
#include <iostream>
#include <map>
#include <fstream>
#include <string>
#include <algorithm>
namespace tap {
#ifdef GTEST_TAP_13_DIAGNOSTIC
// based on http://stackoverflow.com/a/7724536/831180
static std::string replace_all_copy(
std::string const& original,
std::string const& before,
std::string const& after
) {
using namespace std;
if (before == after) return string(original);
string retval;
if (before.length() == after.length()) retval.reserve(original.size());
basic_string <char>::const_iterator end = original.end();
basic_string <char>::const_iterator current = original.begin();
basic_string <char>::const_iterator next =
search(current, end, before.begin(), before.end());
while ( next != end ) {
retval.append( current, next );
retval.append( after );
current = next + before.size();
next = search(current, end, before.begin(), before.end());
}
retval.append( current, next );
return retval;
}
#endif
class TestResult {
private:
int number;
std::string status;
std::string name;
std::string comment;
bool skip;
public:
std::string getComment() const {
std::stringstream ss;
if (this->skip) {
ss << "# SKIP " << this->comment;
} else if (!this->comment.empty()) {
ss << "# " << this->comment;
}
return ss.str();
}
const std::string& getName() const {
return name;
}
int getNumber() const {
return number;
}
const std::string& getStatus() const {
return status;
}
bool getSkip() const {
return skip;
}
void setComment(const std::string& comment) {
this->comment = comment;
}
void setName(const std::string& name) {
this->name = name;
}
void setNumber(int number) {
this->number = number;
}
void setStatus(const std::string& status) {
this->status = status;
}
void setSkip(bool skip) {
this->skip = skip;
}
std::string toString() const {
std::stringstream ss;
ss << this->status << " " << this->number << " " << this->name;
#ifdef GTEST_TAP_13_DIAGNOSTIC
std::string comment_text = this->getComment();
if (!comment_text.empty()) {
ss << std::endl
<< "# Diagnostic" << std::endl
<< " ---" << std::endl
<< " " << replace_all_copy(this->getComment(), "\n", "\n ");
}
#endif
return ss.str();
}
};
class TestSet {
private:
std::list<TestResult> testResults;
public:
const std::list<TestResult>& getTestResults() const {
return testResults;
}
void addTestResult(TestResult& testResult) {
testResult.setNumber((this->getNumberOfTests() + 1));
this->testResults.push_back(testResult);
}
int getNumberOfTests() const {
return this->testResults.size();
}
std::string toString() const {
std::stringstream ss;
ss << "1.." << this->getNumberOfTests() << std::endl;
for (std::list<TestResult>::const_iterator ci = this->testResults.begin();
ci != this->testResults.end(); ++ci) {
TestResult testResult = *ci;
ss << testResult.toString() << std::endl;
}
return ss.str();
}
};
class TapListener: public ::testing::EmptyTestEventListener {
private:
std::map<std::string, tap::TestSet> testCaseTestResultMap;
void addTapTestResult(const testing::TestInfo& testInfo) {
tap::TestResult tapResult;
tapResult.setName(testInfo.name());
tapResult.setSkip(!testInfo.should_run());
const testing::TestResult *testResult = testInfo.result();
int number = testResult->total_part_count();
tapResult.setNumber(number-1);
if (testResult->HasFatalFailure()) {
tapResult.setStatus("Bail out!");
} else if (testResult->Failed()) {
tapResult.setStatus("not ok");
tapResult.setComment(testResult->GetTestPartResult(number-1).summary());
} else {
tapResult.setStatus("ok");
}
this->addNewOrUpdate(testInfo.test_case_name(), tapResult);
}
std::string getCommentOrDirective(const std::string& comment, bool skip) {
std::stringstream commentText;
if (skip) {
commentText << " # SKIP " << comment;
} else if (!comment.empty()) {
commentText << " # " << comment;
}
return commentText.str();
}
void addNewOrUpdate(const std::string& testCaseName, tap::TestResult testResult) {
std::map<std::string, tap::TestSet>::const_iterator ci =
this->testCaseTestResultMap.find(testCaseName);
if (ci != this->testCaseTestResultMap.end()) {
tap::TestSet testSet = ci->second;
testSet.addTestResult(testResult);
this->testCaseTestResultMap[testCaseName] = testSet;
} else {
tap::TestSet testSet;
testSet.addTestResult(testResult);
this->testCaseTestResultMap[testCaseName] = testSet;
}
}
public:
virtual void OnTestEnd(const testing::TestInfo& testInfo) {
//printf("%s %d - %s\n", testInfo.result()->Passed() ? "ok" : "not ok", this->testNumber, testInfo.name());
this->addTapTestResult(testInfo);
}
virtual void OnTestProgramEnd(const testing::UnitTest& unit_test) {
//--- Write the count and the word.
std::map<std::string, tap::TestSet>::const_iterator ci;
for (ci = this->testCaseTestResultMap.begin();
ci != this->testCaseTestResultMap.end(); ++ci) {
const tap::TestSet& testSet = ci->second;
#ifdef GTEST_TAP_PRINT_TO_STDOUT
std::cout << "TAP version 13" << std::endl;
std::cout << testSet.toString();
#else
std::string ext = ".tap";
std::ofstream tapFile;
tapFile.open((ci->first + ext).c_str());
tapFile << testSet.toString();
tapFile.close();
#endif
}
}
};
} // namespace tap
#endif // TAP_H_
|