Refactoring

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@311 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2011-05-08 17:44:49 +00:00
parent 8d7b34792d
commit 5f651655c6
15 changed files with 194 additions and 240 deletions

View file

@ -160,6 +160,10 @@
RelativePath="..\..\src\libraries\turtle\args.hpp" RelativePath="..\..\src\libraries\turtle\args.hpp"
> >
</File> </File>
<File
RelativePath="..\..\src\libraries\turtle\boost_test_error.hpp"
>
</File>
<File <File
RelativePath="..\..\src\libraries\turtle\check.hpp" RelativePath="..\..\src\libraries\turtle\check.hpp"
> >
@ -176,6 +180,10 @@
RelativePath="..\..\src\libraries\turtle\constraints.hpp" RelativePath="..\..\src\libraries\turtle\constraints.hpp"
> >
</File> </File>
<File
RelativePath="..\..\src\libraries\turtle\default_error.hpp"
>
</File>
<File <File
RelativePath="..\..\src\libraries\turtle\error.hpp" RelativePath="..\..\src\libraries\turtle\error.hpp"
> >

View file

@ -238,10 +238,6 @@
RelativePath="..\..\src\tests\turtle_test\sequence_test.cpp" RelativePath="..\..\src\tests\turtle_test\sequence_test.cpp"
> >
</File> </File>
<File
RelativePath="..\..\src\tests\turtle_test\silent_error.hpp"
>
</File>
<File <File
RelativePath="..\..\src\tests\turtle_test\type_name_test.cpp" RelativePath="..\..\src\tests\turtle_test\type_name_test.cpp"
> >

View file

@ -0,0 +1,91 @@
//
// 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)
//
#ifndef MOCK_BOOST_TEST_ERROR_POLICY_HPP_INCLUDED
#define MOCK_BOOST_TEST_ERROR_POLICY_HPP_INCLUDED
#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/exception/enable_current_exception.hpp>
#include <string>
namespace mock
{
struct exception : public boost::execution_aborted
{};
template< typename Result >
struct boost_test_error_policy
{
static Result abort()
{
boost::unit_test::framework::test_unit_aborted(
boost::unit_test::framework::current_test_case() );
throw boost::enable_current_exception( exception() );
}
template< typename Context >
static void fail(
const std::string& message, const Context& context,
const std::string& file = "unknown location", int line = 0 )
{
boost::unit_test::framework::assertion_result( false );
boost::unit_test::unit_test_log
<< boost::unit_test::log::begin( file, (std::size_t)line )
<< boost::unit_test::log_all_errors
<< message << ": " << context
<< boost::unit_test::log::end();
}
template< typename Context >
static void expected_call( const Context& context,
const std::string& file, int line )
{
boost::unit_test::framework::assertion_result( true );
boost::unit_test::unit_test_log
<< boost::unit_test::log::begin( file, (std::size_t)line )
<< boost::unit_test::log_successful_tests
<< "mock expectation fulfilled: " << context
<< boost::unit_test::log::end();
}
template< typename Context >
static void missing_action( const Context& context,
const std::string& file, int line )
{
fail( "missing action", context, file, line );
}
template< typename Context >
static void unexpected_call( const Context& context )
{
fail( "unexpected call", context );
}
template< typename Context >
static void sequence_failed( const Context& context,
const std::string& /*file*/, int /*line*/ )
{
fail( "sequence failed", context );
}
template< typename Context >
static void verification_failed( const Context& context,
const std::string& file, int line )
{
fail( "verification failed", context, file, line );
}
template< typename Context >
static void untriggered_expectation( const Context& context,
const std::string& file, int line )
{
fail( "untriggered expectation", context, file, line );
}
};
}
#endif // MOCK_BOOST_TEST_ERROR_POLICY_HPP_INCLUDED

View file

@ -32,16 +32,4 @@
# error BOOST_FUNCTION_MAX_ARGS must be set to MOCK_MAX_ARGS or higher # error BOOST_FUNCTION_MAX_ARGS must be set to MOCK_MAX_ARGS or higher
#endif #endif
#ifdef BOOST_TEST_DECL
# 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 // MOCK_CONFIG_HPP_INCLUDED #endif // MOCK_CONFIG_HPP_INCLUDED

View file

