Changed project layout

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@460 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2012-06-15 21:14:48 +00:00
parent b22842eade
commit 3358ddda0e
55 changed files with 32 additions and 0 deletions

15
test/bench_0_class.cpp Normal file
View file

@ -0,0 +1,15 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
int main()
{
return 0;
}

View file

@ -0,0 +1,11 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#define MOCK_MAX_ARGS 10
#include <turtle/mock.hpp>

View file

@ -0,0 +1,11 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#define MOCK_MAX_ARGS 20
#include <turtle/mock.hpp>

View file

@ -0,0 +1,11 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#define MOCK_MAX_ARGS 30
#include <turtle/mock.hpp>

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,25 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
namespace
{
struct my_base
{
virtual ~my_base() {}
virtual void my_method() = 0;
virtual void my_method( int ) = 0;
};
MOCK_BASE_CLASS( my_class, my_base )
{
MOCK_METHOD( my_method, 0 )
};
}

View file

@ -0,0 +1,25 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
namespace
{
MOCK_CLASS( my_class )
{
MOCK_METHOD_EXT( my_method, 1, void( int ), my_method )
};
bool constraint( int, int );
void test_case()
{
my_class c;
MOCK_EXPECT( c.my_method ).with( &constraint );
}
}

View file

@ -0,0 +1,23 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
namespace
{
MOCK_CLASS( my_class )
{
MOCK_METHOD_EXT( my_method, 1, void( int ), my_method )
};
void test_case()
{
my_class c;
MOCK_EXPECT( c.my_method ).with( "42" );
}
}

View file

@ -0,0 +1,23 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
namespace
{
MOCK_CLASS( my_class )
{
MOCK_METHOD_EXT( my_method, 1, void( int ), my_method )
};
void test_case()
{
my_class c;
MOCK_EXPECT( c.my_method ).with( mock::equal( "42" ) );
}
}

View file

@ -0,0 +1,23 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
namespace
{
MOCK_CLASS( my_class )
{
MOCK_METHOD_EXT( my_method, 0, int(), my_method )
};
void test_case()
{
my_class c;
MOCK_EXPECT( c.my_method ).returns( "42" );
}
}

View file

@ -0,0 +1,23 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
namespace
{
MOCK_CLASS( my_class )
{
MOCK_METHOD_EXT( my_method, 0, std::string(), my_method )
};
void test_case()
{
my_class c;
MOCK_EXPECT( c.my_method ).returns( 42 );
}
}

View file

@ -0,0 +1,23 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
namespace
{
MOCK_CLASS( my_class )
{
MOCK_METHOD_EXT( my_method, 0, void(), my_method )
};
void test_case()
{
my_class c;
MOCK_EXPECT( c.my_method ).returns( "42" );
}
}

View file

@ -0,0 +1,23 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
namespace
{
struct my_base
{
virtual ~my_base() {}
};
MOCK_BASE_CLASS( my_class, my_base )
{
MOCK_METHOD( my_method, 0 )
};
}

View file

@ -0,0 +1,26 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
BOOST_STATIC_ASSERT( MOCK_MAX_ARGS == 9 );
namespace
{
struct my_base
{
virtual ~my_base() {}
virtual void my_method( int, int, int, int, int, int, int, int, int, int ) = 0;
};
MOCK_BASE_CLASS( my_class, my_base )
{
MOCK_METHOD( my_method, 10 )
};
}

View file

@ -0,0 +1,19 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
namespace
{
template< typename T >
MOCK_CLASS( my_class )
{
MOCK_METHOD_EXT( my_method, 1, void( T ), my_method )
};
}

View file

@ -0,0 +1,24 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
namespace
{
struct my_base
{
virtual ~my_base() {}
virtual void my_method( int ) = 0;
};
MOCK_BASE_CLASS( my_class, my_base )
{
MOCK_METHOD( my_method, 2 )
};
}

View file

@ -0,0 +1,23 @@
//
// Copyright Mathieu Champlon 2011
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
namespace
{
MOCK_CLASS( my_class )
{
MOCK_METHOD_EXT( my_method, 1, void( int ), my_method )
};
void test_case()
{
my_class c;
MOCK_EXPECT( c.my_method ).with( 42, 42 );
}
}

85
test/mock_error.hpp Normal file
View file

@ -0,0 +1,85 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#ifndef MOCK_TEST_MOCK_ERROR_HPP_INCLUDED
#define MOCK_TEST_MOCK_ERROR_HPP_INCLUDED
#define MOCK_ERROR_POLICY mock_error
#include <boost/lexical_cast.hpp>
#include <stdexcept>
namespace
{
int missing_action_count = 0;
int expected_call_count = 0;
int unexpected_call_count = 0;
int sequence_failed_count = 0;
int verification_failed_count = 0;
int untriggered_expectation_count = 0;
std::string last_context;
}
namespace mock
{
template< typename Result >
struct mock_error
{
static Result abort()
{
throw std::runtime_error( "aborted" );
}
static void checkpoint( const char* /*file*/, int /*line*/ )
{}
template< typename Context >
static void missing_action( const Context& context,
const char* /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++missing_action_count;
}
template< typename Context >
static void expected_call( const Context& /*context*/,
const char* /*file*/, int /*line*/ )
{
last_context.clear();
++expected_call_count;
}
template< typename Context >
static void unexpected_call( const Context& context )
{
last_context = boost::lexical_cast< std::string >( context );
++unexpected_call_count;
}
template< typename Context >
static void sequence_failed( const Context& context,
const char* /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++sequence_failed_count;
}
template< typename Context >
static void verification_failed( const Context& context,
const char* /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++verification_failed_count;
}
template< typename Context >
static void untriggered_expectation( const Context& context,
const char* /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++untriggered_expectation_count;
}
};
}
#endif // MOCK_TEST_MOCK_ERROR_HPP_INCLUDED

21
test/test_args.cpp Normal file
View file

@ -0,0 +1,21 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/args.hpp>
#include <boost/test/auto_unit_test.hpp>
#include <boost/mpl/assert.hpp>
BOOST_MPL_ASSERT(( boost::is_same< float, mock::detail::arg< void( float ), 1, 1 >::type > ));
BOOST_MPL_ASSERT(( boost::is_same< float, mock::detail::arg< void( float, int ), 1, 2 >::type > ));
BOOST_MPL_ASSERT(( boost::is_same< int, mock::detail::arg< void( float, int ), 2, 2 >::type > ));
BOOST_MPL_ASSERT(( boost::is_same< mock::detail::invalid_type, mock::detail::arg< void( float ), 1, 2 >::type > ));
BOOST_MPL_ASSERT(( boost::is_same< mock::detail::invalid_type, mock::detail::arg< void( float ), 2, 2 >::type > ));
BOOST_MPL_ASSERT(( boost::is_same< mock::detail::invalid_type, mock::detail::arg< void( float, int ), 1, 1 >::type > ));
BOOST_MPL_ASSERT(( boost::is_same< mock::detail::invalid_type, mock::detail::arg< void( float, int ), 1, 3 >::type > ));
BOOST_MPL_ASSERT(( boost::is_same< mock::detail::invalid_type, mock::detail::arg< void( float, int ), 2, 3 >::type > ));

333
test/test_constraints.cpp Normal file
View file

