Renamed error policies no_match method to unexpected_call

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@152 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2010-05-07 12:01:07 +00:00
parent aa816535b4
commit 348c60a757
5 changed files with 91 additions and 101 deletions

View file

@ -26,100 +26,90 @@ namespace mock
struct exception : public boost::execution_aborted struct exception : public boost::execution_aborted
{}; {};
namespace detail
{
template< typename Result >
Result abort()
{
throw boost::enable_current_exception( exception() );
}
inline void fail(
const std::string& message, const std::string& context,
const std::string& file = "unknown location", int line = 0 )
{
boost::unit_test::framework::assertion_result( false );
boost::unit_test::unit_test_log
<< boost::unit_test::log::begin( file, (std::size_t)line )
<< boost::unit_test::log_all_errors
<< boost::unit_test::lazy_ostream::instance()
<< "mock error: " << message << ": " << context
<< boost::unit_test::log::end();
}
}
template< typename Result > template< typename Result >
struct boost_test_error_policy struct boost_test_error_policy
{ {
static Result abort()
{
throw boost::enable_current_exception( exception() );
}
static void missing_action( const std::string& context,
const std::string& file, int line )
{
fail( "mock error : missing result specification : " + context,
file, line );
}
static void no_match( const std::string& context )
{
fail( "mock error : unexpected call : " + context,
"unknown location", 0 );
}
static void sequence_failed( const std::string& context,
const std::string& /*file*/, int /*line*/ )
{
fail( "mock error : sequence failed : " + context,
"unknown location", 0 );
}
static void verification_failed( const std::string& context,
const std::string& file, int line )
{
fail( "verification failed : " + context, file, line );
}
static void untriggered_expectation( const std::string& context,
const std::string& file, int line )
{
fail( "untriggered expectation : " + context, file, line );
}
static void fail( const std::string& message,
const std::string& file, int line )
{
boost::unit_test::framework::assertion_result( false );
boost::unit_test::unit_test_log
<< boost::unit_test::log::begin( file, (std::size_t)line )
<< boost::unit_test::log_all_errors
<< boost::unit_test::lazy_ostream::instance() << message
<< boost::unit_test::log::end();
}
};
#else // MOCK_USE_BOOST_TEST #else // MOCK_USE_BOOST_TEST
struct exception struct exception
{}; {};
#endif // MOCK_USE_BOOST_TEST namespace detail
{
template< typename Result >
Result abort()
{
throw exception();
}
inline void fail(
const std::string& message, const std::string& context,
const std::string& file = "unknown location", int line = 0 )
{
std::cerr << file << '(' << line << "): "
<< "mock error: " << message << ": " << context << std::endl;
}
}
template< typename Result > template< typename Result >
struct basic_error_policy struct basic_error_policy
{ {
#endif // MOCK_USE_BOOST_TEST
static Result abort() static Result abort()
{ {
throw exception(); return mock::detail::abort< Result >();
} }
static void missing_action( const std::string& context, static void missing_action( const std::string& context,
const std::string& file, int line ) const std::string& file, int line )
{ {
log( "missing result specification", context, file, line ); mock::detail::fail( "missing action", context,
file, line );
} }
static void no_match( const std::string& context ) static void unexpected_call( const std::string& context )
{ {
log( "unexpected call", context ); mock::detail::fail( "unexpected call", context );
} }
static void sequence_failed( const std::string& context, static void sequence_failed( const std::string& context,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{ {
log( "sequence failed", context ); mock::detail::fail( "sequence failed", context );
} }
static void verification_failed( const std::string& context, static void verification_failed( const std::string& context,
const std::string& file, int line ) const std::string& file, int line )
{ {
log( "verification failed", context, file, line ); mock::detail::fail( "verification failed", context, file, line );
} }
static void untriggered_expectation( const std::string& context, static void untriggered_expectation( const std::string& context,
const std::string& file, int line ) const std::string& file, int line )
{ {
log( "untriggered expectation", context, file, line ); mock::detail::fail( "untriggered expectation", context,
} file, line );
static void log( const std::string& message,
const std::string& context,
const std::string& file = "unknown location", int line = 0 )
{
std::cerr << file << '(' << line << "): "
<< "mock error: " << message << ": " << context << std::endl;
} }
}; };
} }

View file

@ -226,7 +226,7 @@ namespace mock
return it->functor()( BOOST_PP_ENUM_PARAMS(n, p) ); \ return it->functor()( BOOST_PP_ENUM_PARAMS(n, p) ); \
} \ } \
valid_ = false; \ valid_ = false; \
ErrorPolicy::no_match( MOCK_EXPECTATION_CONTEXT(n) ); \ ErrorPolicy::unexpected_call( MOCK_EXPECTATION_CONTEXT(n) ); \
return ErrorPolicy::abort(); \ return ErrorPolicy::abort(); \
} }
BOOST_PP_REPEAT_FROM_TO(1, MOCK_NUM_ARGS, MOCK_EXPECTATION_OPERATOR, BOOST_PP_EMPTY) BOOST_PP_REPEAT_FROM_TO(1, MOCK_NUM_ARGS, MOCK_EXPECTATION_OPERATOR, BOOST_PP_EMPTY)
@ -263,7 +263,7 @@ namespace mock
return it->functor()(); return it->functor()();
} }
valid_ = false; valid_ = false;
ErrorPolicy::no_match( context( "" ) ); ErrorPolicy::unexpected_call( context( "" ) );
return T::abort(); return T::abort();
} }

