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