Initial import

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@2 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2009-08-26 22:02:18 +00:00
parent 8081e7006f
commit 8e18676b92
31 changed files with 4062 additions and 0 deletions

View file

@ -0,0 +1,164 @@
//
// 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)
//
#include <turtle/constraint.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
BOOST_AUTO_TEST_CASE( all_comparison_constraints_can_be_instanciated )
{
mock::any;
mock::negate;
mock::evaluate;
mock::equal( 0 );
mock::less( 0 );
mock::greater( 0 );
mock::less_equal( 0 );
mock::greater_equal( 0 );
}
BOOST_AUTO_TEST_CASE( constraints_can_be_negated_using_the_not_operator )
{
! mock::any;
! mock::negate;
! mock::evaluate;
! mock::equal( 0 );
! mock::less( 0 );
! mock::greater( 0 );
! mock::less_equal( 0 );
! mock::greater_equal( 0 );
}
BOOST_AUTO_TEST_CASE( constraints_can_be_combined_using_the_or_operator )
{
mock::less( 0 ) || mock::greater( 0 );
}
BOOST_AUTO_TEST_CASE( constraints_can_be_combined_using_the_and_operator )
{
mock::less( 0 ) && mock::greater( 0 );
}
BOOST_AUTO_TEST_CASE( same )
{
int i = 0;
int j = 0;
BOOST_CHECK_EQUAL( i, j );
BOOST_CHECK( ! mock::same( i ).functor_( j ) );
BOOST_CHECK( mock::same( i ).functor_( i ) );
}
BOOST_AUTO_TEST_CASE( assign )
{
{
int i = 0;
BOOST_CHECK( mock::assign( 3 ).functor_( i ) );
BOOST_CHECK_EQUAL( 3, i );
}
{
const int* i = 0;
const int j = 1;
BOOST_CHECK( mock::assign( &j ).functor_( i ) );
BOOST_CHECK_EQUAL( i, &j );
}
}
BOOST_AUTO_TEST_CASE( retrieve )
{
{
int i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( i ).functor_( j ) );
BOOST_CHECK_EQUAL( i, j );
}
{
int* i = 0;
int j = 1;
BOOST_CHECK( mock::retrieve( i ).functor_( &j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
const int* i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( i ).functor_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
int* i = 0;
int j = 1;
BOOST_CHECK( mock::retrieve( i ).functor_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
const int* i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( i ).functor_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
int** i = 0;
int* j = 0;
BOOST_CHECK( mock::retrieve( i ).functor_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
const int** i = 0;
const int* j = 0;
BOOST_CHECK( mock::retrieve( i ).functor_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
}
namespace
{
struct A
{
};
struct B
{
B& operator=( const A& )
{
return *this;
}
};
}
BOOST_AUTO_TEST_CASE( retrieve_uses_assignment_operator )
{
B b;
const A a = A();
mock::retrieve( b ).functor_( a );
}
BOOST_AUTO_TEST_CASE( negate )
{
int* i = 0;
int j;
BOOST_CHECK( mock::negate.functor_( i ) );
BOOST_CHECK( ! mock::negate.functor_( &j ) );
}
namespace
{
bool return_true()
{
return true;
}
bool return_false()
{
return false;
}
}
BOOST_AUTO_TEST_CASE( evaluate )
{
BOOST_CHECK( mock::evaluate.functor_( &return_true ) );
BOOST_CHECK( ! mock::evaluate.functor_( &return_false ) );
}

View file

@ -0,0 +1,27 @@
//
// 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)
//
#include <turtle/error.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
BOOST_AUTO_TEST_CASE( a_mock_exception_is_not_an_std_exception_to_not_mess_with_user_exceptions )
{
try
{
throw mock::exception( "" );
}
catch( std::exception& )
{
BOOST_FAIL( "mock::exception must not be an std::exception" );
}
catch( mock::exception& )
{}
}

View file

@ -0,0 +1,762 @@
//
// 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)
//
#define BOOST_AUTO_TEST_MAIN
#include <turtle/expectation.hpp>
#include <turtle/constraint.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
namespace
{
bool check_exception_contains( const mock::exception& e, const std::string& str )
{
return e.what().find( str ) != std::string::npos;
}
}
#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_using_boost_bind_and_boost_ref )
{
mock::expectation< void() > e;
boost::function< void() > f = boost::bind( boost::ref( e ) );
}
// invocations
BOOST_AUTO_TEST_CASE( triggering_an_empty_expectation_throws )
{
{
mock::expectation< void() > e;
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "unexpected call" );
}
{
mock::expectation< int( int, const std::string& ) > e;
BOOST_CHECK_EXCEPTION_CONTAINS( e( 1, "s" ), "unexpected call" );
}
}
BOOST_AUTO_TEST_CASE( triggering_a_never_expectation_throws )
{
{
mock::expectation< void() > e;
e.expect().never();
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "unexpected call" );
}
{
mock::expectation< int( int, const std::string& ) > e;
e.expect().never();
BOOST_CHECK_EXCEPTION_CONTAINS( e( 1, "s" ), "unexpected call" );
}
}
BOOST_AUTO_TEST_CASE( triggering_an_unlimited_expectation_never_throws )
{
{
mock::expectation< void() > e;
e.expect();
BOOST_CHECK_NO_THROW( e() );
BOOST_CHECK_NO_THROW( 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" ) );
}
}
BOOST_AUTO_TEST_CASE( triggering_a_once_expectation_throws_after_one_call )
{
{
mock::expectation< void() > e;
e.expect().once();
BOOST_CHECK_NO_THROW( e() );
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "unexpected call" );
}
{
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" );
}
}
/*
BOOST_AUTO_TEST_CASE( literal_zero_can_be_used_in_expectation_operator_call_as_pointers )
{
mock::expectation< void( int* ) > e;
e( 0 );
}
*/
// verify
BOOST_AUTO_TEST_CASE( verifying_an_empty_expectation_succeeds )
{
{
mock::expectation< void() > e;
BOOST_CHECK( e.verify() );
}
{
mock::expectation< int( int, const std::string& ) > e;
BOOST_CHECK( e.verify() );
}
}
BOOST_AUTO_TEST_CASE( verifying_an_unlimited_expectation_succeeds )
{
{
mock::expectation< void() > e;
e.expect();
BOOST_CHECK( e.verify() );
}
{
mock::expectation< int( int, const std::string& ) > e;
e.expect();
BOOST_CHECK( e.verify() );
}
}
BOOST_AUTO_TEST_CASE( verifying_a_once_expectation_after_one_call_succeeds )
{
{
mock::expectation< void() > e;
e.expect().once();
e();
BOOST_CHECK( e.verify() );
}
{
mock::expectation< void( int, const std::string& ) > e;
e.expect().once();
e( 1, "s" );
BOOST_CHECK( e.verify() );
}
}
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 )
{
{
mock::expectation< void(), silent_error< void > > e;
e.expect().once();
BOOST_CHECK( ! e.verify() );
}
{
mock::expectation< int( int, const std::string& ), silent_error< int > > e;
e.expect().once();
BOOST_CHECK( ! e.verify() );
}
}
// reset
BOOST_AUTO_TEST_CASE( triggering_a_reset_expectation_throws )
{
{
mock::expectation< void() > e;
e.expect();
e.reset();
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "unexpected call" );
}
{
mock::expectation< int( int, const std::string& ) > e;
e.expect();
e.reset();
BOOST_CHECK_EXCEPTION_CONTAINS( e( 1, "s" ), "unexpected call" );
}
}
BOOST_AUTO_TEST_CASE( verifying_a_reset_expectation_succeeds )
{
{
mock::expectation< void() > e;
e.expect();
e.reset();
BOOST_CHECK( e.verify() );
}
{
mock::expectation< int( int, const std::string& ) > e;
e.expect();
e.reset();
BOOST_CHECK( e.verify() );
}
}
// constraints
BOOST_AUTO_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_constraint_throws )
{
{
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( 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 )
{
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" );
}
BOOST_AUTO_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_and_not_less_constraint_throws )
{
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" );
}
namespace
{
class my_interface : boost::noncopyable
{
public:
virtual ~my_interface() {}
private:
virtual void my_method() = 0;
};
class my_implementation : public my_interface
{
virtual void my_method() {}
};
}
BOOST_AUTO_TEST_CASE( passing_call_values_by_reference_is_transparent )
{
{
mock::expectation< void( my_interface& ) > e;
my_implementation imp;
e.expect().with( mock::same( imp ) );
e( imp );
}
{
mock::expectation< void( const my_interface& ) > e;
my_implementation imp;
e.expect().with( mock::same( imp ) );
e( imp );
}
}
namespace
{
bool custom_constraint( int )
{
return false;
}
}
BOOST_AUTO_TEST_CASE( triggering_an_expectation_with_failing_custom_constraint_throws )
{
{
mock::expectation< void( int ) > e;
e.expect().with( mock::constraint( &custom_constraint ) );
BOOST_CHECK_EXCEPTION_CONTAINS( e( 42 ), "unexpected call" );
}
{
mock::expectation< int( int, const std::string& ) > e;
e.expect().with( mock::constraint( &custom_constraint ), "actual" );
BOOST_CHECK_EXCEPTION_CONTAINS( e( 42, "actual" ), "unexpected call" );
}
}
BOOST_AUTO_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_custom_constraint_throws )
{
mock::expectation< void( int ) > e;
e.expect().with( mock::constraint( &custom_constraint, "custom constraint" ) );
BOOST_CHECK_EXCEPTION_CONTAINS( e( 42 ), "custom constraint" );
}
/*
BOOST_AUTO_TEST_CASE( literal_zero_can_be_used_in_place_of_null_pointers_in_constraints )
{
mock::expectation< void( int* ) > e;
e.expect().with( 0 );
e.reset();
}
*/
// result handling
BOOST_AUTO_TEST_CASE( triggering_an_expectation_with_no_return_set_throws )
{
{
mock::expectation< int() > e;
e.expect();
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "missing result specification" );
}
{
mock::expectation< int&() > e;
e.expect();
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "missing result specification" );
}
{
mock::expectation< const std::string&() > e;
e.expect();
BOOST_CHECK_EXCEPTION_CONTAINS( e(), "missing result specification" );
}
}
BOOST_AUTO_TEST_CASE( triggering_an_expectation_returns_the_set_value )
{
{
mock::expectation< int() > e;
e.expect().returns( 42 );
BOOST_CHECK_EQUAL( 42, e() );
}
{
mock::expectation< int() > e;
const int i = 42;
e.expect().returns( i );
BOOST_CHECK_EQUAL( i, e() );
}
{
mock::expectation< int() > e;
int i = 42;
e.expect().returns( boost::ref( i ) );
i = 43;
BOOST_CHECK_EQUAL( 43, e() );
}
{
mock::expectation< int&() > e;
e.expect().returns( 42 );
BOOST_CHECK_EQUAL( 42, e() );
}
{
mock::expectation< int&() > e;
const int result = 42;
e.expect().returns( result );
BOOST_CHECK_EQUAL( result, e() );
}
{
mock::expectation< int&() > e;
int i = 42;
e.expect().returns( i );
i = 43;
BOOST_CHECK_EQUAL( 42, e() );
}
{
mock::expectation< int&() > e;
int i = 42;
e.expect().returns( boost::ref( i ) );
i = 43;
BOOST_CHECK_EQUAL( 43, e() );
BOOST_CHECK_EQUAL( 12, e() = 12 );
BOOST_CHECK_EQUAL( 12, i );
}
{
mock::expectation< std::string() > e;
e.expect().returns( "result" );
BOOST_CHECK_EQUAL( "result", e() );
}
{
mock::expectation< const std::string&() > e;
e.expect().returns( "result" );
BOOST_CHECK_EQUAL( "result", e() );
}
{
mock::expectation< int*() > e;
e.expect().returns( 0 );
BOOST_CHECK( ! e() );
}
{
mock::expectation< int() > e;
e.expect().returns( 0 );
BOOST_CHECK_EQUAL( 0, e() );
}
{
mock::expectation< int&() > e;
e.expect().returns( 0 );
BOOST_CHECK_EQUAL( 0, e() );
}
}
namespace
{
struct A {};
struct B : A {};
}
BOOST_AUTO_TEST_CASE( triggering_an_expectation_returns_the_set_auto_ptr_value )
{
{
mock::expectation< std::auto_ptr< int >() > e;
std::auto_ptr< int > ptr( new int( 3 ) );
e.expect().returns( boost::ref( ptr ) );
BOOST_CHECK_EQUAL( 3, *ptr );
BOOST_CHECK_EQUAL( 3, *e() );
BOOST_CHECK( ! ptr.get() );
BOOST_CHECK( ! e().get() );
}
{
mock::expectation< std::auto_ptr< int >() > e;
std::auto_ptr< int > ptr( new int( 3 ) );
e.expect().returns( ptr );
BOOST_CHECK( ! ptr.get() );
BOOST_CHECK_EQUAL( 3, *e() );
BOOST_CHECK( ! e().get() );
}
{
mock::expectation< std::auto_ptr< int >() > e;
e.expect().returns( new int( 3 ) );
BOOST_CHECK_EQUAL( 3, *e() );
BOOST_CHECK( ! e().get() );
}
{
mock::expectation< std::auto_ptr< int >() > e;
e.expect().returns( std::auto_ptr< int >( new int( 3 ) ) );
BOOST_CHECK_EQUAL( 3, *e() );
BOOST_CHECK( ! e().get() );
}
{
mock::expectation< std::auto_ptr< A >() > e;
e.expect().returns( new B );
BOOST_CHECK_NO_THROW( e() );
}
{
mock::expectation< std::auto_ptr< A >() > e;
e.expect().returns( std::auto_ptr< A >( new B ) );
BOOST_CHECK_NO_THROW( e() );
}
{
mock::expectation< std::auto_ptr< A >() > e;
e.expect().returns( std::auto_ptr< B >( new B ) );
BOOST_CHECK_NO_THROW( e() );
}
}
namespace
{
int custom_result()
{
return 42;
}
}
BOOST_AUTO_TEST_CASE( triggering_an_expectation_calls_the_custom_functor )
{
mock::expectation< int() > e;
e.expect().calls( &custom_result );
BOOST_CHECK_EQUAL( 42, e() );
}
namespace
{
int custom_result_with_parameter( int i )
{
return i;
}
}
BOOST_AUTO_TEST_CASE( triggering_an_expectation_calls_the_custom_functor_with_parameters )
{
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 )
{
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 )
{
mock::expectation< void() > e;
e.expect().throws( std::runtime_error( "some exception" ) );
try
{
e();
}
catch( std::runtime_error& e )
{
BOOST_CHECK_EQUAL( "some exception", e.what() );
return;
}
BOOST_FAIL( "should have thrown" );
}
// multiple matchers
BOOST_AUTO_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_twice )
{
{
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" );
}
{
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" );
}
}
BOOST_AUTO_TEST_CASE( best_matcher_is_selected_first )
{
{
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" );
}
{
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" );
}
}
// error report
BOOST_AUTO_TEST_CASE( expectation_can_be_serialized_to_be_human_readable )
{
{
mock::expectation< void( int ) > e( "my expectation" );
e.expect().once().with( 1 );
e.expect().once().with( 2 );
BOOST_CHECK_NO_THROW( e( 2 ) );
std::stringstream s;
s << e;
const std::string expected = "my expectation\n"
". expect( once() ).with( 1 )\n"
"v expect( once() ).with( 2 )";
BOOST_CHECK_EQUAL( expected, s.str() );
e.reset();
}
{
mock::expectation< void( int ) > e( "my expectation" );
e.expect().never().with( 1 );
std::stringstream s;
s << e;
const std::string expected = "my expectation\n"
"v expect( never() ).with( 1 )";
BOOST_CHECK_EQUAL( expected, s.str() );
e.reset();
}
{
mock::expectation< void( const std::string& ) > e;
e.expect().never().with( mock::less( "first" ) );
e.expect().exactly( 2 ).with( "second" );
BOOST_CHECK_NO_THROW( e( "second" ) );
{
std::stringstream s;
s << e;
const std::string expected = "?\n"
"v expect( never() ).with( less( first ) )\n"
". expect( exactly( 1/2 ) ).with( second )";
BOOST_CHECK_EQUAL( expected, s.str() );
}
BOOST_CHECK_NO_THROW( e( "second" ) );
{
std::stringstream s;
s << e;
const std::string expected = "?\n"
"v expect( never() ).with( less( first ) )\n"
"v expect( exactly( 2/2 ) ).with( second )";
BOOST_CHECK_EQUAL( expected, s.str() );
}
e.reset();
}
{
mock::expectation< void( int ) > e( "my expectation" );
e.expect().once();
std::stringstream s;
s << e;
const std::string expected = "my expectation\n"
". expect( once() ).with( any )";
BOOST_CHECK_EQUAL( expected, s.str() );
e.reset();
}
{
mock::expectation< void( int ) > e( "my expectation" );
e.expect().once().with( mock::any );
std::stringstream s;
s << e;
const std::string expected = "my expectation\n"
". expect( once() ).with( any )";
BOOST_CHECK_EQUAL( expected, s.str() );
e.reset();
}
{
mock::expectation< void( int ) > e( "my expectation" );
e.expect().once();
std::stringstream s;
s << e;
const std::string expected = "my expectation\n"
". expect( once() ).with( any )";
BOOST_CHECK_EQUAL( expected, s.str() );
e.reset();
}
{
mock::expectation< void( int ) > e( "my expectation" );
e.expect().once().with( mock::constraint( &custom_constraint ) );
std::stringstream s;
s << e;
const std::string expected = "my expectation\n"
". expect( once() ).with( ? )";
BOOST_CHECK_EQUAL( expected, s.str() );
e.reset();
}
{
mock::expectation< void( int ) > e( "my expectation" );
e.expect().once().with( mock::constraint( &custom_constraint, "custom constraint" ) );
std::stringstream s;
s << e;
const std::string expected = "my expectation\n"
". expect( once() ).with( custom constraint )";
BOOST_CHECK_EQUAL( expected, s.str() );
e.reset();
}
{
mock::expectation< void( int ) > e( "my expectation" );
e.expect().once().with( mock::constraint( &custom_constraint, true ) );
std::stringstream s;
s << e;
const std::string expected = "my expectation\n"
". expect( once() ).with( true )";
BOOST_CHECK_EQUAL( expected, s.str() );
e.reset();
}
}
namespace
{
mock::expectation< void() > no_match_exp( "no_match" );
mock::expectation< void() > sequence_failed_exp( "sequence_failed" );
mock::expectation< void() > verification_failed_exp( "verification_failed" );
mock::expectation< void() > untriggered_expectation_exp( "untriggered_expectation" );
struct mock_error
{
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();
}
};
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 )
{
mock::expectation< void(), mock_error > e;
e.expect().once();
untriggered_expectation_exp.expect().once();
}
BOOST_FIXTURE_TEST_CASE( verifying_expectation_with_remaining_matches_disables_the_automatic_verification_upon_destruction, error_guard )
{
mock::expectation< void(), mock_error > e;
e.expect().once();
verification_failed_exp.expect();
e.verify();
}
BOOST_FIXTURE_TEST_CASE( triggering_no_match_call_disables_the_automatic_verification_upon_destruction, error_guard )
{
mock::expectation< void(), mock_error > e;
no_match_exp.expect();
e();
}
BOOST_FIXTURE_TEST_CASE( adding_a_matcher_reactivates_the_verification_upon_destruction, error_guard )
{
mock::expectation< void(), mock_error > e;
no_match_exp.expect();
e();
e.expect().once();
untriggered_expectation_exp.expect().once();
}

