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