diff options
| author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2001-10-07 18:37:53 +0000 |
|---|---|---|
| committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2001-10-07 18:37:53 +0000 |
| commit | 150339335e3c5538c1e496ae01babf148bd29ec5 (patch) | |
| tree | 9f354aaee81e08c27dc6da0709074babde7672f5 /src/cppunit/CompilerOutputter.cpp | |
| parent | 4410db53e88dc5aca9f86abaf9dffccbd5f94471 (diff) | |
| download | cppunit-150339335e3c5538c1e496ae01babf148bd29ec5.tar.gz | |
Include/cppunit/CompilerTestResultOutputter.
include/cppunit/CompilerTestResultOutputter.h :
renamed CompilerOutputter.h
* src/cppunit/CompilerTestResultOutputter.cpp :
renamed CompilerOutputter.cpp
* include/cppunit/CompilerTestResultOutputter.h :
* src/cppunit/CompilerTestResultOutputter.cpp : ajust max line length
for wrapping. Added static factory method defaultOutputter(). Print
the number of test runs on success.
* include/cppunit/CompilerTestResultOutputter.h : renamed
CompilerOutputter.h.
* src/cppunit/CompilerTestResultOutputter.cpp : renamed
CompilerOutputter.cpp.
* examples/cppunittest/CppUnitTestMain.cpp : use factory method
CompilerTestResultOutputter::defaultOutputter().
* src/msvc6/DSPlugIn/DSPlugIn.dsp : removed COM registration in
post-build step. IT is automatically done by VC++ when the add-in is
added. Caused build to failed if the add-in was used in VC++.
* NEWS : updated.
* src/cppunit/TestAssert.cpp : modified deprecated assert
implementations to use Asserter.
* examples/cppunittest/XmlTestResultOutputterTest.h :
renamed XmlOutputterTest.h.
* examples/cppunittest/XmlTestResultOutputterTest.cpp :
renamed XmlOutputterTest.cpp.
* NEWS :
* examples/cppunittest/CppUnitTestMain.cpp :
* examples/cppunittest/CppUnitTestMain.dsp :
* examples/cppunittest/Makefile.am :
* examples/cppunittest/XmlTestResultOutputterTest.h :
* examples/cppunittest/XmlTestResultOutputterTest.cpp :
* examples/msvc6/CppUniTestApp/CppUnitTestApp.dsp
* include/cppunit/CompilerOutputter.h :
* include/cppunit/Makefile.am :
* include/cppunit/XmlTestResultOutputter.h :
* src/cppunit/CompilerOutputter.cpp :
* src/cppunit/cppunit.dsp :
* src/cppunit/Makefile.am :
* src/cppunit/XmlTestResultOutputter.cpp : change due to renaming
CompilerTestResultOutputter to CompilerOutputter,
XmlTestResultOutputter to XmlOuputter, XmlTestResultOutputterTest
to XmlOutputterTest.
Diffstat (limited to 'src/cppunit/CompilerOutputter.cpp')
| -rw-r--r-- | src/cppunit/CompilerOutputter.cpp | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/src/cppunit/CompilerOutputter.cpp b/src/cppunit/CompilerOutputter.cpp new file mode 100644 index 0000000..aee0ef6 --- /dev/null +++ b/src/cppunit/CompilerOutputter.cpp @@ -0,0 +1,197 @@ +#include <cppunit/NotEqualException.h> +#include <cppunit/SourceLine.h> +#include <cppunit/TestResult.h> +#include <cppunit/CompilerOutputter.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::CompilerOutputter outputter( runner.result(), + * std::cerr ); + * outputter.write(); + * } + * + * return wasSucessful ? 0 : 1; + * } + * \endcode + */ +CompilerOutputter::CompilerOutputter( + TestResult *result, + std::ostream &stream ) : + m_result( result ), + m_stream( stream ) +{ +} + + +CompilerOutputter::~CompilerOutputter() +{ +} + + +CompilerOutputter * +CompilerOutputter::defaultOutputter( TestResult *result, + std::ostream &stream ) +{ + return new CompilerOutputter( result, stream ); +// For automatic adpatation... +// return new CPPUNIT_DEFAULT_OUTPUTTER( result, stream ); +} + + +void +CompilerOutputter::write() +{ + if ( m_result->wasSuccessful() ) + printSucess(); + else + printFailureReport(); +} + + +void +CompilerOutputter::printSucess() +{ + m_stream << "OK (" << m_result->runTests() << ")" + << std::endl; +} + + +void +CompilerOutputter::printFailureReport() +{ + printFailuresList(); + printStatistics(); +} + + +void +CompilerOutputter::printFailuresList() +{ + for ( int index =0; index < m_result->testFailuresTotal(); ++index) + { + printFailureDetail( m_result->failures()[ index ] ); + } +} + + +void +CompilerOutputter::printFailureDetail( TestFailure *failure ) +{ + printFailureLocation( failure->sourceLine() ); + printFailureType( failure ); + printFailedTestName( failure ); + printFailureMessage( failure ); +} + + +void +CompilerOutputter::printFailureLocation( SourceLine sourceLine ) +{ + if ( sourceLine.isValid() ) + m_stream << sourceLine.fileName() + << "(" << sourceLine.lineNumber() << ") : "; + else + m_stream << "##Failure Location unknown## : "; +} + + +void +CompilerOutputter::printFailureType( TestFailure *failure ) +{ + m_stream << (failure->isError() ? "Error" : "Assertion"); +} + + +void +CompilerOutputter::printFailedTestName( TestFailure *failure ) +{ + m_stream << std::endl; + m_stream << "Test name: " << failure->failedTestName(); +} + + +void +CompilerOutputter::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 +CompilerOutputter::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 +CompilerOutputter::printDefaultMessage( Exception *thrownException ) +{ + std::string wrappedMessage = wrap( thrownException->what() ); + m_stream << wrappedMessage << std::endl; +} + + +void +CompilerOutputter::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 +CompilerOutputter::wrap( std::string message ) +{ + const int maxLineLength = 80; + 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 |
