diff options
| author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2001-10-06 11:03:30 +0000 |
|---|---|---|
| committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2001-10-06 11:03:30 +0000 |
| commit | 8ac4da778cb226fb00b413a047ad3480f4d2ae5a (patch) | |
| tree | 1c239ed54fb94b0ba2c329e7d7e177de4219d4b7 /src/cppunit | |
| parent | 4c6c7e6474bef34c4bed0d3eb9889012319c938f (diff) | |
| download | cppunit-8ac4da778cb226fb00b413a047ad3480f4d2ae5a.tar.gz | |
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.
Diffstat (limited to 'src/cppunit')
| -rw-r--r-- | src/cppunit/CompilerTestResultOutputter.cpp | 186 | ||||
| -rw-r--r-- | src/cppunit/Makefile.am | 3 | ||||
| -rw-r--r-- | src/cppunit/TextTestRunner.cpp | 1 | ||||
| -rw-r--r-- | src/cppunit/cppunit.dsp | 9 |
4 files changed, 198 insertions, 1 deletions
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 <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 diff --git a/src/cppunit/Makefile.am b/src/cppunit/Makefile.am index 7dc8752..b7764bd 100644 --- a/src/cppunit/Makefile.am +++ b/src/cppunit/Makefile.am @@ -1,5 +1,5 @@ # -# $Id: Makefile.am,v 1.12 2001-10-05 22:27:14 blep Exp $ +# $Id: Makefile.am,v 1.13 2001-10-06 12:03:30 blep Exp $ # EXTRA_DIST = cppunit.dsw cppunit.dsp @@ -9,6 +9,7 @@ lib_LTLIBRARIES = libcppunit.la libcppunit_la_SOURCES = \ Asserter.cpp \ + CompilerTestResultOutputter.cpp \ NotEqualException.cpp \ RepeatedTest.cpp \ SourceLine.cpp \ diff --git a/src/cppunit/TextTestRunner.cpp b/src/cppunit/TextTestRunner.cpp index 7f2bc37..8b4da15 100644 --- a/src/cppunit/TextTestRunner.cpp +++ b/src/cppunit/TextTestRunner.cpp @@ -96,6 +96,7 @@ TextTestRunner::wait( bool doWait ) void TextTestRunner::printResult( bool doPrintResult ) { + std::cout << std::endl; if ( doPrintResult ) std::cout << *m_result << std::endl; } diff --git a/src/cppunit/cppunit.dsp b/src/cppunit/cppunit.dsp index 234f6cd..bb940d2 100644 --- a/src/cppunit/cppunit.dsp +++ b/src/cppunit/cppunit.dsp @@ -262,6 +262,15 @@ SOURCE=..\..\include\cppunit\TestSuite.h # PROP Default_Filter "" # Begin Source File +SOURCE=.\CompilerTestResultOutputter.cpp +# SUBTRACT CPP /YX +# End Source File +# Begin Source File + +SOURCE=..\..\include\cppunit\CompilerTestResultOutputter.h +# End Source File +# Begin Source File + SOURCE=.\TextTestResult.cpp # End Source File # Begin Source File |
