Format code using Clang-Format 10 and enforce via CI

Makes the format of the code base uniform.
This commit is contained in:
Alexander Grund 2022-01-24 16:11:29 +01:00
parent b5bb500bd2
commit ee72e8b9d8
No known key found for this signature in database
GPG key ID: AA48A0760367A42B
97 changed files with 11189 additions and 10842 deletions

View file

@ -15,10 +15,11 @@
class calculator
{
view& v;
public:
calculator( view& v ): v(v){}
void add( int a, int b ){ v.display(a + b); } // the result will be sent to the view 'v'
public:
calculator(view& v) : v(v) {}
void add(int a, int b) { v.display(a + b); } // the result will be sent to the view 'v'
};
//]

View file

@ -15,152 +15,135 @@
#include <boost/test/unit_test.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_free_function )
BOOST_AUTO_TEST_CASE(forty_one_plus_one_is_forty_two_free_function)
{
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_custom_constraint )
BOOST_AUTO_TEST_CASE(forty_one_plus_one_is_forty_two_custom_constraint)
{
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 - mock::unwrap_ref(expected_) ) <= mock::unwrap_ref(threshold_) ;
return std::abs(actual - mock::unwrap_ref(expected_)) <= mock::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_near )
BOOST_AUTO_TEST_CASE(forty_one_plus_one_is_forty_two_plus_or_minus_one_near)
{
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_near_cref )
BOOST_AUTO_TEST_CASE(forty_one_plus_one_is_forty_two_plus_or_minus_one_near_cref)
{
mock_view v;
calculator c( v );
int expected = 0, threshold = 0;
MOCK_EXPECT( v.display ).with( near( std::cref( expected ), std::cref( threshold ) ) );
expected = 42;
threshold = 1;
c.add( 41, 1 );
mock_view v;
calculator c(v);
int expected = 0, threshold = 0;
MOCK_EXPECT(v.display).with(near(std::cref(expected), std::cref(threshold)));
expected = 42;
threshold = 1;
c.add(41, 1);
}
//]
// Example of a "strong type" float
struct float_wrapper{
struct float_wrapper
{
float value;
float_wrapper(float value): value(value){}
float_wrapper(float value) : value(value) {}
operator float() const { return value; }
friend std::ostream& operator<<( std::ostream& s, const float_wrapper& f)
{
return s << f.value;
}
friend std::ostream& operator<<(std::ostream& s, const float_wrapper& f) { return s << f.value; }
};
BOOST_AUTO_TEST_CASE( near_constraint_works_with_with_float_wrapper_and_cref )
BOOST_AUTO_TEST_CASE(near_constraint_works_with_with_float_wrapper_and_cref)
{
mock_view v;
calculator c( v );
float_wrapper expected = 0, threshold = 0;
// This works even without the unwrap_ref
MOCK_EXPECT( v.display ).once().with( near( expected, threshold ) );
// This requires 2 implicit conversion: from reference_wrapper to float_wrapper, then to float
// so unwrap_ref in near is required as C++ allows only 1 implicit conversion
MOCK_EXPECT( v.display ).once().with( near( std::cref( expected ), std::cref( threshold ) ) );
expected = 42;
threshold = 1;
c.add(0, 0);
c.add(41, 1);
}
mock_view v;
calculator c(v);
float_wrapper expected = 0, threshold = 0;
// This works even without the unwrap_ref
MOCK_EXPECT(v.display).once().with(near(expected, threshold));
// This requires 2 implicit conversion: from reference_wrapper to float_wrapper, then to float
// so unwrap_ref in near is required as C++ allows only 1 implicit conversion
MOCK_EXPECT(v.display).once().with(near(std::cref(expected), std::cref(threshold)));
expected = 42;
threshold = 1;
c.add(0, 0);
c.add(41, 1);
}
} // namespace near_constraint_cref_test
#undef MOCK_MAX_ARGS
//[ max_args
@ -169,12 +152,12 @@ BOOST_AUTO_TEST_CASE( near_constraint_works_with_with_float_wrapper_and_cref )
//]
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
//[ custom_policy
template< typename Result >
template<typename Result>
struct custom_policy
{
static Result abort()
@ -182,17 +165,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.
}
@ -200,7 +183,7 @@ struct custom_policy
//]
#if defined(__GNUC__)
#pragma GCC diagnostic pop
# pragma GCC diagnostic pop
#endif
#undef MOCK_ERROR_POLICY

