blob: 1c75b49150a86dd8b3bbb31dbdbb98398edc6be7 (
plain)
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
|
#include <cppunit/NotEqualException.h>
#include <cppunit/SourceLine.h>
#include <cppunit/TestResult.h>
#include <cppunit/CompilerTestResultOutputter.h>
namespace CppUnit
{
/** Print TestResult in a compiler compatible format.
*
* Heres is an example of usage:
* \code
* int main( int argc, char* argv[] ) {
* bool selfTest = (argc > 1) &&
* (std::string("-selftest") == argv[1]);
*
* CppUnit::TextTestRunner runner;
* runner.addTest( CppUnitTest::suite() );
*
* bool wasSucessful = runner.run( "", false, !selfTest );
* if ( selfTest )
* {
* CppUnit::CompilerTestResultOutputter outputter( runner.result(),
* std::cerr );
* outputter.write();
* }
*
* return wasSucessful ? 0 : 1;
* }
* \endcode
*/
CompilerTestResultOutputter::CompilerTestResultOutputter(
TestResult *result,
std::ostream &stream ) :
m_result( result ),
m_stream( stream )
{
}
CompilerTestResultOutputter::~CompilerTestResultOutputter()
{
}
void
CompilerTestResultOutputter::write()
{
if ( m_result->wasSuccessful() )
printSucess();
else
printFailureReport();
}
void
CompilerTestResultOutputter::printSucess()
{
m_stream << "OK" << std::endl;
}
void
CompilerTestResultOutputter::printFailureReport()
{
printFailuresList();
printStatistics();
}
void
CompilerTestResultOutputter::printFailuresList()
{
for ( int index =0; index < m_result->testFailuresTotal(); ++index)
{
printFailureDetail( m_result->failures()[ index ] );
}
}
void
CompilerTestResultOutputter::printFailureDetail( TestFailure *failure )
{
printFailureLocation( failure->sourceLine() );
printFailureType( failure );
printFailedTestName( failure );
printFailureMessage( failure );
}
void
CompilerTestResultOutputter::printFailureLocation( SourceLine sourceLine )
{
if ( sourceLine.isValid() )
m_stream << sourceLine.fileName()
<< "(" << sourceLine.lineNumber() << ") : ";
else
m_stream << "##Failure Location unknown## : ";
}
void
CompilerTestResultOutputter::printFailureType( TestFailure *failure )
{
m_stream << (failure->isError() ? "Error" : "Assertion");
}
void
CompilerTestResultOutputter::printFailedTestName( TestFailure *failure )
{
m_stream << std::endl;
m_stream << "Test name: " << failure->failedTestName();
}
void
CompilerTestResultOutputter::printFailureMessage( TestFailure *failure )
{
m_stream << std::endl;
Exception *thrownException = failure->thrownException();
if ( thrownException->isInstanceOf( NotEqualException::type() ) )
printNotEqualMessage( thrownException );
else
printDefaultMessage( thrownException );
m_stream << std::endl;
}
void
CompilerTestResultOutputter::printNotEqualMessage( Exception *thrownException )
{
NotEqualException *e = (NotEqualException *)thrownException;
m_stream << wrap( "- Expected : " + e->expectedValue() );
m_stream << std::endl;
m_stream << wrap( "- Actual : " + e->actualValue() );
m_stream << std::endl;
if ( !e->additionalMessage().empty() )
{
m_stream << wrap( "- " + e->additionalMessage() );
m_stream << std::endl;
}
}
void
CompilerTestResultOutputter::printDefaultMessage( Exception *thrownException )
{
std::string wrappedMessage = wrap( thrownException->what() );
m_stream << wrappedMessage << std::endl;
}
void
CompilerTestResultOutputter::printStatistics()
{
m_stream << "Failures !!!" << std::endl;
m_stream << "Run: " << m_result->runTests() << " "
<< "Failure total: " << m_result->testFailuresTotal() << " "
<< "Failures: " << m_result->testFailures() << " "
<< "Errors: " << m_result->testErrors()
<< std::endl;
}
std::string
CompilerTestResultOutputter::wrap( std::string message )
{
const int maxLineLength = 60;
std::string wrapped;
int index =0;
while ( index < message.length() )
{
std::string line( message.substr( index, maxLineLength ) );
wrapped += line;
index += maxLineLength;
if ( index < message.length() )
wrapped += "\n";
}
return wrapped;
}
} // namespace CppUnit
|