Mercurial > fife-parpg
comparison ext/UnitTest++/src/XmlTestReporter.cpp @ 89:fa33cda75471
* Reverting back to 2543 as requested by sleek
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sat, 19 Jul 2008 11:38:52 +0000 |
parents | 0d325e9d5953 |
children |
comparison
equal
deleted
inserted
replaced
88:1c2842ebe393 | 89:fa33cda75471 |
---|---|
1 #include "XmlTestReporter.h" | |
2 | |
3 #include <iostream> | |
4 #include <sstream> | |
5 #include <string> | |
6 | |
7 using std::string; | |
8 using std::ostringstream; | |
9 using std::ostream; | |
10 | |
11 namespace { | |
12 | |
13 void ReplaceChar(string& str, char const c, string const& replacement) | |
14 { | |
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 { | |
21 string escaped = value; | |
22 | |
23 ReplaceChar(escaped, '&', "&"); | |
24 ReplaceChar(escaped, '<', "<"); | |
25 ReplaceChar(escaped, '>', ">"); | |
26 ReplaceChar(escaped, '\'', "'"); | |
27 ReplaceChar(escaped, '\"', """); | |
28 | |
29 return escaped; | |
30 } | |
31 | |
32 string BuildFailureMessage(string const& file, int const line, string const& message) | |
33 { | |
34 ostringstream failureMessage; | |
35 failureMessage << file << "(" << line << ") : " << message; | |
36 return failureMessage.str(); | |
37 } | |
38 | |
39 } | |
40 | |
41 namespace UnitTest { | |
42 | |
43 XmlTestReporter::XmlTestReporter(ostream& ostream) | |
44 : m_ostream(ostream) | |
45 { | |
46 } | |
47 | |
48 void XmlTestReporter::ReportSummary(int const totalTestCount, int const failedTestCount, | |
49 int const failureCount, float const secondsElapsed) | |
50 { | |
51 AddXmlElement(m_ostream, NULL); | |
52 | |
53 BeginResults(m_ostream, totalTestCount, failedTestCount, failureCount, secondsElapsed); | |
54 | |
55 DeferredTestResultList const& results = GetResults(); | |
56 for (DeferredTestResultList::const_iterator i = results.begin(); i != results.end(); ++i) | |
57 { | |
58 BeginTest(m_ostream, *i); | |
59 | |
60 if (i->failed) | |
61 AddFailure(m_ostream, *i); | |
62 | |
63 EndTest(m_ostream, *i); | |
64 } | |
65 | |
66 EndResults(m_ostream); | |
67 } | |
68 | |
69 void XmlTestReporter::AddXmlElement(ostream& os, char const* encoding) | |
70 { | |
71 os << "<?xml version=\"1.0\""; | |
72 | |
73 if (encoding != NULL) | |
74 os << " encoding=\"" << encoding << "\""; | |
75 | |
76 os << "?>"; | |
77 } | |
78 | |
79 void XmlTestReporter::BeginResults(std::ostream& os, int const totalTestCount, int const failedTestCount, | |
80 int const failureCount, float const secondsElapsed) | |
81 { | |
82 os << "<unittest-results" | |
83 << " tests=\"" << totalTestCount << "\"" | |
84 << " failedtests=\"" << failedTestCount << "\"" | |
85 << " failures=\"" << failureCount << "\"" | |
86 << " time=\"" << secondsElapsed << "\"" | |
87 << ">"; | |
88 } | |
89 | |
90 void XmlTestReporter::EndResults(std::ostream& os) | |
91 { | |
92 os << "</unittest-results>"; | |
93 } | |
94 | |
95 void XmlTestReporter::BeginTest(std::ostream& os, DeferredTestResult const& result) | |
96 { | |
97 os << "<test" | |
98 << " suite=\"" << result.suiteName << "\"" | |
99 << " name=\"" << result.testName << "\"" | |
100 << " time=\"" << result.timeElapsed << "\""; | |
101 } | |
102 | |
103 void XmlTestReporter::EndTest(std::ostream& os, DeferredTestResult const& result) | |
104 { | |
105 if (result.failed) | |
106 os << "</test>"; | |
107 else | |
108 os << "/>"; | |
109 } | |
110 | |
111 void XmlTestReporter::AddFailure(std::ostream& os, DeferredTestResult const& result) | |
112 { | |
113 os << ">"; // close <test> element | |
114 | |
115 for (DeferredTestResult::FailureVec::const_iterator it = result.failures.begin(); | |
116 it != result.failures.end(); | |
117 ++it) | |
118 { | |
119 string const escapedMessage = XmlEscape(it->second); | |
120 string const message = BuildFailureMessage(result.failureFile, it->first, escapedMessage); | |
121 | |
122 os << "<failure" << " message=\"" << message << "\"" << "/>"; | |
123 } | |
124 } | |
125 | |
126 } |