summaryrefslogtreecommitdiff
path: root/include/cppunit/TestAssert.h
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2017-05-26 12:05:06 +0200
committerMarkus Mohrhard <markus.mohrhard@googlemail.com>2019-02-14 22:33:13 +0800
commit1caa3a0c66f009fe1a386d7559dc25054dad42a3 (patch)
tree6b778a26c9603cc7e753b485a986dc17374994e3 /include/cppunit/TestAssert.h
parent3788fccdd12ea1e3240d79e85d9146f6c264c6ec (diff)
downloadcppunit-1caa3a0c66f009fe1a386d7559dc25054dad42a3.tar.gz
custom tostring formatter to CPPUNIT_ASSERT_MESSAGE
Provide an extension trait message_to_string that allows client code to call ASSERT_MESSAGE with their own string types. Also provide a default extension trait that accepts std::ostream messages. Change-Id: I516f97063c34d86bc91c40e0ec147d5393e7b6ea
Diffstat (limited to 'include/cppunit/TestAssert.h')
-rw-r--r--include/cppunit/TestAssert.h46
1 files changed, 40 insertions, 6 deletions
diff --git a/include/cppunit/TestAssert.h b/include/cppunit/TestAssert.h
index bb29ac0..1e17868 100644
--- a/include/cppunit/TestAssert.h
+++ b/include/cppunit/TestAssert.h
@@ -119,6 +119,40 @@ struct assertion_traits<double>
};
+/*! \brief Message traits used by CPPUNIT_ASSERT* macros.
+ *
+ * Here is an example of specialising these traits:
+ *
+ * \code
+ * CPPUNIT_NS_BEGIN
+ * static std::string message_to_string(const MyType& m)
+ * {
+ * return m.getStr();
+ * };
+ * CPPUNIT_NS_END
+ * \endcode
+ */
+inline std::string message_to_string( const std::string& s )
+{
+ return s;
+}
+inline std::string message_to_string( const OStream& out )
+{
+ OStringStream ost;
+ ost << out.rdbuf();
+ return ost.str();
+}
+/// for calls to addDetail
+inline AdditionalMessage message_to_string( const AdditionalMessage& msg )
+{
+ return msg;
+}
+/// otherwise calls with string literals are ambiguous
+inline std::string message_to_string( const char * s )
+{
+ return s;
+}
+
/*! \brief (Implementation) Asserts that two objects of the same type are equals.
* Use CPPUNIT_ASSERT_EQUAL instead of this function.
* \sa assertion_traits, Asserter::failNotEqual().
@@ -260,7 +294,7 @@ void assertGreaterEqual( const T& expected,
CPPUNIT_NS::Message( "assertion failed", \
"Expression: " \
#condition, \
- message ), \
+ CPPUNIT_NS::message_to_string(message) ), \
CPPUNIT_SOURCELINE() ) )
/** Fails with the specified message.
@@ -269,7 +303,7 @@ void assertGreaterEqual( const T& expected,
*/
#define CPPUNIT_FAIL( message ) \
( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure", \
- message ), \
+ CPPUNIT_NS::message_to_string(message) ), \
CPPUNIT_SOURCELINE() ) )
#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
@@ -323,7 +357,7 @@ void assertGreaterEqual( const T& expected,
( CPPUNIT_NS::assertEquals( (expected), \
(actual), \
CPPUNIT_SOURCELINE(), \
- (message) ) )
+ CPPUNIT_NS::message_to_string(message) ) )
#endif
/** Asserts that actual is less than expected, provides additional message on failure.
@@ -457,7 +491,7 @@ void assertGreaterEqual( const T& expected,
(actual), \
(delta), \
CPPUNIT_SOURCELINE(), \
- (message) ) )
+ CPPUNIT_NS::message_to_string(message) ) )
/** Asserts that the given expression throws an exception of the specified type.
@@ -499,7 +533,7 @@ void assertGreaterEqual( const T& expected,
do { \
bool cpputCorrectExceptionThrown_ = false; \
CPPUNIT_NS::Message cpputMsg_( "expected exception not thrown" ); \
- cpputMsg_.addDetail( message ); \
+ cpputMsg_.addDetail( CPPUNIT_NS::message_to_string(message) ); \
cpputMsg_.addDetail( "Expected: " \
CPPUNIT_GET_PARAMETER_STRING( ExceptionType ) ); \
\
@@ -551,7 +585,7 @@ void assertGreaterEqual( const T& expected,
# define CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, expression ) \
do { \
CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" ); \
- cpputMsg_.addDetail( message ); \
+ cpputMsg_.addDetail( CPPUNIT_NS::message_to_string(message) ); \
\
try { \
expression; \