Merged logging branch

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@245 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2011-02-19 22:10:22 +00:00
parent 4450ea0330
commit 171accfe27
30 changed files with 1025 additions and 434 deletions

View file

@ -150,7 +150,9 @@
<ClInclude Include="..\..\src\libraries\turtle\functional.hpp" /> <ClInclude Include="..\..\src\libraries\turtle\functional.hpp" />
<ClInclude Include="..\..\src\libraries\turtle\has_operator.hpp" /> <ClInclude Include="..\..\src\libraries\turtle\has_operator.hpp" />
<ClInclude Include="..\..\src\libraries\turtle\invocation.hpp" /> <ClInclude Include="..\..\src\libraries\turtle\invocation.hpp" />
<ClInclude Include="..\..\src\libraries\turtle\is_formattable.hpp" />
<ClInclude Include="..\..\src\libraries\turtle\is_functor.hpp" /> <ClInclude Include="..\..\src\libraries\turtle\is_functor.hpp" />
<ClInclude Include="..\..\src\libraries\turtle\is_serializable.hpp" />
<ClInclude Include="..\..\src\libraries\turtle\mock.hpp" /> <ClInclude Include="..\..\src\libraries\turtle\mock.hpp" />
<ClInclude Include="..\..\src\libraries\turtle\node.hpp" /> <ClInclude Include="..\..\src\libraries\turtle\node.hpp" />
<ClInclude Include="..\..\src\libraries\turtle\object.hpp" /> <ClInclude Include="..\..\src\libraries\turtle\object.hpp" />

View file

@ -73,5 +73,11 @@
<ClInclude Include="..\..\src\libraries\turtle\has_operator.hpp"> <ClInclude Include="..\..\src\libraries\turtle\has_operator.hpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\libraries\turtle\is_formattable.hpp">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="..\..\src\libraries\turtle\is_serializable.hpp">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -18,6 +18,26 @@
<Platform>x64</Platform> <Platform>x64</Platform>
</ProjectConfiguration> </ProjectConfiguration>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\tests\turtle_test\constraints_test.cpp" />
<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\integration_test.cpp" />
<ClCompile Include="..\..\src\tests\turtle_test\invocation_test.cpp" />
<ClCompile Include="..\..\src\tests\turtle_test\is_formattable_test.cpp" />
<ClCompile Include="..\..\src\tests\turtle_test\is_functor_test.cpp" />
<ClCompile Include="..\..\src\tests\turtle_test\is_serializable_test.cpp" />
<ClCompile Include="..\..\src\tests\turtle_test\max_args_test.cpp" />
<ClCompile Include="..\..\src\tests\turtle_test\mock_test.cpp" />
<ClCompile Include="..\..\src\tests\turtle_test\object_test.cpp" />
<ClCompile Include="..\..\src\tests\turtle_test\sequence_test.cpp" />
<ClCompile Include="..\..\src\tests\turtle_test\type_name_test.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\tests\turtle_test\mock_error.hpp" />
<ClInclude Include="..\..\src\tests\turtle_test\silent_error.hpp" />
</ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>{74810A2A-33D8-47D6-9A50-71261F1683F5}</ProjectGuid> <ProjectGuid>{74810A2A-33D8-47D6-9A50-71261F1683F5}</ProjectGuid>
<RootNamespace>turtle_test</RootNamespace> <RootNamespace>turtle_test</RootNamespace>
@ -194,24 +214,6 @@
</Command> </Command>
</PostBuildEvent> </PostBuildEvent>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\src\tests\turtle_test\constraints_test.cpp" />
<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" />
<ClCompile Include="..\..\src\tests\turtle_test\mock_test.cpp" />
<ClCompile Include="..\..\src\tests\turtle_test\object_test.cpp" />
<ClCompile Include="..\..\src\tests\turtle_test\sequence_test.cpp" />
<ClCompile Include="..\..\src\tests\turtle_test\type_name_test.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\tests\turtle_test\mock_error.hpp" />
<ClInclude Include="..\..\src\tests\turtle_test\silent_error.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View file

@ -7,15 +7,6 @@
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\tests\turtle_test\constraints_test.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\tests\turtle_test\error_test.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\tests\turtle_test\format_test.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\tests\turtle_test\function_test.cpp"> <ClCompile Include="..\..\src\tests\turtle_test\function_test.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
@ -25,9 +16,18 @@
<ClCompile Include="..\..\src\tests\turtle_test\invocation_test.cpp"> <ClCompile Include="..\..\src\tests\turtle_test\invocation_test.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\tests\turtle_test\is_formattable_test.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\tests\turtle_test\is_functor_test.cpp"> <ClCompile Include="..\..\src\tests\turtle_test\is_functor_test.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\tests\turtle_test\is_serializable_test.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\tests\turtle_test\max_args_test.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\tests\turtle_test\mock_test.cpp"> <ClCompile Include="..\..\src\tests\turtle_test\mock_test.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
@ -40,7 +40,13 @@
<ClCompile Include="..\..\src\tests\turtle_test\type_name_test.cpp"> <ClCompile Include="..\..\src\tests\turtle_test\type_name_test.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\tests\turtle_test\has_operator_test.cpp"> <ClCompile Include="..\..\src\tests\turtle_test\constraints_test.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\tests\turtle_test\error_test.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\tests\turtle_test\format_test.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>

View file

@ -196,10 +196,22 @@
RelativePath="..\..\src\libraries\turtle\invocation.hpp" RelativePath="..\..\src\libraries\turtle\invocation.hpp"
> >
</File> </File>
<File
RelativePath="..\..\src\libraries\turtle\is_comparable.hpp"
>
</File>
<File
RelativePath="..\..\src\libraries\turtle\is_formattable.hpp"
>
</File>
<File <File
RelativePath="..\..\src\libraries\turtle\is_functor.hpp" RelativePath="..\..\src\libraries\turtle\is_functor.hpp"
> >
</File> </File>
<File
RelativePath="..\..\src\libraries\turtle\is_serializable.hpp"
>
</File>
<File <File
RelativePath="..\..\src\libraries\turtle\lambda.hpp" RelativePath="..\..\src\libraries\turtle\lambda.hpp"
> >
@ -228,6 +240,10 @@
RelativePath="..\..\src\libraries\turtle\sequence.hpp" RelativePath="..\..\src\libraries\turtle\sequence.hpp"
> >
</File> </File>
<File
RelativePath="..\..\src\libraries\turtle\sink.hpp"
>
</File>
<File <File
RelativePath="..\..\src\libraries\turtle\type_name.hpp" RelativePath="..\..\src\libraries\turtle\type_name.hpp"
> >
@ -236,6 +252,10 @@
RelativePath="..\..\src\libraries\turtle\verifiable.hpp" RelativePath="..\..\src\libraries\turtle\verifiable.hpp"
> >
</File> </File>
<File
RelativePath="..\..\src\libraries\turtle\yes_no_type.hpp"
>
</File>
</Filter> </Filter>
</Files> </Files>
<Globals> <Globals>

View file

@ -214,10 +214,22 @@
RelativePath="..\..\src\tests\turtle_test\invocation_test.cpp" RelativePath="..\..\src\tests\turtle_test\invocation_test.cpp"
> >
</File> </File>
<File
RelativePath="..\..\src\tests\turtle_test\is_comparable_test.cpp"
>
</File>
<File
RelativePath="..\..\src\tests\turtle_test\is_formattable_test.cpp"
>
</File>
<File <File
RelativePath="..\..\src\tests\turtle_test\is_functor_test.cpp" RelativePath="..\..\src\tests\turtle_test\is_functor_test.cpp"
> >
</File> </File>
<File
RelativePath="..\..\src\tests\turtle_test\is_serializable_test.cpp"
>
</File>
<File <File
RelativePath="..\..\src\tests\turtle_test\max_args_test.cpp" RelativePath="..\..\src\tests\turtle_test\max_args_test.cpp"
> >

View file

