diff options
| author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2002-06-13 14:31:01 +0000 |
|---|---|---|
| committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2002-06-13 14:31:01 +0000 |
| commit | abd178318ae3cdb6cc0a700e77414a33ef9297ca (patch) | |
| tree | 76bb1f6d0bf28bfe23c710487d0b20bd95878ca4 /src/cppunit | |
| parent | 3702f4f7603f1e49b4d6747c49e795bad712eab7 (diff) | |
| download | cppunit-abd178318ae3cdb6cc0a700e77414a33ef9297ca.tar.gz | |
Include/cppunit/Asserter.
include/cppunit/Asserter.h:
* src/cppunit/Asserter.cpp: added functions that take a Message as a
parameter. Existing function have a short description indicating
an assertion failure.
* include/cppunit/CompilerOuputter.h:
* src/cppunit/CompilerOuputter.cpp: removed printNotEqualMessage() and
printDefaultMessage(). Updated to use Message.
* include/cppunit/Message.h:
* src/cppunit/Message.cpp: added. Represents a message associated to an
Exception.
* include/cppunit/Exception.h:
* src/cppunit/Exception.cpp: the message associated to the exception is now
stored as a Message instead of a string.
* include/cppunit/NotEqualException.cpp: constructs a Message instead of a
string.
* include/cppunit/TestAssert.h:
* src/cppunit/TestAssert.cpp: updated to use Asserter functions that
take a message when pertinent (CPPUNIT_FAIL...).
* include/cppunit/TestCaller.h:
* src/cppunit/TestCaller.cpp: exception not caught failure has a better
short description.
* src/cppunit/TestCase.cpp: better short description when setUp() or
tearDown() fail.
* src/msvc6/testrunner/TestRunnerDlg.cpp: replace '\n' in failure message
with space.
* examples/cppunittest/ExceptionTest.cpp:
* examples/cppunittest/NotEqualExceptionTest.cpp:
* examples/cppunittest/TestCallerTest.cpp:
* examples/cppunittest/TestFailureTest.cpp:
* examples/cppunittest/TestResultCollectorTest.h:
* examples/cppunittest/TestResultCollectorTest.cpp:
* examples/cppunittest/TestResultTest.cpp:
* examples/cppunittest/XmlOutputterTest.h:
* examples/cppunittest/XmlOutputterTest.cpp: updated to use Exception/Message.
* examples/cppunittest/MessageTest.h:
* examples/cppunittest/MessageTest.cpp: added. Unit test for Message.
Diffstat (limited to 'src/cppunit')
| -rw-r--r-- | src/cppunit/Asserter.cpp | 44 | ||||
| -rw-r--r-- | src/cppunit/CompilerOutputter.cpp | 31 | ||||
| -rw-r--r-- | src/cppunit/Exception.cpp | 50 | ||||
| -rw-r--r-- | src/cppunit/Makefile.am | 3 | ||||
| -rw-r--r-- | src/cppunit/Message.cpp | 150 | ||||
| -rw-r--r-- | src/cppunit/NotEqualException.cpp | 12 | ||||
| -rw-r--r-- | src/cppunit/TestCase.cpp | 9 | ||||
| -rw-r--r-- | src/cppunit/TextOutputter.cpp | 18 | ||||
| -rw-r--r-- | src/cppunit/cppunit.dsp | 8 | ||||
| -rw-r--r-- | src/cppunit/cppunit_dll.dsp | 8 |
10 files changed, 247 insertions, 86 deletions
diff --git a/src/cppunit/Asserter.cpp b/src/cppunit/Asserter.cpp index 29c5201..659e328 100644 --- a/src/cppunit/Asserter.cpp +++ b/src/cppunit/Asserter.cpp @@ -1,4 +1,5 @@ #include <cppunit/Asserter.h> +#include <cppunit/Message.h> #include <cppunit/NotEqualException.h> @@ -12,7 +13,15 @@ namespace Asserter void fail( std::string message, - SourceLine sourceLine ) + const SourceLine &sourceLine ) +{ + fail( Message( "assertion failed", message ), sourceLine ); +} + + +void +fail( const Message &message, + const SourceLine &sourceLine ) { throw Exception( message, sourceLine ); } @@ -20,18 +29,41 @@ fail( std::string message, void failIf( bool shouldFail, - std::string message, - SourceLine location ) + const Message &message, + const SourceLine &sourceLine ) { if ( shouldFail ) - fail( message, location ); + fail( message, sourceLine ); +} + + +void +failIf( bool shouldFail, + std::string message, + const SourceLine &sourceLine ) +{ + failIf( shouldFail, Message( "assertion failed", message ), sourceLine ); } +/* @@fixme Need to take NotEqualException down before including that change. +void +failNotEqual( std::string expected, + std::string actual, + const SourceLine &sourceLine, + const Message &additionalMessage ) +{ + Message message( "equality assertion failed", + "Expected: " + expected, + "Actual : " + actual ); + message.addDetail( additionalMessage ); + fail( message, sourceLine ); +} +*/ void failNotEqual( std::string expected, std::string actual, - SourceLine sourceLine, + const SourceLine &sourceLine, std::string additionalMessage ) { throw NotEqualException( expected, @@ -45,7 +77,7 @@ void failNotEqualIf( bool shouldFail, std::string expected, std::string actual, - SourceLine sourceLine, + const SourceLine &sourceLine, std::string additionalMessage ) { if ( shouldFail ) diff --git a/src/cppunit/CompilerOutputter.cpp b/src/cppunit/CompilerOutputter.cpp index 1c0b14f..2355fc9 100644 --- a/src/cppunit/CompilerOutputter.cpp +++ b/src/cppunit/CompilerOutputter.cpp @@ -169,35 +169,8 @@ 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; + m_stream << thrownException->message().shortDescription() << std::endl; + m_stream << wrap( thrownException->message().details() ) << std::endl; } diff --git a/src/cppunit/Exception.cpp b/src/cppunit/Exception.cpp index ab8cdde..2c01324 100644 --- a/src/cppunit/Exception.cpp +++ b/src/cppunit/Exception.cpp @@ -17,48 +17,39 @@ const long Exception::UNKNOWNLINENUMBER = -1; #endif -/// Construct the exception -Exception::Exception( const Exception &other ) : - std::exception( other ) +Exception::Exception( const Exception &other ) + : std::exception( other ) { m_message = other.m_message; m_sourceLine = other.m_sourceLine; } -/*! - * \deprecated Use other constructor instead. - */ -Exception::Exception( std::string message, - SourceLine sourceLine ) : - m_message( message ), - m_sourceLine( sourceLine ) +Exception::Exception( const Message &message, + const SourceLine &sourceLine ) + : m_message( message ) + , m_sourceLine( sourceLine ) { } #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED -/*! - * \deprecated Use other constructor instead. - */ Exception::Exception( std::string message, long lineNumber, - std::string fileName ) : - m_message( message ), - m_sourceLine( fileName, lineNumber ) + std::string fileName ) + : m_message( message ) + , m_sourceLine( fileName, lineNumber ) { } #endif -/// Destruct the exception -Exception::~Exception () throw() +Exception::~Exception() throw() { } -/// Perform an assignment -Exception& +Exception & Exception::operator =( const Exception& other ) { // Don't call superclass operator =(). VC++ STL implementation @@ -76,14 +67,16 @@ Exception::operator =( const Exception& other ) } -/// Return descriptive message const char* Exception::what() const throw() -{ - return m_message.c_str (); +{ + Exception *mutableThis = const_cast<Exception *>( this ); + mutableThis->m_whatMessage = m_message.shortDescription() + "\n" + + m_message.details(); + return m_whatMessage.c_str(); } -/// Location where the error occured + SourceLine Exception::sourceLine() const { @@ -91,8 +84,14 @@ Exception::sourceLine() const } +Message +Exception::message() const +{ + return m_message; +} + + #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED -/// The line on which the error occurred long Exception::lineNumber() const { @@ -101,7 +100,6 @@ Exception::lineNumber() const } -/// The file in which the error occurred std::string Exception::fileName() const { diff --git a/src/cppunit/Makefile.am b/src/cppunit/Makefile.am index c0a45f7..4376042 100644 --- a/src/cppunit/Makefile.am +++ b/src/cppunit/Makefile.am @@ -1,5 +1,5 @@ # -# $Id: Makefile.am,v 1.32 2002-05-25 09:27:39 blep Exp $ +# $Id: Makefile.am,v 1.33 2002-06-13 15:31:01 blep Exp $ # EXTRA_DIST = cppunit.dsp cppunit_dll.dsp DllMain.cpp @@ -15,6 +15,7 @@ libcppunit_la_SOURCES = \ DynamicLibraryManager.cpp \ DynamicLibraryManagerException.cpp \ Exception.cpp \ + Message.cpp \ NotEqualException.cpp \ RepeatedTest.cpp \ PlugInManager.cpp \ diff --git a/src/cppunit/Message.cpp b/src/cppunit/Message.cpp new file mode 100644 index 0000000..315b9bb --- /dev/null +++ b/src/cppunit/Message.cpp @@ -0,0 +1,150 @@ +#include <cppunit/Message.h> + + +namespace CppUnit +{ + + +Message::Message() +{ +} + + +Message::Message( const std::string &shortDescription ) + : m_shortDescription( shortDescription ) +{ +} + + +Message::Message( const std::string &shortDescription, + const std::string &detail1 ) + : m_shortDescription( shortDescription ) +{ + addDetail( detail1 ); +} + + +Message::Message( const std::string &shortDescription, + const std::string &detail1, + const std::string &detail2 ) + : m_shortDescription( shortDescription ) +{ + addDetail( detail1, detail2 ); +} + + +Message::Message( const std::string &shortDescription, + const std::string &detail1, + const std::string &detail2, + const std::string &detail3 ) + : m_shortDescription( shortDescription ) +{ + addDetail( detail1, detail2, detail3 ); +} + + +const std::string & +Message::shortDescription() const +{ + return m_shortDescription; +} + + +int +Message::detailCount() const +{ + return m_details.size(); +} + + +std::string +Message::detailAt( int index ) const +{ + if ( index < 0 || index >= detailCount() ) + throw std::invalid_argument( "Message::detailAt() : invalid index" ); + + return m_details[ index ]; +} + + +std::string +Message::details() const +{ + std::string details; + for ( Details::const_iterator it = m_details.begin(); it != m_details.end(); ++it ) + { + details += "- "; + details += *it; + details += '\n'; + } + return details; +} + + +void +Message::clearDetails() +{ + m_details.clear(); +} + + +void +Message::addDetail( const std::string &detail ) +{ + m_details.push_back( detail ); +} + + +void +Message::addDetail( const std::string &detail1, + const std::string &detail2 ) +{ + addDetail( detail1 ); + addDetail( detail2 ); +} + + +void +Message::addDetail( const std::string &detail1, + const std::string &detail2, + const std::string &detail3 ) +{ + addDetail( detail1, detail2 ); + addDetail( detail3 ); +} + + +void +Message::addDetail( const Message &message ) +{ + m_details.insert( m_details.end(), + message.m_details.begin(), + message.m_details.end() ); +} + + +void +Message::setShortDescription( const std::string &shortDescription ) +{ + m_shortDescription = shortDescription; +} + + +bool +Message::operator ==( const Message &other ) const +{ + return m_shortDescription == other.m_shortDescription && + m_details == other.m_details; +} + + +bool +Message::operator !=( const Message &other ) const +{ + return !( *this == other ); +} + + + +} // namespace CppUnit + diff --git a/src/cppunit/NotEqualException.cpp b/src/cppunit/NotEqualException.cpp index 738961d..912597c 100644 --- a/src/cppunit/NotEqualException.cpp +++ b/src/cppunit/NotEqualException.cpp @@ -7,14 +7,16 @@ NotEqualException::NotEqualException( std::string expected, std::string actual, SourceLine sourceLine , std::string additionalMessage ) : - Exception( "Expected: " + expected + - ", but was: " + actual + - "." + additionalMessage , + Exception( Message( "equality assertion failed", + "Expected: " + expected, + "Actual : " + actual ), sourceLine), m_expected( expected ), m_actual( actual ), m_additionalMessage( additionalMessage ) { + if ( !m_additionalMessage.empty() ) + m_message.addDetail( m_additionalMessage ); } @@ -26,7 +28,9 @@ NotEqualException::NotEqualException( std::string expected, std::string actual, long lineNumber, std::string fileName ) : - Exception( "Expected: " + expected + ", but was: " + actual, + Exception( Message( "equality assertion failed", + "Expected: " + expected, + "Actual : " + actual ), lineNumber, fileName ), m_expected( expected ), diff --git a/src/cppunit/TestCase.cpp b/src/cppunit/TestCase.cpp index 4fb388a..3ab0fbe 100644 --- a/src/cppunit/TestCase.cpp +++ b/src/cppunit/TestCase.cpp @@ -36,10 +36,11 @@ TestCase::run( TestResult *result ) result->addFailure( this, copy ); } catch ( std::exception &e ) { - result->addError( this, new Exception( e.what() ) ); + result->addError( this, new Exception( Message( "uncaught std::exception", + e.what() ) ) ); } catch (...) { - Exception *e = new Exception( "caught unknown exception" ); + Exception *e = new Exception( Message( "uncaught unknown exception" ) ); result->addError( this, e ); } @@ -47,11 +48,11 @@ TestCase::run( TestResult *result ) tearDown(); } catch (...) { - result->addError( this, new Exception( "tearDown() failed" ) ); + result->addError( this, new Exception( Message( "tearDown() failed" ) ) ); } } catch (...) { - result->addError( this, new Exception( "setUp() failed" ) ); + result->addError( this, new Exception( Message( "setUp() failed" ) ) ); } result->endTest( this ); diff --git a/src/cppunit/TextOutputter.cpp b/src/cppunit/TextOutputter.cpp index fc32598..577d808 100644 --- a/src/cppunit/TextOutputter.cpp +++ b/src/cppunit/TextOutputter.cpp @@ -99,22 +99,8 @@ TextOutputter::printFailureLocation( SourceLine sourceLine ) void TextOutputter::printFailureDetail( Exception *thrownException ) { - if ( thrownException->isInstanceOf( NotEqualException::type() ) ) - { - NotEqualException *e = (NotEqualException*)thrownException; - m_stream << "expected: " << e->expectedValue() << std::endl - << "but was: " << e->actualValue(); - if ( !e->additionalMessage().empty() ) - { - m_stream << std::endl; - m_stream << "additional message:" << std::endl - << e->additionalMessage(); - } - } - else - { - m_stream << " \"" << thrownException->what() << "\""; - } + m_stream << thrownException->message().shortDescription() << std::endl; + m_stream << thrownException->message().details(); } diff --git a/src/cppunit/cppunit.dsp b/src/cppunit/cppunit.dsp index e4f4d36..c22fbca 100644 --- a/src/cppunit/cppunit.dsp +++ b/src/cppunit/cppunit.dsp @@ -253,6 +253,14 @@ SOURCE=..\..\include\cppunit\Exception.h # End Source File # Begin Source File +SOURCE=.\Message.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\include\cppunit\Message.h +# End Source File +# Begin Source File + SOURCE=.\NotEqualException.cpp # End Source File # Begin Source File diff --git a/src/cppunit/cppunit_dll.dsp b/src/cppunit/cppunit_dll.dsp index 2acd5a5..ec00f3c 100644 --- a/src/cppunit/cppunit_dll.dsp +++ b/src/cppunit/cppunit_dll.dsp @@ -199,6 +199,14 @@ SOURCE=..\..\include\cppunit\Exception.h # End Source File # Begin Source File +SOURCE=.\Message.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\include\cppunit\Message.h +# End Source File +# Begin Source File + SOURCE=.\NotEqualException.cpp # End Source File # Begin Source File |