View 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)
//
#include <turtle/format.hpp>
#include <ostream>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
namespace
{
struct non_serializable_type {};
struct serializable_type {};
std::ostream& operator<<( std::ostream& s, const serializable_type& )
{
return s << "serializable_type";
}
}
BOOST_AUTO_TEST_CASE( type_not_serializable_in_standard_stream_yields_an_interrogation_mark_when_serialized )
{
BOOST_CHECK_EQUAL( "?", mock::detail::format( non_serializable_type() ) );
}
BOOST_AUTO_TEST_CASE( base_type_serializable_in_standard_stream_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "42", mock::detail::format( 42 ) );
}
BOOST_AUTO_TEST_CASE( custom_type_serializable_in_standard_stream_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "serializable_type", mock::detail::format( serializable_type() ) );
}
namespace
{
struct convertible_to_int
{
operator int() const { return 0; }
};
}
BOOST_AUTO_TEST_CASE( custom_type_convertible_to_base_type_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "0", mock::detail::format( convertible_to_int() ) );
}
namespace
{
struct serializable
{
friend std::ostream& operator<<( std::ostream& s, const serializable& )
{
return s << "serializable";
}
};
struct convertible_to_serializable
{
operator serializable() const { return serializable(); }
};
}
BOOST_AUTO_TEST_CASE( custom_type_convertible_to_another_type_serializable_in_standard_stream_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "serializable", mock::detail::format( convertible_to_serializable() ) );
}
BOOST_AUTO_TEST_CASE( booleans_are_serialized_as_booleans )
{
BOOST_CHECK_EQUAL( "true", mock::detail::format( true ) );
BOOST_CHECK_EQUAL( "false", mock::detail::format( false ) );
}

