1 #include <cppunit/Exception.h>
2 #include <cppunit/NotEqualException.h>
3 #include <cppunit/Test.h>
4 #include <cppunit/TestFailure.h>
5 #include <cppunit/TextTestResult.h>
6 #include <iostream>
7
8
9 using std::cerr;
10 using std::endl;
11 using std::ostream;
12
13 namespace CppUnit {
14
15
TextTestResult()16 TextTestResult::TextTestResult()
17 {
18 addListener( this );
19 }
20
21
22 void
addFailure(Test * test,Exception * e)23 TextTestResult::addFailure( Test *test, Exception *e )
24 {
25 TestResult::addFailure( test, e );
26 }
27
28
29 void
addFailure(const TestFailure & failure)30 TextTestResult::addFailure( const TestFailure &failure )
31 {
32 TestResultCollector::addFailure( failure );
33 cerr << ( failure.isError() ? "E" : "F" );
34 }
35
36
37 void
startTest(Test * test)38 TextTestResult::startTest( Test *test )
39 {
40 TestResultCollector::startTest (test);
41 cerr << ".";
42 }
43
44
45 void
printFailures(ostream & stream)46 TextTestResult::printFailures( ostream &stream )
47 {
48 TestFailures::const_iterator itFailure = failures().begin();
49 int failureNumber = 1;
50 while ( itFailure != failures().end() )
51 {
52 stream << endl;
53 printFailure( *itFailure++, failureNumber++, stream );
54 }
55 }
56
57
58 void
printFailure(TestFailure * failure,int failureNumber,ostream & stream)59 TextTestResult::printFailure( TestFailure *failure,
60 int failureNumber,
61 ostream &stream )
62 {
63 printFailureListMark( failureNumber, stream );
64 stream << ' ';
65 printFailureTestName( failure, stream );
66 stream << ' ';
67 printFailureType( failure, stream );
68 stream << ' ';
69 printFailureLocation( failure->sourceLine(), stream );
70 stream << endl;
71 printFailureDetail( failure->thrownException(), stream );
72 stream << endl;
73 }
74
75
76 void
printFailureListMark(int failureNumber,ostream & stream)77 TextTestResult::printFailureListMark( int failureNumber,
78 ostream &stream )
79 {
80 stream << failureNumber << ")";
81 }
82
83
84 void
printFailureTestName(TestFailure * failure,ostream & stream)85 TextTestResult::printFailureTestName( TestFailure *failure,
86 ostream &stream )
87 {
88 stream << "test: " << failure->failedTest()->getName();
89 }
90
91
92 void
printFailureType(TestFailure * failure,ostream & stream)93 TextTestResult::printFailureType( TestFailure *failure,
94 ostream &stream )
95 {
96 stream << "("
97 << (failure->isError() ? "E" : "F")
98 << ")";
99 }
100
101
102 void
printFailureLocation(SourceLine sourceLine,ostream & stream)103 TextTestResult::printFailureLocation( SourceLine sourceLine,
104 ostream &stream )
105 {
106 if ( !sourceLine.isValid() )
107 return;
108
109 stream << "line: " << sourceLine.lineNumber()
110 << ' ' << sourceLine.fileName();
111 }
112
113
114 void
printFailureDetail(Exception * thrownException,ostream & stream)115 TextTestResult::printFailureDetail( Exception *thrownException,
116 ostream &stream )
117 {
118 if ( thrownException->isInstanceOf( NotEqualException::type() ) )
119 {
120 NotEqualException *e = (NotEqualException*)thrownException;
121 stream << "expected: " << e->expectedValue() << endl
122 << "but was: " << e->actualValue();
123 if ( !e->additionalMessage().empty() )
124 {
125 stream << endl;
126 stream << "additional message:" << endl
127 << e->additionalMessage();
128 }
129 }
130 else
131 {
132 stream << " \"" << thrownException->what() << "\"";
133 }
134 }
135
136
137 void
print(ostream & stream)138 TextTestResult::print( ostream& stream )
139 {
140 printHeader( stream );
141 stream << endl;
142 printFailures( stream );
143 }
144
145
146 void
printHeader(ostream & stream)147 TextTestResult::printHeader( ostream &stream )
148 {
149 if (wasSuccessful ())
150 stream << endl << "OK (" << runTests () << " tests)"
151 << endl;
152 else
153 {
154 stream << endl;
155 printFailureWarning( stream );
156 printStatistics( stream );
157 }
158 }
159
160
161 void
printFailureWarning(ostream & stream)162 TextTestResult::printFailureWarning( ostream &stream )
163 {
164 stream << "!!!FAILURES!!!" << endl;
165 }
166
167
168 void
printStatistics(ostream & stream)169 TextTestResult::printStatistics( ostream &stream )
170 {
171 stream << "Test Results:" << endl;
172
173 stream << "Run: " << runTests()
174 << " Failures: " << testFailures()
175 << " Errors: " << testErrors()
176 << endl;
177 }
178
179
180 ostream &
operator <<(ostream & stream,TextTestResult & result)181 operator <<( ostream &stream,
182 TextTestResult &result )
183 {
184 result.print (stream); return stream;
185 }
186
187
188 } // namespace CppUnit
189