@ -0,0 +1,333 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/constraints.hpp>
#include <turtle/operators.hpp>
#include <boost/test/auto_unit_test.hpp>
BOOST_AUTO_TEST_CASE( all_comparison_constraints_can_be_instanciated )
{
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::affirm;
! 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( equal )
{
BOOST_CHECK( mock::equal( std::string( "string" ) ).f_( "string" ) );
BOOST_CHECK( ! mock::equal( std::string( "string" ) ).f_( "not string" ) );
{
std::string s;
mock::constraint<
mock::detail::equal<
boost::reference_wrapper< const std::string >
>
> c = mock::equal( boost::cref( s ) );
s = "string";
BOOST_CHECK( c.f_( "string" ) );
}
}
BOOST_AUTO_TEST_CASE( same )
{
{
int i = 0;
int j = 0;
BOOST_CHECK_EQUAL( i, j );
BOOST_CHECK( ! mock::same( i ).f_( j ) );
BOOST_CHECK( mock::same( i ).f_( i ) );
}
{
int i = 0;
int j = 0;
BOOST_CHECK_EQUAL( i, j );
mock::constraint<
mock::detail::same<
const boost::reference_wrapper< const int >
>
> c = mock::same( boost::cref( i ) );
BOOST_CHECK( ! c.f_( j ) );
BOOST_CHECK( c.f_( i ) );
}
}
BOOST_AUTO_TEST_CASE( assign )
{
{
int i = 0;
BOOST_CHECK( mock::assign( 3 ).f_( i ) );
BOOST_CHECK_EQUAL( 3, i );
}
{
int i = 0;
BOOST_CHECK( mock::assign( 3 ).f_( &i ) );
BOOST_CHECK_EQUAL( 3, i );
}
{
const int* i = 0;
const int j = 1;
BOOST_CHECK( mock::assign( &j ).f_( i ) );
BOOST_CHECK_EQUAL( &j, i );
}
{
int i = 0;
int j = 1;
mock::constraint<
mock::detail::assign<
boost::reference_wrapper< const int >
>
> c = mock::assign( boost::cref( j ) );
BOOST_CHECK( c.f_( i ) );
BOOST_CHECK_EQUAL( 1, i );
j = 3;
BOOST_CHECK( c.f_( i ) );
BOOST_CHECK_EQUAL( 3, i );
}
{
int i = 0;
int j = 1;
mock::constraint<
mock::detail::assign<
boost::reference_wrapper< const int >
>
> c = mock::assign( boost::cref( j ) );
BOOST_CHECK( c.f_( &i ) );
BOOST_CHECK_EQUAL( 1, i );
j = 3;
BOOST_CHECK( c.f_( &i ) );
BOOST_CHECK_EQUAL( 3, i );
}
{
const int* i = 0;
int k = 1;
int* j = &k;
mock::constraint<
mock::detail::assign<
boost::reference_wrapper< int* const >
>
> c = mock::assign( boost::cref( j ) );
BOOST_CHECK( c.f_( i ) );
BOOST_CHECK_EQUAL( j, i );
j = 0;
BOOST_CHECK( c.f_( i ) );
BOOST_CHECK_EQUAL( j, i );
}
}
BOOST_AUTO_TEST_CASE( retrieve )
{
{
int i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, j );
}
{
int* i = 0;
int j = 1;
BOOST_CHECK( mock::retrieve( i ).f_( &j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
const int* i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
const int* i = 0;
int j = 1;
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
int* i = 0;
int j = 1;
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
const int* i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
int** i = 0;
int* j = 0;
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
const int** i = 0;
const int* j = 0;
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
int i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( boost::ref( i ) ).f_( j ) );
BOOST_CHECK_EQUAL( i, j );
}
{
const int* i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( boost::ref( i ) ).f_( 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 ).f_( a );
}
BOOST_AUTO_TEST_CASE( affirm )
{
int* i = 0;
int j;
BOOST_CHECK( ! mock::affirm.f_( i ) );
BOOST_CHECK( mock::affirm.f_( &j ) );
}
BOOST_AUTO_TEST_CASE( negate )
{
int* i = 0;
int j;
BOOST_CHECK( mock::negate.f_( i ) );
BOOST_CHECK( ! mock::negate.f_( &j ) );
}
namespace
{
bool return_true()
{
return true;
}
bool return_false()
{
return false;
}
}
BOOST_AUTO_TEST_CASE( call )
{
BOOST_CHECK( mock::call( &return_true ).f_() );
BOOST_CHECK( ! mock::call( &return_false ).f_() );
}
BOOST_AUTO_TEST_CASE( evaluate )
{
BOOST_CHECK( mock::evaluate.f_( &return_true ) );
BOOST_CHECK( ! mock::evaluate.f_( &return_false ) );
}
BOOST_AUTO_TEST_CASE( contain_with_const_char_ptr )
{
BOOST_CHECK( mock::contain( "string" ).f_( "this is a string" ) );
BOOST_CHECK( mock::contain( "string" ).f_( std::string( "this is a string" ) ) );
BOOST_CHECK( ! mock::contain( "not found" ).f_( "this is a string" ) );
BOOST_CHECK( ! mock::contain( "not found" ).f_( std::string( "this is a string" ) ) );
{
const char* s;
mock::constraint<
mock::detail::contain<
boost::reference_wrapper< const char* const >
>
> c = mock::contain( boost::cref( s ) );
s = "string";
BOOST_CHECK( c.f_( "this is a string" ) );
BOOST_CHECK( c.f_( std::string( "this is a string" ) ) );
s = "not found";
BOOST_CHECK( ! c.f_( "this is a string" ) );
BOOST_CHECK( ! c.f_( std::string( "this is a string" ) ) );
}
}
BOOST_AUTO_TEST_CASE( contain_with_strings )
{
BOOST_CHECK( mock::contain( std::string( "string" ) ).f_( "this is a string" ) );
BOOST_CHECK( mock::contain( std::string( "string" ) ).f_( std::string( "this is a string" ) ) );
BOOST_CHECK( ! mock::contain( std::string( "not found" ) ).f_( "this is a string" ) );
BOOST_CHECK( ! mock::contain( std::string( "not found" ) ).f_( std::string( "this is a string" ) ) );
{
std::string s;
mock::constraint<
mock::detail::contain<
boost::reference_wrapper< const std::string >
>
> c = mock::contain( boost::cref( s ) );
s = "string";
BOOST_CHECK( c.f_( "this is a string" ) );
BOOST_CHECK( c.f_( std::string( "this is a string" ) ) );
s = "not found";
BOOST_CHECK( ! c.f_( "this is a string" ) );
BOOST_CHECK( ! c.f_( std::string( "this is a string" ) ) );
}
}
namespace
{
struct type_with_overloaded_address_operator
{
void operator&() {}
void operator&() const {}
};
}
BOOST_AUTO_TEST_CASE( type_with_overloaded_address_operator_can_be_used_in_constraints )
{
type_with_overloaded_address_operator t;
mock::same( t ).f_( t );
mock::retrieve( t ).f_( t );
type_with_overloaded_address_operator* pt;
mock::retrieve( pt ).f_( t );
}

25
test/test_error.cpp Normal file
View file

@ -0,0 +1,25 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/error.hpp>
#include <boost/test/auto_unit_test.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& )
{}
}

755
test/test_function.cpp Normal file
View file

@ -0,0 +1,755 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#define BOOST_AUTO_TEST_MAIN
#include "mock_error.hpp"
#include <turtle/function.hpp>
#include <turtle/constraints.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#define CHECK_CALLS( calls ) \
BOOST_CHECK_EQUAL( calls, expected_call_count ); \
expected_call_count = 0;
#define CHECK_ERROR( expr, error, calls, context ) \
BOOST_CHECK( verify() ); \
try { expr; } catch( ... ) {} \
BOOST_CHECK_EQUAL( 1, error##_count ); \
CHECK_CALLS( calls ); \
BOOST_CHECK_EQUAL( context, last_context ); \
reset();
namespace
{
struct error_fixture
{
error_fixture()
{
reset();
}
~error_fixture()
{
BOOST_CHECK( verify() );
BOOST_CHECK_EQUAL( 0, expected_call_count );
}
void reset()
{
missing_action_count = 0;
expected_call_count = 0;
unexpected_call_count = 0;
sequence_failed_count = 0;
verification_failed_count = 0;
untriggered_expectation_count = 0;
last_context.clear();
}
bool verify() const
{
return missing_action_count == 0 &&
unexpected_call_count == 0 &&
sequence_failed_count == 0 &&
verification_failed_count == 0 &&
untriggered_expectation_count == 0;
}
};
}
// static
namespace
{
boost::function< void() > static_f;
}
// functor
BOOST_FIXTURE_TEST_CASE( a_function_can_be_passed_as_functor, error_fixture )
{
mock::function< void() > f;
boost::function< void() > functor = f;
}
BOOST_FIXTURE_TEST_CASE( a_function_can_be_passed_as_functor_using_boost_bind_and_boost_ref, error_fixture )
{
mock::function< void() > f;
boost::function< void() > functor = boost::bind( boost::ref( f ) );
}
// invocations
BOOST_FIXTURE_TEST_CASE( triggering_an_unconfigured_function_calls_unexpected_call_error, error_fixture )
{
{
mock::function< void() > f;
CHECK_ERROR( f(), unexpected_call, 0, "?()" );
}
{
mock::function< int( int, const std::string& ) > f;
CHECK_ERROR( f( 1, "s" ), unexpected_call, 0, "?( 1, \"s\" )" );
}
}
BOOST_FIXTURE_TEST_CASE( triggering_a_never_expectation_calls_unexpected_call_error, error_fixture )
{
{
mock::function< void() > f;
f.expect().never();
CHECK_ERROR( f(), unexpected_call, 0, "?()\nv never()" );
}
{
mock::function< int( int, const std::string& ) > f;
f.expect().never();
CHECK_ERROR( f( 1, "s" ), unexpected_call, 0, "?( 1, \"s\" )\nv never().with( any, any )" );
}
}
BOOST_FIXTURE_TEST_CASE( triggering_an_unlimited_expectation_is_valid, error_fixture )
{
{
mock::function< void() > f;
f.expect();
f();
f();
CHECK_CALLS( 2 );
}
{
mock::function< void( int, const std::string& ) > f;
f.expect();
f( 1, "s" );
f( 1, "s" );
CHECK_CALLS( 2 );
}
}
BOOST_FIXTURE_TEST_CASE( triggering_a_once_expectation_calls_unexpected_call_error_after_one_call, error_fixture )
{
{
mock::function< void() > f;
f.expect().once();
f();
CHECK_ERROR( f(), unexpected_call, 1, "?()\nv once()" );
}
{
mock::function< void( int, const std::string& ) > f;
f.expect().once();
f( 1, "s" );
CHECK_ERROR( f( 1, "s" ), unexpected_call, 1, "?( 1, \"s\" )\nv once().with( any, any )" );
}
}
BOOST_FIXTURE_TEST_CASE( literal_zero_can_be_used_in_function_call_as_pointers, error_fixture )
{
mock::function< void( int* ) > f;
f.expect().once();
f( 0 );
CHECK_CALLS( 1 );
}
// verify
BOOST_FIXTURE_TEST_CASE( verifying_an_unconfigured_function_succeeds, error_fixture )
{
{
mock::function< void() > f;
BOOST_CHECK( f.verify() );
}
{
mock::function< int( int, const std::string& ) > f;
BOOST_CHECK( f.verify() );
}
}
BOOST_FIXTURE_TEST_CASE( verifying_an_unlimited_expectation_succeeds, error_fixture )
{
{
mock::function< void() > f;
f.expect();
BOOST_CHECK( f.verify() );
CHECK_CALLS( 0 );
}
CHECK_CALLS( 1 );
{
mock::function< int( int, const std::string& ) > f;
f.expect();
BOOST_CHECK( f.verify() );
CHECK_CALLS( 0 );
}
CHECK_CALLS( 1 );
}
BOOST_FIXTURE_TEST_CASE( verifying_a_once_expectation_after_one_call_succeeds, error_fixture )
{
{
mock::function< void() > f;
f.expect().once();
f();
BOOST_CHECK( f.verify() );
CHECK_CALLS( 1 );
}
{
mock::function< void( int, const std::string& ) > f;
f.expect().once();
f( 1, "s" );
BOOST_CHECK( f.verify() );
CHECK_CALLS( 1 );
}
}
BOOST_FIXTURE_TEST_CASE( verifying_a_once_expectation_before_the_call_fails, error_fixture )
{
{
mock::function< void() > f;
f.expect().once();
CHECK_ERROR( BOOST_CHECK( ! f.verify() ), verification_failed, 0, "?\n. once()" );
}
{
mock::function< int( int, const std::string& ) > f;
f.expect().once();
CHECK_ERROR( BOOST_CHECK( ! f.verify() ), verification_failed, 0, "?\n. once().with( any, any )" );
}
}
BOOST_FIXTURE_TEST_CASE( verifying_a_once_expectation_after_a_verify_and_one_call_succeeds, error_fixture )
{
mock::function< void() > f;
f.expect().once();
CHECK_ERROR( BOOST_CHECK( ! f.verify() ), verification_failed, 0, "?\n. once()" );
f();
BOOST_CHECK( f.verify() );
CHECK_CALLS( 1 );
}
// reset
BOOST_FIXTURE_TEST_CASE( triggering_a_reset_function_calls_unexpected_call_error, error_fixture )
{
{
mock::function< void() > f;
f.expect();
f.reset();
CHECK_ERROR( f(), unexpected_call, 0, "?()" );
}
{
mock::function< int( int, const std::string& ) > f;
f.expect();
f.reset();
CHECK_ERROR( f( 1, "s" ), unexpected_call, 0, "?( 1, \"s\" )" );
}
}
BOOST_FIXTURE_TEST_CASE( verifying_a_reset_function_succeeds, error_fixture )
{
{
mock::function< void() > f;
f.expect();
f.reset();
BOOST_CHECK( f.verify() );
}
{
mock::function< int( int, const std::string& ) > f;
f.expect();
f.reset();
BOOST_CHECK( f.verify() );
}
}
// constraints
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;
f.expect().with( 42 );
CHECK_ERROR( f( 43 ), unexpected_call, 0, "?( 43 )\n. unlimited().with( 42 )" );
}
{
mock::function< int( int, const std::string& ) > f;
f.expect().with( 42, "expected" );
CHECK_ERROR( f( 42, "actual" ), unexpected_call, 0, "?( 42, \"actual\" )\n. unlimited().with( 42, \"expected\" )" );
}
}
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;
f.expect().with( mock::equal( 42 ) || mock::less( 42 ) );
f( 41 );
f( 42 );
CHECK_ERROR( f( 43 ), unexpected_call, 2, "?( 43 )\n. unlimited().with( ( equal( 42 ) || less( 42 ) ) )" );
}
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;
f.expect().with( mock::equal( 42 ) && ! mock::less( 41 ) );
f( 42 );
CHECK_ERROR( f( 43 ), unexpected_call, 1, "?( 43 )\n. unlimited().with( ( equal( 42 ) && ! less( 41 ) ) )" );
}
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_FIXTURE_TEST_CASE( passing_call_values_by_reference_is_transparent, error_fixture )
{
{
mock::function< void( my_interface& ) > f;
my_implementation imp;
f.expect().with( mock::same( imp ) );
f( imp );
CHECK_CALLS( 1 );
}
{
mock::function< void( const my_interface& ) > f;
my_implementation imp;
f.expect().with( mock::same( imp ) );
f( imp );
CHECK_CALLS( 1 );
}
}
namespace
{
bool custom_constraint( int )
{
return false;
}
}
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_failing_custom_constraint_calls_unexpected_call_error, error_fixture )
{
{
mock::function< void( int ) > f;
f.expect().with( &custom_constraint );
CHECK_ERROR( f( 42 ), unexpected_call, 0, "?( 42 )\n. unlimited().with( ? )" );
}
{
mock::function< int( int, const std::string& ) > f;
f.expect().with( &custom_constraint, "actual" );
CHECK_ERROR( f( 42, "actual" ), unexpected_call, 0, "?( 42, \"actual\" )\n. unlimited().with( ?, \"actual\" )" );
}
}
//BOOST_FIXTURE_TEST_CASE( literal_zero_can_be_used_in_place_of_null_pointers_in_constraints, error_fixture )
//{
// mock::function< void( int* ) > f;
// f.expect().with( 0 );
// f.reset();
// CHECK_CALLS( 1 );
//}
// result handling
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_no_return_set_calls_missing_action, error_fixture )
{
{
mock::function< int() > f;
f.expect();
CHECK_ERROR( f(), missing_action, 0, "?()\n. unlimited()" );
}
{
mock::function< int&() > f;
f.expect();
CHECK_ERROR( f(), missing_action, 0, "?()\n. unlimited()" );
}
{
mock::function< const std::string&() > f;
f.expect();
CHECK_ERROR( f(), missing_action, 0, "?()\n. unlimited()" );
}
}
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_the_set_value, error_fixture )
{
{
mock::function< int() > f;
f.expect().returns( 42 );
BOOST_CHECK_EQUAL( 42, f() );
CHECK_CALLS( 1 );
}
{
mock::function< int() > f;
const int i = 42;
f.expect().returns( i );
BOOST_CHECK_EQUAL( i, f() );
CHECK_CALLS( 1 );
}
{
mock::function< int() > f;
int i = 42;
f.expect().returns( boost::ref( i ) );
i = 43;
BOOST_CHECK_EQUAL( 43, f() );
CHECK_CALLS( 1 );
}
{
mock::function< int&() > f;
f.expect().returns( 42 );
BOOST_CHECK_EQUAL( 42, f() );
CHECK_CALLS( 1 );
}
{
mock::function< int&() > f;
const int result = 42;
f.expect().returns( result );
BOOST_CHECK_EQUAL( result, f() );
CHECK_CALLS( 1 );
}
{
mock::function< int&() > f;
int i = 42;
f.expect().returns( i );
i = 43;
BOOST_CHECK_EQUAL( 42, f() );
CHECK_CALLS( 1 );
}
{
mock::function< int&() > f;
int i = 42;
f.expect().returns( boost::ref( i ) );
i = 43;
BOOST_CHECK_EQUAL( 43, f() );
BOOST_CHECK_EQUAL( 12, f() = 12 );
BOOST_CHECK_EQUAL( 12, i );
CHECK_CALLS( 2 );
}
{
mock::function< std::string() > f;
f.expect().returns( "result" );
BOOST_CHECK_EQUAL( "result", f() );
CHECK_CALLS( 1 );
}
{
mock::function< const std::string&() > f;
f.expect().returns( "result" );
BOOST_CHECK_EQUAL( "result", f() );
CHECK_CALLS( 1 );
}
{
mock::function< int*() > f;
f.expect().returns( 0 );
BOOST_CHECK( ! f() );
CHECK_CALLS( 1 );
}
{
mock::function< int() > f;
f.expect().returns( 0 );
BOOST_CHECK_EQUAL( 0, f() );
CHECK_CALLS( 1 );
}
{
mock::function< int&() > f;
f.expect().returns( 0 );
BOOST_CHECK_EQUAL( 0, f() );
CHECK_CALLS( 1 );
}
}
namespace
{
struct A {};
struct B : A {};
}
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_the_set_auto_ptr_value, error_fixture )
{
{
mock::function< std::auto_ptr< int >() > f;
std::auto_ptr< int > ptr( new int( 3 ) );
f.expect().returns( boost::ref( ptr ) );
BOOST_CHECK_EQUAL( 3, *ptr );
BOOST_CHECK_EQUAL( 3, *f() );
BOOST_CHECK( ! ptr.get() );
BOOST_CHECK( ! f().get() );
CHECK_CALLS( 2 );
}
{
mock::function< std::auto_ptr< int >() > f;
std::auto_ptr< int > ptr( new int( 3 ) );
f.expect().returns( ptr );
BOOST_CHECK( ! ptr.get() );
BOOST_CHECK_EQUAL( 3, *f() );
BOOST_CHECK( ! f().get() );
CHECK_CALLS( 2 );
}
{
mock::function< std::auto_ptr< int >() > f;
f.expect().returns( new int( 3 ) );
BOOST_CHECK_EQUAL( 3, *f() );
BOOST_CHECK( ! f().get() );
CHECK_CALLS( 2 );
}
{
mock::function< std::auto_ptr< int >() > f;
f.expect().returns( std::auto_ptr< int >( new int( 3 ) ) );
BOOST_CHECK_EQUAL( 3, *f() );
BOOST_CHECK( ! f().get() );
CHECK_CALLS( 2 );
}
{
mock::function< std::auto_ptr< A >() > f;
f.expect().returns( new B );
BOOST_CHECK_NO_THROW( f() );
CHECK_CALLS( 1 );
}
{
mock::function< std::auto_ptr< A >() > f;
f.expect().returns( std::auto_ptr< A >( new B ) );
BOOST_CHECK_NO_THROW( f() );
CHECK_CALLS( 1 );
}
{
mock::function< std::auto_ptr< A >() > f;
f.expect().returns( std::auto_ptr< B >( new B ) );
BOOST_CHECK_NO_THROW( f() );
CHECK_CALLS( 1 );
}
}
namespace
{
int custom_result()
{
return 42;
}
}
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_calls_the_custom_functor, error_fixture )
{
mock::function< int() > f;
f.expect().calls( &custom_result );
BOOST_CHECK_EQUAL( 42, f() );
CHECK_CALLS( 1 );
}
namespace
{
int custom_result_with_parameter( int i )
{
return i;
}
}
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_calls_the_custom_functor_with_parameters, error_fixture )
{
mock::function< int( int ) > f;
f.expect().calls( &custom_result_with_parameter );
BOOST_CHECK_EQUAL( 42, f( 42 ) );
CHECK_CALLS( 1 );
}
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_calls_the_custom_functor_without_parameters_thanks_to_boost_bind, error_fixture )
{
mock::function< int( int ) > f;
f.expect().calls( boost::bind( &custom_result ) );
BOOST_CHECK_EQUAL( 42, f( 17 ) );
CHECK_CALLS( 1 );
}
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_throws_the_set_exception, error_fixture )
{
mock::function< int() > f;
f.expect().throws( std::runtime_error( "some exception" ) );
try
{
f();
}
catch( std::runtime_error& f )
{
BOOST_CHECK_EQUAL( "some exception", f.what() );
CHECK_CALLS( 1 );
return;
}
BOOST_FAIL( "should have thrown" );
}
// multiple expectations
BOOST_FIXTURE_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_twice, error_fixture )
{
{
mock::function< void() > f;
f.expect().once();
f.expect().once();
f();
f();
CHECK_ERROR( f(), unexpected_call, 2, "?()\nv once()\nv once()" );
}
{
mock::function< void( const std::string& ) > f;
f.expect().once().with( "first" );
f.expect().once().with( "second" );
f( "first" );
f( "second" );
CHECK_ERROR( f( "third"), unexpected_call, 2, "?( \"third\" )\nv once().with( \"first\" )\nv once().with( \"second\" )" );
}
}
BOOST_FIXTURE_TEST_CASE( best_expectation_is_selected_first, error_fixture )
{
{
mock::function< void( int ) > f;
f.expect().once().with( 1 );
f.expect().once().with( 2 );
f( 2 );
f( 1 );
CHECK_ERROR( f( 3 ), unexpected_call, 2, "?( 3 )\nv once().with( 1 )\nv once().with( 2 )" );
}
{
mock::function< void( const std::string& ) > f;
f.expect().once().with( "first" );
f.expect().once().with( "second" );
f( "second" );
f( "first" );
CHECK_ERROR( f( "third"), unexpected_call, 2, "?( \"third\" )\nv once().with( \"first\" )\nv once().with( \"second\" )" );
}
}
// error report
namespace
{
template< typename T >
std::string to_string( const T& t )
{
std::stringstream s;
s << t;
return s.str();
}
}
BOOST_FIXTURE_TEST_CASE( expectation_can_be_serialized_to_be_human_readable, error_fixture )
{
{
mock::function< void( int ) > f;
f.expect().once().with( 1 );
f.expect().once().with( 2 );
BOOST_CHECK_NO_THROW( f( 2 ) );
const std::string expected = "?\n"
". once().with( 1 )\n"
"v once().with( 2 )";
BOOST_CHECK_EQUAL( expected, to_string( f ) );
CHECK_CALLS( 1 );
f.reset();
}
{
mock::function< void( int ) > f;
f.expect().never().with( 1 );
const std::string expected = "?\n"
"v never().with( 1 )";
BOOST_CHECK_EQUAL( expected, to_string( f ) );
f.reset();
}
{
mock::function< void( const std::string& ) > f;
f.expect().never().with( mock::less( "first" ) );
f.expect().exactly( 2 ).with( "second" );
BOOST_CHECK_NO_THROW( f( "second" ) );
{
const std::string expected = "?\n"
"v never().with( less( \"first\" ) )\n"
". exactly( 1/2 ).with( \"second\" )";
BOOST_CHECK_EQUAL( expected, to_string( f ) );
}
BOOST_CHECK_NO_THROW( f( "second" ) );
{
const std::string expected = "?\n"
"v never().with( less( \"first\" ) )\n"
"v exactly( 2/2 ).with( \"second\" )";
BOOST_CHECK_EQUAL( expected, to_string( f ) );
CHECK_CALLS( 2 );
}
f.reset();
}
{
mock::function< void( int ) > f;
f.expect().once();
const std::string expected = "?\n"
". once().with( any )";
BOOST_CHECK_EQUAL( expected, to_string( f ) );
f.reset();
}
{
mock::function< void( int ) > f;
f.expect().once().with( mock::any );
const std::string expected = "?\n"
". once().with( any )";
BOOST_CHECK_EQUAL( expected, to_string( f ) );
f.reset();
}
{
mock::function< void( int ) > f;
f.expect().once();
const std::string expected = "?\n"
". once().with( any )";
BOOST_CHECK_EQUAL( expected, to_string( f ) );
f.reset();
}
{
mock::function< void( int ) > f;
f.expect().once().with( &custom_constraint );
const std::string expected = "?\n"
". once().with( ? )";
BOOST_CHECK_EQUAL( expected, to_string( f ) );
f.reset();
}
}
BOOST_FIXTURE_TEST_CASE( expectation_with_remaining_untriggered_matches_upon_destruction_calls_untriggered_expectation, error_fixture )
{
std::auto_ptr< mock::function< void() > > f( new mock::function< void() > );
f->expect().once();
CHECK_ERROR( f.reset(), untriggered_expectation, 0, "?\n. once()" );
}
BOOST_FIXTURE_TEST_CASE( verifying_expectation_with_remaining_matches_disables_the_automatic_verification_upon_destruction, error_fixture )
{
mock::function< void() > f;
f.expect().once();
CHECK_ERROR( f.verify(), verification_failed, 0, "?\n. once()" );
}
BOOST_FIXTURE_TEST_CASE( triggering_unexpected_call_call_disables_the_automatic_verification_upon_destruction, error_fixture )
{
mock::function< void() > f;
CHECK_ERROR( f(), unexpected_call, 0, "?()" );
}
BOOST_FIXTURE_TEST_CASE( adding_an_expectation_reactivates_the_verification_upon_destruction, error_fixture )
{
std::auto_ptr< mock::function< void() > > f( new mock::function< void() > );
CHECK_ERROR( (*f)(), unexpected_call, 0, "?()" );
f->expect().once();
CHECK_ERROR( f.reset(), untriggered_expectation, 0, "?\n. once()" );
}
BOOST_FIXTURE_TEST_CASE( throwing_an_exception_disables_the_automatic_verification_upon_destruction, error_fixture )
{
try
{
mock::function< void() > f;
f.expect().once();
throw std::exception();
}
catch( std::exception& )
{}
}