@ -10,14 +10,12 @@
#define MOCK_CHECK_HPP_INCLUDED #define MOCK_CHECK_HPP_INCLUDED
#include "is_functor.hpp" #include "is_functor.hpp"
#include "is_comparable.hpp"
#include "constraints.hpp" #include "constraints.hpp"
#include "operators.hpp" #include "operators.hpp"
#include "format.hpp" #include "format.hpp"
#include <boost/function.hpp>
#include <boost/utility/enable_if.hpp> #include <boost/utility/enable_if.hpp>
#include <boost/concept_check.hpp> #include <boost/concept_check.hpp>
#include <stdexcept>
#include <ostream>
namespace mock namespace mock
{ {
@ -30,8 +28,8 @@ namespace detail
BOOST_CONCEPT_USAGE( FunctorCompatible ) BOOST_CONCEPT_USAGE( FunctorCompatible )
{ {
boost::require_boolean_expr( boost::require_boolean_expr(
// if an error is generated by the line below it means an argument // if an error is generated by the line below it means
// passed to 'with' was of the wrong type. // an argument passed to 'with' was of the wrong type.
functor_accepts( actual_argument_type ) functor_accepts( actual_argument_type )
); );
} }
@ -48,8 +46,8 @@ namespace detail
BOOST_CONCEPT_USAGE( EqualityComparable ) BOOST_CONCEPT_USAGE( EqualityComparable )
{ {
boost::require_boolean_expr( boost::require_boolean_expr(
// if an error is generated by the line below it means an argument // if an error is generated by the line below it means
// passed to 'with' was of the wrong type. // an argument passed to 'with' was of the wrong type.
actual_argument_type == expected_argument_type actual_argument_type == expected_argument_type
); );
} }
@ -60,56 +58,96 @@ namespace detail
}; };
template< typename Actual > template< typename Actual >
class check class check_base : boost::noncopyable
{ {
public: public:
template< typename Functor > virtual ~check_base() {}
explicit check( const Functor& f,
BOOST_DEDUCED_TYPENAME boost::enable_if< virtual bool operator()( Actual ) const = 0;
BOOST_DEDUCED_TYPENAME detail::is_functor< Functor >
>::type* = 0 ) friend std::ostream& operator<<( std::ostream& s, const check_base& c )
: desc_( mock::format( f ) )
{ {
BOOST_CONCEPT_ASSERT(( FunctorCompatible< Functor, Actual > )); c.format( s );
f_ = f; return s;
if( ! f_ )
std::invalid_argument( "invalid constraint" );
} }
template< typename Expected > private:
explicit check( const Expected& expected, virtual void format( std::ostream& ) const = 0;
BOOST_DEDUCED_TYPENAME boost::disable_if< };
BOOST_DEDUCED_TYPENAME detail::is_functor< Expected >
>::type* = 0 ) template< typename Actual, typename Expected, typename Enable = void >
: desc_( mock::format( expected ) ) class check : public check_base< Actual >
{
public:
explicit check( Expected expected )
: expected_( expected )
{ {
BOOST_CONCEPT_ASSERT(( EqualityComparable< Expected, Actual > )); BOOST_CONCEPT_ASSERT(( EqualityComparable< Expected, Actual > ));
f_ = mock::equal( expected ).f_;
if( ! f_ )
std::invalid_argument( "invalid constraint" );
} }
template< typename Functor > private:
explicit check( const constraint< Functor >& ph ) virtual bool operator()( Actual actual ) const
: desc_( mock::format( ph.f_ ) ) {
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<
boost::mpl::or_<
detail::is_functor< Functor >,
boost::mpl::not_<
detail::is_comparable< Actual, Functor >
>
>
>::type
> : public check_base< Actual >
{
public:
explicit check( const Functor& f )
: f_( f )
{ {
BOOST_CONCEPT_ASSERT(( FunctorCompatible< Functor, Actual > )); BOOST_CONCEPT_ASSERT(( FunctorCompatible< Functor, Actual > ));
f_ = ph.f_;
if( ! f_ )
std::invalid_argument( "invalid constraint" );
} }
private:
bool operator()( Actual actual ) const virtual bool operator()( Actual actual ) const
{ {
return f_( actual ); return f_( actual );
} }
virtual void format( std::ostream& s ) const
friend std::ostream& operator<<( std::ostream& s, const check& c )
{ {
return s << c.desc_; s << mock::format( f_ );
} }
private: private:
boost::function< bool( Actual ) > f_; Functor f_;
std::string desc_;
}; };
} }
} }

View file

@ -19,6 +19,8 @@ namespace mock
{} {}
Constraint f_; Constraint f_;
}; };
template< typename T, typename Constraint >
bool operator==( const T&, const constraint< Constraint >& );
} }
#endif // #ifndef MOCK_CONSTRAINT_HPP_INCLUDED #endif // #ifndef MOCK_CONSTRAINT_HPP_INCLUDED

View file

@ -37,27 +37,27 @@ namespace mock
throw boost::enable_current_exception( exception() ); throw boost::enable_current_exception( exception() );
} }
template< typename Context >
static void fail( static void fail(
const std::string& message, const std::string& context, const std::string& message, const Context& context,
const std::string& file = "unknown location", int line = 0 ) const std::string& file = "unknown location", int line = 0 )
{ {
boost::unit_test::framework::assertion_result( false ); boost::unit_test::framework::assertion_result( false );
boost::unit_test::unit_test_log boost::unit_test::unit_test_log
<< boost::unit_test::log::begin( file, (std::size_t)line ) << boost::unit_test::log::begin( file, (std::size_t)line )
<< boost::unit_test::log_all_errors << boost::unit_test::log_all_errors
<< boost::unit_test::lazy_ostream::instance()
<< message << ": " << context << message << ": " << context
<< boost::unit_test::log::end(); << boost::unit_test::log::end();
} }
static void expected_call( const std::string& context, template< typename Context >
static void expected_call( const Context& context,
const std::string& file, int line ) const std::string& file, int line )
{ {
boost::unit_test::framework::assertion_result( true ); boost::unit_test::framework::assertion_result( true );
boost::unit_test::unit_test_log boost::unit_test::unit_test_log
<< boost::unit_test::log::begin( file, (std::size_t)line ) << boost::unit_test::log::begin( file, (std::size_t)line )
<< boost::unit_test::log_successful_tests << boost::unit_test::log_successful_tests
<< boost::unit_test::lazy_ostream::instance()
<< "mock expectation fulfilled: " << context << "mock expectation fulfilled: " << context
<< boost::unit_test::log::end(); << boost::unit_test::log::end();
} }
@ -75,40 +75,47 @@ namespace mock
throw exception(); throw exception();
} }
template< typename Context >
static void fail( static void fail(
const std::string& message, const std::string& context, const std::string& message, const Context& context,
const std::string& file = "unknown location", int line = 0 ) const std::string& file = "unknown location", int line = 0 )
{ {
std::cerr << file << '(' << line << "): " std::cerr << file << '(' << line << "): "
<< message << ": " << context << std::endl; << message << ": " << context << std::endl;
} }
static void expected_call( const std::string& /*context*/, template< typename Context >
static void expected_call( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{} {}
#endif // MOCK_USE_BOOST_TEST #endif // MOCK_USE_BOOST_TEST
static void missing_action( const std::string& context, template< typename Context >
static void missing_action( const Context& context,
const std::string& file, int line ) const std::string& file, int line )
{ {
fail( "missing action", context, file, line ); fail( "missing action", context, file, line );
} }
static void unexpected_call( const std::string& context ) template< typename Context >
static void unexpected_call( const Context& context )
{ {
fail( "unexpected call", context ); fail( "unexpected call", context );
} }
static void sequence_failed( const std::string& context, template< typename Context >
static void sequence_failed( const Context& context,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{ {
fail( "sequence failed", context ); fail( "sequence failed", context );
} }
static void verification_failed( const std::string& context, template< typename Context >
static void verification_failed( const Context& context,
const std::string& file, int line ) const std::string& file, int line )
{ {
fail( "verification failed", context, file, line ); fail( "verification failed", context, file, line );
} }
static void untriggered_expectation( const std::string& context, template< typename Context >
static void untriggered_expectation( const Context& context,
const std::string& file, int line ) const std::string& file, int line )
{ {
fail( "untriggered expectation", context, file, line ); fail( "untriggered expectation", context, file, line );

View file

@ -172,14 +172,13 @@ namespace detail
BOOST_DEDUCED_TYPENAME \ BOOST_DEDUCED_TYPENAME \
boost::function_types::parameter_types< Signature >, \ boost::function_types::parameter_types< Signature >, \
n \ n \
>::type arg##n##_type; \ >::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##_( new detail::check< arg##n##_type, constraint< detail::any > >( mock::any ) )
#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##_.reset( new detail::check< arg##n##_type, Constraint##n >( c##n ) );
#define MOCK_EXPECTATION_WITH(z, n, d) c##n##_ = constraint##n##_type( c##n ); #define MOCK_EXPECTATION_MEMBER(z, n, d) boost::shared_ptr< detail::check_base< arg##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_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_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_SERIALIZE(z, n, d) BOOST_PP_IF(n, << ", " <<,) *m.c##n##_
#define MOCK_EXPECTATION(z, n, d) \ #define MOCK_EXPECTATION(z, n, d) \
template< typename Signature > \ template< typename Signature > \
class expectation< Signature, n > \ class expectation< Signature, n > \
@ -193,7 +192,7 @@ namespace detail
: BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_CONSTRUCTOR, BOOST_PP_EMPTY) \ : BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_CONSTRUCTOR, BOOST_PP_EMPTY) \
{} \ {} \
template< BOOST_PP_ENUM_PARAMS(n, typename Constraint) > \ template< BOOST_PP_ENUM_PARAMS(n, typename Constraint) > \
expectation& with( BOOST_PP_ENUM_BINARY_PARAMS(n, const Constraint, & c) ) \ expectation& with( BOOST_PP_ENUM_BINARY_PARAMS(n, Constraint, c) ) \
{ \ { \
BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_WITH, BOOST_PP_EMPTY) \ BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_WITH, BOOST_PP_EMPTY) \
return *this; \ return *this; \

View file

@ -9,122 +9,127 @@
#ifndef MOCK_FORMAT_HPP_INCLUDED #ifndef MOCK_FORMAT_HPP_INCLUDED
#define MOCK_FORMAT_HPP_INCLUDED #define MOCK_FORMAT_HPP_INCLUDED
#include <boost/type_traits.hpp> #include "is_serializable.hpp"
#include "is_formattable.hpp"
#include <boost/function_types/is_callable_builtin.hpp>
#include <boost/utility/enable_if.hpp> #include <boost/utility/enable_if.hpp>
#include <boost/exception/detail/is_output_streamable.hpp> #include <utility>
#ifndef MOCK_NO_STL_FORMAT
#include <boost/detail/container_fwd.hpp>
#endif // MOCK_NO_STL_FORMAT
#include <sstream>
#include <ostream> #include <ostream>
#include <string>
namespace mock namespace mock
{ {
template< typename T >
class formatter
{
public:
explicit formatter( const T& t )
: t_( &t )
{}
const T& operator*() const
{
return *t_;
}
const T* operator->() const
{
return t_;
}
private:
const T* t_;
};
namespace detail namespace detail
{ {
template< typename T > template< typename T >
std::string serialize( const T& t, void serialize( std::ostream& s, const T& t,
BOOST_DEDUCED_TYPENAME boost::enable_if< BOOST_DEDUCED_TYPENAME boost::enable_if<
BOOST_DEDUCED_TYPENAME boost::is_output_streamable< T > >::type* = 0 ) BOOST_DEDUCED_TYPENAME protect::is_serializable< std::ostream, T > >::type* = 0 )
{ {
std::stringstream s; s << t;
s << std::boolalpha << t;
return s.str();
} }
template< typename T > template< typename T >
std::string serialize( const T&, void serialize( std::ostream& s, const T&,
BOOST_DEDUCED_TYPENAME boost::disable_if< BOOST_DEDUCED_TYPENAME boost::disable_if<
BOOST_DEDUCED_TYPENAME boost::is_output_streamable< T > >::type* = 0 ) BOOST_DEDUCED_TYPENAME protect::is_serializable< std::ostream, T > >::type* = 0 )
{ {
return "?"; s << "?";
} }
} }
template< typename T > template< typename T >
std::string format( const T& t ) BOOST_DEDUCED_TYPENAME boost::enable_if<
BOOST_DEDUCED_TYPENAME mock::detail::is_formattable< std::ostream, T >,
std::ostream&
>::type
operator<<( std::ostream& s, const formatter< T >& f )
{ {
return detail::serialize( t ); format( s, *f );
} return s;
inline std::string format( const char* s )
{
return '"' + std::string( s ) + '"';
}
#ifndef MOCK_NO_STL_FORMAT
inline std::string format( const std::string& s )
{
return '"' + s + '"';
}
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 > template< typename T >
std::string format( const T& begin, const T& end ) BOOST_DEDUCED_TYPENAME boost::disable_if<
BOOST_DEDUCED_TYPENAME mock::detail::is_formattable< std::ostream, T >,
std::ostream&
>::type
operator<<( std::ostream& s, const formatter< T >& f )
{
mock::detail::serialize( s, *f );
return s;
}
inline std::ostream& operator<<( std::ostream& s, const formatter< bool >& f )
{
return s << std::boolalpha << *f;
}
inline std::ostream& operator<<( std::ostream& s, const formatter< std::string >& f )
{
return s << '"' << *f << '"';
}
inline std::ostream& operator<<( std::ostream& s, const formatter< const char* >& f )
{
return s << '"' << *f << '"';
}
template< typename T >
formatter< T > format( const T& t )
{
return formatter< T >( t );
}
template< typename Container >
void format( std::ostream& s, const Container& c,
BOOST_DEDUCED_TYPENAME Container::const_iterator* = 0 )
{ {
std::stringstream s;
s << '('; s << '(';
for( T it = begin; it != end; ++it ) // if an error is generated by the line below it means Container is
s << (it == begin ? "" : ",") << mock::format( *it ); // being mismatched for a container because it has a typedef
// const_iterator : the easiest solution would be to add a format
// function for Container as well.
for( BOOST_DEDUCED_TYPENAME Container::const_iterator it = c.begin();
it != c.end(); ++it )
{
if( it != c.begin() )
s << ',';
s << mock::format( *it );
}
s << ')'; s << ')';
return s.str();
} }
template< typename T, typename A >
std::string format( const std::deque< T, A >& d )
{
return format( d.begin(), d.end() );
}
template< typename T, typename A >
std::string format( const std::list< T, A >& l )
{
return format( l.begin(), l.end() );
}
template< typename T, typename A >
std::string format( const std::vector< T, A >& v )
{
return format( v.begin(), v.end() );
}
template< typename K, typename T, typename C, typename A >
std::string format( const std::map< K, T, C, A >& m )
{
return format( m.begin(), m.end() );
}
template< typename K, typename T, typename C, typename A >
std::string format( const std::multimap< K, T, C, A >& m )
{
return format( m.begin(), m.end() );
}
template< typename T, typename C, typename A >
std::string format( const std::set< T, C, A >& s )
{
return format( s.begin(), s.end() );
}
template< typename T, typename C, typename A >
std::string format( const std::multiset< T, C, A >& s )
{
return format( s.begin(), s.end() );
}
#endif // MOCK_NO_STL_FORMAT
}
#ifndef MOCK_NO_BOOST_FORMAT template< typename T1, typename T2 >
namespace boost void format( std::ostream& s, const std::pair< T1, T2 >& p )
{
namespace assign_detail
{
template< typename T > class generic_list;
}
}
namespace mock
{
template< typename T >
std::string format( const boost::assign_detail::generic_list< T >& l )
{ {
return format( l.begin(), l.end() ); s << '(' << mock::format( p.first )
<< ',' << mock::format( p.second ) << ')';
}
template< typename T >
void format( std::ostream& s, T,
BOOST_DEDUCED_TYPENAME boost::enable_if<
BOOST_DEDUCED_TYPENAME
boost::function_types::is_callable_builtin< T > >::type* = 0 )
{
s << '?';
} }
} }
#endif // MOCK_NO_BOOST_FORMAT
#endif // #ifndef MOCK_FORMAT_HPP_INCLUDED #endif // #ifndef MOCK_FORMAT_HPP_INCLUDED

View file

@ -21,6 +21,8 @@
#include <boost/preprocessor/repetition/repeat_from_to.hpp> #include <boost/preprocessor/repetition/repeat_from_to.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp> #include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/enum_binary_params.hpp> #include <boost/preprocessor/repetition/enum_binary_params.hpp>
#include <boost/preprocessor/comparison/equal.hpp>
#include <boost/test/utils/lazy_ostream.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <ostream> #include <ostream>
#include <list> #include <list>
@ -132,13 +134,45 @@ namespace mock
{ {
if( ! it->verify() ) if( ! it->verify() )
ErrorPolicy::untriggered_expectation( ErrorPolicy::untriggered_expectation(
context(), it->file(), it->line() ); boost::unit_test::lazy_ostream::instance()
<< lazy_context( this )
<< lazy_expectations( this ),
it->file(), it->line() );
else if( ! it->invoked() ) else if( ! it->invoked() )
ErrorPolicy::expected_call( ErrorPolicy::expected_call(
context(), it->file(), it->line() ); boost::unit_test::lazy_ostream::instance()
<< lazy_context( this )
<< lazy_expectations( this ),
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(
boost::unit_test::lazy_ostream::instance()
<< lazy_context( this )
<< lazy_expectations( this ),
it->file(), it->line() );
}
return valid_;
}
virtual void reset()
{
valid_ = true;
expectations_.clear();
}
virtual void untie()
{
parent_ = 0;
}
void tag( const std::string& name ) void tag( const std::string& name )
{ {
name_ = name; name_ = name;
@ -155,28 +189,6 @@ namespace mock
parent_ = &parent; 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& expect( const std::string& file, int line )
{ {
expectation_type& e = expect(); expectation_type& e = expect();
@ -192,26 +204,14 @@ namespace mock
return expectations_.back(); return expectations_.back();
} }
struct no_throw_abort #define MOCK_EXPECTATION_FORMAT(z, n, N) \
{ << " " << mock::format( p##n ) << BOOST_PP_IF(BOOST_PP_EQUAL(N,n), " ", ",")
static void abort() {} #define MOCK_EXPECTATION_CALL_CONTEXT(n) \
}; boost::unit_test::lazy_ostream::instance() \
void test() const << lazy_context( this ) \
{ << "(" BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_FORMAT, BOOST_PP_DEC(n)) << ")" \
invoke< no_throw_abort >(); << lazy_expectations( this )
} #define MOCK_EXPECTATION_INVOKE(z, n, A) \
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 ) \ for( expectations_cit it = expectations_.begin(); it != expectations_.end(); ++it ) \
if( it->is_valid( BOOST_PP_ENUM_PARAMS(n, p) ) ) \ if( it->is_valid( BOOST_PP_ENUM_PARAMS(n, p) ) ) \
@ -219,96 +219,77 @@ namespace mock
if( ! it->invoke() ) \ if( ! it->invoke() ) \
{ \ { \
valid_ = false; \ valid_ = false; \
ErrorPolicy::sequence_failed( MOCK_EXPECTATION_CONTEXT(n), it->file(), it->line() ); \ ErrorPolicy::sequence_failed( MOCK_EXPECTATION_CALL_CONTEXT(n), it->file(), it->line() ); \
return ErrorPolicy::abort(); \ return A; \
} \ } \
if( ! it->functor() ) \ if( ! it->functor() ) \
{ \ { \
ErrorPolicy::missing_action( MOCK_EXPECTATION_CONTEXT(n), it->file(), it->line() ); \ ErrorPolicy::missing_action( MOCK_EXPECTATION_CALL_CONTEXT(n), it->file(), it->line() ); \
return ErrorPolicy::abort(); \ return A; \
} \ } \
ErrorPolicy::expected_call( MOCK_EXPECTATION_CONTEXT(n), it->file(), it->line() ); \ ErrorPolicy::expected_call( MOCK_EXPECTATION_CALL_CONTEXT(n), it->file(), it->line() ); \
return it->functor()( BOOST_PP_ENUM_PARAMS(n, p) ); \ return it->functor()( BOOST_PP_ENUM_PARAMS(n, p) ); \
} \ } \
valid_ = false; \ valid_ = false; \
ErrorPolicy::unexpected_call( MOCK_EXPECTATION_CONTEXT(n) ); \ ErrorPolicy::unexpected_call( MOCK_EXPECTATION_CALL_CONTEXT(n) ); \
return ErrorPolicy::abort(); \ return A; \
} }
BOOST_PP_REPEAT_FROM_TO(1, MOCK_NUM_ARGS, MOCK_EXPECTATION_OPERATOR, BOOST_PP_EMPTY) #define MOCK_EXPECTATION_OPERATOR(z, n, P) \
#undef MOCK_EXPECTATION_CONTEXT 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,)
#undef MOCK_EXPECTATION_FORMAT #undef MOCK_EXPECTATION_FORMAT
#undef MOCK_EXPECTATION_OPERATOR #undef MOCK_EXPECTATION_OPERATOR
#undef MOCK_EXPECTATION_INVOKE
#undef MOCK_EXPECTATION_CALL_CONTEXT
friend std::ostream& operator<<( std::ostream& s, const function_impl& e ) friend std::ostream& operator<<( std::ostream& s, const function_impl& e )
{ {
return s << e.context(); return s << lazy_context( &e ) << lazy_expectations( &e );
} }
private: struct lazy_context
template< typename T >
result_type invoke() const
{ {
for( expectations_cit it = expectations_.begin(); lazy_context( const function_impl* impl )
it != expectations_.end(); ++it ) : impl_( impl )
if( it->is_valid() ) {}
{ friend std::ostream& operator<<( std::ostream& s, const lazy_context& e )
if( ! it->invoke() ) {
{ if( e.impl_->parent_ )
valid_ = false; s << e.impl_->parent_->tag();
ErrorPolicy::sequence_failed( context( "" ), return s << e.impl_->name_;
it->file(), it->line() ); }
return T::abort(); const function_impl* impl_;
} };
if( ! it->functor() )
{ struct lazy_expectations
ErrorPolicy::missing_action( context( "" ), {
it->file(), it->line() ); lazy_expectations( const function_impl* impl )
return T::abort(); : impl_( impl )
} {}
ErrorPolicy::expected_call( context( "" ), friend std::ostream& operator<<( std::ostream& s, const lazy_expectations& e )
it->file(), it->line() ); {
return it->functor()(); for( expectations_cit it = e.impl_->expectations_.begin();
} it != e.impl_->expectations_.end(); ++it )
valid_ = false; s << std::endl << *it;
ErrorPolicy::unexpected_call( context( "" ) ); return s;
return T::abort(); }
} const function_impl* impl_;
};
private:
typedef std::list< expectation_type > expectations_type; typedef std::list< expectation_type > expectations_type;
typedef BOOST_DEDUCED_TYPENAME typedef BOOST_DEDUCED_TYPENAME
expectations_type::const_iterator expectations_cit; expectations_type::const_iterator expectations_cit;
std::string context() const expectations_type expectations_;
{
std::stringstream s;
serialize( s, "" );
return s.str();
}
std::string context( const std::string& parameters ) const
{
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_; std::string name_;
node* parent_; node* parent_;
mutable bool valid_; mutable bool valid_;
expectations_type expectations_;
}; };
private: private:

View file

@ -0,0 +1,50 @@
//
// 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_IS_COMPARABLE_HPP_INCLUDED
#define MOCK_IS_COMPARABLE_HPP_INCLUDED
#include "yes_no_type.hpp"
#include "sink.hpp"
#include <boost/type_traits/detail/yes_no_type.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/mpl/bool.hpp>
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( disable: 4913 )
#endif
namespace mock
{
namespace detail
{
namespace comparable
{
yes_type operator==( sink, sink );
template< typename L, typename R >
struct impl
{
static BOOST_DEDUCED_TYPENAME boost::remove_reference< L >::type* l;
static BOOST_DEDUCED_TYPENAME boost::remove_reference< R >::type* r;
enum { value = sizeof( yes_type(), *l == *r, yes_type() ) == sizeof( yes_type ) };
};
}
template< typename L, typename R >
struct is_comparable : boost::mpl::bool_< comparable::impl< L, R >::value >
{};
}
}
#ifdef _MSC_VER
#pragma warning( pop )
#endif
#endif // #ifndef MOCK_IS_COMPARABLE_HPP_INCLUDED

View file

@ -0,0 +1,44 @@
//
// 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_IS_FORMATABLE_HPP_INCLUDED
#define MOCK_IS_FORMATABLE_HPP_INCLUDED
#include "yes_no_type.hpp"
#include "sink.hpp"
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( disable: 4913 )
#endif
namespace mock
{
template< typename S >
detail::yes_type format( S&, detail::sink );
namespace detail
{
template< typename S, typename T >
struct is_formattable
{
static S* s;
static T* t;
// if an error is generated by the line below it means T has more than
// one conversion to other types which are formattable : the easiest
// solution would be to add a format function for T as well.
enum { value = sizeof( yes_type(), format( *s, *t ), yes_type() ) == sizeof( yes_type ) };
};
}
}
#ifdef _MSC_VER
#pragma warning( pop )
#endif
#endif // #ifndef MOCK_IS_FORMATABLE_HPP_INCLUDED

View file

@ -9,6 +9,7 @@
#ifndef MOCK_IS_FUNCTOR_HPP_INCLUDED #ifndef MOCK_IS_FUNCTOR_HPP_INCLUDED
#define MOCK_IS_FUNCTOR_HPP_INCLUDED #define MOCK_IS_FUNCTOR_HPP_INCLUDED
#include "yes_no_type.hpp"
#include <boost/function_types/is_callable_builtin.hpp> #include <boost/function_types/is_callable_builtin.hpp>
#include <boost/type_traits/integral_constant.hpp> #include <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/detail/yes_no_type.hpp> #include <boost/type_traits/detail/yes_no_type.hpp>
@ -18,9 +19,6 @@ namespace mock
{ {
namespace detail namespace detail
{ {
typedef boost::type_traits::yes_type yes_type;
typedef boost::type_traits::no_type no_type;
#define MOCK_IS_FUNCTION_HELPER(N, M) \ #define MOCK_IS_FUNCTION_HELPER(N, M) \
template< typename T > yes_type& N##_helper( BOOST_DEDUCED_TYPENAME T::M* ); \ template< typename T > yes_type& N##_helper( BOOST_DEDUCED_TYPENAME T::M* ); \
template< typename T > no_type& N##_helper( ... ); \ template< typename T > no_type& N##_helper( ... ); \

View file

@ -0,0 +1,47 @@
//
// 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_IS_SERIALIZABLE_HPP_INCLUDED
#define MOCK_IS_SERIALIZABLE_HPP_INCLUDED
#include "yes_no_type.hpp"
#include "sink.hpp"
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( disable: 4913 )
#endif
namespace mock
{
namespace detail
{
namespace protect
{
template< typename S >
yes_type operator<<( S&, sink );
template< typename S, typename T >
struct is_serializable
{
static S* s;
static T* t;
// if an error is generated by the line below it means T has more than
// one conversion to other types which are serializable : the easiest
// solution would be to add a format function for T.
enum { value = sizeof( yes_type(), (*s << *t), yes_type() ) == sizeof( yes_type ) };
};
}
}
}
#ifdef _MSC_VER
#pragma warning( pop )
#endif
#endif // #ifndef MOCK_IS_SERIALIZABLE_HPP_INCLUDED

View file

@ -69,8 +69,7 @@ namespace detail
throw t; throw t;
} }
static void do_nothing() static void do_nothing()
{ {}
}
}; };
} }
} }

