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 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 32 | #include "gmock/gmock-generated-nice-strict.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 33 | |
| 34 | #include <string> |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 35 | #include "gmock/gmock.h" |
| 36 | #include "gtest/gtest.h" |
| 37 | #include "gtest/gtest-spi.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 54 | using testing::GMOCK_FLAG(verbose); |
| 55 | using testing::HasSubstr; |
zhanyong.wan | 844fa94 | 2013-03-01 01:54:22 +0000 | [diff] [blame] | 56 | using testing::NaggyMock; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 57 | using testing::NiceMock; |
| 58 | using testing::StrictMock; |
| 59 | |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 60 | #if GTEST_HAS_STREAM_REDIRECTION |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 61 | using testing::internal::CaptureStdout; |
| 62 | using testing::internal::GetCapturedStdout; |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 63 | #endif |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 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: |
Nico Weber | 09fd5b3 | 2017-05-15 17:07:03 -0400 | [diff] [blame] | 89 | explicit MockBar(const std::string& s) : str_(s) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 90 | |
Nico Weber | 09fd5b3 | 2017-05-15 17:07:03 -0400 | [diff] [blame] | 91 | MockBar(char a1, char a2, std::string a3, std::string a4, int a5, int a6, |
| 92 | const std::string& a7, const std::string& a8, bool a9, bool a10) { |
| 93 | str_ = std::string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) + |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 94 | static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F'); |
| 95 | } |
| 96 | |
| 97 | virtual ~MockBar() {} |
| 98 | |
Nico Weber | 09fd5b3 | 2017-05-15 17:07:03 -0400 | [diff] [blame] | 99 | const std::string& str() const { return str_; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 100 | |
| 101 | MOCK_METHOD0(This, int()); |
Nico Weber | 09fd5b3 | 2017-05-15 17:07:03 -0400 | [diff] [blame] | 102 | MOCK_METHOD2(That, std::string(int, bool)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 103 | |
| 104 | private: |
Nico Weber | 09fd5b3 | 2017-05-15 17:07:03 -0400 | [diff] [blame] | 105 | std::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 | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 110 | #if GTEST_HAS_STREAM_REDIRECTION |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 111 | |
zhanyong.wan | c896504 | 2013-03-01 07:10:07 +0000 | [diff] [blame] | 112 | // Tests that a raw mock generates warnings for uninteresting calls. |
| 113 | TEST(RawMockTest, WarningForUninterestingCall) { |
Nico Weber | 09fd5b3 | 2017-05-15 17:07:03 -0400 | [diff] [blame] | 114 | const std::string saved_flag = GMOCK_FLAG(verbose); |
zhanyong.wan | c896504 | 2013-03-01 07:10:07 +0000 | [diff] [blame] | 115 | GMOCK_FLAG(verbose) = "warning"; |
| 116 | |
| 117 | MockFoo raw_foo; |
| 118 | |
| 119 | CaptureStdout(); |
| 120 | raw_foo.DoThis(); |
| 121 | raw_foo.DoThat(true); |
| 122 | EXPECT_THAT(GetCapturedStdout(), |
| 123 | HasSubstr("Uninteresting mock function call")); |
| 124 | |
| 125 | GMOCK_FLAG(verbose) = saved_flag; |
| 126 | } |
| 127 | |
| 128 | // Tests that a raw mock generates warnings for uninteresting calls |
| 129 | // that delete the mock object. |
| 130 | TEST(RawMockTest, WarningForUninterestingCallAfterDeath) { |
Nico Weber | 09fd5b3 | 2017-05-15 17:07:03 -0400 | [diff] [blame] | 131 | const std::string saved_flag = GMOCK_FLAG(verbose); |
zhanyong.wan | c896504 | 2013-03-01 07:10:07 +0000 | [diff] [blame] | 132 | GMOCK_FLAG(verbose) = "warning"; |
| 133 | |
| 134 | MockFoo* const raw_foo = new MockFoo; |
| 135 | |
| 136 | ON_CALL(*raw_foo, DoThis()) |
| 137 | .WillByDefault(Invoke(raw_foo, &MockFoo::Delete)); |
| 138 | |
| 139 | CaptureStdout(); |
| 140 | raw_foo->DoThis(); |
| 141 | EXPECT_THAT(GetCapturedStdout(), |
| 142 | HasSubstr("Uninteresting mock function call")); |
| 143 | |
| 144 | GMOCK_FLAG(verbose) = saved_flag; |
| 145 | } |
| 146 | |
| 147 | // Tests that a raw mock generates informational logs for |
| 148 | // uninteresting calls. |
| 149 | TEST(RawMockTest, InfoForUninterestingCall) { |
| 150 | MockFoo raw_foo; |
| 151 | |
Nico Weber | 09fd5b3 | 2017-05-15 17:07:03 -0400 | [diff] [blame] | 152 | const std::string saved_flag = GMOCK_FLAG(verbose); |
zhanyong.wan | c896504 | 2013-03-01 07:10:07 +0000 | [diff] [blame] | 153 | GMOCK_FLAG(verbose) = "info"; |
| 154 | CaptureStdout(); |
| 155 | raw_foo.DoThis(); |
| 156 | EXPECT_THAT(GetCapturedStdout(), |
| 157 | HasSubstr("Uninteresting mock function call")); |
| 158 | |
| 159 | GMOCK_FLAG(verbose) = saved_flag; |
| 160 | } |
| 161 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 162 | // Tests that a nice mock generates no warning for uninteresting calls. |
| 163 | TEST(NiceMockTest, NoWarningForUninterestingCall) { |
| 164 | NiceMock<MockFoo> nice_foo; |
| 165 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 166 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 167 | nice_foo.DoThis(); |
| 168 | nice_foo.DoThat(true); |
zhanyong.wan | 844fa94 | 2013-03-01 01:54:22 +0000 | [diff] [blame] | 169 | EXPECT_EQ("", GetCapturedStdout()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // Tests that a nice mock generates no warning for uninteresting calls |
| 173 | // that delete the mock object. |
| 174 | TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) { |
| 175 | NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>; |
| 176 | |
| 177 | ON_CALL(*nice_foo, DoThis()) |
| 178 | .WillByDefault(Invoke(nice_foo, &MockFoo::Delete)); |
| 179 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 180 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 181 | nice_foo->DoThis(); |
zhanyong.wan | 844fa94 | 2013-03-01 01:54:22 +0000 | [diff] [blame] | 182 | EXPECT_EQ("", GetCapturedStdout()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // Tests that a nice mock generates informational logs for |
| 186 | // uninteresting calls. |
| 187 | TEST(NiceMockTest, InfoForUninterestingCall) { |
| 188 | NiceMock<MockFoo> nice_foo; |
| 189 | |
Nico Weber | 09fd5b3 | 2017-05-15 17:07:03 -0400 | [diff] [blame] | 190 | const std::string saved_flag = GMOCK_FLAG(verbose); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 191 | GMOCK_FLAG(verbose) = "info"; |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 192 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 193 | nice_foo.DoThis(); |
zhanyong.wan | 844fa94 | 2013-03-01 01:54:22 +0000 | [diff] [blame] | 194 | EXPECT_THAT(GetCapturedStdout(), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 195 | HasSubstr("Uninteresting mock function call")); |
| 196 | |
vladlosev | 76c1c61 | 2010-05-05 19:47:46 +0000 | [diff] [blame] | 197 | GMOCK_FLAG(verbose) = saved_flag; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 198 | } |
| 199 | |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 200 | #endif // GTEST_HAS_STREAM_REDIRECTION |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 201 | |
| 202 | // Tests that a nice mock allows expected calls. |
| 203 | TEST(NiceMockTest, AllowsExpectedCall) { |
| 204 | NiceMock<MockFoo> nice_foo; |
| 205 | |
| 206 | EXPECT_CALL(nice_foo, DoThis()); |
| 207 | nice_foo.DoThis(); |
| 208 | } |
| 209 | |
| 210 | // Tests that an unexpected call on a nice mock fails. |
| 211 | TEST(NiceMockTest, UnexpectedCallFails) { |
| 212 | NiceMock<MockFoo> nice_foo; |
| 213 | |
| 214 | EXPECT_CALL(nice_foo, DoThis()).Times(0); |
| 215 | EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected"); |
| 216 | } |
| 217 | |
| 218 | // Tests that NiceMock works with a mock class that has a non-default |
| 219 | // constructor. |
| 220 | TEST(NiceMockTest, NonDefaultConstructor) { |
| 221 | NiceMock<MockBar> nice_bar("hi"); |
| 222 | EXPECT_EQ("hi", nice_bar.str()); |
| 223 | |
| 224 | nice_bar.This(); |
| 225 | nice_bar.That(5, true); |
| 226 | } |
| 227 | |
| 228 | // Tests that NiceMock works with a mock class that has a 10-ary |
| 229 | // non-default constructor. |
| 230 | TEST(NiceMockTest, NonDefaultConstructor10) { |
| 231 | NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f', |
| 232 | "g", "h", true, false); |
| 233 | EXPECT_EQ("abcdefghTF", nice_bar.str()); |
| 234 | |
| 235 | nice_bar.This(); |
| 236 | nice_bar.That(5, true); |
| 237 | } |
| 238 | |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 239 | #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 240 | // Tests that NiceMock<Mock> compiles where Mock is a user-defined |
zhanyong.wan | 844fa94 | 2013-03-01 01:54:22 +0000 | [diff] [blame] | 241 | // class (as opposed to ::testing::Mock). We had to work around an |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 242 | // MSVC 8.0 bug that caused the symbol Mock used in the definition of |
| 243 | // NiceMock to be looked up in the wrong context, and this test |
| 244 | // ensures that our fix works. |
zhanyong.wan | 93244dc | 2009-09-17 19:11:00 +0000 | [diff] [blame] | 245 | // |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 246 | // We have to skip this test on Symbian and Windows Mobile, as it |
| 247 | // causes the program to crash there, for reasons unclear to us yet. |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 248 | TEST(NiceMockTest, AcceptsClassNamedMock) { |
| 249 | NiceMock< ::Mock> nice; |
| 250 | EXPECT_CALL(nice, DoThis()); |
| 251 | nice.DoThis(); |
| 252 | } |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 253 | #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 254 | |
zhanyong.wan | 844fa94 | 2013-03-01 01:54:22 +0000 | [diff] [blame] | 255 | #if GTEST_HAS_STREAM_REDIRECTION |
| 256 | |
| 257 | // Tests that a naggy mock generates warnings for uninteresting calls. |
| 258 | TEST(NaggyMockTest, WarningForUninterestingCall) { |
Nico Weber | 09fd5b3 | 2017-05-15 17:07:03 -0400 | [diff] [blame] | 259 | const std::string saved_flag = GMOCK_FLAG(verbose); |
zhanyong.wan | 844fa94 | 2013-03-01 01:54:22 +0000 | [diff] [blame] | 260 | GMOCK_FLAG(verbose) = "warning"; |
| 261 | |
| 262 | NaggyMock<MockFoo> naggy_foo; |
| 263 | |
| 264 | CaptureStdout(); |
| 265 | naggy_foo.DoThis(); |
| 266 | naggy_foo.DoThat(true); |
| 267 | EXPECT_THAT(GetCapturedStdout(), |
| 268 | HasSubstr("Uninteresting mock function call")); |
| 269 | |
| 270 | GMOCK_FLAG(verbose) = saved_flag; |
| 271 | } |
| 272 | |
| 273 | // Tests that a naggy mock generates a warning for an uninteresting call |
| 274 | // that deletes the mock object. |
| 275 | TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) { |
Nico Weber | 09fd5b3 | 2017-05-15 17:07:03 -0400 | [diff] [blame] | 276 | const std::string saved_flag = GMOCK_FLAG(verbose); |
zhanyong.wan | 844fa94 | 2013-03-01 01:54:22 +0000 | [diff] [blame] | 277 | GMOCK_FLAG(verbose) = "warning"; |
| 278 | |
| 279 | NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>; |
| 280 | |
| 281 | ON_CALL(*naggy_foo, DoThis()) |
| 282 | .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete)); |
| 283 | |
| 284 | CaptureStdout(); |
| 285 | naggy_foo->DoThis(); |
| 286 | EXPECT_THAT(GetCapturedStdout(), |
| 287 | HasSubstr("Uninteresting mock function call")); |
| 288 | |
| 289 | GMOCK_FLAG(verbose) = saved_flag; |
| 290 | } |
| 291 | |
| 292 | #endif // GTEST_HAS_STREAM_REDIRECTION |
| 293 | |
| 294 | // Tests that a naggy mock allows expected calls. |
| 295 | TEST(NaggyMockTest, AllowsExpectedCall) { |
| 296 | NaggyMock<MockFoo> naggy_foo; |
| 297 | |
| 298 | EXPECT_CALL(naggy_foo, DoThis()); |
| 299 | naggy_foo.DoThis(); |
| 300 | } |
| 301 | |
| 302 | // Tests that an unexpected call on a naggy mock fails. |
| 303 | TEST(NaggyMockTest, UnexpectedCallFails) { |
| 304 | NaggyMock<MockFoo> naggy_foo; |
| 305 | |
| 306 | EXPECT_CALL(naggy_foo, DoThis()).Times(0); |
| 307 | EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(), |
| 308 | "called more times than expected"); |
| 309 | } |
| 310 | |
| 311 | // Tests that NaggyMock works with a mock class that has a non-default |
| 312 | // constructor. |
| 313 | TEST(NaggyMockTest, NonDefaultConstructor) { |
| 314 | NaggyMock<MockBar> naggy_bar("hi"); |
| 315 | EXPECT_EQ("hi", naggy_bar.str()); |
| 316 | |
| 317 | naggy_bar.This(); |
| 318 | naggy_bar.That(5, true); |
| 319 | } |
| 320 | |
| 321 | // Tests that NaggyMock works with a mock class that has a 10-ary |
| 322 | // non-default constructor. |
| 323 | TEST(NaggyMockTest, NonDefaultConstructor10) { |
| 324 | NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5', |
| 325 | "6", "7", true, false); |
| 326 | EXPECT_EQ("01234567TF", naggy_bar.str()); |
| 327 | |
| 328 | naggy_bar.This(); |
| 329 | naggy_bar.That(5, true); |
| 330 | } |
| 331 | |
| 332 | #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
| 333 | // Tests that NaggyMock<Mock> compiles where Mock is a user-defined |
| 334 | // class (as opposed to ::testing::Mock). We had to work around an |
| 335 | // MSVC 8.0 bug that caused the symbol Mock used in the definition of |
| 336 | // NaggyMock to be looked up in the wrong context, and this test |
| 337 | // ensures that our fix works. |
| 338 | // |
| 339 | // We have to skip this test on Symbian and Windows Mobile, as it |
| 340 | // causes the program to crash there, for reasons unclear to us yet. |
| 341 | TEST(NaggyMockTest, AcceptsClassNamedMock) { |
| 342 | NaggyMock< ::Mock> naggy; |
| 343 | EXPECT_CALL(naggy, DoThis()); |
| 344 | naggy.DoThis(); |
| 345 | } |
| 346 | #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
| 347 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 348 | // Tests that a strict mock allows expected calls. |
| 349 | TEST(StrictMockTest, AllowsExpectedCall) { |
| 350 | StrictMock<MockFoo> strict_foo; |
| 351 | |
| 352 | EXPECT_CALL(strict_foo, DoThis()); |
| 353 | strict_foo.DoThis(); |
| 354 | } |
| 355 | |
| 356 | // Tests that an unexpected call on a strict mock fails. |
| 357 | TEST(StrictMockTest, UnexpectedCallFails) { |
| 358 | StrictMock<MockFoo> strict_foo; |
| 359 | |
| 360 | EXPECT_CALL(strict_foo, DoThis()).Times(0); |
| 361 | EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(), |
| 362 | "called more times than expected"); |
| 363 | } |
| 364 | |
| 365 | // Tests that an uninteresting call on a strict mock fails. |
| 366 | TEST(StrictMockTest, UninterestingCallFails) { |
| 367 | StrictMock<MockFoo> strict_foo; |
| 368 | |
| 369 | EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(), |
| 370 | "Uninteresting mock function call"); |
| 371 | } |
| 372 | |
| 373 | // Tests that an uninteresting call on a strict mock fails, even if |
| 374 | // the call deletes the mock object. |
| 375 | TEST(StrictMockTest, UninterestingCallFailsAfterDeath) { |
| 376 | StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>; |
| 377 | |
| 378 | ON_CALL(*strict_foo, DoThis()) |
| 379 | .WillByDefault(Invoke(strict_foo, &MockFoo::Delete)); |
| 380 | |
| 381 | EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(), |
| 382 | "Uninteresting mock function call"); |
| 383 | } |
| 384 | |
| 385 | // Tests that StrictMock works with a mock class that has a |
| 386 | // non-default constructor. |
| 387 | TEST(StrictMockTest, NonDefaultConstructor) { |
| 388 | StrictMock<MockBar> strict_bar("hi"); |
| 389 | EXPECT_EQ("hi", strict_bar.str()); |
| 390 | |
| 391 | EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true), |
| 392 | "Uninteresting mock function call"); |
| 393 | } |
| 394 | |
| 395 | // Tests that StrictMock works with a mock class that has a 10-ary |
| 396 | // non-default constructor. |
| 397 | TEST(StrictMockTest, NonDefaultConstructor10) { |
| 398 | StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f', |
| 399 | "g", "h", true, false); |
| 400 | EXPECT_EQ("abcdefghTF", strict_bar.str()); |
| 401 | |
| 402 | EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true), |
| 403 | "Uninteresting mock function call"); |
| 404 | } |
| 405 | |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 406 | #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 407 | // Tests that StrictMock<Mock> compiles where Mock is a user-defined |
zhanyong.wan | 844fa94 | 2013-03-01 01:54:22 +0000 | [diff] [blame] | 408 | // class (as opposed to ::testing::Mock). We had to work around an |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 409 | // MSVC 8.0 bug that caused the symbol Mock used in the definition of |
| 410 | // StrictMock to be looked up in the wrong context, and this test |
| 411 | // ensures that our fix works. |
zhanyong.wan | 93244dc | 2009-09-17 19:11:00 +0000 | [diff] [blame] | 412 | // |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 413 | // We have to skip this test on Symbian and Windows Mobile, as it |
| 414 | // causes the program to crash there, for reasons unclear to us yet. |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 415 | TEST(StrictMockTest, AcceptsClassNamedMock) { |
zhanyong.wan | 93244dc | 2009-09-17 19:11:00 +0000 | [diff] [blame] | 416 | StrictMock< ::Mock> strict; |
| 417 | EXPECT_CALL(strict, DoThis()); |
| 418 | strict.DoThis(); |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 419 | } |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 420 | #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 421 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 422 | } // namespace gmock_nice_strict_test |
| 423 | } // namespace testing |