598
test/test_integration.cpp Normal file
View file

@ -0,0 +1,598 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include "mock_error.hpp"
#include <turtle/mock.hpp>
#include <boost/test/auto_unit_test.hpp>
#include <boost/noncopyable.hpp>
#include <boost/optional.hpp>
#include <boost/ref.hpp>
#include <cmath>
namespace
{
struct my_custom_mock
{
MOCK_METHOD_EXT( my_method, 0, void(), my_method )
};
}
BOOST_AUTO_TEST_CASE( custom_mock_object_without_macros_and_without_inheriting_from_object )
{
my_custom_mock m;
MOCK_EXPECT( m.my_method ).once();
m.my_method();
}
namespace
{
struct my_custom_mock_object
{
MOCK_METHOD_EXT( my_method, 0, void(), my_method )
};
}
BOOST_AUTO_TEST_CASE( custom_mock_object_without_macros )
{
my_custom_mock_object m;
MOCK_EXPECT( m.my_method ).once();
m.my_method();
}
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();
{
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_ambiguited_interface : boost::noncopyable
{
public:
virtual ~my_ambiguited_interface() {}
virtual void my_method() = 0;
virtual void my_method( int ) = 0;
};
MOCK_BASE_CLASS( 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 m;
MOCK_EXPECT( m.tag1 );
BOOST_CHECK_NO_THROW( m.my_method() );
BOOST_CHECK_THROW( m.my_method( 12 ), std::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_BASE_CLASS( 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(), std::exception );
}
namespace
{
struct my_declared_but_undefined_type;
MOCK_CLASS( my_declared_but_undefined_mock )
{
MOCK_METHOD_EXT( m, 1, void( my_declared_but_undefined_type& ), m )
};
}
BOOST_AUTO_TEST_CASE( mock_object_method_with_declared_but_not_defined_parameter_is_valid )
{
my_declared_but_undefined_mock mock;
MOCK_EXPECT( mock.m );
}
BOOST_AUTO_TEST_CASE( mock_functor_in_function_is_supported )
{
boost::function< int( float, const std::string& ) > func;
{
MOCK_FUNCTOR( f, int( float, const std::string& ) );
MOCK_EXPECT( f ).once().with( 3, "op" ).returns( 42 );
func = f;
}
BOOST_CHECK_EQUAL( 42, func( 3, "op" ) );
}
namespace
{
struct functor_fixture
{
MOCK_FUNCTOR( f, int( float, const std::string& ) );
};
}
BOOST_FIXTURE_TEST_CASE( mock_functor_in_fixture_is_supported, functor_fixture )
{
MOCK_EXPECT( f ).once().with( 3, "op" ).returns( 42 );
BOOST_CHECK_EQUAL( 42, f( 3.f, "op" ) );
}
namespace
{
template< typename T >
struct my_template_mock
{
MOCK_METHOD_EXT( my_method, 0, void(), my_method )
MOCK_METHOD_EXT_TPL( my_method, 2, void( T, std::string ), my_method_t )
MOCK_METHOD_EXT_TPL( my_other_method, 0, void(), my_other_method )
};
}
BOOST_AUTO_TEST_CASE( mocking_a_template_class_method_is_supported )
{
my_template_mock< int > m;
MOCK_EXPECT( m.my_method_t ).with( 3, "" );
m.my_method( 3, "" );
BOOST_CHECK( MOCK_VERIFY( m.my_method_t ) );
}
namespace
{
template< typename T >
struct my_template_converter_mock
{
MOCK_CONVERSION_OPERATOR( T, to_t )
};
}
BOOST_AUTO_TEST_CASE( mocking_a_template_class_conversion_operator_is_supported )
{
my_template_converter_mock< int > m;
MOCK_EXPECT( m.to_t ).returns( 42 );
BOOST_CHECK_EQUAL( 42, m );
}
namespace
{
template< typename T >
struct my_template_base_class
{
virtual ~my_template_base_class()
{}
virtual void my_method( T ) = 0;
virtual void my_other_method() = 0;
};
template< typename T >
MOCK_BASE_CLASS( my_template_base_class_mock, my_template_base_class< T > )
{
MOCK_METHOD_EXT_TPL( my_method, 1, void( T ), my_method )
MOCK_METHOD_EXT_TPL( my_other_method, 0, void(), my_other_method )
};
}
BOOST_AUTO_TEST_CASE( mocking_a_template_base_class_method_is_supported )
{
my_template_base_class_mock< int > m;
MOCK_EXPECT( m.my_method ).once().with( 3 );
m.my_method( 3 );
BOOST_CHECK( MOCK_VERIFY( m.my_method ) );
}
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_BASE_CLASS( my_mock_observer, my_observer )
{
MOCK_METHOD( notify, 1 )
};
MOCK_BASE_CLASS( my_mock_manager, my_manager )
{
MOCK_METHOD( get_observer, 0 )
};
struct fixture
{
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
{
MOCK_CLASS( my_constructed_class )
{
MOCK_CONSTRUCTOR( my_constructed_class, 2, ( int, const std::string& ), constructor )
};
}
BOOST_AUTO_TEST_CASE( mocking_a_constructor )
{
MOCK_EXPECT( my_constructed_class::constructor ).with( 42, "some text" ).once();
my_constructed_class( 42, "some text" );
BOOST_CHECK( MOCK_VERIFY( my_constructed_class::constructor ) );
}
namespace
{
template< typename T >
MOCK_CLASS( my_constructed_template_class )
{
MOCK_CONSTRUCTOR_TPL( my_constructed_template_class, 2, ( T, const std::string& ), constructor )
};
}
BOOST_AUTO_TEST_CASE( mocking_a_template_class_constructor )
{
MOCK_EXPECT( my_constructed_template_class< int >::constructor ).with( 42, "some text" ).once();
my_constructed_template_class< int >( 42, "some text" );
BOOST_CHECK( MOCK_VERIFY( my_constructed_template_class< int >::constructor ) );
}
namespace
{
MOCK_CLASS( my_destroyed_class )
{
MOCK_DESTRUCTOR( my_destroyed_class, destructor )
};
}
BOOST_AUTO_TEST_CASE( mocking_a_destructor )
{
my_destroyed_class c;
MOCK_EXPECT( c.destructor ).once();
}
BOOST_AUTO_TEST_CASE( failed_expectation_in_mocked_destructor_does_not_throw )
{
try
{
my_destroyed_class c;
throw std::runtime_error( "should not crash" );
}
catch( std::runtime_error& )
{
}
}
BOOST_AUTO_TEST_CASE( failed_sequence_in_mocked_destructor_does_not_throw )
{
mock::sequence s;
my_custom_mock m;
{
my_destroyed_class c;
MOCK_EXPECT( c.destructor ).once().in( s );
MOCK_EXPECT( m.my_method ).once().in( s );
m.my_method();
}
}
namespace
{
MOCK_CLASS( boost_optional )
{
MOCK_METHOD_EXT( method, 0, boost::optional< my_observer& >(), method )
};
}
BOOST_AUTO_TEST_CASE( boost_optional_on_base_class_reference_as_return_type_is_supported )
{
boost_optional b;
my_mock_observer o;
MOCK_EXPECT( b.method ).once().returns( boost::ref( o ) );
b.method();
}
namespace
{
bool serialized = false;
struct custom_argument
{
friend std::ostream& operator<<( std::ostream& s, custom_argument )
{
serialized = true;
return s;
}
};
struct custom_constraint
{
template< typename Actual >
friend bool operator==( Actual, custom_constraint )
{
return true;
}
friend std::ostream& operator<<( std::ostream& s, custom_constraint )
{
serialized = true;
return s;
}
};
}
BOOST_AUTO_TEST_CASE( constraints_and_arguments_are_serialized_lazily )
{
MOCK_FUNCTOR( f, void( custom_argument ) );
MOCK_EXPECT( f ).with( custom_constraint() );
f( custom_argument() );
BOOST_CHECK( ! serialized );
}
namespace
{
template< typename Expected >
struct near_constraint
{
near_constraint( Expected expected, Expected threshold )
: expected_( expected )
, threshold_( threshold )
{}
template< typename Actual >
bool operator()( Actual actual ) const
{
return std::abs( actual - boost::unwrap_ref( expected_ ) )
< boost::unwrap_ref( threshold_ );
}
friend std::ostream& operator<<( std::ostream& s, const near_constraint& c )
{
return s << "near( " << mock::format( c.expected_ )
<< ", " << mock::format( c.threshold_ ) << " )";
}
Expected expected_;
Expected threshold_;
};
template< typename Expected >
mock::constraint< near_constraint< Expected > > near( Expected expected, Expected threshold )
{
return near_constraint< Expected >( expected, threshold );
}
}
BOOST_AUTO_TEST_CASE( using_custom_constraint )
{
MOCK_FUNCTOR( f, void( float ) );
MOCK_EXPECT( f ).with( near( 3.f, 0.01f ) );
f( 3.f );
const std::string expected = "f\n"
". unlimited().with( near( 3, 0.01 ) )";
std::stringstream s;
s << f;
BOOST_CHECK_EQUAL( expected, s.str() );
}
namespace
{
struct custom_constraint_with_non_const_operator
{
template< typename Actual >
bool operator()( Actual actual )
{
return actual == 42;
}
};
}
BOOST_AUTO_TEST_CASE( custom_constraint_function_operator_does_not_need_to_be_const )
{
MOCK_FUNCTOR( f, void( float ) );
MOCK_EXPECT( f ).with( mock::constraint< custom_constraint_with_non_const_operator >( custom_constraint_with_non_const_operator() ) );
f( 42 );
}
BOOST_AUTO_TEST_CASE( boost_reference_wrapper_is_supported_in_value_constraint )
{
MOCK_FUNCTOR( f, void( const std::string& ) );
std::string s;
MOCK_EXPECT( f ).once().with( boost::cref( s ) );
s = "string";
f( "string" );
}
namespace
{
template< typename T >
void nothing( T )
{}
struct member_pointer_mock_class
{
MOCK_CONST_METHOD_EXT( my_method, 0, void(), my_method )
};
}
BOOST_AUTO_TEST_CASE( member_pointer_on_mock_method_is_valid )
{
nothing( &member_pointer_mock_class::my_method );
}
namespace
{
MOCK_FUNCTION( free_function, 1, void( int ), free_function )
}
BOOST_AUTO_TEST_CASE( a_free_function_can_be_mocked )
{
MOCK_EXPECT( free_function ).once();
BOOST_CHECK( ! MOCK_VERIFY( free_function ) );
free_function( 42 );
BOOST_CHECK( MOCK_VERIFY( free_function ) );
MOCK_RESET( free_function );
}
namespace
{
struct some_class : mock::object
{
MOCK_STATIC_METHOD( some_static_method, 1, void( int ), some_static_method )
};
}
BOOST_AUTO_TEST_CASE( a_static_method_can_be_mocked )
{
MOCK_EXPECT( some_class::some_static_method ).once();
BOOST_CHECK( ! MOCK_VERIFY( some_class::some_static_method ) );
some_class::some_static_method( 42 );
BOOST_CHECK( MOCK_VERIFY( some_class::some_static_method ) );
MOCK_RESET( some_class::some_static_method );
}
BOOST_AUTO_TEST_CASE( a_static_method_is_not_reset_when_resetting_an_instance_of_the_class )
{
MOCK_EXPECT( some_class::some_static_method ).once();
some_class c;
mock::reset( c );
BOOST_CHECK( ! MOCK_VERIFY( some_class::some_static_method ) );
MOCK_RESET( some_class::some_static_method );
}
namespace
{
template< typename T >
struct some_template_class
{
MOCK_STATIC_METHOD_TPL( some_static_method, 1, void( T ), some_static_method )
};
}
BOOST_AUTO_TEST_CASE( a_static_method_in_a_template_class_can_be_mocked )
{
MOCK_EXPECT( some_template_class< int >::some_static_method ).once();
BOOST_CHECK( ! MOCK_VERIFY( some_template_class< int >::some_static_method ) );
some_template_class< int >::some_static_method( 42 );
BOOST_CHECK( mock::verify() );
BOOST_CHECK( MOCK_VERIFY( some_template_class< int >::some_static_method ) );
MOCK_RESET( some_template_class< int >::some_static_method );
}
namespace
{
MOCK_CLASS( mock_class )
{
MOCK_METHOD_EXT( m, 0, void(), m );
};
}
BOOST_AUTO_TEST_CASE( resetting_referenced_mock_class_does_not_crash )
{
MOCK_FUNCTOR( f, mock_class() );
{
mock_class c;
MOCK_EXPECT( f ).returns( c );
MOCK_EXPECT( c.m );
}
mock::reset();
}
namespace
{
MOCK_CLASS( mock_class2 )
{
MOCK_METHOD_EXT( m, 0, mock_class2(), m );
};
}
BOOST_AUTO_TEST_CASE( resetting_self_referenced_mock_class_does_not_crash )
{
{
mock_class2 c;
MOCK_EXPECT( c.m ).returns( c );
}
mock::reset();
}

77
test/test_invocation.cpp Normal file
View file

@ -0,0 +1,77 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/invocation.hpp>
#include <boost/test/auto_unit_test.hpp>
BOOST_AUTO_TEST_CASE( unlimited )
{
mock::detail::unlimited invocation;
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( ! invocation.exhausted() );
BOOST_CHECK( invocation.invoke() );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( ! invocation.exhausted() );
BOOST_CHECK( invocation.invoke() );
}
BOOST_AUTO_TEST_CASE( once )
{
mock::detail::once invocation;
BOOST_CHECK( ! invocation.verify() );
BOOST_CHECK( ! invocation.exhausted() );
BOOST_CHECK( invocation.invoke() );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( invocation.exhausted() );
BOOST_CHECK( ! invocation.invoke() );
}
BOOST_AUTO_TEST_CASE( never )
{
mock::detail::never invocation;
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( invocation.exhausted() );
BOOST_CHECK( ! invocation.invoke() );
}
BOOST_AUTO_TEST_CASE( at_most )
{
mock::detail::at_most invocation( 1 );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( ! invocation.exhausted() );
BOOST_CHECK( invocation.invoke() );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( invocation.exhausted() );
BOOST_CHECK( ! invocation.invoke() );
}
BOOST_AUTO_TEST_CASE( at_least )
{
mock::detail::at_least invocation( 1 );
BOOST_CHECK( ! invocation.verify() );
BOOST_CHECK( ! invocation.exhausted() );
BOOST_CHECK( invocation.invoke() );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( ! invocation.exhausted() );
BOOST_CHECK( invocation.invoke() );
}
BOOST_AUTO_TEST_CASE( between )
{
mock::detail::between invocation( 1, 2 );
BOOST_CHECK( ! invocation.verify() );
BOOST_CHECK( ! invocation.exhausted() );
BOOST_CHECK( invocation.invoke() );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( ! invocation.exhausted() );
BOOST_CHECK( invocation.invoke() );
BOOST_CHECK( invocation.verify() );
BOOST_CHECK( invocation.exhausted() );
BOOST_CHECK( ! invocation.invoke() );
}

128
test/test_is_functor.cpp Normal file
View file

@ -0,0 +1,128 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/is_functor.hpp>
#include <boost/test/auto_unit_test.hpp>
#ifdef _MSC_VER
#pragma warning( push, 0 )
#endif
#include <boost/lambda/lambda.hpp>
#include <boost/spirit/home/phoenix.hpp>
#ifdef _MSC_VER
#pragma warning( pop )
#endif
#include <boost/function.hpp>
#include <boost/bind.hpp>
namespace
{
struct declared_but_not_defined;
BOOST_MPL_ASSERT_NOT(( mock::detail::is_functor< declared_but_not_defined > ));
template< typename T >
void check( T )
{
BOOST_MPL_ASSERT(( mock::detail::is_functor< T > ));
}
void f0 () {}
bool f1( int ) { return false; }
bool f2( std::string, int ) { return false; }
}
BOOST_AUTO_TEST_CASE( function_is_functor )
{
check( f0 );
check( f1 );
check( f2 );
}
BOOST_AUTO_TEST_CASE( function_pointer_is_functor )
{
check( &f0 );
check( &f1 );
check( &f2 );
}
BOOST_AUTO_TEST_CASE( std_ptr_fun_is_functor )
{
check( std::ptr_fun( &f1 ) );
check( std::ptr_fun( &f2 ) );
}
BOOST_AUTO_TEST_CASE( std_bind_first_is_functor )
{
check( std::bind1st( std::ptr_fun( &f2 ), "" ) );
}
namespace
{
struct unary_functor0 : public std::unary_function< void, void >
{
};
struct unary_functor1 : public std::unary_function< int, void >
{
};
}
BOOST_AUTO_TEST_CASE( std_unary_functor_is_functor )
{
check( unary_functor0() );
check( unary_functor1() );
}
BOOST_AUTO_TEST_CASE( boost_bind_is_functor )
{
check( boost::bind( &f0 ) );
check( boost::bind( &f1, _1 ) );
check( boost::bind( &f2, "", _1 ) );
}
BOOST_AUTO_TEST_CASE( boost_lambda_is_functor )
{
check( boost::lambda::_1 < 42 );
}
BOOST_AUTO_TEST_CASE( boost_phoenix_is_functor )
{
check( boost::phoenix::arg_names::arg1 < 42 );
check( boost::phoenix::arg_names::_1 < 42 );
}
BOOST_AUTO_TEST_CASE( boost_function_is_functor )
{
check( boost::function< void() >() );
}
namespace
{
struct result_type_functor
{
typedef void result_type;
};
}
BOOST_AUTO_TEST_CASE( class_with_result_type_is_functor )
{
check( result_type_functor() );
}
namespace
{
struct sig_functor
{
template< typename Args >
struct sig
{
typedef void type;
};
};
}
BOOST_AUTO_TEST_CASE( class_with_sig_is_functor )
{
check( sig_functor() );
}

602
test/test_log.cpp Normal file
View file

@ -0,0 +1,602 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/log.hpp>
#include <boost/test/auto_unit_test.hpp>
#include <boost/assign.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/lexical_cast.hpp>
#ifdef _MSC_VER
#pragma warning( push, 0 )
#endif
#include <boost/spirit/home/phoenix.hpp>
#include <boost/spirit/home/phoenix/bind.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#ifdef _MSC_VER
#pragma warning( pop )
#endif
#include <boost/bind.hpp>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <set>
namespace
{
template< typename T >
std::string to_string( T t )
{
std::stringstream s;
s << mock::format( t );
return s.str();
}
}
BOOST_AUTO_TEST_CASE( pointer_yields_its_value_when_serialized )
{
{
int i = 0;
BOOST_CHECK_NE( "?", to_string( &i ) );
BOOST_CHECK_EQUAL( boost::lexical_cast< std::string >( &i ), to_string( &i ) );
}
{
const int i = 0;
BOOST_CHECK_NE( "?", to_string( &i ) );
BOOST_CHECK_EQUAL( boost::lexical_cast< std::string >( &i ), to_string( &i ) );
}
}
BOOST_AUTO_TEST_CASE( base_type_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "42", to_string( 42 ) );
}
BOOST_AUTO_TEST_CASE( booleans_are_serialized_as_bool_alpha )
{
BOOST_CHECK_EQUAL( "true", to_string( true ) );
BOOST_CHECK_EQUAL( "false", to_string( false ) );
}
BOOST_AUTO_TEST_CASE( strings_are_serialized_with_double_quotes )
{
BOOST_CHECK_EQUAL( "\"string\"", to_string( "string" ) );
BOOST_CHECK_EQUAL( "\"string\"", to_string( std::string( "string" ) ) );
}
namespace
{
struct non_serializable
{};
}
BOOST_AUTO_TEST_CASE( non_serializable_type_yields_an_question_mark_when_serialized )
{
BOOST_CHECK_EQUAL( "?", to_string( non_serializable() ) );
}
namespace
{
struct serializable
{};
std::ostream& operator<<( std::ostream& s, const serializable& )
{
return s << "serializable";
}
}
BOOST_AUTO_TEST_CASE( serializable_type_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "serializable", to_string( serializable() ) );
}
namespace
{
struct streamable
{};
std::ostream& operator<<( std::ostream& s, const streamable& )
{
BOOST_FAIL( "should not have been called" );
return s;
}
mock::stream& operator<<( mock::stream& s, const streamable& )
{
return s << "streamable";
}
}
BOOST_AUTO_TEST_CASE( streamable_type_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "streamable", to_string( streamable() ) );
}
namespace
{
struct mock_streamable
{};
std::ostream& operator<<( std::ostream& s, const mock_streamable& )
{
BOOST_FAIL( "should not have been called" );
return s;
}
}
namespace mock
{
stream& operator<<( stream& s, const mock_streamable& )
{
return s << "mock_streamable";
}
}
BOOST_AUTO_TEST_CASE( mock_streamable_type_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "mock_streamable", to_string( mock_streamable() ) );
}
namespace
{
struct derived_from_serializable : serializable
{};
}
BOOST_AUTO_TEST_CASE( type_derived_from_serializable_yields_an_question_mark_when_serialized )
{
#ifdef MOCK_USE_CONVERSIONS
BOOST_CHECK_EQUAL( "serializable", to_string( derived_from_serializable() ) );
#else
BOOST_CHECK_EQUAL( "?", to_string( derived_from_serializable() ) );
#endif
}
namespace
{
struct derived_from_streamable : streamable
{};
}
BOOST_AUTO_TEST_CASE( type_derived_from_streamable_yields_an_question_mark_when_serialized )
{
#ifdef MOCK_USE_CONVERSIONS
BOOST_CHECK_EQUAL( "streamable", to_string( derived_from_streamable() ) );
#else
BOOST_CHECK_EQUAL( "?", to_string( derived_from_streamable() ) );
#endif
}
#ifndef MOCK_USE_CONVERSIONS // all this does not compile with conversions activated, which is precisely the purpose of having this compilation flag
namespace
{
struct convertible_to_base
{
operator int() const;
};
}
BOOST_AUTO_TEST_CASE( type_convertible_to_base_yields_an_question_mark_when_serialized )
{
BOOST_CHECK_EQUAL( "?", to_string( convertible_to_base() ) );
}
namespace
{
struct convertible_to_serializable
{
operator serializable() const;
};
}
BOOST_AUTO_TEST_CASE( type_convertible_to_serializable_yields_an_question_mark_when_serialized )
{
BOOST_CHECK_EQUAL( "?", to_string( convertible_to_serializable() ) );
}
namespace
{
struct convertible_to_streamable
{
operator streamable() const;
};
}
BOOST_AUTO_TEST_CASE( type_convertible_to_streamable_yields_an_question_mark_when_serialized )
{
BOOST_CHECK_EQUAL( "?", to_string( convertible_to_streamable() ) );
}
namespace
{
struct ambiguous_convertible
{
operator float() const;
operator int() const;
operator serializable() const;
operator streamable() const;
template< typename T > operator T() const;
};
}
BOOST_AUTO_TEST_CASE( type_ambiguous_convertible_yields_an_question_mark_when_serialized )
{
BOOST_CHECK_EQUAL( "?", to_string( ambiguous_convertible() ) );
}
namespace
{
struct ambiguous_convertible_serializable
{
operator float() const;
operator int() const;
operator serializable() const;
operator streamable() const;
template< typename T > operator T() const;
};
std::ostream& operator<<( std::ostream& s, const ambiguous_convertible_serializable& )
{
return s << "ambiguous_convertible_serializable";
}
}
BOOST_AUTO_TEST_CASE( type_convertible_serializable_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "ambiguous_convertible_serializable", to_string( ambiguous_convertible_serializable() ) );
}
#endif // MOCK_USE_CONVERSIONS
namespace
{
struct ambiguous_convertible_streamable
{
operator float() const;
operator int() const;
operator serializable() const;
operator streamable() const;
template< typename T > operator T() const;
};
std::ostream& operator<<( std::ostream& s, const ambiguous_convertible_streamable& )
{
BOOST_FAIL( "should not have been called" );
return s;
}
mock::stream& operator<<( mock::stream& s, const ambiguous_convertible_streamable& )
{
return s << "ambiguous_convertible_streamable";
}
}
BOOST_AUTO_TEST_CASE( type_ambiguous_convertible_streamable_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "ambiguous_convertible_streamable", to_string( ambiguous_convertible_streamable() ) );
}
namespace
{
struct ambiguous_convertible_mock_streamable
{
operator float() const;
operator int() const;
operator serializable() const;
operator streamable() const;
template< typename T > operator T() const;
};
std::ostream& operator<<( std::ostream& s, const ambiguous_convertible_mock_streamable& )
{
BOOST_FAIL( "should not have been called" );
return s;
}
}
namespace mock
{
stream& operator<<( stream& s, const ambiguous_convertible_mock_streamable& )
{
return s << "ambiguous_convertible_mock_streamable";
}
}
BOOST_AUTO_TEST_CASE( type_ambiguous_convertible_mock_streamable_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "ambiguous_convertible_mock_streamable", to_string( ambiguous_convertible_mock_streamable() ) );
}
namespace
{
template< typename T >
struct template_serializable
{};
template< typename T >
std::ostream& operator<<( std::ostream& s, const template_serializable< T >& )
{
return s << "template_serializable";
}
}
BOOST_AUTO_TEST_CASE( template_type_serializable_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "template_serializable", to_string( template_serializable< int >() ) );
}
namespace
{
template< typename T >
struct template_streamable
{};
template< typename T >
std::ostream& operator<<( std::ostream& s, const template_streamable< T >& )
{
BOOST_FAIL( "should not have been called" );
return s;
}
template< typename T >
mock::stream& operator<<( mock::stream& s, const template_streamable< T >& )
{
return s << "template_streamable";
}
}
BOOST_AUTO_TEST_CASE( template_template_streamable_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "template_streamable", to_string( template_streamable< int >() ) );
}
namespace
{
template< typename T >
struct template_mock_streamable
{};
template< typename T >
std::ostream& operator<<( std::ostream& s, const template_mock_streamable< T >& )
{
BOOST_FAIL( "should not have been called" );
return s;
}
}
namespace mock
{
template< typename T >
stream& operator<<( stream& s, const template_mock_streamable< T >& )
{
return s << "template_mock_streamable";
}
}
BOOST_AUTO_TEST_CASE( template_mock_streamable_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "template_mock_streamable", to_string( template_mock_streamable< int >() ) );
}
BOOST_AUTO_TEST_CASE( std_pairs_are_serialized )
{
BOOST_CHECK_EQUAL( "(3,42)", to_string( std::make_pair( 3, 42.f ) ) );
}
BOOST_AUTO_TEST_CASE( std_auto_ptr_are_serialized )
{
BOOST_CHECK_NE( "?", to_string( std::auto_ptr< int >() ) );
BOOST_CHECK_NE( "?", to_string( std::auto_ptr< int >( new int( 42 ) ) ) );
}
BOOST_AUTO_TEST_CASE( boost_shared_ptr_are_serialized )
{
BOOST_CHECK_NE( "?", to_string( boost::shared_ptr< int >() ) );
BOOST_CHECK_NE( "?", to_string( boost::shared_ptr< int >( new int( 42 ) ) ) );
}
BOOST_AUTO_TEST_CASE( boost_weak_ptr_are_serialized )
{
BOOST_CHECK_NE( "?", to_string( boost::weak_ptr< int >( boost::shared_ptr< int >() ) ) );
BOOST_CHECK_NE( "?", to_string( boost::weak_ptr< int >( boost::shared_ptr< int >( new int( 42 ) ) ) ) );
}
BOOST_AUTO_TEST_CASE( std_deques_are_serialized )
{
std::deque< int > d;
d.push_back( 12 );
d.push_back( 42 );
BOOST_CHECK_EQUAL( "(12,42)", to_string( d ) );
}
BOOST_AUTO_TEST_CASE( std_lists_are_serialized )
{
std::list< int > l;
l.push_back( 12 );
l.push_back( 42 );
BOOST_CHECK_EQUAL( "(12,42)", to_string( l ) );
}
BOOST_AUTO_TEST_CASE( std_vectors_are_serialized )
{
std::vector< int > v;
v.push_back( 12 );
v.push_back( 42 );
BOOST_CHECK_EQUAL( "(12,42)", to_string( v ) );
}
BOOST_AUTO_TEST_CASE( std_maps_are_serialized )
{
std::map< int, std::string > m;
m[ 12 ] = "12";
m[ 42 ] = "42";
BOOST_CHECK_EQUAL( "((12,\"12\"),(42,\"42\"))", to_string( m ) );
}
BOOST_AUTO_TEST_CASE( std_multimaps_are_serialized )
{
std::multimap< int, std::string > m;
m.insert( std::make_pair( 12, "12" ));
m.insert( std::make_pair( 42, "42" ));
BOOST_CHECK_EQUAL( "((12,\"12\"),(42,\"42\"))", to_string( m ) );
}
BOOST_AUTO_TEST_CASE( std_sets_are_serialized )
{
std::set< int > s;
s.insert( 12 );
s.insert( 42 );
BOOST_CHECK_EQUAL( "(12,42)", to_string( s ) );
}
BOOST_AUTO_TEST_CASE( std_multisets_are_serialized )
{
std::multiset< int > s;
s.insert( 12 );
s.insert( 42 );
BOOST_CHECK_EQUAL( "(12,42)", to_string( s ) );
}
BOOST_AUTO_TEST_CASE( boost_assign_list_of_are_serialized )
{
BOOST_CHECK_EQUAL( "(12,42)", to_string( boost::assign::list_of( 12 )( 42 ) ) );
}
BOOST_AUTO_TEST_CASE( boost_assign_map_list_of_are_serialized )
{
BOOST_CHECK_EQUAL( "((12,\"12\"),(42,\"42\"))", to_string( boost::assign::map_list_of( 12, "12" )( 42, "42" ) ) );
}
BOOST_AUTO_TEST_CASE( boost_reference_wrappers_are_serialized )
{
BOOST_CHECK_EQUAL( "3", to_string( boost::cref( 3 ) ) );
BOOST_CHECK_EQUAL( "\"string\"", to_string( boost::cref( "string" ) ) );
}
namespace
{
void callable_builtin()
{}
}
BOOST_AUTO_TEST_CASE( callable_builtin_yields_an_question_mark_when_serialized )
{
BOOST_CHECK_EQUAL( "?", to_string( callable_builtin ) );
BOOST_CHECK_EQUAL( "?", to_string( &callable_builtin ) );
}
namespace
{
struct serialized_using_format
{};
std::ostream& operator<<( std::ostream& s, const serialized_using_format& )
{
return s << mock::format( "string" );
}
}
BOOST_AUTO_TEST_CASE( type_can_use_format_when_serialized )
{
BOOST_CHECK_EQUAL( "\"string\"", to_string( serialized_using_format() ) );
}
namespace
{
struct streamed_using_format
{};
mock::stream& operator<<( mock::stream& s, const streamed_using_format& )
{
return s << mock::format( "string" );
}
}
BOOST_AUTO_TEST_CASE( type_can_use_format_when_streamed )
{
BOOST_CHECK_EQUAL( "\"string\"", to_string( streamed_using_format() ) );
}
namespace
{
struct std_string_streamed
{};
mock::stream& operator<<( mock::stream& s, const std_string_streamed& )
{
return s << std::string( "string" );
}
}
BOOST_AUTO_TEST_CASE( std_string_streamed_is_not_a_container )
{
BOOST_CHECK_EQUAL( "string", to_string( std_string_streamed() ) );
}
namespace mock
{
namespace detail
{
template< typename T >
struct template_serializable
{};
template< typename T >
std::ostream& operator<<( std::ostream& s, const template_serializable< T >& )
{
return s << "mock::detail::template_serializable";
}
}
}
BOOST_AUTO_TEST_CASE( mock_detail_template_type_serializable_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "mock::detail::template_serializable", to_string( mock::detail::template_serializable< int >() ) );
}
namespace mock
{
namespace detail
{
template< typename T >
struct template_streamable
{};
template< typename T >
std::ostream& operator<<( std::ostream& s, const template_streamable< T >& )
{
BOOST_FAIL( "should not have been called" );
return s;
}
template< typename T >
mock::stream& operator<<( mock::stream& s, const template_streamable< T >& )
{
return s << "mock::detail::template_streamable";
}
}
}
BOOST_AUTO_TEST_CASE( mock_detail_template_template_streamable_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "mock::detail::template_streamable", to_string( mock::detail::template_streamable< int >() ) );
}
BOOST_AUTO_TEST_CASE( unsigned_char_is_serialized_as_int )
{
BOOST_CHECK_EQUAL( boost::lexical_cast< std::string >( static_cast< int >( 'a' ) ), to_string< unsigned char >( 'a' ) );
}
namespace
{
bool some_function()
{
return false;
}
}
BOOST_AUTO_TEST_CASE( boost_phoenix_functor_yields_question_mark_when_serialized )
{
BOOST_CHECK_EQUAL( "?", to_string( boost::phoenix::bind( &some_function ) ) );
BOOST_CHECK_EQUAL( "?", to_string( boost::phoenix::arg_names::_1 < 42 ) );
}
BOOST_AUTO_TEST_CASE( boost_bind_functor_yields_question_mark_when_serialized )
{
BOOST_CHECK_EQUAL( "?", to_string( boost::bind( &some_function ) ) );
}
BOOST_AUTO_TEST_CASE( boost_lambda_functor_yields_question_mark_when_serialized )
{
BOOST_CHECK_EQUAL( "?", to_string( boost::lambda::bind( &some_function ) ) );
BOOST_CHECK_EQUAL( "?", to_string( boost::lambda::_1 < 42 ) );
}

