diff --git a/src/libraries/turtle/constraints.hpp b/src/libraries/turtle/constraints.hpp index 4369e92..829d6c8 100644 --- a/src/libraries/turtle/constraints.hpp +++ b/src/libraries/turtle/constraints.hpp @@ -11,6 +11,7 @@ #include "constraint.hpp" #include "log.hpp" +#include #include #include #include @@ -75,11 +76,11 @@ namespace mock return detail::N< T >( t ); \ } - MOCK_CONSTRAINT(equal, actual == expected_) - MOCK_CONSTRAINT(less, actual < expected_) - MOCK_CONSTRAINT(greater, actual > expected_) - MOCK_CONSTRAINT(less_equal, actual <= expected_) - MOCK_CONSTRAINT(greater_equal, actual >= expected_) + MOCK_CONSTRAINT(equal, actual == boost::unwrap_ref( expected_ )) + MOCK_CONSTRAINT(less, actual < boost::unwrap_ref( expected_ )) + MOCK_CONSTRAINT(greater, actual > boost::unwrap_ref( expected_ )) + MOCK_CONSTRAINT(less_equal, actual <= boost::unwrap_ref( expected_ )) + MOCK_CONSTRAINT(greater_equal, actual >= boost::unwrap_ref( expected_ )) #undef MOCK_CONSTRAINT @@ -115,7 +116,7 @@ namespace detail boost::is_convertible< Expected*, Actual > >::type* = 0 ) const { - actual = expected_; + actual = boost::unwrap_ref( expected_ ); return true; } template< typename Actual > @@ -124,7 +125,7 @@ namespace detail boost::is_convertible< Expected, Actual > >::type* = 0 ) const { - *actual = expected_; + *actual = boost::unwrap_ref( expected_ ); return true; } friend std::ostream& operator<<( std::ostream& s, const assign& a ) @@ -173,7 +174,7 @@ namespace detail {} bool operator()( const std::string& actual ) const { - return actual.find( expected_ ) != std::string::npos; + return actual.find( boost::unwrap_ref( expected_ ) ) != std::string::npos; } friend std::ostream& operator<<( std::ostream& s, const contain& n ) { diff --git a/src/libraries/turtle/log.hpp b/src/libraries/turtle/log.hpp index 9def4bd..f521f8f 100644 --- a/src/libraries/turtle/log.hpp +++ b/src/libraries/turtle/log.hpp @@ -21,6 +21,7 @@ namespace assign_detail { template< typename T > class generic_list; } + template< typename T > class reference_wrapper; } namespace mock @@ -241,6 +242,12 @@ namespace detail mock::detail::serialize( s, t.begin(), t.end() ); return s; } + template< typename T > + stream& operator<<( stream& s, + const boost::reference_wrapper< T >& t ) + { + return s << mock::format( t.get() ); + } template< typename T > BOOST_DEDUCED_TYPENAME boost::enable_if< diff --git a/src/tests/turtle_test/constraints_test.cpp b/src/tests/turtle_test/constraints_test.cpp index 6ef92a1..4c7460d 100644 --- a/src/tests/turtle_test/constraints_test.cpp +++ b/src/tests/turtle_test/constraints_test.cpp @@ -44,6 +44,22 @@ BOOST_AUTO_TEST_CASE( constraints_can_be_combined_using_the_and_operator ) mock::less( 0 ) && mock::greater( 0 ); } +BOOST_AUTO_TEST_CASE( equal ) +{ + BOOST_CHECK( mock::equal( std::string( "string" ) ).f_( "string" ) ); + BOOST_CHECK( ! mock::equal( std::string( "string" ) ).f_( "not string" ) ); + { + std::string s; + mock::constraint< + mock::detail::equal< + boost::reference_wrapper< const std::string > + > + > c = mock::equal( boost::cref( s ) ); + s = "string"; + BOOST_CHECK( c.f_( "string" ) ); + } +} + BOOST_AUTO_TEST_CASE( same ) { int i = 0; @@ -71,6 +87,30 @@ BOOST_AUTO_TEST_CASE( assign ) BOOST_CHECK( mock::assign( &j ).f_( i ) ); BOOST_CHECK_EQUAL( i, &j ); } + { + int i = 0; + int j = 0; + mock::constraint< + mock::detail::assign< + boost::reference_wrapper< const int > + > + > c = mock::assign( boost::cref( j ) ); + j = 3; + BOOST_CHECK( c.f_( i ) ); + BOOST_CHECK_EQUAL( 3, i ); + } + { + int i = 0; + int j = 0; + mock::constraint< + mock::detail::assign< + boost::reference_wrapper< const int > + > + > c = mock::assign( boost::cref( j ) ); + j = 3; + BOOST_CHECK( c.f_( &i ) ); + BOOST_CHECK_EQUAL( 3, i ); + } } BOOST_AUTO_TEST_CASE( retrieve ) @@ -184,6 +224,20 @@ BOOST_AUTO_TEST_CASE( contain_with_const_char_ptr ) BOOST_CHECK( mock::contain( "string" ).f_( std::string( "this is a string" ) ) ); BOOST_CHECK( ! mock::contain( "not found" ).f_( "this is a string" ) ); BOOST_CHECK( ! mock::contain( "not found" ).f_( std::string( "this is a string" ) ) ); + { + char* s; + mock::constraint< + mock::detail::contain< + boost::reference_wrapper< char* const > + > + > c = mock::contain( boost::cref( s ) ); + s = "string"; + BOOST_CHECK( c.f_( "this is a string" ) ); + BOOST_CHECK( c.f_( std::string( "this is a string" ) ) ); + s = "not found"; + BOOST_CHECK( ! c.f_( "this is a string" ) ); + BOOST_CHECK( ! c.f_( std::string( "this is a string" ) ) ); + } } BOOST_AUTO_TEST_CASE( contain_with_strings ) @@ -192,4 +246,18 @@ BOOST_AUTO_TEST_CASE( contain_with_strings ) BOOST_CHECK( mock::contain( std::string( "string" ) ).f_( std::string( "this is a string" ) ) ); BOOST_CHECK( ! mock::contain( std::string( "not found" ) ).f_( "this is a string" ) ); BOOST_CHECK( ! mock::contain( std::string( "not found" ) ).f_( std::string( "this is a string" ) ) ); + { + std::string s; + mock::constraint< + mock::detail::contain< + boost::reference_wrapper< const std::string > + > + > c = mock::contain( boost::cref( s ) ); + s = "string"; + BOOST_CHECK( c.f_( "this is a string" ) ); + BOOST_CHECK( c.f_( std::string( "this is a string" ) ) ); + s = "not found"; + BOOST_CHECK( ! c.f_( "this is a string" ) ); + BOOST_CHECK( ! c.f_( std::string( "this is a string" ) ) ); + } } diff --git a/src/tests/turtle_test/log_with_conversions_test.cpp b/src/tests/turtle_test/log_with_conversions_test.cpp index 6262433..e462130 100644 --- a/src/tests/turtle_test/log_with_conversions_test.cpp +++ b/src/tests/turtle_test/log_with_conversions_test.cpp @@ -410,6 +410,12 @@ BOOST_AUTO_TEST_CASE( boost_assign_map_list_of_are_serialized_with_conversions ) BOOST_CHECK_EQUAL( "((12,\"12\"),(42,\"42\"))", to_string( boost::assign::map_list_of( 12, "12" )( 42, "42" ) ) ); } +BOOST_AUTO_TEST_CASE( boost_reference_wrappers_are_serialized_with_conversions ) +{ + BOOST_CHECK_EQUAL( "3", to_string( boost::cref( 3 ) ) ); + BOOST_CHECK_EQUAL( "\"string\"", to_string( boost::cref( "string" ) ) ); +} + namespace { void callable_builtin() diff --git a/src/tests/turtle_test/log_without_conversions_test.cpp b/src/tests/turtle_test/log_without_conversions_test.cpp index 84f75e8..b0cdbe5 100644 --- a/src/tests/turtle_test/log_without_conversions_test.cpp +++ b/src/tests/turtle_test/log_without_conversions_test.cpp @@ -406,6 +406,12 @@ BOOST_AUTO_TEST_CASE( boost_assign_map_list_of_are_serialized_without_conversion BOOST_CHECK_EQUAL( "((12,\"12\"),(42,\"42\"))", to_string( boost::assign::map_list_of( 12, "12" )( 42, "42" ) ) ); } +BOOST_AUTO_TEST_CASE( boost_reference_wrappers_are_serialized_without_conversions ) +{ + BOOST_CHECK_EQUAL( "3", to_string( boost::cref( 3 ) ) ); + BOOST_CHECK_EQUAL( "\"string\"", to_string( boost::cref( "string" ) ) ); +} + namespace { void callable_builtin()