Forced MOCK_ERROR_POLICY to mock_error across all tests to prevent ODR violation

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@697 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2013-11-13 09:42:55 +00:00
parent dbf430692d
commit 0609c6c1fa
10 changed files with 312 additions and 247 deletions

View file

@ -11,81 +11,106 @@
#define MOCK_ERROR_POLICY mock_error
#include <boost/lexical_cast.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/utils/trivial_singleton.hpp>
#include <stdexcept>
namespace
struct mock_error_data_t : boost::unit_test::singleton< mock_error_data_t >
{
struct mock_error_data
~mock_error_data_t()
{
void reset()
{
call_count = 0;
error_count = 0;
last_message.clear();
last_context.clear();
}
bool verify()
{
return error_count == 0;
}
void call()
{
last_context.clear();
last_message.clear();
++call_count;
}
void fail( const std::string& message,
const std::string& context,
const char* file, int line )
{
last_context = context;
last_message = message;
last_file = file;
last_line = line;
++error_count;
}
int error_count;
int call_count;
std::string last_message;
std::string last_context;
std::string last_file;
int last_line;
};
inline mock_error_data& data()
assert( verify() );
}
void reset()
{
static mock_error_data p;
return p;
call_count = 0;
error_count = 0;
last_message.clear();
last_context.clear();
}
bool verify()
{
return error_count == 0;
}
template< typename Result >
struct mock_error
void call()
{
static Result abort()
{
throw std::runtime_error( "aborted" );
}
last_context.clear();
last_message.clear();
++call_count;
}
void fail( const std::string& message,
const std::string& context,
const char* file, int line )
{
last_context = context;
last_message = message;
last_file = file;
last_line = line;
++error_count;
}
static void pass( const char* /*file*/, int /*line*/ )
{}
int error_count;
int call_count;
std::string last_message;
std::string last_context;
std::string last_file;
int last_line;
private:
BOOST_TEST_SINGLETON_CONS( mock_error_data_t );
};
BOOST_TEST_SINGLETON_INST( mock_error_data )
template< typename Context >
static void call( const Context& /*context*/,
const char* /*file*/, int /*line*/ )
{
data().call();
}
template< typename Result >
struct mock_error
{
static Result abort()
{
throw std::runtime_error( "aborted" );
}
template< typename Context >
static void fail( const std::string& message, const Context& context,
const char* file = "", int line = 0 )
{
data().fail( message,
boost::lexical_cast< std::string >( context ), file, line );
}
};
}
static void pass( const char* /*file*/, int /*line*/ )
{}
template< typename Context >
static void call( const Context& /*context*/,
const char* /*file*/, int /*line*/ )
{
mock_error_data.call();
}
template< typename Context >
static void fail( const std::string& message, const Context& context,
const char* file = "", int line = 0 )
{
mock_error_data.fail( message,
boost::lexical_cast< std::string >( context ), file, line );
}
};
struct mock_error_fixture
{
mock_error_fixture()
{
mock_error_data.reset();
}
~mock_error_fixture()
{
BOOST_CHECK( mock_error_data.verify() );
BOOST_CHECK_EQUAL( 0, mock_error_data.call_count );
}
};
#define CHECK_CALLS( calls ) \
BOOST_CHECK_EQUAL( calls, mock_error_data.call_count ); \
mock_error_data.call_count = 0;
#define CHECK_ERROR( expr, error, calls, context ) \
BOOST_CHECK( mock_error_data.verify() ); \
try { expr; } catch( ... ) {} \
BOOST_CHECK_EQUAL( 1, mock_error_data.error_count ); \
BOOST_CHECK_EQUAL( error, mock_error_data.last_message ); \
BOOST_CHECK_EQUAL( context, mock_error_data.last_context ); \
CHECK_CALLS( calls ); \
mock_error_data.reset();
#endif // MOCK_TEST_MOCK_ERROR_HPP_INCLUDED