gmock-spec-builders.h (googletest-release-1.10.0) | : | gmock-spec-builders.h (googletest-release-1.11.0) | ||
---|---|---|---|---|
skipping to change at line 60 | skipping to change at line 60 | |||
// .After(expectations) | // .After(expectations) | |||
// .WillOnce(action) | // .WillOnce(action) | |||
// .WillRepeatedly(action) | // .WillRepeatedly(action) | |||
// .RetiresOnSaturation(); | // .RetiresOnSaturation(); | |||
// | // | |||
// where all clauses are optional, and .InSequence()/.After()/ | // where all clauses are optional, and .InSequence()/.After()/ | |||
// .WillOnce() can appear any number of times. | // .WillOnce() can appear any number of times. | |||
// GOOGLETEST_CM0002 DO NOT DELETE | // GOOGLETEST_CM0002 DO NOT DELETE | |||
#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ | #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ | |||
#define GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ | #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ | |||
#include <functional> | #include <functional> | |||
#include <map> | #include <map> | |||
#include <memory> | #include <memory> | |||
#include <set> | #include <set> | |||
#include <sstream> | #include <sstream> | |||
#include <string> | #include <string> | |||
#include <type_traits> | #include <type_traits> | |||
#include <utility> | #include <utility> | |||
#include <vector> | #include <vector> | |||
skipping to change at line 110 | skipping to change at line 110 | |||
// Base class for expectations. | // Base class for expectations. | |||
class ExpectationBase; | class ExpectationBase; | |||
// Implements an expectation. | // Implements an expectation. | |||
template <typename F> class TypedExpectation; | template <typename F> class TypedExpectation; | |||
// Helper class for testing the Expectation class template. | // Helper class for testing the Expectation class template. | |||
class ExpectationTester; | class ExpectationTester; | |||
// Helper classes for implementing NiceMock, StrictMock, and NaggyMock. | ||||
template <typename MockClass> | ||||
class NiceMockImpl; | ||||
template <typename MockClass> | ||||
class StrictMockImpl; | ||||
template <typename MockClass> | ||||
class NaggyMockImpl; | ||||
// Protects the mock object registry (in class Mock), all function | // Protects the mock object registry (in class Mock), all function | |||
// mockers, and all expectations. | // mockers, and all expectations. | |||
// | // | |||
// The reason we don't use more fine-grained protection is: when a | // The reason we don't use more fine-grained protection is: when a | |||
// mock function Foo() is called, it needs to consult its expectations | // mock function Foo() is called, it needs to consult its expectations | |||
// to see which one should be picked. If another thread is allowed to | // to see which one should be picked. If another thread is allowed to | |||
// call a mock function (either Foo() or a different one) at the same | // call a mock function (either Foo() or a different one) at the same | |||
// time, it could affect the "retired" attributes of Foo()'s | // time, it could affect the "retired" attributes of Foo()'s | |||
// expectations when InSequence() is used, and thus affect which | // expectations when InSequence() is used, and thus affect which | |||
// expectation gets picked. Therefore, we sequence all mock function | // expectation gets picked. Therefore, we sequence all mock function | |||
skipping to change at line 415 | skipping to change at line 423 | |||
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); | |||
private: | private: | |||
friend class internal::UntypedFunctionMockerBase; | friend class internal::UntypedFunctionMockerBase; | |||
// Needed for a function mocker to register itself (so that we know | // Needed for a function mocker to register itself (so that we know | |||
// how to clear a mock object). | // how to clear a mock object). | |||
template <typename F> | template <typename F> | |||
friend class internal::FunctionMocker; | friend class internal::FunctionMocker; | |||
template <typename M> | template <typename MockClass> | |||
friend class NiceMock; | friend class internal::NiceMockImpl; | |||
template <typename MockClass> | ||||
template <typename M> | friend class internal::NaggyMockImpl; | |||
friend class NaggyMock; | template <typename MockClass> | |||
friend class internal::StrictMockImpl; | ||||
template <typename M> | ||||
friend class StrictMock; | ||||
// Tells Google Mock to allow uninteresting calls on the given mock | // Tells Google Mock to allow uninteresting calls on the given mock | |||
// object. | // object. | |||
static void AllowUninterestingCalls(const void* mock_obj) | static void AllowUninterestingCalls(const void* mock_obj) | |||
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); | |||
// Tells Google Mock to warn the user about uninteresting calls on | // Tells Google Mock to warn the user about uninteresting calls on | |||
// the given mock object. | // the given mock object. | |||
static void WarnUninterestingCalls(const void* mock_obj) | static void WarnUninterestingCalls(const void* mock_obj) | |||
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); | |||
skipping to change at line 501 | skipping to change at line 507 | |||
// Notes: | // Notes: | |||
// - This class is copyable and has value semantics. | // - This class is copyable and has value semantics. | |||
// - Constness is shallow: a const Expectation object itself cannot | // - Constness is shallow: a const Expectation object itself cannot | |||
// be modified, but the mutable methods of the ExpectationBase | // be modified, but the mutable methods of the ExpectationBase | |||
// object it references can be called via expectation_base(). | // object it references can be called via expectation_base(). | |||
class GTEST_API_ Expectation { | class GTEST_API_ Expectation { | |||
public: | public: | |||
// Constructs a null object that doesn't reference any expectation. | // Constructs a null object that doesn't reference any expectation. | |||
Expectation(); | Expectation(); | |||
Expectation(Expectation&&) = default; | ||||
Expectation(const Expectation&) = default; | ||||
Expectation& operator=(Expectation&&) = default; | ||||
Expectation& operator=(const Expectation&) = default; | ||||
~Expectation(); | ~Expectation(); | |||
// This single-argument ctor must not be explicit, in order to support the | // This single-argument ctor must not be explicit, in order to support the | |||
// Expectation e = EXPECT_CALL(...); | // Expectation e = EXPECT_CALL(...); | |||
// syntax. | // syntax. | |||
// | // | |||
// A TypedExpectation object stores its pre-requisites as | // A TypedExpectation object stores its pre-requisites as | |||
// Expectation objects, and needs to call the non-const Retire() | // Expectation objects, and needs to call the non-const Retire() | |||
// method on the ExpectationBase objects they reference. Therefore | // method on the ExpectationBase objects they reference. Therefore | |||
// Expectation must receive a *non-const* reference to the | // Expectation must receive a *non-const* reference to the | |||
skipping to change at line 880 | skipping to change at line 889 | |||
// and can change as the mock function is called. | // and can change as the mock function is called. | |||
int call_count_; // How many times this expectation has been invoked. | int call_count_; // How many times this expectation has been invoked. | |||
bool retired_; // True if and only if this expectation has retired. | bool retired_; // True if and only if this expectation has retired. | |||
UntypedActions untyped_actions_; | UntypedActions untyped_actions_; | |||
bool extra_matcher_specified_; | bool extra_matcher_specified_; | |||
bool repeated_action_specified_; // True if a WillRepeatedly() was specified. | bool repeated_action_specified_; // True if a WillRepeatedly() was specified. | |||
bool retires_on_saturation_; | bool retires_on_saturation_; | |||
Clause last_clause_; | Clause last_clause_; | |||
mutable bool action_count_checked_; // Under mutex_. | mutable bool action_count_checked_; // Under mutex_. | |||
mutable Mutex mutex_; // Protects action_count_checked_. | mutable Mutex mutex_; // Protects action_count_checked_. | |||
GTEST_DISALLOW_ASSIGN_(ExpectationBase); | ||||
}; // class ExpectationBase | }; // class ExpectationBase | |||
// Impements an expectation for the given function type. | // Impements an expectation for the given function type. | |||
template <typename F> | template <typename F> | |||
class TypedExpectation : public ExpectationBase { | class TypedExpectation : public ExpectationBase { | |||
public: | public: | |||
typedef typename Function<F>::ArgumentTuple ArgumentTuple; | typedef typename Function<F>::ArgumentTuple ArgumentTuple; | |||
typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple; | typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple; | |||
typedef typename Function<F>::Result Result; | typedef typename Function<F>::Result Result; | |||
skipping to change at line 1296 | skipping to change at line 1303 | |||
} | } | |||
private: | private: | |||
template <typename Function> | template <typename Function> | |||
friend class internal::FunctionMocker; | friend class internal::FunctionMocker; | |||
// The function mocker that owns this spec. | // The function mocker that owns this spec. | |||
internal::FunctionMocker<F>* const function_mocker_; | internal::FunctionMocker<F>* const function_mocker_; | |||
// The argument matchers specified in the spec. | // The argument matchers specified in the spec. | |||
ArgumentMatcherTuple matchers_; | ArgumentMatcherTuple matchers_; | |||
GTEST_DISALLOW_ASSIGN_(MockSpec); | ||||
}; // class MockSpec | }; // class MockSpec | |||
// Wrapper type for generically holding an ordinary value or lvalue reference. | // Wrapper type for generically holding an ordinary value or lvalue reference. | |||
// If T is not a reference type, it must be copyable or movable. | // If T is not a reference type, it must be copyable or movable. | |||
// ReferenceOrValueWrapper<T> is movable, and will also be copyable unless | // ReferenceOrValueWrapper<T> is movable, and will also be copyable unless | |||
// T is a move-only value type (which means that it will always be copyable | // T is a move-only value type (which means that it will always be copyable | |||
// if the current platform does not support move semantics). | // if the current platform does not support move semantics). | |||
// | // | |||
// The primary template defines handling for values, but function header | // The primary template defines handling for values, but function header | |||
// comments describe the contract for the whole template (including | // comments describe the contract for the whole template (including | |||
skipping to change at line 1351 | skipping to change at line 1356 | |||
typedef T& reference; | typedef T& reference; | |||
explicit ReferenceOrValueWrapper(reference ref) | explicit ReferenceOrValueWrapper(reference ref) | |||
: value_ptr_(&ref) {} | : value_ptr_(&ref) {} | |||
T& Unwrap() { return *value_ptr_; } | T& Unwrap() { return *value_ptr_; } | |||
const T& Peek() const { return *value_ptr_; } | const T& Peek() const { return *value_ptr_; } | |||
private: | private: | |||
T* value_ptr_; | T* value_ptr_; | |||
}; | }; | |||
// MSVC warns about using 'this' in base member initializer list, so | ||||
// we need to temporarily disable the warning. We have to do it for | ||||
// the entire class to suppress the warning, even though it's about | ||||
// the constructor only. | ||||
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4355) | ||||
// C++ treats the void type specially. For example, you cannot define | // C++ treats the void type specially. For example, you cannot define | |||
// a void-typed variable or pass a void value to a function. | // a void-typed variable or pass a void value to a function. | |||
// ActionResultHolder<T> holds a value of type T, where T must be a | // ActionResultHolder<T> holds a value of type T, where T must be a | |||
// copyable type or void (T doesn't need to be default-constructable). | // copyable type or void (T doesn't need to be default-constructable). | |||
// It hides the syntactic difference between void and other types, and | // It hides the syntactic difference between void and other types, and | |||
// is used to unify the code for invoking both void-returning and | // is used to unify the code for invoking both void-returning and | |||
// non-void-returning mock functions. | // non-void-returning mock functions. | |||
// Untyped base class for ActionResultHolder<T>. | // Untyped base class for ActionResultHolder<T>. | |||
class UntypedActionResultHolderBase { | class UntypedActionResultHolderBase { | |||
skipping to change at line 1787 | skipping to change at line 1786 | |||
if (count > 1) { | if (count > 1) { | |||
*why << "tried expectation #" << i << ": "; | *why << "tried expectation #" << i << ": "; | |||
} | } | |||
*why << expectation->source_text() << "...\n"; | *why << expectation->source_text() << "...\n"; | |||
expectation->ExplainMatchResultTo(args, why); | expectation->ExplainMatchResultTo(args, why); | |||
expectation->DescribeCallCountTo(why); | expectation->DescribeCallCountTo(why); | |||
} | } | |||
} | } | |||
}; // class FunctionMocker | }; // class FunctionMocker | |||
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4355 | ||||
// Reports an uninteresting call (whose description is in msg) in the | // Reports an uninteresting call (whose description is in msg) in the | |||
// manner specified by 'reaction'. | // manner specified by 'reaction'. | |||
void ReportUninterestingCall(CallReaction reaction, const std::string& msg); | void ReportUninterestingCall(CallReaction reaction, const std::string& msg); | |||
} // namespace internal | } // namespace internal | |||
// A MockFunction<F> class has one mock method whose type is F. It is | namespace internal { | |||
// useful when you just want your test code to emit some messages and | ||||
// have Google Mock verify the right messages are sent (and perhaps at | template <typename F> | |||
// the right times). For example, if you are exercising code: | class MockFunction; | |||
template <typename R, typename... Args> | ||||
class MockFunction<R(Args...)> { | ||||
public: | ||||
MockFunction(const MockFunction&) = delete; | ||||
MockFunction& operator=(const MockFunction&) = delete; | ||||
std::function<R(Args...)> AsStdFunction() { | ||||
return [this](Args... args) -> R { | ||||
return this->Call(std::forward<Args>(args)...); | ||||
}; | ||||
} | ||||
// Implementation detail: the expansion of the MOCK_METHOD macro. | ||||
R Call(Args... args) { | ||||
mock_.SetOwnerAndName(this, "Call"); | ||||
return mock_.Invoke(std::forward<Args>(args)...); | ||||
} | ||||
MockSpec<R(Args...)> gmock_Call(Matcher<Args>... m) { | ||||
mock_.RegisterOwner(this); | ||||
return mock_.With(std::move(m)...); | ||||
} | ||||
MockSpec<R(Args...)> gmock_Call(const WithoutMatchers&, R (*)(Args...)) { | ||||
return this->gmock_Call(::testing::A<Args>()...); | ||||
} | ||||
protected: | ||||
MockFunction() = default; | ||||
~MockFunction() = default; | ||||
private: | ||||
FunctionMocker<R(Args...)> mock_; | ||||
}; | ||||
/* | ||||
The SignatureOf<F> struct is a meta-function returning function signature | ||||
corresponding to the provided F argument. | ||||
It makes use of MockFunction easier by allowing it to accept more F arguments | ||||
than just function signatures. | ||||
Specializations provided here cover a signature type itself and any template | ||||
that can be parameterized with a signature, including std::function and | ||||
boost::function. | ||||
*/ | ||||
template <typename F, typename = void> | ||||
struct SignatureOf; | ||||
template <typename R, typename... Args> | ||||
struct SignatureOf<R(Args...)> { | ||||
using type = R(Args...); | ||||
}; | ||||
template <template <typename> class C, typename F> | ||||
struct SignatureOf<C<F>, | ||||
typename std::enable_if<std::is_function<F>::value>::type> | ||||
: SignatureOf<F> {}; | ||||
template <typename F> | ||||
using SignatureOfT = typename SignatureOf<F>::type; | ||||
} // namespace internal | ||||
// A MockFunction<F> type has one mock method whose type is | ||||
// internal::SignatureOfT<F>. It is useful when you just want your | ||||
// test code to emit some messages and have Google Mock verify the | ||||
// right messages are sent (and perhaps at the right times). For | ||||
// example, if you are exercising code: | ||||
// | // | |||
// Foo(1); | // Foo(1); | |||
// Foo(2); | // Foo(2); | |||
// Foo(3); | // Foo(3); | |||
// | // | |||
// and want to verify that Foo(1) and Foo(3) both invoke | // and want to verify that Foo(1) and Foo(3) both invoke | |||
// mock.Bar("a"), but Foo(2) doesn't invoke anything, you can write: | // mock.Bar("a"), but Foo(2) doesn't invoke anything, you can write: | |||
// | // | |||
// TEST(FooTest, InvokesBarCorrectly) { | // TEST(FooTest, InvokesBarCorrectly) { | |||
// MyMock mock; | // MyMock mock; | |||
skipping to change at line 1832 | skipping to change at line 1900 | |||
// Foo(3); | // Foo(3); | |||
// } | // } | |||
// | // | |||
// The expectation spec says that the first Bar("a") must happen | // The expectation spec says that the first Bar("a") must happen | |||
// before check point "1", the second Bar("a") must happen after check | // before check point "1", the second Bar("a") must happen after check | |||
// point "2", and nothing should happen between the two check | // point "2", and nothing should happen between the two check | |||
// points. The explicit check points make it easy to tell which | // points. The explicit check points make it easy to tell which | |||
// Bar("a") is called by which call to Foo(). | // Bar("a") is called by which call to Foo(). | |||
// | // | |||
// MockFunction<F> can also be used to exercise code that accepts | // MockFunction<F> can also be used to exercise code that accepts | |||
// std::function<F> callbacks. To do so, use AsStdFunction() method | // std::function<internal::SignatureOfT<F>> callbacks. To do so, use | |||
// to create std::function proxy forwarding to original object's Call. | // AsStdFunction() method to create std::function proxy forwarding to | |||
// Example: | // original object's Call. Example: | |||
// | // | |||
// TEST(FooTest, RunsCallbackWithBarArgument) { | // TEST(FooTest, RunsCallbackWithBarArgument) { | |||
// MockFunction<int(string)> callback; | // MockFunction<int(string)> callback; | |||
// EXPECT_CALL(callback, Call("bar")).WillOnce(Return(1)); | // EXPECT_CALL(callback, Call("bar")).WillOnce(Return(1)); | |||
// Foo(callback.AsStdFunction()); | // Foo(callback.AsStdFunction()); | |||
// } | // } | |||
// | ||||
// The internal::SignatureOfT<F> indirection allows to use other types | ||||
// than just function signature type. This is typically useful when | ||||
// providing a mock for a predefined std::function type. Example: | ||||
// | ||||
// using FilterPredicate = std::function<bool(string)>; | ||||
// void MyFilterAlgorithm(FilterPredicate predicate); | ||||
// | ||||
// TEST(FooTest, FilterPredicateAlwaysAccepts) { | ||||
// MockFunction<FilterPredicate> predicateMock; | ||||
// EXPECT_CALL(predicateMock, Call(_)).WillRepeatedly(Return(true)); | ||||
// MyFilterAlgorithm(predicateMock.AsStdFunction()); | ||||
// } | ||||
template <typename F> | template <typename F> | |||
class MockFunction; | class MockFunction : public internal::MockFunction<internal::SignatureOfT<F>> { | |||
using Base = internal::MockFunction<internal::SignatureOfT<F>>; | ||||
template <typename R, typename... Args> | ||||
class MockFunction<R(Args...)> { | ||||
public: | public: | |||
MockFunction() {} | using Base::Base; | |||
MockFunction(const MockFunction&) = delete; | ||||
MockFunction& operator=(const MockFunction&) = delete; | ||||
std::function<R(Args...)> AsStdFunction() { | ||||
return [this](Args... args) -> R { | ||||
return this->Call(std::forward<Args>(args)...); | ||||
}; | ||||
} | ||||
// Implementation detail: the expansion of the MOCK_METHOD macro. | ||||
R Call(Args... args) { | ||||
mock_.SetOwnerAndName(this, "Call"); | ||||
return mock_.Invoke(std::forward<Args>(args)...); | ||||
} | ||||
internal::MockSpec<R(Args...)> gmock_Call(Matcher<Args>... m) { | ||||
mock_.RegisterOwner(this); | ||||
return mock_.With(std::move(m)...); | ||||
} | ||||
internal::MockSpec<R(Args...)> gmock_Call(const internal::WithoutMatchers&, | ||||
R (*)(Args...)) { | ||||
return this->gmock_Call(::testing::A<Args>()...); | ||||
} | ||||
private: | ||||
internal::FunctionMocker<R(Args...)> mock_; | ||||
}; | }; | |||
// The style guide prohibits "using" statements in a namespace scope | // The style guide prohibits "using" statements in a namespace scope | |||
// inside a header file. However, the MockSpec class template is | // inside a header file. However, the MockSpec class template is | |||
// meant to be defined in the ::testing namespace. The following line | // meant to be defined in the ::testing namespace. The following line | |||
// is just a trick for working around a bug in MSVC 8.0, which cannot | // is just a trick for working around a bug in MSVC 8.0, which cannot | |||
// handle it if we define MockSpec in ::testing. | // handle it if we define MockSpec in ::testing. | |||
using internal::MockSpec; | using internal::MockSpec; | |||
// Const(x) is a convenient function for obtaining a const reference | // Const(x) is a convenient function for obtaining a const reference | |||
skipping to change at line 1983 | skipping to change at line 2036 | |||
((mock_expr).gmock_##call)(::testing::internal::GetWithoutMatchers(), \ | ((mock_expr).gmock_##call)(::testing::internal::GetWithoutMatchers(), \ | |||
nullptr) \ | nullptr) \ | |||
.Setter(__FILE__, __LINE__, #mock_expr, #call) | .Setter(__FILE__, __LINE__, #mock_expr, #call) | |||
#define ON_CALL(obj, call) \ | #define ON_CALL(obj, call) \ | |||
GMOCK_ON_CALL_IMPL_(obj, InternalDefaultActionSetAt, call) | GMOCK_ON_CALL_IMPL_(obj, InternalDefaultActionSetAt, call) | |||
#define EXPECT_CALL(obj, call) \ | #define EXPECT_CALL(obj, call) \ | |||
GMOCK_ON_CALL_IMPL_(obj, InternalExpectedAt, call) | GMOCK_ON_CALL_IMPL_(obj, InternalExpectedAt, call) | |||
#endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ | #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ | |||
End of changes. 15 change blocks. | ||||
61 lines changed or deleted | 114 lines changed or added |