View file

@ -12,7 +12,6 @@
#include "node.hpp" #include "node.hpp"
#include "root.hpp" #include "root.hpp"
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <ostream>
#include <string> #include <string>
namespace mock namespace mock

View file

@ -10,60 +10,63 @@
#define MOCK_OPERATORS_HPP_INCLUDED #define MOCK_OPERATORS_HPP_INCLUDED
#include "constraint.hpp" #include "constraint.hpp"
#include "format.hpp"
namespace mock namespace mock
{ {
namespace detail namespace detail
{ {
template< typename Functor1, typename Functor2 > template< typename Constraint1, typename Constraint2 >
class and_ class and_
{ {
public: public:
and_( const Functor1& f1, const Functor2& f2 ) and_( const Constraint1& c1, const Constraint2& c2 )
: f1_( f1 ) : c1_( c1 )
, f2_( f2 ) , c2_( c2 )
{} {}
template< typename Actual > template< typename Actual >
bool operator()( const Actual& actual ) const bool operator()( const Actual& actual ) const
{ {
return f1_( actual ) && f2_( actual ); return c1_( actual ) && c2_( actual );
} }
friend std::ostream& operator<<( std::ostream& s, const and_& a ) friend std::ostream& operator<<( std::ostream& s, const and_& a )
{ {
return s << "( " << a.f1_ << " && " << a.f2_ << " )"; return s << "( " << mock::format( a.c1_ )
<< " && " << mock::format( a.c2_ ) << " )";
} }
private: private:
Functor1 f1_; Constraint1 c1_;
Functor2 f2_; Constraint2 c2_;
}; };
template< typename Functor1, typename Functor2 > template< typename Constraint1, typename Constraint2 >
class or_ class or_
{ {
public: public:
or_( const Functor1& f1, const Functor2& f2 ) or_( const Constraint1& c1, const Constraint2& c2 )
: f1_( f1 ) : c1_( c1 )
, f2_( f2 ) , c2_( c2 )
{} {}
template< typename Actual > template< typename Actual >
bool operator()( const Actual& actual ) const bool operator()( const Actual& actual ) const
{ {
return f1_( actual ) || f2_( actual ); return c1_( actual ) || c2_( actual );
} }
friend std::ostream& operator<<( std::ostream& s, const or_& o ) friend std::ostream& operator<<( std::ostream& s, const or_& o )
{ {
return s << "( " << o.f1_ << " || " << o.f2_ << " )"; return s << "( " << mock::format( o.c1_ )
<< " || " << mock::format( o.c2_ )<< " )";
} }
private: private:
Functor1 f1_; Constraint1 c1_;
Functor2 f2_; Constraint2 c2_;
}; };
template< typename Functor > template< typename Constraint >
class not_ class not_
{ {
public: public:
explicit not_( const Functor& f ) explicit not_( const Constraint& f )
: f_( f ) : f_( f )
{} {}
template< typename Actual > template< typename Actual >
@ -73,34 +76,34 @@ namespace detail
} }
friend std::ostream& operator<<( std::ostream& s, const not_& n ) friend std::ostream& operator<<( std::ostream& s, const not_& n )
{ {
return s << "! " << n.f_; return s << "! " << mock::format( n.f_ );
} }
private: private:
Functor f_; Constraint f_;
}; };
} }
template< typename Functor1, typename Functor2 > template< typename Constraint1, typename Constraint2 >
const constraint< detail::or_< Functor1, Functor2 > > const constraint< detail::or_< Constraint1, Constraint2 > >
operator||( const constraint< Functor1 >& lhs, operator||( const constraint< Constraint1 >& lhs,
const constraint< Functor2 >& rhs ) const constraint< Constraint2 >& rhs )
{ {
return detail::or_< Functor1, Functor2 >( lhs.f_, rhs.f_ ); return detail::or_< Constraint1, Constraint2 >( lhs.f_, rhs.f_ );
} }
template< typename Functor1, typename Functor2 > template< typename Constraint1, typename Constraint2 >
const constraint< detail::and_< Functor1, Functor2 > > const constraint< detail::and_< Constraint1, Constraint2 > >
operator&&( const constraint< Functor1 >& lhs, operator&&( const constraint< Constraint1 >& lhs,
const constraint< Functor2 >& rhs ) const constraint< Constraint2 >& rhs )
{ {
return detail::and_< Functor1, Functor2 >( lhs.f_, rhs.f_ ); return detail::and_< Constraint1, Constraint2 >( lhs.f_, rhs.f_ );
} }
template< typename Functor > template< typename Constraint >
const constraint< detail::not_< Functor > > const constraint< detail::not_< Constraint > >
operator!( const constraint< Functor >& c ) operator!( const constraint< Constraint >& c )
{ {
return detail::not_< Functor >( c.f_ ); return detail::not_< Constraint >( c.f_ );
} }
} }