View 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)
//
#include <turtle/invocation.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
BOOST_AUTO_TEST_CASE( unlimited )
{
mock::detail::unlimited invocation;
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( invocation.is_valid() );
BOOST_CHECK( invocation.invoke() );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( invocation.is_valid() );
BOOST_CHECK( invocation.invoke() );
}
BOOST_AUTO_TEST_CASE( once )
{
mock::detail::once invocation;
BOOST_CHECK( ! invocation.verify() );
BOOST_CHECK( invocation.is_valid() );
BOOST_CHECK( invocation.invoke() );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( ! invocation.is_valid() );
BOOST_CHECK( ! invocation.invoke() );
}
BOOST_AUTO_TEST_CASE( never )
{
mock::detail::never invocation;
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( ! invocation.is_valid() );
BOOST_CHECK( ! invocation.invoke() );
}
BOOST_AUTO_TEST_CASE( at_most )
{
mock::detail::at_most invocation( 1 );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( invocation.is_valid() );
BOOST_CHECK( invocation.invoke() );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( ! invocation.is_valid() );
BOOST_CHECK( ! invocation.invoke() );
}
BOOST_AUTO_TEST_CASE( at_least )
{
mock::detail::at_least invocation( 1 );
BOOST_CHECK( ! invocation.verify() );
BOOST_CHECK( invocation.is_valid() );
BOOST_CHECK( invocation.invoke() );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( invocation.is_valid() );
BOOST_CHECK( invocation.invoke() );
}
BOOST_AUTO_TEST_CASE( between )
{
mock::detail::between invocation( 1, 2 );
BOOST_CHECK( ! invocation.verify() );
BOOST_CHECK( invocation.is_valid() );
BOOST_CHECK( invocation.invoke() );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( invocation.is_valid() );
BOOST_CHECK( invocation.invoke() );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( ! invocation.is_valid() );
BOOST_CHECK( ! invocation.invoke() );
}

