From 506e10f76aa02b8c3abeea0a0d0ced554dff192d Mon Sep 17 00:00:00 2001 From: Mathieu Champlon Date: Mon, 8 Feb 2016 05:40:39 +0100 Subject: [PATCH] Added logging support for boost::optional --- doc/changelog.qbk | 1 + include/turtle/log.hpp | 13 +++++++++++++ test/test_log.cpp | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/doc/changelog.qbk b/doc/changelog.qbk index 6aeb960..44f4815 100644 --- a/doc/changelog.qbk +++ b/doc/changelog.qbk @@ -14,6 +14,7 @@ Not yet released * Fixed build errors with Boost 1.59 * Fixed multiply defined symbol definition for MOCK_FUNCTION included from several translation units * Fixed extra semicolon warning with BOOST_GLOBAL_FIXTURE prior to Boost 1.59 +* Added logging support for boost::optional [endsect] diff --git a/include/turtle/log.hpp b/include/turtle/log.hpp index 2680365..0faff6a 100644 --- a/include/turtle/log.hpp +++ b/include/turtle/log.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include namespace boost @@ -22,6 +23,7 @@ namespace boost template< typename T > class shared_ptr; template< typename T > class weak_ptr; template< typename T > class reference_wrapper; + template< typename T > class optional; namespace phoenix { @@ -127,6 +129,17 @@ namespace detail { return s << mock::format( t.lock() ); } + inline stream& operator<<( stream& s, const boost::none_t& ) + { + return s << "none"; + } + template< typename T > + stream& operator<<( stream& s, const boost::optional< T >& t ) + { + if( t ) + return s << mock::format( t.get() ); + return s << boost::none; + } #ifdef MOCK_SMART_PTR template< typename T > diff --git a/test/test_log.cpp b/test/test_log.cpp index e39c385..44fdd31 100644 --- a/test/test_log.cpp +++ b/test/test_log.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #ifdef BOOST_MSVC #pragma warning( push, 0 ) #endif @@ -668,3 +669,12 @@ BOOST_AUTO_TEST_CASE( nullptr_is_serialized ) } #endif + +BOOST_AUTO_TEST_CASE( mock_boost_optional_yields_its_value_when_serialized ) +{ + BOOST_CHECK_EQUAL( "7", to_string( boost::optional< int >( 7 ) ) ); + BOOST_CHECK_EQUAL( "?", to_string( boost::optional< non_serializable >( non_serializable() ) ) ); + BOOST_CHECK_EQUAL( "none", to_string( boost::optional< int >() ) ); + BOOST_CHECK_EQUAL( "none", to_string( boost::optional< non_serializable >() ) ); + BOOST_CHECK_EQUAL( "none", to_string( boost::none ) ); +}