diff --git a/src/libraries/turtle/mock.hpp b/src/libraries/turtle/mock.hpp index 73ebf12..de87a21 100644 --- a/src/libraries/turtle/mock.hpp +++ b/src/libraries/turtle/mock.hpp @@ -29,6 +29,7 @@ #include #define BOOST_TYPEOF_SILENT #include +#include #include namespace mock @@ -86,45 +87,57 @@ namespace detail }; template< typename E > - void set_parent( E& e, const object& o ) + E& set_parent( E& e, const object& o ) { o.set_parent( e ); - } - template< typename E, typename T > - void set_parent( E&, const T&, - BOOST_DEDUCED_TYPENAME boost::disable_if< - BOOST_DEDUCED_TYPENAME boost::is_base_of< object, T >::type - >::type* = 0 ) - {} - template< typename E > - void tag( E& e, const object& o, const std::string& type_name, - const std::string& name ) - { - e.tag( type_name + o.tag() + "::" + name ); - } - template< typename E, typename T > - void tag( E& e, const T&, const std::string& type_name, - const std::string& name, - BOOST_DEDUCED_TYPENAME boost::disable_if< - BOOST_DEDUCED_TYPENAME boost::is_base_of< object, T >::type - >::type* = 0 ) - { - e.tag( type_name + "::" + name ); - } - - template< typename E > - E& configure( typename E::expectation_tag, const std::string& object, - const std::string& name, E& e ) - { - e.tag( name == "_" ? object : name ); return e; } template< typename E, typename T > - E& configure( E& e, const std::string& /*object*/, - const std::string& name, const T& t ) + E& set_parent( E& e, const T&, + BOOST_DEDUCED_TYPENAME boost::disable_if< + BOOST_DEDUCED_TYPENAME boost::is_base_of< object, T >::type + >::type* = 0 ) + { + return e; + } + + template< typename T > + std::string op( T& ) + { + return "."; + } + template< typename T > + std::string op( T* ) + { + return "->"; + } + template< typename T > + std::string op( std::auto_ptr< T >& ) + { + return "->"; + } + template< typename T > + std::string op( boost::shared_ptr< T >& ) + { + return "->"; + } + + template< typename E > + E& configure( BOOST_DEDUCED_TYPENAME E::expectation_tag, + const std::string& object, const std::string& /*op*/, + const std::string& /*name*/, E& e ) + { + if( object != "?" || e.tag() == "?" ) + e.tag( object ); + return e; + } + template< typename E, typename T > + E& configure( E& e, const std::string& object, const std::string& op, + const std::string& name, const T& t ) { set_parent( e, t ); - tag( e, t, type_name< T >(), name ); + if( object != "?" || e.tag() == "?" ) + e.tag( object + op + type_name< T >() + "::" + name ); return e; } @@ -145,7 +158,11 @@ namespace detail #define MOCK_MOCKER(o, t) \ mock::detail::configure( mock::detail::ref( o ).exp##t, \ - BOOST_PP_STRINGIZE(o), BOOST_PP_STRINGIZE(t), mock::detail::ref( o ) ) + BOOST_PP_STRINGIZE(o), mock::detail::op( o ), \ + BOOST_PP_STRINGIZE(t), mock::detail::ref( o ) ) +#define MOCK_ANONYMOUS_MOCKER(o, t) \ + mock::detail::configure( mock::detail::ref( o ).exp##t, \ + "?", ".", BOOST_PP_STRINGIZE(t), mock::detail::ref( o ) ) #define MOCK_METHOD_ARG(z, n, arg) BOOST_PP_COMMA_IF(n) \ BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)),_type) \ @@ -163,7 +180,7 @@ namespace detail tpn boost::function< S >::result_type M( \ MOCK_METHOD_ARGS(n, tpn boost::function< S >::arg) ) c \ { \ - return MOCK_MOCKER(this, t)( MOCK_MOCKER_ARGS(n) ); \ + return MOCK_ANONYMOUS_MOCKER(this, t)( MOCK_MOCKER_ARGS(n) ); \ } #define MOCK_SIGNATURE(M) \ mock::detail::signature< BOOST_TYPEOF(&base_type::M) >::type diff --git a/src/libraries/turtle/node.hpp b/src/libraries/turtle/node.hpp index 317dbb0..6745bc1 100644 --- a/src/libraries/turtle/node.hpp +++ b/src/libraries/turtle/node.hpp @@ -44,18 +44,10 @@ namespace mock std::mem_fun( &verifiable::reset ) ); } - friend std::ostream& operator<<( std::ostream& s, const node& n ) - { - n.serialize( s ); - return s; - } - protected: virtual ~node() {} - virtual void serialize( std::ostream& s ) const = 0; - private: typedef std::vector< verifiable* > verifiables_type; typedef verifiables_type::const_iterator verifiables_cit; diff --git a/src/libraries/turtle/object.hpp b/src/libraries/turtle/object.hpp index b524526..e796668 100644 --- a/src/libraries/turtle/object.hpp +++ b/src/libraries/turtle/object.hpp @@ -24,15 +24,6 @@ namespace mock : impl_( new object_impl() ) {} - void tag( const std::string& name ) - { - impl_->name_ = name; - } - const std::string& tag() const - { - return impl_->name_; - } - template< typename T > void set_parent( T& t ) const { @@ -60,14 +51,6 @@ namespace mock { root.remove( *this ); } - - std::string name_; - - private: - virtual void serialize( std::ostream& s ) const - { - s << (name_.empty() ? "?" : name_) << "::"; - } }; boost::shared_ptr< object_impl > impl_; diff --git a/src/libraries/turtle/root.hpp b/src/libraries/turtle/root.hpp index e6fb519..1b581f6 100644 --- a/src/libraries/turtle/root.hpp +++ b/src/libraries/turtle/root.hpp @@ -18,8 +18,6 @@ namespace mock class root_t : public boost::unit_test::singleton< root_t >, public node { private: - virtual void serialize( std::ostream& /*s*/ ) const - {} BOOST_TEST_SINGLETON_CONS( root_t ); }; BOOST_TEST_SINGLETON_INST( root ) diff --git a/src/tests/turtle_test/integration_test.cpp b/src/tests/turtle_test/integration_test.cpp index 44d39a0..2a07e9b 100644 --- a/src/tests/turtle_test/integration_test.cpp +++ b/src/tests/turtle_test/integration_test.cpp @@ -243,10 +243,6 @@ namespace struct fixture { - fixture() - { - manager.tag( "(the only one)" ); - } my_mock_manager manager; my_mock_observer observer; }; diff --git a/src/tests/turtle_test/mock_test.cpp b/src/tests/turtle_test/mock_test.cpp index 2ecf7c8..f841049 100644 --- a/src/tests/turtle_test/mock_test.cpp +++ b/src/tests/turtle_test/mock_test.cpp @@ -152,14 +152,19 @@ namespace BOOST_AUTO_TEST_CASE( mock_object_is_named ) { my_mock m; - BOOST_CHECK_EQUAL( "my_mock::my_method", to_string( MOCK_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "?.my_mock::my_method", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "m.my_mock::my_method", to_string( MOCK_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "m.my_mock::my_method", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "m.my_mock::my_method", to_string( MOCK_MOCKER( m, my_method ) ) ); } -BOOST_AUTO_TEST_CASE( mock_object_with_tag_is_named ) +BOOST_AUTO_TEST_CASE( mock_object_pointer_is_named ) { - my_mock m; - m.tag( "(my tag)" ); - BOOST_CHECK_EQUAL( "my_mock(my tag)::my_method", to_string( MOCK_MOCKER( m, my_method ) ) ); + std::auto_ptr< my_mock > m( new my_mock ); + BOOST_CHECK_EQUAL( "?.my_mock::my_method", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_MOCKER( m, my_method ) ) ); } namespace @@ -173,7 +178,10 @@ namespace BOOST_AUTO_TEST_CASE( custom_mock_object_without_macros_and_without_inheriting_from_object_is_named ) { my_custom_mock m; - BOOST_CHECK_EQUAL( "my_custom_mock::my_method", to_string( MOCK_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "?.my_custom_mock::my_method", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "m.my_custom_mock::my_method", to_string( MOCK_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "m.my_custom_mock::my_method", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "m.my_custom_mock::my_method", to_string( MOCK_MOCKER( m, my_method ) ) ); } namespace @@ -187,7 +195,10 @@ namespace BOOST_AUTO_TEST_CASE( custom_mock_object_without_macros_is_named ) { my_custom_mock_object m; - BOOST_CHECK_EQUAL( "my_custom_mock_object::my_method", to_string( MOCK_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "?.my_custom_mock_object::my_method", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "m.my_custom_mock_object::my_method", to_string( MOCK_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "m.my_custom_mock_object::my_method", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method ) ) ); + BOOST_CHECK_EQUAL( "m.my_custom_mock_object::my_method", to_string( MOCK_MOCKER( m, my_method ) ) ); } BOOST_AUTO_TEST_CASE( mock_functor_is_named ) @@ -196,13 +207,6 @@ BOOST_AUTO_TEST_CASE( mock_functor_is_named ) BOOST_CHECK_EQUAL( "f", to_string( MOCK_MOCKER( f, _ ) ) ); } -BOOST_AUTO_TEST_CASE( mock_functor_with_tag_has_no_effect ) -{ - MOCK_FUNCTOR( int( float, const std::string& ) ) f; - f.tag( "my functor" ); - BOOST_CHECK_EQUAL( "f", to_string( MOCK_MOCKER( f, _ ) ) ); -} - namespace { MOCK_CLASS( my_destroyed_class )