View file

@ -7,15 +7,15 @@
// http://www.boost.org/LICENSE_1_0.txt)
#include <functional>
#include <string>
#include <sstream>
#include <string>
std::function<void()> error_handler_abort;
std::function<void(const char*, int)> error_handler_pass;
std::function<void(const std::string&, const char*, int)> error_handler_call;
std::function<void(const char* message, const std::string&, const char*, int)> error_handler_fail;
template< typename Result >
template<typename Result>
struct configurable_mock_error
{
static Result abort()
@ -24,25 +24,22 @@ struct configurable_mock_error
return Result();
}
static void pass( const char* file, int line )
{
error_handler_pass(file, line);
}
static void pass(const char* file, int line) { error_handler_pass(file, line); }
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)
{
std::stringstream s;
s << context;
error_handler_call( s.str(), file, line );
error_handler_call(s.str(), file, line);
}
template< typename Context >
static void fail( const char* message, const Context& context, const char* file = "", int line = 0 )
template<typename Context>
static void fail(const char* message, const Context& context, const char* file = "", int line = 0)
{
std::stringstream s;
s << context;
error_handler_fail( message, s.str(), file, line );
error_handler_fail(message, s.str(), file, line);
}
};
@ -50,8 +47,8 @@ struct configurable_mock_error
#define MOCK_USE_BOOST_TEST
//[ prerequisite
#include <boost/test/unit_test.hpp>
#include <turtle/mock.hpp>
#include <boost/test/unit_test.hpp>
//]
#include "calculator.hpp"
#include "mock_view.hpp"
@ -69,124 +66,114 @@ struct Fixture
BOOST_FIXTURE_TEST_SUITE(GettingStarted, Fixture)
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_reset )
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero_reset)
{
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_expect )
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero_expect)
{
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_then_1_plus_0_is_1 )
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero_then_1_plus_0_is_1)
{
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( add_several_numbers_in_sequences )
BOOST_AUTO_TEST_CASE(add_several_numbers_in_sequences)
{
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
BOOST_AUTO_TEST_SUITE_END()
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_BASE_CLASS(mock_view, view)
{
MOCK_METHOD( display, 1 )
MOCK_METHOD(display, 1)
};
class calculator
{
view& v;
public:
calculator( view& v ): v(v) {}
void add( int a, int b ){ v.display(a + b); }
public:
calculator(view& v) : v(v) {}
void add(int a, int b) { v.display(a + b); }
};
struct CatchFailureFixture: Fixture
struct CatchFailureFixture : Fixture
{
static bool aborted;
static std::string fail_msg;
static void abort()
{
aborted = true;
}
static void fail( const std::string& message, const std::string&, const char* = "", int = 0 ){
fail_msg = message;
}
static void abort() { aborted = true; }
static void fail(const std::string& message, const std::string&, const char* = "", int = 0) { fail_msg = message; }
CatchFailureFixture()
{
error_handler_abort = abort;
@ -201,22 +188,21 @@ struct CatchFailureFixture: Fixture
bool CatchFailureFixture::aborted = false;
std::string CatchFailureFixture::fail_msg;
struct AssertMissingAction: CatchFailureFixture{
void teardown(){
assert_failure("missing action");
}
struct AssertMissingAction : CatchFailureFixture
{
void teardown() { assert_failure("missing action"); }
};
BOOST_FIXTURE_TEST_SUITE( MissingReturnActionSuite, AssertMissingAction)
BOOST_FIXTURE_TEST_SUITE(MissingReturnActionSuite, AssertMissingAction)
//[ action_test
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero_with_action )
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero_with_action)
{
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
BOOST_AUTO_TEST_SUITE_END()

View file

@ -6,53 +6,46 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/test/unit_test.hpp>
#include <turtle/mock.hpp>
#include <boost/test/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
class derived : public base
{
//[ 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 )
};
public:
virtual void method(const int);
};
void derived::method(int) {}
//]
}
} // namespace limitations_const_parameter_warning_explanation
namespace {
//[ 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)
};
//]
} // namespace
BOOST_AUTO_TEST_CASE(check_method_stub_is_called)
{
mock_base b;
MOCK_EXPECT(b.method).once().with(1);
// Example user code taking a base* (or smart pointer variant)
auto callMethod = [](base* bPtr){ bPtr->method(1); };
auto callMethod = [](base* bPtr) { bPtr->method(1); };
callMethod(&b);
}

