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