[418] | 1 | #include "XmlTestReporter.h" |
---|
| 2 | #include "Config.h" |
---|
| 3 | |
---|
| 4 | #include <iostream> |
---|
| 5 | #include <sstream> |
---|
| 6 | #include <string> |
---|
| 7 | |
---|
| 8 | using std::string; |
---|
| 9 | using std::ostringstream; |
---|
| 10 | using std::ostream; |
---|
| 11 | |
---|
| 12 | namespace { |
---|
| 13 | |
---|
[477] | 14 | void ReplaceChar ( string& str, char c, string const& replacement ) { |
---|
| 15 | for ( size_t pos = str.find ( c ); pos != string::npos; pos = str.find ( c, pos + 1 ) ) |
---|
| 16 | str.replace ( pos, 1, replacement ); |
---|
[418] | 17 | } |
---|
| 18 | |
---|
[477] | 19 | string XmlEscape ( string const& value ) { |
---|
| 20 | string escaped = value; |
---|
[418] | 21 | |
---|
[477] | 22 | ReplaceChar ( escaped, '&', "&" ); |
---|
| 23 | ReplaceChar ( escaped, '<', "<" ); |
---|
| 24 | ReplaceChar ( escaped, '>', ">" ); |
---|
| 25 | ReplaceChar ( escaped, '\'', "'" ); |
---|
| 26 | ReplaceChar ( escaped, '\"', """ ); |
---|
| 27 | |
---|
| 28 | return escaped; |
---|
[418] | 29 | } |
---|
| 30 | |
---|
[477] | 31 | string BuildFailureMessage ( string const& file, int line, string const& message ) { |
---|
| 32 | ostringstream failureMessage; |
---|
| 33 | failureMessage << file << "(" << line << ") : " << message; |
---|
| 34 | return failureMessage.str(); |
---|
[418] | 35 | } |
---|
| 36 | |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | namespace UnitTest { |
---|
| 40 | |
---|
[477] | 41 | XmlTestReporter::XmlTestReporter ( ostream& ostream ) |
---|
| 42 | : m_ostream ( ostream ) { |
---|
[418] | 43 | } |
---|
| 44 | |
---|
[477] | 45 | void XmlTestReporter::ReportSummary ( int totalTestCount, int failedTestCount, |
---|
| 46 | int failureCount, float secondsElapsed ) { |
---|
| 47 | AddXmlElement ( m_ostream, NULL ); |
---|
[418] | 48 | |
---|
[477] | 49 | BeginResults ( m_ostream, totalTestCount, failedTestCount, failureCount, secondsElapsed ); |
---|
[418] | 50 | |
---|
[477] | 51 | DeferredTestResultList const& results = GetResults(); |
---|
| 52 | for ( DeferredTestResultList::const_iterator i = results.begin(); i != results.end(); ++i ) { |
---|
| 53 | BeginTest ( m_ostream, *i ); |
---|
[418] | 54 | |
---|
[477] | 55 | if ( i->failed ) |
---|
| 56 | AddFailure ( m_ostream, *i ); |
---|
[418] | 57 | |
---|
[477] | 58 | EndTest ( m_ostream, *i ); |
---|
| 59 | } |
---|
[418] | 60 | |
---|
[477] | 61 | EndResults ( m_ostream ); |
---|
[418] | 62 | } |
---|
| 63 | |
---|
[477] | 64 | void XmlTestReporter::AddXmlElement ( ostream& os, char const* encoding ) { |
---|
| 65 | os << "<?xml version=\"1.0\""; |
---|
[418] | 66 | |
---|
[477] | 67 | if ( encoding != NULL ) |
---|
| 68 | os << " encoding=\"" << encoding << "\""; |
---|
[418] | 69 | |
---|
[477] | 70 | os << "?>"; |
---|
[418] | 71 | } |
---|
| 72 | |
---|
[477] | 73 | void XmlTestReporter::BeginResults ( std::ostream& os, int totalTestCount, int failedTestCount, |
---|
| 74 | int failureCount, float secondsElapsed ) { |
---|
| 75 | os << "<unittest-results" |
---|
| 76 | << " tests=\"" << totalTestCount << "\"" |
---|
| 77 | << " failedtests=\"" << failedTestCount << "\"" |
---|
| 78 | << " failures=\"" << failureCount << "\"" |
---|
| 79 | << " time=\"" << secondsElapsed << "\"" |
---|
| 80 | << ">"; |
---|
[418] | 81 | } |
---|
| 82 | |
---|
[477] | 83 | void XmlTestReporter::EndResults ( std::ostream& os ) { |
---|
| 84 | os << "</unittest-results>"; |
---|
[418] | 85 | } |
---|
| 86 | |
---|
[477] | 87 | void XmlTestReporter::BeginTest ( std::ostream& os, DeferredTestResult const& result ) { |
---|
| 88 | os << "<test" |
---|
| 89 | << " suite=\"" << result.suiteName << "\"" |
---|
| 90 | << " name=\"" << result.testName << "\"" |
---|
| 91 | << " time=\"" << result.timeElapsed << "\""; |
---|
[418] | 92 | } |
---|
| 93 | |
---|
[477] | 94 | void XmlTestReporter::EndTest ( std::ostream& os, DeferredTestResult const& result ) { |
---|
| 95 | if ( result.failed ) |
---|
| 96 | os << "</test>"; |
---|
| 97 | else |
---|
| 98 | os << "/>"; |
---|
[418] | 99 | } |
---|
| 100 | |
---|
[477] | 101 | void XmlTestReporter::AddFailure ( std::ostream& os, DeferredTestResult const& result ) { |
---|
| 102 | os << ">"; // close <test> element |
---|
[418] | 103 | |
---|
[477] | 104 | for ( DeferredTestResult::FailureVec::const_iterator it = result.failures.begin(); |
---|
| 105 | it != result.failures.end(); |
---|
| 106 | ++it ) { |
---|
| 107 | string const escapedMessage = XmlEscape ( it->second ); |
---|
| 108 | string const message = BuildFailureMessage ( result.failureFile, it->first, escapedMessage ); |
---|
[418] | 109 | |
---|
[477] | 110 | os << "<failure" << " message=\"" << message << "\"" << "/>"; |
---|
| 111 | } |
---|
[418] | 112 | } |
---|
| 113 | |
---|
| 114 | } |
---|