View file

@ -6,35 +6,34 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/test/unit_test.hpp>
#include <turtle/mock.hpp>
#include <boost/test/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_3
MOCK_EXPECT( m.method ).with( nullptr );
//]
//[ 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_3
MOCK_EXPECT(m.method).with(nullptr);
//]
}

View file

@ -6,8 +6,8 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/test/unit_test.hpp>
#include <turtle/mock.hpp>
#include <boost/test/unit_test.hpp>
//[ limitations_non_virtual_method_problem
class base
@ -19,9 +19,9 @@ public:
//]
//[ limitations_non_virtual_method_problem_2
MOCK_BASE_CLASS( mock_base, base )
MOCK_BASE_CLASS(mock_base, base)
{
MOCK_METHOD( method, 0 )
MOCK_METHOD(method, 0)
};
//]

View file

@ -6,31 +6,36 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/test/unit_test.hpp>
#include <turtle/mock.hpp>
#include <boost/test/unit_test.hpp>
namespace
{
namespace {
//[ limitations_protected_private_method_problem
class base
class base
{
public:
void call()
{
public:
void call(){ method_1(); method_2(); }
protected:
virtual void method_1() = 0;
private:
virtual void method_2() = 0;
};
method_1();
method_2();
}
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
BOOST_AUTO_TEST_CASE(mocked_methods_are_called)
{

View file

@ -6,30 +6,29 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/test/unit_test.hpp>
#include <turtle/mock.hpp>
#include <boost/test/unit_test.hpp>
namespace
{
namespace {
//[ limitations_template_base_class_method_problem
template< typename T >
class base
{
public:
virtual ~base() = default;
template<typename T>
class base
{
public:
virtual ~base() = default;
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, 0, void() )
};
template<typename T>
MOCK_BASE_CLASS(mock_base, base<T>)
{
MOCK_METHOD(method, 0, void())
};
//]
}
} // namespace
BOOST_AUTO_TEST_CASE(call_method_from_templated_class)
{

View file

@ -6,32 +6,31 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/test/unit_test.hpp>
#include <turtle/mock.hpp>
#include <boost/test/unit_test.hpp>
namespace limitations_template_method_problem
{
namespace limitations_template_method_problem {
//[ limitations_template_method_problem
class concept_class
{
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_CLASS(mock_concept)
{
MOCK_METHOD( method, 1, void( int ), method_int )
MOCK_METHOD( method, 1, void( const char* ), method_string )
MOCK_METHOD(method, 1, void(int), method_int)
MOCK_METHOD(method, 1, void(const char*), method_string)
};
//]
@ -42,46 +41,45 @@ BOOST_AUTO_TEST_CASE(mocked_templated_methods_are_called)
MOCK_EXPECT(b.method_string).once().with(mock::equal(std::string("string")));
function_under_test(b);
}
}
} // namespace limitations_template_method_problem
namespace limitations_template_method_problem_2
{
namespace limitations_template_method_problem_2 {
//[ limitations_template_method_problem_2
class concept_class
{
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();
}
@ -94,4 +92,4 @@ BOOST_AUTO_TEST_CASE(dispatch_methods_are_called)
MOCK_EXPECT(b.create_string).once().returns("");
function_under_test(b);
}
}
} // namespace limitations_template_method_problem_2

View file

@ -6,31 +6,27 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/test/unit_test.hpp>
#include <turtle/mock.hpp>
#include <boost/test/unit_test.hpp>
namespace
{
namespace {
//[ limitations_throw_specifier_problem
struct base_class
{
virtual ~base_class() = default;
struct base_class
{
virtual ~base_class() = default;
virtual void method() throw() = 0;
};
virtual void method() throw() = 0;
};
//]
//[ limitations_throw_specifier_solution
MOCK_BASE_CLASS( mock_class, base_class )
{
void method() throw() override
{
method_proxy();
}
MOCK_METHOD( method_proxy, 0, void(), method )
};
MOCK_BASE_CLASS(mock_class, base_class)
{
void method() throw() override { method_proxy(); }
MOCK_METHOD(method_proxy, 0, void(), method)
};
//]
}
} // namespace
BOOST_AUTO_TEST_CASE(call_method_proxy)
{

View file

@ -9,14 +9,14 @@
#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)
};
//]
//]
#endif // MOCK_VIEW

View file

@ -6,42 +6,41 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/test/unit_test.hpp>
#include <turtle/mock.hpp>
#include "calculator.hpp"
#include "mock_view.hpp"
#include <turtle/mock.hpp>
#include <boost/test/unit_test.hpp>
namespace simple
{
namespace simple {
//[ simple_calculator
class calculator
{
public:
int add( int a, int b );
int add(int a, int b);
};
//]
int calculator::add( int a, int b ){ return a + b; }
int calculator::add(int a, int b)
{
return a + 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;
@ -52,26 +51,26 @@ public:
//]
//[ zero_plus_zero_is_zero_without_mock_object
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero_without_mock_object )
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero_without_mock_object)
{
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_with_mock_object )
BOOST_AUTO_TEST_CASE(zero_plus_zero_is_zero_with_mock_object)
{
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

View file

@ -7,68 +7,66 @@
// http://www.boost.org/LICENSE_1_0.txt)
//[ async_call_problem
namespace mock_test
namespace mock_test {
class base_class
{
class base_class
{
public:
virtual void method() = 0;
};
public:
virtual void method() = 0;
};
class my_class
{
base_class& b;
public:
explicit my_class( base_class& );
class my_class
{
base_class& b;
void flush(); // repetitively calling this method will in turn call base_class::method at some point
};
}
public:
explicit my_class(base_class&);
void flush(); // repetitively calling this method will in turn call base_class::method at some point
};
} // namespace mock_test
//]
namespace mock_test
namespace mock_test {
my_class::my_class(base_class& b) : b(b) {}
void my_class::flush()
{
my_class::my_class( base_class& b): b(b){}
void my_class::flush()
{
static int counter = 7;
if(--counter == 0)
b.method();
}
static int counter = 7;
if(--counter == 0)
b.method();
}
} // namespace mock_test
//[ async_call_solution
#include <turtle/mock.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/thread.hpp>
#include <turtle/mock.hpp>
namespace mock_test
namespace mock_test {
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();
}
--attempts;
boost::this_thread::sleep(boost::posix_time::milliseconds(sleep));
flush();
}
}
MOCK_BASE_CLASS( mock_base_class, base_class )
{
MOCK_METHOD( method, 0 )
};
MOCK_BASE_CLASS(mock_base_class, base_class)
{
MOCK_METHOD(method, 0)
};
BOOST_AUTO_TEST_CASE( method_is_called )
BOOST_AUTO_TEST_CASE(method_is_called)
{
mock_base_class m;
my_class c( m );
my_class c(m);
bool done = false;
MOCK_EXPECT( m.method ).once().calls( [&done](){ done = true; } );
check( done, [&c](){ c.flush(); } ); // just wait on done, flushing from time to time
MOCK_EXPECT(m.method).once().calls([&done]() { done = true; });
check(done, [&c]() { c.flush(); }); // just wait on done, flushing from time to time
}
}
} // namespace mock_test
//]