38
test/test_max_args.cpp Normal file
View file

@ -0,0 +1,38 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include "mock_error.hpp"
#include <turtle/mock.hpp>
#include <boost/test/auto_unit_test.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#define IDENTITY(z, n, d) d
namespace
{
struct my_custom_mock
{
MOCK_METHOD_EXT( method, MOCK_MAX_ARGS, void( BOOST_PP_ENUM(MOCK_MAX_ARGS, IDENTITY, int) ), method )
MOCK_METHOD_EXT( method2, MOCK_MAX_ARGS, int( BOOST_PP_ENUM(MOCK_MAX_ARGS, IDENTITY, int) ), method2 )
};
}
BOOST_AUTO_TEST_CASE( call_mock_method_with_max_number_of_args )
{
my_custom_mock m;
MOCK_EXPECT( m.method ).once().with( BOOST_PP_ENUM(MOCK_MAX_ARGS, IDENTITY, 0) );
m.method( BOOST_PP_ENUM(MOCK_MAX_ARGS, IDENTITY, 0) );
}
BOOST_AUTO_TEST_CASE( call_mock_method_with_max_number_of_args_and_returning_something )
{
my_custom_mock m;
MOCK_EXPECT( m.method2 ).once().with( BOOST_PP_ENUM(MOCK_MAX_ARGS, IDENTITY, 0) ).returns( 42 );
BOOST_CHECK_EQUAL( 42, m.method2( BOOST_PP_ENUM(MOCK_MAX_ARGS, IDENTITY, 0) ) );
}

313
test/test_mock.cpp Normal file
View file

@ -0,0 +1,313 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/mock.hpp>
#include <boost/test/auto_unit_test.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
namespace
{
struct s
{
void m1();
float m2( int );
};
BOOST_MPL_ASSERT((
boost::is_same<
void(),
mock::detail::signature< BOOST_TYPEOF( &s::m1 ) >::type
> ));
BOOST_MPL_ASSERT((
boost::is_same<
float( int ),
mock::detail::signature< BOOST_TYPEOF( &s::m2 ) >::type
> ));
}
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 m;
MOCK_EXPECT( m.my_method ).once().with( "some parameter" );
my_function( m );
}
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 m;
MOCK_EXPECT( m.addition ).once().returns( boost::ref( m ) );
m += 1;
}
namespace
{
MOCK_CLASS( mock_class_with_conversion_operator )
{
MOCK_CONVERSION_OPERATOR( int, conversion )
};
}
BOOST_AUTO_TEST_CASE( mock_conversion_operator )
{
mock_class_with_conversion_operator m;
MOCK_EXPECT( m.conversion ).once().returns( 42 );
BOOST_CHECK_EQUAL( 42, static_cast< int >( m ) );
}
namespace
{
template< typename T >
MOCK_CLASS( mock_template_class_with_conversion_operator )
{
MOCK_CONVERSION_OPERATOR( T, conversion )
};
}
BOOST_AUTO_TEST_CASE( mock_template_conversion_operator )
{
mock_template_class_with_conversion_operator< int > m;
MOCK_EXPECT( m.conversion ).once().returns( 42 );
BOOST_CHECK_EQUAL( 42, static_cast< int >( m ) );
}
namespace
{
MOCK_CLASS( mock_class_with_const_conversion_operator )
{
MOCK_CONST_CONVERSION_OPERATOR( int, conversion )
};
}
BOOST_AUTO_TEST_CASE( mock_const_conversion_operator )
{
mock_class_with_const_conversion_operator m;
MOCK_EXPECT( m.conversion ).once().returns( 42 );
int i = m;
BOOST_CHECK_EQUAL( 42, i );
}
namespace
{
MOCK_CLASS( mock_class_with_non_const_conversion_operator )
{
MOCK_CONST_CONVERSION_OPERATOR( int, conversion )
};
}
BOOST_AUTO_TEST_CASE( mock_non_const_conversion_operator )
{
mock_class_with_non_const_conversion_operator m;
MOCK_EXPECT( m.conversion ).once().returns( 42 );
int i = m;
BOOST_CHECK_EQUAL( 42, i );
}
namespace
{
MOCK_CLASS( my_mock )
{
MOCK_CONST_METHOD_EXT( my_method, 1, void( int ), my_method )
MOCK_CONST_METHOD_EXT( my_method_2, 1, void( int ), my_method_2 )
};
}
BOOST_AUTO_TEST_CASE( MOCK_CONST_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 );
}
namespace
{
template< typename T >
std::string to_string( const T& t )
{
std::stringstream s;
s << t;
return s.str();
}
}
BOOST_AUTO_TEST_CASE( mock_object_is_named )
{
my_mock m;
BOOST_CHECK_EQUAL( "?.my_mock::my_method", to_string( MOCK_ANONYMOUS_HELPER( m.my_method ) ) );
BOOST_CHECK_EQUAL( "?.my_mock::my_method_2", to_string( MOCK_ANONYMOUS_HELPER( m.my_method_2 ) ) );
BOOST_CHECK_EQUAL( "m.my_mock::my_method", to_string( MOCK_HELPER( m.my_method ) ) );
BOOST_CHECK_EQUAL( "m.my_mock::my_method_2", to_string( MOCK_ANONYMOUS_HELPER( m.my_method_2 ) ) );
BOOST_CHECK_EQUAL( "m.my_mock::my_method", to_string( MOCK_ANONYMOUS_HELPER( m.my_method ) ) );
BOOST_CHECK_EQUAL( "m.my_mock::my_method", to_string( MOCK_HELPER( m.my_method ) ) );
}
BOOST_AUTO_TEST_CASE( mock_object_auto_pointer_is_named )
{
std::auto_ptr< my_mock > m( new my_mock );
BOOST_CHECK_EQUAL( "?.my_mock::my_method", to_string( MOCK_ANONYMOUS_HELPER( m->my_method ) ) );
BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_HELPER( m->my_method ) ) );
BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_ANONYMOUS_HELPER( m->my_method ) ) );
BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_HELPER( m->my_method ) ) );
}
BOOST_AUTO_TEST_CASE( mock_object_const_auto_pointer_is_named )
{
const std::auto_ptr< my_mock > m( new my_mock );
BOOST_CHECK_EQUAL( "?.my_mock::my_method", to_string( MOCK_ANONYMOUS_HELPER( m->my_method ) ) );
BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_HELPER( m->my_method ) ) );
BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_ANONYMOUS_HELPER( m->my_method ) ) );
BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_HELPER( m->my_method ) ) );
}
BOOST_AUTO_TEST_CASE( mock_object_shared_pointer_is_named )
{
boost::shared_ptr< my_mock > m( new my_mock );
BOOST_CHECK_EQUAL( "?.my_mock::my_method", to_string( MOCK_ANONYMOUS_HELPER( m->my_method ) ) );
BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_HELPER( m->my_method ) ) );
BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_ANONYMOUS_HELPER( m->my_method ) ) );
BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_HELPER( m->my_method ) ) );
}
BOOST_AUTO_TEST_CASE( mock_object_const_shared_pointer_is_named )
{
const boost::shared_ptr< my_mock > m( new my_mock );
BOOST_CHECK_EQUAL( "?.my_mock::my_method", to_string( MOCK_ANONYMOUS_HELPER( m->my_method ) ) );
BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_HELPER( m->my_method ) ) );
BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_ANONYMOUS_HELPER( m->my_method ) ) );
BOOST_CHECK_EQUAL( "m->my_mock::my_method", to_string( MOCK_HELPER( m->my_method ) ) );
}
namespace
{
struct my_custom_mock
{
MOCK_METHOD_EXT( my_method, 0, void(), my_method )
MOCK_METHOD_EXT( my_method_2, 0, void(), my_method_2 )
};
}
BOOST_AUTO_TEST_CASE( custom_mock_object_without_macros_and_without_inheriting_from_object_is_named )
{
my_custom_mock m;
BOOST_CHECK_EQUAL( "?.my_custom_mock::my_method", to_string( MOCK_ANONYMOUS_HELPER( m.my_method ) ) );
BOOST_CHECK_EQUAL( "?.my_custom_mock::my_method_2", to_string( MOCK_ANONYMOUS_HELPER( m.my_method_2 ) ) );
BOOST_CHECK_EQUAL( "m.my_custom_mock::my_method", to_string( MOCK_HELPER( m.my_method ) ) );
BOOST_CHECK_EQUAL( "m.my_custom_mock::my_method_2", to_string( MOCK_ANONYMOUS_HELPER( m.my_method_2 ) ) );
BOOST_CHECK_EQUAL( "m.my_custom_mock::my_method", to_string( MOCK_ANONYMOUS_HELPER( m.my_method ) ) );
BOOST_CHECK_EQUAL( "m.my_custom_mock::my_method", to_string( MOCK_HELPER( m.my_method ) ) );
}
namespace
{
struct my_custom_mock_object : mock::object
{
MOCK_METHOD_EXT( my_method, 0, void(), my_method )
MOCK_METHOD_EXT( my_method_2, 0, void(), my_method_2 )
};
}
BOOST_AUTO_TEST_CASE( custom_mock_object_without_macros_is_named )
{
my_custom_mock_object m;
BOOST_CHECK_EQUAL( "?.my_custom_mock_object::my_method", to_string( MOCK_ANONYMOUS_HELPER( m.my_method ) ) );
BOOST_CHECK_EQUAL( "?.my_custom_mock_object::my_method_2", to_string( MOCK_ANONYMOUS_HELPER( m.my_method_2 ) ) );
BOOST_CHECK_EQUAL( "m.my_custom_mock_object::my_method", to_string( MOCK_HELPER( m.my_method ) ) );
BOOST_CHECK_EQUAL( "m.my_custom_mock_object::my_method_2", to_string( MOCK_ANONYMOUS_HELPER( m.my_method_2 ) ) );
BOOST_CHECK_EQUAL( "m.my_custom_mock_object::my_method", to_string( MOCK_ANONYMOUS_HELPER( m.my_method ) ) );
BOOST_CHECK_EQUAL( "m.my_custom_mock_object::my_method", to_string( MOCK_HELPER( m.my_method ) ) );
}
BOOST_AUTO_TEST_CASE( mock_functor )
{
MOCK_FUNCTOR( f1, void() );
MOCK_FUNCTOR( f2, int( const std::string& ) );
}
BOOST_AUTO_TEST_CASE( mock_functor_reset )
{
MOCK_FUNCTOR( f, void() );
MOCK_RESET( f );
mock::reset( f );
}
BOOST_AUTO_TEST_CASE( mock_functor_verify )
{
MOCK_FUNCTOR( f, void() );
MOCK_VERIFY( f );
mock::verify( f );
}
BOOST_AUTO_TEST_CASE( mock_functor_is_named )
{
MOCK_FUNCTOR( f, void() );
BOOST_CHECK_EQUAL( "f", to_string( MOCK_HELPER( f ) ) );
}
namespace
{
MOCK_FUNCTION( mock_function, 1, float( int ), mock_function )
}
BOOST_AUTO_TEST_CASE( mock_function_is_named )
{
BOOST_CHECK_EQUAL( "mock_function", to_string( MOCK_HELPER( mock_function ) ) );
}
namespace
{
MOCK_CLASS( static_function_class )
{
MOCK_STATIC_METHOD( f, 1, float( int ), f )
};
}
BOOST_AUTO_TEST_CASE( mock_static_function_is_named )
{
BOOST_CHECK_EQUAL( "static_function_class::f", to_string( MOCK_HELPER( static_function_class::f ) ) );
}

