shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | // Copyright 2007, 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 | // Google Mock - a framework for writing C++ mock classes. |
| 33 | // |
| 34 | // This file tests the spec builder syntax. |
| 35 | |
| 36 | #include <gmock/gmock-spec-builders.h> |
| 37 | |
| 38 | #include <ostream> // NOLINT |
| 39 | #include <sstream> |
| 40 | #include <string> |
| 41 | |
| 42 | #include <gmock/gmock.h> |
| 43 | #include <gmock/internal/gmock-port.h> |
| 44 | #include <gtest/gtest.h> |
| 45 | #include <gtest/gtest-spi.h> |
| 46 | |
| 47 | namespace testing { |
| 48 | namespace internal { |
| 49 | |
| 50 | // Helper class for testing the Expectation class template. |
| 51 | class ExpectationTester { |
| 52 | public: |
| 53 | // Sets the call count of the given expectation to the given number. |
| 54 | void SetCallCount(int n, ExpectationBase* exp) { |
| 55 | exp->call_count_ = n; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | } // namespace internal |
| 60 | } // namespace testing |
| 61 | |
| 62 | namespace { |
| 63 | |
| 64 | using testing::_; |
| 65 | using testing::AnyNumber; |
| 66 | using testing::AtLeast; |
| 67 | using testing::AtMost; |
| 68 | using testing::Between; |
| 69 | using testing::Cardinality; |
| 70 | using testing::CardinalityInterface; |
| 71 | using testing::Const; |
| 72 | using testing::DoAll; |
| 73 | using testing::DoDefault; |
| 74 | using testing::GMOCK_FLAG(verbose); |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 75 | using testing::Gt; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 76 | using testing::InSequence; |
| 77 | using testing::Invoke; |
| 78 | using testing::InvokeWithoutArgs; |
| 79 | using testing::IsSubstring; |
| 80 | using testing::Lt; |
| 81 | using testing::Message; |
| 82 | using testing::Mock; |
| 83 | using testing::Return; |
| 84 | using testing::Sequence; |
| 85 | using testing::internal::g_gmock_mutex; |
| 86 | using testing::internal::kErrorVerbosity; |
| 87 | using testing::internal::kInfoVerbosity; |
| 88 | using testing::internal::kWarningVerbosity; |
| 89 | using testing::internal::Expectation; |
| 90 | using testing::internal::ExpectationTester; |
| 91 | using testing::internal::string; |
| 92 | |
| 93 | class Result {}; |
| 94 | |
| 95 | class MockA { |
| 96 | public: |
| 97 | MOCK_METHOD1(DoA, void(int n)); // NOLINT |
| 98 | MOCK_METHOD1(ReturnResult, Result(int n)); // NOLINT |
| 99 | MOCK_METHOD2(Binary, bool(int x, int y)); // NOLINT |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 100 | MOCK_METHOD2(ReturnInt, int(int x, int y)); // NOLINT |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | class MockB { |
| 104 | public: |
| 105 | MOCK_CONST_METHOD0(DoB, int()); // NOLINT |
| 106 | MOCK_METHOD1(DoB, int(int n)); // NOLINT |
| 107 | }; |
| 108 | |
| 109 | // Tests that EXPECT_CALL and ON_CALL compile in a presence of macro |
| 110 | // redefining a mock method name. This could happen, for example, when |
| 111 | // the tested code #includes Win32 API headers which define many APIs |
| 112 | // as macros, e.g. #define TextOut TextOutW. |
| 113 | |
| 114 | #define Method MethodW |
| 115 | |
| 116 | class CC { |
| 117 | public: |
| 118 | virtual ~CC() {} |
| 119 | virtual int Method() = 0; |
| 120 | }; |
| 121 | class MockCC : public CC { |
| 122 | public: |
| 123 | MOCK_METHOD0(Method, int()); |
| 124 | }; |
| 125 | |
| 126 | // Tests that a method with expanded name compiles. |
| 127 | TEST(OnCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) { |
| 128 | MockCC cc; |
| 129 | ON_CALL(cc, Method()); |
| 130 | } |
| 131 | |
| 132 | // Tests that the method with expanded name not only compiles but runs |
| 133 | // and returns a correct value, too. |
| 134 | TEST(OnCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) { |
| 135 | MockCC cc; |
| 136 | ON_CALL(cc, Method()).WillByDefault(Return(42)); |
| 137 | EXPECT_EQ(42, cc.Method()); |
| 138 | } |
| 139 | |
| 140 | // Tests that a method with expanded name compiles. |
| 141 | TEST(ExpectCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) { |
| 142 | MockCC cc; |
| 143 | EXPECT_CALL(cc, Method()); |
| 144 | cc.Method(); |
| 145 | } |
| 146 | |
| 147 | // Tests that it works, too. |
| 148 | TEST(ExpectCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) { |
| 149 | MockCC cc; |
| 150 | EXPECT_CALL(cc, Method()).WillOnce(Return(42)); |
| 151 | EXPECT_EQ(42, cc.Method()); |
| 152 | } |
| 153 | |
| 154 | #undef Method // Done with macro redefinition tests. |
| 155 | |
| 156 | // Tests that ON_CALL evaluates its arguments exactly once as promised |
| 157 | // by Google Mock. |
| 158 | TEST(OnCallSyntaxTest, EvaluatesFirstArgumentOnce) { |
| 159 | MockA a; |
| 160 | MockA* pa = &a; |
| 161 | |
| 162 | ON_CALL(*pa++, DoA(_)); |
| 163 | EXPECT_EQ(&a + 1, pa); |
| 164 | } |
| 165 | |
| 166 | TEST(OnCallSyntaxTest, EvaluatesSecondArgumentOnce) { |
| 167 | MockA a; |
| 168 | int n = 0; |
| 169 | |
| 170 | ON_CALL(a, DoA(n++)); |
| 171 | EXPECT_EQ(1, n); |
| 172 | } |
| 173 | |
| 174 | // Tests that the syntax of ON_CALL() is enforced at run time. |
| 175 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 176 | TEST(OnCallSyntaxTest, WithIsOptional) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 177 | MockA a; |
| 178 | |
| 179 | ON_CALL(a, DoA(5)) |
| 180 | .WillByDefault(Return()); |
| 181 | ON_CALL(a, DoA(_)) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 182 | .With(_) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 183 | .WillByDefault(Return()); |
| 184 | } |
| 185 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 186 | TEST(OnCallSyntaxTest, WithCanAppearAtMostOnce) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 187 | MockA a; |
| 188 | |
| 189 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 190 | ON_CALL(a, ReturnResult(_)) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 191 | .With(_) |
| 192 | .With(_) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 193 | .WillByDefault(Return(Result())); |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 194 | }, ".With() cannot appear more than once in an ON_CALL()"); |
| 195 | } |
| 196 | |
| 197 | TEST(OnCallSyntaxTest, WithArgumentsIsSynonymOfWith) { |
| 198 | MockA a; |
| 199 | ON_CALL(a, ReturnInt(_, _)) |
| 200 | .WithArguments(Lt()) |
| 201 | .WillByDefault(Return(1)); |
| 202 | ON_CALL(a, ReturnInt(_, _)) |
| 203 | .WithArguments(Gt()) |
| 204 | .WillByDefault(Return(2)); |
| 205 | EXPECT_CALL(a, ReturnInt(_, _)) |
| 206 | .Times(AnyNumber()); |
| 207 | |
| 208 | EXPECT_EQ(1, a.ReturnInt(1, 2)); |
| 209 | EXPECT_EQ(2, a.ReturnInt(2, 1)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 210 | } |
| 211 | |
zhanyong.wan | 652540a | 2009-02-23 23:37:29 +0000 | [diff] [blame] | 212 | #if GTEST_HAS_DEATH_TEST |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 213 | |
| 214 | TEST(OnCallSyntaxTest, WillByDefaultIsMandatory) { |
| 215 | MockA a; |
| 216 | |
| 217 | EXPECT_DEATH({ // NOLINT |
| 218 | ON_CALL(a, DoA(5)); |
| 219 | a.DoA(5); |
| 220 | }, ""); |
| 221 | } |
| 222 | |
| 223 | #endif // GTEST_HAS_DEATH_TEST |
| 224 | |
| 225 | TEST(OnCallSyntaxTest, WillByDefaultCanAppearAtMostOnce) { |
| 226 | MockA a; |
| 227 | |
| 228 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 229 | ON_CALL(a, DoA(5)) |
| 230 | .WillByDefault(Return()) |
| 231 | .WillByDefault(Return()); |
| 232 | }, ".WillByDefault() must appear exactly once in an ON_CALL()"); |
| 233 | } |
| 234 | |
| 235 | // Tests that EXPECT_CALL evaluates its arguments exactly once as |
| 236 | // promised by Google Mock. |
| 237 | TEST(ExpectCallSyntaxTest, EvaluatesFirstArgumentOnce) { |
| 238 | MockA a; |
| 239 | MockA* pa = &a; |
| 240 | |
| 241 | EXPECT_CALL(*pa++, DoA(_)); |
| 242 | a.DoA(0); |
| 243 | EXPECT_EQ(&a + 1, pa); |
| 244 | } |
| 245 | |
| 246 | TEST(ExpectCallSyntaxTest, EvaluatesSecondArgumentOnce) { |
| 247 | MockA a; |
| 248 | int n = 0; |
| 249 | |
| 250 | EXPECT_CALL(a, DoA(n++)); |
| 251 | a.DoA(0); |
| 252 | EXPECT_EQ(1, n); |
| 253 | } |
| 254 | |
| 255 | // Tests that the syntax of EXPECT_CALL() is enforced at run time. |
| 256 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 257 | TEST(ExpectCallSyntaxTest, WithIsOptional) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 258 | MockA a; |
| 259 | |
| 260 | EXPECT_CALL(a, DoA(5)) |
| 261 | .Times(0); |
| 262 | EXPECT_CALL(a, DoA(6)) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 263 | .With(_) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 264 | .Times(0); |
| 265 | } |
| 266 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 267 | TEST(ExpectCallSyntaxTest, WithCanAppearAtMostOnce) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 268 | MockA a; |
| 269 | |
| 270 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 271 | EXPECT_CALL(a, DoA(6)) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 272 | .With(_) |
| 273 | .With(_); |
| 274 | }, ".With() cannot appear more than once in an EXPECT_CALL()"); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 275 | |
| 276 | a.DoA(6); |
| 277 | } |
| 278 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 279 | TEST(ExpectCallSyntaxTest, WithMustBeFirstClause) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 280 | MockA a; |
| 281 | |
| 282 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 283 | EXPECT_CALL(a, DoA(1)) |
| 284 | .Times(1) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 285 | .With(_); |
| 286 | }, ".With() must be the first clause in an EXPECT_CALL()"); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 287 | |
| 288 | a.DoA(1); |
| 289 | |
| 290 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 291 | EXPECT_CALL(a, DoA(2)) |
| 292 | .WillOnce(Return()) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 293 | .With(_); |
| 294 | }, ".With() must be the first clause in an EXPECT_CALL()"); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 295 | |
| 296 | a.DoA(2); |
| 297 | } |
| 298 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame^] | 299 | TEST(ExpectCallSyntaxTest, WithArgumentsIsSynonymOfWith) { |
| 300 | MockA a; |
| 301 | EXPECT_CALL(a, ReturnInt(_, _)) |
| 302 | .WithArguments(Lt()) |
| 303 | .WillOnce(Return(1)); |
| 304 | EXPECT_CALL(a, ReturnInt(_, _)) |
| 305 | .WithArguments(Gt()) |
| 306 | .WillOnce(Return(2)); |
| 307 | |
| 308 | EXPECT_EQ(1, a.ReturnInt(1, 2)); |
| 309 | EXPECT_EQ(2, a.ReturnInt(2, 1)); |
| 310 | } |
| 311 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 312 | TEST(ExpectCallSyntaxTest, TimesCanBeInferred) { |
| 313 | MockA a; |
| 314 | |
| 315 | EXPECT_CALL(a, DoA(1)) |
| 316 | .WillOnce(Return()); |
| 317 | |
| 318 | EXPECT_CALL(a, DoA(2)) |
| 319 | .WillOnce(Return()) |
| 320 | .WillRepeatedly(Return()); |
| 321 | |
| 322 | a.DoA(1); |
| 323 | a.DoA(2); |
| 324 | a.DoA(2); |
| 325 | } |
| 326 | |
| 327 | TEST(ExpectCallSyntaxTest, TimesCanAppearAtMostOnce) { |
| 328 | MockA a; |
| 329 | |
| 330 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 331 | EXPECT_CALL(a, DoA(1)) |
| 332 | .Times(1) |
| 333 | .Times(2); |
| 334 | }, ".Times() cannot appear more than once in an EXPECT_CALL()"); |
| 335 | |
| 336 | a.DoA(1); |
| 337 | a.DoA(1); |
| 338 | } |
| 339 | |
| 340 | TEST(ExpectCallSyntaxTest, TimesMustBeBeforeInSequence) { |
| 341 | MockA a; |
| 342 | Sequence s; |
| 343 | |
| 344 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 345 | EXPECT_CALL(a, DoA(1)) |
| 346 | .InSequence(s) |
| 347 | .Times(1); |
| 348 | }, ".Times() cannot appear after "); |
| 349 | |
| 350 | a.DoA(1); |
| 351 | } |
| 352 | |
| 353 | TEST(ExpectCallSyntaxTest, InSequenceIsOptional) { |
| 354 | MockA a; |
| 355 | Sequence s; |
| 356 | |
| 357 | EXPECT_CALL(a, DoA(1)); |
| 358 | EXPECT_CALL(a, DoA(2)) |
| 359 | .InSequence(s); |
| 360 | |
| 361 | a.DoA(1); |
| 362 | a.DoA(2); |
| 363 | } |
| 364 | |
| 365 | TEST(ExpectCallSyntaxTest, InSequenceCanAppearMultipleTimes) { |
| 366 | MockA a; |
| 367 | Sequence s1, s2; |
| 368 | |
| 369 | EXPECT_CALL(a, DoA(1)) |
| 370 | .InSequence(s1, s2) |
| 371 | .InSequence(s1); |
| 372 | |
| 373 | a.DoA(1); |
| 374 | } |
| 375 | |
| 376 | TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWill) { |
| 377 | MockA a; |
| 378 | Sequence s; |
| 379 | |
| 380 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 381 | EXPECT_CALL(a, DoA(1)) |
| 382 | .WillOnce(Return()) |
| 383 | .InSequence(s); |
| 384 | }, ".InSequence() cannot appear after "); |
| 385 | |
| 386 | a.DoA(1); |
| 387 | } |
| 388 | |
| 389 | TEST(ExpectCallSyntaxTest, WillIsOptional) { |
| 390 | MockA a; |
| 391 | |
| 392 | EXPECT_CALL(a, DoA(1)); |
| 393 | EXPECT_CALL(a, DoA(2)) |
| 394 | .WillOnce(Return()); |
| 395 | |
| 396 | a.DoA(1); |
| 397 | a.DoA(2); |
| 398 | } |
| 399 | |
| 400 | TEST(ExpectCallSyntaxTest, WillCanAppearMultipleTimes) { |
| 401 | MockA a; |
| 402 | |
| 403 | EXPECT_CALL(a, DoA(1)) |
| 404 | .Times(AnyNumber()) |
| 405 | .WillOnce(Return()) |
| 406 | .WillOnce(Return()) |
| 407 | .WillOnce(Return()); |
| 408 | } |
| 409 | |
| 410 | TEST(ExpectCallSyntaxTest, WillMustBeBeforeWillRepeatedly) { |
| 411 | MockA a; |
| 412 | |
| 413 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 414 | EXPECT_CALL(a, DoA(1)) |
| 415 | .WillRepeatedly(Return()) |
| 416 | .WillOnce(Return()); |
| 417 | }, ".WillOnce() cannot appear after "); |
| 418 | |
| 419 | a.DoA(1); |
| 420 | } |
| 421 | |
| 422 | TEST(ExpectCallSyntaxTest, WillRepeatedlyIsOptional) { |
| 423 | MockA a; |
| 424 | |
| 425 | EXPECT_CALL(a, DoA(1)) |
| 426 | .WillOnce(Return()); |
| 427 | EXPECT_CALL(a, DoA(2)) |
| 428 | .WillOnce(Return()) |
| 429 | .WillRepeatedly(Return()); |
| 430 | |
| 431 | a.DoA(1); |
| 432 | a.DoA(2); |
| 433 | a.DoA(2); |
| 434 | } |
| 435 | |
| 436 | TEST(ExpectCallSyntaxTest, WillRepeatedlyCannotAppearMultipleTimes) { |
| 437 | MockA a; |
| 438 | |
| 439 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 440 | EXPECT_CALL(a, DoA(1)) |
| 441 | .WillRepeatedly(Return()) |
| 442 | .WillRepeatedly(Return()); |
| 443 | }, ".WillRepeatedly() cannot appear more than once in an " |
| 444 | "EXPECT_CALL()"); |
| 445 | } |
| 446 | |
| 447 | TEST(ExpectCallSyntaxTest, WillRepeatedlyMustBeBeforeRetiresOnSaturation) { |
| 448 | MockA a; |
| 449 | |
| 450 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 451 | EXPECT_CALL(a, DoA(1)) |
| 452 | .RetiresOnSaturation() |
| 453 | .WillRepeatedly(Return()); |
| 454 | }, ".WillRepeatedly() cannot appear after "); |
| 455 | } |
| 456 | |
| 457 | TEST(ExpectCallSyntaxTest, RetiresOnSaturationIsOptional) { |
| 458 | MockA a; |
| 459 | |
| 460 | EXPECT_CALL(a, DoA(1)); |
| 461 | EXPECT_CALL(a, DoA(1)) |
| 462 | .RetiresOnSaturation(); |
| 463 | |
| 464 | a.DoA(1); |
| 465 | a.DoA(1); |
| 466 | } |
| 467 | |
| 468 | TEST(ExpectCallSyntaxTest, RetiresOnSaturationCannotAppearMultipleTimes) { |
| 469 | MockA a; |
| 470 | |
| 471 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 472 | EXPECT_CALL(a, DoA(1)) |
| 473 | .RetiresOnSaturation() |
| 474 | .RetiresOnSaturation(); |
| 475 | }, ".RetiresOnSaturation() cannot appear more than once"); |
| 476 | |
| 477 | a.DoA(1); |
| 478 | } |
| 479 | |
| 480 | TEST(ExpectCallSyntaxTest, DefaultCardinalityIsOnce) { |
| 481 | { |
| 482 | MockA a; |
| 483 | EXPECT_CALL(a, DoA(1)); |
| 484 | a.DoA(1); |
| 485 | } |
| 486 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 487 | MockA a; |
| 488 | EXPECT_CALL(a, DoA(1)); |
| 489 | }, "to be called once"); |
| 490 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 491 | MockA a; |
| 492 | EXPECT_CALL(a, DoA(1)); |
| 493 | a.DoA(1); |
| 494 | a.DoA(1); |
| 495 | }, "to be called once"); |
| 496 | } |
| 497 | |
| 498 | // TODO(wan@google.com): find a way to re-enable these tests. |
| 499 | #if 0 |
| 500 | |
| 501 | // Tests that Google Mock doesn't print a warning when the number of |
| 502 | // WillOnce() is adequate. |
| 503 | TEST(ExpectCallSyntaxTest, DoesNotWarnOnAdequateActionCount) { |
| 504 | CaptureTestStdout(); |
| 505 | { |
| 506 | MockB b; |
| 507 | |
| 508 | // It's always fine to omit WillOnce() entirely. |
| 509 | EXPECT_CALL(b, DoB()) |
| 510 | .Times(0); |
| 511 | EXPECT_CALL(b, DoB(1)) |
| 512 | .Times(AtMost(1)); |
| 513 | EXPECT_CALL(b, DoB(2)) |
| 514 | .Times(1) |
| 515 | .WillRepeatedly(Return(1)); |
| 516 | |
| 517 | // It's fine for the number of WillOnce()s to equal the upper bound. |
| 518 | EXPECT_CALL(b, DoB(3)) |
| 519 | .Times(Between(1, 2)) |
| 520 | .WillOnce(Return(1)) |
| 521 | .WillOnce(Return(2)); |
| 522 | |
| 523 | // It's fine for the number of WillOnce()s to be smaller than the |
| 524 | // upper bound when there is a WillRepeatedly(). |
| 525 | EXPECT_CALL(b, DoB(4)) |
| 526 | .Times(AtMost(3)) |
| 527 | .WillOnce(Return(1)) |
| 528 | .WillRepeatedly(Return(2)); |
| 529 | |
| 530 | // Satisfies the above expectations. |
| 531 | b.DoB(2); |
| 532 | b.DoB(3); |
| 533 | } |
| 534 | const string& output = GetCapturedTestStdout(); |
| 535 | EXPECT_EQ("", output); |
| 536 | } |
| 537 | |
| 538 | // Tests that Google Mock warns on having too many actions in an |
| 539 | // expectation compared to its cardinality. |
| 540 | TEST(ExpectCallSyntaxTest, WarnsOnTooManyActions) { |
| 541 | CaptureTestStdout(); |
| 542 | { |
| 543 | MockB b; |
| 544 | |
| 545 | // Warns when the number of WillOnce()s is larger than the upper bound. |
| 546 | EXPECT_CALL(b, DoB()) |
| 547 | .Times(0) |
| 548 | .WillOnce(Return(1)); // #1 |
| 549 | EXPECT_CALL(b, DoB()) |
| 550 | .Times(AtMost(1)) |
| 551 | .WillOnce(Return(1)) |
| 552 | .WillOnce(Return(2)); // #2 |
| 553 | EXPECT_CALL(b, DoB(1)) |
| 554 | .Times(1) |
| 555 | .WillOnce(Return(1)) |
| 556 | .WillOnce(Return(2)) |
| 557 | .RetiresOnSaturation(); // #3 |
| 558 | |
| 559 | // Warns when the number of WillOnce()s equals the upper bound and |
| 560 | // there is a WillRepeatedly(). |
| 561 | EXPECT_CALL(b, DoB()) |
| 562 | .Times(0) |
| 563 | .WillRepeatedly(Return(1)); // #4 |
| 564 | EXPECT_CALL(b, DoB(2)) |
| 565 | .Times(1) |
| 566 | .WillOnce(Return(1)) |
| 567 | .WillRepeatedly(Return(2)); // #5 |
| 568 | |
| 569 | // Satisfies the above expectations. |
| 570 | b.DoB(1); |
| 571 | b.DoB(2); |
| 572 | } |
| 573 | const string& output = GetCapturedTestStdout(); |
| 574 | EXPECT_PRED_FORMAT2(IsSubstring, |
| 575 | "Too many actions specified.\n" |
| 576 | "Expected to be never called, but has 1 WillOnce().", |
| 577 | output); // #1 |
| 578 | EXPECT_PRED_FORMAT2(IsSubstring, |
| 579 | "Too many actions specified.\n" |
| 580 | "Expected to be called at most once, " |
| 581 | "but has 2 WillOnce()s.", |
| 582 | output); // #2 |
| 583 | EXPECT_PRED_FORMAT2(IsSubstring, |
| 584 | "Too many actions specified.\n" |
| 585 | "Expected to be called once, but has 2 WillOnce()s.", |
| 586 | output); // #3 |
| 587 | EXPECT_PRED_FORMAT2(IsSubstring, |
| 588 | "Too many actions specified.\n" |
| 589 | "Expected to be never called, but has 0 WillOnce()s " |
| 590 | "and a WillRepeatedly().", |
| 591 | output); // #4 |
| 592 | EXPECT_PRED_FORMAT2(IsSubstring, |
| 593 | "Too many actions specified.\n" |
| 594 | "Expected to be called once, but has 1 WillOnce() " |
| 595 | "and a WillRepeatedly().", |
| 596 | output); // #5 |
| 597 | } |
| 598 | |
| 599 | // Tests that Google Mock warns on having too few actions in an |
| 600 | // expectation compared to its cardinality. |
| 601 | TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) { |
| 602 | MockB b; |
| 603 | |
| 604 | EXPECT_CALL(b, DoB()) |
| 605 | .Times(Between(2, 3)) |
| 606 | .WillOnce(Return(1)); |
| 607 | |
| 608 | CaptureTestStdout(); |
| 609 | b.DoB(); |
| 610 | const string& output = GetCapturedTestStdout(); |
| 611 | EXPECT_PRED_FORMAT2(IsSubstring, |
| 612 | "Too few actions specified.\n" |
| 613 | "Expected to be called between 2 and 3 times, " |
| 614 | "but has only 1 WillOnce().", |
| 615 | output); |
| 616 | b.DoB(); |
| 617 | } |
| 618 | |
| 619 | #endif // 0 |
| 620 | |
| 621 | // Tests the semantics of ON_CALL(). |
| 622 | |
| 623 | // Tests that the built-in default action is taken when no ON_CALL() |
| 624 | // is specified. |
| 625 | TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCall) { |
| 626 | MockB b; |
| 627 | EXPECT_CALL(b, DoB()); |
| 628 | |
| 629 | EXPECT_EQ(0, b.DoB()); |
| 630 | } |
| 631 | |
| 632 | // Tests that the built-in default action is taken when no ON_CALL() |
| 633 | // matches the invocation. |
| 634 | TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCallMatches) { |
| 635 | MockB b; |
| 636 | ON_CALL(b, DoB(1)) |
| 637 | .WillByDefault(Return(1)); |
| 638 | EXPECT_CALL(b, DoB(_)); |
| 639 | |
| 640 | EXPECT_EQ(0, b.DoB(2)); |
| 641 | } |
| 642 | |
| 643 | // Tests that the last matching ON_CALL() action is taken. |
| 644 | TEST(OnCallTest, PicksLastMatchingOnCall) { |
| 645 | MockB b; |
| 646 | ON_CALL(b, DoB(_)) |
| 647 | .WillByDefault(Return(3)); |
| 648 | ON_CALL(b, DoB(2)) |
| 649 | .WillByDefault(Return(2)); |
| 650 | ON_CALL(b, DoB(1)) |
| 651 | .WillByDefault(Return(1)); |
| 652 | EXPECT_CALL(b, DoB(_)); |
| 653 | |
| 654 | EXPECT_EQ(2, b.DoB(2)); |
| 655 | } |
| 656 | |
| 657 | // Tests the semantics of EXPECT_CALL(). |
| 658 | |
| 659 | // Tests that any call is allowed when no EXPECT_CALL() is specified. |
| 660 | TEST(ExpectCallTest, AllowsAnyCallWhenNoSpec) { |
| 661 | MockB b; |
| 662 | EXPECT_CALL(b, DoB()); |
| 663 | // There is no expectation on DoB(int). |
| 664 | |
| 665 | b.DoB(); |
| 666 | |
| 667 | // DoB(int) can be called any number of times. |
| 668 | b.DoB(1); |
| 669 | b.DoB(2); |
| 670 | } |
| 671 | |
| 672 | // Tests that the last matching EXPECT_CALL() fires. |
| 673 | TEST(ExpectCallTest, PicksLastMatchingExpectCall) { |
| 674 | MockB b; |
| 675 | EXPECT_CALL(b, DoB(_)) |
| 676 | .WillRepeatedly(Return(2)); |
| 677 | EXPECT_CALL(b, DoB(1)) |
| 678 | .WillRepeatedly(Return(1)); |
| 679 | |
| 680 | EXPECT_EQ(1, b.DoB(1)); |
| 681 | } |
| 682 | |
| 683 | // Tests lower-bound violation. |
| 684 | TEST(ExpectCallTest, CatchesTooFewCalls) { |
| 685 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 686 | MockB b; |
| 687 | EXPECT_CALL(b, DoB(5)) |
| 688 | .Times(AtLeast(2)); |
| 689 | |
| 690 | b.DoB(5); |
| 691 | }, "Actual function call count doesn't match this expectation.\n" |
| 692 | " Expected: to be called at least twice\n" |
| 693 | " Actual: called once - unsatisfied and active"); |
| 694 | } |
| 695 | |
| 696 | // Tests that the cardinality can be inferred when no Times(...) is |
| 697 | // specified. |
| 698 | TEST(ExpectCallTest, InfersCardinalityWhenThereIsNoWillRepeatedly) { |
| 699 | { |
| 700 | MockB b; |
| 701 | EXPECT_CALL(b, DoB()) |
| 702 | .WillOnce(Return(1)) |
| 703 | .WillOnce(Return(2)); |
| 704 | |
| 705 | EXPECT_EQ(1, b.DoB()); |
| 706 | EXPECT_EQ(2, b.DoB()); |
| 707 | } |
| 708 | |
| 709 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 710 | MockB b; |
| 711 | EXPECT_CALL(b, DoB()) |
| 712 | .WillOnce(Return(1)) |
| 713 | .WillOnce(Return(2)); |
| 714 | |
| 715 | EXPECT_EQ(1, b.DoB()); |
| 716 | }, "to be called twice"); |
| 717 | |
| 718 | { // NOLINT |
| 719 | MockB b; |
| 720 | EXPECT_CALL(b, DoB()) |
| 721 | .WillOnce(Return(1)) |
| 722 | .WillOnce(Return(2)); |
| 723 | |
| 724 | EXPECT_EQ(1, b.DoB()); |
| 725 | EXPECT_EQ(2, b.DoB()); |
| 726 | EXPECT_NONFATAL_FAILURE(b.DoB(), "to be called twice"); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | TEST(ExpectCallTest, InfersCardinality1WhenThereIsWillRepeatedly) { |
| 731 | { |
| 732 | MockB b; |
| 733 | EXPECT_CALL(b, DoB()) |
| 734 | .WillOnce(Return(1)) |
| 735 | .WillRepeatedly(Return(2)); |
| 736 | |
| 737 | EXPECT_EQ(1, b.DoB()); |
| 738 | } |
| 739 | |
| 740 | { // NOLINT |
| 741 | MockB b; |
| 742 | EXPECT_CALL(b, DoB()) |
| 743 | .WillOnce(Return(1)) |
| 744 | .WillRepeatedly(Return(2)); |
| 745 | |
| 746 | EXPECT_EQ(1, b.DoB()); |
| 747 | EXPECT_EQ(2, b.DoB()); |
| 748 | EXPECT_EQ(2, b.DoB()); |
| 749 | } |
| 750 | |
| 751 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 752 | MockB b; |
| 753 | EXPECT_CALL(b, DoB()) |
| 754 | .WillOnce(Return(1)) |
| 755 | .WillRepeatedly(Return(2)); |
| 756 | }, "to be called at least once"); |
| 757 | } |
| 758 | |
| 759 | // Tests that the n-th action is taken for the n-th matching |
| 760 | // invocation. |
| 761 | TEST(ExpectCallTest, NthMatchTakesNthAction) { |
| 762 | MockB b; |
| 763 | EXPECT_CALL(b, DoB()) |
| 764 | .WillOnce(Return(1)) |
| 765 | .WillOnce(Return(2)) |
| 766 | .WillOnce(Return(3)); |
| 767 | |
| 768 | EXPECT_EQ(1, b.DoB()); |
| 769 | EXPECT_EQ(2, b.DoB()); |
| 770 | EXPECT_EQ(3, b.DoB()); |
| 771 | } |
| 772 | |
| 773 | // TODO(wan@google.com): find a way to re-enable these tests. |
| 774 | #if 0 |
| 775 | |
| 776 | // Tests that the default action is taken when the WillOnce(...) list is |
| 777 | // exhausted and there is no WillRepeatedly(). |
| 778 | TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) { |
| 779 | MockB b; |
| 780 | EXPECT_CALL(b, DoB(_)) |
| 781 | .Times(1); |
| 782 | EXPECT_CALL(b, DoB()) |
| 783 | .Times(AnyNumber()) |
| 784 | .WillOnce(Return(1)) |
| 785 | .WillOnce(Return(2)); |
| 786 | |
| 787 | CaptureTestStdout(); |
| 788 | EXPECT_EQ(0, b.DoB(1)); // Shouldn't generate a warning as the |
| 789 | // expectation has no action clause at all. |
| 790 | EXPECT_EQ(1, b.DoB()); |
| 791 | EXPECT_EQ(2, b.DoB()); |
| 792 | const string& output1 = GetCapturedTestStdout(); |
| 793 | EXPECT_EQ("", output1); |
| 794 | |
| 795 | CaptureTestStdout(); |
| 796 | EXPECT_EQ(0, b.DoB()); |
| 797 | EXPECT_EQ(0, b.DoB()); |
| 798 | const string& output2 = GetCapturedTestStdout(); |
| 799 | EXPECT_PRED2(RE::PartialMatch, output2, |
| 800 | "Actions ran out\\.\n" |
| 801 | "Called 3 times, but only 2 WillOnce\\(\\)s are specified - " |
| 802 | "returning default value\\."); |
| 803 | EXPECT_PRED2(RE::PartialMatch, output2, |
| 804 | "Actions ran out\\.\n" |
| 805 | "Called 4 times, but only 2 WillOnce\\(\\)s are specified - " |
| 806 | "returning default value\\."); |
| 807 | } |
| 808 | |
| 809 | #endif // 0 |
| 810 | |
| 811 | // Tests that the WillRepeatedly() action is taken when the WillOnce(...) |
| 812 | // list is exhausted. |
| 813 | TEST(ExpectCallTest, TakesRepeatedActionWhenWillListIsExhausted) { |
| 814 | MockB b; |
| 815 | EXPECT_CALL(b, DoB()) |
| 816 | .WillOnce(Return(1)) |
| 817 | .WillRepeatedly(Return(2)); |
| 818 | |
| 819 | EXPECT_EQ(1, b.DoB()); |
| 820 | EXPECT_EQ(2, b.DoB()); |
| 821 | EXPECT_EQ(2, b.DoB()); |
| 822 | } |
| 823 | |
| 824 | // Tests that an uninteresting call performs the default action. |
| 825 | TEST(UninterestingCallTest, DoesDefaultAction) { |
| 826 | // When there is an ON_CALL() statement, the action specified by it |
| 827 | // should be taken. |
| 828 | MockA a; |
| 829 | ON_CALL(a, Binary(_, _)) |
| 830 | .WillByDefault(Return(true)); |
| 831 | EXPECT_TRUE(a.Binary(1, 2)); |
| 832 | |
| 833 | // When there is no ON_CALL(), the default value for the return type |
| 834 | // should be returned. |
| 835 | MockB b; |
| 836 | EXPECT_EQ(0, b.DoB()); |
| 837 | } |
| 838 | |
| 839 | // Tests that an unexpected call performs the default action. |
| 840 | TEST(UnexpectedCallTest, DoesDefaultAction) { |
| 841 | // When there is an ON_CALL() statement, the action specified by it |
| 842 | // should be taken. |
| 843 | MockA a; |
| 844 | ON_CALL(a, Binary(_, _)) |
| 845 | .WillByDefault(Return(true)); |
| 846 | EXPECT_CALL(a, Binary(0, 0)); |
| 847 | a.Binary(0, 0); |
| 848 | bool result = false; |
| 849 | EXPECT_NONFATAL_FAILURE(result = a.Binary(1, 2), |
| 850 | "Unexpected mock function call"); |
| 851 | EXPECT_TRUE(result); |
| 852 | |
| 853 | // When there is no ON_CALL(), the default value for the return type |
| 854 | // should be returned. |
| 855 | MockB b; |
| 856 | EXPECT_CALL(b, DoB(0)) |
| 857 | .Times(0); |
| 858 | int n = -1; |
| 859 | EXPECT_NONFATAL_FAILURE(n = b.DoB(1), |
| 860 | "Unexpected mock function call"); |
| 861 | EXPECT_EQ(0, n); |
| 862 | } |
| 863 | |
| 864 | // Tests that when an unexpected void function generates the right |
| 865 | // failure message. |
| 866 | TEST(UnexpectedCallTest, GeneratesFailureForVoidFunction) { |
| 867 | // First, tests the message when there is only one EXPECT_CALL(). |
| 868 | MockA a1; |
| 869 | EXPECT_CALL(a1, DoA(1)); |
| 870 | a1.DoA(1); |
| 871 | // Ideally we should match the failure message against a regex, but |
| 872 | // EXPECT_NONFATAL_FAILURE doesn't support that, so we test for |
| 873 | // multiple sub-strings instead. |
| 874 | EXPECT_NONFATAL_FAILURE( |
| 875 | a1.DoA(9), |
| 876 | "Unexpected mock function call - returning directly.\n" |
| 877 | " Function call: DoA(9)\n" |
| 878 | "Google Mock tried the following 1 expectation, but it didn't match:"); |
| 879 | EXPECT_NONFATAL_FAILURE( |
| 880 | a1.DoA(9), |
| 881 | " Expected arg #0: is equal to 1\n" |
| 882 | " Actual: 9\n" |
| 883 | " Expected: to be called once\n" |
| 884 | " Actual: called once - saturated and active"); |
| 885 | |
| 886 | // Next, tests the message when there are more than one EXPECT_CALL(). |
| 887 | MockA a2; |
| 888 | EXPECT_CALL(a2, DoA(1)); |
| 889 | EXPECT_CALL(a2, DoA(3)); |
| 890 | a2.DoA(1); |
| 891 | EXPECT_NONFATAL_FAILURE( |
| 892 | a2.DoA(2), |
| 893 | "Unexpected mock function call - returning directly.\n" |
| 894 | " Function call: DoA(2)\n" |
| 895 | "Google Mock tried the following 2 expectations, but none matched:"); |
| 896 | EXPECT_NONFATAL_FAILURE( |
| 897 | a2.DoA(2), |
| 898 | "tried expectation #0\n" |
| 899 | " Expected arg #0: is equal to 1\n" |
| 900 | " Actual: 2\n" |
| 901 | " Expected: to be called once\n" |
| 902 | " Actual: called once - saturated and active"); |
| 903 | EXPECT_NONFATAL_FAILURE( |
| 904 | a2.DoA(2), |
| 905 | "tried expectation #1\n" |
| 906 | " Expected arg #0: is equal to 3\n" |
| 907 | " Actual: 2\n" |
| 908 | " Expected: to be called once\n" |
| 909 | " Actual: never called - unsatisfied and active"); |
| 910 | a2.DoA(3); |
| 911 | } |
| 912 | |
| 913 | // Tests that an unexpected non-void function generates the right |
| 914 | // failure message. |
| 915 | TEST(UnexpectedCallTest, GeneartesFailureForNonVoidFunction) { |
| 916 | MockB b1; |
| 917 | EXPECT_CALL(b1, DoB(1)); |
| 918 | b1.DoB(1); |
| 919 | EXPECT_NONFATAL_FAILURE( |
| 920 | b1.DoB(2), |
| 921 | "Unexpected mock function call - returning default value.\n" |
| 922 | " Function call: DoB(2)\n" |
| 923 | " Returns: 0\n" |
| 924 | "Google Mock tried the following 1 expectation, but it didn't match:"); |
| 925 | EXPECT_NONFATAL_FAILURE( |
| 926 | b1.DoB(2), |
| 927 | " Expected arg #0: is equal to 1\n" |
| 928 | " Actual: 2\n" |
| 929 | " Expected: to be called once\n" |
| 930 | " Actual: called once - saturated and active"); |
| 931 | } |
| 932 | |
| 933 | // Tests that Google Mock explains that an retired expectation doesn't |
| 934 | // match the call. |
| 935 | TEST(UnexpectedCallTest, RetiredExpectation) { |
| 936 | MockB b; |
| 937 | EXPECT_CALL(b, DoB(1)) |
| 938 | .RetiresOnSaturation(); |
| 939 | |
| 940 | b.DoB(1); |
| 941 | EXPECT_NONFATAL_FAILURE( |
| 942 | b.DoB(1), |
| 943 | " Expected: the expectation is active\n" |
| 944 | " Actual: it is retired"); |
| 945 | } |
| 946 | |
| 947 | // Tests that Google Mock explains that an expectation that doesn't |
| 948 | // match the arguments doesn't match the call. |
| 949 | TEST(UnexpectedCallTest, UnmatchedArguments) { |
| 950 | MockB b; |
| 951 | EXPECT_CALL(b, DoB(1)); |
| 952 | |
| 953 | EXPECT_NONFATAL_FAILURE( |
| 954 | b.DoB(2), |
| 955 | " Expected arg #0: is equal to 1\n" |
| 956 | " Actual: 2\n"); |
| 957 | b.DoB(1); |
| 958 | } |
| 959 | |
| 960 | #ifdef GMOCK_HAS_REGEX |
| 961 | |
| 962 | // Tests that Google Mock explains that an expectation with |
| 963 | // unsatisfied pre-requisites doesn't match the call. |
| 964 | TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) { |
| 965 | Sequence s1, s2; |
| 966 | MockB b; |
| 967 | EXPECT_CALL(b, DoB(1)) |
| 968 | .InSequence(s1); |
| 969 | EXPECT_CALL(b, DoB(2)) |
| 970 | .Times(AnyNumber()) |
| 971 | .InSequence(s1); |
| 972 | EXPECT_CALL(b, DoB(3)) |
| 973 | .InSequence(s2); |
| 974 | EXPECT_CALL(b, DoB(4)) |
| 975 | .InSequence(s1, s2); |
| 976 | |
| 977 | ::testing::TestPartResultArray failures; |
| 978 | { |
| 979 | ::testing::ScopedFakeTestPartResultReporter reporter(&failures); |
| 980 | b.DoB(4); |
| 981 | // Now 'failures' contains the Google Test failures generated by |
| 982 | // the above statement. |
| 983 | } |
| 984 | |
| 985 | // There should be one non-fatal failure. |
| 986 | ASSERT_EQ(1, failures.size()); |
| 987 | const ::testing::TestPartResult& r = failures.GetTestPartResult(0); |
| 988 | EXPECT_EQ(::testing::TPRT_NONFATAL_FAILURE, r.type()); |
| 989 | |
| 990 | // Verifies that the failure message contains the two unsatisfied |
| 991 | // pre-requisites but not the satisfied one. |
| 992 | const char* const pattern = |
| 993 | #if GMOCK_USES_PCRE |
| 994 | // PCRE has trouble using (.|\n) to match any character, but |
| 995 | // supports the (?s) prefix for using . to match any character. |
| 996 | "(?s)the following immediate pre-requisites are not satisfied:\n" |
| 997 | ".*: pre-requisite #0\n" |
| 998 | ".*: pre-requisite #1"; |
| 999 | #else |
| 1000 | // POSIX RE doesn't understand the (?s) prefix, but has no trouble |
| 1001 | // with (.|\n). |
| 1002 | "the following immediate pre-requisites are not satisfied:\n" |
| 1003 | "(.|\n)*: pre-requisite #0\n" |
| 1004 | "(.|\n)*: pre-requisite #1"; |
| 1005 | #endif // GMOCK_USES_PCRE |
| 1006 | |
| 1007 | EXPECT_TRUE( |
| 1008 | ::testing::internal::RE::PartialMatch(r.message(), pattern)) |
| 1009 | << " where the message is " << r.message(); |
| 1010 | b.DoB(1); |
| 1011 | b.DoB(3); |
| 1012 | b.DoB(4); |
| 1013 | } |
| 1014 | |
| 1015 | #endif // GMOCK_HAS_REGEX |
| 1016 | |
zhanyong.wan | 652540a | 2009-02-23 23:37:29 +0000 | [diff] [blame] | 1017 | #if GTEST_HAS_DEATH_TEST |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 1018 | |
| 1019 | TEST(UndefinedReturnValueTest, ReturnValueIsMandatory) { |
| 1020 | MockA a; |
| 1021 | // TODO(wan@google.com): We should really verify the output message, |
| 1022 | // but we cannot yet due to that EXPECT_DEATH only captures stderr |
| 1023 | // while Google Mock logs to stdout. |
| 1024 | EXPECT_DEATH(a.ReturnResult(1), ""); |
| 1025 | } |
| 1026 | |
| 1027 | #endif // GTEST_HAS_DEATH_TEST |
| 1028 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1029 | // Tests that an excessive call (one whose arguments match the |
| 1030 | // matchers but is called too many times) performs the default action. |
| 1031 | TEST(ExcessiveCallTest, DoesDefaultAction) { |
| 1032 | // When there is an ON_CALL() statement, the action specified by it |
| 1033 | // should be taken. |
| 1034 | MockA a; |
| 1035 | ON_CALL(a, Binary(_, _)) |
| 1036 | .WillByDefault(Return(true)); |
| 1037 | EXPECT_CALL(a, Binary(0, 0)); |
| 1038 | a.Binary(0, 0); |
| 1039 | bool result = false; |
| 1040 | EXPECT_NONFATAL_FAILURE(result = a.Binary(0, 0), |
| 1041 | "Mock function called more times than expected"); |
| 1042 | EXPECT_TRUE(result); |
| 1043 | |
| 1044 | // When there is no ON_CALL(), the default value for the return type |
| 1045 | // should be returned. |
| 1046 | MockB b; |
| 1047 | EXPECT_CALL(b, DoB(0)) |
| 1048 | .Times(0); |
| 1049 | int n = -1; |
| 1050 | EXPECT_NONFATAL_FAILURE(n = b.DoB(0), |
| 1051 | "Mock function called more times than expected"); |
| 1052 | EXPECT_EQ(0, n); |
| 1053 | } |
| 1054 | |
| 1055 | // Tests that when a void function is called too many times, |
| 1056 | // the failure message contains the argument values. |
| 1057 | TEST(ExcessiveCallTest, GeneratesFailureForVoidFunction) { |
| 1058 | MockA a; |
| 1059 | EXPECT_CALL(a, DoA(_)) |
| 1060 | .Times(0); |
| 1061 | EXPECT_NONFATAL_FAILURE( |
| 1062 | a.DoA(9), |
| 1063 | "Mock function called more times than expected - returning directly.\n" |
| 1064 | " Function call: DoA(9)\n" |
| 1065 | " Expected: to be never called\n" |
| 1066 | " Actual: called once - over-saturated and active"); |
| 1067 | } |
| 1068 | |
| 1069 | // Tests that when a non-void function is called too many times, the |
| 1070 | // failure message contains the argument values and the return value. |
| 1071 | TEST(ExcessiveCallTest, GeneratesFailureForNonVoidFunction) { |
| 1072 | MockB b; |
| 1073 | EXPECT_CALL(b, DoB(_)); |
| 1074 | b.DoB(1); |
| 1075 | EXPECT_NONFATAL_FAILURE( |
| 1076 | b.DoB(2), |
| 1077 | "Mock function called more times than expected - " |
| 1078 | "returning default value.\n" |
| 1079 | " Function call: DoB(2)\n" |
| 1080 | " Returns: 0\n" |
| 1081 | " Expected: to be called once\n" |
| 1082 | " Actual: called twice - over-saturated and active"); |
| 1083 | } |
| 1084 | |
| 1085 | // Tests using sequences. |
| 1086 | |
| 1087 | TEST(InSequenceTest, AllExpectationInScopeAreInSequence) { |
| 1088 | MockA a; |
| 1089 | { |
| 1090 | InSequence dummy; |
| 1091 | |
| 1092 | EXPECT_CALL(a, DoA(1)); |
| 1093 | EXPECT_CALL(a, DoA(2)); |
| 1094 | } |
| 1095 | |
| 1096 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 1097 | a.DoA(2); |
| 1098 | }, "Unexpected mock function call"); |
| 1099 | |
| 1100 | a.DoA(1); |
| 1101 | a.DoA(2); |
| 1102 | } |
| 1103 | |
| 1104 | TEST(InSequenceTest, NestedInSequence) { |
| 1105 | MockA a; |
| 1106 | { |
| 1107 | InSequence dummy; |
| 1108 | |
| 1109 | EXPECT_CALL(a, DoA(1)); |
| 1110 | { |
| 1111 | InSequence dummy2; |
| 1112 | |
| 1113 | EXPECT_CALL(a, DoA(2)); |
| 1114 | EXPECT_CALL(a, DoA(3)); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 1119 | a.DoA(1); |
| 1120 | a.DoA(3); |
| 1121 | }, "Unexpected mock function call"); |
| 1122 | |
| 1123 | a.DoA(2); |
| 1124 | a.DoA(3); |
| 1125 | } |
| 1126 | |
| 1127 | TEST(InSequenceTest, ExpectationsOutOfScopeAreNotAffected) { |
| 1128 | MockA a; |
| 1129 | { |
| 1130 | InSequence dummy; |
| 1131 | |
| 1132 | EXPECT_CALL(a, DoA(1)); |
| 1133 | EXPECT_CALL(a, DoA(2)); |
| 1134 | } |
| 1135 | EXPECT_CALL(a, DoA(3)); |
| 1136 | |
| 1137 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 1138 | a.DoA(2); |
| 1139 | }, "Unexpected mock function call"); |
| 1140 | |
| 1141 | a.DoA(3); |
| 1142 | a.DoA(1); |
| 1143 | a.DoA(2); |
| 1144 | } |
| 1145 | |
| 1146 | // Tests that any order is allowed when no sequence is used. |
| 1147 | TEST(SequenceTest, AnyOrderIsOkByDefault) { |
| 1148 | { |
| 1149 | MockA a; |
| 1150 | MockB b; |
| 1151 | |
| 1152 | EXPECT_CALL(a, DoA(1)); |
| 1153 | EXPECT_CALL(b, DoB()) |
| 1154 | .Times(AnyNumber()); |
| 1155 | |
| 1156 | a.DoA(1); |
| 1157 | b.DoB(); |
| 1158 | } |
| 1159 | |
| 1160 | { // NOLINT |
| 1161 | MockA a; |
| 1162 | MockB b; |
| 1163 | |
| 1164 | EXPECT_CALL(a, DoA(1)); |
| 1165 | EXPECT_CALL(b, DoB()) |
| 1166 | .Times(AnyNumber()); |
| 1167 | |
| 1168 | b.DoB(); |
| 1169 | a.DoA(1); |
| 1170 | } |
| 1171 | } |
| 1172 | |
zhanyong.wan | 652540a | 2009-02-23 23:37:29 +0000 | [diff] [blame] | 1173 | #if GTEST_HAS_DEATH_TEST |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1174 | |
| 1175 | // Tests that the calls must be in strict order when a complete order |
| 1176 | // is specified. |
| 1177 | TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo) { |
| 1178 | MockA a; |
| 1179 | Sequence s; |
| 1180 | |
| 1181 | EXPECT_CALL(a, ReturnResult(1)) |
| 1182 | .InSequence(s) |
| 1183 | .WillOnce(Return(Result())); |
| 1184 | |
| 1185 | EXPECT_CALL(a, ReturnResult(2)) |
| 1186 | .InSequence(s) |
| 1187 | .WillOnce(Return(Result())); |
| 1188 | |
| 1189 | EXPECT_CALL(a, ReturnResult(3)) |
| 1190 | .InSequence(s) |
| 1191 | .WillOnce(Return(Result())); |
| 1192 | |
| 1193 | EXPECT_DEATH({ // NOLINT |
| 1194 | a.ReturnResult(1); |
| 1195 | a.ReturnResult(3); |
| 1196 | a.ReturnResult(2); |
| 1197 | }, ""); |
| 1198 | |
| 1199 | EXPECT_DEATH({ // NOLINT |
| 1200 | a.ReturnResult(2); |
| 1201 | a.ReturnResult(1); |
| 1202 | a.ReturnResult(3); |
| 1203 | }, ""); |
| 1204 | |
| 1205 | a.ReturnResult(1); |
| 1206 | a.ReturnResult(2); |
| 1207 | a.ReturnResult(3); |
| 1208 | } |
| 1209 | |
| 1210 | // Tests specifying a DAG using multiple sequences. |
| 1211 | TEST(SequenceTest, CallsMustConformToSpecifiedDag) { |
| 1212 | MockA a; |
| 1213 | MockB b; |
| 1214 | Sequence x, y; |
| 1215 | |
| 1216 | EXPECT_CALL(a, ReturnResult(1)) |
| 1217 | .InSequence(x) |
| 1218 | .WillOnce(Return(Result())); |
| 1219 | |
| 1220 | EXPECT_CALL(b, DoB()) |
| 1221 | .Times(2) |
| 1222 | .InSequence(y); |
| 1223 | |
| 1224 | EXPECT_CALL(a, ReturnResult(2)) |
| 1225 | .InSequence(x, y) |
| 1226 | .WillRepeatedly(Return(Result())); |
| 1227 | |
| 1228 | EXPECT_CALL(a, ReturnResult(3)) |
| 1229 | .InSequence(x) |
| 1230 | .WillOnce(Return(Result())); |
| 1231 | |
| 1232 | EXPECT_DEATH({ // NOLINT |
| 1233 | a.ReturnResult(1); |
| 1234 | b.DoB(); |
| 1235 | a.ReturnResult(2); |
| 1236 | }, ""); |
| 1237 | |
| 1238 | EXPECT_DEATH({ // NOLINT |
| 1239 | a.ReturnResult(2); |
| 1240 | }, ""); |
| 1241 | |
| 1242 | EXPECT_DEATH({ // NOLINT |
| 1243 | a.ReturnResult(3); |
| 1244 | }, ""); |
| 1245 | |
| 1246 | EXPECT_DEATH({ // NOLINT |
| 1247 | a.ReturnResult(1); |
| 1248 | b.DoB(); |
| 1249 | b.DoB(); |
| 1250 | a.ReturnResult(3); |
| 1251 | a.ReturnResult(2); |
| 1252 | }, ""); |
| 1253 | |
| 1254 | b.DoB(); |
| 1255 | a.ReturnResult(1); |
| 1256 | b.DoB(); |
| 1257 | a.ReturnResult(3); |
| 1258 | } |
| 1259 | |
| 1260 | #endif // GTEST_HAS_DEATH_TEST |
| 1261 | |
| 1262 | TEST(SequenceTest, Retirement) { |
| 1263 | MockA a; |
| 1264 | Sequence s; |
| 1265 | |
| 1266 | EXPECT_CALL(a, DoA(1)) |
| 1267 | .InSequence(s); |
| 1268 | EXPECT_CALL(a, DoA(_)) |
| 1269 | .InSequence(s) |
| 1270 | .RetiresOnSaturation(); |
| 1271 | EXPECT_CALL(a, DoA(1)) |
| 1272 | .InSequence(s); |
| 1273 | |
| 1274 | a.DoA(1); |
| 1275 | a.DoA(2); |
| 1276 | a.DoA(1); |
| 1277 | } |
| 1278 | |
| 1279 | // Tests that Google Mock correctly handles calls to mock functions |
| 1280 | // after a mock object owning one of their pre-requisites has died. |
| 1281 | |
| 1282 | // Tests that calls that satisfy the original spec are successful. |
| 1283 | TEST(DeletingMockEarlyTest, Success1) { |
| 1284 | MockB* const b1 = new MockB; |
| 1285 | MockA* const a = new MockA; |
| 1286 | MockB* const b2 = new MockB; |
| 1287 | |
| 1288 | { |
| 1289 | InSequence dummy; |
| 1290 | EXPECT_CALL(*b1, DoB(_)) |
| 1291 | .WillOnce(Return(1)); |
| 1292 | EXPECT_CALL(*a, Binary(_, _)) |
| 1293 | .Times(AnyNumber()) |
| 1294 | .WillRepeatedly(Return(true)); |
| 1295 | EXPECT_CALL(*b2, DoB(_)) |
| 1296 | .Times(AnyNumber()) |
| 1297 | .WillRepeatedly(Return(2)); |
| 1298 | } |
| 1299 | |
| 1300 | EXPECT_EQ(1, b1->DoB(1)); |
| 1301 | delete b1; |
| 1302 | // a's pre-requisite has died. |
| 1303 | EXPECT_TRUE(a->Binary(0, 1)); |
| 1304 | delete b2; |
| 1305 | // a's successor has died. |
| 1306 | EXPECT_TRUE(a->Binary(1, 2)); |
| 1307 | delete a; |
| 1308 | } |
| 1309 | |
| 1310 | // Tests that calls that satisfy the original spec are successful. |
| 1311 | TEST(DeletingMockEarlyTest, Success2) { |
| 1312 | MockB* const b1 = new MockB; |
| 1313 | MockA* const a = new MockA; |
| 1314 | MockB* const b2 = new MockB; |
| 1315 | |
| 1316 | { |
| 1317 | InSequence dummy; |
| 1318 | EXPECT_CALL(*b1, DoB(_)) |
| 1319 | .WillOnce(Return(1)); |
| 1320 | EXPECT_CALL(*a, Binary(_, _)) |
| 1321 | .Times(AnyNumber()); |
| 1322 | EXPECT_CALL(*b2, DoB(_)) |
| 1323 | .Times(AnyNumber()) |
| 1324 | .WillRepeatedly(Return(2)); |
| 1325 | } |
| 1326 | |
| 1327 | delete a; // a is trivially satisfied. |
| 1328 | EXPECT_EQ(1, b1->DoB(1)); |
| 1329 | EXPECT_EQ(2, b2->DoB(2)); |
| 1330 | delete b1; |
| 1331 | delete b2; |
| 1332 | } |
| 1333 | |
zhanyong.wan | 6f14769 | 2009-03-03 06:44:08 +0000 | [diff] [blame] | 1334 | // Tests that it's OK to delete a mock object itself in its action. |
| 1335 | |
| 1336 | ACTION_P(Delete, ptr) { delete ptr; } |
| 1337 | |
| 1338 | TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningVoid) { |
| 1339 | MockA* const a = new MockA; |
| 1340 | EXPECT_CALL(*a, DoA(_)).WillOnce(Delete(a)); |
| 1341 | a->DoA(42); // This will cause a to be deleted. |
| 1342 | } |
| 1343 | |
| 1344 | TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningValue) { |
| 1345 | MockA* const a = new MockA; |
| 1346 | EXPECT_CALL(*a, ReturnResult(_)) |
| 1347 | .WillOnce(DoAll(Delete(a), Return(Result()))); |
| 1348 | a->ReturnResult(42); // This will cause a to be deleted. |
| 1349 | } |
| 1350 | |
| 1351 | // Tests that calls that violate the original spec yield failures. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1352 | TEST(DeletingMockEarlyTest, Failure1) { |
| 1353 | MockB* const b1 = new MockB; |
| 1354 | MockA* const a = new MockA; |
| 1355 | MockB* const b2 = new MockB; |
| 1356 | |
| 1357 | { |
| 1358 | InSequence dummy; |
| 1359 | EXPECT_CALL(*b1, DoB(_)) |
| 1360 | .WillOnce(Return(1)); |
| 1361 | EXPECT_CALL(*a, Binary(_, _)) |
| 1362 | .Times(AnyNumber()); |
| 1363 | EXPECT_CALL(*b2, DoB(_)) |
| 1364 | .Times(AnyNumber()) |
| 1365 | .WillRepeatedly(Return(2)); |
| 1366 | } |
| 1367 | |
| 1368 | delete a; // a is trivially satisfied. |
| 1369 | EXPECT_NONFATAL_FAILURE({ |
| 1370 | b2->DoB(2); |
| 1371 | }, "Unexpected mock function call"); |
| 1372 | EXPECT_EQ(1, b1->DoB(1)); |
| 1373 | delete b1; |
| 1374 | delete b2; |
| 1375 | } |
| 1376 | |
zhanyong.wan | 6f14769 | 2009-03-03 06:44:08 +0000 | [diff] [blame] | 1377 | // Tests that calls that violate the original spec yield failures. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1378 | TEST(DeletingMockEarlyTest, Failure2) { |
| 1379 | MockB* const b1 = new MockB; |
| 1380 | MockA* const a = new MockA; |
| 1381 | MockB* const b2 = new MockB; |
| 1382 | |
| 1383 | { |
| 1384 | InSequence dummy; |
| 1385 | EXPECT_CALL(*b1, DoB(_)); |
| 1386 | EXPECT_CALL(*a, Binary(_, _)) |
| 1387 | .Times(AnyNumber()); |
| 1388 | EXPECT_CALL(*b2, DoB(_)) |
| 1389 | .Times(AnyNumber()); |
| 1390 | } |
| 1391 | |
| 1392 | EXPECT_NONFATAL_FAILURE(delete b1, |
| 1393 | "Actual: never called"); |
| 1394 | EXPECT_NONFATAL_FAILURE(a->Binary(0, 1), |
| 1395 | "Unexpected mock function call"); |
| 1396 | EXPECT_NONFATAL_FAILURE(b2->DoB(1), |
| 1397 | "Unexpected mock function call"); |
| 1398 | delete a; |
| 1399 | delete b2; |
| 1400 | } |
| 1401 | |
| 1402 | class EvenNumberCardinality : public CardinalityInterface { |
| 1403 | public: |
| 1404 | // Returns true iff call_count calls will satisfy this cardinality. |
| 1405 | virtual bool IsSatisfiedByCallCount(int call_count) const { |
| 1406 | return call_count % 2 == 0; |
| 1407 | } |
| 1408 | |
| 1409 | // Returns true iff call_count calls will saturate this cardinality. |
| 1410 | virtual bool IsSaturatedByCallCount(int call_count) const { return false; } |
| 1411 | |
| 1412 | // Describes self to an ostream. |
| 1413 | virtual void DescribeTo(::std::ostream* os) const { |
| 1414 | *os << "called even number of times"; |
| 1415 | } |
| 1416 | }; |
| 1417 | |
| 1418 | Cardinality EvenNumber() { |
| 1419 | return Cardinality(new EvenNumberCardinality); |
| 1420 | } |
| 1421 | |
| 1422 | TEST(ExpectationBaseTest, |
| 1423 | AllPrerequisitesAreSatisfiedWorksForNonMonotonicCardinality) { |
| 1424 | MockA* a = new MockA; |
| 1425 | Sequence s; |
| 1426 | |
| 1427 | EXPECT_CALL(*a, DoA(1)) |
| 1428 | .Times(EvenNumber()) |
| 1429 | .InSequence(s); |
| 1430 | EXPECT_CALL(*a, DoA(2)) |
| 1431 | .Times(AnyNumber()) |
| 1432 | .InSequence(s); |
| 1433 | EXPECT_CALL(*a, DoA(3)) |
| 1434 | .Times(AnyNumber()); |
| 1435 | |
| 1436 | a->DoA(3); |
| 1437 | a->DoA(1); |
| 1438 | EXPECT_NONFATAL_FAILURE(a->DoA(2), "Unexpected mock function call"); |
| 1439 | EXPECT_NONFATAL_FAILURE(delete a, "to be called even number of times"); |
| 1440 | } |
| 1441 | |
| 1442 | // The following tests verify the message generated when a mock |
| 1443 | // function is called. |
| 1444 | |
| 1445 | struct Printable { |
| 1446 | }; |
| 1447 | |
| 1448 | inline void operator<<(::std::ostream& os, const Printable&) { |
| 1449 | os << "Printable"; |
| 1450 | } |
| 1451 | |
| 1452 | struct Unprintable { |
| 1453 | Unprintable() : value(0) {} |
| 1454 | int value; |
| 1455 | }; |
| 1456 | |
| 1457 | class MockC { |
| 1458 | public: |
| 1459 | MOCK_METHOD6(VoidMethod, void(bool cond, int n, string s, void* p, |
| 1460 | const Printable& x, Unprintable y)); |
| 1461 | MOCK_METHOD0(NonVoidMethod, int()); // NOLINT |
| 1462 | }; |
| 1463 | |
| 1464 | // TODO(wan@google.com): find a way to re-enable these tests. |
| 1465 | #if 0 |
| 1466 | |
| 1467 | // Tests that an uninteresting mock function call generates a warning |
| 1468 | // containing the stack trace. |
| 1469 | TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) { |
| 1470 | MockC c; |
| 1471 | CaptureTestStdout(); |
| 1472 | c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); |
| 1473 | const string& output = GetCapturedTestStdout(); |
| 1474 | EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output); |
| 1475 | EXPECT_PRED_FORMAT2(IsSubstring, "Stack trace:", output); |
| 1476 | #ifndef NDEBUG |
| 1477 | // We check the stack trace content in dbg-mode only, as opt-mode |
| 1478 | // may inline the call we are interested in seeing. |
| 1479 | |
| 1480 | // Verifies that a void mock function's name appears in the stack |
| 1481 | // trace. |
| 1482 | EXPECT_PRED_FORMAT2(IsSubstring, "::MockC::VoidMethod(", output); |
| 1483 | |
| 1484 | // Verifies that a non-void mock function's name appears in the |
| 1485 | // stack trace. |
| 1486 | CaptureTestStdout(); |
| 1487 | c.NonVoidMethod(); |
| 1488 | const string& output2 = GetCapturedTestStdout(); |
| 1489 | EXPECT_PRED_FORMAT2(IsSubstring, "::MockC::NonVoidMethod(", output2); |
| 1490 | #endif // NDEBUG |
| 1491 | } |
| 1492 | |
| 1493 | // Tests that an uninteresting mock function call causes the function |
| 1494 | // arguments and return value to be printed. |
| 1495 | TEST(FunctionCallMessageTest, UninterestingCallPrintsArgumentsAndReturnValue) { |
| 1496 | // A non-void mock function. |
| 1497 | MockB b; |
| 1498 | CaptureTestStdout(); |
| 1499 | b.DoB(); |
| 1500 | const string& output1 = GetCapturedTestStdout(); |
| 1501 | EXPECT_PRED_FORMAT2( |
| 1502 | IsSubstring, |
| 1503 | "Uninteresting mock function call - returning default value.\n" |
| 1504 | " Function call: DoB()\n" |
| 1505 | " Returns: 0\n", output1); |
| 1506 | // Makes sure the return value is printed. |
| 1507 | |
| 1508 | // A void mock function. |
| 1509 | MockC c; |
| 1510 | CaptureTestStdout(); |
| 1511 | c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); |
| 1512 | const string& output2 = GetCapturedTestStdout(); |
| 1513 | EXPECT_PRED2(RE::PartialMatch, output2, |
| 1514 | "Uninteresting mock function call - returning directly\\.\n" |
| 1515 | " Function call: VoidMethod" |
| 1516 | "\\(false, 5, \"Hi\", NULL, @0x\\w+ " |
| 1517 | "Printable, 4-byte object <0000 0000>\\)"); |
| 1518 | // A void function has no return value to print. |
| 1519 | } |
| 1520 | |
| 1521 | // Tests how the --gmock_verbose flag affects Google Mock's output. |
| 1522 | |
| 1523 | class GMockVerboseFlagTest : public testing::Test { |
| 1524 | public: |
| 1525 | // Verifies that the given Google Mock output is correct. (When |
| 1526 | // should_print is true, the output should match the given regex and |
| 1527 | // contain the given function name in the stack trace. When it's |
| 1528 | // false, the output should be empty.) |
| 1529 | void VerifyOutput(const string& output, bool should_print, |
| 1530 | const string& regex, |
| 1531 | const string& function_name) { |
| 1532 | if (should_print) { |
| 1533 | EXPECT_PRED2(RE::PartialMatch, output, regex); |
| 1534 | #ifndef NDEBUG |
| 1535 | // We check the stack trace content in dbg-mode only, as opt-mode |
| 1536 | // may inline the call we are interested in seeing. |
| 1537 | EXPECT_PRED_FORMAT2(IsSubstring, function_name, output); |
| 1538 | #endif // NDEBUG |
| 1539 | } else { |
| 1540 | EXPECT_EQ("", output); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | // Tests how the flag affects expected calls. |
| 1545 | void TestExpectedCall(bool should_print) { |
| 1546 | MockA a; |
| 1547 | EXPECT_CALL(a, DoA(5)); |
| 1548 | EXPECT_CALL(a, Binary(_, 1)) |
| 1549 | .WillOnce(Return(true)); |
| 1550 | |
| 1551 | // A void-returning function. |
| 1552 | CaptureTestStdout(); |
| 1553 | a.DoA(5); |
| 1554 | VerifyOutput( |
| 1555 | GetCapturedTestStdout(), |
| 1556 | should_print, |
| 1557 | "Expected mock function call\\.\n" |
| 1558 | " Function call: DoA\\(5\\)\n" |
| 1559 | "Stack trace:", |
| 1560 | "MockA::DoA"); |
| 1561 | |
| 1562 | // A non-void-returning function. |
| 1563 | CaptureTestStdout(); |
| 1564 | a.Binary(2, 1); |
| 1565 | VerifyOutput( |
| 1566 | GetCapturedTestStdout(), |
| 1567 | should_print, |
| 1568 | "Expected mock function call\\.\n" |
| 1569 | " Function call: Binary\\(2, 1\\)\n" |
| 1570 | " Returns: true\n" |
| 1571 | "Stack trace:", |
| 1572 | "MockA::Binary"); |
| 1573 | } |
| 1574 | |
| 1575 | // Tests how the flag affects uninteresting calls. |
| 1576 | void TestUninterestingCall(bool should_print) { |
| 1577 | MockA a; |
| 1578 | |
| 1579 | // A void-returning function. |
| 1580 | CaptureTestStdout(); |
| 1581 | a.DoA(5); |
| 1582 | VerifyOutput( |
| 1583 | GetCapturedTestStdout(), |
| 1584 | should_print, |
| 1585 | "\nGMOCK WARNING:\n" |
| 1586 | "Uninteresting mock function call - returning directly\\.\n" |
| 1587 | " Function call: DoA\\(5\\)\n" |
| 1588 | "Stack trace:\n" |
| 1589 | "[\\s\\S]*", |
| 1590 | "MockA::DoA"); |
| 1591 | |
| 1592 | // A non-void-returning function. |
| 1593 | CaptureTestStdout(); |
| 1594 | a.Binary(2, 1); |
| 1595 | VerifyOutput( |
| 1596 | GetCapturedTestStdout(), |
| 1597 | should_print, |
| 1598 | "\nGMOCK WARNING:\n" |
| 1599 | "Uninteresting mock function call - returning default value\\.\n" |
| 1600 | " Function call: Binary\\(2, 1\\)\n" |
| 1601 | " Returns: false\n" |
| 1602 | "Stack trace:\n" |
| 1603 | "[\\s\\S]*", |
| 1604 | "MockA::Binary"); |
| 1605 | } |
| 1606 | }; |
| 1607 | |
| 1608 | // Tests that --gmock_verbose=info causes both expected and |
| 1609 | // uninteresting calls to be reported. |
| 1610 | TEST_F(GMockVerboseFlagTest, Info) { |
| 1611 | GMOCK_FLAG(verbose) = kInfoVerbosity; |
| 1612 | TestExpectedCall(true); |
| 1613 | TestUninterestingCall(true); |
| 1614 | } |
| 1615 | |
| 1616 | // Tests that --gmock_verbose=warning causes uninteresting calls to be |
| 1617 | // reported. |
| 1618 | TEST_F(GMockVerboseFlagTest, Warning) { |
| 1619 | GMOCK_FLAG(verbose) = kWarningVerbosity; |
| 1620 | TestExpectedCall(false); |
| 1621 | TestUninterestingCall(true); |
| 1622 | } |
| 1623 | |
| 1624 | // Tests that --gmock_verbose=warning causes neither expected nor |
| 1625 | // uninteresting calls to be reported. |
| 1626 | TEST_F(GMockVerboseFlagTest, Error) { |
| 1627 | GMOCK_FLAG(verbose) = kErrorVerbosity; |
| 1628 | TestExpectedCall(false); |
| 1629 | TestUninterestingCall(false); |
| 1630 | } |
| 1631 | |
| 1632 | // Tests that --gmock_verbose=SOME_INVALID_VALUE has the same effect |
| 1633 | // as --gmock_verbose=warning. |
| 1634 | TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) { |
| 1635 | GMOCK_FLAG(verbose) = "invalid"; // Treated as "warning". |
| 1636 | TestExpectedCall(false); |
| 1637 | TestUninterestingCall(true); |
| 1638 | } |
| 1639 | |
| 1640 | #endif // 0 |
| 1641 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1642 | // A helper class that generates a failure when printed. We use it to |
| 1643 | // ensure that Google Mock doesn't print a value (even to an internal |
| 1644 | // buffer) when it is not supposed to do so. |
| 1645 | class PrintMeNot {}; |
| 1646 | |
| 1647 | void PrintTo(PrintMeNot /* dummy */, ::std::ostream* /* os */) { |
| 1648 | ADD_FAILURE() << "Google Mock is printing a value that shouldn't be " |
| 1649 | << "printed even to an internal buffer."; |
| 1650 | } |
| 1651 | |
| 1652 | class LogTestHelper { |
| 1653 | public: |
| 1654 | MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot)); |
| 1655 | }; |
| 1656 | |
| 1657 | class GMockLogTest : public ::testing::Test { |
| 1658 | protected: |
| 1659 | virtual void SetUp() { original_verbose_ = GMOCK_FLAG(verbose); } |
| 1660 | virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } |
| 1661 | |
| 1662 | LogTestHelper helper_; |
| 1663 | string original_verbose_; |
| 1664 | }; |
| 1665 | |
| 1666 | TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsWarning) { |
| 1667 | GMOCK_FLAG(verbose) = kWarningVerbosity; |
| 1668 | EXPECT_CALL(helper_, Foo(_)) |
| 1669 | .WillOnce(Return(PrintMeNot())); |
| 1670 | helper_.Foo(PrintMeNot()); // This is an expected call. |
| 1671 | } |
| 1672 | |
| 1673 | TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsError) { |
| 1674 | GMOCK_FLAG(verbose) = kErrorVerbosity; |
| 1675 | EXPECT_CALL(helper_, Foo(_)) |
| 1676 | .WillOnce(Return(PrintMeNot())); |
| 1677 | helper_.Foo(PrintMeNot()); // This is an expected call. |
| 1678 | } |
| 1679 | |
| 1680 | TEST_F(GMockLogTest, DoesNotPrintWarningInternallyIfVerbosityIsError) { |
| 1681 | GMOCK_FLAG(verbose) = kErrorVerbosity; |
| 1682 | ON_CALL(helper_, Foo(_)) |
| 1683 | .WillByDefault(Return(PrintMeNot())); |
| 1684 | helper_.Foo(PrintMeNot()); // This should generate a warning. |
| 1685 | } |
| 1686 | |
| 1687 | // Tests Mock::AllowLeak(). |
| 1688 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 1689 | TEST(AllowLeakTest, AllowsLeakingUnusedMockObject) { |
| 1690 | MockA* a = new MockA; |
| 1691 | Mock::AllowLeak(a); |
| 1692 | } |
| 1693 | |
| 1694 | TEST(AllowLeakTest, CanBeCalledBeforeOnCall) { |
| 1695 | MockA* a = new MockA; |
| 1696 | Mock::AllowLeak(a); |
| 1697 | ON_CALL(*a, DoA(_)).WillByDefault(Return()); |
| 1698 | a->DoA(0); |
| 1699 | } |
| 1700 | |
| 1701 | TEST(AllowLeakTest, CanBeCalledAfterOnCall) { |
| 1702 | MockA* a = new MockA; |
| 1703 | ON_CALL(*a, DoA(_)).WillByDefault(Return()); |
| 1704 | Mock::AllowLeak(a); |
| 1705 | } |
| 1706 | |
| 1707 | TEST(AllowLeakTest, CanBeCalledBeforeExpectCall) { |
| 1708 | MockA* a = new MockA; |
| 1709 | Mock::AllowLeak(a); |
| 1710 | EXPECT_CALL(*a, DoA(_)); |
| 1711 | a->DoA(0); |
| 1712 | } |
| 1713 | |
| 1714 | TEST(AllowLeakTest, CanBeCalledAfterExpectCall) { |
| 1715 | MockA* a = new MockA; |
| 1716 | EXPECT_CALL(*a, DoA(_)).Times(AnyNumber()); |
| 1717 | Mock::AllowLeak(a); |
| 1718 | } |
| 1719 | |
| 1720 | TEST(AllowLeakTest, WorksWhenBothOnCallAndExpectCallArePresent) { |
| 1721 | MockA* a = new MockA; |
| 1722 | ON_CALL(*a, DoA(_)).WillByDefault(Return()); |
| 1723 | EXPECT_CALL(*a, DoA(_)).Times(AnyNumber()); |
| 1724 | Mock::AllowLeak(a); |
| 1725 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1726 | |
| 1727 | // Tests that we can verify and clear a mock object's expectations |
| 1728 | // when none of its methods has expectations. |
| 1729 | TEST(VerifyAndClearExpectationsTest, NoMethodHasExpectations) { |
| 1730 | MockB b; |
| 1731 | ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b)); |
| 1732 | |
| 1733 | // There should be no expectations on the methods now, so we can |
| 1734 | // freely call them. |
| 1735 | EXPECT_EQ(0, b.DoB()); |
| 1736 | EXPECT_EQ(0, b.DoB(1)); |
| 1737 | } |
| 1738 | |
| 1739 | // Tests that we can verify and clear a mock object's expectations |
| 1740 | // when some, but not all, of its methods have expectations *and* the |
| 1741 | // verification succeeds. |
| 1742 | TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndSucceed) { |
| 1743 | MockB b; |
| 1744 | EXPECT_CALL(b, DoB()) |
| 1745 | .WillOnce(Return(1)); |
| 1746 | b.DoB(); |
| 1747 | ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b)); |
| 1748 | |
| 1749 | // There should be no expectations on the methods now, so we can |
| 1750 | // freely call them. |
| 1751 | EXPECT_EQ(0, b.DoB()); |
| 1752 | EXPECT_EQ(0, b.DoB(1)); |
| 1753 | } |
| 1754 | |
| 1755 | // Tests that we can verify and clear a mock object's expectations |
| 1756 | // when some, but not all, of its methods have expectations *and* the |
| 1757 | // verification fails. |
| 1758 | TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndFail) { |
| 1759 | MockB b; |
| 1760 | EXPECT_CALL(b, DoB()) |
| 1761 | .WillOnce(Return(1)); |
| 1762 | bool result; |
| 1763 | EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b), |
| 1764 | "Actual: never called"); |
| 1765 | ASSERT_FALSE(result); |
| 1766 | |
| 1767 | // There should be no expectations on the methods now, so we can |
| 1768 | // freely call them. |
| 1769 | EXPECT_EQ(0, b.DoB()); |
| 1770 | EXPECT_EQ(0, b.DoB(1)); |
| 1771 | } |
| 1772 | |
| 1773 | // Tests that we can verify and clear a mock object's expectations |
| 1774 | // when all of its methods have expectations. |
| 1775 | TEST(VerifyAndClearExpectationsTest, AllMethodsHaveExpectations) { |
| 1776 | MockB b; |
| 1777 | EXPECT_CALL(b, DoB()) |
| 1778 | .WillOnce(Return(1)); |
| 1779 | EXPECT_CALL(b, DoB(_)) |
| 1780 | .WillOnce(Return(2)); |
| 1781 | b.DoB(); |
| 1782 | b.DoB(1); |
| 1783 | ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b)); |
| 1784 | |
| 1785 | // There should be no expectations on the methods now, so we can |
| 1786 | // freely call them. |
| 1787 | EXPECT_EQ(0, b.DoB()); |
| 1788 | EXPECT_EQ(0, b.DoB(1)); |
| 1789 | } |
| 1790 | |
| 1791 | // Tests that we can verify and clear a mock object's expectations |
| 1792 | // when a method has more than one expectation. |
| 1793 | TEST(VerifyAndClearExpectationsTest, AMethodHasManyExpectations) { |
| 1794 | MockB b; |
| 1795 | EXPECT_CALL(b, DoB(0)) |
| 1796 | .WillOnce(Return(1)); |
| 1797 | EXPECT_CALL(b, DoB(_)) |
| 1798 | .WillOnce(Return(2)); |
| 1799 | b.DoB(1); |
| 1800 | bool result; |
| 1801 | EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b), |
| 1802 | "Actual: never called"); |
| 1803 | ASSERT_FALSE(result); |
| 1804 | |
| 1805 | // There should be no expectations on the methods now, so we can |
| 1806 | // freely call them. |
| 1807 | EXPECT_EQ(0, b.DoB()); |
| 1808 | EXPECT_EQ(0, b.DoB(1)); |
| 1809 | } |
| 1810 | |
| 1811 | // Tests that we can call VerifyAndClearExpectations() on the same |
| 1812 | // mock object multiple times. |
| 1813 | TEST(VerifyAndClearExpectationsTest, CanCallManyTimes) { |
| 1814 | MockB b; |
| 1815 | EXPECT_CALL(b, DoB()); |
| 1816 | b.DoB(); |
| 1817 | Mock::VerifyAndClearExpectations(&b); |
| 1818 | |
| 1819 | EXPECT_CALL(b, DoB(_)) |
| 1820 | .WillOnce(Return(1)); |
| 1821 | b.DoB(1); |
| 1822 | Mock::VerifyAndClearExpectations(&b); |
| 1823 | Mock::VerifyAndClearExpectations(&b); |
| 1824 | |
| 1825 | // There should be no expectations on the methods now, so we can |
| 1826 | // freely call them. |
| 1827 | EXPECT_EQ(0, b.DoB()); |
| 1828 | EXPECT_EQ(0, b.DoB(1)); |
| 1829 | } |
| 1830 | |
| 1831 | // Tests that we can clear a mock object's default actions when none |
| 1832 | // of its methods has default actions. |
| 1833 | TEST(VerifyAndClearTest, NoMethodHasDefaultActions) { |
| 1834 | MockB b; |
| 1835 | // If this crashes or generates a failure, the test will catch it. |
| 1836 | Mock::VerifyAndClear(&b); |
| 1837 | EXPECT_EQ(0, b.DoB()); |
| 1838 | } |
| 1839 | |
| 1840 | // Tests that we can clear a mock object's default actions when some, |
| 1841 | // but not all of its methods have default actions. |
| 1842 | TEST(VerifyAndClearTest, SomeMethodsHaveDefaultActions) { |
| 1843 | MockB b; |
| 1844 | ON_CALL(b, DoB()) |
| 1845 | .WillByDefault(Return(1)); |
| 1846 | |
| 1847 | Mock::VerifyAndClear(&b); |
| 1848 | |
| 1849 | // Verifies that the default action of int DoB() was removed. |
| 1850 | EXPECT_EQ(0, b.DoB()); |
| 1851 | } |
| 1852 | |
| 1853 | // Tests that we can clear a mock object's default actions when all of |
| 1854 | // its methods have default actions. |
| 1855 | TEST(VerifyAndClearTest, AllMethodsHaveDefaultActions) { |
| 1856 | MockB b; |
| 1857 | ON_CALL(b, DoB()) |
| 1858 | .WillByDefault(Return(1)); |
| 1859 | ON_CALL(b, DoB(_)) |
| 1860 | .WillByDefault(Return(2)); |
| 1861 | |
| 1862 | Mock::VerifyAndClear(&b); |
| 1863 | |
| 1864 | // Verifies that the default action of int DoB() was removed. |
| 1865 | EXPECT_EQ(0, b.DoB()); |
| 1866 | |
| 1867 | // Verifies that the default action of int DoB(int) was removed. |
| 1868 | EXPECT_EQ(0, b.DoB(0)); |
| 1869 | } |
| 1870 | |
| 1871 | // Tests that we can clear a mock object's default actions when a |
| 1872 | // method has more than one ON_CALL() set on it. |
| 1873 | TEST(VerifyAndClearTest, AMethodHasManyDefaultActions) { |
| 1874 | MockB b; |
| 1875 | ON_CALL(b, DoB(0)) |
| 1876 | .WillByDefault(Return(1)); |
| 1877 | ON_CALL(b, DoB(_)) |
| 1878 | .WillByDefault(Return(2)); |
| 1879 | |
| 1880 | Mock::VerifyAndClear(&b); |
| 1881 | |
| 1882 | // Verifies that the default actions (there are two) of int DoB(int) |
| 1883 | // were removed. |
| 1884 | EXPECT_EQ(0, b.DoB(0)); |
| 1885 | EXPECT_EQ(0, b.DoB(1)); |
| 1886 | } |
| 1887 | |
| 1888 | // Tests that we can call VerifyAndClear() on a mock object multiple |
| 1889 | // times. |
| 1890 | TEST(VerifyAndClearTest, CanCallManyTimes) { |
| 1891 | MockB b; |
| 1892 | ON_CALL(b, DoB()) |
| 1893 | .WillByDefault(Return(1)); |
| 1894 | Mock::VerifyAndClear(&b); |
| 1895 | Mock::VerifyAndClear(&b); |
| 1896 | |
| 1897 | ON_CALL(b, DoB(_)) |
| 1898 | .WillByDefault(Return(1)); |
| 1899 | Mock::VerifyAndClear(&b); |
| 1900 | |
| 1901 | EXPECT_EQ(0, b.DoB()); |
| 1902 | EXPECT_EQ(0, b.DoB(1)); |
| 1903 | } |
| 1904 | |
| 1905 | // Tests that VerifyAndClear() works when the verification succeeds. |
| 1906 | TEST(VerifyAndClearTest, Success) { |
| 1907 | MockB b; |
| 1908 | ON_CALL(b, DoB()) |
| 1909 | .WillByDefault(Return(1)); |
| 1910 | EXPECT_CALL(b, DoB(1)) |
| 1911 | .WillOnce(Return(2)); |
| 1912 | |
| 1913 | b.DoB(); |
| 1914 | b.DoB(1); |
| 1915 | ASSERT_TRUE(Mock::VerifyAndClear(&b)); |
| 1916 | |
| 1917 | // There should be no expectations on the methods now, so we can |
| 1918 | // freely call them. |
| 1919 | EXPECT_EQ(0, b.DoB()); |
| 1920 | EXPECT_EQ(0, b.DoB(1)); |
| 1921 | } |
| 1922 | |
| 1923 | // Tests that VerifyAndClear() works when the verification fails. |
| 1924 | TEST(VerifyAndClearTest, Failure) { |
| 1925 | MockB b; |
| 1926 | ON_CALL(b, DoB(_)) |
| 1927 | .WillByDefault(Return(1)); |
| 1928 | EXPECT_CALL(b, DoB()) |
| 1929 | .WillOnce(Return(2)); |
| 1930 | |
| 1931 | b.DoB(1); |
| 1932 | bool result; |
| 1933 | EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClear(&b), |
| 1934 | "Actual: never called"); |
| 1935 | ASSERT_FALSE(result); |
| 1936 | |
| 1937 | // There should be no expectations on the methods now, so we can |
| 1938 | // freely call them. |
| 1939 | EXPECT_EQ(0, b.DoB()); |
| 1940 | EXPECT_EQ(0, b.DoB(1)); |
| 1941 | } |
| 1942 | |
| 1943 | // Tests that VerifyAndClear() works when the default actions and |
| 1944 | // expectations are set on a const mock object. |
| 1945 | TEST(VerifyAndClearTest, Const) { |
| 1946 | MockB b; |
| 1947 | ON_CALL(Const(b), DoB()) |
| 1948 | .WillByDefault(Return(1)); |
| 1949 | |
| 1950 | EXPECT_CALL(Const(b), DoB()) |
| 1951 | .WillOnce(DoDefault()) |
| 1952 | .WillOnce(Return(2)); |
| 1953 | |
| 1954 | b.DoB(); |
| 1955 | b.DoB(); |
| 1956 | ASSERT_TRUE(Mock::VerifyAndClear(&b)); |
| 1957 | |
| 1958 | // There should be no expectations on the methods now, so we can |
| 1959 | // freely call them. |
| 1960 | EXPECT_EQ(0, b.DoB()); |
| 1961 | EXPECT_EQ(0, b.DoB(1)); |
| 1962 | } |
| 1963 | |
| 1964 | // Tests that we can set default actions and expectations on a mock |
| 1965 | // object after VerifyAndClear() has been called on it. |
| 1966 | TEST(VerifyAndClearTest, CanSetDefaultActionsAndExpectationsAfterwards) { |
| 1967 | MockB b; |
| 1968 | ON_CALL(b, DoB()) |
| 1969 | .WillByDefault(Return(1)); |
| 1970 | EXPECT_CALL(b, DoB(_)) |
| 1971 | .WillOnce(Return(2)); |
| 1972 | b.DoB(1); |
| 1973 | |
| 1974 | Mock::VerifyAndClear(&b); |
| 1975 | |
| 1976 | EXPECT_CALL(b, DoB()) |
| 1977 | .WillOnce(Return(3)); |
| 1978 | ON_CALL(b, DoB(_)) |
| 1979 | .WillByDefault(Return(4)); |
| 1980 | |
| 1981 | EXPECT_EQ(3, b.DoB()); |
| 1982 | EXPECT_EQ(4, b.DoB(1)); |
| 1983 | } |
| 1984 | |
| 1985 | // Tests that calling VerifyAndClear() on one mock object does not |
| 1986 | // affect other mock objects (either of the same type or not). |
| 1987 | TEST(VerifyAndClearTest, DoesNotAffectOtherMockObjects) { |
| 1988 | MockA a; |
| 1989 | MockB b1; |
| 1990 | MockB b2; |
| 1991 | |
| 1992 | ON_CALL(a, Binary(_, _)) |
| 1993 | .WillByDefault(Return(true)); |
| 1994 | EXPECT_CALL(a, Binary(_, _)) |
| 1995 | .WillOnce(DoDefault()) |
| 1996 | .WillOnce(Return(false)); |
| 1997 | |
| 1998 | ON_CALL(b1, DoB()) |
| 1999 | .WillByDefault(Return(1)); |
| 2000 | EXPECT_CALL(b1, DoB(_)) |
| 2001 | .WillOnce(Return(2)); |
| 2002 | |
| 2003 | ON_CALL(b2, DoB()) |
| 2004 | .WillByDefault(Return(3)); |
| 2005 | EXPECT_CALL(b2, DoB(_)); |
| 2006 | |
| 2007 | b2.DoB(0); |
| 2008 | Mock::VerifyAndClear(&b2); |
| 2009 | |
| 2010 | // Verifies that the default actions and expectations of a and b1 |
| 2011 | // are still in effect. |
| 2012 | EXPECT_TRUE(a.Binary(0, 0)); |
| 2013 | EXPECT_FALSE(a.Binary(0, 0)); |
| 2014 | |
| 2015 | EXPECT_EQ(1, b1.DoB()); |
| 2016 | EXPECT_EQ(2, b1.DoB(0)); |
| 2017 | } |
| 2018 | |
| 2019 | // Tests that a mock function's action can call a mock function |
| 2020 | // (either the same function or a different one) either as an explicit |
| 2021 | // action or as a default action without causing a dead lock. It |
| 2022 | // verifies that the action is not performed inside the critical |
| 2023 | // section. |
| 2024 | |
| 2025 | void Helper(MockC* c) { |
| 2026 | c->NonVoidMethod(); |
| 2027 | } |
| 2028 | |
| 2029 | } // namespace |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 2030 | |
| 2031 | int main(int argc, char **argv) { |
| 2032 | testing::InitGoogleMock(&argc, argv); |
| 2033 | |
| 2034 | // Ensures that the tests pass no matter what value of |
| 2035 | // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. |
| 2036 | testing::GMOCK_FLAG(catch_leaked_mocks) = true; |
| 2037 | testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity; |
| 2038 | |
| 2039 | return RUN_ALL_TESTS(); |
| 2040 | } |