shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame^] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | |
| 32 | #include <gmock/gmock-generated-nice-strict.h> |
| 33 | |
| 34 | #include <string> |
| 35 | #include <gmock/gmock.h> |
| 36 | #include <gtest/gtest.h> |
| 37 | #include <gtest/gtest-spi.h> |
| 38 | |
| 39 | namespace testing { |
| 40 | namespace gmock_nice_strict_test { |
| 41 | |
| 42 | using testing::internal::string; |
| 43 | using testing::GMOCK_FLAG(verbose); |
| 44 | using testing::HasSubstr; |
| 45 | using testing::NiceMock; |
| 46 | using testing::StrictMock; |
| 47 | |
| 48 | // Defines some mock classes needed by the tests. |
| 49 | |
| 50 | class Foo { |
| 51 | public: |
| 52 | virtual ~Foo() {} |
| 53 | |
| 54 | virtual void DoThis() = 0; |
| 55 | virtual int DoThat(bool flag) = 0; |
| 56 | }; |
| 57 | |
| 58 | class MockFoo : public Foo { |
| 59 | public: |
| 60 | void Delete() { delete this; } |
| 61 | |
| 62 | MOCK_METHOD0(DoThis, void()); |
| 63 | MOCK_METHOD1(DoThat, int(bool flag)); |
| 64 | }; |
| 65 | |
| 66 | class MockBar { |
| 67 | public: |
| 68 | explicit MockBar(const string& s) : str_(s) {} |
| 69 | |
| 70 | MockBar(char a1, char a2, string a3, string a4, int a5, int a6, |
| 71 | const string& a7, const string& a8, bool a9, bool a10) { |
| 72 | str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) + |
| 73 | static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F'); |
| 74 | } |
| 75 | |
| 76 | virtual ~MockBar() {} |
| 77 | |
| 78 | const string& str() const { return str_; } |
| 79 | |
| 80 | MOCK_METHOD0(This, int()); |
| 81 | MOCK_METHOD2(That, string(int, bool)); |
| 82 | |
| 83 | private: |
| 84 | string str_; |
| 85 | }; |
| 86 | |
| 87 | // TODO(wan@google.com): find a way to re-enable these tests. |
| 88 | #if 0 |
| 89 | |
| 90 | // Tests that a nice mock generates no warning for uninteresting calls. |
| 91 | TEST(NiceMockTest, NoWarningForUninterestingCall) { |
| 92 | NiceMock<MockFoo> nice_foo; |
| 93 | |
| 94 | CaptureTestStdout(); |
| 95 | nice_foo.DoThis(); |
| 96 | nice_foo.DoThat(true); |
| 97 | EXPECT_EQ("", GetCapturedTestStdout()); |
| 98 | } |
| 99 | |
| 100 | // Tests that a nice mock generates no warning for uninteresting calls |
| 101 | // that delete the mock object. |
| 102 | TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) { |
| 103 | NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>; |
| 104 | |
| 105 | ON_CALL(*nice_foo, DoThis()) |
| 106 | .WillByDefault(Invoke(nice_foo, &MockFoo::Delete)); |
| 107 | |
| 108 | CaptureTestStdout(); |
| 109 | nice_foo->DoThis(); |
| 110 | EXPECT_EQ("", GetCapturedTestStdout()); |
| 111 | } |
| 112 | |
| 113 | // Tests that a nice mock generates informational logs for |
| 114 | // uninteresting calls. |
| 115 | TEST(NiceMockTest, InfoForUninterestingCall) { |
| 116 | NiceMock<MockFoo> nice_foo; |
| 117 | |
| 118 | GMOCK_FLAG(verbose) = "info"; |
| 119 | CaptureTestStdout(); |
| 120 | nice_foo.DoThis(); |
| 121 | EXPECT_THAT(GetCapturedTestStdout(), |
| 122 | HasSubstr("Uninteresting mock function call")); |
| 123 | |
| 124 | CaptureTestStdout(); |
| 125 | nice_foo.DoThat(true); |
| 126 | EXPECT_THAT(GetCapturedTestStdout(), |
| 127 | HasSubstr("Uninteresting mock function call")); |
| 128 | } |
| 129 | |
| 130 | #endif // 0 |
| 131 | |
| 132 | // Tests that a nice mock allows expected calls. |
| 133 | TEST(NiceMockTest, AllowsExpectedCall) { |
| 134 | NiceMock<MockFoo> nice_foo; |
| 135 | |
| 136 | EXPECT_CALL(nice_foo, DoThis()); |
| 137 | nice_foo.DoThis(); |
| 138 | } |
| 139 | |
| 140 | // Tests that an unexpected call on a nice mock fails. |
| 141 | TEST(NiceMockTest, UnexpectedCallFails) { |
| 142 | NiceMock<MockFoo> nice_foo; |
| 143 | |
| 144 | EXPECT_CALL(nice_foo, DoThis()).Times(0); |
| 145 | EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected"); |
| 146 | } |
| 147 | |
| 148 | // Tests that NiceMock works with a mock class that has a non-default |
| 149 | // constructor. |
| 150 | TEST(NiceMockTest, NonDefaultConstructor) { |
| 151 | NiceMock<MockBar> nice_bar("hi"); |
| 152 | EXPECT_EQ("hi", nice_bar.str()); |
| 153 | |
| 154 | nice_bar.This(); |
| 155 | nice_bar.That(5, true); |
| 156 | } |
| 157 | |
| 158 | // Tests that NiceMock works with a mock class that has a 10-ary |
| 159 | // non-default constructor. |
| 160 | TEST(NiceMockTest, NonDefaultConstructor10) { |
| 161 | NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f', |
| 162 | "g", "h", true, false); |
| 163 | EXPECT_EQ("abcdefghTF", nice_bar.str()); |
| 164 | |
| 165 | nice_bar.This(); |
| 166 | nice_bar.That(5, true); |
| 167 | } |
| 168 | |
| 169 | // Tests that a strict mock allows expected calls. |
| 170 | TEST(StrictMockTest, AllowsExpectedCall) { |
| 171 | StrictMock<MockFoo> strict_foo; |
| 172 | |
| 173 | EXPECT_CALL(strict_foo, DoThis()); |
| 174 | strict_foo.DoThis(); |
| 175 | } |
| 176 | |
| 177 | // Tests that an unexpected call on a strict mock fails. |
| 178 | TEST(StrictMockTest, UnexpectedCallFails) { |
| 179 | StrictMock<MockFoo> strict_foo; |
| 180 | |
| 181 | EXPECT_CALL(strict_foo, DoThis()).Times(0); |
| 182 | EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(), |
| 183 | "called more times than expected"); |
| 184 | } |
| 185 | |
| 186 | // Tests that an uninteresting call on a strict mock fails. |
| 187 | TEST(StrictMockTest, UninterestingCallFails) { |
| 188 | StrictMock<MockFoo> strict_foo; |
| 189 | |
| 190 | EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(), |
| 191 | "Uninteresting mock function call"); |
| 192 | } |
| 193 | |
| 194 | // Tests that an uninteresting call on a strict mock fails, even if |
| 195 | // the call deletes the mock object. |
| 196 | TEST(StrictMockTest, UninterestingCallFailsAfterDeath) { |
| 197 | StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>; |
| 198 | |
| 199 | ON_CALL(*strict_foo, DoThis()) |
| 200 | .WillByDefault(Invoke(strict_foo, &MockFoo::Delete)); |
| 201 | |
| 202 | EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(), |
| 203 | "Uninteresting mock function call"); |
| 204 | } |
| 205 | |
| 206 | // Tests that StrictMock works with a mock class that has a |
| 207 | // non-default constructor. |
| 208 | TEST(StrictMockTest, NonDefaultConstructor) { |
| 209 | StrictMock<MockBar> strict_bar("hi"); |
| 210 | EXPECT_EQ("hi", strict_bar.str()); |
| 211 | |
| 212 | EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true), |
| 213 | "Uninteresting mock function call"); |
| 214 | } |
| 215 | |
| 216 | // Tests that StrictMock works with a mock class that has a 10-ary |
| 217 | // non-default constructor. |
| 218 | TEST(StrictMockTest, NonDefaultConstructor10) { |
| 219 | StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f', |
| 220 | "g", "h", true, false); |
| 221 | EXPECT_EQ("abcdefghTF", strict_bar.str()); |
| 222 | |
| 223 | EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true), |
| 224 | "Uninteresting mock function call"); |
| 225 | } |
| 226 | |
| 227 | } // namespace gmock_nice_strict_test |
| 228 | } // namespace testing |