View file

@ -13,14 +13,17 @@ static void someFunctor(int newValue);
//[ invoke_functor_problem
#include <functional>
class base_class
{
public:
virtual void method( const std::function< void( int ) >& functor ) = 0;
};
class base_class
{
public:
virtual void method(const std::function<void(int)>& functor) = 0;
};
// the function will call 'method' with a functor to be applied
void function(base_class& c) { c.method(someFunctor); }
// the function will call 'method' with a functor to be applied
void function(base_class& c)
{
c.method(someFunctor);
}
//]
// Some test-only code to verify what is described
@ -42,21 +45,22 @@ struct CheckReceivedValue
#define BOOST_AUTO_TEST_CASE(name) BOOST_FIXTURE_TEST_CASE(name, CheckReceivedValue)
//[ invoke_functor_solution
#include <boost/test/unit_test.hpp>
#include <turtle/mock.hpp>
#include <boost/test/unit_test.hpp>
namespace
namespace {
MOCK_BASE_CLASS(mock_class, base_class)
{
MOCK_BASE_CLASS( mock_class, base_class )
{
MOCK_METHOD( method, 1 )
};
}
MOCK_METHOD(method, 1)
};
} // namespace
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( [](const auto &functor){ functor(42); } ); // whenever 'method' is called, invoke the functor with 42
function( mock );
MOCK_EXPECT(mock.method).calls([](const auto& functor) {
functor(42);
}); // whenever 'method' is called, invoke the functor with 42
function(mock);
}
//]

