Shared parent names among their expectations when a mock::object is used as a base class

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@124 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2010-02-16 22:20:41 +00:00
parent 8a2030ce5f
commit c40535dc91
5 changed files with 49 additions and 24 deletions

View file

@ -282,7 +282,7 @@ namespace mock
std::string context() const
{
std::stringstream s;
s << name_;
s << parent_->tag() << name_;
serialize( s );
return s.str();
}

View file

@ -87,17 +87,39 @@ namespace detail
};
template< typename E >
E& set_parent( E& e, const object& o )
void set_parent( E& e, const std::string& prefix,
const std::string& name, const object& o )
{
o.set_parent( e );
return e;
o.tag( prefix );
e.tag( name );
}
template< typename E, typename T >
E& set_parent( E& e, const T&,
void set_parent( E& e, const std::string& prefix,
const std::string& name, const T&,
BOOST_DEDUCED_TYPENAME boost::disable_if<
BOOST_DEDUCED_TYPENAME boost::is_base_of< object, T >::type
>::type* = 0 )
{
e.tag( prefix + name );
}
template< typename E >
E& configure( BOOST_DEDUCED_TYPENAME E::expectation_tag,
const std::string& parent, const std::string& /*op*/,
const std::string& /*name*/, E& e )
{
if( parent != "?" || e.tag() == "?" )
e.tag( parent );
return e;
}
template< typename E, typename T >
E& configure( E& e, const std::string& parent, const std::string& op,
const std::string& name, const T& t )
{
if( parent != "?" || e.tag() == "?" )
set_parent( e, parent + op + type_name( typeid( T ) ) + "::",
name, t );
return e;
}
@ -122,25 +144,6 @@ namespace detail
return "->";
}
template< typename E >
E& configure( BOOST_DEDUCED_TYPENAME E::expectation_tag,
const std::string& parent, const std::string& /*op*/,
const std::string& /*name*/, E& e )
{
if( parent != "?" || e.tag() == "?" )
e.tag( parent );
return e;
}
template< typename E, typename T >
E& configure( E& e, const std::string& parent, const std::string& op,
const std::string& name, const T& t )
{
set_parent( e, t );
if( parent != "?" || e.tag() == "?" )
e.tag( parent + op + type_name( typeid( T ) ) + "::" + name );
return e;
}
template< typename T >
struct base
{

View file

@ -12,7 +12,6 @@
#include "verifiable.hpp"
#include <functional>
#include <algorithm>
#include <ostream>
#include <vector>
#include <string>
@ -44,6 +43,15 @@ namespace mock
std::mem_fun( &verifiable::reset ) );
}
void tag( const std::string& name )
{
name_ = name;
}
const std::string& tag() const
{
return name_;
}
protected:
virtual ~node()
{}
@ -53,6 +61,7 @@ namespace mock
typedef verifiables_type::const_iterator verifiables_cit;
std::vector< verifiable* > v_;
std::string name_;
};
}

View file

@ -29,6 +29,10 @@ namespace mock
{
t.set_parent( *impl_ );
}
void tag( const std::string& name ) const
{
impl_->tag( name );
}
bool verify() const
{

View file

@ -108,6 +108,7 @@ namespace
MOCK_CLASS( my_mock )
{
MOCK_CONST_METHOD_EXT( my_method, 1, void( int ), my_method )
MOCK_CONST_METHOD_EXT( my_method_2, 1, void( int ), my_method_2 )
};
}
@ -151,7 +152,9 @@ BOOST_AUTO_TEST_CASE( mock_object_is_named )
{
my_mock m;
BOOST_CHECK_EQUAL( "?.my_mock::my_method", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method ) ) );
BOOST_CHECK_EQUAL( "?.my_mock::my_method_2", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method_2 ) ) );
BOOST_CHECK_EQUAL( "m.my_mock::my_method", to_string( MOCK_MOCKER( m, my_method ) ) );
BOOST_CHECK_EQUAL( "m.my_mock::my_method_2", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method_2 ) ) );
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 ) ) );
}
@ -170,6 +173,7 @@ namespace
struct my_custom_mock
{
MOCK_METHOD_EXT( my_method, 0, void(), my_method )
MOCK_METHOD_EXT( my_method_2, 0, void(), my_method_2 )
};
}
@ -177,7 +181,9 @@ BOOST_AUTO_TEST_CASE( custom_mock_object_without_macros_and_without_inheriting_f
{
my_custom_mock m;
BOOST_CHECK_EQUAL( "?.my_custom_mock::my_method", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method ) ) );
BOOST_CHECK_EQUAL( "?.my_custom_mock::my_method_2", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method_2 ) ) );
BOOST_CHECK_EQUAL( "m.my_custom_mock::my_method", to_string( MOCK_MOCKER( m, my_method ) ) );
BOOST_CHECK_EQUAL( "?.my_custom_mock::my_method_2", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method_2 ) ) );
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 ) ) );
}
@ -187,6 +193,7 @@ namespace
struct my_custom_mock_object : mock::object
{
MOCK_METHOD_EXT( my_method, 0, void(), my_method )
MOCK_METHOD_EXT( my_method_2, 0, void(), my_method_2 )
};
}
@ -194,7 +201,9 @@ 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_ANONYMOUS_MOCKER( m, my_method ) ) );
BOOST_CHECK_EQUAL( "?.my_custom_mock_object::my_method_2", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method_2 ) ) );
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_2", to_string( MOCK_ANONYMOUS_MOCKER( m, my_method_2 ) ) );
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 ) ) );
}