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 | |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 39 | // This must not be defined inside the ::testing namespace, or it will |
| 40 | // clash with ::testing::Mock. |
| 41 | class Mock { |
| 42 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 43 | Mock() {} |
| 44 | |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 45 | MOCK_METHOD0(DoThis, void()); |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock); |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 51 | namespace testing { |
| 52 | namespace gmock_nice_strict_test { |
| 53 | |
| 54 | using testing::internal::string; |
| 55 | using testing::GMOCK_FLAG(verbose); |
| 56 | using testing::HasSubstr; |
| 57 | using testing::NiceMock; |
| 58 | using testing::StrictMock; |
| 59 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 60 | #if GTEST_HAS_STREAM_REDIRECTION_ |
| 61 | using testing::internal::CaptureStdout; |
| 62 | using testing::internal::GetCapturedStdout; |
| 63 | #endif // GTEST_HAS_STREAM_REDIRECTION_ |
| 64 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 65 | // Defines some mock classes needed by the tests. |
| 66 | |
| 67 | class Foo { |
| 68 | public: |
| 69 | virtual ~Foo() {} |
| 70 | |
| 71 | virtual void DoThis() = 0; |
| 72 | virtual int DoThat(bool flag) = 0; |
| 73 | }; |
| 74 | |
| 75 | class MockFoo : public Foo { |
| 76 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 77 | MockFoo() {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 78 | void Delete() { delete this; } |
| 79 | |
| 80 | MOCK_METHOD0(DoThis, void()); |
| 81 | MOCK_METHOD1(DoThat, int(bool flag)); |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | class MockBar { |
| 88 | public: |
| 89 | explicit MockBar(const string& s) : str_(s) {} |
| 90 | |
| 91 | MockBar(char a1, char a2, string a3, string a4, int a5, int a6, |
| 92 | const string& a7, const string& a8, bool a9, bool a10) { |
| 93 | str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) + |
| 94 | static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F'); |
| 95 | } |
| 96 | |
| 97 | virtual ~MockBar() {} |
| 98 | |
| 99 | const string& str() const { return str_; } |
| 100 | |
| 101 | MOCK_METHOD0(This, int()); |
| 102 | MOCK_METHOD2(That, string(int, bool)); |
| 103 | |
| 104 | private: |
| 105 | string str_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 106 | |
| 107 | GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 110 | #if GTEST_HAS_STREAM_REDIRECTION_ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 111 | |
| 112 | // Tests that a nice mock generates no warning for uninteresting calls. |
| 113 | TEST(NiceMockTest, NoWarningForUninterestingCall) { |
| 114 | NiceMock<MockFoo> nice_foo; |
| 115 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 116 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 117 | nice_foo.DoThis(); |
| 118 | nice_foo.DoThat(true); |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 119 | EXPECT_STREQ("", GetCapturedStdout().c_str()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Tests that a nice mock generates no warning for uninteresting calls |
| 123 | // that delete the mock object. |
| 124 | TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) { |
| 125 | NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>; |
| 126 | |
| 127 | ON_CALL(*nice_foo, DoThis()) |
| 128 | .WillByDefault(Invoke(nice_foo, &MockFoo::Delete)); |
| 129 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 130 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 131 | nice_foo->DoThis(); |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 132 | EXPECT_STREQ("", GetCapturedStdout().c_str()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Tests that a nice mock generates informational logs for |
| 136 | // uninteresting calls. |
| 137 | TEST(NiceMockTest, InfoForUninterestingCall) { |
| 138 | NiceMock<MockFoo> nice_foo; |
| 139 | |
| 140 | GMOCK_FLAG(verbose) = "info"; |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 141 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 142 | nice_foo.DoThis(); |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 143 | EXPECT_THAT(GetCapturedStdout(), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 144 | HasSubstr("Uninteresting mock function call")); |
| 145 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 146 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 147 | nice_foo.DoThat(true); |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 148 | EXPECT_THAT(GetCapturedStdout(), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 149 | HasSubstr("Uninteresting mock function call")); |
| 150 | } |
| 151 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 152 | #endif // GTEST_HAS_STREAM_REDIRECTION_ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 153 | |
| 154 | // Tests that a nice mock allows expected calls. |
| 155 | TEST(NiceMockTest, AllowsExpectedCall) { |
| 156 | NiceMock<MockFoo> nice_foo; |
| 157 | |
| 158 | EXPECT_CALL(nice_foo, DoThis()); |
| 159 | nice_foo.DoThis(); |
| 160 | } |
| 161 | |
| 162 | // Tests that an unexpected call on a nice mock fails. |
| 163 | TEST(NiceMockTest, UnexpectedCallFails) { |
| 164 | NiceMock<MockFoo> nice_foo; |
| 165 | |
| 166 | EXPECT_CALL(nice_foo, DoThis()).Times(0); |
| 167 | EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected"); |
| 168 | } |
| 169 | |
| 170 | // Tests that NiceMock works with a mock class that has a non-default |
| 171 | // constructor. |
| 172 | TEST(NiceMockTest, NonDefaultConstructor) { |
| 173 | NiceMock<MockBar> nice_bar("hi"); |
| 174 | EXPECT_EQ("hi", nice_bar.str()); |
| 175 | |
| 176 | nice_bar.This(); |
| 177 | nice_bar.That(5, true); |
| 178 | } |
| 179 | |
| 180 | // Tests that NiceMock works with a mock class that has a 10-ary |
| 181 | // non-default constructor. |
| 182 | TEST(NiceMockTest, NonDefaultConstructor10) { |
| 183 | NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f', |
| 184 | "g", "h", true, false); |
| 185 | EXPECT_EQ("abcdefghTF", nice_bar.str()); |
| 186 | |
| 187 | nice_bar.This(); |
| 188 | nice_bar.That(5, true); |
| 189 | } |
| 190 | |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 191 | #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 192 | // Tests that NiceMock<Mock> compiles where Mock is a user-defined |
| 193 | // class (as opposed to ::testing::Mock). We had to workaround an |
| 194 | // MSVC 8.0 bug that caused the symbol Mock used in the definition of |
| 195 | // NiceMock to be looked up in the wrong context, and this test |
| 196 | // ensures that our fix works. |
zhanyong.wan | 93244dc | 2009-09-17 19:11:00 +0000 | [diff] [blame] | 197 | // |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 198 | // We have to skip this test on Symbian and Windows Mobile, as it |
| 199 | // causes the program to crash there, for reasons unclear to us yet. |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 200 | TEST(NiceMockTest, AcceptsClassNamedMock) { |
| 201 | NiceMock< ::Mock> nice; |
| 202 | EXPECT_CALL(nice, DoThis()); |
| 203 | nice.DoThis(); |
| 204 | } |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 205 | #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 206 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 207 | // Tests that a strict mock allows expected calls. |
| 208 | TEST(StrictMockTest, AllowsExpectedCall) { |
| 209 | StrictMock<MockFoo> strict_foo; |
| 210 | |
| 211 | EXPECT_CALL(strict_foo, DoThis()); |
| 212 | strict_foo.DoThis(); |
| 213 | } |
| 214 | |
| 215 | // Tests that an unexpected call on a strict mock fails. |
| 216 | TEST(StrictMockTest, UnexpectedCallFails) { |
| 217 | StrictMock<MockFoo> strict_foo; |
| 218 | |
| 219 | EXPECT_CALL(strict_foo, DoThis()).Times(0); |
| 220 | EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(), |
| 221 | "called more times than expected"); |
| 222 | } |
| 223 | |
| 224 | // Tests that an uninteresting call on a strict mock fails. |
| 225 | TEST(StrictMockTest, UninterestingCallFails) { |
| 226 | StrictMock<MockFoo> strict_foo; |
| 227 | |
| 228 | EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(), |
| 229 | "Uninteresting mock function call"); |
| 230 | } |
| 231 | |
| 232 | // Tests that an uninteresting call on a strict mock fails, even if |
| 233 | // the call deletes the mock object. |
| 234 | TEST(StrictMockTest, UninterestingCallFailsAfterDeath) { |
| 235 | StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>; |
| 236 | |
| 237 | ON_CALL(*strict_foo, DoThis()) |
| 238 | .WillByDefault(Invoke(strict_foo, &MockFoo::Delete)); |
| 239 | |
| 240 | EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(), |
| 241 | "Uninteresting mock function call"); |
| 242 | } |
| 243 | |
| 244 | // Tests that StrictMock works with a mock class that has a |
| 245 | // non-default constructor. |
| 246 | TEST(StrictMockTest, NonDefaultConstructor) { |
| 247 | StrictMock<MockBar> strict_bar("hi"); |
| 248 | EXPECT_EQ("hi", strict_bar.str()); |
| 249 | |
| 250 | EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true), |
| 251 | "Uninteresting mock function call"); |
| 252 | } |
| 253 | |
| 254 | // Tests that StrictMock works with a mock class that has a 10-ary |
| 255 | // non-default constructor. |
| 256 | TEST(StrictMockTest, NonDefaultConstructor10) { |
| 257 | StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f', |
| 258 | "g", "h", true, false); |
| 259 | EXPECT_EQ("abcdefghTF", strict_bar.str()); |
| 260 | |
| 261 | EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true), |
| 262 | "Uninteresting mock function call"); |
| 263 | } |
| 264 | |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 265 | #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 266 | // Tests that StrictMock<Mock> compiles where Mock is a user-defined |
| 267 | // class (as opposed to ::testing::Mock). We had to workaround an |
| 268 | // MSVC 8.0 bug that caused the symbol Mock used in the definition of |
| 269 | // StrictMock to be looked up in the wrong context, and this test |
| 270 | // ensures that our fix works. |
zhanyong.wan | 93244dc | 2009-09-17 19:11:00 +0000 | [diff] [blame] | 271 | // |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 272 | // We have to skip this test on Symbian and Windows Mobile, as it |
| 273 | // causes the program to crash there, for reasons unclear to us yet. |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 274 | TEST(StrictMockTest, AcceptsClassNamedMock) { |
zhanyong.wan | 93244dc | 2009-09-17 19:11:00 +0000 | [diff] [blame] | 275 | StrictMock< ::Mock> strict; |
| 276 | EXPECT_CALL(strict, DoThis()); |
| 277 | strict.DoThis(); |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 278 | } |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 279 | #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 280 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 281 | } // namespace gmock_nice_strict_test |
| 282 | } // namespace testing |