View file

@ -0,0 +1,72 @@
//
// 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)
//
#include <turtle/object.hpp>
#include <turtle/expectation.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.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( o );
e.expect().once();
BOOST_CHECK( ! o.verify() );
}
BOOST_AUTO_TEST_CASE( resetting_an_object_containing_a_failing_expectation_and_verifying_it_succeeds )
{
mock::object o;
mock::expectation< void() > e( o );
e.expect().once();
o.reset();
BOOST_CHECK( o.verify() );
BOOST_CHECK( e.verify() );
}
BOOST_AUTO_TEST_CASE( verifying_an_object_containing_another_object_with_a_failing_expectation_fails )
{
mock::object o1;
mock::object o2( o1 );
mock::expectation< void(), silent_error > e( o2 );
e.expect().once();
BOOST_CHECK( ! o1.verify() );
}
BOOST_AUTO_TEST_CASE( resetting_an_object_containing_another_object_with_a_failing_expectation_and_verifying_it_succeeds )
{
mock::object o1;
mock::object o2( o1 );
mock::expectation< void() > e( o2 );
e.expect().once();
o1.reset();
BOOST_CHECK( o1.verify() );
BOOST_CHECK( o2.verify() );
BOOST_CHECK( e.verify() );
}

View file

@ -0,0 +1,166 @@
//
// 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)
//
#include <turtle/object.hpp>
#include <turtle/expectation.hpp>
#include <turtle/constraint.hpp>
#include <turtle/tools.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
#include <boost/noncopyable.hpp>
#include <boost/ref.hpp>
#ifdef _MSC_VER
# pragma warning( disable : 4355 4505 )
#endif
namespace
{
MOCK_CLASS( my_mock )
{
MOCK_METHOD_EXT( my_method, 1, int( int ), my_method )
};
}
BOOST_AUTO_TEST_CASE( basic_mock_object_usage )
{
my_mock m;
MOCK_EXPECT( m, my_method ).once().returns( 0 );
BOOST_CHECK_EQUAL( 0, m.my_method( 13 ) );
mock::verify();
mock::reset(); // $$$$ MAT : shouldn't reset implicitly call verify ?
MOCK_EXPECT( m, my_method ).once().with( 42 ).returns( 7 );
BOOST_CHECK_EQUAL( 7, m.my_method( 42 ) );
mock::verify();
mock::reset();
MOCK_EXPECT( m, my_method ).once().returns( 51 );
BOOST_CHECK_EQUAL( 51, m.my_method( 27 ) );
}
namespace
{
class my_observer : boost::noncopyable
{
public:
virtual ~my_observer() {}
virtual void notify( int value ) = 0;
};
class my_manager : boost::noncopyable
{
public:
virtual ~my_manager() {}
virtual my_observer& get_observer() const = 0;
};
class my_subject : boost::noncopyable
{
public:
explicit my_subject( my_manager& f )
: o_( f.get_observer() )
, value_( 0 )
{}
void increment()
{
o_.notify( ++value_ );
}
private:
my_observer& o_;
int value_;
};
MOCK_INTERFACE( my_mock_observer, my_observer )
{
MOCK_METHOD( notify, 1 )
};
MOCK_INTERFACE( my_mock_manager, my_manager )
{
MOCK_METHOD( get_observer, 0 )
};
class fixture
{
public:
fixture()
: manager( "(the only one)" )
{}
my_mock_manager manager;
my_mock_observer observer;
};
}
BOOST_FIXTURE_TEST_CASE( basic_mock_object_collaboration_usage, fixture )
{
MOCK_EXPECT( manager, get_observer ).returns( boost::ref( observer ) );
my_subject subject( manager );
MOCK_EXPECT( observer, notify ).once().with( 1 );
subject.increment();
MOCK_EXPECT( observer, notify ).once().with( 2 );
subject.increment();
MOCK_EXPECT( observer, notify ).once().with( 3 );
subject.increment();
}
namespace
{
class my_ambiguited_interface : boost::noncopyable
{
public:
virtual ~my_ambiguited_interface() {}
virtual void my_method() = 0;
virtual void my_method( int ) = 0;
};
MOCK_INTERFACE( my_ambiguited_mock, my_ambiguited_interface )
{
MOCK_METHOD_EXT( my_method, 0, void(), tag1 )
MOCK_METHOD_EXT( my_method, 1, void( int ), tag2 )
};
}
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 );
}
namespace
{
class my_const_ambiguited_interface : boost::noncopyable
{
public:
virtual ~my_const_ambiguited_interface() {}
virtual void my_method() = 0;
virtual void my_method() const = 0;
};
MOCK_INTERFACE( my_const_ambiguited_mock, my_const_ambiguited_interface )
{
MOCK_NON_CONST_METHOD_EXT( my_method, 0, void(), tag1 )
MOCK_CONST_METHOD_EXT( my_method, 0, void(), tag2 )
};
}
BOOST_AUTO_TEST_CASE( mock_object_method_const_disambiguation )
{
my_const_ambiguited_mock mock;
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 );
}

