diff options
author | sanine <sanine.not@pm.me> | 2022-10-01 20:59:36 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-10-01 20:59:36 -0500 |
commit | c5fc66ee58f2c60f2d226868bb1cf5b91badaf53 (patch) | |
tree | 277dd280daf10bf77013236b8edfa5f88708c7e0 /libs/ode-0.16.1/tests/UnitTest++/src/XmlTestReporter.cpp | |
parent | 1cf9cc3408af7008451f9133fb95af66a9697d15 (diff) |
add ode
Diffstat (limited to 'libs/ode-0.16.1/tests/UnitTest++/src/XmlTestReporter.cpp')
-rw-r--r-- | libs/ode-0.16.1/tests/UnitTest++/src/XmlTestReporter.cpp | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/libs/ode-0.16.1/tests/UnitTest++/src/XmlTestReporter.cpp b/libs/ode-0.16.1/tests/UnitTest++/src/XmlTestReporter.cpp new file mode 100644 index 0000000..0895e37 --- /dev/null +++ b/libs/ode-0.16.1/tests/UnitTest++/src/XmlTestReporter.cpp @@ -0,0 +1,126 @@ +#include "XmlTestReporter.h" + +#include <iostream> +#include <sstream> +#include <string> + +using std::string; +using std::ostringstream; +using std::ostream; + +namespace { + +void ReplaceChar(string& str, char const c, string const& replacement) +{ + for (size_t pos = str.find(c); pos != string::npos; pos = str.find(c, pos + 1)) + str.replace(pos, 1, replacement); +} + +string XmlEscape(string const& value) +{ + string escaped = value; + + ReplaceChar(escaped, '&', "&"); + ReplaceChar(escaped, '<', "<"); + ReplaceChar(escaped, '>', ">"); + ReplaceChar(escaped, '\'', "'"); + ReplaceChar(escaped, '\"', """); + + return escaped; +} + +string BuildFailureMessage(string const& file, int const line, string const& message) +{ + ostringstream failureMessage; + failureMessage << file << "(" << line << ") : " << message; + return failureMessage.str(); +} + +} + +namespace UnitTest { + +XmlTestReporter::XmlTestReporter(ostream& ostream) + : m_ostream(ostream) +{ +} + +void XmlTestReporter::ReportSummary(int const totalTestCount, int const failedTestCount, + int const failureCount, float const secondsElapsed) +{ + AddXmlElement(m_ostream, NULL); + + BeginResults(m_ostream, totalTestCount, failedTestCount, failureCount, secondsElapsed); + + DeferredTestResultList const& results = GetResults(); + for (DeferredTestResultList::const_iterator i = results.begin(); i != results.end(); ++i) + { + BeginTest(m_ostream, *i); + + if (i->failed) + AddFailure(m_ostream, *i); + + EndTest(m_ostream, *i); + } + + EndResults(m_ostream); +} + +void XmlTestReporter::AddXmlElement(ostream& os, char const* encoding) +{ + os << "<?xml version=\"1.0\""; + + if (encoding != NULL) + os << " encoding=\"" << encoding << "\""; + + os << "?>"; +} + +void XmlTestReporter::BeginResults(std::ostream& os, int const totalTestCount, int const failedTestCount, + int const failureCount, float const secondsElapsed) +{ + os << "<unittest-results" + << " tests=\"" << totalTestCount << "\"" + << " failedtests=\"" << failedTestCount << "\"" + << " failures=\"" << failureCount << "\"" + << " time=\"" << secondsElapsed << "\"" + << ">"; +} + +void XmlTestReporter::EndResults(std::ostream& os) +{ + os << "</unittest-results>"; +} + +void XmlTestReporter::BeginTest(std::ostream& os, DeferredTestResult const& result) +{ + os << "<test" + << " suite=\"" << result.suiteName << "\"" + << " name=\"" << result.testName << "\"" + << " time=\"" << result.timeElapsed << "\""; +} + +void XmlTestReporter::EndTest(std::ostream& os, DeferredTestResult const& result) +{ + if (result.failed) + os << "</test>"; + else + os << "/>"; +} + +void XmlTestReporter::AddFailure(std::ostream& os, DeferredTestResult const& result) +{ + os << ">"; // close <test> element + + for (DeferredTestResult::FailureVec::const_iterator it = result.failures.begin(); + it != result.failures.end(); + ++it) + { + string const escapedMessage = XmlEscape(it->second); + string const message = BuildFailureMessage(result.failureFile, it->first, escapedMessage); + + os << "<failure" << " message=\"" << message << "\"" << "/>"; + } +} + +} |