shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | $$ -*- mode: c++; -*- |
| 2 | $$ This is a Pump source file. Please use Pump to convert it to |
| 3 | $$ gmock-generated-nice-strict.h. |
| 4 | $$ |
| 5 | $var n = 10 $$ The maximum arity we support. |
| 6 | // Copyright 2008, Google Inc. |
| 7 | // All rights reserved. |
| 8 | // |
| 9 | // Redistribution and use in source and binary forms, with or without |
| 10 | // modification, are permitted provided that the following conditions are |
| 11 | // met: |
| 12 | // |
| 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 |
| 18 | // distribution. |
| 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. |
| 22 | // |
| 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. |
| 34 | // |
| 35 | // Author: wan@google.com (Zhanyong Wan) |
| 36 | |
| 37 | // Implements class templates NiceMock and StrictMock. |
| 38 | // |
| 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), and StrictMock<MockFoo> is a subclass of |
| 43 | // MockFoo that treats all uninteresting calls as errors. |
| 44 | // |
| 45 | // NiceMock and StrictMock "inherits" the constructors of their |
| 46 | // respective base class, with up-to $n arguments. Therefore you can |
| 47 | // write NiceMock<MockFoo>(5, "a") to construct a nice mock where |
| 48 | // MockFoo has a constructor that accepts (int, const char*), for |
| 49 | // example. |
| 50 | // |
| 51 | // A known limitation is that NiceMock<MockFoo> and |
| 52 | // StrictMock<MockFoo> only works for mock methods defined using the |
| 53 | // MOCK_METHOD* family of macros DIRECTLY in the MockFoo class. If a |
| 54 | // mock method is defined in a base class of MockFoo, the "nice" or |
| 55 | // "strict" modifier may not affect it, depending on the compiler. In |
| 56 | // particular, nesting NiceMock and StrictMock is NOT supported. |
| 57 | // |
| 58 | // Another known limitation is that the constructors of the base mock |
| 59 | // cannot have arguments passed by non-const reference, which are |
| 60 | // banned by the Google C++ style guide anyway. |
| 61 | |
| 62 | #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_ |
| 63 | #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_ |
| 64 | |
| 65 | #include <gmock/gmock-spec-builders.h> |
| 66 | #include <gmock/internal/gmock-port.h> |
| 67 | |
| 68 | namespace testing { |
| 69 | |
| 70 | template <class MockClass> |
| 71 | class NiceMock : public MockClass { |
| 72 | public: |
| 73 | // We don't factor out the constructor body to a common method, as |
| 74 | // we have to avoid a possible clash with members of MockClass. |
| 75 | NiceMock() { |
| 76 | Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); |
| 77 | } |
| 78 | |
| 79 | // C++ doesn't (yet) allow inheritance of constructors, so we have |
| 80 | // to define it for each arity. |
| 81 | template <typename A1> |
| 82 | explicit NiceMock(const A1& a1) : MockClass(a1) { |
| 83 | Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); |
| 84 | } |
| 85 | |
| 86 | $range i 2..n |
| 87 | $for i [[ |
| 88 | $range j 1..i |
| 89 | template <$for j, [[typename A$j]]> |
| 90 | NiceMock($for j, [[const A$j& a$j]]) : MockClass($for j, [[a$j]]) { |
| 91 | Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | ]] |
| 96 | virtual ~NiceMock() { |
| 97 | Mock::UnregisterCallReaction(internal::implicit_cast<MockClass*>(this)); |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | template <class MockClass> |
| 102 | class StrictMock : public MockClass { |
| 103 | public: |
| 104 | // We don't factor out the constructor body to a common method, as |
| 105 | // we have to avoid a possible clash with members of MockClass. |
| 106 | StrictMock() { |
| 107 | Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); |
| 108 | } |
| 109 | |
| 110 | template <typename A1> |
| 111 | explicit StrictMock(const A1& a1) : MockClass(a1) { |
| 112 | Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); |
| 113 | } |
| 114 | |
| 115 | $for i [[ |
| 116 | $range j 1..i |
| 117 | template <$for j, [[typename A$j]]> |
| 118 | StrictMock($for j, [[const A$j& a$j]]) : MockClass($for j, [[a$j]]) { |
| 119 | Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | ]] |
| 124 | virtual ~StrictMock() { |
| 125 | Mock::UnregisterCallReaction(internal::implicit_cast<MockClass*>(this)); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | // The following specializations catch some (relatively more common) |
| 130 | // user errors of nesting nice and strict mocks. They do NOT catch |
| 131 | // all possible errors. |
| 132 | |
| 133 | // These specializations are declared but not defined, as NiceMock and |
| 134 | // StrictMock cannot be nested. |
| 135 | template <typename MockClass> |
| 136 | class NiceMock<NiceMock<MockClass> >; |
| 137 | template <typename MockClass> |
| 138 | class NiceMock<StrictMock<MockClass> >; |
| 139 | template <typename MockClass> |
| 140 | class StrictMock<NiceMock<MockClass> >; |
| 141 | template <typename MockClass> |
| 142 | class StrictMock<StrictMock<MockClass> >; |
| 143 | |
| 144 | } // namespace testing |
| 145 | |
| 146 | #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_ |