From 8d2fb9c94c13560f948159c2217a44dce6c3bfe2 Mon Sep 17 00:00:00 2001 From: mat007 Date: Fri, 3 May 2013 20:17:46 +0000 Subject: [PATCH] Added support for several sequences in 'in' git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@635 860be788-9bd5-4423-9f1e-828f051e677b --- build/boost/doc/changelog.qbk | 1 + build/boost/doc/example/getting_started.cpp | 4 ++-- build/boost/doc/reference.qbk | 19 ++++++++++++------- test/test_sequence.cpp | 2 +- turtle/config.hpp | 4 ++++ turtle/detail/expectation_template.hpp | 19 ++++++++++++++----- 6 files changed, 34 insertions(+), 15 deletions(-) diff --git a/build/boost/doc/changelog.qbk b/build/boost/doc/changelog.qbk index c820758..2978c5d 100644 --- a/build/boost/doc/changelog.qbk +++ b/build/boost/doc/changelog.qbk @@ -6,6 +6,7 @@ Not yet released * Added support for C++11 lambdas as constraints * Return actions now accept by copy types derived from abstract base types * Officially documented MOCK_UNARY_CONSTRAINT and MOCK_BINARY_CONSTRAINT +* Added support for several sequences in 'in' [endsect] diff --git a/build/boost/doc/example/getting_started.cpp b/build/boost/doc/example/getting_started.cpp index de78495..4285c2a 100644 --- a/build/boost/doc/example/getting_started.cpp +++ b/build/boost/doc/example/getting_started.cpp @@ -61,7 +61,7 @@ BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero ) calculator c( v ); mock::sequence s; MOCK_EXPECT( v.display ).once().with( 0 ).in( s ); // add this expectation to the sequence - MOCK_EXPECT( v.display ).with( 1 ).in( s ); // add this expectation to the sequence after the previous one + MOCK_EXPECT( v.display ).with( 1 ).in( s ); // add this expectation to the sequence after the previous call c.add( 0, 0 ); c.add( 1, 0 ); } @@ -78,7 +78,7 @@ BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero ) mock::sequence s1, s2; MOCK_EXPECT( v.display ).once().with( 0 ).in( s1 ); MOCK_EXPECT( v.display ).once().with( 1 ).in( s2 ); - MOCK_EXPECT( v.display ).with( 2 ).in( s1 ).in( s2 ); // add this expectation to both sequences after the previous ones + MOCK_EXPECT( v.display ).with( 2 ).in( s1, s2 ); // add this expectation to both sequences after the previous calls c.add( 0, 0 ); c.add( 1, 0 ); c.add( 1, 1 ); diff --git a/build/boost/doc/reference.qbk b/build/boost/doc/reference.qbk index 5d93ec4..4bc5556 100644 --- a/build/boost/doc/reference.qbk +++ b/build/boost/doc/reference.qbk @@ -393,7 +393,7 @@ An expectation is a statement of configuration for a mock object. Synopsis : - MOCK_EXPECT( identifier ).``[link turtle.reference.expectation.invocation invocation]``( arguments ).with( ``[link turtle.reference.expectation.constraints constraints]`` ).in( ``[link turtle.reference.expectation.sequence sequence]`` ).``[link turtle.reference.expectation.actions action]``( value ); + MOCK_EXPECT( identifier ).``[link turtle.reference.expectation.invocation invocation]``( arguments ).with( ``[link turtle.reference.expectation.constraints constraints]`` ).in( ``[link turtle.reference.expectation.sequence sequences]`` ).``[link turtle.reference.expectation.actions action]``( value ); [note The identifier refers to the one specified when [link turtle.reference.creation creating] a mock object] @@ -612,9 +612,9 @@ Example using &&, || and ! : Synopsis : - mock::sequence s; - MOCK_EXPECT( identifier_1 ).in( s ); - MOCK_EXPECT( identifier_2 ).in( s ); + mock::sequence s_1, s_2; + MOCK_EXPECT( identifier_1 ).in( s_1 ); + MOCK_EXPECT( identifier_2 ).in( s_1, s_2 ); Example : @@ -636,20 +636,25 @@ Example : MOCK_EXPECT( c_2.method_2 ).in( s ); } -Example of setting several sequences by chaining calls to ['in] : +Example of setting several sequences : MOCK_CLASS( mock_class ) { MOCK_METHOD( method, 0, void() ) }; - BOOST_AUTO_TEST_CASE( demonstrates_enforcing_several_expectations_orders ) + BOOST_AUTO_TEST_CASE( demonstrates_enforcing_several_expectation_orders ) { mock_class c; mock::sequence s_1, s_2; - MOCK_EXPECT( c.method ).in( s_1 ).in( s_2 ); + MOCK_EXPECT( c.method ).in( s_1, s_2 ); } +The maximum number of sequences that can be passed to ['in] is MOCK_MAX_SEQUENCES which defaults to 5. If needed the value can be increased before including the library : + + #define MOCK_MAX_SEQUENCES 7 + #include + [endsect] [section Actions] diff --git a/test/test_sequence.cpp b/test/test_sequence.cpp index f2534ee..7f7c4f3 100644 --- a/test/test_sequence.cpp +++ b/test/test_sequence.cpp @@ -94,7 +94,7 @@ BOOST_AUTO_TEST_CASE( an_expectation_can_be_used_in_several_sequences ) { mock::sequence s1, s2; mock::detail::function< void() > e; - e.expect().once().in( s1 ).in( s2 ); + e.expect().once().in( s1, s2 ); BOOST_CHECK_NO_THROW( e() ); BOOST_CHECK( e.verify() ); } diff --git a/turtle/config.hpp b/turtle/config.hpp index 0ac8e25..c46daf3 100644 --- a/turtle/config.hpp +++ b/turtle/config.hpp @@ -24,6 +24,10 @@ # define MOCK_USE_BOOST_PHOENIX #endif +#ifndef MOCK_MAX_SEQUENCES +# define MOCK_MAX_SEQUENCES 10 +#endif + #ifndef BOOST_FUNCTION_MAX_ARGS # define BOOST_FUNCTION_MAX_ARGS MOCK_MAX_ARGS #elif BOOST_PP_LESS(BOOST_FUNCTION_MAX_ARGS, MOCK_MAX_ARGS) diff --git a/turtle/detail/expectation_template.hpp b/turtle/detail/expectation_template.hpp index 3d6f0bb..d18551c 100644 --- a/turtle/detail/expectation_template.hpp +++ b/turtle/detail/expectation_template.hpp @@ -30,6 +30,15 @@ #define MOCK_EXPECTATION_SERIALIZE(z, n, d) \ BOOST_PP_IF(n, << ", " <<,) *m.c##n##_ +#define MOCK_EXPECTATION_IN_ADD(z, n, d ) \ + add( s##n.impl_ ); +#define MOCK_EXPECTATION_IN(z, n, d) \ + expectation& in( BOOST_PP_ENUM_PARAMS(n, sequence& s) ) \ + { \ + BOOST_PP_REPEAT(n, MOCK_EXPECTATION_IN_ADD, _ ) \ + return *this; \ + } + namespace mock { namespace detail @@ -68,11 +77,9 @@ namespace detail MOCK_EXPECTATION_IS_VALID, BOOST_PP_EMPTY); } - expectation& in( sequence& s ) - { - add( s.impl_ ); - return *this; - } + BOOST_PP_REPEAT(MOCK_MAX_SEQUENCES, + MOCK_EXPECTATION_IN, BOOST_PP_EMPTY) + expectation& once() { i_ = boost::make_shared< detail::once >(); @@ -131,3 +138,5 @@ namespace detail #undef MOCK_EXPECTATION_ARGS #undef MOCK_EXPECTATION_IS_VALID #undef MOCK_EXPECTATION_SERIALIZE +#undef MOCK_EXPECTATION_IN +#undef MOCK_EXPECTATION_IN_ADD