From 8ac4da778cb226fb00b413a047ad3480f4d2ae5a Mon Sep 17 00:00:00 2001 From: Baptiste Lepilleur Date: Sat, 6 Oct 2001 11:03:30 +0000 Subject: Include/cppunit/CompilerTestResultOutputter. include/cppunit/CompilerTestResultOutputter.h : * src/cppunit/CompilerTestResultOutputter.cpp : added. Output result in a compiler compatible format. * src/cppunit/CppUnit.dsp : * include/cppunit/MakeFile.am : * src/cppunit/MakeFile.am : added CompilerTestResultOutputter.cpp and CompilerTestResultOutputter.h. * examples/cppunittest/CppUnitTestMain.cpp : if -selftest is specified on the command line, no standard test result are printed, but compiler compatible result at printed. * examples/cppunittest/CppUnitTestMain.dsp : added post-build step to run the test suite with -selftest. * NEWS : updated. * src/cppunit/TextTestRunner.cpp : skip a line after printing progress. --- src/cppunit/CompilerTestResultOutputter.cpp | 186 ++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 src/cppunit/CompilerTestResultOutputter.cpp (limited to 'src/cppunit/CompilerTestResultOutputter.cpp') diff --git a/src/cppunit/CompilerTestResultOutputter.cpp b/src/cppunit/CompilerTestResultOutputter.cpp new file mode 100644 index 0000000..1c75b49 --- /dev/null +++ b/src/cppunit/CompilerTestResultOutputter.cpp @@ -0,0 +1,186 @@ +#include +#include +#include +#include + + +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 -- cgit v1.2.1