View file

@ -0,0 +1,24 @@
//
// 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_ANY_HPP_INCLUDED
#define MOCK_ANY_HPP_INCLUDED
namespace mock
{
namespace detail
{
struct sink
{
template< typename T >
sink( const T& );
};
}
}
#endif // #ifndef MOCK_ANY_HPP_INCLUDED

View file

@ -0,0 +1,31 @@
//
// 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_YES_NO_TYPE_HPP_INCLUDED
#define MOCK_YES_NO_TYPE_HPP_INCLUDED
#include <boost/static_assert.hpp>
namespace mock
{
namespace detail
{
typedef char no_type;
struct yes_type
{
char no_type[2];
};
BOOST_STATIC_ASSERT( sizeof( yes_type ) != sizeof( no_type ) );
template< typename T > void operator,( yes_type, const T& );
no_type operator,( yes_type, yes_type );
no_type operator,( no_type, yes_type );
}
}
#endif // #ifndef MOCK_YES_NO_TYPE_HPP_INCLUDED

View file

@ -8,10 +8,9 @@
#include <turtle/format.hpp> #include <turtle/format.hpp>
#include <boost/assign.hpp> #include <boost/assign.hpp>
#include <ostream> #include <vector>
#include <deque> #include <deque>
#include <list> #include <list>
#include <vector>
#include <map> #include <map>
#include <set> #include <set>
@ -21,8 +20,6 @@
namespace namespace
{ {
struct non_serializable_type {};
template< typename T > template< typename T >
std::string to_string( T t ) std::string to_string( T t )
{ {
@ -32,65 +29,92 @@ namespace
} }
} }
BOOST_AUTO_TEST_CASE( type_not_serializable_in_standard_stream_yields_an_interrogation_mark_when_serialized )
{
BOOST_CHECK_EQUAL( "?", to_string( non_serializable_type() ) );
}
BOOST_AUTO_TEST_CASE( base_type_serializable_in_standard_stream_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "42", to_string( 42 ) );
}
namespace namespace
{ {
struct serializable_type {}; struct non_serializable
{};
std::ostream& operator<<( std::ostream& s, const serializable_type& )
{
return s << "serializable_type";
}
} }
BOOST_AUTO_TEST_CASE( custom_type_serializable_in_standard_stream_yields_its_value_when_serialized ) BOOST_AUTO_TEST_CASE( non_serializable_type_yields_an_interrogation_mark_when_serialized )
{ {
BOOST_CHECK_EQUAL( "serializable_type", to_string( serializable_type() ) ); BOOST_CHECK_EQUAL( "?", to_string( non_serializable() ) );
}
namespace
{
struct convertible_to_int
{
operator int() const { return 12; }
};
}
BOOST_AUTO_TEST_CASE( custom_type_convertible_to_base_type_yields_an_interrogation_mark_when_serialized )
{
BOOST_CHECK_EQUAL( "?", to_string( convertible_to_int() ) );
} }
namespace namespace
{ {
struct serializable struct serializable
{};
std::ostream& operator<<( std::ostream& s, const serializable& )
{ {
friend std::ostream& operator<<( std::ostream& s, const serializable& ) return s << "serializable";
{ }
return s << "serializable";
}
};
struct convertible_to_serializable
{
operator serializable() const { return serializable(); }
};
} }
BOOST_AUTO_TEST_CASE( custom_type_convertible_to_another_type_serializable_in_standard_stream_yields_an_interrogation_mark_when_serialized ) BOOST_AUTO_TEST_CASE( serializable_type_yields_its_value_when_serialized )
{ {
BOOST_CHECK_EQUAL( "?", to_string( convertible_to_serializable() ) ); BOOST_CHECK_EQUAL( "serializable", to_string( serializable() ) );
} }
BOOST_AUTO_TEST_CASE( booleans_are_serialized_as_booleans ) namespace
{
struct formattable {};
std::ostream& operator<<( std::ostream& s, const formattable& )
{
BOOST_FAIL( "should not be called" );
return s;
}
}
BOOST_AUTO_TEST_CASE( format_overrides_standard_stream_serialization_even_if_defined_after_being_used )
{
BOOST_CHECK_EQUAL( "formattable", to_string( formattable() ) );
}
namespace
{
void format( std::ostream& s, const formattable& )
{
s << "formattable";
}
}
namespace
{
struct formatterable {};
std::ostream& operator<<( std::ostream& s, const formatterable& )
{
BOOST_FAIL( "should not be called" );
return s;
}
void format( std::ostream&, const formatterable& )
{
BOOST_FAIL( "should not be called" );
}
}
BOOST_AUTO_TEST_CASE( formatter_overrides_standard_stream_serialization_and_format_even_if_defined_after_being_used )
{
BOOST_CHECK_EQUAL( "formatterable", to_string( formatterable() ) );
}
namespace
{
std::ostream& operator<<( std::ostream& s, const mock::formatter< formatterable >& )
{
return s << "formatterable";
}
}
BOOST_AUTO_TEST_CASE( base_type_yields_its_value_when_serialized )
{
BOOST_CHECK_EQUAL( "42", to_string( 42 ) );
}
BOOST_AUTO_TEST_CASE( booleans_are_serialized_as_bool_alpha )
{ {
BOOST_CHECK_EQUAL( "true", to_string( true ) ); BOOST_CHECK_EQUAL( "true", to_string( true ) );
BOOST_CHECK_EQUAL( "false", to_string( false ) ); BOOST_CHECK_EQUAL( "false", to_string( false ) );

View file

@ -20,11 +20,12 @@
#define CHECK_CALLS( calls ) \ #define CHECK_CALLS( calls ) \
BOOST_CHECK_EQUAL( calls, expected_call_count ); \ BOOST_CHECK_EQUAL( calls, expected_call_count ); \
expected_call_count = 0; expected_call_count = 0;
#define CHECK_ERROR( expr, error, calls ) \ #define CHECK_ERROR( expr, error, calls, context ) \
BOOST_CHECK( verify() ); \ BOOST_CHECK( verify() ); \
expr; \ expr; \
BOOST_CHECK_EQUAL( 1, error##_count ); \ BOOST_CHECK_EQUAL( 1, error##_count ); \
CHECK_CALLS( calls ); \ CHECK_CALLS( calls ); \
BOOST_CHECK_EQUAL( context, last_context ); \
reset(); reset();
namespace namespace
@ -48,6 +49,7 @@ namespace
sequence_failed_count = 0; sequence_failed_count = 0;
verification_failed_count = 0; verification_failed_count = 0;
untriggered_expectation_count = 0; untriggered_expectation_count = 0;
last_context.clear();
} }
bool verify() const bool verify() const
{ {
@ -87,11 +89,11 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_unconfigured_function_calls_unexpected_ca
{ {
{ {
mock::function< void() > f; mock::function< void() > f;
CHECK_ERROR( f(), unexpected_call, 0 ); CHECK_ERROR( f(), unexpected_call, 0, "?()" );
} }
{ {
mock::function< int( int, const std::string& ) > f; mock::function< int( int, const std::string& ) > f;
CHECK_ERROR( f( 1, "s" ), unexpected_call, 0 ); CHECK_ERROR( f( 1, "s" ), unexpected_call, 0, "?( 1, \"s\" )" );
} }
} }
@ -100,12 +102,12 @@ BOOST_FIXTURE_TEST_CASE( triggering_a_never_expectation_calls_unexpected_call_er
{ {
mock::function< void() > f; mock::function< void() > f;
f.expect().never(); f.expect().never();
CHECK_ERROR( f(), unexpected_call, 0 ); CHECK_ERROR( f(), unexpected_call, 0, "?()\nv never()" );
} }
{ {
mock::function< int( int, const std::string& ) > f; mock::function< int( int, const std::string& ) > f;
f.expect().never(); f.expect().never();
CHECK_ERROR( f( 1, "s" ), unexpected_call, 0 ); CHECK_ERROR( f( 1, "s" ), unexpected_call, 0, "?( 1, \"s\" )\nv never().with( any, any )" );
} }
} }
@ -133,13 +135,13 @@ BOOST_FIXTURE_TEST_CASE( triggering_a_once_expectation_calls_unexpected_call_err
mock::function< void() > f; mock::function< void() > f;
f.expect().once(); f.expect().once();
f(); f();
CHECK_ERROR( f(), unexpected_call, 1 ); CHECK_ERROR( f(), unexpected_call, 1, "?()\nv once()" );
} }
{ {
mock::function< void( int, const std::string& ) > f; mock::function< void( int, const std::string& ) > f;
f.expect().once(); f.expect().once();
f( 1, "s" ); f( 1, "s" );
CHECK_ERROR( f( 1, "s" ), unexpected_call, 1 ); CHECK_ERROR( f( 1, "s" ), unexpected_call, 1, "?( 1, \"s\" )\nv once().with( any, any )" );
} }
} }
@ -206,12 +208,12 @@ BOOST_FIXTURE_TEST_CASE( verifying_a_once_expectation_before_the_call_fails, err
{ {
mock::function< void() > f; mock::function< void() > f;
f.expect().once(); f.expect().once();
CHECK_ERROR( BOOST_CHECK( ! f.verify() ), verification_failed, 0 ); CHECK_ERROR( BOOST_CHECK( ! f.verify() ), verification_failed, 0, "?\n. once()" );
} }
{ {
mock::function< int( int, const std::string& ) > f; mock::function< int( int, const std::string& ) > f;
f.expect().once(); f.expect().once();
CHECK_ERROR( BOOST_CHECK( ! f.verify() ), verification_failed, 0 ); CHECK_ERROR( BOOST_CHECK( ! f.verify() ), verification_failed, 0, "?\n. once().with( any, any )" );
} }
} }
@ -223,13 +225,13 @@ BOOST_FIXTURE_TEST_CASE( triggering_a_reset_function_calls_unexpected_call_error
mock::function< void() > f; mock::function< void() > f;
f.expect(); f.expect();
f.reset(); f.reset();
CHECK_ERROR( f(), unexpected_call, 0 ); CHECK_ERROR( f(), unexpected_call, 0, "?()" );
} }
{ {
mock::function< int( int, const std::string& ) > f; mock::function< int( int, const std::string& ) > f;
f.expect(); f.expect();
f.reset(); f.reset();
CHECK_ERROR( f( 1, "s" ), unexpected_call, 0 ); CHECK_ERROR( f( 1, "s" ), unexpected_call, 0, "?( 1, \"s\" )" );
} }
} }
@ -256,12 +258,12 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in
{ {
mock::function< void( int ) > f; mock::function< void( int ) > f;
f.expect().with( 42 ); f.expect().with( 42 );
CHECK_ERROR( f( 43 ), unexpected_call, 0 ); CHECK_ERROR( f( 43 ), unexpected_call, 0, "?( 43 )\n. unlimited().with( 42 )" );
} }
{ {
mock::function< int( int, const std::string& ) > f; mock::function< int( int, const std::string& ) > f;
f.expect().with( 42, "expected" ); f.expect().with( 42, "expected" );
CHECK_ERROR( f( 42, "actual" ), unexpected_call, 0 ); CHECK_ERROR( f( 42, "actual" ), unexpected_call, 0, "?( 42, \"actual\" )\n. unlimited().with( 42, \"expected\" )" );
} }
} }
@ -271,7 +273,7 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in
f.expect().with( mock::equal( 42 ) || mock::less( 42 ) ); f.expect().with( mock::equal( 42 ) || mock::less( 42 ) );
f( 41 ); f( 41 );
f( 42 ); f( 42 );
CHECK_ERROR( f( 43 ), unexpected_call, 2 ); CHECK_ERROR( f( 43 ), unexpected_call, 2, "?( 43 )\n. unlimited().with( ( equal( 42 ) || less( 42 ) ) )" );
} }
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_and_not_less_constraint_calls_unexpected_call_error, error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in_equal_and_not_less_constraint_calls_unexpected_call_error, error_fixture )
@ -279,7 +281,7 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_wrong_parameter_value_in
mock::function< void( int ) > f; mock::function< void( int ) > f;
f.expect().with( mock::equal( 42 ) && ! mock::less( 41 ) ); f.expect().with( mock::equal( 42 ) && ! mock::less( 41 ) );
f( 42 ); f( 42 );
CHECK_ERROR( f( 43 ), unexpected_call, 1 ); CHECK_ERROR( f( 43 ), unexpected_call, 1, "?( 43 )\n. unlimited().with( ( equal( 42 ) && ! less( 41 ) ) )" );
} }
namespace namespace
@ -328,24 +330,22 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_failing_custom_constrain
{ {
mock::function< void( int ) > f; mock::function< void( int ) > f;
f.expect().with( &custom_constraint ); f.expect().with( &custom_constraint );
CHECK_ERROR( f( 42 ), unexpected_call, 0 ); CHECK_ERROR( f( 42 ), unexpected_call, 0, "?( 42 )\n. unlimited().with( ? )" );
} }
{ {
mock::function< int( int, const std::string& ) > f; mock::function< int( int, const std::string& ) > f;
f.expect().with( &custom_constraint, "actual" ); f.expect().with( &custom_constraint, "actual" );
CHECK_ERROR( f( 42, "actual" ), unexpected_call, 0 ); CHECK_ERROR( f( 42, "actual" ), unexpected_call, 0, "?( 42, \"actual\" )\n. unlimited().with( ?, \"actual\" )" );
} }
} }
/* //BOOST_FIXTURE_TEST_CASE( literal_zero_can_be_used_in_place_of_null_pointers_in_constraints, error_fixture )
BOOST_FIXTURE_TEST_CASE( literal_zero_can_be_used_in_place_of_null_pointers_in_constraints, error_fixture ) //{
{ // mock::function< void( int* ) > f;
mock::function< void( int* ) > f; // f.expect().with( 0 );
f.expect().with( 0 ); // f.reset();
f.reset(); // CHECK_CALLS( 1 );
CHECK_CALLS( 1 ); //}
}
*/
// result handling // result handling
@ -354,17 +354,17 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_with_no_return_set_calls_miss
{ {
mock::function< int() > f; mock::function< int() > f;
f.expect(); f.expect();
CHECK_ERROR( f(), missing_action, 0 ); CHECK_ERROR( f(), missing_action, 0, "?()\n. unlimited()" );
} }
{ {
mock::function< int&() > f; mock::function< int&() > f;
f.expect(); f.expect();
CHECK_ERROR( f(), missing_action, 0 ); CHECK_ERROR( f(), missing_action, 0, "?()\n. unlimited()" );
} }
{ {
mock::function< const std::string&() > f; mock::function< const std::string&() > f;
f.expect(); f.expect();
CHECK_ERROR( f(), missing_action, 0 ); CHECK_ERROR( f(), missing_action, 0, "?()\n. unlimited()" );
} }
} }
@ -582,7 +582,7 @@ BOOST_FIXTURE_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_
f.expect().once(); f.expect().once();
f(); f();
f(); f();
CHECK_ERROR( f(), unexpected_call, 2 ); CHECK_ERROR( f(), unexpected_call, 2, "?()\nv once()\nv once()" );
} }
{ {
mock::function< void( const std::string& ) > f; mock::function< void( const std::string& ) > f;
@ -590,7 +590,7 @@ BOOST_FIXTURE_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_
f.expect().once().with( "second" ); f.expect().once().with( "second" );
f( "first" ); f( "first" );
f( "second" ); f( "second" );
CHECK_ERROR( f( "third"), unexpected_call, 2 ); CHECK_ERROR( f( "third"), unexpected_call, 2, "?( \"third\" )\nv once().with( \"first\" )\nv once().with( \"second\" )" );
} }
} }
@ -602,7 +602,7 @@ BOOST_FIXTURE_TEST_CASE( best_expectation_is_selected_first, error_fixture )
f.expect().once().with( 2 ); f.expect().once().with( 2 );
f( 2 ); f( 2 );
f( 1 ); f( 1 );
CHECK_ERROR( f( 3 ), unexpected_call, 2 ); CHECK_ERROR( f( 3 ), unexpected_call, 2, "?( 3 )\nv once().with( 1 )\nv once().with( 2 )" );
} }
{ {
mock::function< void( const std::string& ) > f; mock::function< void( const std::string& ) > f;
@ -610,7 +610,7 @@ BOOST_FIXTURE_TEST_CASE( best_expectation_is_selected_first, error_fixture )
f.expect().once().with( "second" ); f.expect().once().with( "second" );
f( "second" ); f( "second" );
f( "first" ); f( "first" );
CHECK_ERROR( f( "third"), unexpected_call, 2 ); CHECK_ERROR( f( "third"), unexpected_call, 2, "?( \"third\" )\nv once().with( \"first\" )\nv once().with( \"second\" )" );
} }
} }
@ -714,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() > ); std::auto_ptr< mock::function< void() > > f( new mock::function< void() > );
f->expect().once(); f->expect().once();
CHECK_ERROR( f.reset(), untriggered_expectation, 0 ); CHECK_ERROR( f.reset(), untriggered_expectation, 0, "?\n. once()" );
} }
BOOST_FIXTURE_TEST_CASE( verifying_expectation_with_remaining_matches_disables_the_automatic_verification_upon_destruction, error_fixture ) BOOST_FIXTURE_TEST_CASE( verifying_expectation_with_remaining_matches_disables_the_automatic_verification_upon_destruction, error_fixture )
{ {
mock::function< void() > f; mock::function< void() > f;
f.expect().once(); f.expect().once();
CHECK_ERROR( f.verify(), verification_failed, 0 ); CHECK_ERROR( f.verify(), verification_failed, 0, "?\n. once()" );
} }
BOOST_FIXTURE_TEST_CASE( triggering_unexpected_call_call_disables_the_automatic_verification_upon_destruction, error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_unexpected_call_call_disables_the_automatic_verification_upon_destruction, error_fixture )
{ {
mock::function< void() > f; 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 ) 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() > ); 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(); f->expect().once();
CHECK_ERROR( f.reset(), untriggered_expectation, 0 ); CHECK_ERROR( f.reset(), untriggered_expectation, 0, "?\n. once()" );
} }
BOOST_FIXTURE_TEST_CASE( throwing_an_exception_disables_the_automatic_verification_upon_destruction, error_fixture ) BOOST_FIXTURE_TEST_CASE( throwing_an_exception_disables_the_automatic_verification_upon_destruction, error_fixture )

