[Libreoffice-commits] cppunit.git: examples/cppunittest include/cppunit

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Thu Feb 14 14:37:13 UTC 2019


 examples/cppunittest/MessageTest.cpp |   27 ++++++++++++++++++++
 examples/cppunittest/MessageTest.h   |    5 +++
 include/cppunit/TestAssert.h         |   46 ++++++++++++++++++++++++++++++-----
 3 files changed, 72 insertions(+), 6 deletions(-)

New commits:
commit 1caa3a0c66f009fe1a386d7559dc25054dad42a3
Author:     Noel Grandin <noel.grandin at collabora.co.uk>
AuthorDate: Fri May 26 12:05:06 2017 +0200
Commit:     Markus Mohrhard <markus.mohrhard at googlemail.com>
CommitDate: Thu Feb 14 22:33:13 2019 +0800

    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

diff --git a/examples/cppunittest/MessageTest.cpp b/examples/cppunittest/MessageTest.cpp
index c59544d..c0674b8 100644
--- a/examples/cppunittest/MessageTest.cpp
+++ b/examples/cppunittest/MessageTest.cpp
@@ -232,3 +232,30 @@ MessageTest::testNotEqual()
   CPPUNIT_ASSERT( message1 != message2 );
   CPPUNIT_ASSERT( !(message1 != message1) );
 }
+
+
+struct Foo
+{
+    std::string s;
+};
+CPPUNIT_NS_BEGIN
+static std::string message_to_string(const Foo& m)
+{
+    return m.s;
+}
+CPPUNIT_NS_END
+
+void
+MessageTest::testCustomMessageType()
+{
+  Foo foo { "xxxx" };
+  CPPUNIT_ASSERT_MESSAGE( foo, true );
+}
+
+void
+MessageTest::testOStreamMessage()
+{
+  std::ostringstream ost;
+  ost << "xxx" << "yyy";
+  CPPUNIT_ASSERT_MESSAGE( ost, true );
+}
diff --git a/examples/cppunittest/MessageTest.h b/examples/cppunittest/MessageTest.h
index 4c757e8..6fc2237 100644
--- a/examples/cppunittest/MessageTest.h
+++ b/examples/cppunittest/MessageTest.h
@@ -28,6 +28,8 @@ class MessageTest : public CPPUNIT_NS::TestFixture
   CPPUNIT_TEST( testDetailsSome );
   CPPUNIT_TEST( testEqual );
   CPPUNIT_TEST( testNotEqual );
+  CPPUNIT_TEST( testCustomMessageType );
+  CPPUNIT_TEST( testOStreamMessage );
   CPPUNIT_TEST_SUITE_END();
 
 public:
@@ -60,6 +62,9 @@ public:
   void testEqual();
   void testNotEqual();
 
+  void testCustomMessageType();
+  void testOStreamMessage();
+
 private:
   /// Prevents the use of the copy constructor.
   MessageTest( const MessageTest &other );
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;                                                          \


More information about the Libreoffice-commits mailing list