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
|
#include <cppunit/Exception.h>
#include <cppunit/Test.h>
#include <cppunit/TestFailure.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/XmlOutputter.h>
#include <cppunit/XmlOutputterHook.h>
#include <cppunit/tools/XmlDocument.h>
#include <cppunit/tools/XmlElement.h>
#include <stdlib.h>
#include <algorithm>
CPPUNIT_NS_BEGIN
XmlOutputter::XmlOutputter( TestResultCollector *result,
OStream &stream,
std::string encoding )
: m_result( result )
, m_stream( stream )
, m_xml( new XmlDocument( encoding ) )
{
}
XmlOutputter::~XmlOutputter()
{
delete m_xml;
}
void
XmlOutputter::addHook( XmlOutputterHook *hook )
{
m_hooks.push_back( hook );
}
void
XmlOutputter::removeHook( XmlOutputterHook *hook )
{
m_hooks.erase( std::find( m_hooks.begin(), m_hooks.end(), hook ) );
}
void
XmlOutputter::write()
{
setRootNode();
m_stream << m_xml->toString();
}
void
XmlOutputter::setStyleSheet( const std::string &styleSheet )
{
m_xml->setStyleSheet( styleSheet );
}
void
XmlOutputter::setStandalone( bool standalone )
{
m_xml->setStandalone( standalone );
}
void
XmlOutputter::setRootNode()
{
XmlElement *rootNode = new XmlElement( "TestRun" );
m_xml->setRootElement( rootNode );
for ( Hooks::iterator it = m_hooks.begin(); it != m_hooks.end(); ++it )
(*it)->beginDocument( m_xml );
FailedTests failedTests;
fillFailedTestsMap( failedTests );
addFailedTests( failedTests, rootNode );
addSuccessfulTests( failedTests, rootNode );
addStatistics( rootNode );
for ( Hooks::iterator itEnd = m_hooks.begin(); itEnd != m_hooks.end(); ++itEnd )
(*itEnd)->endDocument( m_xml );
}
void
XmlOutputter::fillFailedTestsMap( FailedTests &failedTests )
{
const TestResultCollector::TestFailures &failures = m_result->failures();
TestResultCollector::TestFailures::const_iterator itFailure = failures.begin();
while ( itFailure != failures.end() )
{
TestFailure *failure = *itFailure++;
failedTests.insert( std::pair<Test* const, TestFailure*>(failure->failedTest(), failure ) );
}
}
void
XmlOutputter::addFailedTests( FailedTests &failedTests,
XmlElement *rootNode )
{
XmlElement *testsNode = new XmlElement( "FailedTests" );
rootNode->addElement( testsNode );
const TestResultCollector::Tests &tests = m_result->tests();
for ( unsigned int testNumber = 0; testNumber < tests.size(); ++testNumber )
{
Test *test = tests[testNumber];
if ( failedTests.find( test ) != failedTests.end() )
addFailedTest( test, failedTests[test], testNumber+1, testsNode );
}
}
void
XmlOutputter::addSuccessfulTests( FailedTests &failedTests,
XmlElement *rootNode )
{
XmlElement *testsNode = new XmlElement( "SuccessfulTests" );
rootNode->addElement( testsNode );
const TestResultCollector::Tests &tests = m_result->tests();
for ( unsigned int testNumber = 0; testNumber < tests.size(); ++testNumber )
{
Test *test = tests[testNumber];
if ( failedTests.find( test ) == failedTests.end() )
addSuccessfulTest( test, testNumber+1, testsNode );
}
}
void
XmlOutputter::addStatistics( XmlElement *rootNode )
{
XmlElement *statisticsElement = new XmlElement( "Statistics" );
rootNode->addElement( statisticsElement );
statisticsElement->addElement( new XmlElement( "Tests", m_result->runTests() ) );
statisticsElement->addElement( new XmlElement( "FailuresTotal",
m_result->testFailuresTotal() ) );
statisticsElement->addElement( new XmlElement( "Errors", m_result->testErrors() ) );
statisticsElement->addElement( new XmlElement( "Failures", m_result->testFailures() ) );
for ( Hooks::iterator it = m_hooks.begin(); it != m_hooks.end(); ++it )
(*it)->statisticsAdded( m_xml, statisticsElement );
}
void
XmlOutputter::addFailedTest( Test *test,
TestFailure *failure,
int testNumber,
XmlElement *testsNode )
{
Exception *thrownException = failure->thrownException();
XmlElement *testElement = new XmlElement( "FailedTest" );
testsNode->addElement( testElement );
testElement->addAttribute( "id", testNumber );
testElement->addElement( new XmlElement( "Name", test->getName() ) );
testElement->addElement( new XmlElement( "FailureType",
failure->isError() ? "Error" :
"Assertion" ) );
if ( failure->sourceLine().isValid() )
addFailureLocation( failure, testElement );
testElement->addElement( new XmlElement( "Message", thrownException->what() ) );
for ( Hooks::iterator it = m_hooks.begin(); it != m_hooks.end(); ++it )
(*it)->failTestAdded( m_xml, testElement, test, failure );
}
void
XmlOutputter::addFailureLocation( TestFailure *failure,
XmlElement *testElement )
{
XmlElement *locationNode = new XmlElement( "Location" );
testElement->addElement( locationNode );
SourceLine sourceLine = failure->sourceLine();
locationNode->addElement( new XmlElement( "File", sourceLine.fileName() ) );
locationNode->addElement( new XmlElement( "Line", sourceLine.lineNumber() ) );
}
void
XmlOutputter::addSuccessfulTest( Test *test,
int testNumber,
XmlElement *testsNode )
{
XmlElement *testElement = new XmlElement( "Test" );
testsNode->addElement( testElement );
testElement->addAttribute( "id", testNumber );
testElement->addElement( new XmlElement( "Name", test->getName() ) );
for ( Hooks::iterator it = m_hooks.begin(); it != m_hooks.end(); ++it )
(*it)->successfulTestAdded( m_xml, testElement, test );
}
CPPUNIT_NS_END
|