summaryrefslogtreecommitdiff
path: root/examples
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 /examples
parent3788fccdd12ea1e3240d79e85d9146f6c264c6ec (diff)
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 'examples')
-rw-r--r--examples/cppunittest/MessageTest.cpp27
-rw-r--r--examples/cppunittest/MessageTest.h5
2 files changed, 32 insertions, 0 deletions
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 );