View file

@ -37,7 +37,7 @@ namespace
void reset() void reset()
{ {
missing_action_count = 0; missing_action_count = 0;
no_match_count = 0; unexpected_call_count = 0;
sequence_failed_count = 0; sequence_failed_count = 0;
verification_failed_count = 0; verification_failed_count = 0;
untriggered_expectation_count = 0; untriggered_expectation_count = 0;
@ -45,7 +45,7 @@ namespace
bool verify() const bool verify() const
{ {
return missing_action_count == 0 && return missing_action_count == 0 &&
no_match_count == 0 && unexpected_call_count == 0 &&
sequence_failed_count == 0 && sequence_failed_count == 0 &&
verification_failed_count == 0 && verification_failed_count == 0 &&
untriggered_expectation_count == 0; untriggered_expectation_count == 0;
@ -76,29 +76,29 @@ BOOST_FIXTURE_TEST_CASE( a_function_can_be_passed_as_functor_using_boost_bind_an
// invocations // invocations
BOOST_FIXTURE_TEST_CASE( triggering_an_unconfigured_function_calls_no_match_error, error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_an_unconfigured_function_calls_unexpected_call_error, error_fixture )
{ {
{ {
mock::function< void() > f; mock::function< void() > f;
CHECK_ERROR( f(), no_match ); CHECK_ERROR( f(), unexpected_call );
} }
{ {
mock::function< int( int, const std::string& ) > f; mock::function< int( int, const std::string& ) > f;
CHECK_ERROR( f( 1, "s" ), no_match ); CHECK_ERROR( f( 1, "s" ), unexpected_call );
} }
} }
BOOST_FIXTURE_TEST_CASE( triggering_a_never_expectation_calls_no_match_error, error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_a_never_expectation_calls_unexpected_call_error, error_fixture )
{ {
{ {
mock::function< void() > f; mock::function< void() > f;
f.expect().never(); f.expect().never();
CHECK_ERROR( f(), no_match ); CHECK_ERROR( f(), unexpected_call );
} }
{ {
mock::function< int( int, const std::string& ) > f; mock::function< int( int, const std::string& ) > f;
f.expect().never(); f.expect().never();
CHECK_ERROR( f( 1, "s" ), no_match ); CHECK_ERROR( f( 1, "s" ), unexpected_call );
} }
} }
@ -118,19 +118,19 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_unlimited_expectation_is_valid, error_fix
} }
} }
BOOST_FIXTURE_TEST_CASE( triggering_a_once_expectation_calls_no_match_error_after_one_call, error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_a_once_expectation_calls_unexpected_call_error_after_one_call, error_fixture )
{ {
{ {
mock::function< void() > f; mock::function< void() > f;
f.expect().once(); f.expect().once();
f(); f();
CHECK_ERROR( f(), no_match ); CHECK_ERROR( f(), unexpected_call );
} }
{ {
mock::function< void( int, const std::string& ) > f; mock::function< void( int, const std::string& ) > f;
f.expect().once(); f.expect().once();
f( 1, "s" ); f( 1, "s" );
CHECK_ERROR( f( 1, "s" ), no_match ); CHECK_ERROR( f( 1, "s" ), unexpected_call );
} }
} }
@ -201,19 +201,19 @@ BOOST_FIXTURE_TEST_CASE( verifying_a_once_expectation_before_the_call_fails, err
// reset // reset
BOOST_FIXTURE_TEST_CASE( triggering_a_reset_function_calls_no_match_error, error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_a_reset_function_calls_unexpected_call_error, error_fixture )
{ {
{ {
mock::function< void() > f; mock::function< void() > f;
f.expect(); f.expect();
f.reset(); f.reset();
CHECK_ERROR( f(), no_match ); CHECK_ERROR( f(), unexpected_call );
} }
{ {
mock::function< int( int, const std::string& ) > f; mock::function< int( int, const std::string& ) > f;
f.expect(); f.expect();
f.reset(); f.reset();
CHECK_ERROR( f( 1, "s" ), no_match ); CHECK_ERROR( f( 1, "s" ), unexpected_call );
} }
} }
@ -235,35 +235,35 @@ BOOST_FIXTURE_TEST_CASE( verifying_a_reset_function_succeeds, error_fixture )
// constraints // constraints
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_constraint_calls_no_match_error, error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_constraint_calls_unexpected_call_error, error_fixture )
{ {
{ {
mock::function< void( int ) > f; mock::function< void( int ) > f;
f.expect().with( 42 ); f.expect().with( 42 );
CHECK_ERROR( f( 43 ), no_match ); CHECK_ERROR( f( 43 ), unexpected_call );
} }
{ {
mock::function< int( int, const std::string& ) > f; mock::function< int( int, const std::string& ) > f;
f.expect().with( 42, "expected" ); f.expect().with( 42, "expected" );
CHECK_ERROR( f( 42, "actual" ), no_match ); CHECK_ERROR( f( 42, "actual" ), unexpected_call );
} }
} }
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_or_less_constraint_calls_no_match_error, error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_or_less_constraint_calls_unexpected_call_error, error_fixture )
{ {
mock::function< void( int ) > f; mock::function< void( int ) > f;
f.expect().with( mock::equal( 42 ) || mock::less( 42 ) ); f.expect().with( mock::equal( 42 ) || mock::less( 42 ) );
f( 41 ); f( 41 );
f( 42 ); f( 42 );
CHECK_ERROR( f( 43 ), no_match ); CHECK_ERROR( f( 43 ), unexpected_call );
} }
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_and_not_less_constraint_calls_no_match_error, error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_and_not_less_constraint_calls_unexpected_call_error, error_fixture )
{ {
mock::function< void( int ) > f; mock::function< void( int ) > f;
f.expect().with( mock::equal( 42 ) && ! mock::less( 41 ) ); f.expect().with( mock::equal( 42 ) && ! mock::less( 41 ) );
f( 42 ); f( 42 );
CHECK_ERROR( f( 43 ), no_match ); CHECK_ERROR( f( 43 ), unexpected_call );
} }
namespace namespace
@ -305,25 +305,25 @@ namespace
} }
} }
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_failing_custom_constraint_calls_no_match_error, error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_failing_custom_constraint_calls_unexpected_call_error, error_fixture )
{ {
{ {
mock::function< void( int ) > f; mock::function< void( int ) > f;
f.expect().with( &custom_constraint ); f.expect().with( &custom_constraint );
CHECK_ERROR( f( 42 ), no_match ); CHECK_ERROR( f( 42 ), unexpected_call );
} }
{ {
mock::function< int( int, const std::string& ) > f; mock::function< int( int, const std::string& ) > f;
f.expect().with( &custom_constraint, "actual" ); f.expect().with( &custom_constraint, "actual" );
CHECK_ERROR( f( 42, "actual" ), no_match ); CHECK_ERROR( f( 42, "actual" ), unexpected_call );
} }
} }
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_custom_constraint_calls_no_match_error, error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_custom_constraint_calls_unexpected_call_error, error_fixture )
{ {
mock::function< void( int ) > f; mock::function< void( int ) > f;
f.expect().with( mock::constraint( &custom_constraint, "custom constraint" ) ); f.expect().with( mock::constraint( &custom_constraint, "custom constraint" ) );
CHECK_ERROR( f( 42 ), no_match ); CHECK_ERROR( f( 42 ), unexpected_call );
} }
/* /*
@ -547,7 +547,7 @@ BOOST_FIXTURE_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_
f.expect().once(); f.expect().once();
f(); f();
f(); f();
CHECK_ERROR( f(), no_match ); CHECK_ERROR( f(), unexpected_call );
} }
{ {
mock::function< void( const std::string& ) > f; mock::function< void( const std::string& ) > f;
@ -555,7 +555,7 @@ BOOST_FIXTURE_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_
f.expect().once().with( "second" ); f.expect().once().with( "second" );
f( "first" ); f( "first" );
f( "second" ); f( "second" );
CHECK_ERROR( f( "third"), no_match ); CHECK_ERROR( f( "third"), unexpected_call );
} }
} }
@ -567,7 +567,7 @@ BOOST_FIXTURE_TEST_CASE( best_expectation_is_selected_first, error_fixture )
f.expect().once().with( 2 ); f.expect().once().with( 2 );
f( 2 ); f( 2 );
f( 1 ); f( 1 );
CHECK_ERROR( f( 3 ), no_match ); CHECK_ERROR( f( 3 ), unexpected_call );
} }
{ {
mock::function< void( const std::string& ) > f; mock::function< void( const std::string& ) > f;
@ -575,7 +575,7 @@ BOOST_FIXTURE_TEST_CASE( best_expectation_is_selected_first, error_fixture )
f.expect().once().with( "second" ); f.expect().once().with( "second" );
f( "second" ); f( "second" );
f( "first" ); f( "first" );
CHECK_ERROR( f( "third"), no_match ); CHECK_ERROR( f( "third"), unexpected_call );
} }
} }
@ -705,16 +705,16 @@ BOOST_FIXTURE_TEST_CASE( verifying_expectation_with_remaining_matches_disables_t
CHECK_ERROR( f.verify(), verification_failed ); CHECK_ERROR( f.verify(), verification_failed );
} }
BOOST_FIXTURE_TEST_CASE( triggering_no_match_call_disables_the_automatic_verification_upon_destruction, error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_unexpected_call_call_disables_the_automatic_verification_upon_destruction, error_fixture )
{ {
mock::function< void() > f; mock::function< void() > f;
CHECK_ERROR( f(), no_match ); CHECK_ERROR( f(), unexpected_call );
} }
BOOST_FIXTURE_TEST_CASE( adding_a_expectation_reactivates_the_verification_upon_destruction, error_fixture ) BOOST_FIXTURE_TEST_CASE( adding_a_expectation_reactivates_the_verification_upon_destruction, error_fixture )
{ {
std::auto_ptr< mock::function< void() > > f( new mock::function< void() > ); std::auto_ptr< mock::function< void() > > f( new mock::function< void() > );
CHECK_ERROR( (*f)(), no_match ); CHECK_ERROR( (*f)(), unexpected_call );
f->expect().once(); f->expect().once();
CHECK_ERROR( f.reset(), untriggered_expectation ); CHECK_ERROR( f.reset(), untriggered_expectation );
} }

View file

@ -15,7 +15,7 @@
namespace namespace
{ {
int missing_action_count = 0; int missing_action_count = 0;
int no_match_count = 0; int unexpected_call_count = 0;
int sequence_failed_count = 0; int sequence_failed_count = 0;
int verification_failed_count = 0; int verification_failed_count = 0;
int untriggered_expectation_count = 0; int untriggered_expectation_count = 0;
@ -36,9 +36,9 @@ namespace mock
{ {
++missing_action_count; ++missing_action_count;
} }
static void no_match( const std::string& /*context*/ ) static void unexpected_call( const std::string& /*context*/ )
{ {
++no_match_count; ++unexpected_call_count;
} }
static void sequence_failed( const std::string& /*context*/, static void sequence_failed( const std::string& /*context*/,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
@ -66,9 +66,9 @@ namespace mock
{ {
++missing_action_count; ++missing_action_count;
} }
static void no_match( const std::string& /*context*/ ) static void unexpected_call( const std::string& /*context*/ )
{ {
++no_match_count; ++unexpected_call_count;
} }
static void sequence_failed( const std::string& /*context*/, static void sequence_failed( const std::string& /*context*/,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )

View file

@ -22,7 +22,7 @@ namespace mock
{ {
throw std::runtime_error( "abort" ); throw std::runtime_error( "abort" );
} }
static void no_match( const std::string& /*context*/ ) static void unexpected_call( const std::string& /*context*/ )
{} {}
static void missing_action( const std::string& /*context*/, static void missing_action( const std::string& /*context*/,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )