Added support for boost::cref and boost::ref in built-in constraints and mock::format

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@283 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2011-04-05 21:08:36 +00:00
parent 5aa5cceb88
commit 57848d082c
5 changed files with 96 additions and 8 deletions

View file

@ -11,6 +11,7 @@
#include "constraint.hpp" #include "constraint.hpp"
#include "log.hpp" #include "log.hpp"
#include <boost/ref.hpp>
#include <boost/utility/enable_if.hpp> #include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_convertible.hpp> #include <boost/type_traits/is_convertible.hpp>
#include <boost/preprocessor/stringize.hpp> #include <boost/preprocessor/stringize.hpp>
@ -75,11 +76,11 @@ namespace mock
return detail::N< T >( t ); \ return detail::N< T >( t ); \
} }
MOCK_CONSTRAINT(equal, actual == expected_) MOCK_CONSTRAINT(equal, actual == boost::unwrap_ref( expected_ ))
MOCK_CONSTRAINT(less, actual < expected_) MOCK_CONSTRAINT(less, actual < boost::unwrap_ref( expected_ ))
MOCK_CONSTRAINT(greater, actual > expected_) MOCK_CONSTRAINT(greater, actual > boost::unwrap_ref( expected_ ))
MOCK_CONSTRAINT(less_equal, actual <= expected_) MOCK_CONSTRAINT(less_equal, actual <= boost::unwrap_ref( expected_ ))
MOCK_CONSTRAINT(greater_equal, actual >= expected_) MOCK_CONSTRAINT(greater_equal, actual >= boost::unwrap_ref( expected_ ))
#undef MOCK_CONSTRAINT #undef MOCK_CONSTRAINT
@ -115,7 +116,7 @@ namespace detail
boost::is_convertible< Expected*, Actual > boost::is_convertible< Expected*, Actual >
>::type* = 0 ) const >::type* = 0 ) const
{ {
actual = expected_; actual = boost::unwrap_ref( expected_ );
return true; return true;
} }
template< typename Actual > template< typename Actual >
@ -124,7 +125,7 @@ namespace detail
boost::is_convertible< Expected, Actual > boost::is_convertible< Expected, Actual >
>::type* = 0 ) const >::type* = 0 ) const
{ {
*actual = expected_; *actual = boost::unwrap_ref( expected_ );
return true; return true;
} }
friend std::ostream& operator<<( std::ostream& s, const assign& a ) friend std::ostream& operator<<( std::ostream& s, const assign& a )
@ -173,7 +174,7 @@ namespace detail
{} {}
bool operator()( const std::string& actual ) const 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 ) friend std::ostream& operator<<( std::ostream& s, const contain& n )
{ {

View file

@ -21,6 +21,7 @@ namespace assign_detail
{ {
template< typename T > class generic_list; template< typename T > class generic_list;
} }
template< typename T > class reference_wrapper;
} }
namespace mock namespace mock
@ -241,6 +242,12 @@ namespace detail
mock::detail::serialize( s, t.begin(), t.end() ); mock::detail::serialize( s, t.begin(), t.end() );
return s; return s;
} }
template< typename T >
stream& operator<<( stream& s,
const boost::reference_wrapper< T >& t )
{
return s << mock::format( t.get() );
}
template< typename T > template< typename T >
BOOST_DEDUCED_TYPENAME boost::enable_if< BOOST_DEDUCED_TYPENAME boost::enable_if<

View file

@ -44,6 +44,22 @@ BOOST_AUTO_TEST_CASE( constraints_can_be_combined_using_the_and_operator )
mock::less( 0 ) && mock::greater( 0 ); 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 ) BOOST_AUTO_TEST_CASE( same )
{ {
int i = 0; int i = 0;
@ -71,6 +87,30 @@ BOOST_AUTO_TEST_CASE( assign )
BOOST_CHECK( mock::assign( &j ).f_( i ) ); BOOST_CHECK( mock::assign( &j ).f_( i ) );
BOOST_CHECK_EQUAL( i, &j ); 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 ) 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( "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_( "this is a string" ) );
BOOST_CHECK( ! mock::contain( "not found" ).f_( std::string( "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 ) 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( "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_( "this is a string" ) );
BOOST_CHECK( ! mock::contain( std::string( "not found" ) ).f_( std::string( "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" ) ) );
}
} }

View file

@ -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_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 namespace
{ {
void callable_builtin() void callable_builtin()

View file

@ -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_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 namespace
{ {
void callable_builtin() void callable_builtin()