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