Refactoring

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@57 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2009-09-27 21:19:52 +00:00
parent 30480d031a
commit a1ea17ff9f
5 changed files with 79 additions and 60 deletions

View file

@ -50,14 +50,18 @@ namespace mock
{}; {};
expectation_tag exp_; expectation_tag exp_;
expectation( const std::string& name = "?" ) expectation()
: impl_( new expectation_impl( root, name ) ) : impl_( new expectation_impl() )
{} {}
void tag( const std::string& name ) void tag( const std::string& name )
{ {
impl_->tag( name ); impl_->tag( name );
} }
const std::string& tag() const
{
return impl_->tag();
}
void set_parent( node& parent ) void set_parent( node& parent )
{ {
impl_->set_parent( parent ); impl_->set_parent( parent );
@ -104,9 +108,9 @@ namespace mock
class expectation_impl : private verifiable class expectation_impl : private verifiable
{ {
public: public:
expectation_impl( node& parent, const std::string& name ) expectation_impl()
: name_( name ) : name_( "?" )
, parent_( &parent ) , parent_( &root )
, valid_( true ) , valid_( true )
{ {
parent_->add( *this ); parent_->add( *this );
@ -126,6 +130,10 @@ namespace mock
{ {
name_ = name; name_ = name;
} }
const std::string& tag() const
{
return name_;
}
void set_parent( node& parent ) void set_parent( node& parent )
{ {
parent_->remove( *this ); parent_->remove( *this );

View file

@ -97,12 +97,14 @@ namespace detail
>::type* = 0 ) >::type* = 0 )
{} {}
template< typename E > template< typename E >
void tag( E& e, const object& o, const std::string& type_name, const std::string& name ) void tag( E& e, const object& o, const std::string& type_name,
const std::string& name )
{ {
e.tag( type_name + o.tag() + "::" + name ); e.tag( type_name + o.tag() + "::" + name );
} }
template< typename E, typename T > template< typename E, typename T >
void tag( E& e, const T&, const std::string& type_name, const std::string& name, 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::disable_if<
BOOST_DEDUCED_TYPENAME boost::is_base_of< object, T >::type BOOST_DEDUCED_TYPENAME boost::is_base_of< object, T >::type
>::type* = 0 ) >::type* = 0 )
@ -111,13 +113,15 @@ namespace detail
} }
template< typename E > template< typename E >
E& configure( typename E::expectation_tag, const std::string& name, E& e ) E& configure( typename E::expectation_tag, const std::string& object,
const std::string& name, E& e )
{ {
e.tag( name ); e.tag( name == "_" ? object : name );
return e; return e;
} }
template< typename E, typename T > template< typename E, typename T >
E& configure( E& e, const std::string& name, const T& t ) E& configure( E& e, const std::string& /*object*/,
const std::string& name, const T& t )
{ {
set_parent( e, t ); set_parent( e, t );
tag( e, t, type_name< T >(), name ); tag( e, t, type_name< T >(), name );
@ -129,12 +133,6 @@ namespace detail
{ {
typedef T base_type; typedef T base_type;
}; };
inline std::string name( const std::string& object,
const std::string& tag )
{
return tag == "_" ? object : tag;
}
} }
} }
@ -147,8 +145,7 @@ namespace detail
#define MOCK_MOCKER(o, t) \ #define MOCK_MOCKER(o, t) \
mock::detail::configure( mock::detail::ref( o ).exp##t, \ mock::detail::configure( mock::detail::ref( o ).exp##t, \
mock::detail::name( BOOST_PP_STRINGIZE(o), BOOST_PP_STRINGIZE(t) ), \ BOOST_PP_STRINGIZE(o), BOOST_PP_STRINGIZE(t), mock::detail::ref( o ) )
mock::detail::ref( o ) )
#define MOCK_METHOD_ARG(z, n, arg) BOOST_PP_COMMA_IF(n) \ #define MOCK_METHOD_ARG(z, n, arg) BOOST_PP_COMMA_IF(n) \
BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)),_type) \ BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)),_type) \

View file

@ -594,7 +594,8 @@ namespace
BOOST_AUTO_TEST_CASE( expectation_can_be_serialized_to_be_human_readable ) BOOST_AUTO_TEST_CASE( expectation_can_be_serialized_to_be_human_readable )
{ {
{ {
mock::expectation< void( int ) > e( "my expectation" ); mock::expectation< void( int ) > e;
e.tag( "my expectation" );
e.expect().once().with( 1 ); e.expect().once().with( 1 );
e.expect().once().with( 2 ); e.expect().once().with( 2 );
BOOST_CHECK_NO_THROW( e( 2 ) ); BOOST_CHECK_NO_THROW( e( 2 ) );
@ -605,7 +606,8 @@ BOOST_AUTO_TEST_CASE( expectation_can_be_serialized_to_be_human_readable )
e.reset(); e.reset();
} }
{ {
mock::expectation< void( int ) > e( "my expectation" ); mock::expectation< void( int ) > e;
e.tag( "my expectation" );
e.expect().never().with( 1 ); e.expect().never().with( 1 );
const std::string expected = "my expectation\n" const std::string expected = "my expectation\n"
"v expect( never() ).with( 1 )"; "v expect( never() ).with( 1 )";
@ -633,7 +635,8 @@ BOOST_AUTO_TEST_CASE( expectation_can_be_serialized_to_be_human_readable )
e.reset(); e.reset();
} }
{ {
mock::expectation< void( int ) > e( "my expectation" ); mock::expectation< void( int ) > e;
e.tag( "my expectation" );
e.expect().once(); e.expect().once();
const std::string expected = "my expectation\n" const std::string expected = "my expectation\n"
". expect( once() ).with( any )"; ". expect( once() ).with( any )";
@ -641,7 +644,8 @@ BOOST_AUTO_TEST_CASE( expectation_can_be_serialized_to_be_human_readable )
e.reset(); e.reset();
} }
{ {
mock::expectation< void( int ) > e( "my expectation" ); mock::expectation< void( int ) > e;
e.tag( "my expectation" );
e.expect().once().with( mock::any ); e.expect().once().with( mock::any );
const std::string expected = "my expectation\n" const std::string expected = "my expectation\n"
". expect( once() ).with( any )"; ". expect( once() ).with( any )";
@ -649,7 +653,8 @@ BOOST_AUTO_TEST_CASE( expectation_can_be_serialized_to_be_human_readable )
e.reset(); e.reset();
} }
{ {
mock::expectation< void( int ) > e( "my expectation" ); mock::expectation< void( int ) > e;
e.tag( "my expectation" );
e.expect().once(); e.expect().once();
const std::string expected = "my expectation\n" const std::string expected = "my expectation\n"
". expect( once() ).with( any )"; ". expect( once() ).with( any )";
@ -657,7 +662,8 @@ BOOST_AUTO_TEST_CASE( expectation_can_be_serialized_to_be_human_readable )
e.reset(); e.reset();
} }
{ {
mock::expectation< void( int ) > e( "my expectation" ); mock::expectation< void( int ) > e;
e.tag( "my expectation" );
e.expect().once().with( &custom_constraint ); e.expect().once().with( &custom_constraint );
const std::string expected = "my expectation\n" const std::string expected = "my expectation\n"
". expect( once() ).with( ? )"; ". expect( once() ).with( ? )";
@ -665,7 +671,8 @@ BOOST_AUTO_TEST_CASE( expectation_can_be_serialized_to_be_human_readable )
e.reset(); e.reset();
} }
{ {
mock::expectation< void( int ) > e( "my expectation" ); mock::expectation< void( int ) > e;
e.tag( "my expectation" );
e.expect().once().with( mock::constraint( &custom_constraint, "custom constraint" ) ); e.expect().once().with( mock::constraint( &custom_constraint, "custom constraint" ) );
const std::string expected = "my expectation\n" const std::string expected = "my expectation\n"
". expect( once() ).with( custom constraint )"; ". expect( once() ).with( custom constraint )";
@ -673,7 +680,8 @@ BOOST_AUTO_TEST_CASE( expectation_can_be_serialized_to_be_human_readable )
e.reset(); e.reset();
} }
{ {
mock::expectation< void( int ) > e( "my expectation" ); mock::expectation< void( int ) > e;
e.tag( "my expectation" );
e.expect().once().with( mock::constraint( &custom_constraint, true ) ); e.expect().once().with( mock::constraint( &custom_constraint, true ) );
const std::string expected = "my expectation\n" const std::string expected = "my expectation\n"
". expect( once() ).with( true )"; ". expect( once() ).with( true )";
@ -684,13 +692,31 @@ BOOST_AUTO_TEST_CASE( expectation_can_be_serialized_to_be_human_readable )
namespace namespace
{ {
mock::expectation< void() > no_match_exp( "no_match" ); mock::expectation< void() > no_match_exp;
mock::expectation< void() > sequence_failed_exp( "sequence_failed" ); mock::expectation< void() > sequence_failed_exp;
mock::expectation< void() > verification_failed_exp( "verification_failed" ); mock::expectation< void() > verification_failed_exp;
mock::expectation< void() > untriggered_expectation_exp( "untriggered_expectation" ); mock::expectation< void() > untriggered_expectation_exp;
struct mock_error struct error_fixture
{ {
error_fixture()
{
no_match_exp.tag( "no_match" );
sequence_failed_exp.tag( "sequence_failed" );
verification_failed_exp.tag( "verification_failed" );
untriggered_expectation_exp.tag( "untriggered_expectation" );
}
~error_fixture()
{
no_match_exp.verify();
no_match_exp.reset();
sequence_failed_exp.verify();
sequence_failed_exp.reset();
verification_failed_exp.verify();
verification_failed_exp.reset();
untriggered_expectation_exp.verify();
untriggered_expectation_exp.reset();
}
static void no_match( const std::string& /*context*/ ) static void no_match( const std::string& /*context*/ )
{ {
no_match_exp(); no_match_exp();
@ -711,58 +737,44 @@ namespace
untriggered_expectation_exp(); untriggered_expectation_exp();
} }
}; };
struct error_guard
{
~error_guard()
{
no_match_exp.verify();
no_match_exp.reset();
sequence_failed_exp.verify();
sequence_failed_exp.reset();
verification_failed_exp.verify();
verification_failed_exp.reset();
untriggered_expectation_exp.verify();
untriggered_expectation_exp.reset();
}
};
} }
BOOST_FIXTURE_TEST_CASE( expectation_with_remaining_untriggered_matches_notifies_an_error_upon_destructions, error_guard ) BOOST_FIXTURE_TEST_CASE( expectation_with_remaining_untriggered_matches_notifies_an_error_upon_destructions, error_fixture )
{ {
mock::expectation< void(), mock_error > e; mock::expectation< void(), error_fixture > e;
e.expect().once(); e.expect().once();
untriggered_expectation_exp.expect().once(); untriggered_expectation_exp.expect().once();
} }
BOOST_FIXTURE_TEST_CASE( verifying_expectation_with_remaining_matches_disables_the_automatic_verification_upon_destruction, error_guard ) BOOST_FIXTURE_TEST_CASE( verifying_expectation_with_remaining_matches_disables_the_automatic_verification_upon_destruction, error_fixture )
{ {
mock::expectation< void(), mock_error > e; mock::expectation< void(), error_fixture > e;
e.expect().once(); e.expect().once();
verification_failed_exp.expect(); verification_failed_exp.expect();
e.verify(); e.verify();
} }
BOOST_FIXTURE_TEST_CASE( triggering_no_match_call_disables_the_automatic_verification_upon_destruction, error_guard ) BOOST_FIXTURE_TEST_CASE( triggering_no_match_call_disables_the_automatic_verification_upon_destruction, error_fixture )
{ {
mock::expectation< void(), mock_error > e; mock::expectation< void(), error_fixture > e;
no_match_exp.expect(); no_match_exp.expect();
e(); e();
} }
BOOST_FIXTURE_TEST_CASE( adding_a_matcher_reactivates_the_verification_upon_destruction, error_guard ) BOOST_FIXTURE_TEST_CASE( adding_a_matcher_reactivates_the_verification_upon_destruction, error_fixture )
{ {
mock::expectation< void(), mock_error > e; mock::expectation< void(), error_fixture > e;
no_match_exp.expect(); no_match_exp.expect();
e(); e();
e.expect().once(); e.expect().once();
untriggered_expectation_exp.expect().once(); untriggered_expectation_exp.expect().once();
} }
BOOST_FIXTURE_TEST_CASE( throwing_an_exception_disables_the_automatic_verification_upon_destruction, error_guard ) BOOST_FIXTURE_TEST_CASE( throwing_an_exception_disables_the_automatic_verification_upon_destruction, error_fixture )
{ {
try try
{ {
mock::expectation< void(), mock_error > e; mock::expectation< void(), error_fixture > e;
e.expect().once(); e.expect().once();
throw std::exception(); throw std::exception();
} }

View file

@ -136,11 +136,6 @@ BOOST_AUTO_TEST_CASE( mock_functor_in_function_is_supported )
BOOST_CHECK_EQUAL( 42, func( 3, "op" ) ); BOOST_CHECK_EQUAL( 42, func( 3, "op" ) );
} }
BOOST_AUTO_TEST_CASE( mock_functor_name_can_be_customised )
{
MOCK_FUNCTOR( int( float, const std::string& ) ) f( "my functor" );
}
namespace namespace
{ {
struct functor_fixture struct functor_fixture

View file

@ -199,3 +199,10 @@ BOOST_AUTO_TEST_CASE( mock_functor_is_named )
MOCK_FUNCTOR( void() ) f; MOCK_FUNCTOR( void() ) f;
BOOST_CHECK_EQUAL( "f", to_string( MOCK_MOCKER( f, _ ) ) ); 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, _ ) ) );
}