mirror of
https://github.com/mat007/turtle.git
synced 2026-06-22 12:13:43 +00:00
Error policy now customizable and missing result specifications now reported with file name and line
git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@67 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
parent
51379e7844
commit
6ff9fc5564
14 changed files with 380 additions and 271 deletions
|
|
@ -16,7 +16,7 @@ BOOST_AUTO_TEST_CASE( a_mock_exception_is_not_an_std_exception_to_not_mess_with_
|
|||
{
|
||||
try
|
||||
{
|
||||
throw mock::exception( "" );
|
||||
throw mock::exception();
|
||||
}
|
||||
catch( std::exception& )
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,33 +6,62 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <turtle/expectation.hpp>
|
||||
#include <turtle/constraint.hpp>
|
||||
#include "mock_error.hpp"
|
||||
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#define BOOST_LIB_NAME boost_unit_test_framework
|
||||
#include <boost/config/auto_link.hpp>
|
||||
|
||||
#define MOCK_ERROR_POLICY mock_error
|
||||
#include <turtle/mock.hpp>
|
||||
|
||||
#define CHECK_ERROR( expr, error ) \
|
||||
BOOST_CHECK( verify() ); \
|
||||
expr; \
|
||||
BOOST_CHECK_EQUAL( 1, error##_count ); \
|
||||
reset();
|
||||
|
||||
namespace
|
||||
{
|
||||
bool check_exception_contains( const mock::exception& e, const std::string& str )
|
||||
struct error_fixture
|
||||
{
|
||||
return e.what().find( str ) != std::string::npos;
|
||||
}
|
||||
error_fixture()
|
||||
{
|
||||
reset();
|
||||
}
|
||||
~error_fixture()
|
||||
{
|
||||
BOOST_CHECK( verify() );
|
||||
}
|
||||
void reset()
|
||||
{
|
||||
missing_result_specification_count = 0;
|
||||
no_match_count = 0;
|
||||
sequence_failed_count = 0;
|
||||
verification_failed_count = 0;
|
||||
untriggered_expectation_count = 0;
|
||||
}
|
||||
bool verify() const
|
||||
{
|
||||
return missing_result_specification_count == 0 &&
|
||||
no_match_count == 0 &&
|
||||
sequence_failed_count == 0 &&
|
||||
verification_failed_count == 0 &&
|
||||
untriggered_expectation_count == 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
#define BOOST_CHECK_EXCEPTION_CONTAINS( S, C ) \
|
||||
BOOST_CHECK_EXCEPTION( S, mock::exception, boost::bind( &check_exception_contains, _1, C ) );
|
||||
|
||||
// functor
|
||||
|
||||
BOOST_AUTO_TEST_CASE( an_expectation_can_be_passed_as_functor )
|
||||
BOOST_FIXTURE_TEST_CASE( an_expectation_can_be_passed_as_functor, error_fixture )
|
||||
{
|
||||
mock::expectation< void() > e;
|
||||
boost::function< void() > f = e;
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( an_expectation_can_be_passed_as_functor_using_boost_bind_and_boost_ref )
|
||||
BOOST_FIXTURE_TEST_CASE( an_expectation_can_be_passed_as_functor_using_boost_bind_and_boost_ref, error_fixture )
|
||||
{
|
||||
mock::expectation< void() > e;
|
||||
boost::function< void() > f = boost::bind( boost::ref( e ) );
|
||||
|
|
@ -40,66 +69,66 @@ BOOST_AUTO_TEST_CASE( an_expectation_can_be_passed_as_functor_using_boost_bind_a
|
|||
|
||||
// invocations
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_empty_expectation_throws )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_empty_expectation_calls_no_match_error, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void() > e;
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "unexpected call" );
|
||||
CHECK_ERROR( e(), no_match );
|
||||
}
|
||||
{
|
||||
mock::expectation< int( int, const std::string& ) > e;
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 1, "s" ), "unexpected call" );
|
||||
CHECK_ERROR( e( 1, "s" ), no_match );
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_a_never_expectation_throws )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_a_never_expectation_calls_no_match_error, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void() > e;
|
||||
e.expect().never();
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "unexpected call" );
|
||||
CHECK_ERROR( e(), no_match );
|
||||
}
|
||||
{
|
||||
mock::expectation< int( int, const std::string& ) > e;
|
||||
e.expect().never();
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 1, "s" ), "unexpected call" );
|
||||
CHECK_ERROR( e( 1, "s" ), no_match );
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_unlimited_expectation_never_throws )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_unlimited_expectation_is_valid, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void() > e;
|
||||
e.expect();
|
||||
BOOST_CHECK_NO_THROW( e() );
|
||||
BOOST_CHECK_NO_THROW( e() );
|
||||
e();
|
||||
e();
|
||||
}
|
||||
{
|
||||
mock::expectation< void( int, const std::string& ) > e;
|
||||
e.expect();
|
||||
BOOST_CHECK_NO_THROW( e( 1, "s" ) );
|
||||
BOOST_CHECK_NO_THROW( e( 1, "s" ) );
|
||||
e( 1, "s" );
|
||||
e( 1, "s" );
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_a_once_expectation_throws_after_one_call )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_a_once_expectation_calls_no_match_error_after_one_call, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void() > e;
|
||||
e.expect().once();
|
||||
BOOST_CHECK_NO_THROW( e() );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "unexpected call" );
|
||||
e();
|
||||
CHECK_ERROR( e(), no_match );
|
||||
}
|
||||
{
|
||||
mock::expectation< void( int, const std::string& ) > e;
|
||||
e.expect().once();
|
||||
BOOST_CHECK_NO_THROW( e( 1, "s" ) );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 1, "s" ), "unexpected call" );
|
||||
e( 1, "s" );
|
||||
CHECK_ERROR( e( 1, "s" ), no_match );
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
BOOST_AUTO_TEST_CASE( literal_zero_can_be_used_in_expectation_operator_call_as_pointers )
|
||||
BOOST_FIXTURE_TEST_CASE( literal_zero_can_be_used_in_expectation_operator_call_as_pointers, error_fixture )
|
||||
{
|
||||
mock::expectation< void( int* ) > e;
|
||||
e( 0 );
|
||||
|
|
@ -108,7 +137,7 @@ BOOST_AUTO_TEST_CASE( literal_zero_can_be_used_in_expectation_operator_call_as_p
|
|||
|
||||
// verify
|
||||
|
||||
BOOST_AUTO_TEST_CASE( verifying_an_empty_expectation_succeeds )
|
||||
BOOST_FIXTURE_TEST_CASE( verifying_an_empty_expectation_succeeds, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void() > e;
|
||||
|
|
@ -120,7 +149,7 @@ BOOST_AUTO_TEST_CASE( verifying_an_empty_expectation_succeeds )
|
|||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( verifying_an_unlimited_expectation_succeeds )
|
||||
BOOST_FIXTURE_TEST_CASE( verifying_an_unlimited_expectation_succeeds, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void() > e;
|
||||
|
|
@ -134,7 +163,7 @@ BOOST_AUTO_TEST_CASE( verifying_an_unlimited_expectation_succeeds )
|
|||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( verifying_a_once_expectation_after_one_call_succeeds )
|
||||
BOOST_FIXTURE_TEST_CASE( verifying_a_once_expectation_after_one_call_succeeds, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void() > e;
|
||||
|
|
@ -150,60 +179,39 @@ BOOST_AUTO_TEST_CASE( verifying_a_once_expectation_after_one_call_succeeds )
|
|||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
template< typename Result >
|
||||
struct silent_error
|
||||
{
|
||||
static Result no_match( const std::string& context )
|
||||
{
|
||||
throw std::runtime_error( __FUNCTION__ );
|
||||
}
|
||||
static void sequence_failed( const std::string& /*context*/,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{}
|
||||
static void verification_failed( const std::string& /*context*/,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{}
|
||||
static void untriggered_expectation( const std::string& /*context*/,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{}
|
||||
};
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( verifying_a_once_expectation_before_the_call_fails )
|
||||
BOOST_FIXTURE_TEST_CASE( verifying_a_once_expectation_before_the_call_fails, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void(), silent_error< void > > e;
|
||||
mock::expectation< void() > e;
|
||||
e.expect().once();
|
||||
BOOST_CHECK( ! e.verify() );
|
||||
CHECK_ERROR( BOOST_CHECK( ! e.verify() ), verification_failed );
|
||||
}
|
||||
{
|
||||
mock::expectation< int( int, const std::string& ), silent_error< int > > e;
|
||||
mock::expectation< int( int, const std::string& ) > e;
|
||||
e.expect().once();
|
||||
BOOST_CHECK( ! e.verify() );
|
||||
CHECK_ERROR( BOOST_CHECK( ! e.verify() ), verification_failed );
|
||||
}
|
||||
}
|
||||
|
||||
// reset
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_a_reset_expectation_throws )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_a_reset_expectation_calls_no_match_error, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void() > e;
|
||||
e.expect();
|
||||
e.reset();
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "unexpected call" );
|
||||
CHECK_ERROR( e(), no_match );
|
||||
}
|
||||
{
|
||||
mock::expectation< int( int, const std::string& ) > e;
|
||||
e.expect();
|
||||
e.reset();
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 1, "s" ), "unexpected call" );
|
||||
CHECK_ERROR( e( 1, "s" ), no_match );
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( verifying_a_reset_expectation_succeeds )
|
||||
BOOST_FIXTURE_TEST_CASE( verifying_a_reset_expectation_succeeds, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void() > e;
|
||||
|
|
@ -221,49 +229,35 @@ BOOST_AUTO_TEST_CASE( verifying_a_reset_expectation_succeeds )
|
|||
|
||||
// constraints
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_constraint_throws )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_constraint_calls_no_match_error, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void( int ) > e;
|
||||
e.expect().with( 42 );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 43 ), "unexpected call" );
|
||||
CHECK_ERROR( e( 43 ), no_match );
|
||||
}
|
||||
{
|
||||
mock::expectation< int( int, const std::string& ) > e;
|
||||
e.expect().with( 42, "expected" );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 42, "actual" ), "unexpected call" );
|
||||
CHECK_ERROR( e( 42, "actual" ), no_match );
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( passing_raw_parameter_as_constraint_falls_back_to_using_the_equal_constraint )
|
||||
{
|
||||
{
|
||||
mock::expectation< void( int ) > e;
|
||||
e.expect().with( 42 );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 43 ), "unexpected call" );
|
||||
}
|
||||
{
|
||||
mock::expectation< int( int, const std::string& ) > e;
|
||||
e.expect().with( 42, "expected" );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 42, "actual" ), "unexpected call" );
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_or_less_constraint_throws )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_or_less_constraint_calls_no_match_error, error_fixture )
|
||||
{
|
||||
mock::expectation< void( int ) > e;
|
||||
e.expect().with( mock::equal( 42 ) || mock::less( 42 ) );
|
||||
BOOST_CHECK_NO_THROW( e( 41 ) );
|
||||
BOOST_CHECK_NO_THROW( e( 42 ) );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 43 ), "unexpected call" );
|
||||
e( 41 );
|
||||
e( 42 );
|
||||
CHECK_ERROR( e( 43 ), no_match );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_and_not_less_constraint_throws )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_and_not_less_constraint_calls_no_match_error, error_fixture )
|
||||
{
|
||||
mock::expectation< void( int ) > e;
|
||||
e.expect().with( mock::equal( 42 ) && ! mock::less( 41 ) );
|
||||
BOOST_CHECK_NO_THROW( e( 42 ) );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 43 ), "unexpected call" );
|
||||
e( 42 );
|
||||
CHECK_ERROR( e( 43 ), no_match );
|
||||
}
|
||||
|
||||
namespace
|
||||
|
|
@ -281,7 +275,7 @@ namespace
|
|||
};
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( passing_call_values_by_reference_is_transparent )
|
||||
BOOST_FIXTURE_TEST_CASE( passing_call_values_by_reference_is_transparent, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void( my_interface& ) > e;
|
||||
|
|
@ -305,28 +299,29 @@ namespace
|
|||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_expectation_with_failing_custom_constraint_throws )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_failing_custom_constraint_calls_no_match_error, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void( int ) > e;
|
||||
e.expect().with( &custom_constraint );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 42 ), "unexpected call" );
|
||||
CHECK_ERROR( e( 42 ), no_match );
|
||||
}
|
||||
{
|
||||
mock::expectation< int( int, const std::string& ) > e;
|
||||
e.expect().with( &custom_constraint, "actual" );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 42, "actual" ), "unexpected call" );
|
||||
CHECK_ERROR( e( 42, "actual" ), no_match );
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_custom_constraint_throws )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_custom_constraint_calls_no_match_error, error_fixture )
|
||||
{
|
||||
mock::expectation< void( int ) > e;
|
||||
e.expect().with( mock::constraint( &custom_constraint, "custom constraint" ) );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 42 ), "custom constraint" );
|
||||
CHECK_ERROR( e( 42 ), no_match );
|
||||
}
|
||||
|
||||
/*
|
||||
BOOST_AUTO_TEST_CASE( literal_zero_can_be_used_in_place_of_null_pointers_in_constraints )
|
||||
BOOST_FIXTURE_TEST_CASE( literal_zero_can_be_used_in_place_of_null_pointers_in_constraints, error_fixture )
|
||||
{
|
||||
mock::expectation< void( int* ) > e;
|
||||
e.expect().with( 0 );
|
||||
|
|
@ -336,26 +331,26 @@ BOOST_AUTO_TEST_CASE( literal_zero_can_be_used_in_place_of_null_pointers_in_cons
|
|||
|
||||
// result handling
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_expectation_with_no_return_set_throws )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_no_return_set_calls_missing_result_specification, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< int() > e;
|
||||
e.expect();
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "missing result specification" );
|
||||
CHECK_ERROR( e(), missing_result_specification );
|
||||
}
|
||||
{
|
||||
mock::expectation< int&() > e;
|
||||
e.expect();
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "missing result specification" );
|
||||
CHECK_ERROR( e(), missing_result_specification );
|
||||
}
|
||||
{
|
||||
mock::expectation< const std::string&() > e;
|
||||
e.expect();
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "missing result specification" );
|
||||
CHECK_ERROR( e(), missing_result_specification );
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_expectation_returns_the_set_value )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_the_set_value, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< int() > e;
|
||||
|
|
@ -435,7 +430,7 @@ namespace
|
|||
struct B : A {};
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_expectation_returns_the_set_auto_ptr_value )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_the_set_auto_ptr_value, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< std::auto_ptr< int >() > e;
|
||||
|
|
@ -491,7 +486,7 @@ namespace
|
|||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_expectation_calls_the_custom_functor )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_calls_the_custom_functor, error_fixture )
|
||||
{
|
||||
mock::expectation< int() > e;
|
||||
e.expect().calls( &custom_result );
|
||||
|
|
@ -506,21 +501,21 @@ namespace
|
|||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_expectation_calls_the_custom_functor_with_parameters )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_calls_the_custom_functor_with_parameters, error_fixture )
|
||||
{
|
||||
mock::expectation< int( int ) > e;
|
||||
e.expect().calls( &custom_result_with_parameter );
|
||||
BOOST_CHECK_EQUAL( 42, e( 42 ) );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_expectation_calls_the_custom_functor_without_parameters_thanks_to_boost_bind )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_calls_the_custom_functor_without_parameters_thanks_to_boost_bind, error_fixture )
|
||||
{
|
||||
mock::expectation< int( int ) > e;
|
||||
e.expect().calls( boost::bind( &custom_result ) );
|
||||
BOOST_CHECK_EQUAL( 42, e( 17 ) );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( triggering_an_expectation_throws_the_set_exception )
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_throws_the_set_exception, error_fixture )
|
||||
{
|
||||
mock::expectation< void() > e;
|
||||
e.expect().throws( std::runtime_error( "some exception" ) );
|
||||
|
|
@ -538,43 +533,43 @@ BOOST_AUTO_TEST_CASE( triggering_an_expectation_throws_the_set_exception )
|
|||
|
||||
// multiple matchers
|
||||
|
||||
BOOST_AUTO_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_twice )
|
||||
BOOST_FIXTURE_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_twice, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void() > e;
|
||||
e.expect().once();
|
||||
e.expect().once();
|
||||
BOOST_CHECK_NO_THROW( e() );
|
||||
BOOST_CHECK_NO_THROW( e() );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "unexpected call" );
|
||||
e();
|
||||
e();
|
||||
CHECK_ERROR( e(), no_match );
|
||||
}
|
||||
{
|
||||
mock::expectation< void( const std::string& ) > e;
|
||||
e.expect().once().with( "first" );
|
||||
e.expect().once().with( "second" );
|
||||
BOOST_CHECK_NO_THROW( e( "first" ) );
|
||||
BOOST_CHECK_NO_THROW( e( "second" ) );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( "third" ), "unexpected call" );
|
||||
e( "first" );
|
||||
e( "second" );
|
||||
CHECK_ERROR( e( "third"), no_match );
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( best_matcher_is_selected_first )
|
||||
BOOST_FIXTURE_TEST_CASE( best_matcher_is_selected_first, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void( int ) > e;
|
||||
e.expect().once().with( 1 );
|
||||
e.expect().once().with( 2 );
|
||||
BOOST_CHECK_NO_THROW( e( 2 ) );
|
||||
BOOST_CHECK_NO_THROW( e( 1 ) );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( 3 ), "unexpected call" );
|
||||
e( 2 );
|
||||
e( 1 );
|
||||
CHECK_ERROR( e( 3 ), no_match );
|
||||
}
|
||||
{
|
||||
mock::expectation< void( const std::string& ) > e;
|
||||
e.expect().once().with( "first" );
|
||||
e.expect().once().with( "second" );
|
||||
BOOST_CHECK_NO_THROW( e( "second" ) );
|
||||
BOOST_CHECK_NO_THROW( e( "first" ) );
|
||||
BOOST_CHECK_EXCEPTION_CONTAINS( e( "third" ), "unexpected call" );
|
||||
e( "second" );
|
||||
e( "first" );
|
||||
CHECK_ERROR( e( "third"), no_match );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -591,7 +586,7 @@ namespace
|
|||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( expectation_can_be_serialized_to_be_human_readable )
|
||||
BOOST_FIXTURE_TEST_CASE( expectation_can_be_serialized_to_be_human_readable, error_fixture )
|
||||
{
|
||||
{
|
||||
mock::expectation< void( int ) > e;
|
||||
|
|
@ -690,91 +685,39 @@ BOOST_AUTO_TEST_CASE( expectation_can_be_serialized_to_be_human_readable )
|
|||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
BOOST_FIXTURE_TEST_CASE( expectation_with_remaining_untriggered_matches_upon_destruction_calls_untriggered_expectation, error_fixture )
|
||||
{
|
||||
mock::expectation< void() > no_match_exp;
|
||||
mock::expectation< void() > sequence_failed_exp;
|
||||
mock::expectation< void() > verification_failed_exp;
|
||||
mock::expectation< void() > untriggered_expectation_exp;
|
||||
|
||||
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*/ )
|
||||
{
|
||||
no_match_exp();
|
||||
}
|
||||
static void sequence_failed( const std::string& /*context*/,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
sequence_failed_exp();
|
||||
}
|
||||
static void verification_failed( const std::string& /*context*/,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
verification_failed_exp();
|
||||
}
|
||||
static void untriggered_expectation( const std::string& /*context*/,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
untriggered_expectation_exp();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE( expectation_with_remaining_untriggered_matches_notifies_an_error_upon_destructions, error_fixture )
|
||||
{
|
||||
mock::expectation< void(), error_fixture > e;
|
||||
e.expect().once();
|
||||
untriggered_expectation_exp.expect().once();
|
||||
std::auto_ptr< mock::expectation< void() > > e( new mock::expectation< void() > );
|
||||
e->expect().once();
|
||||
CHECK_ERROR( e.reset(), untriggered_expectation );
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE( verifying_expectation_with_remaining_matches_disables_the_automatic_verification_upon_destruction, error_fixture )
|
||||
{
|
||||
mock::expectation< void(), error_fixture > e;
|
||||
mock::expectation< void() > e;
|
||||
e.expect().once();
|
||||
verification_failed_exp.expect();
|
||||
e.verify();
|
||||
CHECK_ERROR( e.verify(), verification_failed );
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE( triggering_no_match_call_disables_the_automatic_verification_upon_destruction, error_fixture )
|
||||
{
|
||||
mock::expectation< void(), error_fixture > e;
|
||||
no_match_exp.expect();
|
||||
e();
|
||||
mock::expectation< void() > e;
|
||||
CHECK_ERROR( e(), no_match );
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE( adding_a_matcher_reactivates_the_verification_upon_destruction, error_fixture )
|
||||
{
|
||||
mock::expectation< void(), error_fixture > e;
|
||||
no_match_exp.expect();
|
||||
e();
|
||||
e.expect().once();
|
||||
untriggered_expectation_exp.expect().once();
|
||||
std::auto_ptr< mock::expectation< void() > > e( new mock::expectation< void() > );
|
||||
CHECK_ERROR( (*e)(), no_match );
|
||||
e->expect().once();
|
||||
CHECK_ERROR( e.reset(), untriggered_expectation );
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE( throwing_an_exception_disables_the_automatic_verification_upon_destruction, error_fixture )
|
||||
{
|
||||
try
|
||||
{
|
||||
mock::expectation< void(), error_fixture > e;
|
||||
mock::expectation< void() > e;
|
||||
e.expect().once();
|
||||
throw std::exception();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,12 +6,15 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#include <turtle/mock.hpp>
|
||||
#include "silent_error.hpp"
|
||||
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#define BOOST_LIB_NAME boost_unit_test_framework
|
||||
#include <boost/config/auto_link.hpp>
|
||||
|
||||
#define MOCK_ERROR_POLICY silent_error
|
||||
#include <turtle/mock.hpp>
|
||||
|
||||
#include <boost/noncopyable.hpp>
|
||||
#include <boost/ref.hpp>
|
||||
|
||||
|
|
@ -91,7 +94,7 @@ BOOST_AUTO_TEST_CASE( mock_object_method_disambiguation )
|
|||
my_ambiguited_mock mock;
|
||||
MOCK_EXPECT( mock, tag1 );
|
||||
BOOST_CHECK_NO_THROW( mock.my_method() );
|
||||
BOOST_CHECK_THROW( mock.my_method( 12 ), mock::exception );
|
||||
BOOST_CHECK_THROW( mock.my_method( 12 ), std::exception );
|
||||
}
|
||||
|
||||
namespace
|
||||
|
|
@ -118,7 +121,7 @@ BOOST_AUTO_TEST_CASE( mock_object_method_const_disambiguation )
|
|||
MOCK_EXPECT( mock, tag1 );
|
||||
BOOST_CHECK_NO_THROW( mock.my_method() );
|
||||
const my_const_ambiguited_mock const_mock;
|
||||
BOOST_CHECK_THROW( const_mock.my_method(), mock::exception );
|
||||
BOOST_CHECK_THROW( const_mock.my_method(), std::exception );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( mock_functor_in_function_is_supported )
|
||||
|
|
|
|||
79
src/tests/turtle_test/mock_error.hpp
Normal file
79
src/tests/turtle_test/mock_error.hpp
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
//
|
||||
// Copyright Mathieu Champlon 2008
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef MOCK_TEST_MOCK_ERROR_HPP_INCLUDED
|
||||
#define MOCK_TEST_MOCK_ERROR_HPP_INCLUDED
|
||||
|
||||
#include <boost/type_traits/remove_reference.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
int missing_result_specification_count = 0;
|
||||
int no_match_count = 0;
|
||||
int sequence_failed_count = 0;
|
||||
int verification_failed_count = 0;
|
||||
int untriggered_expectation_count = 0;
|
||||
}
|
||||
namespace mock
|
||||
{
|
||||
template< typename Result >
|
||||
struct mock_error
|
||||
{
|
||||
static Result missing_result_specification( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
++missing_result_specification_count;
|
||||
static BOOST_DEDUCED_TYPENAME boost::remove_reference< Result >::type r;
|
||||
return r;
|
||||
}
|
||||
static Result no_match( const std::string& /*context*/ )
|
||||
{
|
||||
++no_match_count;
|
||||
static BOOST_DEDUCED_TYPENAME boost::remove_reference< Result >::type r;
|
||||
return r;
|
||||
}
|
||||
static void sequence_failed( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
++sequence_failed_count;
|
||||
}
|
||||
static void verification_failed( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
++verification_failed_count;
|
||||
}
|
||||
static void untriggered_expectation( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
++untriggered_expectation_count;
|
||||
}
|
||||
};
|
||||
template<>
|
||||
struct mock_error< void >
|
||||
{
|
||||
static void missing_result_specification( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
++missing_result_specification_count;
|
||||
}
|
||||
static void no_match( const std::string& /*context*/ )
|
||||
{
|
||||
++no_match_count;
|
||||
}
|
||||
static void sequence_failed( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
++sequence_failed_count;
|
||||
}
|
||||
static void verification_failed( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
++verification_failed_count;
|
||||
}
|
||||
static void untriggered_expectation( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
++untriggered_expectation_count;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // #ifndef MOCK_TEST_MOCK_ERROR_HPP_INCLUDED
|
||||
|
|
@ -6,36 +6,26 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#include <turtle/object.hpp>
|
||||
#include <turtle/expectation.hpp>
|
||||
#include "silent_error.hpp"
|
||||
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#define BOOST_LIB_NAME boost_unit_test_framework
|
||||
#include <boost/config/auto_link.hpp>
|
||||
|
||||
#define MOCK_ERROR_POLICY silent_error
|
||||
#include <turtle/object.hpp>
|
||||
#include <turtle/expectation.hpp>
|
||||
|
||||
BOOST_AUTO_TEST_CASE( verifying_an_empty_object_succeeds )
|
||||
{
|
||||
mock::object o;
|
||||
BOOST_CHECK( o.verify() );
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
struct silent_error
|
||||
{
|
||||
static void verification_failed( const std::string& /*context*/,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{}
|
||||
static void untriggered_expectation( const std::string& /*context*/,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{}
|
||||
};
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( verifying_an_object_containing_a_failing_expectation_fails )
|
||||
{
|
||||
mock::object o;
|
||||
mock::expectation< void(), silent_error > e;
|
||||
mock::expectation< void() > e;
|
||||
o.set_parent( e );
|
||||
e.expect().once();
|
||||
BOOST_CHECK( ! o.verify() );
|
||||
|
|
@ -46,7 +36,7 @@ BOOST_AUTO_TEST_CASE( verifying_an_object_containing_a_failing_expectation_fails
|
|||
BOOST_AUTO_TEST_CASE( verifying_all_objects_with_one_of_them_containing_a_failing_expectation_fails )
|
||||
{
|
||||
mock::object o;
|
||||
mock::expectation< void(), silent_error > e;
|
||||
mock::expectation< void() > e;
|
||||
o.set_parent( e );
|
||||
e.expect().once();
|
||||
BOOST_CHECK( ! mock::verify() );
|
||||
|
|
@ -67,7 +57,7 @@ BOOST_AUTO_TEST_CASE( resetting_an_object_containing_a_failing_expectation_and_v
|
|||
BOOST_AUTO_TEST_CASE( an_object_is_assignable_by_sharing_its_state )
|
||||
{
|
||||
mock::object o1;
|
||||
mock::expectation< void(), silent_error > e;
|
||||
mock::expectation< void() > e;
|
||||
{
|
||||
mock::object o2;
|
||||
o2.set_parent( e );
|
||||
|
|
@ -83,7 +73,7 @@ BOOST_AUTO_TEST_CASE( an_object_is_copiable_by_sharing_its_state )
|
|||
{
|
||||
std::auto_ptr< mock::object > o2( new mock::object );
|
||||
const mock::object o1( *o2 );
|
||||
mock::expectation< void(), silent_error > e;
|
||||
mock::expectation< void() > e;
|
||||
o2->set_parent( e );
|
||||
e.expect().once();
|
||||
BOOST_CHECK( ! o2->verify() );
|
||||
|
|
|
|||
|
|
@ -6,14 +6,15 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#include <turtle/sequence.hpp>
|
||||
#include <turtle/expectation.hpp>
|
||||
#include <turtle/constraint.hpp>
|
||||
#include "silent_error.hpp"
|
||||
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#define BOOST_LIB_NAME boost_unit_test_framework
|
||||
#include <boost/config/auto_link.hpp>
|
||||
|
||||
#define MOCK_ERROR_POLICY mock::silent_error
|
||||
#include <turtle/mock.hpp>
|
||||
|
||||
BOOST_AUTO_TEST_CASE( registering_to_a_sequence_and_calling_out_of_order_throws )
|
||||
{
|
||||
mock::sequence s;
|
||||
|
|
@ -21,7 +22,7 @@ BOOST_AUTO_TEST_CASE( registering_to_a_sequence_and_calling_out_of_order_throws
|
|||
e.expect().once().with( 1 ).in( s );
|
||||
e.expect().once().with( 2 ).in( s );
|
||||
BOOST_CHECK_NO_THROW( e( 2 ) );
|
||||
BOOST_CHECK_THROW( e( 1 ), mock::exception );
|
||||
BOOST_CHECK_THROW( e( 1 ), std::exception );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( registering_to_a_sequence_and_calling_out_of_order_multiple_invocations_throws )
|
||||
|
|
@ -32,7 +33,7 @@ BOOST_AUTO_TEST_CASE( registering_to_a_sequence_and_calling_out_of_order_multipl
|
|||
e.expect().once().with( 2 ).in( s );
|
||||
BOOST_CHECK_NO_THROW( e( 1 ) );
|
||||
BOOST_CHECK_NO_THROW( e( 2 ) );
|
||||
BOOST_CHECK_THROW( e( 1 ), mock::exception );
|
||||
BOOST_CHECK_THROW( e( 1 ), std::exception );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( registering_to_a_sequence_and_calling_in_order_is_valid )
|
||||
|
|
@ -52,7 +53,7 @@ BOOST_AUTO_TEST_CASE( registering_to_a_sequence_enforces_call_order_verification
|
|||
e1.expect().once().in( s );
|
||||
e2.expect().once().in( s );
|
||||
BOOST_CHECK_NO_THROW( e2() );
|
||||
BOOST_CHECK_THROW( e1(), mock::exception );
|
||||
BOOST_CHECK_THROW( e1(), std::exception );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( destroying_a_sequence_removes_order_call_enforcement )
|
||||
|
|
|
|||
43
src/tests/turtle_test/silent_error.hpp
Normal file
43
src/tests/turtle_test/silent_error.hpp
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
//
|
||||
// Copyright Mathieu Champlon 2008
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef MOCK_TEST_SILENT_ERROR_HPP_INCLUDED
|
||||
#define MOCK_TEST_SILENT_ERROR_HPP_INCLUDED
|
||||
|
||||
#include <string>
|
||||
#include <exception>
|
||||
|
||||
namespace mock
|
||||
{
|
||||
template< typename Result >
|
||||
struct silent_error
|
||||
{
|
||||
static Result no_match( const std::string& /*context*/ )
|
||||
{
|
||||
throw std::exception();
|
||||
}
|
||||
static Result missing_result_specification( const std::string& /*context*/,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
throw std::exception();
|
||||
}
|
||||
static void sequence_failed( const std::string& /*context*/,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
throw std::exception();
|
||||
}
|
||||
static void verification_failed( const std::string& /*context*/,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{}
|
||||
static void untriggered_expectation( const std::string& /*context*/,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // #ifndef MOCK_TEST_SILENT_ERROR_HPP_INCLUDED
|
||||
Loading…
Add table
Add a link
Reference in a new issue