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