From c8072405c8c4532114e24360b6bd0d512d1a3866 Mon Sep 17 00:00:00 2001 From: mat007 Date: Mon, 20 May 2013 10:49:03 +0000 Subject: [PATCH] Added MOCK_TERNARY_CONSTRAINT helper macro git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@658 860be788-9bd5-4423-9f1e-828f051e677b --- build/boost/doc/example/reference.cpp | 7 ++++ build/boost/doc/reference.qbk | 16 +++++++++ turtle/constraint.hpp | 49 ++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/build/boost/doc/example/reference.cpp b/build/boost/doc/example/reference.cpp index edb3138..b59ded4 100644 --- a/build/boost/doc/example/reference.cpp +++ b/build/boost/doc/example/reference.cpp @@ -771,3 +771,10 @@ MOCK_BINARY_CONSTRAINT( equal, actual == expected ) // this is MOCK_BINARY_CONSTRAINT( near, std::abs( actual - expected ) < 0.01 ) // this defines a 'near' constraint which can be used as 'near( 42 )' //] } + +namespace helpers_example_3 +{ +//[ helpers_example_3 +MOCK_TERNARY_CONSTRAINT( near, std::abs( actual - expected ) < arg ) // this defines a 'near' constraint which can be used as 'near( 42, 0.01 )' +//] +} diff --git a/build/boost/doc/reference.qbk b/build/boost/doc/reference.qbk index 0cd7281..5a51372 100644 --- a/build/boost/doc/reference.qbk +++ b/build/boost/doc/reference.qbk @@ -518,6 +518,22 @@ Example : [endsect] +[section MOCK_TERNARY_CONSTRAINT] + +Synopsis : + + MOCK_TERNARY_CONSTRAINT( name, expression ) // defines a constraint 'name' based on the given 'expression' + +The expression manipulates the received parameter 'actual' as well as the passed arguments 'expected' and 'arg' in order to implement the constraint. + +[note The types of expected and arg must be copy-constructible and assignable.] + +Example : + +[helpers_example_3] + +[endsect] + [endsect] [endsect] diff --git a/turtle/constraint.hpp b/turtle/constraint.hpp index fd45bb4..bf06548 100644 --- a/turtle/constraint.hpp +++ b/turtle/constraint.hpp @@ -119,7 +119,7 @@ namespace detail } } // mock -#define MOCK_UNARY_CONSTRAINT(N,Expr) \ +#define MOCK_UNARY_CONSTRAINT(N, Expr) \ namespace detail \ { \ struct N \ @@ -137,7 +137,7 @@ namespace detail } \ const mock::constraint< detail::N > N; -#define MOCK_BINARY_CONSTRAINT(N,Expr) \ +#define MOCK_BINARY_CONSTRAINT(N, Expr) \ namespace detail \ { \ template< typename Expected > \ @@ -164,10 +164,49 @@ namespace detail Expected expected_; \ }; \ } \ - template< typename T > \ - mock::constraint< detail::N< T > > N( T t ) \ + template< typename Expected > \ + mock::constraint< detail::N< Expected > > N( Expected expected ) \ { \ - return detail::N< T >( t ); \ + return detail::N< Expected >( expected ); \ + } + +#define MOCK_TERNARY_CONSTRAINT(N, Expr) \ + namespace detail \ + { \ + template< typename Expected, typename Arg > \ + struct N \ + { \ + explicit N( const Expected& expected, const Arg& arg ) \ + : expected_( expected ) \ + , arg_( arg ) \ + {} \ + template< typename Actual > \ + bool operator()( const Actual& actual ) const \ + { \ + return test( actual, boost::unwrap_ref( expected_ ), \ + boost::unwrap_ref( arg_ ) ); \ + } \ + template< typename Actual, typename T1, typename T2 > \ + bool test( const Actual& actual, \ + const T1& expected, const T2& arg ) const \ + { \ + return Expr; \ + } \ + friend std::ostream& operator<<( std::ostream& s, const N& n ) \ + { \ + return s << BOOST_STRINGIZE(N) \ + << "( " << mock::format( n.expected_ ) \ + << ", " << mock::format( n.arg_ ) << " )"; \ + } \ + Expected expected_; \ + Arg arg_; \ + }; \ + } \ + template< typename Expected, typename Arg > \ + mock::constraint< detail::N< Expected, Arg > > N( \ + Expected expected, Arg arg ) \ + { \ + return detail::N< Expected, Arg >( expected, arg ); \ } #endif // MOCK_CONSTRAINT_HPP_INCLUDED