91
test/test_object.cpp Normal file
View file

@ -0,0 +1,91 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include "mock_error.hpp"
#include <turtle/object.hpp>
#include <turtle/function.hpp>
#include <boost/test/auto_unit_test.hpp>
namespace
{
struct object : mock::object
{};
object static_o;
}
BOOST_AUTO_TEST_CASE( verifying_an_empty_object_succeeds )
{
object o;
BOOST_CHECK( mock::verify( o ) );
}
namespace
{
struct fixture
{
fixture()
{
mock::detail::configure( o, e, "instance", mock::detail::type_name( typeid( "type" ) ), "name" );
}
object o;
mock::function< void() > e;
};
}
BOOST_FIXTURE_TEST_CASE( verifying_an_object_containing_a_failing_expectation_fails, fixture )
{
e.expect().once();
BOOST_CHECK( ! mock::verify( o ) );
mock::reset( o );
BOOST_CHECK( mock::verify( o ) );
}
BOOST_FIXTURE_TEST_CASE( verifying_all_objects_with_one_of_them_containing_a_failing_expectation_fails, fixture )
{
e.expect().once();
BOOST_CHECK( ! mock::verify() );
mock::reset();
BOOST_CHECK( mock::verify() );
}
BOOST_FIXTURE_TEST_CASE( resetting_an_object_containing_a_failing_expectation_and_verifying_it_succeeds, fixture )
{
e.expect().once();
mock::reset( o );
BOOST_CHECK( mock::verify( o ) );
}
BOOST_AUTO_TEST_CASE( an_object_is_assignable_by_sharing_its_state )
{
object o1;
mock::function< void() > e;
{
object o2;
mock::detail::configure( o2, e, "instance", mock::detail::type_name( typeid( "type" ) ), "name" );
e.expect().once();
o1 = o2;
BOOST_CHECK( ! mock::verify( o2 ) );
BOOST_CHECK( ! mock::verify( o1 ) );
}
BOOST_CHECK( ! mock::verify( o1 ) );
}
BOOST_AUTO_TEST_CASE( an_object_is_copiable_by_sharing_its_state )
{
std::auto_ptr< object > o2( new object );
const object o1( *o2 );
mock::function< void() > e;
mock::detail::configure( *o2, e, "instance", mock::detail::type_name( typeid( "type" ) ), "name" );
e.expect().once();
BOOST_CHECK( ! mock::verify( *o2 ) );
BOOST_CHECK( ! mock::verify( o1 ) );
o2.reset();
BOOST_CHECK( ! mock::verify( o1 ) );
}

