Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:03:07

0001 // Copyright 2008, Google Inc.
0002 // All rights reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 
0030 // Implements class templates NiceMock, NaggyMock, and StrictMock.
0031 //
0032 // Given a mock class MockFoo that is created using Google Mock,
0033 // NiceMock<MockFoo> is a subclass of MockFoo that allows
0034 // uninteresting calls (i.e. calls to mock methods that have no
0035 // EXPECT_CALL specs), NaggyMock<MockFoo> is a subclass of MockFoo
0036 // that prints a warning when an uninteresting call occurs, and
0037 // StrictMock<MockFoo> is a subclass of MockFoo that treats all
0038 // uninteresting calls as errors.
0039 //
0040 // Currently a mock is naggy by default, so MockFoo and
0041 // NaggyMock<MockFoo> behave like the same.  However, we will soon
0042 // switch the default behavior of mocks to be nice, as that in general
0043 // leads to more maintainable tests.  When that happens, MockFoo will
0044 // stop behaving like NaggyMock<MockFoo> and start behaving like
0045 // NiceMock<MockFoo>.
0046 //
0047 // NiceMock, NaggyMock, and StrictMock "inherit" the constructors of
0048 // their respective base class.  Therefore you can write
0049 // NiceMock<MockFoo>(5, "a") to construct a nice mock where MockFoo
0050 // has a constructor that accepts (int, const char*), for example.
0051 //
0052 // A known limitation is that NiceMock<MockFoo>, NaggyMock<MockFoo>,
0053 // and StrictMock<MockFoo> only works for mock methods defined using
0054 // the MOCK_METHOD* family of macros DIRECTLY in the MockFoo class.
0055 // If a mock method is defined in a base class of MockFoo, the "nice"
0056 // or "strict" modifier may not affect it, depending on the compiler.
0057 // In particular, nesting NiceMock, NaggyMock, and StrictMock is NOT
0058 // supported.
0059 
0060 // IWYU pragma: private, include "gmock/gmock.h"
0061 // IWYU pragma: friend gmock/.*
0062 
0063 #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
0064 #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
0065 
0066 #include <cstdint>
0067 #include <type_traits>
0068 
0069 #include "gmock/gmock-spec-builders.h"
0070 #include "gmock/internal/gmock-port.h"
0071 
0072 namespace testing {
0073 template <class MockClass>
0074 class NiceMock;
0075 template <class MockClass>
0076 class NaggyMock;
0077 template <class MockClass>
0078 class StrictMock;
0079 
0080 namespace internal {
0081 template <typename T>
0082 std::true_type StrictnessModifierProbe(const NiceMock<T>&);
0083 template <typename T>
0084 std::true_type StrictnessModifierProbe(const NaggyMock<T>&);
0085 template <typename T>
0086 std::true_type StrictnessModifierProbe(const StrictMock<T>&);
0087 std::false_type StrictnessModifierProbe(...);
0088 
0089 template <typename T>
0090 constexpr bool HasStrictnessModifier() {
0091   return decltype(StrictnessModifierProbe(std::declval<const T&>()))::value;
0092 }
0093 
0094 // Base classes that register and deregister with testing::Mock to alter the
0095 // default behavior around uninteresting calls. Inheriting from one of these
0096 // classes first and then MockClass ensures the MockClass constructor is run
0097 // after registration, and that the MockClass destructor runs before
0098 // deregistration. This guarantees that MockClass's constructor and destructor
0099 // run with the same level of strictness as its instance methods.
0100 
0101 #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW && \
0102     (defined(_MSC_VER) || defined(__clang__))
0103 // We need to mark these classes with this declspec to ensure that
0104 // the empty base class optimization is performed.
0105 #define GTEST_INTERNAL_EMPTY_BASE_CLASS __declspec(empty_bases)
0106 #else
0107 #define GTEST_INTERNAL_EMPTY_BASE_CLASS
0108 #endif
0109 
0110 template <typename Base>
0111 class NiceMockImpl {
0112  public:
0113   NiceMockImpl() {
0114     ::testing::Mock::AllowUninterestingCalls(reinterpret_cast<uintptr_t>(this));
0115   }
0116 
0117   ~NiceMockImpl() {
0118     ::testing::Mock::UnregisterCallReaction(reinterpret_cast<uintptr_t>(this));
0119   }
0120 };
0121 
0122 template <typename Base>
0123 class NaggyMockImpl {
0124  public:
0125   NaggyMockImpl() {
0126     ::testing::Mock::WarnUninterestingCalls(reinterpret_cast<uintptr_t>(this));
0127   }
0128 
0129   ~NaggyMockImpl() {
0130     ::testing::Mock::UnregisterCallReaction(reinterpret_cast<uintptr_t>(this));
0131   }
0132 };
0133 
0134 template <typename Base>
0135 class StrictMockImpl {
0136  public:
0137   StrictMockImpl() {
0138     ::testing::Mock::FailUninterestingCalls(reinterpret_cast<uintptr_t>(this));
0139   }
0140 
0141   ~StrictMockImpl() {
0142     ::testing::Mock::UnregisterCallReaction(reinterpret_cast<uintptr_t>(this));
0143   }
0144 };
0145 
0146 }  // namespace internal
0147 
0148 template <class MockClass>
0149 class GTEST_INTERNAL_EMPTY_BASE_CLASS NiceMock
0150     : private internal::NiceMockImpl<MockClass>,
0151       public MockClass {
0152  public:
0153   static_assert(!internal::HasStrictnessModifier<MockClass>(),
0154                 "Can't apply NiceMock to a class hierarchy that already has a "
0155                 "strictness modifier. See "
0156                 "https://google.github.io/googletest/"
0157                 "gmock_cook_book.html#NiceStrictNaggy");
0158   NiceMock() : MockClass() {
0159     static_assert(sizeof(*this) == sizeof(MockClass),
0160                   "The impl subclass shouldn't introduce any padding");
0161   }
0162 
0163   // Ideally, we would inherit base class's constructors through a using
0164   // declaration, which would preserve their visibility. However, many existing
0165   // tests rely on the fact that current implementation reexports protected
0166   // constructors as public. These tests would need to be cleaned up first.
0167 
0168   // Single argument constructor is special-cased so that it can be
0169   // made explicit.
0170   template <typename A>
0171   explicit NiceMock(A&& arg) : MockClass(std::forward<A>(arg)) {
0172     static_assert(sizeof(*this) == sizeof(MockClass),
0173                   "The impl subclass shouldn't introduce any padding");
0174   }
0175 
0176   template <typename TArg1, typename TArg2, typename... An>
0177   NiceMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
0178       : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
0179                   std::forward<An>(args)...) {
0180     static_assert(sizeof(*this) == sizeof(MockClass),
0181                   "The impl subclass shouldn't introduce any padding");
0182   }
0183 
0184  private:
0185   NiceMock(const NiceMock&) = delete;
0186   NiceMock& operator=(const NiceMock&) = delete;
0187 };
0188 
0189 template <class MockClass>
0190 class GTEST_INTERNAL_EMPTY_BASE_CLASS NaggyMock
0191     : private internal::NaggyMockImpl<MockClass>,
0192       public MockClass {
0193   static_assert(!internal::HasStrictnessModifier<MockClass>(),
0194                 "Can't apply NaggyMock to a class hierarchy that already has a "
0195                 "strictness modifier. See "
0196                 "https://google.github.io/googletest/"
0197                 "gmock_cook_book.html#NiceStrictNaggy");
0198 
0199  public:
0200   NaggyMock() : MockClass() {
0201     static_assert(sizeof(*this) == sizeof(MockClass),
0202                   "The impl subclass shouldn't introduce any padding");
0203   }
0204 
0205   // Ideally, we would inherit base class's constructors through a using
0206   // declaration, which would preserve their visibility. However, many existing
0207   // tests rely on the fact that current implementation reexports protected
0208   // constructors as public. These tests would need to be cleaned up first.
0209 
0210   // Single argument constructor is special-cased so that it can be
0211   // made explicit.
0212   template <typename A>
0213   explicit NaggyMock(A&& arg) : MockClass(std::forward<A>(arg)) {
0214     static_assert(sizeof(*this) == sizeof(MockClass),
0215                   "The impl subclass shouldn't introduce any padding");
0216   }
0217 
0218   template <typename TArg1, typename TArg2, typename... An>
0219   NaggyMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
0220       : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
0221                   std::forward<An>(args)...) {
0222     static_assert(sizeof(*this) == sizeof(MockClass),
0223                   "The impl subclass shouldn't introduce any padding");
0224   }
0225 
0226  private:
0227   NaggyMock(const NaggyMock&) = delete;
0228   NaggyMock& operator=(const NaggyMock&) = delete;
0229 };
0230 
0231 template <class MockClass>
0232 class GTEST_INTERNAL_EMPTY_BASE_CLASS StrictMock
0233     : private internal::StrictMockImpl<MockClass>,
0234       public MockClass {
0235  public:
0236   static_assert(
0237       !internal::HasStrictnessModifier<MockClass>(),
0238       "Can't apply StrictMock to a class hierarchy that already has a "
0239       "strictness modifier. See "
0240       "https://google.github.io/googletest/"
0241       "gmock_cook_book.html#NiceStrictNaggy");
0242   StrictMock() : MockClass() {
0243     static_assert(sizeof(*this) == sizeof(MockClass),
0244                   "The impl subclass shouldn't introduce any padding");
0245   }
0246 
0247   // Ideally, we would inherit base class's constructors through a using
0248   // declaration, which would preserve their visibility. However, many existing
0249   // tests rely on the fact that current implementation reexports protected
0250   // constructors as public. These tests would need to be cleaned up first.
0251 
0252   // Single argument constructor is special-cased so that it can be
0253   // made explicit.
0254   template <typename A>
0255   explicit StrictMock(A&& arg) : MockClass(std::forward<A>(arg)) {
0256     static_assert(sizeof(*this) == sizeof(MockClass),
0257                   "The impl subclass shouldn't introduce any padding");
0258   }
0259 
0260   template <typename TArg1, typename TArg2, typename... An>
0261   StrictMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
0262       : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
0263                   std::forward<An>(args)...) {
0264     static_assert(sizeof(*this) == sizeof(MockClass),
0265                   "The impl subclass shouldn't introduce any padding");
0266   }
0267 
0268  private:
0269   StrictMock(const StrictMock&) = delete;
0270   StrictMock& operator=(const StrictMock&) = delete;
0271 };
0272 
0273 #undef GTEST_INTERNAL_EMPTY_BASE_CLASS
0274 
0275 }  // namespace testing
0276 
0277 #endif  // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_