View file

@ -7,31 +7,28 @@
// http://www.boost.org/LICENSE_1_0.txt)
//[ quick_constraint_problem
#include <boost/test/unit_test.hpp>
#include <turtle/mock.hpp>
#include <boost/test/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
@ -39,18 +36,21 @@ 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
{
std::ostringstream s;
s << actual;
return s.str() == expected;
}
} // mock
bool operator==(const my_class& actual,
const std::string& expected) // the first part of the trick is to compare to a string
{
std::ostringstream s;
s << actual;
return s.str() == 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));
}
//]

View file

@ -7,55 +7,59 @@
// 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
//]
namespace
namespace {
static base_class* global_b = nullptr;
my_class::my_class(base_class& b)
{
static base_class* global_b = nullptr;
my_class::my_class( base_class& b){ global_b = &b; }
void my_class::process()
{
int secret_value = 42;
global_b->method(secret_value);
global_b->method(secret_value);
}
global_b = &b;
}
void my_class::process()
{
int secret_value = 42;
global_b->method(secret_value);
global_b->method(secret_value);
}
} // namespace
//[ retrieve_cref_solution
#include <boost/test/unit_test.hpp>
#include <turtle/mock.hpp>
#include <boost/test/unit_test.hpp>
namespace
namespace {
MOCK_BASE_CLASS(mock_base_class, base_class)
{
MOCK_BASE_CLASS( mock_base_class, base_class )
{
MOCK_METHOD( method, 1 )
};
}
MOCK_METHOD(method, 1)
};
} // namespace
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( std::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(std::cref(value)); // on second call compare the previously retrieved value with the newly received one
c.process();
}
//]

View file

@ -12,37 +12,36 @@
#define MOCK_MAKE_TEST_PASS 1
#if MOCK_MAKE_TEST_PASS
#undef BOOST_AUTO_TEST_CASE
#define BOOST_AUTO_TEST_CASE(name) BOOST_FIXTURE_TEST_CASE(name, mock::cleanup)
# undef BOOST_AUTO_TEST_CASE
# define BOOST_AUTO_TEST_CASE(name) BOOST_FIXTURE_TEST_CASE(name, mock::cleanup)
#endif
//[ static_objects_problem
#include <boost/test/unit_test.hpp>
#include <turtle/mock.hpp>
#include <boost/test/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' outlives 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' outlives 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
//]
@ -51,25 +50,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();
}
//]

View file

@ -6,9 +6,9 @@
// (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>
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <limits>
@ -16,18 +16,20 @@
// Dummy to detect if the assertion unexpectedly succeeded to test what is explained
#undef BOOST_CHECK_THROW
#define BOOST_CHECK_THROW(expr, exc) \
try { \
expr; \
} catch(const exc&) { \
std::cerr << "Exception thrown but should not"; \
}
#define BOOST_CHECK_THROW(expr, exc) \
try \
{ \
expr; \
} catch(const exc&) \
{ \
std::cerr << "Exception thrown but should not"; \
}
//[ 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

View file

@ -13,7 +13,7 @@
class view
{
public:
virtual void display( int result ) = 0;
virtual void display(int result) = 0;
};
//]