View file

@ -0,0 +1,78 @@
//
// 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)
//
#include <turtle/sequence.hpp>
#include <turtle/expectation.hpp>
#include <turtle/constraint.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
BOOST_AUTO_TEST_CASE( registering_to_a_sequence_and_calling_out_of_order_throws )
{
mock::sequence s;
mock::expectation< void( int ) > e;
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_AUTO_TEST_CASE( registering_to_a_sequence_and_calling_out_of_order_multiple_invocations_throws )
{
mock::sequence s;
mock::expectation< void( int ) > e;
e.expect().with( 1 ).in( s );
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_AUTO_TEST_CASE( registering_to_a_sequence_and_calling_in_order_is_valid )
{
mock::sequence s;
mock::expectation< void( int ) > e;
e.expect().once().with( 1 ).in( s );
e.expect().once().with( 2 ).in( s );
BOOST_CHECK_NO_THROW( e( 1 ) );
BOOST_CHECK_NO_THROW( e( 2 ) );
}
BOOST_AUTO_TEST_CASE( registering_to_a_sequence_enforces_call_order_verification_between_two_different_expectations )
{
mock::sequence s;
mock::expectation< void() > e1, e2;
e1.expect().once().in( s );
e2.expect().once().in( s );
BOOST_CHECK_NO_THROW( e2() );
BOOST_CHECK_THROW( e1(), mock::exception );
}
BOOST_AUTO_TEST_CASE( destroying_a_sequence_removes_order_call_enforcement )
{
mock::expectation< void() > e1, e2;
{
mock::sequence s;
e1.expect().once().in( s );
e2.expect().once().in( s );
}
BOOST_CHECK_NO_THROW( e2() );
BOOST_CHECK_NO_THROW( e1() );
}
BOOST_AUTO_TEST_CASE( resetting_an_expectation_removes_it_from_order_call_enforcement )
{
mock::sequence s;
mock::expectation< void() > e1, e2;
e1.expect().once().in( s );
e2.expect().once().in( s );
e1.reset();
BOOST_CHECK_NO_THROW( e2() );
}

View file

@ -0,0 +1,143 @@
//
// Copyright Mathieu Champlon 2009
//
// 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)
//
#include <turtle/tools.hpp>
#include <boost/static_assert.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
#ifdef _MSC_VER
# pragma warning( disable : 4355 )
#endif
namespace
{
void f1();
int f2( float );
BOOST_STATIC_ASSERT( (boost::mpl::equal< mock::expectation< void() >,
mock::expectation< BOOST_TYPEOF( f1 ) >
>::value) );
BOOST_STATIC_ASSERT( (boost::mpl::equal< mock::expectation< int( float ) >,
mock::expectation< BOOST_TYPEOF( f2 ) >
>::value) );
struct example
{
void method1();
float method2( int );
};
BOOST_STATIC_ASSERT(
(boost::mpl::equal<
mock::expectation< void() >,
mock::expectation<
mock::detail::signature<
BOOST_TYPEOF( &example::method1 )
>::type
>
>::value) );
BOOST_STATIC_ASSERT(
(boost::mpl::equal<
mock::expectation< float( int ) >,
mock::expectation<
mock::detail::signature<
BOOST_TYPEOF( &example::method2 )
>::type
>
>::value) );
}
BOOST_AUTO_TEST_CASE( ptr_uniformizes_reference_and_pointer )
{
int i = 0;
BOOST_CHECK_EQUAL( mock::detail::ref( i ), mock::detail::ref( &i ) );
}
BOOST_AUTO_TEST_CASE( ptr_accesses_inner_pointer_from_auto_ptr )
{
std::auto_ptr< int > i( new int( 0 ) );
BOOST_CHECK_EQUAL( *i, mock::detail::ref( i ) );
}
BOOST_AUTO_TEST_CASE( ptr_accesses_inner_pointer_from_shared_ptr )
{
boost::shared_ptr< int > i( new int( 0 ) );
BOOST_CHECK_EQUAL( *i, mock::detail::ref( i ) );
}
namespace
{
template< typename T >
void my_function( T& t )
{
t.my_method( "some parameter" );
}
MOCK_CLASS( mock_class )
{
MOCK_METHOD_EXT( my_method, 1, void( const std::string& ), my_method )
};
}
BOOST_AUTO_TEST_CASE( mock_object_for_static_polymorphism )
{
const mock_class mock;
MOCK_EXPECT( mock, my_method ).once().with( "some parameter" );
my_function( mock );
}
namespace
{
MOCK_CLASS( mock_class_with_operator )
{
MOCK_CONST_METHOD_EXT( operator+=, 1, mock_class_with_operator&( int ), addition )
};
}
BOOST_AUTO_TEST_CASE( mock_addition_operator )
{
mock_class_with_operator mock;
MOCK_EXPECT( mock, addition ).once().returns( boost::ref( mock ) );
mock += 1;
}
namespace
{
MOCK_CLASS( my_mock )
{
MOCK_METHOD_EXT( my_method, 1, void( int ), my_method )
};
}
BOOST_AUTO_TEST_CASE( MOCK_METHOD_EXT_macro_defines_a_bindable_method )
{
my_mock m;
boost::bind( &my_mock::my_method, &m, 42 );
}
BOOST_AUTO_TEST_CASE( MOCK_VERIFY_macro )
{
my_mock m;
MOCK_VERIFY( m, my_method );
}
BOOST_AUTO_TEST_CASE( MOCK_RESET_macro )
{
my_mock m;
MOCK_RESET( m, my_method );
}
BOOST_AUTO_TEST_CASE( MOCK_EXPECT_macro )
{
my_mock m;
MOCK_EXPECT( m, my_method ).once().with( 42 );
m.my_method( 42 );
}