"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "googlemock/include/gmock/gmock-nice-strict.h" between
googletest-release-1.10.0.tar.gz and googletest-release-1.11.0.tar.gz

About: GoogleTest is Google's (unit) testing and mocking framework for C++ tests.

gmock-nice-strict.h  (googletest-release-1.10.0):gmock-nice-strict.h  (googletest-release-1.11.0)
skipping to change at line 62 skipping to change at line 62
// A known limitation is that NiceMock<MockFoo>, NaggyMock<MockFoo>, // A known limitation is that NiceMock<MockFoo>, NaggyMock<MockFoo>,
// and StrictMock<MockFoo> only works for mock methods defined using // and StrictMock<MockFoo> only works for mock methods defined using
// the MOCK_METHOD* family of macros DIRECTLY in the MockFoo class. // the MOCK_METHOD* family of macros DIRECTLY in the MockFoo class.
// If a mock method is defined in a base class of MockFoo, the "nice" // If a mock method is defined in a base class of MockFoo, the "nice"
// or "strict" modifier may not affect it, depending on the compiler. // or "strict" modifier may not affect it, depending on the compiler.
// In particular, nesting NiceMock, NaggyMock, and StrictMock is NOT // In particular, nesting NiceMock, NaggyMock, and StrictMock is NOT
// supported. // supported.
// GOOGLETEST_CM0002 DO NOT DELETE // GOOGLETEST_CM0002 DO NOT DELETE
#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_ #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
#define GMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_ #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
#include <type_traits>
#include "gmock/gmock-spec-builders.h" #include "gmock/gmock-spec-builders.h"
#include "gmock/internal/gmock-port.h" #include "gmock/internal/gmock-port.h"
namespace testing { namespace testing {
template <class MockClass>
class NiceMock;
template <class MockClass>
class NaggyMock;
template <class MockClass>
class StrictMock;
namespace internal {
template <typename T>
std::true_type StrictnessModifierProbe(const NiceMock<T>&);
template <typename T>
std::true_type StrictnessModifierProbe(const NaggyMock<T>&);
template <typename T>
std::true_type StrictnessModifierProbe(const StrictMock<T>&);
std::false_type StrictnessModifierProbe(...);
template <typename T>
constexpr bool HasStrictnessModifier() {
return decltype(StrictnessModifierProbe(std::declval<const T&>()))::value;
}
// Base classes that register and deregister with testing::Mock to alter the
// default behavior around uninteresting calls. Inheriting from one of these
// classes first and then MockClass ensures the MockClass constructor is run
// after registration, and that the MockClass destructor runs before
// deregistration. This guarantees that MockClass's constructor and destructor
// run with the same level of strictness as its instance methods.
#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW && \
(defined(_MSC_VER) || defined(__clang__))
// We need to mark these classes with this declspec to ensure that
// the empty base class optimization is performed.
#define GTEST_INTERNAL_EMPTY_BASE_CLASS __declspec(empty_bases)
#else
#define GTEST_INTERNAL_EMPTY_BASE_CLASS
#endif
template <typename Base>
class NiceMockImpl {
public:
NiceMockImpl() { ::testing::Mock::AllowUninterestingCalls(this); }
~NiceMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
};
template <typename Base>
class NaggyMockImpl {
public:
NaggyMockImpl() { ::testing::Mock::WarnUninterestingCalls(this); }
~NaggyMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
};
template <typename Base>
class StrictMockImpl {
public:
StrictMockImpl() { ::testing::Mock::FailUninterestingCalls(this); }
~StrictMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
};
} // namespace internal
template <class MockClass> template <class MockClass>
class NiceMock : public MockClass { class GTEST_INTERNAL_EMPTY_BASE_CLASS NiceMock
: private internal::NiceMockImpl<MockClass>,
public MockClass {
public: public:
static_assert(!internal::HasStrictnessModifier<MockClass>(),
"Can't apply NiceMock to a class hierarchy that already has a "
"strictness modifier. See "
"https://google.github.io/googletest/"
"gmock_cook_book.html#NiceStrictNaggy");
NiceMock() : MockClass() { NiceMock() : MockClass() {
::testing::Mock::AllowUninterestingCalls( static_assert(sizeof(*this) == sizeof(MockClass),
internal::ImplicitCast_<MockClass*>(this)); "The impl subclass shouldn't introduce any padding");
} }
// Ideally, we would inherit base class's constructors through a using // Ideally, we would inherit base class's constructors through a using
// declaration, which would preserve their visibility. However, many existing // declaration, which would preserve their visibility. However, many existing
// tests rely on the fact that current implementation reexports protected // tests rely on the fact that current implementation reexports protected
// constructors as public. These tests would need to be cleaned up first. // constructors as public. These tests would need to be cleaned up first.
// Single argument constructor is special-cased so that it can be // Single argument constructor is special-cased so that it can be
// made explicit. // made explicit.
template <typename A> template <typename A>
explicit NiceMock(A&& arg) : MockClass(std::forward<A>(arg)) { explicit NiceMock(A&& arg) : MockClass(std::forward<A>(arg)) {
::testing::Mock::AllowUninterestingCalls( static_assert(sizeof(*this) == sizeof(MockClass),
internal::ImplicitCast_<MockClass*>(this)); "The impl subclass shouldn't introduce any padding");
} }
template <typename A1, typename A2, typename... An> template <typename TArg1, typename TArg2, typename... An>
NiceMock(A1&& arg1, A2&& arg2, An&&... args) NiceMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
: MockClass(std::forward<A1>(arg1), std::forward<A2>(arg2), : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
std::forward<An>(args)...) { std::forward<An>(args)...) {
::testing::Mock::AllowUninterestingCalls( static_assert(sizeof(*this) == sizeof(MockClass),
internal::ImplicitCast_<MockClass*>(this)); "The impl subclass shouldn't introduce any padding");
}
~NiceMock() { // NOLINT
::testing::Mock::UnregisterCallReaction(
internal::ImplicitCast_<MockClass*>(this));
} }
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(NiceMock); GTEST_DISALLOW_COPY_AND_ASSIGN_(NiceMock);
}; };
template <class MockClass> template <class MockClass>
class NaggyMock : public MockClass { class GTEST_INTERNAL_EMPTY_BASE_CLASS NaggyMock
: private internal::NaggyMockImpl<MockClass>,
public MockClass {
static_assert(!internal::HasStrictnessModifier<MockClass>(),
"Can't apply NaggyMock to a class hierarchy that already has a "
"strictness modifier. See "
"https://google.github.io/googletest/"
"gmock_cook_book.html#NiceStrictNaggy");
public: public:
NaggyMock() : MockClass() { NaggyMock() : MockClass() {
::testing::Mock::WarnUninterestingCalls( static_assert(sizeof(*this) == sizeof(MockClass),
internal::ImplicitCast_<MockClass*>(this)); "The impl subclass shouldn't introduce any padding");
} }
// Ideally, we would inherit base class's constructors through a using // Ideally, we would inherit base class's constructors through a using
// declaration, which would preserve their visibility. However, many existing // declaration, which would preserve their visibility. However, many existing
// tests rely on the fact that current implementation reexports protected // tests rely on the fact that current implementation reexports protected
// constructors as public. These tests would need to be cleaned up first. // constructors as public. These tests would need to be cleaned up first.
// Single argument constructor is special-cased so that it can be // Single argument constructor is special-cased so that it can be
// made explicit. // made explicit.
template <typename A> template <typename A>
explicit NaggyMock(A&& arg) : MockClass(std::forward<A>(arg)) { explicit NaggyMock(A&& arg) : MockClass(std::forward<A>(arg)) {
::testing::Mock::WarnUninterestingCalls( static_assert(sizeof(*this) == sizeof(MockClass),
internal::ImplicitCast_<MockClass*>(this)); "The impl subclass shouldn't introduce any padding");
} }
template <typename A1, typename A2, typename... An> template <typename TArg1, typename TArg2, typename... An>
NaggyMock(A1&& arg1, A2&& arg2, An&&... args) NaggyMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
: MockClass(std::forward<A1>(arg1), std::forward<A2>(arg2), : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
std::forward<An>(args)...) { std::forward<An>(args)...) {
::testing::Mock::WarnUninterestingCalls( static_assert(sizeof(*this) == sizeof(MockClass),
internal::ImplicitCast_<MockClass*>(this)); "The impl subclass shouldn't introduce any padding");
}
~NaggyMock() { // NOLINT
::testing::Mock::UnregisterCallReaction(
internal::ImplicitCast_<MockClass*>(this));
} }
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(NaggyMock); GTEST_DISALLOW_COPY_AND_ASSIGN_(NaggyMock);
}; };
template <class MockClass> template <class MockClass>
class StrictMock : public MockClass { class GTEST_INTERNAL_EMPTY_BASE_CLASS StrictMock
: private internal::StrictMockImpl<MockClass>,
public MockClass {
public: public:
static_assert(
!internal::HasStrictnessModifier<MockClass>(),
"Can't apply StrictMock to a class hierarchy that already has a "
"strictness modifier. See "
"https://google.github.io/googletest/"
"gmock_cook_book.html#NiceStrictNaggy");
StrictMock() : MockClass() { StrictMock() : MockClass() {
::testing::Mock::FailUninterestingCalls( static_assert(sizeof(*this) == sizeof(MockClass),
internal::ImplicitCast_<MockClass*>(this)); "The impl subclass shouldn't introduce any padding");
} }
// Ideally, we would inherit base class's constructors through a using // Ideally, we would inherit base class's constructors through a using
// declaration, which would preserve their visibility. However, many existing // declaration, which would preserve their visibility. However, many existing
// tests rely on the fact that current implementation reexports protected // tests rely on the fact that current implementation reexports protected
// constructors as public. These tests would need to be cleaned up first. // constructors as public. These tests would need to be cleaned up first.
// Single argument constructor is special-cased so that it can be // Single argument constructor is special-cased so that it can be
// made explicit. // made explicit.
template <typename A> template <typename A>
explicit StrictMock(A&& arg) : MockClass(std::forward<A>(arg)) { explicit StrictMock(A&& arg) : MockClass(std::forward<A>(arg)) {
::testing::Mock::FailUninterestingCalls( static_assert(sizeof(*this) == sizeof(MockClass),
internal::ImplicitCast_<MockClass*>(this)); "The impl subclass shouldn't introduce any padding");
} }
template <typename A1, typename A2, typename... An> template <typename TArg1, typename TArg2, typename... An>
StrictMock(A1&& arg1, A2&& arg2, An&&... args) StrictMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
: MockClass(std::forward<A1>(arg1), std::forward<A2>(arg2), : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
std::forward<An>(args)...) { std::forward<An>(args)...) {
::testing::Mock::FailUninterestingCalls( static_assert(sizeof(*this) == sizeof(MockClass),
internal::ImplicitCast_<MockClass*>(this)); "The impl subclass shouldn't introduce any padding");
}
~StrictMock() { // NOLINT
::testing::Mock::UnregisterCallReaction(
internal::ImplicitCast_<MockClass*>(this));
} }
private: private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(StrictMock); GTEST_DISALLOW_COPY_AND_ASSIGN_(StrictMock);
}; };
// The following specializations catch some (relatively more common) #undef GTEST_INTERNAL_EMPTY_BASE_CLASS
// user errors of nesting nice and strict mocks. They do NOT catch
// all possible errors.
// These specializations are declared but not defined, as NiceMock,
// NaggyMock, and StrictMock cannot be nested.
template <typename MockClass>
class NiceMock<NiceMock<MockClass> >;
template <typename MockClass>
class NiceMock<NaggyMock<MockClass> >;
template <typename MockClass>
class NiceMock<StrictMock<MockClass> >;
template <typename MockClass>
class NaggyMock<NiceMock<MockClass> >;
template <typename MockClass>
class NaggyMock<NaggyMock<MockClass> >;
template <typename MockClass>
class NaggyMock<StrictMock<MockClass> >;
template <typename MockClass>
class StrictMock<NiceMock<MockClass> >;
template <typename MockClass>
class StrictMock<NaggyMock<MockClass> >;
template <typename MockClass>
class StrictMock<StrictMock<MockClass> >;
} // namespace testing } // namespace testing
#endif // GMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_ #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
 End of changes. 21 change blocks. 
74 lines changed or deleted 120 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)