mirror of
https://github.com/mat007/turtle.git
synced 2026-06-22 12:13:43 +00:00
Error policy now customizable and missing result specifications now reported with file name and line
git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@67 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
parent
51379e7844
commit
6ff9fc5564
14 changed files with 380 additions and 271 deletions
|
|
@ -23,4 +23,12 @@ BOOST_PP_ASSERT( BOOST_PP_LESS_EQUAL(MOCK_MAX_ARGS, BOOST_FUNCTION_MAX_ARGS) )
|
|||
# define MOCK_USE_BOOST_TEST
|
||||
#endif
|
||||
|
||||
#ifndef MOCK_ERROR_POLICY
|
||||
# ifdef MOCK_USE_BOOST_TEST
|
||||
# define MOCK_ERROR_POLICY boost_test_error_policy
|
||||
# else
|
||||
# define MOCK_ERROR_POLICY basic_error_policy
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#endif // #ifndef MOCK_CONFIG_HPP_INCLUDED
|
||||
|
|
|
|||
|
|
@ -9,54 +9,43 @@
|
|||
#ifndef MOCK_ERROR_HPP_INCLUDED
|
||||
#define MOCK_ERROR_HPP_INCLUDED
|
||||
|
||||
#include "config.hpp"
|
||||
#include <boost/test/framework.hpp>
|
||||
#include <boost/test/test_tools.hpp>
|
||||
#include <boost/test/unit_test_suite.hpp>
|
||||
#include <boost/test/execution_monitor.hpp>
|
||||
#include <boost/test/utils/trivial_singleton.hpp>
|
||||
#include <boost/exception/enable_current_exception.hpp>
|
||||
#include <iostream>
|
||||
|
||||
namespace mock
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
class errors_t : public boost::unit_test::singleton< errors_t >
|
||||
{
|
||||
private:
|
||||
friend class boost::unit_test::singleton< errors_t >;
|
||||
};
|
||||
BOOST_TEST_SINGLETON_INST( errors )
|
||||
}
|
||||
|
||||
class exception : public boost::execution_exception
|
||||
{
|
||||
public:
|
||||
explicit exception( const std::string& s )
|
||||
: boost::execution_exception( boost::execution_exception::user_error, s )
|
||||
{}
|
||||
};
|
||||
|
||||
template< typename Result >
|
||||
struct boost_test_error_policy
|
||||
{
|
||||
static void missing_result_specification()
|
||||
static Result missing_result_specification( const std::string& context,
|
||||
const std::string& file, int line )
|
||||
{
|
||||
static std::string m;
|
||||
m = "mock error : missing result specification";
|
||||
throw boost::enable_current_exception( mock::exception( m ) );
|
||||
notify( "mock error : missing result specification : " + context,
|
||||
file, line );
|
||||
throw boost::enable_current_exception(
|
||||
boost::execution_aborted() );
|
||||
}
|
||||
|
||||
static Result no_match( const std::string& context )
|
||||
{
|
||||
static std::string m;
|
||||
m = "mock error : unexpected call : " + context;
|
||||
throw boost::enable_current_exception( mock::exception( m ) );
|
||||
notify( "mock error : unexpected call : " + context,
|
||||
"unknown location", 0 );
|
||||
throw boost::enable_current_exception(
|
||||
boost::execution_aborted() );
|
||||
}
|
||||
|
||||
static void sequence_failed( const std::string& context,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
static std::string m;
|
||||
m = "mock error : sequence failed : " + context;
|
||||
throw boost::enable_current_exception( mock::exception( m ) );
|
||||
notify( "mock error : sequence failed : " + context,
|
||||
"unknown location", 0 );
|
||||
throw boost::enable_current_exception(
|
||||
boost::execution_aborted() );
|
||||
}
|
||||
|
||||
static void verification_failed( const std::string& context,
|
||||
|
|
@ -74,13 +63,58 @@ namespace detail
|
|||
static void notify( const std::string& message,
|
||||
const std::string& file, int line )
|
||||
{
|
||||
boost::test_tools::tt_detail::check_impl(
|
||||
false,
|
||||
boost::unit_test::lazy_ostream::instance() << message,
|
||||
file, (std::size_t)line,
|
||||
boost::test_tools::tt_detail::CHECK,
|
||||
boost::test_tools::tt_detail::CHECK_MSG,
|
||||
0 );
|
||||
boost::unit_test::unit_test_log
|
||||
<< boost::unit_test::log::begin( file, (std::size_t)line )
|
||||
<< boost::unit_test::log_all_errors
|
||||
<< boost::unit_test::lazy_ostream::instance() << message
|
||||
<< boost::unit_test::log::end();
|
||||
}
|
||||
};
|
||||
|
||||
struct exception
|
||||
{};
|
||||
|
||||
template< typename Result >
|
||||
struct basic_error_policy
|
||||
{
|
||||
static void log( const std::string& message,
|
||||
const std::string& context,
|
||||
const std::string& file = "unknown location", int line = 0 )
|
||||
{
|
||||
std::cerr << file << '(' << line << "): "
|
||||
<< "mock error: " << message << ": " << context << std::endl;
|
||||
}
|
||||
|
||||
static Result missing_result_specification( const std::string& context,
|
||||
const std::string& file, int line )
|
||||
{
|
||||
log( "missing result specification", context, file, line );
|
||||
throw exception();
|
||||
}
|
||||
|
||||
static Result no_match( const std::string& context )
|
||||
{
|
||||
log( "unexpected call", context );
|
||||
throw exception();
|
||||
}
|
||||
|
||||
static void sequence_failed( const std::string& context,
|
||||
const std::string& /*file*/, int /*line*/ )
|
||||
{
|
||||
log( "sequence failed", context );
|
||||
throw exception();
|
||||
}
|
||||
|
||||
static void verification_failed( const std::string& context,
|
||||
const std::string& file, int line )
|
||||
{
|
||||
log( "verification failed", context, file, line );
|
||||
}
|
||||
|
||||
static void untriggered_expectation( const std::string& context,
|
||||
const std::string& file, int line )
|
||||
{
|
||||
log( "untriggered expectation", context, file, line );
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,9 +29,9 @@
|
|||
namespace mock
|
||||
{
|
||||
template< typename Signature,
|
||||
typename ErrorPolicy = boost_test_error_policy<
|
||||
typename ErrorPolicy = MOCK_ERROR_POLICY<
|
||||
BOOST_DEDUCED_TYPENAME boost::function<
|
||||
Signature >::result_type > > // $$$$ MAT : concept check Signature is actually a signature
|
||||
Signature >::result_type > >
|
||||
class expectation
|
||||
{
|
||||
public:
|
||||
|
|
@ -47,7 +47,6 @@ namespace mock
|
|||
private:
|
||||
typedef detail::matcher< result_type,
|
||||
Signature,
|
||||
ErrorPolicy,
|
||||
boost::function< Signature >::arity >
|
||||
matcher_type;
|
||||
|
||||
|
|
@ -185,12 +184,15 @@ namespace mock
|
|||
it != matchers_.end(); ++it )
|
||||
if( it->is_valid() )
|
||||
{
|
||||
if( !it->invoke() )
|
||||
if( ! it->invoke() )
|
||||
{
|
||||
valid_ = false;
|
||||
ErrorPolicy::sequence_failed( context( "" ),
|
||||
it->file(), it->line() );
|
||||
}
|
||||
if( ! it->functor() )
|
||||
return ErrorPolicy::missing_result_specification(
|
||||
context( "" ), it->file(), it->line() );
|
||||
return it->functor()();
|
||||
}
|
||||
valid_ = false;
|
||||
|
|
@ -208,11 +210,14 @@ namespace mock
|
|||
for( matchers_cit it = matchers_.begin(); it != matchers_.end(); ++it ) \
|
||||
if( it->is_valid( BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_PARAMETER, BOOST_PP_EMPTY) ) ) \
|
||||
{ \
|
||||
if( !it->invoke() ) \
|
||||
if( ! it->invoke() ) \
|
||||
{ \
|
||||
valid_ = false; \
|
||||
ErrorPolicy::sequence_failed( context( MOCK_EXPECTATION_PARAMETERS(n) ), it->file(), it->line() ); \
|
||||
} \
|
||||
if( ! it->functor() ) \
|
||||
return ErrorPolicy::missing_result_specification( \
|
||||
context( MOCK_EXPECTATION_PARAMETERS(n) ), it->file(), it->line() ); \
|
||||
return it->functor()( BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_PARAMETER, BOOST_PP_EMPTY) ); \
|
||||
} \
|
||||
valid_ = false; \
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ namespace detail
|
|||
int line_;
|
||||
};
|
||||
|
||||
template< typename Result, typename Signature, typename ErrorPolicy, int >
|
||||
template< typename Result, typename Signature, int >
|
||||
class matcher
|
||||
{
|
||||
};
|
||||
|
|
@ -146,9 +146,9 @@ namespace detail
|
|||
return *this; \
|
||||
}
|
||||
|
||||
template< typename Result, typename Signature, typename ErrorPolicy >
|
||||
class matcher< Result, Signature, ErrorPolicy, 0 >
|
||||
: public matcher_base, public result< Result, Signature, ErrorPolicy >
|
||||
template< typename Result, typename Signature >
|
||||
class matcher< Result, Signature, 0 >
|
||||
: public matcher_base, public result< Result, Signature >
|
||||
{
|
||||
public:
|
||||
bool is_valid() const
|
||||
|
|
@ -176,9 +176,9 @@ namespace detail
|
|||
#define MOCK_MATCHER_IS_VALID(z, n, d) && c##n##_( a##n )
|
||||
#define MOCK_MATCHER_SERIALIZE(z, n, d) << ", " << m.c##n##_
|
||||
#define MOCK_MATCHER(z, n, d) \
|
||||
template< typename Result, typename Signature, typename ErrorPolicy > \
|
||||
class matcher< Result, Signature, ErrorPolicy, n > \
|
||||
: public matcher_base, public result< Result, Signature, ErrorPolicy > \
|
||||
template< typename Result, typename Signature > \
|
||||
class matcher< Result, Signature, n > \
|
||||
: public matcher_base, public result< Result, Signature > \
|
||||
{ \
|
||||
BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_MATCHER_TYPEDEF, BOOST_PP_EMPTY) \
|
||||
public: \
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ namespace mock
|
|||
{
|
||||
namespace detail
|
||||
{
|
||||
template< typename T, typename Signature, typename ErrorPolicy >
|
||||
template< typename T, typename Signature >
|
||||
class result
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME
|
||||
|
|
@ -46,8 +46,6 @@ namespace detail
|
|||
|
||||
const functor_type& functor() const
|
||||
{
|
||||
if( !f_ )
|
||||
ErrorPolicy::missing_result_specification();
|
||||
return f_;
|
||||
}
|
||||
|
||||
|
|
@ -76,8 +74,8 @@ namespace detail
|
|||
functor_type f_;
|
||||
};
|
||||
|
||||
template< typename T, typename Signature, typename ErrorPolicy >
|
||||
class result< T*, Signature, ErrorPolicy >
|
||||
template< typename T, typename Signature >
|
||||
class result< T*, Signature >
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME
|
||||
boost::function< Signature > functor_type;
|
||||
|
|
@ -108,8 +106,6 @@ namespace detail
|
|||
|
||||
const functor_type& functor() const
|
||||
{
|
||||
if( !f_ )
|
||||
ErrorPolicy::missing_result_specification();
|
||||
return f_;
|
||||
}
|
||||
|
||||
|
|
@ -128,8 +124,8 @@ namespace detail
|
|||
functor_type f_;
|
||||
};
|
||||
|
||||
template< typename Signature, typename ErrorPolicy >
|
||||
class result< void, Signature, ErrorPolicy >
|
||||
template< typename Signature >
|
||||
class result< void, Signature >
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME
|
||||
boost::function< Signature > functor_type;
|
||||
|
|
@ -170,8 +166,8 @@ namespace detail
|
|||
functor_type f_;
|
||||
};
|
||||
|
||||
template< typename T, typename Signature, typename ErrorPolicy >
|
||||
class result< std::auto_ptr< T >, Signature, ErrorPolicy >
|
||||
template< typename T, typename Signature >
|
||||
class result< std::auto_ptr< T >, Signature >
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME
|
||||
boost::function< Signature > functor_type;
|
||||
|
|
@ -208,8 +204,6 @@ namespace detail
|
|||
|
||||
const functor_type& functor() const
|
||||
{
|
||||
if( !f_ )
|
||||
ErrorPolicy::missing_result_specification();
|
||||
return f_;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
#ifndef MOCK_ROOT_HPP_INCLUDED
|
||||
#define MOCK_ROOT_HPP_INCLUDED
|
||||
|
||||
#include "config.hpp"
|
||||
#include "node.hpp"
|
||||
#include <boost/test/utils/trivial_singleton.hpp>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue