mirror of
https://github.com/mat007/turtle.git
synced 2026-06-22 12:13:43 +00:00
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:
parent
4d75d42be5
commit
176611c735
10 changed files with 144 additions and 7 deletions
|
|
@ -148,7 +148,7 @@
|
|||
<ClInclude Include="..\..\src\libraries\turtle\format.hpp" />
|
||||
<ClInclude Include="..\..\src\libraries\turtle\function.hpp" />
|
||||
<ClInclude Include="..\..\src\libraries\turtle\functional.hpp" />
|
||||
<ClInclude Include="..\..\src\libraries\turtle\hippomocks.h" />
|
||||
<ClInclude Include="..\..\src\libraries\turtle\has_operator.hpp" />
|
||||
<ClInclude Include="..\..\src\libraries\turtle\invocation.hpp" />
|
||||
<ClInclude Include="..\..\src\libraries\turtle\is_functor.hpp" />
|
||||
<ClInclude Include="..\..\src\libraries\turtle\mock.hpp" />
|
||||
|
|
|
|||
|
|
@ -37,9 +37,6 @@
|
|||
<ClInclude Include="..\..\src\libraries\turtle\functional.hpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\libraries\turtle\hippomocks.h">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\libraries\turtle\invocation.hpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
|
|
@ -73,5 +70,8 @@
|
|||
<ClInclude Include="..\..\src\libraries\turtle\verifiable.hpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\libraries\turtle\has_operator.hpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
@ -199,6 +199,7 @@
|
|||
<ClCompile Include="..\..\src\tests\turtle_test\error_test.cpp" />
|
||||
<ClCompile Include="..\..\src\tests\turtle_test\format_test.cpp" />
|
||||
<ClCompile Include="..\..\src\tests\turtle_test\function_test.cpp" />
|
||||
<ClCompile Include="..\..\src\tests\turtle_test\has_operator_test.cpp" />
|
||||
<ClCompile Include="..\..\src\tests\turtle_test\integration_test.cpp" />
|
||||
<ClCompile Include="..\..\src\tests\turtle_test\invocation_test.cpp" />
|
||||
<ClCompile Include="..\..\src\tests\turtle_test\is_functor_test.cpp" />
|
||||
|
|
|
|||
|
|
@ -40,6 +40,9 @@
|
|||
<ClCompile Include="..\..\src\tests\turtle_test\type_name_test.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\tests\turtle_test\has_operator_test.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\src\tests\turtle_test\mock_error.hpp">
|
||||
|
|
|
|||
|
|
@ -192,6 +192,10 @@
|
|||
RelativePath="..\..\src\libraries\turtle\functional.hpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\src\libraries\turtle\has_operator.hpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\src\libraries\turtle\invocation.hpp"
|
||||
>
|
||||
|
|
|
|||
|
|
@ -206,6 +206,10 @@
|
|||
RelativePath="..\..\src\tests\turtle_test\function_test.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\src\tests\turtle_test\has_operator_test.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\src\tests\turtle_test\integration_test.cpp"
|
||||
>
|
||||
|
|
|
|||
|
|
@ -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 ) )
|
||||
{
|
||||
|
|
|
|||
34
src/libraries/turtle/has_operator.hpp
Normal file
34
src/libraries/turtle/has_operator.hpp
Normal 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
|
||||
35
src/tests/turtle_test/has_operator_test.cpp
Normal file
35
src/tests/turtle_test/has_operator_test.cpp
Normal 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 ));
|
||||
}
|
||||
|
|
@ -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 );
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue