Enhanced custom constraints support

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@178 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2011-01-10 14:22:32 +00:00
parent 4d75d42be5
commit 176611c735
10 changed files with 144 additions and 7 deletions

View file

@ -11,6 +11,7 @@
#include "placeholder.hpp"
#include "is_functor.hpp"
#include "has_operator.hpp"
#include "constraints.hpp"
#include "format.hpp"
#include <boost/function.hpp>
@ -67,9 +68,12 @@ namespace detail
template< typename Functor >
explicit check( const Functor& f,
BOOST_DEDUCED_TYPENAME boost::enable_if<
BOOST_DEDUCED_TYPENAME detail::is_functor< Functor >
BOOST_DEDUCED_TYPENAME boost::mpl::or_<
BOOST_DEDUCED_TYPENAME detail::is_functor< Functor >,
BOOST_DEDUCED_TYPENAME detail::has_operator< Functor, Actual > // $$$$ MAT : add a has_const_operator too ?
>
>::type* = 0 )
: desc_( "?" )
: desc_( mock::format( f ) )
{
BOOST_CONCEPT_ASSERT(( FunctorCompatible< Functor, Actual > ));
f_ = f;
@ -79,7 +83,10 @@ namespace detail
template< typename Expected >
explicit check( const Expected& expected,
BOOST_DEDUCED_TYPENAME boost::disable_if<
BOOST_DEDUCED_TYPENAME detail::is_functor< Expected >
BOOST_DEDUCED_TYPENAME boost::mpl::or_<
BOOST_DEDUCED_TYPENAME detail::is_functor< Expected >,
BOOST_DEDUCED_TYPENAME detail::has_operator< Expected, Actual >
>
>::type* = 0 )
: desc_( mock::format( expected ) )
{

View file

@ -0,0 +1,34 @@
//
// 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_HAS_OPERATOR_HPP_INCLUDED
#define MOCK_HAS_OPERATOR_HPP_INCLUDED
#include <boost/function_types/is_callable_builtin.hpp>
#include <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/detail/yes_no_type.hpp>
namespace mock
{
namespace detail
{
typedef boost::type_traits::yes_type yes_type;
typedef boost::type_traits::no_type no_type;
template< typename T, typename Actual, bool(T::*)( Actual ) const > struct has_operator_helper_class {};
template< typename T, typename Actual > yes_type& has_operator_helper( has_operator_helper_class< T, Actual, &T::operator() >* );
template< typename T, typename Actual > no_type& has_operator_helper( ... );
template< typename T, typename Actual > struct has_operator
: boost::mpl::bool_< sizeof( has_operator_helper< T, Actual >( 0 ) ) == sizeof( yes_type ) >
{};
}
}
#endif // #ifndef MOCK_HAS_OPERATOR_HPP_INCLUDED

View file

@ -0,0 +1,35 @@
//
// 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)
//
#include <turtle/has_operator.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
namespace
{
struct functor_with_valid_operator
{
bool operator()( float ) const
{
return true;
}
};
BOOST_STATIC_ASSERT(( mock::detail::has_operator< functor_with_valid_operator, float >::type::value ));
struct functor_with_valid_template_operator
{
template< typename T >
bool operator()( T ) const
{
return true;
}
};
BOOST_STATIC_ASSERT(( mock::detail::has_operator< functor_with_valid_template_operator, float >::type::value ));
}

View file

@ -310,3 +310,52 @@ BOOST_AUTO_TEST_CASE( boost_optional_on_base_class_reference_as_return_type )
MOCK_EXPECT( b, method ).once().returns( boost::ref( o ) );
b.method();
}
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 - expected_ ) < threshold_;
}
friend std::ostream& operator<<( std::ostream& os, const near_constraint& c )
{
return os << "std::abs( _ - " << c.expected_ << " ) < " << c.threshold_;
}
//template< typename Actual >
//void explain( std::ostream& os, Actual actual ) const
//{
// os << std::abs( actual - expected_ ) << " >= " << threshold_;
//}
Expected expected_;
Expected threshold_;
};
template< typename Expected >
near_constraint< Expected > near( Expected expected, Expected threshold )
{
return near_constraint< Expected >( expected, threshold );
}
MOCK_CLASS( custom_constraint_mock )
{
MOCK_METHOD_EXT( method, 1, void( float ), method )
};
}
BOOST_AUTO_TEST_CASE( writing_custom_constraint )
{
custom_constraint_mock m;
MOCK_EXPECT( m, method ).with( near( 3.f, 0.01f ) );
m.method( 3.f );
}