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 | |
---|
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 ); |
---|
17 | } |
---|
18 | |
---|
19 | string XmlEscape ( string const& value ) { |
---|
20 | string escaped = value; |
---|
21 | |
---|
22 | ReplaceChar ( escaped, '&', "&" ); |
---|
23 | ReplaceChar ( escaped, '<', "<" ); |
---|
24 | ReplaceChar ( escaped, '>', ">" ); |
---|
25 | ReplaceChar ( escaped, '\'', "'" ); |
---|
26 | ReplaceChar ( escaped, '\"', """ ); |
---|
27 | |
---|
28 | return escaped; |
---|
29 | } |
---|
30 | |
---|
31 | string BuildFailureMessage ( string const& file, int line, string const& message ) { |
---|
32 | ostringstream failureMessage; |
---|
33 | failureMessage << file << "(" << line << ") : " << message; |
---|
34 | return failureMessage.str(); |
---|
35 | } |
---|
36 | |
---|
37 | } |
---|
38 | |
---|
39 | namespace UnitTest { |
---|
40 | |
---|
41 | XmlTestReporter::XmlTestReporter ( ostream& ostream ) |
---|
42 | : m_ostream ( ostream ) { |
---|
43 | } |
---|
44 | |
---|
45 | void XmlTestReporter::ReportSummary ( int totalTestCount, int failedTestCount, |
---|
46 | int failureCount, float secondsElapsed ) { |
---|
47 | AddXmlElement ( m_ostream, NULL ); |
---|
48 | |
---|
49 | BeginResults ( m_ostream, totalTestCount, failedTestCount, failureCount, secondsElapsed ); |
---|
50 | |
---|
51 | DeferredTestResultList const& results = GetResults(); |
---|
52 | for ( DeferredTestResultList::const_iterator i = results.begin(); i != results.end(); ++i ) { |
---|
53 | BeginTest ( m_ostream, *i ); |
---|
54 | |
---|
55 | if ( i->failed ) |
---|
56 | AddFailure ( m_ostream, *i ); |
---|
57 | |
---|
58 | EndTest ( m_ostream, *i ); |
---|
59 | } |
---|
60 | |
---|
61 | EndResults ( m_ostream ); |
---|
62 | } |
---|
63 | |
---|
64 | void XmlTestReporter::AddXmlElement ( ostream& os, char const* encoding ) { |
---|
65 | os << "<?xml version=\"1.0\""; |
---|
66 | |
---|
67 | if ( encoding != NULL ) |
---|
68 | os << " encoding=\"" << encoding << "\""; |
---|
69 | |
---|
70 | os << "?>"; |
---|
71 | } |
---|
72 | |
---|
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 | << ">"; |
---|
81 | } |
---|
82 | |
---|
83 | void XmlTestReporter::EndResults ( std::ostream& os ) { |
---|
84 | os << "</unittest-results>"; |
---|
85 | } |
---|
86 | |
---|
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 << "\""; |
---|
92 | } |
---|
93 | |
---|
94 | void XmlTestReporter::EndTest ( std::ostream& os, DeferredTestResult const& result ) { |
---|
95 | if ( result.failed ) |
---|
96 | os << "</test>"; |
---|
97 | else |
---|
98 | os << "/>"; |
---|
99 | } |
---|
100 | |
---|
101 | void XmlTestReporter::AddFailure ( std::ostream& os, DeferredTestResult const& result ) { |
---|
102 | os << ">"; // close <test> element |
---|
103 | |
---|
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 ); |
---|
109 | |
---|
110 | os << "<failure" << " message=\"" << message << "\"" << "/>"; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | } |
---|