2 $$ This is a Pump source file. Please use Pump to convert it to
3 $$ gmock-generated-nice-strict.h.
5 $var n = 10 $$ The maximum arity we support.
6 // Copyright 2008, Google Inc.
7 // All rights reserved.
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
13 // * Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 // * Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following disclaimer
17 // in the documentation and/or other materials provided with the
19 // * Neither the name of Google Inc. nor the names of its
20 // contributors may be used to endorse or promote products derived from
21 // this software without specific prior written permission.
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 // Author: wan@google.com (Zhanyong Wan)
37 // Implements class templates NiceMock, NaggyMock, and StrictMock.
39 // Given a mock class MockFoo that is created using Google Mock,
40 // NiceMock<MockFoo> is a subclass of MockFoo that allows
41 // uninteresting calls (i.e. calls to mock methods that have no
42 // EXPECT_CALL specs), NaggyMock<MockFoo> is a subclass of MockFoo
43 // that prints a warning when an uninteresting call occurs, and
44 // StrictMock<MockFoo> is a subclass of MockFoo that treats all
45 // uninteresting calls as errors.
47 // Currently a mock is naggy by default, so MockFoo and
48 // NaggyMock<MockFoo> behave like the same. However, we will soon
49 // switch the default behavior of mocks to be nice, as that in general
50 // leads to more maintainable tests. When that happens, MockFoo will
51 // stop behaving like NaggyMock<MockFoo> and start behaving like
54 // NiceMock, NaggyMock, and StrictMock "inherit" the constructors of
55 // their respective base class, with up-to $n arguments. Therefore
56 // you can write NiceMock<MockFoo>(5, "a") to construct a nice mock
57 // where MockFoo has a constructor that accepts (int, const char*),
60 // A known limitation is that NiceMock<MockFoo>, NaggyMock<MockFoo>,
61 // and StrictMock<MockFoo> only works for mock methods defined using
62 // the MOCK_METHOD* family of macros DIRECTLY in the MockFoo class.
63 // If a mock method is defined in a base class of MockFoo, the "nice"
64 // or "strict" modifier may not affect it, depending on the compiler.
65 // In particular, nesting NiceMock, NaggyMock, and StrictMock is NOT
68 // Another known limitation is that the constructors of the base mock
69 // cannot have arguments passed by non-const reference, which are
70 // banned by the Google C++ style guide anyway.
72 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_
73 #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_
75 #include "gmock/gmock-spec-builders.h"
76 #include "gmock/internal/gmock-port.h"
83 $var clazz=[[$if kind==0 [[NiceMock]]
84 $elif kind==1 [[NaggyMock]]
85 $else [[StrictMock]]]]
87 $var method=[[$if kind==0 [[AllowUninterestingCalls]]
88 $elif kind==1 [[WarnUninterestingCalls]]
89 $else [[FailUninterestingCalls]]]]
91 template <class MockClass>
92 class $clazz : public MockClass {
94 // We don't factor out the constructor body to a common method, as
95 // we have to avoid a possible clash with members of MockClass.
97 ::testing::Mock::$method(
98 internal::ImplicitCast_<MockClass*>(this));
101 // C++ doesn't (yet) allow inheritance of constructors, so we have
102 // to define it for each arity.
103 template <typename A1>
104 explicit $clazz(const A1& a1) : MockClass(a1) {
105 ::testing::Mock::$method(
106 internal::ImplicitCast_<MockClass*>(this));
112 template <$for j, [[typename A$j]]>
113 $clazz($for j, [[const A$j& a$j]]) : MockClass($for j, [[a$j]]) {
114 ::testing::Mock::$method(
115 internal::ImplicitCast_<MockClass*>(this));
121 ::testing::Mock::UnregisterCallReaction(
122 internal::ImplicitCast_<MockClass*>(this));
126 GTEST_DISALLOW_COPY_AND_ASSIGN_($clazz);
131 // The following specializations catch some (relatively more common)
132 // user errors of nesting nice and strict mocks. They do NOT catch
133 // all possible errors.
135 // These specializations are declared but not defined, as NiceMock,
136 // NaggyMock, and StrictMock cannot be nested.
138 template <typename MockClass>
139 class NiceMock<NiceMock<MockClass> >;
140 template <typename MockClass>
141 class NiceMock<NaggyMock<MockClass> >;
142 template <typename MockClass>
143 class NiceMock<StrictMock<MockClass> >;
145 template <typename MockClass>
146 class NaggyMock<NiceMock<MockClass> >;
147 template <typename MockClass>
148 class NaggyMock<NaggyMock<MockClass> >;
149 template <typename MockClass>
150 class NaggyMock<StrictMock<MockClass> >;
152 template <typename MockClass>
153 class StrictMock<NiceMock<MockClass> >;
154 template <typename MockClass>
155 class StrictMock<NaggyMock<MockClass> >;
156 template <typename MockClass>
157 class StrictMock<StrictMock<MockClass> >;
159 } // namespace testing
161 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_