mirror of
https://github.com/mat007/turtle.git
synced 2026-06-22 12:13:43 +00:00
Preview of clang-format changes and CI
This commit is contained in:
parent
bfd1701fcb
commit
805e3b02bf
98 changed files with 6339 additions and 11357 deletions
|
|
@ -15,9 +15,9 @@ class view;
|
|||
class calculator
|
||||
{
|
||||
public:
|
||||
calculator( view& v );
|
||||
calculator(view& v);
|
||||
|
||||
void add( int a, int b ); // the result will be sent to the view 'v'
|
||||
void add(int a, int b); // the result will be sent to the view 'v'
|
||||
};
|
||||
//]
|
||||
|
||||
|
|
|
|||
|
|
@ -14,126 +14,110 @@
|
|||
#include "mock_view.hpp"
|
||||
|
||||
//[ mock_stream_user_type
|
||||
namespace user_namespace
|
||||
{
|
||||
struct user_type
|
||||
{};
|
||||
namespace user_namespace {
|
||||
struct user_type
|
||||
{};
|
||||
|
||||
inline mock::stream& operator<<( mock::stream& s, const user_type& )
|
||||
{
|
||||
return s << "user_type";
|
||||
}
|
||||
inline mock::stream& operator<<(mock::stream& s, const user_type&)
|
||||
{
|
||||
return s << "user_type";
|
||||
}
|
||||
} // namespace user_namespace
|
||||
//]
|
||||
|
||||
namespace custom_constraint_free_function_test
|
||||
{
|
||||
namespace custom_constraint_free_function_test {
|
||||
//[ custom_constraint_free_function
|
||||
bool custom_constraint( int actual )
|
||||
bool custom_constraint(int actual)
|
||||
{
|
||||
return actual == 42;
|
||||
}
|
||||
//]
|
||||
|
||||
//[ custom_constraint_free_function_test
|
||||
BOOST_AUTO_TEST_CASE( forty_one_plus_one_is_forty_two )
|
||||
BOOST_AUTO_TEST_CASE(forty_one_plus_one_is_forty_two)
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
MOCK_EXPECT( v.display ).with( &custom_constraint );
|
||||
c.add( 41, 1 );
|
||||
}
|
||||
//]
|
||||
calculator c(v);
|
||||
MOCK_EXPECT(v.display).with(&custom_constraint);
|
||||
c.add(41, 1);
|
||||
}
|
||||
//]
|
||||
} // namespace custom_constraint_free_function_test
|
||||
|
||||
namespace custom_constraint_functor_test
|
||||
{
|
||||
namespace custom_constraint_functor_test {
|
||||
//[ custom_constraint_functor
|
||||
struct custom_constraint
|
||||
{
|
||||
friend bool operator==( int actual, const custom_constraint& )
|
||||
{
|
||||
return actual == 42;
|
||||
}
|
||||
friend bool operator==(int actual, const custom_constraint&) { return actual == 42; }
|
||||
|
||||
friend std::ostream& operator<<( std::ostream& s, const custom_constraint& )
|
||||
{
|
||||
return s << "_ == 42";
|
||||
}
|
||||
friend std::ostream& operator<<(std::ostream& s, const custom_constraint&) { return s << "_ == 42"; }
|
||||
};
|
||||
//]
|
||||
|
||||
//[ custom_constraint_functor_test
|
||||
BOOST_AUTO_TEST_CASE( forty_one_plus_one_is_forty_two )
|
||||
BOOST_AUTO_TEST_CASE(forty_one_plus_one_is_forty_two)
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
MOCK_EXPECT( v.display ).with( custom_constraint() );
|
||||
c.add( 41, 1 );
|
||||
calculator c(v);
|
||||
MOCK_EXPECT(v.display).with(custom_constraint());
|
||||
c.add(41, 1);
|
||||
}
|
||||
//]
|
||||
}
|
||||
} // namespace custom_constraint_functor_test
|
||||
|
||||
//[ near_constraint
|
||||
template< typename Expected >
|
||||
template<typename Expected>
|
||||
struct near_constraint
|
||||
{
|
||||
near_constraint( Expected expected, Expected threshold )
|
||||
: expected_( expected )
|
||||
, threshold_( threshold )
|
||||
{}
|
||||
near_constraint(Expected expected, Expected threshold) : expected_(expected), threshold_(threshold) {}
|
||||
|
||||
template< typename Actual >
|
||||
bool operator()( Actual actual ) const
|
||||
template<typename Actual>
|
||||
bool operator()(Actual actual) const
|
||||
{
|
||||
return std::abs( actual - boost::unwrap_ref( expected_ ) )
|
||||
< boost::unwrap_ref( threshold_ );
|
||||
return std::abs(actual - boost::unwrap_ref(expected_)) < boost::unwrap_ref(threshold_);
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<( std::ostream& s, const near_constraint& c )
|
||||
friend std::ostream& operator<<(std::ostream& s, const near_constraint& c)
|
||||
{
|
||||
return s << "near( " << mock::format( c.expected_ )
|
||||
<< ", " << mock::format( c.threshold_ ) << " )";
|
||||
return s << "near( " << mock::format(c.expected_) << ", " << mock::format(c.threshold_) << " )";
|
||||
}
|
||||
|
||||
Expected expected_, threshold_;
|
||||
};
|
||||
|
||||
template< typename Expected >
|
||||
mock::constraint< near_constraint< Expected > > near( Expected expected, Expected threshold )
|
||||
template<typename Expected>
|
||||
mock::constraint<near_constraint<Expected>> near(Expected expected, Expected threshold)
|
||||
{
|
||||
return near_constraint< Expected >( expected, threshold );
|
||||
return near_constraint<Expected>(expected, threshold);
|
||||
}
|
||||
//]
|
||||
|
||||
namespace near_constraint_test
|
||||
{
|
||||
namespace near_constraint_test {
|
||||
//[ near_constraint_test
|
||||
BOOST_AUTO_TEST_CASE( forty_one_plus_one_is_forty_two_plus_or_minus_one )
|
||||
BOOST_AUTO_TEST_CASE(forty_one_plus_one_is_forty_two_plus_or_minus_one)
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
MOCK_EXPECT( v.display ).with( near( 42, 1 ) );
|
||||
c.add( 41, 1 );
|
||||
mock_view v;
|
||||
calculator c(v);
|
||||
MOCK_EXPECT(v.display).with(near(42, 1));
|
||||
c.add(41, 1);
|
||||
}
|
||||
//]
|
||||
}
|
||||
} // namespace near_constraint_test
|
||||
|
||||
namespace near_constraint_cref_test
|
||||
{
|
||||
namespace near_constraint_cref_test {
|
||||
//[ near_constraint_cref_test
|
||||
BOOST_AUTO_TEST_CASE( forty_one_plus_one_is_forty_two_plus_or_minus_one )
|
||||
BOOST_AUTO_TEST_CASE(forty_one_plus_one_is_forty_two_plus_or_minus_one)
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
int expected, threshold;
|
||||
MOCK_EXPECT( v.display ).with( near( boost::cref( expected ), boost::cref( threshold ) ) );
|
||||
expected = 42;
|
||||
threshold = 1;
|
||||
c.add( 41, 1 );
|
||||
mock_view v;
|
||||
calculator c(v);
|
||||
int expected, threshold;
|
||||
MOCK_EXPECT(v.display).with(near(boost::cref(expected), boost::cref(threshold)));
|
||||
expected = 42;
|
||||
threshold = 1;
|
||||
c.add(41, 1);
|
||||
}
|
||||
//]
|
||||
}
|
||||
} // namespace near_constraint_cref_test
|
||||
|
||||
#undef MOCK_MAX_ARGS
|
||||
//[ max_args
|
||||
|
|
@ -142,7 +126,7 @@ BOOST_AUTO_TEST_CASE( forty_one_plus_one_is_forty_two_plus_or_minus_one )
|
|||
//]
|
||||
|
||||
//[ custom_policy
|
||||
template< typename Result >
|
||||
template<typename Result>
|
||||
struct custom_policy
|
||||
{
|
||||
static Result abort()
|
||||
|
|
@ -150,17 +134,17 @@ struct custom_policy
|
|||
// Notify the test framework that an error occurs which makes it impossible to continue the test.
|
||||
// This should most likely throw an exception of some kind.
|
||||
}
|
||||
template< typename Context >
|
||||
static void fail( const char* message, const Context& context, const char* file = "unknown location", int line = 0 )
|
||||
template<typename Context>
|
||||
static void fail(const char* message, const Context& context, const char* file = "unknown location", int line = 0)
|
||||
{
|
||||
// Notify the test framework that an unexpected call has occurred.
|
||||
}
|
||||
template< typename Context >
|
||||
static void call( const Context& context, const char* file, int line )
|
||||
template<typename Context>
|
||||
static void call(const Context& context, const char* file, int line)
|
||||
{
|
||||
// Notify the test framework that an expectation has been fulfilled.
|
||||
}
|
||||
static void pass( const char* file, int line )
|
||||
static void pass(const char* file, int line)
|
||||
{
|
||||
// Notify the test framework that the test execution merely passed the given code location.
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,122 +8,113 @@
|
|||
|
||||
//[ prerequisite
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
//]
|
||||
#include "calculator.hpp"
|
||||
#include "mock_view.hpp"
|
||||
|
||||
namespace phases
|
||||
{
|
||||
namespace phases {
|
||||
//[ phases
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero)
|
||||
{
|
||||
mock_view v; // create mock objects
|
||||
calculator c( v ); // create object under test
|
||||
MOCK_EXPECT( v.display ).once().with( 0 ); // configure mock objects
|
||||
c.add( 0, 0 ); // exercise object under test
|
||||
} // verify mock objects
|
||||
mock_view v; // create mock objects
|
||||
calculator c(v); // create object under test
|
||||
MOCK_EXPECT(v.display).once().with(0); // configure mock objects
|
||||
c.add(0, 0); // exercise object under test
|
||||
} // verify mock objects
|
||||
//]
|
||||
}
|
||||
} // namespace phases
|
||||
|
||||
namespace verify_reset
|
||||
{
|
||||
namespace verify_reset {
|
||||
//[ verify_reset
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero)
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
MOCK_EXPECT( v.display ).once().with( 0 );
|
||||
c.add( 0, 0 );
|
||||
MOCK_VERIFY( v.display ); // verify all expectations are fulfilled for the 'display' method
|
||||
mock::verify( v ); // verify all expectations are fulfilled for all methods of 'v'
|
||||
mock::verify(); // verify all expectations are fulfilled for all existing mock objects
|
||||
MOCK_RESET( v.display ); // reset all expectations for the 'display' method
|
||||
mock::reset( v ); // reset all expectations for all methods of 'v'
|
||||
mock::reset(); // reset all expectations for all existing mock objects
|
||||
} // automatically verify all expectations are fulfilled for all mock objects going out of scope
|
||||
calculator c(v);
|
||||
MOCK_EXPECT(v.display).once().with(0);
|
||||
c.add(0, 0);
|
||||
MOCK_VERIFY(v.display); // verify all expectations are fulfilled for the 'display' method
|
||||
mock::verify(v); // verify all expectations are fulfilled for all methods of 'v'
|
||||
mock::verify(); // verify all expectations are fulfilled for all existing mock objects
|
||||
MOCK_RESET(v.display); // reset all expectations for the 'display' method
|
||||
mock::reset(v); // reset all expectations for all methods of 'v'
|
||||
mock::reset(); // reset all expectations for all existing mock objects
|
||||
} // automatically verify all expectations are fulfilled for all mock objects going out of scope
|
||||
//]
|
||||
}
|
||||
} // namespace verify_reset
|
||||
|
||||
namespace expectations
|
||||
{
|
||||
namespace expectations {
|
||||
//[ expectations
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero)
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
MOCK_EXPECT( v.display ).once().with( 0 ); // this call must occur once (and only once)
|
||||
MOCK_EXPECT( v.display ).with( 1 ); // this call can occur any number of times (including never)
|
||||
c.add( 0, 0 );
|
||||
calculator c(v);
|
||||
MOCK_EXPECT(v.display).once().with(0); // this call must occur once (and only once)
|
||||
MOCK_EXPECT(v.display).with(1); // this call can occur any number of times (including never)
|
||||
c.add(0, 0);
|
||||
}
|
||||
//]
|
||||
}
|
||||
} // namespace expectations
|
||||
|
||||
namespace sequence
|
||||
{
|
||||
namespace sequence {
|
||||
//[ sequence
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero)
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
calculator c(v);
|
||||
mock::sequence s;
|
||||
MOCK_EXPECT( v.display ).once().with( 0 ).in( s ); // add this expectation to the sequence
|
||||
MOCK_EXPECT( v.display ).with( 1 ).in( s ); // add this expectation to the sequence after the previous call
|
||||
c.add( 0, 0 );
|
||||
c.add( 1, 0 );
|
||||
MOCK_EXPECT(v.display).once().with(0).in(s); // add this expectation to the sequence
|
||||
MOCK_EXPECT(v.display).with(1).in(s); // add this expectation to the sequence after the previous call
|
||||
c.add(0, 0);
|
||||
c.add(1, 0);
|
||||
}
|
||||
//]
|
||||
}
|
||||
} // namespace sequence
|
||||
|
||||
namespace several_sequences
|
||||
{
|
||||
namespace several_sequences {
|
||||
//[ several_sequences
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero)
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
calculator c(v);
|
||||
mock::sequence s1, s2;
|
||||
MOCK_EXPECT( v.display ).once().with( 0 ).in( s1 );
|
||||
MOCK_EXPECT( v.display ).once().with( 1 ).in( s2 );
|
||||
MOCK_EXPECT( v.display ).with( 2 ).in( s1, s2 ); // add this expectation to both sequences after the previous calls
|
||||
c.add( 0, 0 );
|
||||
c.add( 1, 0 );
|
||||
c.add( 1, 1 );
|
||||
c.add( 2, 0 );
|
||||
MOCK_EXPECT(v.display).once().with(0).in(s1);
|
||||
MOCK_EXPECT(v.display).once().with(1).in(s2);
|
||||
MOCK_EXPECT(v.display).with(2).in(s1, s2); // add this expectation to both sequences after the previous calls
|
||||
c.add(0, 0);
|
||||
c.add(1, 0);
|
||||
c.add(1, 1);
|
||||
c.add(2, 0);
|
||||
}
|
||||
//]
|
||||
}
|
||||
} // namespace several_sequences
|
||||
|
||||
namespace action
|
||||
{
|
||||
namespace action {
|
||||
//[ action_view
|
||||
class view
|
||||
{
|
||||
public:
|
||||
virtual bool display( int result ) = 0; // returns a boolean
|
||||
virtual bool display(int result) = 0; // returns a boolean
|
||||
};
|
||||
//]
|
||||
|
||||
MOCK_BASE_CLASS( mock_view, view )
|
||||
{
|
||||
MOCK_METHOD( display, 1 )
|
||||
};
|
||||
MOCK_BASE_CLASS(mock_view, view){MOCK_METHOD(display, 1)};
|
||||
|
||||
class calculator
|
||||
{
|
||||
public:
|
||||
calculator( view& v );
|
||||
calculator(view& v);
|
||||
|
||||
void add( int a, int b );
|
||||
void add(int a, int b);
|
||||
};
|
||||
//[ action_test
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero)
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
MOCK_EXPECT( v.display ).once().with( 0 ); // missing returns( true )
|
||||
c.add( 0, 0 );
|
||||
mock_view v;
|
||||
calculator c(v);
|
||||
MOCK_EXPECT(v.display).once().with(0); // missing returns( true )
|
||||
c.add(0, 0);
|
||||
}
|
||||
//]
|
||||
}
|
||||
} // namespace action
|
||||
|
|
|
|||
|
|
@ -7,42 +7,36 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
//[ limitations_comma_in_macro_problem
|
||||
template< typename T1, typename T2 >
|
||||
struct my_base_class
|
||||
{};
|
||||
template<typename T1, typename T2>
|
||||
struct my_base_class
|
||||
{};
|
||||
//]
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace limitations_comma_in_macro_solution_1
|
||||
{
|
||||
namespace limitations_comma_in_macro_solution_1 {
|
||||
//[ limitations_comma_in_macro_solution_1
|
||||
typedef my_base_class< int, int > my_base_type;
|
||||
|
||||
MOCK_BASE_CLASS( my_mock, my_base_type )
|
||||
{};
|
||||
//]
|
||||
}
|
||||
typedef my_base_class<int, int> my_base_type;
|
||||
|
||||
namespace limitations_comma_in_macro_solution_2
|
||||
{
|
||||
MOCK_BASE_CLASS(my_mock, my_base_type){};
|
||||
//]
|
||||
} // namespace limitations_comma_in_macro_solution_1
|
||||
|
||||
namespace limitations_comma_in_macro_solution_2 {
|
||||
//[ limitations_comma_in_macro_solution_2
|
||||
template< typename T1, typename T2 >
|
||||
MOCK_BASE_CLASS( my_mock, my_base_class< T1 BOOST_PP_COMMA() T2 > )
|
||||
{};
|
||||
template<typename T1, typename T2>
|
||||
MOCK_BASE_CLASS(my_mock, my_base_class<T1 BOOST_PP_COMMA() T2>){};
|
||||
//]
|
||||
}
|
||||
} // namespace limitations_comma_in_macro_solution_2
|
||||
|
||||
namespace limitations_comma_in_macro_solution_3
|
||||
{
|
||||
namespace limitations_comma_in_macro_solution_3 {
|
||||
//[ limitations_comma_in_macro_solution_3
|
||||
template< typename T1, typename T2 >
|
||||
struct my_mock : my_base_class< T1, T2 >, mock::object
|
||||
{};
|
||||
template<typename T1, typename T2>
|
||||
struct my_mock : my_base_class<T1, T2>, mock::object
|
||||
{};
|
||||
//]
|
||||
}
|
||||
} // namespace limitations_comma_in_macro_solution_3
|
||||
|
|
|
|||
|
|
@ -7,44 +7,37 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
//[ limitations_const_parameter_warning_problem
|
||||
class base
|
||||
{
|
||||
public:
|
||||
virtual void method( const int ) = 0;
|
||||
};
|
||||
//]
|
||||
}
|
||||
|
||||
namespace limitations_const_parameter_warning_explanation
|
||||
class base
|
||||
{
|
||||
public:
|
||||
virtual void method(const int) = 0;
|
||||
};
|
||||
//]
|
||||
} // namespace
|
||||
|
||||
namespace limitations_const_parameter_warning_explanation {
|
||||
//[ limitations_const_parameter_warning_explanation
|
||||
class derived : public base
|
||||
{
|
||||
public:
|
||||
virtual void method( const int );
|
||||
};
|
||||
|
||||
void derived::method( int )
|
||||
{}
|
||||
//]
|
||||
}
|
||||
|
||||
namespace limitations_const_parameter_warning_solution
|
||||
class derived : public base
|
||||
{
|
||||
public:
|
||||
virtual void method(const int);
|
||||
};
|
||||
|
||||
void derived::method(int) {}
|
||||
//]
|
||||
} // namespace limitations_const_parameter_warning_explanation
|
||||
|
||||
namespace limitations_const_parameter_warning_solution {
|
||||
//[ limitations_const_parameter_warning_solution
|
||||
MOCK_BASE_CLASS( mock_base, base )
|
||||
{
|
||||
void method( const int i )
|
||||
{
|
||||
method_stub( i );
|
||||
}
|
||||
MOCK_METHOD( method_stub, 1, void( int ), method )
|
||||
};
|
||||
MOCK_BASE_CLASS(mock_base, base){void method(const int i){method_stub(i);
|
||||
} // namespace limitations_const_parameter_warning_solution
|
||||
MOCK_METHOD(method_stub, 1, void(int), method)
|
||||
}
|
||||
;
|
||||
//]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,37 +7,33 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
//[ limitations_literal_zero_problem
|
||||
class base
|
||||
{
|
||||
public:
|
||||
virtual void method( int* i ) = 0;
|
||||
};
|
||||
class base
|
||||
{
|
||||
public:
|
||||
virtual void method(int* i) = 0;
|
||||
};
|
||||
|
||||
MOCK_BASE_CLASS( mock_base, base )
|
||||
{
|
||||
MOCK_METHOD( method, 1 )
|
||||
};
|
||||
MOCK_BASE_CLASS(mock_base, base){MOCK_METHOD(method, 1)};
|
||||
//]
|
||||
}
|
||||
} // namespace
|
||||
|
||||
BOOST_AUTO_TEST_CASE( literal_zero )
|
||||
BOOST_AUTO_TEST_CASE(literal_zero)
|
||||
{
|
||||
mock_base m;
|
||||
//[ limitations_literal_zero_solution_1
|
||||
MOCK_EXPECT( m.method ).with( mock::equal< int* >( 0 ) ); // this compiles
|
||||
//]
|
||||
//[ limitations_literal_zero_solution_2
|
||||
MOCK_EXPECT( m.method ).with( mock::negate );
|
||||
//[ limitations_literal_zero_solution_1
|
||||
MOCK_EXPECT(m.method).with(mock::equal<int*>(0)); // this compiles
|
||||
//]
|
||||
//[ limitations_literal_zero_solution_2
|
||||
MOCK_EXPECT(m.method).with(mock::negate);
|
||||
//]
|
||||
#ifdef MOCK_NULLPTR
|
||||
//[ limitations_literal_zero_solution_3
|
||||
MOCK_EXPECT( m.method ).with( nullptr );
|
||||
//[ limitations_literal_zero_solution_3
|
||||
MOCK_EXPECT(m.method).with(nullptr);
|
||||
//]
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
|
||||
//[ limitations_non_virtual_method_problem
|
||||
class base
|
||||
|
|
@ -20,8 +20,5 @@ public:
|
|||
//]
|
||||
|
||||
//[ limitations_non_virtual_method_problem_2
|
||||
MOCK_BASE_CLASS( mock_base, base )
|
||||
{
|
||||
MOCK_METHOD( method, 0 )
|
||||
};
|
||||
MOCK_BASE_CLASS(mock_base, base){MOCK_METHOD(method, 0)};
|
||||
//]
|
||||
|
|
|
|||
|
|
@ -7,26 +7,22 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
//[ limitations_protected_private_method_problem
|
||||
class base
|
||||
{
|
||||
protected:
|
||||
virtual void method_1() = 0;
|
||||
private:
|
||||
virtual void method_2() = 0;
|
||||
};
|
||||
class base
|
||||
{
|
||||
protected:
|
||||
virtual void method_1() = 0;
|
||||
|
||||
private:
|
||||
virtual void method_2() = 0;
|
||||
};
|
||||
//]
|
||||
|
||||
//[ limitations_protected_private_method_solution
|
||||
MOCK_BASE_CLASS( mock_base, base )
|
||||
{
|
||||
MOCK_METHOD( method_1, 0, void() )
|
||||
MOCK_METHOD( method_2, 0, void() )
|
||||
};
|
||||
MOCK_BASE_CLASS(mock_base, base){MOCK_METHOD(method_1, 0, void()) MOCK_METHOD(method_2, 0, void())};
|
||||
//]
|
||||
}
|
||||
} // namespace
|
||||
|
|
|
|||
|
|
@ -7,28 +7,23 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
//[ limitations_template_base_class_method_problem
|
||||
template< typename T >
|
||||
class base
|
||||
{
|
||||
public:
|
||||
virtual ~base()
|
||||
{}
|
||||
template<typename T>
|
||||
class base
|
||||
{
|
||||
public:
|
||||
virtual ~base() {}
|
||||
|
||||
virtual void method() = 0;
|
||||
};
|
||||
virtual void method() = 0;
|
||||
};
|
||||
//]
|
||||
|
||||
|
||||
//[ limitations_template_base_class_method_solution
|
||||
template< typename T >
|
||||
MOCK_BASE_CLASS( mock_base, base< T > )
|
||||
{
|
||||
MOCK_METHOD( method, 1, void() )
|
||||
};
|
||||
template<typename T>
|
||||
MOCK_BASE_CLASS(mock_base, base<T>){MOCK_METHOD(method, 1, void())};
|
||||
//]
|
||||
}
|
||||
} // namespace
|
||||
|
|
|
|||
|
|
@ -7,77 +7,72 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
|
||||
namespace limitations_template_method_problem
|
||||
{
|
||||
namespace limitations_template_method_problem {
|
||||
//[ limitations_template_method_problem
|
||||
class concept
|
||||
{
|
||||
public:
|
||||
template< typename T >
|
||||
void method( T t )
|
||||
template<typename T>
|
||||
void method(T t)
|
||||
{}
|
||||
};
|
||||
|
||||
template< typename T >
|
||||
void function_under_test( T t ) // T is supposed to model the previous concept
|
||||
template<typename T>
|
||||
void function_under_test(T t) // T is supposed to model the previous concept
|
||||
{
|
||||
t.method( 42 );
|
||||
t.method( "string" );
|
||||
t.method(42);
|
||||
t.method("string");
|
||||
}
|
||||
//]
|
||||
|
||||
//[ limitations_template_method_solution
|
||||
MOCK_CLASS( mock_concept )
|
||||
{
|
||||
MOCK_METHOD( method, 1, void( int ), method_int )
|
||||
MOCK_METHOD( method, 1, void( const char* ), method_string )
|
||||
};
|
||||
//]
|
||||
}
|
||||
|
||||
namespace limitations_template_method_problem_2
|
||||
{
|
||||
//[ limitations_template_method_solution
|
||||
MOCK_CLASS(mock_concept){MOCK_METHOD(method, 1, void(int), method_int)
|
||||
MOCK_METHOD(method, 1, void(const char*), method_string)};
|
||||
//]
|
||||
} // namespace limitations_template_method_problem
|
||||
|
||||
namespace limitations_template_method_problem_2 {
|
||||
//[ limitations_template_method_problem_2
|
||||
class concept
|
||||
{
|
||||
public:
|
||||
template< typename T >
|
||||
template<typename T>
|
||||
T create()
|
||||
{
|
||||
return T();
|
||||
}
|
||||
};
|
||||
|
||||
template< typename T >
|
||||
void function_under_test( T t ) // T is supposed to model the previous concept
|
||||
template<typename T>
|
||||
void function_under_test(T t) // T is supposed to model the previous concept
|
||||
{
|
||||
t.template create< int >();
|
||||
t.template create< std::string >();
|
||||
t.template create<int>();
|
||||
t.template create<std::string>();
|
||||
}
|
||||
//]
|
||||
|
||||
|
||||
//[ limitations_template_method_solution_2
|
||||
MOCK_CLASS( mock_concept )
|
||||
MOCK_CLASS(mock_concept)
|
||||
{
|
||||
template< typename T >
|
||||
template<typename T>
|
||||
T create();
|
||||
|
||||
MOCK_METHOD( create_int, 0, int(), create_int )
|
||||
MOCK_METHOD( create_string, 0, std::string(), create_string )
|
||||
MOCK_METHOD(create_int, 0, int(), create_int)
|
||||
MOCK_METHOD(create_string, 0, std::string(), create_string)
|
||||
};
|
||||
|
||||
template<>
|
||||
int mock_concept::create< int >()
|
||||
int mock_concept::create<int>()
|
||||
{
|
||||
return create_int();
|
||||
}
|
||||
template<>
|
||||
std::string mock_concept::create< std::string >()
|
||||
std::string mock_concept::create<std::string>()
|
||||
{
|
||||
return create_string();
|
||||
}
|
||||
//]
|
||||
}
|
||||
} // namespace limitations_template_method_problem_2
|
||||
|
|
|
|||
|
|
@ -7,29 +7,24 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
//[ limitations_throw_specifier_problem
|
||||
struct base_class
|
||||
{
|
||||
virtual ~base_class()
|
||||
{}
|
||||
struct base_class
|
||||
{
|
||||
virtual ~base_class() {}
|
||||
|
||||
virtual void method() throw ();
|
||||
};
|
||||
virtual void method() throw();
|
||||
};
|
||||
//]
|
||||
|
||||
//[ limitations_throw_specifier_solution
|
||||
MOCK_BASE_CLASS( mock_class, base_class )
|
||||
{
|
||||
void method() throw ()
|
||||
{
|
||||
method_proxy();
|
||||
}
|
||||
MOCK_METHOD( method_proxy, 0, void(), method )
|
||||
};
|
||||
MOCK_BASE_CLASS(mock_class, base_class){void method() throw(){method_proxy();
|
||||
} // namespace
|
||||
MOCK_METHOD(method_proxy, 0, void(), method)
|
||||
}
|
||||
;
|
||||
//]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,13 +9,13 @@
|
|||
#ifndef MOCK_VIEW
|
||||
#define MOCK_VIEW
|
||||
|
||||
#include <turtle/mock.hpp>
|
||||
#include "view.hpp"
|
||||
#include <turtle/mock.hpp>
|
||||
|
||||
//[ mock_view
|
||||
MOCK_BASE_CLASS( mock_view, view ) // declare a 'mock_view' class implementing 'view'
|
||||
MOCK_BASE_CLASS(mock_view, view) // declare a 'mock_view' class implementing 'view'
|
||||
{
|
||||
MOCK_METHOD( display, 1 ) // implement the 'display' method from 'view' (taking 1 argument)
|
||||
MOCK_METHOD(display, 1) // implement the 'display' method from 'view' (taking 1 argument)
|
||||
};
|
||||
//]
|
||||
|
||||
|
|
|
|||
|
|
@ -7,40 +7,36 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include "calculator.hpp"
|
||||
#include "mock_view.hpp"
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
|
||||
namespace simple
|
||||
{
|
||||
namespace simple {
|
||||
//[ simple_calculator
|
||||
class calculator
|
||||
{
|
||||
public:
|
||||
int add( int a, int b );
|
||||
int add(int a, int b);
|
||||
};
|
||||
//]
|
||||
|
||||
//[ simple_zero_plus_zero_is_zero
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero)
|
||||
{
|
||||
calculator c;
|
||||
BOOST_CHECK_EQUAL( 0, c.add( 0, 0 ) );
|
||||
BOOST_CHECK_EQUAL(0, c.add(0, 0));
|
||||
}
|
||||
//]
|
||||
}
|
||||
} // namespace simple
|
||||
|
||||
namespace without_mock_object
|
||||
{
|
||||
namespace without_mock_object {
|
||||
//[ my_view
|
||||
class my_view : public view
|
||||
{
|
||||
public:
|
||||
my_view()
|
||||
: called( false )
|
||||
{}
|
||||
virtual void display( int result )
|
||||
my_view() : called(false) {}
|
||||
virtual void display(int result)
|
||||
{
|
||||
called = true;
|
||||
value = result;
|
||||
|
|
@ -51,26 +47,26 @@ public:
|
|||
//]
|
||||
|
||||
//[ zero_plus_zero_is_zero_without_mock_object
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero)
|
||||
{
|
||||
my_view v;
|
||||
calculator c( v );
|
||||
c.add( 0, 0 );
|
||||
BOOST_REQUIRE( v.called );
|
||||
BOOST_CHECK_EQUAL( 0, v.value );
|
||||
calculator c(v);
|
||||
c.add(0, 0);
|
||||
BOOST_REQUIRE(v.called);
|
||||
BOOST_CHECK_EQUAL(0, v.value);
|
||||
}
|
||||
//]
|
||||
}
|
||||
} // namespace without_mock_object
|
||||
|
||||
namespace with_mock_object
|
||||
{
|
||||
namespace with_mock_object {
|
||||
//[ zero_plus_zero_is_zero_with_mock_object
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero)
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
MOCK_EXPECT( v.display ).once().with( 0 ); // expect the 'display' method to be called once (and only once) with a parameter value equal to 0
|
||||
c.add( 0, 0 );
|
||||
calculator c(v);
|
||||
MOCK_EXPECT(v.display).once().with(
|
||||
0); // expect the 'display' method to be called once (and only once) with a parameter value equal to 0
|
||||
c.add(0, 0);
|
||||
}
|
||||
//]
|
||||
}
|
||||
} // namespace with_mock_object
|
||||
|
|
|
|||
|
|
@ -7,67 +7,62 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
//[ async_call_problem
|
||||
namespace
|
||||
namespace {
|
||||
class base_class
|
||||
{
|
||||
class base_class
|
||||
{
|
||||
public:
|
||||
virtual void method() = 0;
|
||||
};
|
||||
public:
|
||||
virtual void method() = 0;
|
||||
};
|
||||
|
||||
class my_class
|
||||
{
|
||||
public:
|
||||
explicit my_class( base_class& );
|
||||
class my_class
|
||||
{
|
||||
public:
|
||||
explicit my_class(base_class&);
|
||||
|
||||
void flush(); // repetitively calling this method will in turn call base_class::method at some point
|
||||
};
|
||||
}
|
||||
void flush(); // repetitively calling this method will in turn call base_class::method at some point
|
||||
};
|
||||
} // namespace
|
||||
//]
|
||||
|
||||
//[ async_call_solution
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <boost/lambda/lambda.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/lambda/lambda.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
|
||||
namespace
|
||||
namespace {
|
||||
template<typename F>
|
||||
void check(bool& condition, F flush, int attempts = 100, int sleep = 100)
|
||||
{
|
||||
template< typename F >
|
||||
void check( bool& condition, F flush, int attempts = 100, int sleep = 100 )
|
||||
while(!condition && attempts > 0)
|
||||
{
|
||||
while( !condition && attempts > 0 )
|
||||
{
|
||||
--attempts;
|
||||
boost::this_thread::sleep( boost::posix_time::milliseconds( sleep ) );
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
MOCK_BASE_CLASS( mock_base_class, base_class )
|
||||
{
|
||||
MOCK_METHOD( method, 0 )
|
||||
};
|
||||
void set_bool(bool& b)
|
||||
{
|
||||
b = true;
|
||||
--attempts;
|
||||
boost::this_thread::sleep(boost::posix_time::milliseconds(sleep));
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( method_is_called )
|
||||
MOCK_BASE_CLASS(mock_base_class, base_class){MOCK_METHOD(method, 0)};
|
||||
void set_bool(bool& b)
|
||||
{
|
||||
b = true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
BOOST_AUTO_TEST_CASE(method_is_called)
|
||||
{
|
||||
mock_base_class m;
|
||||
my_class c( m );
|
||||
my_class c(m);
|
||||
bool done = false;
|
||||
// when method is called it will set done to true
|
||||
// Note: Boost 1.57 introduced a bug preventing usage of the lambda with clang in C++98
|
||||
// See: https://svn.boost.org/trac10/ticket/10785
|
||||
#if defined(BOOST_CLANG) && (BOOST_VERSION >= 105700)
|
||||
MOCK_EXPECT( m.method ).once().calls( boost::bind(&set_bool, done) );
|
||||
MOCK_EXPECT(m.method).once().calls(boost::bind(&set_bool, done));
|
||||
#else
|
||||
MOCK_EXPECT( m.method ).once().calls( boost::lambda::var( done ) = true );
|
||||
MOCK_EXPECT(m.method).once().calls(boost::lambda::var(done) = true);
|
||||
#endif
|
||||
check( done, boost::bind( &my_class::flush, &c ) ); // just wait on done, flushing from time to time
|
||||
check(done, boost::bind(&my_class::flush, &c)); // just wait on done, flushing from time to time
|
||||
}
|
||||
//]
|
||||
|
|
|
|||
|
|
@ -9,36 +9,32 @@
|
|||
//[ invoke_functor_problem
|
||||
#include <boost/function.hpp>
|
||||
|
||||
namespace
|
||||
namespace {
|
||||
class base_class
|
||||
{
|
||||
class base_class
|
||||
{
|
||||
public:
|
||||
virtual void method( const boost::function< void( int ) >& functor ) = 0;
|
||||
};
|
||||
public:
|
||||
virtual void method(const boost::function<void(int)>& functor) = 0;
|
||||
};
|
||||
|
||||
void function( base_class& ); // the function will call 'method' with a functor to be applied
|
||||
}
|
||||
void function(base_class&); // the function will call 'method' with a functor to be applied
|
||||
} // namespace
|
||||
//]
|
||||
|
||||
//[ invoke_functor_solution
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <boost/bind/apply.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/bind/apply.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
MOCK_BASE_CLASS( mock_class, base_class )
|
||||
{
|
||||
MOCK_METHOD( method, 1 )
|
||||
};
|
||||
namespace {
|
||||
MOCK_BASE_CLASS(mock_class, base_class){MOCK_METHOD(method, 1)};
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( how_to_invoke_a_functor_passed_as_parameter_of_a_mock_method )
|
||||
BOOST_AUTO_TEST_CASE(how_to_invoke_a_functor_passed_as_parameter_of_a_mock_method)
|
||||
{
|
||||
mock_class mock;
|
||||
MOCK_EXPECT( mock.method ).calls( boost::bind( boost::apply< void >(), _1, 42 ) ); // whenever 'method' is called, invoke the functor with 42
|
||||
function( mock );
|
||||
MOCK_EXPECT(mock.method)
|
||||
.calls(boost::bind(boost::apply<void>(), _1, 42)); // whenever 'method' is called, invoke the functor with 42
|
||||
function(mock);
|
||||
}
|
||||
//]
|
||||
|
|
|
|||
|
|
@ -8,31 +8,27 @@
|
|||
|
||||
//[ quick_constraint_problem
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <iostream>
|
||||
|
||||
namespace
|
||||
namespace {
|
||||
class my_class
|
||||
{
|
||||
class my_class
|
||||
{
|
||||
public:
|
||||
explicit my_class( int data )
|
||||
: data_( data )
|
||||
{}
|
||||
int data_;
|
||||
};
|
||||
public:
|
||||
explicit my_class(int data) : data_(data) {}
|
||||
int data_;
|
||||
};
|
||||
|
||||
std::ostream& operator<<( std::ostream& os, const my_class& c ) // my_class is serializable to an std::ostream
|
||||
{
|
||||
return os << "my_class( " << c.data_ << " )";
|
||||
}
|
||||
|
||||
MOCK_CLASS( my_mock )
|
||||
{
|
||||
MOCK_METHOD( method, 1, void( const my_class& ) ) // how to simply write a custom constraint ?
|
||||
};
|
||||
std::ostream& operator<<(std::ostream& os, const my_class& c) // my_class is serializable to an std::ostream
|
||||
{
|
||||
return os << "my_class( " << c.data_ << " )";
|
||||
}
|
||||
|
||||
MOCK_CLASS(my_mock){
|
||||
MOCK_METHOD(method, 1, void(const my_class&)) // how to simply write a custom constraint ?
|
||||
};
|
||||
} // namespace
|
||||
//]
|
||||
|
||||
//[ quick_constraint_solution
|
||||
|
|
@ -40,16 +36,19 @@ namespace
|
|||
|
||||
namespace // in the same namespace as 'my_class'
|
||||
{
|
||||
bool operator==( const my_class& actual, const std::string& expected ) // the first part of the trick is to compare to a string
|
||||
{
|
||||
return boost::lexical_cast< std::string >( actual ) == expected;
|
||||
}
|
||||
} // mock
|
||||
bool operator==(const my_class& actual,
|
||||
const std::string& expected) // the first part of the trick is to compare to a string
|
||||
{
|
||||
return boost::lexical_cast<std::string>(actual) == expected;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
BOOST_AUTO_TEST_CASE( method_is_called )
|
||||
BOOST_AUTO_TEST_CASE(method_is_called)
|
||||
{
|
||||
my_mock mock;
|
||||
MOCK_EXPECT( mock.method ).once().with( "my_class( 42 )" ); // the second part of the trick is to express the constraint as a string
|
||||
mock.method( my_class( 42 ) );
|
||||
MOCK_EXPECT(mock.method)
|
||||
.once()
|
||||
.with("my_class( 42 )"); // the second part of the trick is to express the constraint as a string
|
||||
mock.method(my_class(42));
|
||||
}
|
||||
//]
|
||||
|
|
|
|||
|
|
@ -7,44 +7,43 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
//[ retrieve_cref_problem
|
||||
namespace
|
||||
namespace {
|
||||
class base_class
|
||||
{
|
||||
class base_class
|
||||
{
|
||||
public:
|
||||
virtual void method( int value ) = 0;
|
||||
};
|
||||
public:
|
||||
virtual void method(int value) = 0;
|
||||
};
|
||||
|
||||
class my_class
|
||||
{
|
||||
public:
|
||||
explicit my_class( base_class& );
|
||||
class my_class
|
||||
{
|
||||
public:
|
||||
explicit my_class(base_class&);
|
||||
|
||||
void process(); // the processing will call 'method' two times with the same value, but we don't know what value beforehand
|
||||
};
|
||||
}
|
||||
void process(); // the processing will call 'method' two times with the same value, but we don't know what value
|
||||
// beforehand
|
||||
};
|
||||
} // namespace
|
||||
//]
|
||||
|
||||
//[ retrieve_cref_solution
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
MOCK_BASE_CLASS( mock_base_class, base_class )
|
||||
{
|
||||
MOCK_METHOD( method, 1 )
|
||||
};
|
||||
namespace {
|
||||
MOCK_BASE_CLASS(mock_base_class, base_class){MOCK_METHOD(method, 1)};
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( method_is_called_two_times_with_the_same_value )
|
||||
BOOST_AUTO_TEST_CASE(method_is_called_two_times_with_the_same_value)
|
||||
{
|
||||
mock_base_class mock;
|
||||
my_class c( mock );
|
||||
my_class c(mock);
|
||||
int value;
|
||||
MOCK_EXPECT( mock.method ).once().with( mock::retrieve( value ) ); // on first call retrieve the value, this expectation takes precedence because it can never fail
|
||||
MOCK_EXPECT( mock.method ).once().with( boost::cref( value ) ); // on second call compare the previously retrieved value with the newly received one
|
||||
MOCK_EXPECT(mock.method).once().with(mock::retrieve(value)); // on first call retrieve the value, this expectation
|
||||
// takes precedence because it can never fail
|
||||
MOCK_EXPECT(mock.method)
|
||||
.once()
|
||||
.with(boost::cref(value)); // on second call compare the previously retrieved value with the newly received one
|
||||
c.process();
|
||||
}
|
||||
//]
|
||||
|
|
|
|||
|
|
@ -8,33 +8,31 @@
|
|||
|
||||
//[ static_objects_problem
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <ostream>
|
||||
|
||||
namespace
|
||||
namespace {
|
||||
struct my_class
|
||||
{
|
||||
struct my_class
|
||||
{
|
||||
my_class( int i )
|
||||
: i_( i )
|
||||
{}
|
||||
my_class(int i) : i_(i) {}
|
||||
|
||||
int i_;
|
||||
};
|
||||
int i_;
|
||||
};
|
||||
|
||||
std::ostream& operator<<( std::ostream& os, const my_class* c )
|
||||
{
|
||||
return os << "my_class " << c->i_; // the 'c' pointer must be valid when logging
|
||||
}
|
||||
|
||||
MOCK_FUNCTION( f, 1, void( my_class* ) ) // being static 'f' outlive the test case
|
||||
std::ostream& operator<<(std::ostream& os, const my_class* c)
|
||||
{
|
||||
return os << "my_class " << c->i_; // the 'c' pointer must be valid when logging
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( static_objects_problem )
|
||||
MOCK_FUNCTION(f, 1, void(my_class*)) // being static 'f' outlive the test case
|
||||
} // namespace
|
||||
|
||||
BOOST_AUTO_TEST_CASE(static_objects_problem)
|
||||
{
|
||||
my_class c( 42 );
|
||||
MOCK_EXPECT( f ).once().with( &c ); // the set expectation will also outlive the test case and leak into other test cases using 'f'
|
||||
my_class c(42);
|
||||
MOCK_EXPECT(f).once().with(
|
||||
&c); // the set expectation will also outlive the test case and leak into other test cases using 'f'
|
||||
} // the 'c' instance goes out of scope and the '&c' pointer becomes dangling
|
||||
//]
|
||||
|
||||
|
|
@ -43,25 +41,28 @@ struct fixture
|
|||
{
|
||||
~fixture()
|
||||
{
|
||||
mock::reset(); // the use of a fixture ensures the reset will prevent the expectations from leaking into other test cases
|
||||
mock::reset(); // the use of a fixture ensures the reset will prevent the expectations from leaking into other
|
||||
// test cases
|
||||
}
|
||||
};
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE( static_object_partial_solution, fixture )
|
||||
BOOST_FIXTURE_TEST_CASE(static_object_partial_solution, fixture)
|
||||
{
|
||||
my_class c( 42 );
|
||||
MOCK_EXPECT( f ).once().with( &c );
|
||||
f( &c );
|
||||
my_class c(42);
|
||||
MOCK_EXPECT(f).once().with(&c);
|
||||
f(&c);
|
||||
mock::verify(); // verify the expectations before local objects are destroyed and before the fixture resets them
|
||||
}
|
||||
//]
|
||||
|
||||
//[ static_objects_solution
|
||||
BOOST_FIXTURE_TEST_CASE( static_objects_solution, mock::cleanup ) // actually the library includes a ready to use fixture just like the one described
|
||||
BOOST_FIXTURE_TEST_CASE(
|
||||
static_objects_solution,
|
||||
mock::cleanup) // actually the library includes a ready to use fixture just like the one described
|
||||
{
|
||||
my_class c( 42 );
|
||||
MOCK_EXPECT( f ).once().with( &c );
|
||||
f( &c );
|
||||
my_class c(42);
|
||||
MOCK_EXPECT(f).once().with(&c);
|
||||
f(&c);
|
||||
mock::verify();
|
||||
}
|
||||
//]
|
||||
|
|
|
|||
|
|
@ -6,15 +6,15 @@
|
|||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <turtle/mock.hpp>
|
||||
#include "calculator.hpp"
|
||||
#include "mock_view.hpp"
|
||||
#include <turtle/mock.hpp>
|
||||
|
||||
//[ overflow_throws
|
||||
BOOST_AUTO_TEST_CASE( overflow_throws )
|
||||
BOOST_AUTO_TEST_CASE(overflow_throws)
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
BOOST_CHECK_THROW( c.add( (std::numeric_limits< int >::max)(), 1 ), std::exception );
|
||||
calculator c(v);
|
||||
BOOST_CHECK_THROW(c.add((std::numeric_limits<int>::max)(), 1), std::exception);
|
||||
}
|
||||
//]
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -13,7 +13,7 @@
|
|||
class view
|
||||
{
|
||||
public:
|
||||
virtual void display( int result ) = 0;
|
||||
virtual void display(int result) = 0;
|
||||
};
|
||||
//]
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue