Merged the logging branch

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@220 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2011-01-29 21:34:45 +00:00
parent 78befdb9be
commit f7be6ea503
12 changed files with 517 additions and 314 deletions

View file

@ -8,10 +8,9 @@
#include <turtle/format.hpp>
#include <boost/assign.hpp>
#include <ostream>
#include <vector>
#include <deque>
#include <list>
#include <vector>
#include <map>
#include <set>

View file

@ -20,11 +20,12 @@
#define CHECK_CALLS( calls ) \
BOOST_CHECK_EQUAL( calls, expected_call_count ); \
expected_call_count = 0;
#define CHECK_ERROR( expr, error, calls ) \
#define CHECK_ERROR( expr, error, calls, context ) \
BOOST_CHECK( verify() ); \
expr; \
BOOST_CHECK_EQUAL( 1, error##_count ); \
CHECK_CALLS( calls ); \
BOOST_CHECK_EQUAL( context, last_context ); \
reset();
namespace
@ -48,6 +49,7 @@ namespace
sequence_failed_count = 0;
verification_failed_count = 0;
untriggered_expectation_count = 0;
last_context.clear();
}
bool verify() const
{
@ -87,11 +89,11 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_unconfigured_function_calls_unexpected_ca
{
{
mock::function< void() > f;
CHECK_ERROR( f(), unexpected_call, 0 );
CHECK_ERROR( f(), unexpected_call, 0, "?()" );
}
{
mock::function< int( int, const std::string& ) > f;
CHECK_ERROR( f( 1, "s" ), unexpected_call, 0 );
CHECK_ERROR( f( 1, "s" ), unexpected_call, 0, "?( 1, \"s\" )" );
}
}
@ -100,12 +102,12 @@ BOOST_FIXTURE_TEST_CASE( triggering_a_never_expectation_calls_unexpected_call_er
{
mock::function< void() > f;
f.expect().never();
CHECK_ERROR( f(), unexpected_call, 0 );
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 );
CHECK_ERROR( f( 1, "s" ), unexpected_call, 0, "?( 1, \"s\" )\nv never().with( any, any )" );
}
}
@ -133,13 +135,13 @@ BOOST_FIXTURE_TEST_CASE( triggering_a_once_expectation_calls_unexpected_call_err
mock::function< void() > f;
f.expect().once();
f();
CHECK_ERROR( f(), unexpected_call, 1 );
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 );
CHECK_ERROR( f( 1, "s" ), unexpected_call, 1, "?( 1, \"s\" )\nv once().with( any, any )" );
}
}
@ -206,12 +208,12 @@ BOOST_FIXTURE_TEST_CASE( verifying_a_once_expectation_before_the_call_fails, err
{
mock::function< void() > f;
f.expect().once();
CHECK_ERROR( BOOST_CHECK( ! f.verify() ), verification_failed, 0 );
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 );
CHECK_ERROR( BOOST_CHECK( ! f.verify() ), verification_failed, 0, "?\n. once().with( any, any )" );
}
}
@ -223,13 +225,13 @@ BOOST_FIXTURE_TEST_CASE( triggering_a_reset_function_calls_unexpected_call_error
mock::function< void() > f;
f.expect();
f.reset();
CHECK_ERROR( f(), unexpected_call, 0 );
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 );
CHECK_ERROR( f( 1, "s" ), unexpected_call, 0, "?( 1, \"s\" )" );
}
}
@ -256,12 +258,12 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in
{
mock::function< void( int ) > f;
f.expect().with( 42 );
CHECK_ERROR( f( 43 ), unexpected_call, 0 );
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 );
CHECK_ERROR( f( 42, "actual" ), unexpected_call, 0, "?( 42, \"actual\" )\n. unlimited().with( 42, \"expected\" )" );
}
}
@ -271,7 +273,7 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in
f.expect().with( mock::equal( 42 ) || mock::less( 42 ) );
f( 41 );
f( 42 );
CHECK_ERROR( f( 43 ), unexpected_call, 2 );
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 )
@ -279,7 +281,7 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in
mock::function< void( int ) > f;
f.expect().with( mock::equal( 42 ) && ! mock::less( 41 ) );
f( 42 );
CHECK_ERROR( f( 43 ), unexpected_call, 1 );
CHECK_ERROR( f( 43 ), unexpected_call, 1, "?( 43 )\n. unlimited().with( ( equal( 42 ) && ! less( 41 ) ) )" );
}
namespace
@ -328,12 +330,12 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_failing_custom_constrain
{
mock::function< void( int ) > f;
f.expect().with( &custom_constraint );
CHECK_ERROR( f( 42 ), unexpected_call, 0 );
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 );
CHECK_ERROR( f( 42, "actual" ), unexpected_call, 0, "?( 42, \"actual\" )\n. unlimited().with( ?, \"actual\" )" );
}
}
@ -354,17 +356,17 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_no_return_set_calls_miss
{
mock::function< int() > f;
f.expect();
CHECK_ERROR( f(), missing_action, 0 );
CHECK_ERROR( f(), missing_action, 0, "?()\n. unlimited()" );
}
{
mock::function< int&() > f;
f.expect();
CHECK_ERROR( f(), missing_action, 0 );
CHECK_ERROR( f(), missing_action, 0, "?()\n. unlimited()" );
}
{
mock::function< const std::string&() > f;
f.expect();
CHECK_ERROR( f(), missing_action, 0 );
CHECK_ERROR( f(), missing_action, 0, "?()\n. unlimited()" );
}
}
@ -582,7 +584,7 @@ BOOST_FIXTURE_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_
f.expect().once();
f();
f();
CHECK_ERROR( f(), unexpected_call, 2 );
CHECK_ERROR( f(), unexpected_call, 2, "?()\nv once()\nv once()" );
}
{
mock::function< void( const std::string& ) > f;
@ -590,7 +592,7 @@ BOOST_FIXTURE_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_
f.expect().once().with( "second" );
f( "first" );
f( "second" );
CHECK_ERROR( f( "third"), unexpected_call, 2 );
CHECK_ERROR( f( "third"), unexpected_call, 2, "?( \"third\" )\nv once().with( \"first\" )\nv once().with( \"second\" )" );
}
}
@ -602,7 +604,7 @@ BOOST_FIXTURE_TEST_CASE( best_expectation_is_selected_first, error_fixture )
f.expect().once().with( 2 );
f( 2 );
f( 1 );
CHECK_ERROR( f( 3 ), unexpected_call, 2 );
CHECK_ERROR( f( 3 ), unexpected_call, 2, "?( 3 )\nv once().with( 1 )\nv once().with( 2 )" );
}
{
mock::function< void( const std::string& ) > f;
@ -610,7 +612,7 @@ BOOST_FIXTURE_TEST_CASE( best_expectation_is_selected_first, error_fixture )
f.expect().once().with( "second" );
f( "second" );
f( "first" );
CHECK_ERROR( f( "third"), unexpected_call, 2 );
CHECK_ERROR( f( "third"), unexpected_call, 2, "?( \"third\" )\nv once().with( \"first\" )\nv once().with( \"second\" )" );
}
}
@ -714,28 +716,28 @@ BOOST_FIXTURE_TEST_CASE( expectation_with_remaining_untriggered_matches_upon_des
{
std::auto_ptr< mock::function< void() > > f( new mock::function< void() > );
f->expect().once();
CHECK_ERROR( f.reset(), untriggered_expectation, 0 );
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 );
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 );
CHECK_ERROR( f(), unexpected_call, 0, "?()" );
}
BOOST_FIXTURE_TEST_CASE( adding_a_expectation_reactivates_the_verification_upon_destruction, error_fixture )
{
std::auto_ptr< mock::function< void() > > f( new mock::function< void() > );
CHECK_ERROR( (*f)(), unexpected_call, 0 );
CHECK_ERROR( (*f)(), unexpected_call, 0, "?()" );
f->expect().once();
CHECK_ERROR( f.reset(), untriggered_expectation, 0 );
CHECK_ERROR( f.reset(), untriggered_expectation, 0, "?\n. once()" );
}
BOOST_FIXTURE_TEST_CASE( throwing_an_exception_disables_the_automatic_verification_upon_destruction, error_fixture )

View file

@ -329,6 +329,41 @@ BOOST_AUTO_TEST_CASE( boost_optional_on_base_class_reference_as_return_type_is_s
b.method();
}
namespace
{
bool serialized = false;
struct custom_argument
{
friend std::ostream& operator<<( std::ostream& s, const custom_argument& )
{
serialized = true;
return s;
}
};
struct custom_constraint
{
template< typename Actual >
friend bool operator==( Actual, const custom_constraint& )
{
return true;
}
friend std::ostream& operator<<( std::ostream& s, const custom_constraint& )
{
serialized = true;
return s;
}
};
}
BOOST_AUTO_TEST_CASE( constraints_and_arguments_are_serialized_lazily )
{
MOCK_FUNCTOR( void( custom_argument ) ) f;
MOCK_EXPECT( f, _ ).with( custom_constraint() );
f( custom_argument() );
BOOST_CHECK( ! serialized );
}
namespace
{
template< typename Expected >

View file

@ -10,6 +10,7 @@
#define MOCK_TEST_MOCK_ERROR_HPP_INCLUDED
#include <boost/type_traits/remove_reference.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
namespace
@ -20,6 +21,8 @@ namespace
int sequence_failed_count = 0;
int verification_failed_count = 0;
int untriggered_expectation_count = 0;
std::string last_context;
}
namespace mock
{
@ -32,33 +35,45 @@ namespace mock
return r;
}
static void missing_action( const std::string& /*context*/,
template< typename Context >
static void missing_action( const Context& context,
const std::string& /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++missing_action_count;
}
static void expected_call( const std::string& /*context*/,
template< typename Context >
static void expected_call( const Context& context,
const std::string& /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++expected_call_count;
}
static void unexpected_call( const std::string& /*context*/ )
template< typename Context >
static void unexpected_call( const Context& context )
{
last_context = boost::lexical_cast< std::string >( context );
++unexpected_call_count;
}
static void sequence_failed( const std::string& /*context*/,
template< typename Context >
static void sequence_failed( const Context& context,
const std::string& /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++sequence_failed_count;
}
static void verification_failed( const std::string& /*context*/,
template< typename Context >
static void verification_failed( const Context& context,
const std::string& /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++verification_failed_count;
}
static void untriggered_expectation( const std::string& /*context*/,
template< typename Context >
static void untriggered_expectation( const Context& context,
const std::string& /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++untriggered_expectation_count;
}
};
@ -67,33 +82,45 @@ namespace mock
{
static void abort()
{}
static void missing_action( const std::string& /*context*/,
template< typename Context >
static void missing_action( const Context& context,
const std::string& /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++missing_action_count;
}
static void expected_call( const std::string& /*context*/,
template< typename Context >
static void expected_call( const Context& context,
const std::string& /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++expected_call_count;
}
static void unexpected_call( const std::string& /*context*/ )
template< typename Context >
static void unexpected_call( const Context& context )
{
last_context = boost::lexical_cast< std::string >( context );
++unexpected_call_count;
}
static void sequence_failed( const std::string& /*context*/,
template< typename Context >
static void sequence_failed( const Context& context,
const std::string& /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++sequence_failed_count;
}
static void verification_failed( const std::string& /*context*/,
template< typename Context >
static void verification_failed( const Context& context,
const std::string& /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++verification_failed_count;
}
static void untriggered_expectation( const std::string& /*context*/,
template< typename Context >
static void untriggered_expectation( const Context& context,
const std::string& /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++untriggered_expectation_count;
}
};

View file

@ -10,7 +10,6 @@
#define MOCK_TEST_SILENT_ERROR_HPP_INCLUDED
#include <string>
#include <sstream>
#include <stdexcept>
namespace mock
@ -22,21 +21,27 @@ namespace mock
{
throw std::runtime_error( "abort" );
}
static void expected_call( const std::string& /*context*/,
template< typename Context >
static void expected_call( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ )
{}
static void unexpected_call( const std::string& /*context*/ )
template< typename Context >
static void unexpected_call( const Context& /*context*/ )
{}
static void missing_action( const std::string& /*context*/,
template< typename Context >
static void missing_action( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ )
{}
static void sequence_failed( const std::string& /*context*/,
template< typename Context >
static void sequence_failed( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ )
{}
static void verification_failed( const std::string& /*context*/,
template< typename Context >
static void verification_failed( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ )
{}
static void untriggered_expectation( const std::string& /*context*/,
template< typename Context >
static void untriggered_expectation( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ )
{}
};