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 9using std::cerr; 10using std::endl; 11using std::ostream; 12 13namespace CppUnit { 14 15 16TextTestResult::TextTestResult() 17{ 18 addListener( this ); 19} 20 21 22void 23TextTestResult::addFailure( Test *test, Exception *e ) 24{ 25 TestResult::addFailure( test, e ); 26} 27 28 29void 30TextTestResult::addFailure( const TestFailure &failure ) 31{ 32 TestResultCollector::addFailure( failure ); 33 cerr << ( failure.isError() ? "E" : "F" ); 34} 35 36 37void 38TextTestResult::startTest( Test *test ) 39{ 40 TestResultCollector::startTest (test); 41 cerr << "."; 42} 43 44 45void 46TextTestResult::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 58void 59TextTestResult::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 76void 77TextTestResult::printFailureListMark( int failureNumber, 78 ostream &stream ) 79{ 80 stream << failureNumber << ")"; 81} 82 83 84void 85TextTestResult::printFailureTestName( TestFailure *failure, 86 ostream &stream ) 87{ 88 stream << "test: " << failure->failedTest()->getName(); 89} 90 91 92void 93TextTestResult::printFailureType( TestFailure *failure, 94 ostream &stream ) 95{ 96 stream << "(" 97 << (failure->isError() ? "E" : "F") 98 << ")"; 99} 100 101 102void 103TextTestResult::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 114void 115TextTestResult::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 137void 138TextTestResult::print( ostream& stream ) 139{ 140 printHeader( stream ); 141 stream << endl; 142 printFailures( stream ); 143} 144 145 146void 147TextTestResult::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 161void 162TextTestResult::printFailureWarning( ostream &stream ) 163{ 164 stream << "!!!FAILURES!!!" << endl; 165} 166 167 168void 169TextTestResult::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 180ostream & 181operator <<( ostream &stream, 182 TextTestResult &result ) 183{ 184 result.print (stream); return stream; 185} 186 187 188} // namespace CppUnit 189