View file

@ -329,6 +329,41 @@ BOOST_AUTO_TEST_CASE( boost_optional_on_base_class_reference_as_return_type_is_s
b.method(); 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 >
bool operator()( Actual ) const
{
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 namespace
{ {
template< typename Expected > template< typename Expected >

View file

@ -0,0 +1,72 @@
//
// 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)
//
#include <turtle/is_comparable.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
namespace
{
struct declared_but_not_defined;
struct non_comparable
{};
struct comparable
{};
bool operator==( const comparable&, int );
bool operator==( int, const comparable& );
struct derived_from_comparable : comparable
{};
struct convertible_to_comparable
{
operator comparable() const;
};
template< typename T >
struct template_comparable
{};
template< typename T >
bool operator==( const template_comparable< T >&, int );
template< typename T >
bool operator==( int, const template_comparable< T >& );
struct comparable_to_everything
{
template< typename T >
bool operator==( const T& ) const;
};
}
BOOST_MPL_ASSERT_NOT(( mock::detail::is_comparable< int, declared_but_not_defined > ));
BOOST_MPL_ASSERT_NOT(( mock::detail::is_comparable< declared_but_not_defined, int > ));
BOOST_MPL_ASSERT_NOT(( mock::detail::is_comparable< int, non_comparable > ));
BOOST_MPL_ASSERT_NOT(( mock::detail::is_comparable< non_comparable, int > ));
BOOST_MPL_ASSERT(( mock::detail::is_comparable< int, comparable > ));
BOOST_MPL_ASSERT(( mock::detail::is_comparable< int, comparable& > ));
BOOST_MPL_ASSERT(( mock::detail::is_comparable< int, const comparable& > ));
BOOST_MPL_ASSERT(( mock::detail::is_comparable< comparable, int > ));
BOOST_MPL_ASSERT(( mock::detail::is_comparable< const comparable&, int > ));
BOOST_MPL_ASSERT(( mock::detail::is_comparable< int, template_comparable< int > > ));
BOOST_MPL_ASSERT(( mock::detail::is_comparable< template_comparable< int >, int > ));
BOOST_MPL_ASSERT(( mock::detail::is_comparable< int, derived_from_comparable > ));
BOOST_MPL_ASSERT(( mock::detail::is_comparable< derived_from_comparable, int > ));
BOOST_MPL_ASSERT(( mock::detail::is_comparable< int, convertible_to_comparable > ));
BOOST_MPL_ASSERT(( mock::detail::is_comparable< convertible_to_comparable, int > ));
BOOST_MPL_ASSERT_NOT(( mock::detail::is_comparable< int, comparable_to_everything > ));
BOOST_MPL_ASSERT(( mock::detail::is_comparable< comparable_to_everything, int > ));

View file

@ -0,0 +1,61 @@
//
// 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)
//
#include <turtle/is_formattable.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
BOOST_STATIC_ASSERT(( ! mock::detail::is_formattable< std::ostream, int >::value ));
namespace
{
struct non_formattable {};
}
BOOST_STATIC_ASSERT(( ! mock::detail::is_formattable< std::ostream, non_formattable >::value ));
namespace
{
struct formattable {};
void format( std::ostream&, const formattable& );
}
BOOST_STATIC_ASSERT(( mock::detail::is_formattable< std::ostream, formattable >::value ));
namespace nm
{
struct formattable {};
}
namespace mock
{
void format( std::ostream&, nm::formattable );
}
BOOST_STATIC_ASSERT(( mock::detail::is_formattable< std::ostream, nm::formattable >::value ));
namespace
{
struct derived_from_formattable : formattable
{};
}
BOOST_STATIC_ASSERT(( mock::detail::is_formattable< std::ostream, derived_from_formattable >::value ));
namespace
{
struct convertible_to_formattable
{
operator formattable() const;
};
}
BOOST_STATIC_ASSERT(( mock::detail::is_formattable< std::ostream, convertible_to_formattable >::value ));

View file

@ -10,12 +10,12 @@
#include <boost/function.hpp> #include <boost/function.hpp>
#include <boost/bind.hpp> #include <boost/bind.hpp>
#ifdef _MSC_VER #ifdef _MSC_VER
# pragma warning( push, 0 ) #pragma warning( push, 0 )
#endif #endif
#include <boost/lambda/lambda.hpp> #include <boost/lambda/lambda.hpp>
#include <boost/spirit/home/phoenix.hpp> #include <boost/spirit/home/phoenix.hpp>
#ifdef _MSC_VER #ifdef _MSC_VER
# pragma warning( pop ) #pragma warning( pop )
#endif #endif
#include <boost/test/auto_unit_test.hpp> #include <boost/test/auto_unit_test.hpp>
@ -24,12 +24,13 @@
namespace namespace
{ {
BOOST_STATIC_ASSERT( sizeof( mock::detail::yes_type ) != sizeof( mock::detail::no_type ) ); struct declared_but_not_defined;
BOOST_MPL_ASSERT_NOT(( mock::detail::is_functor< declared_but_not_defined > ));
template< typename T > template< typename T >
void check( T ) void check( T )
{ {
BOOST_STATIC_ASSERT(( mock::detail::is_functor< T >::type::value )); BOOST_MPL_ASSERT(( mock::detail::is_functor< T > ));
} }
void f0 () {} void f0 () {}

View file

@ -0,0 +1,91 @@
//
// 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)
//
#include <turtle/is_serializable.hpp>
#include <boost/static_assert.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, int >::value ));
BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, std::string >::value ));
namespace
{
struct declared_but_not_defined;
}
BOOST_STATIC_ASSERT(( ! mock::detail::protect::is_serializable< std::ostream, declared_but_not_defined >::value ));
namespace
{
struct non_serializable
{};
}
BOOST_STATIC_ASSERT(( ! mock::detail::protect::is_serializable< std::ostream, non_serializable >::value ));
namespace
{
struct serializable
{};
std::ostream& operator<<( std::ostream& s, const serializable& );
}
BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, serializable >::value ));
namespace
{
template< typename T >
struct template_serializable {};
template< typename T >
std::ostream& operator<<( std::ostream& s, const template_serializable< T >& );
}
BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, template_serializable< int > >::value ));
namespace
{
struct convertible_to_base
{
operator int() const;
};
}
BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, convertible_to_base >::value ));
namespace
{
struct convertible_to_string
{
operator std::string() const;
};
}
BOOST_STATIC_ASSERT(( ! mock::detail::protect::is_serializable< std::ostream, convertible_to_string >::value ));
namespace
{
struct convertible_to_serializable
{
operator serializable() const;
};
}
BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, convertible_to_serializable >::value ));
namespace
{
struct derived_from_serializable : serializable
{};
}
BOOST_STATIC_ASSERT(( mock::detail::protect::is_serializable< std::ostream, derived_from_serializable >::value ));