@ -0,0 +1,74 @@
//
// 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)
//
#ifndef MOCK_DEFAULT_ERROR_POLICY_HPP_INCLUDED
#define MOCK_DEFAULT_ERROR_POLICY_HPP_INCLUDED
#include <iostream>
#include <string>
namespace mock
{
struct exception
{};
template< typename Result >
struct basic_error_policy
{
static Result abort()
{
throw exception();
}
template< typename Context >
static void fail(
const std::string& message, const Context& context,
const std::string& file = "unknown location", int line = 0 )
{
std::cerr << file << '(' << line << "): "
<< message << ": " << context << std::endl;
}
template< typename Context >
static void expected_call( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ )
{}
template< typename Context >
static void missing_action( const Context& context,
const std::string& file, int line )
{
fail( "missing action", context, file, line );
}
template< typename Context >
static void unexpected_call( const Context& context )
{
fail( "unexpected call", context );
}
template< typename Context >
static void sequence_failed( const Context& context,
const std::string& /*file*/, int /*line*/ )
{
fail( "sequence failed", context );
}
template< typename Context >
static void verification_failed( const Context& context,
const std::string& file, int line )
{
fail( "verification failed", context, file, line );
}
template< typename Context >
static void untriggered_expectation( const Context& context,
const std::string& file, int line )
{
fail( "untriggered expectation", context, file, line );
}
};
}
#endif // MOCK_DEFAULT_ERROR_POLICY_HPP_INCLUDED

View file