110
test/test_sequence.cpp Normal file
View file

@ -0,0 +1,110 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include "mock_error.hpp"
#include <turtle/sequence.hpp>
#include <turtle/function.hpp>
#include <boost/test/auto_unit_test.hpp>
BOOST_AUTO_TEST_CASE( registering_to_a_sequence_and_calling_out_of_order_throws )
{
mock::sequence s;
mock::function< 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 ), std::exception );
}
BOOST_AUTO_TEST_CASE( registering_to_a_sequence_and_calling_out_of_order_multiple_invocations_throws )
{
mock::sequence s;
mock::function< 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 ), std::exception );
}
BOOST_AUTO_TEST_CASE( registering_to_a_sequence_and_calling_in_order_is_valid )
{
mock::sequence s;
mock::function< 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_CHECK( e.verify() );
}
BOOST_AUTO_TEST_CASE( registering_to_a_sequence_and_multiply_calling_in_order_is_valid )
{
mock::sequence s;
mock::function< void( int ) > e;
e.expect().exactly( 2 ).with( 1 ).in( s );
e.expect().exactly( 2 ).with( 2 ).in( s );
BOOST_CHECK_NO_THROW( e( 1 ) );
BOOST_CHECK_NO_THROW( e( 1 ) );
BOOST_CHECK_NO_THROW( e( 2 ) );
BOOST_CHECK_NO_THROW( e( 2 ) );
BOOST_CHECK( e.verify() );
}
BOOST_AUTO_TEST_CASE( registering_to_a_sequence_enforces_call_order_verification_between_two_different_expectations )
{
mock::sequence s;
mock::function< void() > e1, e2;
e1.expect().once().in( s );
e2.expect().once().in( s );
BOOST_CHECK_NO_THROW( e2() );
BOOST_CHECK_THROW( e1(), std::exception );
}
BOOST_AUTO_TEST_CASE( destroying_a_sequence_does_not_remove_order_call_enforcement )
{
mock::function< void() > e1, e2;
{
mock::sequence s;
e1.expect().once().in( s );
e2.expect().once().in( s );
}
BOOST_CHECK_NO_THROW( e2() );
BOOST_CHECK_THROW( e1(), std::exception );
}
BOOST_AUTO_TEST_CASE( resetting_an_expectation_removes_it_from_order_call_enforcement )
{
mock::sequence s;
mock::function< void() > e1, e2;
e1.expect().once().in( s );
e2.expect().once().in( s );
e1.reset();
BOOST_CHECK_NO_THROW( e2() );
BOOST_CHECK( e1.verify() );
BOOST_CHECK( e2.verify() );
}
BOOST_AUTO_TEST_CASE( an_expectation_can_be_used_in_several_sequences )
{
mock::sequence s1, s2;
mock::function< void() > e;
e.expect().once().in( s1 ).in( s2 );
BOOST_CHECK_NO_THROW( e() );
BOOST_CHECK( e.verify() );
}
BOOST_AUTO_TEST_CASE( a_result_specification_is_set_after_a_sequence )
{
mock::sequence s;
mock::function< int() > e;
e.expect().once().in( s ).returns( 3 );
BOOST_CHECK_EQUAL( 3, e() );
BOOST_CHECK( e.verify() );
}

