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