@ -10,118 +10,15 @@
#define MOCK_ERROR_HPP_INCLUDED #define MOCK_ERROR_HPP_INCLUDED
#include "config.hpp" #include "config.hpp"
#ifdef MOCK_USE_BOOST_TEST
#include <boost/test/framework.hpp> #ifndef MOCK_ERROR_POLICY
#include <boost/test/test_tools.hpp> # if defined(BOOST_TEST_DECL) || defined(MOCK_USE_BOOST_TEST)
#include <boost/test/unit_test_suite.hpp> # define MOCK_ERROR_POLICY boost_test_error_policy
#include <boost/test/execution_monitor.hpp> # include "boost_test_error.hpp"
#include <boost/exception/enable_current_exception.hpp>
# else # else
#include <iostream> # define MOCK_ERROR_POLICY basic_error_policy
#endif // MOCK_USE_BOOST_TEST # include "default_error.hpp"
#include <string> # endif
#endif
namespace mock
{
#ifdef MOCK_USE_BOOST_TEST
struct exception : public boost::execution_aborted
{};
template< typename Result >
struct boost_test_error_policy
{
static Result abort()
{
boost::unit_test::framework::test_unit_aborted(
boost::unit_test::framework::current_test_case() );
throw boost::enable_current_exception( exception() );
}
template< typename Context >
static void fail(
const std::string& message, const Context& context,
const std::string& file = "unknown location", int line = 0 )
{
boost::unit_test::framework::assertion_result( false );
boost::unit_test::unit_test_log
<< boost::unit_test::log::begin( file, (std::size_t)line )
<< boost::unit_test::log_all_errors
<< message << ": " << context
<< boost::unit_test::log::end();
}
template< typename Context >
static void expected_call( const Context& context,
const std::string& file, int line )
{
boost::unit_test::framework::assertion_result( true );
boost::unit_test::unit_test_log
<< boost::unit_test::log::begin( file, (std::size_t)line )
<< boost::unit_test::log_successful_tests
<< "mock expectation fulfilled: " << context
<< boost::unit_test::log::end();
}
#else // MOCK_USE_BOOST_TEST
struct exception
{};
template< typename Result >
struct basic_error_policy
{
static Result abort()
{
throw exception();
}
template< typename Context >
static void fail(
const std::string& message, const Context& context,
const std::string& file = "unknown location", int line = 0 )
{
std::cerr << file << '(' << line << "): "
<< message << ": " << context << std::endl;
}
template< typename Context >
static void expected_call( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ )
{}
#endif // MOCK_USE_BOOST_TEST
template< typename Context >
static void missing_action( const Context& context,
const std::string& file, int line )
{
fail( "missing action", context, file, line );
}
template< typename Context >
static void unexpected_call( const Context& context )
{
fail( "unexpected call", context );
}
template< typename Context >
static void sequence_failed( const Context& context,
const std::string& /*file*/, int /*line*/ )
{
fail( "sequence failed", context );
}
template< typename Context >
static void verification_failed( const Context& context,
const std::string& file, int line )
{
fail( "verification failed", context, file, line );
}
template< typename Context >
static void untriggered_expectation( const Context& context,
const std::string& file, int line )
{
fail( "untriggered expectation", context, file, line );
}
};
}
#endif // MOCK_ERROR_HPP_INCLUDED #endif // MOCK_ERROR_HPP_INCLUDED

View file

@ -10,7 +10,6 @@
#define MOCK_MOCK_HPP_INCLUDED #define MOCK_MOCK_HPP_INCLUDED
#include "config.hpp" #include "config.hpp"
#include "error.hpp"
#include "object.hpp" #include "object.hpp"
#include "function.hpp" #include "function.hpp"
#include "type_name.hpp" #include "type_name.hpp"

View file

@ -22,7 +22,7 @@
expected_call_count = 0; expected_call_count = 0;
#define CHECK_ERROR( expr, error, calls, context ) \ #define CHECK_ERROR( expr, error, calls, context ) \
BOOST_CHECK( verify() ); \ BOOST_CHECK( verify() ); \
expr; \ try { expr; } catch( ... ) {} \
BOOST_CHECK_EQUAL( 1, error##_count ); \ BOOST_CHECK_EQUAL( 1, error##_count ); \
CHECK_CALLS( calls ); \ CHECK_CALLS( calls ); \
BOOST_CHECK_EQUAL( context, last_context ); \ BOOST_CHECK_EQUAL( context, last_context ); \

View file

@ -6,13 +6,13 @@
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
// //
#include "silent_error.hpp" #include "mock_error.hpp"
#include <boost/test/auto_unit_test.hpp> #include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework #define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp> #include <boost/config/auto_link.hpp>
#define MOCK_ERROR_POLICY silent_error #define MOCK_ERROR_POLICY mock_error
#include <turtle/mock.hpp> #include <turtle/mock.hpp>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>

View file

@ -7,9 +7,9 @@
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
// //
#include "silent_error.hpp" #include "mock_error.hpp"
#define MOCK_ERROR_POLICY silent_error #define MOCK_ERROR_POLICY mock_error
#include <turtle/mock.hpp> #include <turtle/mock.hpp>
#include <boost/preprocessor/repetition/enum.hpp> #include <boost/preprocessor/repetition/enum.hpp>

View file

@ -31,8 +31,7 @@ namespace mock
{ {
static Result abort() static Result abort()
{ {
static BOOST_DEDUCED_TYPENAME boost::remove_reference< Result >::type r; throw std::runtime_error( "aborted" );
return r;
} }
template< typename Context > template< typename Context >
@ -43,57 +42,10 @@ namespace mock
++missing_action_count; ++missing_action_count;
} }
template< typename Context > template< typename Context >
static void expected_call( const Context& context, static void expected_call( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{ {
last_context = boost::lexical_cast< std::string >( context ); 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 std::string& /*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 std::string& /*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 std::string& /*file*/, int /*line*/ )
{
last_context = boost::lexical_cast< std::string >( context );
++untriggered_expectation_count;
}
};
template<>
struct mock_error< void >
{
static void abort()
{}
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;
}
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; ++expected_call_count;
} }
template< typename Context > template< typename Context >

View file

@ -6,7 +6,6 @@
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
// //
#define MOCK_USE_BOOST_TEST
#include <turtle/mock.hpp> #include <turtle/mock.hpp>
#include <boost/mpl/assert.hpp> #include <boost/mpl/assert.hpp>
#include <boost/bind.hpp> #include <boost/bind.hpp>

View file

@ -6,13 +6,13 @@
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
// //
#include "silent_error.hpp" #include "mock_error.hpp"
#include <boost/test/auto_unit_test.hpp> #include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework #define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp> #include <boost/config/auto_link.hpp>
#define MOCK_ERROR_POLICY silent_error #define MOCK_ERROR_POLICY mock_error
#include <turtle/object.hpp> #include <turtle/object.hpp>
#include <turtle/function.hpp> #include <turtle/function.hpp>

View file

@ -6,13 +6,13 @@
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
// //
#include "silent_error.hpp" #include "mock_error.hpp"
#include <boost/test/auto_unit_test.hpp> #include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework #define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp> #include <boost/config/auto_link.hpp>
#define MOCK_ERROR_POLICY silent_error #define MOCK_ERROR_POLICY mock_error
#include <turtle/mock.hpp> #include <turtle/mock.hpp>
BOOST_AUTO_TEST_CASE( registering_to_a_sequence_and_calling_out_of_order_throws ) BOOST_AUTO_TEST_CASE( registering_to_a_sequence_and_calling_out_of_order_throws )

View file

@ -1,50 +0,0 @@
//
// 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)
//
#ifndef MOCK_TEST_SILENT_ERROR_HPP_INCLUDED
#define MOCK_TEST_SILENT_ERROR_HPP_INCLUDED
#include <string>
#include <stdexcept>
namespace mock
{
template< typename Result >
struct silent_error
{
static Result abort()
{
throw std::runtime_error( "abort" );
}
template< typename Context >
static void expected_call( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ )
{}
template< typename Context >
static void unexpected_call( const Context& /*context*/ )
{}
template< typename Context >
static void missing_action( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ )
{}
template< typename Context >
static void sequence_failed( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ )
{}
template< typename Context >
static void verification_failed( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ )
{}
template< typename Context >
static void untriggered_expectation( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ )
{}
};
}
#endif // MOCK_TEST_SILENT_ERROR_HPP_INCLUDED