diff --git a/src/libraries/turtle/check.hpp b/src/libraries/turtle/check.hpp index 6315330..0314036 100644 --- a/src/libraries/turtle/check.hpp +++ b/src/libraries/turtle/check.hpp @@ -13,8 +13,11 @@ #include "constraints.hpp" #include "operators.hpp" #include "format.hpp" +#include #include #include +#include +#include namespace mock { @@ -27,8 +30,8 @@ namespace detail BOOST_CONCEPT_USAGE( FunctorCompatible ) { boost::require_boolean_expr( - // if an error is generated by the line below it means - // an argument passed to 'with' was of the wrong type. + // if an error is generated by the line below it means an argument + // passed to 'with' was of the wrong type. functor_accepts( actual_argument_type ) ); } @@ -45,8 +48,8 @@ namespace detail BOOST_CONCEPT_USAGE( EqualityComparable ) { boost::require_boolean_expr( - // if an error is generated by the line below it means - // an argument passed to 'with' was of the wrong type. + // if an error is generated by the line below it means an argument + // passed to 'with' was of the wrong type. actual_argument_type == expected_argument_type ); } @@ -57,91 +60,56 @@ namespace detail }; template< typename Actual > - class check_base : boost::noncopyable + class check { public: - virtual ~check_base() {} - - virtual bool operator()( Actual ) const = 0; - - friend std::ostream& operator<<( std::ostream& s, const check_base& c ) - { - c.format( s ); - return s; - } - private: - virtual void format( std::ostream& ) const = 0; - }; - - template< typename Actual, typename Expected, typename Enable = void > - class check : public check_base< Actual > - { - public: - explicit check( Expected expected ) - : expected_( expected ) - { - BOOST_CONCEPT_ASSERT(( EqualityComparable< Expected, Actual > )); - } - private: - virtual bool operator()( Actual actual ) const - { - return actual == expected_; - } - virtual void format( std::ostream& s ) const - { - s << mock::format( expected_ ); - } - private: - Expected expected_; - }; - - template< typename Actual, typename Constraint > - class check< Actual, mock::constraint< Constraint > > - : public check_base< Actual > - { - public: - explicit check( const constraint< Constraint >& c ) - : c_( c.f_ ) - { - BOOST_CONCEPT_ASSERT(( FunctorCompatible< Constraint, Actual > )); - } - private: - virtual bool operator()( Actual actual ) const - { - return c_( actual ); - } - virtual void format( std::ostream& s ) const - { - s << mock::format( c_ ); - } - private: - Constraint c_; - }; - - template< typename Actual, typename Functor > - class check< Actual, Functor, - BOOST_DEDUCED_TYPENAME boost::enable_if< - detail::is_functor< Functor > - >::type - > : public check_base< Actual > - { - public: - explicit check( const Functor& f ) - : f_( f ) + template< typename Functor > + explicit check( const Functor& f, + BOOST_DEDUCED_TYPENAME boost::enable_if< + BOOST_DEDUCED_TYPENAME detail::is_functor< Functor > + >::type* = 0 ) + : desc_( mock::format( f ) ) { BOOST_CONCEPT_ASSERT(( FunctorCompatible< Functor, Actual > )); + f_ = f; + if( ! f_ ) + std::invalid_argument( "invalid constraint" ); } - private: - virtual bool operator()( Actual actual ) const + template< typename Expected > + explicit check( const Expected& expected, + BOOST_DEDUCED_TYPENAME boost::disable_if< + BOOST_DEDUCED_TYPENAME detail::is_functor< Expected > + >::type* = 0 ) + : desc_( mock::format( expected ) ) + { + BOOST_CONCEPT_ASSERT(( EqualityComparable< Expected, Actual > )); + f_ = mock::equal( expected ).f_; + if( ! f_ ) + std::invalid_argument( "invalid constraint" ); + } + template< typename Functor > + explicit check( const constraint< Functor >& ph ) + : desc_( mock::format( ph.f_ ) ) + { + BOOST_CONCEPT_ASSERT(( FunctorCompatible< Functor, Actual > )); + f_ = ph.f_; + if( ! f_ ) + std::invalid_argument( "invalid constraint" ); + } + + bool operator()( Actual actual ) const { return f_( actual ); } - virtual void format( std::ostream& s ) const + + friend std::ostream& operator<<( std::ostream& s, const check& c ) { - s << mock::format( f_ ); + return s << c.desc_; } + private: - Functor f_; + boost::function< bool( Actual ) > f_; + std::string desc_; }; } } diff --git a/src/libraries/turtle/error.hpp b/src/libraries/turtle/error.hpp index 7c9c4fa..8f145fa 100644 --- a/src/libraries/turtle/error.hpp +++ b/src/libraries/turtle/error.hpp @@ -37,27 +37,27 @@ namespace mock throw boost::enable_current_exception( exception() ); } - template< typename Context > static void fail( - const std::string& message, const Context& context, + const std::string& message, const std::string& context, const std::string& file = "unknown location", int line = 0 ) { boost::unit_test::framework::assertion_result( false ); boost::unit_test::unit_test_log << boost::unit_test::log::begin( file, (std::size_t)line ) << boost::unit_test::log_all_errors + << boost::unit_test::lazy_ostream::instance() << message << ": " << context << boost::unit_test::log::end(); } - template< typename Context > - static void expected_call( const Context& context, + static void expected_call( const std::string& context, const std::string& file, int line ) { boost::unit_test::framework::assertion_result( true ); boost::unit_test::unit_test_log << boost::unit_test::log::begin( file, (std::size_t)line ) << boost::unit_test::log_successful_tests + << boost::unit_test::lazy_ostream::instance() << "mock expectation fulfilled: " << context << boost::unit_test::log::end(); } @@ -75,47 +75,40 @@ namespace mock throw exception(); } - template< typename Context > static void fail( - const std::string& message, const Context& context, + const std::string& message, const std::string& context, const std::string& file = "unknown location", int line = 0 ) { std::cerr << file << '(' << line << "): " << message << ": " << context << std::endl; } - template< typename Context > - static void expected_call( const Context& /*context*/, + static void expected_call( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) {} #endif // MOCK_USE_BOOST_TEST - template< typename Context > - static void missing_action( const Context& context, + static void missing_action( const std::string& context, const std::string& file, int line ) { fail( "missing action", context, file, line ); } - template< typename Context > - static void unexpected_call( const Context& context ) + static void unexpected_call( const std::string& context ) { fail( "unexpected call", context ); } - template< typename Context > - static void sequence_failed( const Context& context, + static void sequence_failed( const std::string& context, const std::string& /*file*/, int /*line*/ ) { fail( "sequence failed", context ); } - template< typename Context > - static void verification_failed( const Context& context, + static void verification_failed( const std::string& context, const std::string& file, int line ) { fail( "verification failed", context, file, line ); } - template< typename Context > - static void untriggered_expectation( const Context& context, + static void untriggered_expectation( const std::string& context, const std::string& file, int line ) { fail( "untriggered expectation", context, file, line ); diff --git a/src/libraries/turtle/expectation.hpp b/src/libraries/turtle/expectation.hpp index 96afbae..4c2aa62 100644 --- a/src/libraries/turtle/expectation.hpp +++ b/src/libraries/turtle/expectation.hpp @@ -172,13 +172,14 @@ namespace detail BOOST_DEDUCED_TYPENAME \ boost::function_types::parameter_types< Signature >, \ n \ - >::type arg##n##_type; -#define MOCK_EXPECTATION_CONSTRUCTOR(z, n, d) BOOST_PP_COMMA_IF(n) c##n##_( new detail::check< arg##n##_type, constraint< detail::any > >( mock::any ) ) -#define MOCK_EXPECTATION_WITH(z, n, d) c##n##_.reset( new detail::check< arg##n##_type, Constraint##n >( c##n ) ); -#define MOCK_EXPECTATION_MEMBER(z, n, d) boost::shared_ptr< detail::check_base< arg##n##_type > > c##n##_; + >::type arg##n##_type; \ + typedef detail::check< arg##n##_type > constraint##n##_type; +#define MOCK_EXPECTATION_CONSTRUCTOR(z, n, d) BOOST_PP_COMMA_IF(n) c##n##_( mock::any ) +#define MOCK_EXPECTATION_WITH(z, n, d) c##n##_ = constraint##n##_type( c##n ); +#define MOCK_EXPECTATION_MEMBER(z, n, d) constraint##n##_type c##n##_; #define MOCK_EXPECTATION_ARGS(z, n, d) BOOST_PP_COMMA_IF(n) arg##n##_type a##n -#define MOCK_EXPECTATION_IS_VALID(z, n, d) && (*c##n##_)( a##n ) -#define MOCK_EXPECTATION_SERIALIZE(z, n, d) BOOST_PP_IF(n, << ", " <<,) *m.c##n##_ +#define MOCK_EXPECTATION_IS_VALID(z, n, d) && c##n##_( a##n ) +#define MOCK_EXPECTATION_SERIALIZE(z, n, d) BOOST_PP_IF(n, << ", " <<,) m.c##n##_ #define MOCK_EXPECTATION(z, n, d) \ template< typename Signature > \ class expectation< Signature, n > \ @@ -192,7 +193,7 @@ namespace detail : BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_CONSTRUCTOR, BOOST_PP_EMPTY) \ {} \ template< BOOST_PP_ENUM_PARAMS(n, typename Constraint) > \ - expectation& with( BOOST_PP_ENUM_BINARY_PARAMS(n, Constraint, c) ) \ + expectation& with( BOOST_PP_ENUM_BINARY_PARAMS(n, const Constraint, & c) ) \ { \ BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_WITH, BOOST_PP_EMPTY) \ return *this; \ diff --git a/src/libraries/turtle/format.hpp b/src/libraries/turtle/format.hpp index 747b624..3c6eb75 100644 --- a/src/libraries/turtle/format.hpp +++ b/src/libraries/turtle/format.hpp @@ -9,214 +9,107 @@ #ifndef MOCK_FORMAT_HPP_INCLUDED #define MOCK_FORMAT_HPP_INCLUDED -#include -#include +#include #include -#include +#include +#ifndef MOCK_NO_STL_FORMAT #include +#endif // MOCK_NO_STL_FORMAT +#include #include -#include namespace mock { - struct stream : boost::noncopyable - { - stream( std::ostream& s ) - : s_( s ) - { - s << std::boolalpha; - } - -#define MOCK_STREAM_OPERATOR(Type) \ - stream& operator<<( Type val ) \ - { \ - s_ << val; \ - return *this; \ - } - MOCK_STREAM_OPERATOR( bool ) - MOCK_STREAM_OPERATOR( short ) - MOCK_STREAM_OPERATOR( unsigned short ) - MOCK_STREAM_OPERATOR( int ) - MOCK_STREAM_OPERATOR( unsigned int ) - MOCK_STREAM_OPERATOR( long ) - MOCK_STREAM_OPERATOR( unsigned long ) - MOCK_STREAM_OPERATOR( float ) - MOCK_STREAM_OPERATOR( double ) - MOCK_STREAM_OPERATOR( long double ) - MOCK_STREAM_OPERATOR( char ) - MOCK_STREAM_OPERATOR( unsigned char ) -#undef MOCK_STREAM_OPERATOR - - std::ostream& s_; - }; - namespace detail { -namespace protect -{ - template< typename S, typename T > - char operator<<( S&, const T& ); - - template< typename S, typename T > - struct is_serializable_impl + template< typename T > + std::string serialize( const T& t, + BOOST_DEDUCED_TYPENAME boost::enable_if< + BOOST_DEDUCED_TYPENAME boost::is_output_streamable< T > >::type* = 0 ) { - BOOST_STATIC_ASSERT( sizeof( S& ) != sizeof( char ) ); - static S* s; - static T* t; - enum { value = sizeof( *s << *t ) == sizeof( S& ) }; - }; - - template< typename S, typename T > - struct is_serializable : boost::integral_constant< bool, is_serializable_impl< S, T >::value > - {}; + std::stringstream s; + s << std::boolalpha << t; + return s.str(); + } + template< typename T > + std::string serialize( const T&, + BOOST_DEDUCED_TYPENAME boost::disable_if< + BOOST_DEDUCED_TYPENAME boost::is_output_streamable< T > >::type* = 0 ) + { + return "?"; + } } template< typename T > - void serialize( std::ostream& s, const T& t, - BOOST_DEDUCED_TYPENAME boost::enable_if< - BOOST_DEDUCED_TYPENAME protect::is_serializable< std::ostream, T > >::type* = 0 ) + std::string format( const T& t ) { - s << t; - } - template< typename T > - void serialize( std::ostream& s, const T&, - BOOST_DEDUCED_TYPENAME boost::disable_if< - BOOST_DEDUCED_TYPENAME protect::is_serializable< std::ostream, T > >::type* = 0 ) - { - s << "?"; + return detail::serialize( t ); } - template< typename T > - void serialize( mock::stream& s, const T& t, - BOOST_DEDUCED_TYPENAME boost::enable_if< - BOOST_DEDUCED_TYPENAME protect::is_serializable< mock::stream, T > >::type* = 0 ) + inline std::string format( const char* s ) { - s << t; - } - template< typename T > - void serialize( mock::stream& s, const T& t, - BOOST_DEDUCED_TYPENAME boost::disable_if< - BOOST_DEDUCED_TYPENAME protect::is_serializable< mock::stream, T > >::type* = 0 ) - { - detail::serialize( s.s_, t ); + return '"' + std::string( s ) + '"'; } - template< typename T > - struct formatter +#ifndef MOCK_NO_STL_FORMAT + inline std::string format( const std::string& s ) { - explicit formatter( const T& t ) - : t_( &t ) - {} - friend std::ostream& operator<<( std::ostream& s, const formatter& t ) - { - mock::stream ms( s ); - detail::serialize( ms, *t.t_ ); - return s; - } - friend mock::stream& operator<<( mock::stream& s, const formatter& t ) - { - detail::serialize( s, *t.t_ ); - return s; - } - const T* t_; - }; - template<> - struct formatter< std::string > - { - explicit formatter( const std::string& s ) - : s_( &s ) - {} - friend std::ostream& operator<<( std::ostream& s, const formatter& t ) - { - return s << '"' << *t.s_ << '"'; - } - friend mock::stream& operator<<( mock::stream& s, const formatter& t ) - { - s.s_ << '"' << *t.s_ << '"'; - return s; - } - const std::string* s_; - }; - template<> - struct formatter< const char* > - { - explicit formatter( const char* s ) - : s_( s ) - {} - friend std::ostream& operator<<( std::ostream& s, const formatter& t ) - { - return s << '"' << t.s_ << '"'; - } - friend mock::stream& operator<<( mock::stream& s, const formatter& t ) - { - s.s_ << '"' << t.s_ << '"'; - return s; - } - const char* s_; - }; -} - - template< typename T > - detail::formatter< T > format( const T& t ) - { - return detail::formatter< T >( t ); + return '"' + s + '"'; } - - template< typename T > - mock::stream& serialize( mock::stream& s, const T& begin, const T& end ) + template< typename T1, typename T2 > + inline std::string format( const std::pair< T1, T2 >& p ) { + return '(' + mock::format( p.first ) + ',' + mock::format( p.second ) + ')'; + } + template< typename T > + std::string format( const T& begin, const T& end ) + { + std::stringstream s; s << '('; for( T it = begin; it != end; ++it ) - { - if( it != begin ) - s << ','; - s << mock::format( *it ); - } - return s << ')'; - } - - template< typename T1, typename T2 > - mock::stream& operator<<( mock::stream& s, const std::pair< T1, T2 >& p ) - { - return s << '(' << mock::format( p.first ) << ',' << mock::format( p.second ) << ')'; + s << (it == begin ? "" : ",") << mock::format( *it ); + s << ')'; + return s.str(); } template< typename T, typename A > - mock::stream& operator<<( mock::stream& s, const std::deque< T, A >& c ) + std::string format( const std::deque< T, A >& d ) { - return mock::serialize( s, c.begin(), c.end() ); + return format( d.begin(), d.end() ); } template< typename T, typename A > - mock::stream& operator<<( mock::stream& s, const std::list< T, A >& c ) + std::string format( const std::list< T, A >& l ) { - return mock::serialize( s, c.begin(), c.end() ); + return format( l.begin(), l.end() ); } template< typename T, typename A > - mock::stream& operator<<( mock::stream& s, const std::vector< T, A >& c ) + std::string format( const std::vector< T, A >& v ) { - return mock::serialize( s, c.begin(), c.end() ); + return format( v.begin(), v.end() ); } template< typename K, typename T, typename C, typename A > - mock::stream& operator<<( mock::stream& s, const std::map< K, T, C, A >& c ) + std::string format( const std::map< K, T, C, A >& m ) { - return mock::serialize( s, c.begin(), c.end() ); + return format( m.begin(), m.end() ); } template< typename K, typename T, typename C, typename A > - mock::stream& operator<<( mock::stream& s, const std::multimap< K, T, C, A >& c ) + std::string format( const std::multimap< K, T, C, A >& m ) { - return mock::serialize( s, c.begin(), c.end() ); + return format( m.begin(), m.end() ); } template< typename T, typename C, typename A > - mock::stream& operator<<( mock::stream& s, const std::set< T, C, A >& c ) + std::string format( const std::set< T, C, A >& s ) { - return mock::serialize( s, c.begin(), c.end() ); + return format( s.begin(), s.end() ); } template< typename T, typename C, typename A > - mock::stream& operator<<( mock::stream& s, const std::multiset< T, C, A >& c ) + std::string format( const std::multiset< T, C, A >& s ) { - return mock::serialize( s, c.begin(), c.end() ); + return format( s.begin(), s.end() ); } +#endif // MOCK_NO_STL_FORMAT } +#ifndef MOCK_NO_BOOST_FORMAT namespace boost { namespace assign_detail @@ -227,10 +120,11 @@ namespace assign_detail namespace mock { template< typename T > - mock::stream& operator<<( mock::stream& s, const boost::assign_detail::generic_list< T >& c ) + std::string format( const boost::assign_detail::generic_list< T >& l ) { - return mock::serialize( s, c.begin(), c.end() ); + return format( l.begin(), l.end() ); } } +#endif // MOCK_NO_BOOST_FORMAT #endif // #ifndef MOCK_FORMAT_HPP_INCLUDED diff --git a/src/libraries/turtle/function.hpp b/src/libraries/turtle/function.hpp index f8d4a87..5c151ab 100644 --- a/src/libraries/turtle/function.hpp +++ b/src/libraries/turtle/function.hpp @@ -21,8 +21,6 @@ #include #include #include -#include -#include #include #include #include @@ -115,11 +113,6 @@ namespace mock return s << *e.impl_; } -#define MOCK_EXPECTATION_CONTEXT \ - boost::unit_test::lazy_ostream::instance() \ - << lazy_context( parent_, name_ ) \ - << lazy_expectations( expectations_ ) - private: class function_impl : private verifiable { @@ -139,41 +132,13 @@ namespace mock { if( ! it->verify() ) ErrorPolicy::untriggered_expectation( - MOCK_EXPECTATION_CONTEXT, - it->file(), it->line() ); + context(), it->file(), it->line() ); else if( ! it->invoked() ) ErrorPolicy::expected_call( - MOCK_EXPECTATION_CONTEXT, - it->file(), it->line() ); + context(), it->file(), it->line() ); } } - virtual bool verify() const - { - for( expectations_cit it = expectations_.begin(); - it != expectations_.end(); ++it ) - if( !it->verify() ) - { - valid_ = false; - ErrorPolicy::verification_failed( - MOCK_EXPECTATION_CONTEXT, - it->file(), it->line() ); - } - return valid_; - } - -#undef MOCK_EXPECTATION_CONTEXT - - virtual void reset() - { - valid_ = true; - expectations_.clear(); - } - virtual void untie() - { - parent_ = 0; - } - void tag( const std::string& name ) { name_ = name; @@ -190,6 +155,28 @@ namespace mock parent_ = &parent; } + virtual bool verify() const + { + for( expectations_cit it = expectations_.begin(); + it != expectations_.end(); ++it ) + if( !it->verify() ) + { + valid_ = false; + ErrorPolicy::verification_failed( context(), + it->file(), it->line() ); + } + return valid_; + } + virtual void reset() + { + valid_ = true; + expectations_.clear(); + } + virtual void untie() + { + parent_ = 0; + } + expectation_type& expect( const std::string& file, int line ) { expectation_type& e = expect(); @@ -205,14 +192,26 @@ namespace mock return expectations_.back(); } -#define MOCK_EXPECTATION_FORMAT(z, n, N) \ - << " " << mock::format( p##n ) << BOOST_PP_IF(BOOST_PP_EQUAL(N,n), " ", ",") -#define MOCK_EXPECTATION_CALL_CONTEXT(n) \ - boost::unit_test::lazy_ostream::instance() \ - << lazy_context( parent_, name_ ) \ - << "(" BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_FORMAT, BOOST_PP_DEC(n)) << ")" \ - << lazy_expectations( expectations_ ) -#define MOCK_EXPECTATION_INVOKE(z, n, A) \ + struct no_throw_abort + { + static void abort() {} + }; + void test() const + { + invoke< no_throw_abort >(); + } + + result_type operator()() const + { + return invoke< ErrorPolicy >(); + } + +#define MOCK_EXPECTATION_FORMAT(z, n, d) \ + BOOST_PP_IF(n, + ", " +,) mock::format( p##n ) +#define MOCK_EXPECTATION_CONTEXT(n) \ + context( BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_FORMAT, BOOST_PP_EMPTY) ) +#define MOCK_EXPECTATION_OPERATOR(z, n, d) \ + MOCK_DECL(operator(), n, Signature, const, BOOST_DEDUCED_TYPENAME) \ { \ for( expectations_cit it = expectations_.begin(); it != expectations_.end(); ++it ) \ if( it->is_valid( BOOST_PP_ENUM_PARAMS(n, p) ) ) \ @@ -220,39 +219,59 @@ namespace mock if( ! it->invoke() ) \ { \ valid_ = false; \ - ErrorPolicy::sequence_failed( MOCK_EXPECTATION_CALL_CONTEXT(n), it->file(), it->line() ); \ - return A; \ + ErrorPolicy::sequence_failed( MOCK_EXPECTATION_CONTEXT(n), it->file(), it->line() ); \ + return ErrorPolicy::abort(); \ } \ if( ! it->functor() ) \ { \ - ErrorPolicy::missing_action( MOCK_EXPECTATION_CALL_CONTEXT(n), it->file(), it->line() ); \ - return A; \ + ErrorPolicy::missing_action( MOCK_EXPECTATION_CONTEXT(n), it->file(), it->line() ); \ + return ErrorPolicy::abort(); \ } \ - ErrorPolicy::expected_call( MOCK_EXPECTATION_CALL_CONTEXT(n), it->file(), it->line() ); \ + ErrorPolicy::expected_call( MOCK_EXPECTATION_CONTEXT(n), it->file(), it->line() ); \ return it->functor()( BOOST_PP_ENUM_PARAMS(n, p) ); \ } \ valid_ = false; \ - ErrorPolicy::unexpected_call( MOCK_EXPECTATION_CALL_CONTEXT(n) ); \ - return A; \ + ErrorPolicy::unexpected_call( MOCK_EXPECTATION_CONTEXT(n) ); \ + return ErrorPolicy::abort(); \ } -#define MOCK_EXPECTATION_OPERATOR(z, n, P) \ - MOCK_DECL(operator(), n, Signature, const, BOOST_DEDUCED_TYPENAME) \ - MOCK_EXPECTATION_INVOKE(z, n, P) - - BOOST_PP_REPEAT_FROM_TO(0, MOCK_NUM_ARGS, MOCK_EXPECTATION_OPERATOR, ErrorPolicy::abort()) - - void test() const - MOCK_EXPECTATION_INVOKE(, 0,) - + BOOST_PP_REPEAT_FROM_TO(1, MOCK_NUM_ARGS, MOCK_EXPECTATION_OPERATOR, BOOST_PP_EMPTY) +#undef MOCK_EXPECTATION_CONTEXT #undef MOCK_EXPECTATION_FORMAT #undef MOCK_EXPECTATION_OPERATOR -#undef MOCK_EXPECTATION_INVOKE -#undef MOCK_EXPECTATION_CALL_CONTEXT friend std::ostream& operator<<( std::ostream& s, const function_impl& e ) { - return s << lazy_context( e.parent_, e.name_ ) - << lazy_expectations( e.expectations_ ); + return s << e.context(); + } + + private: + template< typename T > + result_type invoke() const + { + for( expectations_cit it = expectations_.begin(); + it != expectations_.end(); ++it ) + if( it->is_valid() ) + { + if( ! it->invoke() ) + { + valid_ = false; + ErrorPolicy::sequence_failed( context( "" ), + it->file(), it->line() ); + return T::abort(); + } + if( ! it->functor() ) + { + ErrorPolicy::missing_action( context( "" ), + it->file(), it->line() ); + return T::abort(); + } + ErrorPolicy::expected_call( context( "" ), + it->file(), it->line() ); + return it->functor()(); + } + valid_ = false; + ErrorPolicy::unexpected_call( context( "" ) ); + return T::abort(); } private: @@ -260,39 +279,31 @@ namespace mock typedef BOOST_DEDUCED_TYPENAME expectations_type::const_iterator expectations_cit; - struct lazy_context + std::string context() const { - lazy_context( const node* parent, const std::string& name ) - : parent_( parent ) - , name_( &name ) - {} - friend std::ostream& operator<<( std::ostream& s, const lazy_context& e ) - { - if( e.parent_ ) - s << e.parent_->tag(); - return s << *e.name_; - } - const node* parent_; - const std::string* name_; - }; - - struct lazy_expectations + std::stringstream s; + serialize( s, "" ); + return s.str(); + } + std::string context( const std::string& parameters ) const { - lazy_expectations( const expectations_type& expectations ) - : expectations_( &expectations ) - {} - friend std::ostream& operator<<( std::ostream& s, - const lazy_expectations& e ) - { - typedef BOOST_DEDUCED_TYPENAME - expectations_type::const_iterator expectations_cit; - for( expectations_cit it = e.expectations_->begin(); - it != e.expectations_->end(); ++it ) - s << std::endl << *it; - return s; - } - const expectations_type* expectations_; - }; + std::stringstream s; + if( parameters.empty() ) + serialize( s, "()" ); + else + serialize( s, "( " + parameters + " )" ); + return s.str(); + } + void serialize( std::ostream& s, + const std::string& parameters ) const + { + if( parent_ ) + s << parent_->tag(); + s << name_ << parameters; + for( expectations_cit it = expectations_.begin(); + it != expectations_.end(); ++it ) + s << std::endl << *it; + } std::string name_; node* parent_; diff --git a/src/libraries/turtle/object.hpp b/src/libraries/turtle/object.hpp index 548019a..37294d8 100644 --- a/src/libraries/turtle/object.hpp +++ b/src/libraries/turtle/object.hpp @@ -12,6 +12,7 @@ #include "node.hpp" #include "root.hpp" #include +#include #include namespace mock diff --git a/src/libraries/turtle/operators.hpp b/src/libraries/turtle/operators.hpp index 12ded04..562c306 100644 --- a/src/libraries/turtle/operators.hpp +++ b/src/libraries/turtle/operators.hpp @@ -10,63 +10,60 @@ #define MOCK_OPERATORS_HPP_INCLUDED #include "constraint.hpp" -#include "format.hpp" namespace mock { namespace detail { - template< typename Constraint1, typename Constraint2 > + template< typename Functor1, typename Functor2 > class and_ { public: - and_( const Constraint1& c1, const Constraint2& c2 ) - : c1_( c1 ) - , c2_( c2 ) + and_( const Functor1& f1, const Functor2& f2 ) + : f1_( f1 ) + , f2_( f2 ) {} template< typename Actual > bool operator()( const Actual& actual ) const { - return c1_( actual ) && c2_( actual ); + return f1_( actual ) && f2_( actual ); } friend std::ostream& operator<<( std::ostream& s, const and_& a ) { - return s << "( " << mock::format( a.c1_ ) - << " && " << mock::format( a.c2_ ) << " )"; + return s << "( " << a.f1_ << " && " << a.f2_ << " )"; } private: - Constraint1 c1_; - Constraint2 c2_; + Functor1 f1_; + Functor2 f2_; }; - template< typename Constraint1, typename Constraint2 > + template< typename Functor1, typename Functor2 > class or_ { public: - or_( const Constraint1& c1, const Constraint2& c2 ) - : c1_( c1 ) - , c2_( c2 ) + or_( const Functor1& f1, const Functor2& f2 ) + : f1_( f1 ) + , f2_( f2 ) {} template< typename Actual > bool operator()( const Actual& actual ) const { - return c1_( actual ) || c2_( actual ); + return f1_( actual ) || f2_( actual ); } friend std::ostream& operator<<( std::ostream& s, const or_& o ) { - return s << "( " << mock::format( o.c1_ ) - << " || " << mock::format( o.c2_ )<< " )"; + return s << "( " << o.f1_ << " || " << o.f2_ << " )"; } private: - Constraint1 c1_; - Constraint2 c2_; + Functor1 f1_; + Functor2 f2_; }; - template< typename Constraint > + template< typename Functor > class not_ { public: - explicit not_( const Constraint& f ) + explicit not_( const Functor& f ) : f_( f ) {} template< typename Actual > @@ -76,34 +73,34 @@ namespace detail } friend std::ostream& operator<<( std::ostream& s, const not_& n ) { - return s << "! " << mock::format( n.f_ ); + return s << "! " << n.f_; } private: - Constraint f_; + Functor f_; }; } - template< typename Constraint1, typename Constraint2 > - const constraint< detail::or_< Constraint1, Constraint2 > > - operator||( const constraint< Constraint1 >& lhs, - const constraint< Constraint2 >& rhs ) + template< typename Functor1, typename Functor2 > + const constraint< detail::or_< Functor1, Functor2 > > + operator||( const constraint< Functor1 >& lhs, + const constraint< Functor2 >& rhs ) { - return detail::or_< Constraint1, Constraint2 >( lhs.f_, rhs.f_ ); + return detail::or_< Functor1, Functor2 >( lhs.f_, rhs.f_ ); } - template< typename Constraint1, typename Constraint2 > - const constraint< detail::and_< Constraint1, Constraint2 > > - operator&&( const constraint< Constraint1 >& lhs, - const constraint< Constraint2 >& rhs ) + template< typename Functor1, typename Functor2 > + const constraint< detail::and_< Functor1, Functor2 > > + operator&&( const constraint< Functor1 >& lhs, + const constraint< Functor2 >& rhs ) { - return detail::and_< Constraint1, Constraint2 >( lhs.f_, rhs.f_ ); + return detail::and_< Functor1, Functor2 >( lhs.f_, rhs.f_ ); } - template< typename Constraint > - const constraint< detail::not_< Constraint > > - operator!( const constraint< Constraint >& c ) + template< typename Functor > + const constraint< detail::not_< Functor > > + operator!( const constraint< Functor >& c ) { - return detail::not_< Constraint >( c.f_ ); + return detail::not_< Functor >( c.f_ ); } } diff --git a/src/tests/turtle_test/format_test.cpp b/src/tests/turtle_test/format_test.cpp index 962f283..25ea2ac 100644 --- a/src/tests/turtle_test/format_test.cpp +++ b/src/tests/turtle_test/format_test.cpp @@ -8,9 +8,10 @@ #include #include -#include +#include #include #include +#include #include #include diff --git a/src/tests/turtle_test/function_test.cpp b/src/tests/turtle_test/function_test.cpp index c9fa9fd..d587a06 100644 --- a/src/tests/turtle_test/function_test.cpp +++ b/src/tests/turtle_test/function_test.cpp @@ -20,12 +20,11 @@ #define CHECK_CALLS( calls ) \ BOOST_CHECK_EQUAL( calls, expected_call_count ); \ expected_call_count = 0; -#define CHECK_ERROR( expr, error, calls, context ) \ +#define CHECK_ERROR( expr, error, calls ) \ BOOST_CHECK( verify() ); \ expr; \ BOOST_CHECK_EQUAL( 1, error##_count ); \ CHECK_CALLS( calls ); \ - BOOST_CHECK_EQUAL( context, last_context ); \ reset(); namespace @@ -49,7 +48,6 @@ namespace sequence_failed_count = 0; verification_failed_count = 0; untriggered_expectation_count = 0; - last_context.clear(); } bool verify() const { @@ -89,11 +87,11 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_unconfigured_function_calls_unexpected_ca { { mock::function< void() > f; - CHECK_ERROR( f(), unexpected_call, 0, "?()" ); + CHECK_ERROR( f(), unexpected_call, 0 ); } { mock::function< int( int, const std::string& ) > f; - CHECK_ERROR( f( 1, "s" ), unexpected_call, 0, "?( 1, \"s\" )" ); + CHECK_ERROR( f( 1, "s" ), unexpected_call, 0 ); } } @@ -102,12 +100,12 @@ BOOST_FIXTURE_TEST_CASE( triggering_a_never_expectation_calls_unexpected_call_er { mock::function< void() > f; f.expect().never(); - CHECK_ERROR( f(), unexpected_call, 0, "?()\nv never()" ); + CHECK_ERROR( f(), unexpected_call, 0 ); } { mock::function< int( int, const std::string& ) > f; f.expect().never(); - CHECK_ERROR( f( 1, "s" ), unexpected_call, 0, "?( 1, \"s\" )\nv never().with( any, any )" ); + CHECK_ERROR( f( 1, "s" ), unexpected_call, 0 ); } } @@ -135,13 +133,13 @@ BOOST_FIXTURE_TEST_CASE( triggering_a_once_expectation_calls_unexpected_call_err mock::function< void() > f; f.expect().once(); f(); - CHECK_ERROR( f(), unexpected_call, 1, "?()\nv once()" ); + CHECK_ERROR( f(), unexpected_call, 1 ); } { mock::function< void( int, const std::string& ) > f; f.expect().once(); f( 1, "s" ); - CHECK_ERROR( f( 1, "s" ), unexpected_call, 1, "?( 1, \"s\" )\nv once().with( any, any )" ); + CHECK_ERROR( f( 1, "s" ), unexpected_call, 1 ); } } @@ -208,12 +206,12 @@ BOOST_FIXTURE_TEST_CASE( verifying_a_once_expectation_before_the_call_fails, err { mock::function< void() > f; f.expect().once(); - CHECK_ERROR( BOOST_CHECK( ! f.verify() ), verification_failed, 0, "?\n. once()" ); + CHECK_ERROR( BOOST_CHECK( ! f.verify() ), verification_failed, 0 ); } { mock::function< int( int, const std::string& ) > f; f.expect().once(); - CHECK_ERROR( BOOST_CHECK( ! f.verify() ), verification_failed, 0, "?\n. once().with( any, any )" ); + CHECK_ERROR( BOOST_CHECK( ! f.verify() ), verification_failed, 0 ); } } @@ -225,13 +223,13 @@ BOOST_FIXTURE_TEST_CASE( triggering_a_reset_function_calls_unexpected_call_error mock::function< void() > f; f.expect(); f.reset(); - CHECK_ERROR( f(), unexpected_call, 0, "?()" ); + CHECK_ERROR( f(), unexpected_call, 0 ); } { mock::function< int( int, const std::string& ) > f; f.expect(); f.reset(); - CHECK_ERROR( f( 1, "s" ), unexpected_call, 0, "?( 1, \"s\" )" ); + CHECK_ERROR( f( 1, "s" ), unexpected_call, 0 ); } } @@ -258,12 +256,12 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in { mock::function< void( int ) > f; f.expect().with( 42 ); - CHECK_ERROR( f( 43 ), unexpected_call, 0, "?( 43 )\n. unlimited().with( 42 )" ); + CHECK_ERROR( f( 43 ), unexpected_call, 0 ); } { mock::function< int( int, const std::string& ) > f; f.expect().with( 42, "expected" ); - CHECK_ERROR( f( 42, "actual" ), unexpected_call, 0, "?( 42, \"actual\" )\n. unlimited().with( 42, \"expected\" )" ); + CHECK_ERROR( f( 42, "actual" ), unexpected_call, 0 ); } } @@ -273,7 +271,7 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in f.expect().with( mock::equal( 42 ) || mock::less( 42 ) ); f( 41 ); f( 42 ); - CHECK_ERROR( f( 43 ), unexpected_call, 2, "?( 43 )\n. unlimited().with( ( equal( 42 ) || less( 42 ) ) )" ); + CHECK_ERROR( f( 43 ), unexpected_call, 2 ); } BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_and_not_less_constraint_calls_unexpected_call_error, error_fixture ) @@ -281,7 +279,7 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in mock::function< void( int ) > f; f.expect().with( mock::equal( 42 ) && ! mock::less( 41 ) ); f( 42 ); - CHECK_ERROR( f( 43 ), unexpected_call, 1, "?( 43 )\n. unlimited().with( ( equal( 42 ) && ! less( 41 ) ) )" ); + CHECK_ERROR( f( 43 ), unexpected_call, 1 ); } namespace @@ -330,12 +328,12 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_failing_custom_constrain { mock::function< void( int ) > f; f.expect().with( &custom_constraint ); - CHECK_ERROR( f( 42 ), unexpected_call, 0, "?( 42 )\n. unlimited().with( ? )" ); + CHECK_ERROR( f( 42 ), unexpected_call, 0 ); } { mock::function< int( int, const std::string& ) > f; f.expect().with( &custom_constraint, "actual" ); - CHECK_ERROR( f( 42, "actual" ), unexpected_call, 0, "?( 42, \"actual\" )\n. unlimited().with( ?, \"actual\" )" ); + CHECK_ERROR( f( 42, "actual" ), unexpected_call, 0 ); } } @@ -356,17 +354,17 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_no_return_set_calls_miss { mock::function< int() > f; f.expect(); - CHECK_ERROR( f(), missing_action, 0, "?()\n. unlimited()" ); + CHECK_ERROR( f(), missing_action, 0 ); } { mock::function< int&() > f; f.expect(); - CHECK_ERROR( f(), missing_action, 0, "?()\n. unlimited()" ); + CHECK_ERROR( f(), missing_action, 0 ); } { mock::function< const std::string&() > f; f.expect(); - CHECK_ERROR( f(), missing_action, 0, "?()\n. unlimited()" ); + CHECK_ERROR( f(), missing_action, 0 ); } } @@ -584,7 +582,7 @@ BOOST_FIXTURE_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_ f.expect().once(); f(); f(); - CHECK_ERROR( f(), unexpected_call, 2, "?()\nv once()\nv once()" ); + CHECK_ERROR( f(), unexpected_call, 2 ); } { mock::function< void( const std::string& ) > f; @@ -592,7 +590,7 @@ BOOST_FIXTURE_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_ f.expect().once().with( "second" ); f( "first" ); f( "second" ); - CHECK_ERROR( f( "third"), unexpected_call, 2, "?( \"third\" )\nv once().with( \"first\" )\nv once().with( \"second\" )" ); + CHECK_ERROR( f( "third"), unexpected_call, 2 ); } } @@ -604,7 +602,7 @@ BOOST_FIXTURE_TEST_CASE( best_expectation_is_selected_first, error_fixture ) f.expect().once().with( 2 ); f( 2 ); f( 1 ); - CHECK_ERROR( f( 3 ), unexpected_call, 2, "?( 3 )\nv once().with( 1 )\nv once().with( 2 )" ); + CHECK_ERROR( f( 3 ), unexpected_call, 2 ); } { mock::function< void( const std::string& ) > f; @@ -612,7 +610,7 @@ BOOST_FIXTURE_TEST_CASE( best_expectation_is_selected_first, error_fixture ) f.expect().once().with( "second" ); f( "second" ); f( "first" ); - CHECK_ERROR( f( "third"), unexpected_call, 2, "?( \"third\" )\nv once().with( \"first\" )\nv once().with( \"second\" )" ); + CHECK_ERROR( f( "third"), unexpected_call, 2 ); } } @@ -716,28 +714,28 @@ BOOST_FIXTURE_TEST_CASE( expectation_with_remaining_untriggered_matches_upon_des { std::auto_ptr< mock::function< void() > > f( new mock::function< void() > ); f->expect().once(); - CHECK_ERROR( f.reset(), untriggered_expectation, 0, "?\n. once()" ); + CHECK_ERROR( f.reset(), untriggered_expectation, 0 ); } BOOST_FIXTURE_TEST_CASE( verifying_expectation_with_remaining_matches_disables_the_automatic_verification_upon_destruction, error_fixture ) { mock::function< void() > f; f.expect().once(); - CHECK_ERROR( f.verify(), verification_failed, 0, "?\n. once()" ); + CHECK_ERROR( f.verify(), verification_failed, 0 ); } BOOST_FIXTURE_TEST_CASE( triggering_unexpected_call_call_disables_the_automatic_verification_upon_destruction, error_fixture ) { mock::function< void() > f; - CHECK_ERROR( f(), unexpected_call, 0, "?()" ); + CHECK_ERROR( f(), unexpected_call, 0 ); } BOOST_FIXTURE_TEST_CASE( adding_a_expectation_reactivates_the_verification_upon_destruction, error_fixture ) { std::auto_ptr< mock::function< void() > > f( new mock::function< void() > ); - CHECK_ERROR( (*f)(), unexpected_call, 0, "?()" ); + CHECK_ERROR( (*f)(), unexpected_call, 0 ); f->expect().once(); - CHECK_ERROR( f.reset(), untriggered_expectation, 0, "?\n. once()" ); + CHECK_ERROR( f.reset(), untriggered_expectation, 0 ); } BOOST_FIXTURE_TEST_CASE( throwing_an_exception_disables_the_automatic_verification_upon_destruction, error_fixture ) diff --git a/src/tests/turtle_test/integration_test.cpp b/src/tests/turtle_test/integration_test.cpp index bacf86f..b3be97f 100644 --- a/src/tests/turtle_test/integration_test.cpp +++ b/src/tests/turtle_test/integration_test.cpp @@ -329,41 +329,6 @@ BOOST_AUTO_TEST_CASE( boost_optional_on_base_class_reference_as_return_type_is_s b.method(); } -namespace -{ - bool serialized = false; - - struct custom_argument - { - friend std::ostream& operator<<( std::ostream& s, const custom_argument& ) - { - serialized = true; - return s; - } - }; - struct custom_constraint - { - template< typename Actual > - friend bool operator==( Actual, const custom_constraint& ) - { - return true; - } - friend std::ostream& operator<<( std::ostream& s, const custom_constraint& ) - { - serialized = true; - return s; - } - }; -} - -BOOST_AUTO_TEST_CASE( constraints_and_arguments_are_serialized_lazily ) -{ - MOCK_FUNCTOR( void( custom_argument ) ) f; - MOCK_EXPECT( f, _ ).with( custom_constraint() ); - f( custom_argument() ); - BOOST_CHECK( ! serialized ); -} - namespace { template< typename Expected > diff --git a/src/tests/turtle_test/mock_error.hpp b/src/tests/turtle_test/mock_error.hpp index 553bdd6..09088e1 100644 --- a/src/tests/turtle_test/mock_error.hpp +++ b/src/tests/turtle_test/mock_error.hpp @@ -10,7 +10,6 @@ #define MOCK_TEST_MOCK_ERROR_HPP_INCLUDED #include -#include #include namespace @@ -21,8 +20,6 @@ namespace int sequence_failed_count = 0; int verification_failed_count = 0; int untriggered_expectation_count = 0; - - std::string last_context; } namespace mock { @@ -35,45 +32,33 @@ namespace mock return r; } - template< typename Context > - static void missing_action( const Context& context, + static void missing_action( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) { - last_context = boost::lexical_cast< std::string >( context ); ++missing_action_count; } - template< typename Context > - static void expected_call( const Context& context, + static void expected_call( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) { - last_context = boost::lexical_cast< std::string >( context ); ++expected_call_count; } - template< typename Context > - static void unexpected_call( const Context& context ) + static void unexpected_call( const std::string& /*context*/ ) { - last_context = boost::lexical_cast< std::string >( context ); ++unexpected_call_count; } - template< typename Context > - static void sequence_failed( const Context& context, + static void sequence_failed( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) { - last_context = boost::lexical_cast< std::string >( context ); ++sequence_failed_count; } - template< typename Context > - static void verification_failed( const Context& context, + static void verification_failed( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) { - last_context = boost::lexical_cast< std::string >( context ); ++verification_failed_count; } - template< typename Context > - static void untriggered_expectation( const Context& context, + static void untriggered_expectation( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) { - last_context = boost::lexical_cast< std::string >( context ); ++untriggered_expectation_count; } }; @@ -82,45 +67,33 @@ namespace mock { static void abort() {} - template< typename Context > - static void missing_action( const Context& context, + static void missing_action( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) { - last_context = boost::lexical_cast< std::string >( context ); ++missing_action_count; } - template< typename Context > - static void expected_call( const Context& context, + static void expected_call( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) { - last_context = boost::lexical_cast< std::string >( context ); ++expected_call_count; } - template< typename Context > - static void unexpected_call( const Context& context ) + static void unexpected_call( const std::string& /*context*/ ) { - last_context = boost::lexical_cast< std::string >( context ); ++unexpected_call_count; } - template< typename Context > - static void sequence_failed( const Context& context, + static void sequence_failed( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) { - last_context = boost::lexical_cast< std::string >( context ); ++sequence_failed_count; } - template< typename Context > - static void verification_failed( const Context& context, + static void verification_failed( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) { - last_context = boost::lexical_cast< std::string >( context ); ++verification_failed_count; } - template< typename Context > - static void untriggered_expectation( const Context& context, + static void untriggered_expectation( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) { - last_context = boost::lexical_cast< std::string >( context ); ++untriggered_expectation_count; } }; diff --git a/src/tests/turtle_test/silent_error.hpp b/src/tests/turtle_test/silent_error.hpp index d19e363..911fdb4 100644 --- a/src/tests/turtle_test/silent_error.hpp +++ b/src/tests/turtle_test/silent_error.hpp @@ -10,6 +10,7 @@ #define MOCK_TEST_SILENT_ERROR_HPP_INCLUDED #include +#include #include namespace mock @@ -21,27 +22,21 @@ namespace mock { throw std::runtime_error( "abort" ); } - template< typename Context > - static void expected_call( const Context& /*context*/, + static void expected_call( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) {} - template< typename Context > - static void unexpected_call( const Context& /*context*/ ) + static void unexpected_call( const std::string& /*context*/ ) {} - template< typename Context > - static void missing_action( const Context& /*context*/, + static void missing_action( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) {} - template< typename Context > - static void sequence_failed( const Context& /*context*/, + static void sequence_failed( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) {} - template< typename Context > - static void verification_failed( const Context& /*context*/, + static void verification_failed( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) {} - template< typename Context > - static void untriggered_expectation( const Context& /*context*/, + static void untriggered_expectation( const std::string& /*context*/, const std::string& /*file*/, int /*line*/ ) {} };