80
test/test_type_name.cpp Normal file
View file

@ -0,0 +1,80 @@
//
// 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)
//
// See http://turtle.sf.net for documentation.
#include <turtle/type_name.hpp>
#include <boost/test/auto_unit_test.hpp>
#include <boost/lexical_cast.hpp>
struct my_type_from_default_namespace {};
namespace
{
template< typename T >
std::string to_string( const T& )
{
return boost::lexical_cast< std::string >( mock::detail::type_name( typeid( T ) ) );
}
}
BOOST_AUTO_TEST_CASE( name_of_type_from_default_namespace_is_extracted )
{
BOOST_CHECK_EQUAL( "my_type_from_default_namespace", to_string( my_type_from_default_namespace() ) );
}
namespace
{
struct my_type_from_anonymous_namespace {};
}
BOOST_AUTO_TEST_CASE( name_of_type_from_anonymous_namespace_is_extracted )
{
BOOST_CHECK_EQUAL( "my_type_from_anonymous_namespace", to_string( my_type_from_anonymous_namespace() ) );
}
namespace nm
{
struct my_type_from_named_namespace {};
}
BOOST_AUTO_TEST_CASE( name_of_type_from_named_namespace_is_extracted )
{
BOOST_CHECK_EQUAL( "my_type_from_named_namespace", to_string( nm::my_type_from_named_namespace() ) );
}
namespace nm
{
namespace inner
{
struct my_type_from_named_inner_namespace {};
}
}
BOOST_AUTO_TEST_CASE( name_of_type_from_named_inner_namespace_is_extracted )
{
BOOST_CHECK_EQUAL( "my_type_from_named_inner_namespace", to_string( nm::inner::my_type_from_named_inner_namespace() ) );
}
namespace
{
namespace inner
{
struct my_type_from_unnamed_inner_namespace {};
}
}
BOOST_AUTO_TEST_CASE( name_of_type_from_unnamed_inner_namespace_is_extracted )
{
BOOST_CHECK_EQUAL( "my_type_from_unnamed_inner_namespace", to_string( inner::my_type_from_unnamed_inner_namespace() ) );
}
BOOST_AUTO_TEST_CASE( name_of_local_type_is_extracted )
{
struct my_local_type {};
BOOST_CHECK_EQUAL( "my_local_type", boost::lexical_cast< std::string >( mock::detail::type_name( typeid( my_local_type ) ) ) );
}