From a2d36e961a2f77adf70cc8f4bb3cae067417aafe Mon Sep 17 00:00:00 2001 From: Mathieu Champlon Date: Tue, 16 Jan 2018 19:29:31 +0100 Subject: [PATCH] Fixed move-only type support in constraints --- doc/changelog.qbk | 1 + include/turtle/constraint.hpp | 15 ++++++++++----- test/test_constraints.cpp | 34 +++++++++++++++++++++++++--------- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/doc/changelog.qbk b/doc/changelog.qbk index e76e4ab..36a82f6 100644 --- a/doc/changelog.qbk +++ b/doc/changelog.qbk @@ -14,6 +14,7 @@ Not yet released * Added MOCK_NO_AUTO_PTR to deactivate std::auto_ptr support * Added [@https://github.com/philsquared/Catch Catch] integration * Fixed move-only type argument in actions +* Fixed move-only type support in constraints [endsect] diff --git a/include/turtle/constraint.hpp b/include/turtle/constraint.hpp index 32329c2..6654b71 100644 --- a/include/turtle/constraint.hpp +++ b/include/turtle/constraint.hpp @@ -19,6 +19,7 @@ #include #include #include +#include namespace mock { @@ -158,11 +159,14 @@ namespace detail #define MOCK_CONSTRAINT_CREF_PARAM(z, n, Args) \ typename \ - boost::unwrap_reference< Expected_##n >::type \ + const boost::unwrap_reference< Expected_##n >::type& \ BOOST_PP_ARRAY_ELEM(n, Args) +#define MOCK_CONSTRAINT_ARG(z, n, Args) \ + BOOST_FWD_REF(T##n) BOOST_PP_ARRAY_ELEM(n, Args) + #define MOCK_CONSTRAINT_PARAM(z, n, Args) \ - T##n BOOST_PP_ARRAY_ELEM(n, Args) + boost::forward< T##n >( BOOST_PP_ARRAY_ELEM(n, Args) ) #define MOCK_NARY_CONSTRAINT(Name, n, Args, Expr) \ namespace detail \ @@ -171,7 +175,7 @@ namespace detail struct Name \ { \ explicit Name( \ - BOOST_PP_ENUM_BINARY_PARAMS(n, const Expected_, & e) ) \ + BOOST_PP_ENUM_BINARY_PARAMS(n, Expected_, e) ) \ : BOOST_PP_ENUM(n, MOCK_CONSTRAINT_ASSIGN, _) \ {} \ template< typename Actual > \ @@ -199,9 +203,10 @@ namespace detail template< BOOST_PP_ENUM_PARAMS(n, typename T) > \ mock::constraint< \ detail::Name< BOOST_PP_ENUM_PARAMS(n, T) > \ - > Name( BOOST_PP_ENUM(n, MOCK_CONSTRAINT_PARAM, (n, Args)) ) \ + > Name( BOOST_PP_ENUM(n, MOCK_CONSTRAINT_ARG, (n, Args)) ) \ { \ - return detail::Name< BOOST_PP_ENUM_PARAMS(n, T) > Args; \ + return detail::Name< BOOST_PP_ENUM_PARAMS(n, T) >( \ + BOOST_PP_ENUM(n, MOCK_CONSTRAINT_PARAM, (n, Args)) ); \ } #define MOCK_CONSTRAINT_EXT(Name, n, Args, Expr) \ diff --git a/test/test_constraints.cpp b/test/test_constraints.cpp index 60e3128..7fea74f 100644 --- a/test/test_constraints.cpp +++ b/test/test_constraints.cpp @@ -47,14 +47,20 @@ BOOST_AUTO_TEST_CASE( equal_constraint ) BOOST_CHECK( ! mock::equal( std::string( "string" ) ).c_( "not string" ) ); { std::string s; - mock::constraint< - mock::detail::equal< - boost::reference_wrapper< const std::string > - > - > c = mock::equal( boost::cref( s ) ); + auto c = mock::equal( boost::cref( s ) ); s = "string"; BOOST_CHECK( c.c_( "string" ) ); } +#ifdef MOCK_SMART_PTR + { + std::unique_ptr< int > i; + std::unique_ptr< int > j( new int( 3 ) ); + BOOST_CHECK( ! mock::equal( i ).c_( j ) ); + BOOST_CHECK( ! mock::equal( j ).c_( i ) ); + BOOST_CHECK( mock::equal( i ).c_( i ) ); + BOOST_CHECK( mock::equal( j ).c_( j ) ); + } +#endif } BOOST_AUTO_TEST_CASE( same_constraint ) @@ -244,10 +250,20 @@ BOOST_AUTO_TEST_CASE( retrieve_constraint_uses_assignment_operator ) BOOST_AUTO_TEST_CASE( affirm_constraint ) { - int* i = 0; - int j; - BOOST_CHECK( ! mock::affirm.c_( i ) ); - BOOST_CHECK( mock::affirm.c_( &j ) ); + { + int* i = 0; + int j; + BOOST_CHECK( ! mock::affirm.c_( i ) ); + BOOST_CHECK( mock::affirm.c_( &j ) ); + } + { +#ifdef MOCK_SMART_PTR + std::unique_ptr< int > i; + std::unique_ptr< int > j( new int( 3 ) ); + BOOST_CHECK( ! mock::affirm.c_( i ) ); + BOOST_CHECK( mock::affirm.c_( j ) ); +#endif + } } BOOST_AUTO_TEST_CASE( negate_constraint )