summaryrefslogtreecommitdiff
path: root/include/cppunit
diff options
context:
space:
mode:
authorBaptiste Lepilleur <gaiacrtn@free.fr>2002-06-13 14:31:01 +0000
committerBaptiste Lepilleur <gaiacrtn@free.fr>2002-06-13 14:31:01 +0000
commitabd178318ae3cdb6cc0a700e77414a33ef9297ca (patch)
tree76bb1f6d0bf28bfe23c710487d0b20bd95878ca4 /include/cppunit
parent3702f4f7603f1e49b4d6747c49e795bad712eab7 (diff)
downloadcppunit-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 'include/cppunit')
-rw-r--r--include/cppunit/Asserter.h41
-rw-r--r--include/cppunit/CompilerOutputter.h2
-rw-r--r--include/cppunit/Exception.h98
-rw-r--r--include/cppunit/Makefile.am1
-rw-r--r--include/cppunit/Message.h153
-rw-r--r--include/cppunit/TestAssert.h18
-rw-r--r--include/cppunit/TestCaller.h10
7 files changed, 269 insertions, 54 deletions
diff --git a/include/cppunit/Asserter.h b/include/cppunit/Asserter.h
index 808b909..412b831 100644
--- a/include/cppunit/Asserter.h
+++ b/include/cppunit/Asserter.h
@@ -8,6 +8,9 @@
namespace CppUnit
{
+class Message;
+
+
/*! \brief A set of functions to help writing assertion macros.
* \ingroup CreatingNewAssertions
*
@@ -44,8 +47,14 @@ namespace Asserter
/*! Throws a Exception with the specified message and location.
*/
+ void CPPUNIT_API fail( const Message &message,
+ const SourceLine &sourceLine = SourceLine() );
+
+ /*! Throws a Exception with the specified message and location.
+ * \deprecated Use fail( Message, SourceLine ) instead.
+ */
void CPPUNIT_API fail( std::string message,
- SourceLine sourceLine = SourceLine() );
+ const SourceLine &sourceLine = SourceLine() );
/*! Throws a Exception with the specified message and location.
* \param shouldFail if \c true then the exception is thrown. Otherwise
@@ -54,8 +63,32 @@ namespace Asserter
* \param sourceLine Location of the assertion.
*/
void CPPUNIT_API failIf( bool shouldFail,
+ const Message &message,
+ const SourceLine &sourceLine = SourceLine() );
+
+ /*! Throws a Exception with the specified message and location.
+ * \deprecated Use failIf( bool, Message, SourceLine ) instead.
+ * \param shouldFail if \c true then the exception is thrown. Otherwise
+ * nothing happen.
+ * \param message Message explaining the assertion failiure.
+ * \param sourceLine Location of the assertion.
+ */
+ void CPPUNIT_API failIf( bool shouldFail,
std::string message,
- SourceLine sourceLine = SourceLine() );
+ const SourceLine &sourceLine = SourceLine() );
+
+
+ /*! Throws a NotEqualException with the specified message and location.
+ * \param expected Text describing the expected value.
+ * \param actual Text describing the actual value.
+ * \param additionalMessage Additional message. Usually used to report
+ * what are the differences between the expected and actual value.
+ * \param sourceLine Location of the assertion.
+ */
+ void CPPUNIT_API failNotEqual( std::string expected,
+ std::string actual,
+ const SourceLine &sourceLine,
+ const Message &additionalMessage );
/*! Throws a NotEqualException with the specified message and location.
* \param expected Text describing the expected value.
@@ -66,7 +99,7 @@ namespace Asserter
*/
void CPPUNIT_API failNotEqual( std::string expected,
std::string actual,
- SourceLine sourceLine = SourceLine(),
+ const SourceLine &sourceLine = SourceLine(),
std::string additionalMessage ="" );
/*! Throws a NotEqualException with the specified message and location.
@@ -81,7 +114,7 @@ namespace Asserter
void CPPUNIT_API failNotEqualIf( bool shouldFail,
std::string expected,
std::string actual,
- SourceLine sourceLine = SourceLine(),
+ const SourceLine &sourceLine = SourceLine(),
std::string additionalMessage ="" );
} // namespace Asserter
diff --git a/include/cppunit/CompilerOutputter.h b/include/cppunit/CompilerOutputter.h
index 6337768..5b9c418 100644
--- a/include/cppunit/CompilerOutputter.h
+++ b/include/cppunit/CompilerOutputter.h
@@ -114,8 +114,6 @@ public:
virtual void printFailureType( TestFailure *failure );
virtual void printFailedTestName( TestFailure *failure );
virtual void printFailureMessage( TestFailure *failure );
- virtual void printNotEqualMessage( Exception *thrownException );
- virtual void printDefaultMessage( Exception *thrownException );
virtual std::string wrap( std::string message );
private:
diff --git a/include/cppunit/Exception.h b/include/cppunit/Exception.h
index 505d16e..afadd00 100644
--- a/include/cppunit/Exception.h
+++ b/include/cppunit/Exception.h
@@ -2,9 +2,10 @@
#define CPPUNIT_EXCEPTION_H
#include <cppunit/Portability.h>
+#include <cppunit/Message.h>
#include <cppunit/SourceLine.h>
#include <exception>
-#include <string>
+
namespace CppUnit {
@@ -18,60 +19,87 @@ class CPPUNIT_API Exception : public std::exception
{
public:
- class Type
+ class Type
+ {
+ public:
+ Type( std::string type ) : m_type ( type )
+ {
+ }
+
+ bool operator ==( const Type &other ) const
{
- public:
- Type( std::string type ) : m_type ( type ) {}
+ return m_type == other.m_type;
+ }
- bool operator ==( const Type &other ) const
- {
- return m_type == other.m_type;
- }
- private:
- const std::string m_type;
- };
+ private:
+ const std::string m_type;
+ };
- Exception( std::string message = "",
- SourceLine sourceLine = SourceLine() );
+ /*! Constructs the exception with the specified message and source location.
+ * \param message Message associated to the exception.
+ * \param sourceLine Source location related to the exception.
+ */
+ Exception( const Message &message = Message(),
+ const SourceLine &sourceLine = SourceLine() );
#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
- Exception( std::string message,
- long lineNumber,
- std::string fileName );
+ /*!
+ * \deprecated Use other constructor instead.
+ */
+ Exception( std::string message,
+ long lineNumber,
+ std::string fileName );
#endif
- Exception (const Exception& other);
+ /*! Constructs a copy of an exception.
+ * \param other Exception to copy.
+ */
+ Exception( const Exception &other );
- virtual ~Exception () throw();
+ /// Destructs the exception
+ virtual ~Exception() throw();
- Exception& operator= (const Exception& other);
+ /// Performs an assignment
+ Exception &operator =( const Exception &other );
- const char *what() const throw ();
+ /// Returns descriptive message
+ const char *what() const throw();
- SourceLine sourceLine() const;
+ /// Location where the error occured
+ SourceLine sourceLine() const;
+
+ /// Message related to the exception.
+ Message message() const;
#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
- long lineNumber() const;
- std::string fileName() const;
+ /// The line on which the error occurred
+ long lineNumber() const;
+
+ /// The file in which the error occurred
+ std::string fileName() const;
- static const std::string UNKNOWNFILENAME;
- static const long UNKNOWNLINENUMBER;
+ static const std::string UNKNOWNFILENAME;
+ static const long UNKNOWNLINENUMBER;
#endif
- virtual Exception *clone() const;
-
- virtual bool isInstanceOf( const Type &type ) const;
+ /// Clones the exception.
+ virtual Exception *clone() const;
+
+ /// Tests if the exception is an instance of the specified type.
+ virtual bool isInstanceOf( const Type &type ) const;
- static Type type();
+ /// Type of this exception.
+ static Type type();
-private:
- // VC++ does not recognize call to parent class when prefixed
- // with a namespace. This is a workaround.
- typedef std::exception SuperClass;
+protected:
+ // VC++ does not recognize call to parent class when prefixed
+ // with a namespace. This is a workaround.
+ typedef std::exception SuperClass;
- std::string m_message;
- SourceLine m_sourceLine;
+ Message m_message;
+ SourceLine m_sourceLine;
+ std::string m_whatMessage;
};
diff --git a/include/cppunit/Makefile.am b/include/cppunit/Makefile.am
index dfe60f0..e9c7eaf 100644
--- a/include/cppunit/Makefile.am
+++ b/include/cppunit/Makefile.am
@@ -9,6 +9,7 @@ libcppunitinclude_HEADERS = \
BriefTestProgressListener.h \
CompilerOutputter.h \
Exception.h \
+ Message.h \
NotEqualException.h \
Outputter.h \
Portability.h \
diff --git a/include/cppunit/Message.h b/include/cppunit/Message.h
new file mode 100644
index 0000000..441a056
--- /dev/null
+++ b/include/cppunit/Message.h
@@ -0,0 +1,153 @@
+#ifndef CPPUNIT_MESSAGE_H
+#define CPPUNIT_MESSAGE_H
+
+#include <cppunit/Portability.h>
+
+#if CPPUNIT_NEED_DLL_DECL
+#pragma warning( push )
+#pragma warning( disable: 4251 ) // X needs to have dll-interface to be used by clients of class Z
+#endif
+
+#include <deque>
+#include <string>
+
+
+namespace CppUnit
+{
+
+#if CPPUNIT_NEED_DLL_DECL
+ template class CPPUNIT_API std::deque<std::string>;
+#endif
+
+/*! \brief Message associated to an Exception.
+ * \ingroup CreatingNewAssertions
+ * A message is composed of two items:
+ * - a short description (~20/30 characters)
+ * - a list of detail strings
+ *
+ * The short description is used to indicate how the detail strings should be
+ * interpreted. It usually indicates the failure types, such as
+ * "assertion failed", "forced failure", "unexpected exception caught",
+ * "equality assertion failed"... It should not contains new line character (\n).
+ *
+ * Detail strings are used to provide more information about the failure. It
+ * can contains the asserted expression, the expected and actual values in an
+ * equality assertion, some addional messages... Detail strings can contains
+ * new line characters (\n).
+ */
+class CPPUNIT_API Message
+{
+public:
+ Message();
+
+ explicit Message( const std::string &shortDescription );
+
+ Message( const std::string &shortDescription,
+ const std::string &detail1 );
+
+ Message( const std::string &shortDescription,
+ const std::string &detail1,
+ const std::string &detail2 );
+
+ Message( const std::string &shortDescription,
+ const std::string &detail1,
+ const std::string &detail2,
+ const std::string &detail3 );
+
+ /*! \brief Returns the short description.
+ * \return Short description.
+ */
+ const std::string &shortDescription() const;
+
+ /*! \brief Returns the number of detail string.
+ * \return Number of detail string.
+ */
+ int detailCount() const;
+
+ /*! \brief Returns the detail at the specified index.
+ * \param index Zero based index of the detail string to return.
+ * \returns Detail string at the specified index.
+ * \exception std::invalid_argument if \a index < 0 or index >= detailCount().
+ */
+ std::string detailAt( int index ) const;
+
+ /*! \brief Returns a string that represents a list of the detail strings.
+ *
+ * Example:
+ * \code
+ * Message message( "not equal", "Expected: 3", "Actual: 7" );
+ * std::string details = message.details();
+ * // details contains:
+ * // "- Expected: 3\n- Actual: 7\n" \endcode
+ *
+ * \return A string that is a concatenation of all the detail strings. Each detail
+ * string is prefixed with '- ' and suffixed with '\n' before being
+ * concatenated to the other.
+ */
+ std::string details() const;
+
+ /*! \brief Removes all detail strings.
+ */
+ void clearDetails();
+
+ /*! \brief Adds a single detail string.
+ * \param detail Detail string to add.
+ */
+ void addDetail( const std::string &detail );
+
+ /*! \brief Adds two detail strings.
+ * \param detail1 Detail string to add.
+ * \param detail2 Detail string to add.
+ */
+ void addDetail( const std::string &detail1,
+ const std::string &detail2 );
+
+ /*! \brief Adds three detail strings.
+ * \param detail1 Detail string to add.
+ * \param detail2 Detail string to add.
+ * \param detail3 Detail string to add.
+ */
+ void addDetail( const std::string &detail1,
+ const std::string &detail2,
+ const std::string &detail3 );
+
+ /*! \brief Adds the detail strings of the specified message.
+ * \param message All the detail strings of this message are added to this one.
+ */
+ void addDetail( const Message &message );
+
+ /*! \brief Sets the short description.
+ * \param shortDecription New short description.
+ */
+ void setShortDescription( const std::string &shortDescription );
+
+ /*! Tests if a message is identical to another one.
+ * \param other Message this message is compared to.
+ * \return \c true if the two message are identical, \c false otherwise.
+ */
+ bool operator ==( const Message &other ) const;
+
+
+ /*! Tests if a message is different from another one.
+ * \param other Message this message is compared to.
+ * \return \c true if the two message are not identical, \c false otherwise.
+ */
+ bool operator !=( const Message &other ) const;
+
+private:
+ std::string m_shortDescription;
+
+ typedef std::deque<std::string> Details;
+ Details m_details;
+};
+
+
+
+} // namespace CppUnit
+
+#if CPPUNIT_NEED_DLL_DECL
+#pragma warning( pop )
+#endif
+
+
+#endif // CPPUNIT_MESSAGE_H
diff --git a/include/cppunit/TestAssert.h b/include/cppunit/TestAssert.h
index 2f47f59..835fe9f 100644
--- a/include/cppunit/TestAssert.h
+++ b/include/cppunit/TestAssert.h
@@ -118,14 +118,15 @@ namespace CppUnit {
/** Assertions that a condition is \c true.
* \ingroup Assertions
*/
-#define CPPUNIT_ASSERT(condition) \
- ( ::CppUnit::Asserter::failIf( !(condition), \
- (#condition), \
+#define CPPUNIT_ASSERT(condition) \
+ ( ::CppUnit::Asserter::failIf( !(condition), \
+ ::CppUnit::Message( "assertion failed", \
+ "Expression: " #condition), \
CPPUNIT_SOURCELINE() ) )
#else
-#define CPPUNIT_ASSERT(condition) \
- ( ::CppUnit::Asserter::failIf( !(condition), \
- "", \
+#define CPPUNIT_ASSERT(condition) \
+ ( ::CppUnit::Asserter::failIf( !(condition), \
+ ::CppUnit::Message( "assertion failed" ), \
CPPUNIT_SOURCELINE() ) )
#endif
@@ -145,8 +146,9 @@ namespace CppUnit {
* \ingroup Assertions
* \param message Message reported in diagnostic.
*/
-#define CPPUNIT_FAIL( message ) \
- ( ::CppUnit::Asserter::fail( message, \
+#define CPPUNIT_FAIL( message ) \
+ ( ::CppUnit::Asserter::fail( ::CppUnit::Message( "forced failure", \
+ message ), \
CPPUNIT_SOURCELINE() ) )
#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
diff --git a/include/cppunit/TestCaller.h b/include/cppunit/TestCaller.h
index 7853019..3b10985 100644
--- a/include/cppunit/TestCaller.h
+++ b/include/cppunit/TestCaller.h
@@ -33,13 +33,13 @@ struct ExpectedExceptionTraits
static void expectedException()
{
#if CPPUNIT_USE_TYPEINFO_NAME
- std::string message( "Expected exception of type " );
- message += TypeInfoHelper::getClassName( typeid( ExceptionType ) );
- message += ", but got none";
+ throw Exception( Message(
+ "expected exception not thrown",
+ "Expected exception type: " +
+ TypeInfoHelper::getClassName( typeid( ExceptionType ) ) ) );
#else
- std::string message( "Expected exception but got none" );
+ throw Exception( "expected exception not thrown" );
#endif
- throw Exception( message );
}
};