View file

@ -10,6 +10,7 @@
#define MOCK_TEST_MOCK_ERROR_HPP_INCLUDED #define MOCK_TEST_MOCK_ERROR_HPP_INCLUDED
#include <boost/type_traits/remove_reference.hpp> #include <boost/type_traits/remove_reference.hpp>
#include <boost/lexical_cast.hpp>
#include <string> #include <string>
namespace namespace
@ -20,6 +21,8 @@ namespace
int sequence_failed_count = 0; int sequence_failed_count = 0;
int verification_failed_count = 0; int verification_failed_count = 0;
int untriggered_expectation_count = 0; int untriggered_expectation_count = 0;
std::string last_context;
} }
namespace mock namespace mock
{ {
@ -32,33 +35,45 @@ namespace mock
return r; return r;
} }
static void missing_action( const std::string& /*context*/, template< typename Context >
static void missing_action( const Context& context,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{ {
last_context = boost::lexical_cast< std::string >( context );
++missing_action_count; ++missing_action_count;
} }
static void expected_call( const std::string& /*context*/, template< typename Context >
static void expected_call( const Context& context,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{ {
last_context = boost::lexical_cast< std::string >( context );
++expected_call_count; ++expected_call_count;
} }
static void unexpected_call( const std::string& /*context*/ ) template< typename Context >
static void unexpected_call( const Context& context )
{ {
last_context = boost::lexical_cast< std::string >( context );
++unexpected_call_count; ++unexpected_call_count;
} }
static void sequence_failed( const std::string& /*context*/, template< typename Context >
static void sequence_failed( const Context& context,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{ {
last_context = boost::lexical_cast< std::string >( context );
++sequence_failed_count; ++sequence_failed_count;
} }
static void verification_failed( const std::string& /*context*/, template< typename Context >
static void verification_failed( const Context& context,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{ {
last_context = boost::lexical_cast< std::string >( context );
++verification_failed_count; ++verification_failed_count;
} }
static void untriggered_expectation( const std::string& /*context*/, template< typename Context >
static void untriggered_expectation( const Context& context,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{ {
last_context = boost::lexical_cast< std::string >( context );
++untriggered_expectation_count; ++untriggered_expectation_count;
} }
}; };
@ -67,33 +82,45 @@ namespace mock
{ {
static void abort() static void abort()
{} {}
static void missing_action( const std::string& /*context*/, template< typename Context >
static void missing_action( const Context& context,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{ {
last_context = boost::lexical_cast< std::string >( context );
++missing_action_count; ++missing_action_count;
} }
static void expected_call( const std::string& /*context*/, template< typename Context >
static void expected_call( const Context& context,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{ {
last_context = boost::lexical_cast< std::string >( context );
++expected_call_count; ++expected_call_count;
} }
static void unexpected_call( const std::string& /*context*/ ) template< typename Context >
static void unexpected_call( const Context& context )
{ {
last_context = boost::lexical_cast< std::string >( context );
++unexpected_call_count; ++unexpected_call_count;
} }
static void sequence_failed( const std::string& /*context*/, template< typename Context >
static void sequence_failed( const Context& context,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{ {
last_context = boost::lexical_cast< std::string >( context );
++sequence_failed_count; ++sequence_failed_count;
} }
static void verification_failed( const std::string& /*context*/, template< typename Context >
static void verification_failed( const Context& context,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{ {
last_context = boost::lexical_cast< std::string >( context );
++verification_failed_count; ++verification_failed_count;
} }
static void untriggered_expectation( const std::string& /*context*/, template< typename Context >
static void untriggered_expectation( const Context& context,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{ {
last_context = boost::lexical_cast< std::string >( context );
++untriggered_expectation_count; ++untriggered_expectation_count;
} }
}; };

View file

@ -10,7 +10,6 @@
#define MOCK_TEST_SILENT_ERROR_HPP_INCLUDED #define MOCK_TEST_SILENT_ERROR_HPP_INCLUDED
#include <string> #include <string>
#include <sstream>
#include <stdexcept> #include <stdexcept>
namespace mock namespace mock
@ -22,21 +21,27 @@ namespace mock
{ {
throw std::runtime_error( "abort" ); throw std::runtime_error( "abort" );
} }
static void expected_call( const std::string& /*context*/, template< typename Context >
static void expected_call( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{} {}
static void unexpected_call( const std::string& /*context*/ ) template< typename Context >
static void unexpected_call( const Context& /*context*/ )
{} {}
static void missing_action( const std::string& /*context*/, template< typename Context >
static void missing_action( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{} {}
static void sequence_failed( const std::string& /*context*/, template< typename Context >
static void sequence_failed( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{} {}
static void verification_failed( const std::string& /*context*/, template< typename Context >
static void verification_failed( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{} {}
static void untriggered_expectation( const std::string& /*context*/, template< typename Context >
static void untriggered_expectation( const Context& /*context*/,
const std::string& /*file*/, int /*line*/ ) const std::string& /*file*/, int /*line*/ )
{} {}
}; };