blob: 8a8632dc10feaac99251f415499800f95c00cd76 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests the spec builder syntax.
35
zhanyong.wan53e08c42010-09-14 05:38:21 +000036#include "gmock/gmock-spec-builders.h"
shiqiane35fdd92008-12-10 05:08:54 +000037
38#include <ostream> // NOLINT
39#include <sstream>
40#include <string>
41
zhanyong.wan53e08c42010-09-14 05:38:21 +000042#include "gmock/gmock.h"
43#include "gmock/internal/gmock-port.h"
44#include "gtest/gtest.h"
45#include "gtest/gtest-spi.h"
vladloseve5121b52011-02-11 23:50:38 +000046#include "gtest/internal/gtest-port.h"
shiqiane35fdd92008-12-10 05:08:54 +000047
48namespace testing {
49namespace internal {
50
51// Helper class for testing the Expectation class template.
52class ExpectationTester {
53 public:
54 // Sets the call count of the given expectation to the given number.
55 void SetCallCount(int n, ExpectationBase* exp) {
56 exp->call_count_ = n;
57 }
58};
59
60} // namespace internal
61} // namespace testing
62
63namespace {
64
65using testing::_;
66using testing::AnyNumber;
67using testing::AtLeast;
68using testing::AtMost;
69using testing::Between;
70using testing::Cardinality;
71using testing::CardinalityInterface;
zhanyong.wand14aaed2010-01-14 05:36:32 +000072using testing::ContainsRegex;
shiqiane35fdd92008-12-10 05:08:54 +000073using testing::Const;
74using testing::DoAll;
75using testing::DoDefault;
zhanyong.wan41b9b0b2009-07-01 19:04:51 +000076using testing::Eq;
77using testing::Expectation;
78using testing::ExpectationSet;
shiqiane35fdd92008-12-10 05:08:54 +000079using testing::GMOCK_FLAG(verbose);
zhanyong.wanbf550852009-06-09 06:09:53 +000080using testing::Gt;
shiqiane35fdd92008-12-10 05:08:54 +000081using testing::InSequence;
82using testing::Invoke;
83using testing::InvokeWithoutArgs;
84using testing::IsSubstring;
85using testing::Lt;
86using testing::Message;
87using testing::Mock;
zhanyong.wan41b9b0b2009-07-01 19:04:51 +000088using testing::Ne;
shiqiane35fdd92008-12-10 05:08:54 +000089using testing::Return;
90using testing::Sequence;
vladlosev9bcb5f92011-10-24 23:41:07 +000091using testing::SetArgPointee;
zhanyong.wan470df422010-02-02 22:34:58 +000092using testing::internal::ExpectationTester;
vladloseve5121b52011-02-11 23:50:38 +000093using testing::internal::FormatFileLocation;
shiqiane35fdd92008-12-10 05:08:54 +000094using testing::internal::kErrorVerbosity;
95using testing::internal::kInfoVerbosity;
96using testing::internal::kWarningVerbosity;
vladlosev9bcb5f92011-10-24 23:41:07 +000097using testing::internal::linked_ptr;
shiqiane35fdd92008-12-10 05:08:54 +000098using testing::internal::string;
99
zhanyong.wan2516f602010-08-31 18:28:02 +0000100#if GTEST_HAS_STREAM_REDIRECTION
zhanyong.wan470df422010-02-02 22:34:58 +0000101using testing::HasSubstr;
102using testing::internal::CaptureStdout;
103using testing::internal::GetCapturedStdout;
zhanyong.wan2516f602010-08-31 18:28:02 +0000104#endif
zhanyong.wan470df422010-02-02 22:34:58 +0000105
zhanyong.waned6c9272011-02-23 19:39:27 +0000106class Incomplete;
107
108class MockIncomplete {
109 public:
110 // This line verifies that a mock method can take a by-reference
111 // argument of an incomplete type.
112 MOCK_METHOD1(ByRefFunc, void(const Incomplete& x));
113};
114
115// Tells Google Mock how to print a value of type Incomplete.
116void PrintTo(const Incomplete& x, ::std::ostream* os);
117
118TEST(MockMethodTest, CanInstantiateWithIncompleteArgType) {
119 // Even though this mock class contains a mock method that takes
120 // by-reference an argument whose type is incomplete, we can still
121 // use the mock, as long as Google Mock knows how to print the
122 // argument.
123 MockIncomplete incomplete;
124 EXPECT_CALL(incomplete, ByRefFunc(_))
125 .Times(AnyNumber());
126}
127
128// The definition of the printer for the argument type doesn't have to
129// be visible where the mock is used.
130void PrintTo(const Incomplete& /* x */, ::std::ostream* os) {
131 *os << "incomplete";
132}
133
shiqiane35fdd92008-12-10 05:08:54 +0000134class Result {};
135
136class MockA {
137 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000138 MockA() {}
139
shiqiane35fdd92008-12-10 05:08:54 +0000140 MOCK_METHOD1(DoA, void(int n)); // NOLINT
141 MOCK_METHOD1(ReturnResult, Result(int n)); // NOLINT
142 MOCK_METHOD2(Binary, bool(int x, int y)); // NOLINT
zhanyong.wanbf550852009-06-09 06:09:53 +0000143 MOCK_METHOD2(ReturnInt, int(int x, int y)); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000144
145 private:
146 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockA);
shiqiane35fdd92008-12-10 05:08:54 +0000147};
148
149class MockB {
150 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000151 MockB() {}
152
shiqiane35fdd92008-12-10 05:08:54 +0000153 MOCK_CONST_METHOD0(DoB, int()); // NOLINT
154 MOCK_METHOD1(DoB, int(int n)); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000155
156 private:
157 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB);
shiqiane35fdd92008-12-10 05:08:54 +0000158};
159
vladlosev9bcb5f92011-10-24 23:41:07 +0000160class ReferenceHoldingMock {
161 public:
162 ReferenceHoldingMock() {}
163
164 MOCK_METHOD1(AcceptReference, void(linked_ptr<MockA>*));
165
166 private:
167 GTEST_DISALLOW_COPY_AND_ASSIGN_(ReferenceHoldingMock);
168};
169
shiqiane35fdd92008-12-10 05:08:54 +0000170// Tests that EXPECT_CALL and ON_CALL compile in a presence of macro
171// redefining a mock method name. This could happen, for example, when
172// the tested code #includes Win32 API headers which define many APIs
173// as macros, e.g. #define TextOut TextOutW.
174
175#define Method MethodW
176
177class CC {
178 public:
179 virtual ~CC() {}
180 virtual int Method() = 0;
181};
182class MockCC : public CC {
183 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000184 MockCC() {}
185
shiqiane35fdd92008-12-10 05:08:54 +0000186 MOCK_METHOD0(Method, int());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000187
188 private:
189 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockCC);
shiqiane35fdd92008-12-10 05:08:54 +0000190};
191
192// Tests that a method with expanded name compiles.
193TEST(OnCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) {
194 MockCC cc;
195 ON_CALL(cc, Method());
196}
197
198// Tests that the method with expanded name not only compiles but runs
199// and returns a correct value, too.
200TEST(OnCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) {
201 MockCC cc;
202 ON_CALL(cc, Method()).WillByDefault(Return(42));
203 EXPECT_EQ(42, cc.Method());
204}
205
206// Tests that a method with expanded name compiles.
207TEST(ExpectCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) {
208 MockCC cc;
209 EXPECT_CALL(cc, Method());
210 cc.Method();
211}
212
213// Tests that it works, too.
214TEST(ExpectCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) {
215 MockCC cc;
216 EXPECT_CALL(cc, Method()).WillOnce(Return(42));
217 EXPECT_EQ(42, cc.Method());
218}
219
220#undef Method // Done with macro redefinition tests.
221
222// Tests that ON_CALL evaluates its arguments exactly once as promised
223// by Google Mock.
224TEST(OnCallSyntaxTest, EvaluatesFirstArgumentOnce) {
225 MockA a;
226 MockA* pa = &a;
227
228 ON_CALL(*pa++, DoA(_));
229 EXPECT_EQ(&a + 1, pa);
230}
231
232TEST(OnCallSyntaxTest, EvaluatesSecondArgumentOnce) {
233 MockA a;
234 int n = 0;
235
236 ON_CALL(a, DoA(n++));
237 EXPECT_EQ(1, n);
238}
239
240// Tests that the syntax of ON_CALL() is enforced at run time.
241
zhanyong.wanbf550852009-06-09 06:09:53 +0000242TEST(OnCallSyntaxTest, WithIsOptional) {
shiqiane35fdd92008-12-10 05:08:54 +0000243 MockA a;
244
245 ON_CALL(a, DoA(5))
246 .WillByDefault(Return());
247 ON_CALL(a, DoA(_))
zhanyong.wanbf550852009-06-09 06:09:53 +0000248 .With(_)
shiqiane35fdd92008-12-10 05:08:54 +0000249 .WillByDefault(Return());
250}
251
zhanyong.wanbf550852009-06-09 06:09:53 +0000252TEST(OnCallSyntaxTest, WithCanAppearAtMostOnce) {
shiqiane35fdd92008-12-10 05:08:54 +0000253 MockA a;
254
255 EXPECT_NONFATAL_FAILURE({ // NOLINT
256 ON_CALL(a, ReturnResult(_))
zhanyong.wanbf550852009-06-09 06:09:53 +0000257 .With(_)
258 .With(_)
shiqiane35fdd92008-12-10 05:08:54 +0000259 .WillByDefault(Return(Result()));
zhanyong.wanbf550852009-06-09 06:09:53 +0000260 }, ".With() cannot appear more than once in an ON_CALL()");
261}
262
shiqiane35fdd92008-12-10 05:08:54 +0000263TEST(OnCallSyntaxTest, WillByDefaultIsMandatory) {
264 MockA a;
265
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000266 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000267 ON_CALL(a, DoA(5));
268 a.DoA(5);
269 }, "");
270}
271
shiqiane35fdd92008-12-10 05:08:54 +0000272TEST(OnCallSyntaxTest, WillByDefaultCanAppearAtMostOnce) {
273 MockA a;
274
275 EXPECT_NONFATAL_FAILURE({ // NOLINT
276 ON_CALL(a, DoA(5))
277 .WillByDefault(Return())
278 .WillByDefault(Return());
279 }, ".WillByDefault() must appear exactly once in an ON_CALL()");
280}
281
282// Tests that EXPECT_CALL evaluates its arguments exactly once as
283// promised by Google Mock.
284TEST(ExpectCallSyntaxTest, EvaluatesFirstArgumentOnce) {
285 MockA a;
286 MockA* pa = &a;
287
288 EXPECT_CALL(*pa++, DoA(_));
289 a.DoA(0);
290 EXPECT_EQ(&a + 1, pa);
291}
292
293TEST(ExpectCallSyntaxTest, EvaluatesSecondArgumentOnce) {
294 MockA a;
295 int n = 0;
296
297 EXPECT_CALL(a, DoA(n++));
298 a.DoA(0);
299 EXPECT_EQ(1, n);
300}
301
302// Tests that the syntax of EXPECT_CALL() is enforced at run time.
303
zhanyong.wanbf550852009-06-09 06:09:53 +0000304TEST(ExpectCallSyntaxTest, WithIsOptional) {
shiqiane35fdd92008-12-10 05:08:54 +0000305 MockA a;
306
307 EXPECT_CALL(a, DoA(5))
308 .Times(0);
309 EXPECT_CALL(a, DoA(6))
zhanyong.wanbf550852009-06-09 06:09:53 +0000310 .With(_)
shiqiane35fdd92008-12-10 05:08:54 +0000311 .Times(0);
312}
313
zhanyong.wanbf550852009-06-09 06:09:53 +0000314TEST(ExpectCallSyntaxTest, WithCanAppearAtMostOnce) {
shiqiane35fdd92008-12-10 05:08:54 +0000315 MockA a;
316
317 EXPECT_NONFATAL_FAILURE({ // NOLINT
318 EXPECT_CALL(a, DoA(6))
zhanyong.wanbf550852009-06-09 06:09:53 +0000319 .With(_)
320 .With(_);
321 }, ".With() cannot appear more than once in an EXPECT_CALL()");
shiqiane35fdd92008-12-10 05:08:54 +0000322
323 a.DoA(6);
324}
325
zhanyong.wanbf550852009-06-09 06:09:53 +0000326TEST(ExpectCallSyntaxTest, WithMustBeFirstClause) {
shiqiane35fdd92008-12-10 05:08:54 +0000327 MockA a;
328
329 EXPECT_NONFATAL_FAILURE({ // NOLINT
330 EXPECT_CALL(a, DoA(1))
331 .Times(1)
zhanyong.wanbf550852009-06-09 06:09:53 +0000332 .With(_);
333 }, ".With() must be the first clause in an EXPECT_CALL()");
shiqiane35fdd92008-12-10 05:08:54 +0000334
335 a.DoA(1);
336
337 EXPECT_NONFATAL_FAILURE({ // NOLINT
338 EXPECT_CALL(a, DoA(2))
339 .WillOnce(Return())
zhanyong.wanbf550852009-06-09 06:09:53 +0000340 .With(_);
341 }, ".With() must be the first clause in an EXPECT_CALL()");
shiqiane35fdd92008-12-10 05:08:54 +0000342
343 a.DoA(2);
344}
345
346TEST(ExpectCallSyntaxTest, TimesCanBeInferred) {
347 MockA a;
348
349 EXPECT_CALL(a, DoA(1))
350 .WillOnce(Return());
351
352 EXPECT_CALL(a, DoA(2))
353 .WillOnce(Return())
354 .WillRepeatedly(Return());
355
356 a.DoA(1);
357 a.DoA(2);
358 a.DoA(2);
359}
360
361TEST(ExpectCallSyntaxTest, TimesCanAppearAtMostOnce) {
362 MockA a;
363
364 EXPECT_NONFATAL_FAILURE({ // NOLINT
365 EXPECT_CALL(a, DoA(1))
366 .Times(1)
367 .Times(2);
368 }, ".Times() cannot appear more than once in an EXPECT_CALL()");
369
370 a.DoA(1);
371 a.DoA(1);
372}
373
374TEST(ExpectCallSyntaxTest, TimesMustBeBeforeInSequence) {
375 MockA a;
376 Sequence s;
377
378 EXPECT_NONFATAL_FAILURE({ // NOLINT
379 EXPECT_CALL(a, DoA(1))
380 .InSequence(s)
381 .Times(1);
382 }, ".Times() cannot appear after ");
383
384 a.DoA(1);
385}
386
387TEST(ExpectCallSyntaxTest, InSequenceIsOptional) {
388 MockA a;
389 Sequence s;
390
391 EXPECT_CALL(a, DoA(1));
392 EXPECT_CALL(a, DoA(2))
393 .InSequence(s);
394
395 a.DoA(1);
396 a.DoA(2);
397}
398
399TEST(ExpectCallSyntaxTest, InSequenceCanAppearMultipleTimes) {
400 MockA a;
401 Sequence s1, s2;
402
403 EXPECT_CALL(a, DoA(1))
404 .InSequence(s1, s2)
405 .InSequence(s1);
406
407 a.DoA(1);
408}
409
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000410TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeAfter) {
411 MockA a;
412 Sequence s;
413
414 Expectation e = EXPECT_CALL(a, DoA(1))
415 .Times(AnyNumber());
416 EXPECT_NONFATAL_FAILURE({ // NOLINT
417 EXPECT_CALL(a, DoA(2))
418 .After(e)
419 .InSequence(s);
420 }, ".InSequence() cannot appear after ");
421
422 a.DoA(2);
423}
424
425TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWillOnce) {
shiqiane35fdd92008-12-10 05:08:54 +0000426 MockA a;
427 Sequence s;
428
429 EXPECT_NONFATAL_FAILURE({ // NOLINT
430 EXPECT_CALL(a, DoA(1))
431 .WillOnce(Return())
432 .InSequence(s);
433 }, ".InSequence() cannot appear after ");
434
435 a.DoA(1);
436}
437
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000438TEST(ExpectCallSyntaxTest, AfterMustBeBeforeWillOnce) {
439 MockA a;
440
441 Expectation e = EXPECT_CALL(a, DoA(1));
442 EXPECT_NONFATAL_FAILURE({
443 EXPECT_CALL(a, DoA(2))
444 .WillOnce(Return())
445 .After(e);
446 }, ".After() cannot appear after ");
447
448 a.DoA(1);
449 a.DoA(2);
450}
451
shiqiane35fdd92008-12-10 05:08:54 +0000452TEST(ExpectCallSyntaxTest, WillIsOptional) {
453 MockA a;
454
455 EXPECT_CALL(a, DoA(1));
456 EXPECT_CALL(a, DoA(2))
457 .WillOnce(Return());
458
459 a.DoA(1);
460 a.DoA(2);
461}
462
463TEST(ExpectCallSyntaxTest, WillCanAppearMultipleTimes) {
464 MockA a;
465
466 EXPECT_CALL(a, DoA(1))
467 .Times(AnyNumber())
468 .WillOnce(Return())
469 .WillOnce(Return())
470 .WillOnce(Return());
471}
472
473TEST(ExpectCallSyntaxTest, WillMustBeBeforeWillRepeatedly) {
474 MockA a;
475
476 EXPECT_NONFATAL_FAILURE({ // NOLINT
477 EXPECT_CALL(a, DoA(1))
478 .WillRepeatedly(Return())
479 .WillOnce(Return());
480 }, ".WillOnce() cannot appear after ");
481
482 a.DoA(1);
483}
484
485TEST(ExpectCallSyntaxTest, WillRepeatedlyIsOptional) {
486 MockA a;
487
488 EXPECT_CALL(a, DoA(1))
489 .WillOnce(Return());
490 EXPECT_CALL(a, DoA(2))
491 .WillOnce(Return())
492 .WillRepeatedly(Return());
493
494 a.DoA(1);
495 a.DoA(2);
496 a.DoA(2);
497}
498
499TEST(ExpectCallSyntaxTest, WillRepeatedlyCannotAppearMultipleTimes) {
500 MockA a;
501
502 EXPECT_NONFATAL_FAILURE({ // NOLINT
503 EXPECT_CALL(a, DoA(1))
504 .WillRepeatedly(Return())
505 .WillRepeatedly(Return());
506 }, ".WillRepeatedly() cannot appear more than once in an "
507 "EXPECT_CALL()");
508}
509
510TEST(ExpectCallSyntaxTest, WillRepeatedlyMustBeBeforeRetiresOnSaturation) {
511 MockA a;
512
513 EXPECT_NONFATAL_FAILURE({ // NOLINT
514 EXPECT_CALL(a, DoA(1))
515 .RetiresOnSaturation()
516 .WillRepeatedly(Return());
517 }, ".WillRepeatedly() cannot appear after ");
518}
519
520TEST(ExpectCallSyntaxTest, RetiresOnSaturationIsOptional) {
521 MockA a;
522
523 EXPECT_CALL(a, DoA(1));
524 EXPECT_CALL(a, DoA(1))
525 .RetiresOnSaturation();
526
527 a.DoA(1);
528 a.DoA(1);
529}
530
531TEST(ExpectCallSyntaxTest, RetiresOnSaturationCannotAppearMultipleTimes) {
532 MockA a;
533
534 EXPECT_NONFATAL_FAILURE({ // NOLINT
535 EXPECT_CALL(a, DoA(1))
536 .RetiresOnSaturation()
537 .RetiresOnSaturation();
538 }, ".RetiresOnSaturation() cannot appear more than once");
539
540 a.DoA(1);
541}
542
543TEST(ExpectCallSyntaxTest, DefaultCardinalityIsOnce) {
544 {
545 MockA a;
546 EXPECT_CALL(a, DoA(1));
547 a.DoA(1);
548 }
549 EXPECT_NONFATAL_FAILURE({ // NOLINT
550 MockA a;
551 EXPECT_CALL(a, DoA(1));
552 }, "to be called once");
553 EXPECT_NONFATAL_FAILURE({ // NOLINT
554 MockA a;
555 EXPECT_CALL(a, DoA(1));
556 a.DoA(1);
557 a.DoA(1);
558 }, "to be called once");
559}
560
zhanyong.wan2516f602010-08-31 18:28:02 +0000561#if GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000562
563// Tests that Google Mock doesn't print a warning when the number of
564// WillOnce() is adequate.
565TEST(ExpectCallSyntaxTest, DoesNotWarnOnAdequateActionCount) {
zhanyong.wan470df422010-02-02 22:34:58 +0000566 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000567 {
568 MockB b;
569
570 // It's always fine to omit WillOnce() entirely.
571 EXPECT_CALL(b, DoB())
572 .Times(0);
573 EXPECT_CALL(b, DoB(1))
574 .Times(AtMost(1));
575 EXPECT_CALL(b, DoB(2))
576 .Times(1)
577 .WillRepeatedly(Return(1));
578
579 // It's fine for the number of WillOnce()s to equal the upper bound.
580 EXPECT_CALL(b, DoB(3))
581 .Times(Between(1, 2))
582 .WillOnce(Return(1))
583 .WillOnce(Return(2));
584
585 // It's fine for the number of WillOnce()s to be smaller than the
586 // upper bound when there is a WillRepeatedly().
587 EXPECT_CALL(b, DoB(4))
588 .Times(AtMost(3))
589 .WillOnce(Return(1))
590 .WillRepeatedly(Return(2));
591
592 // Satisfies the above expectations.
593 b.DoB(2);
594 b.DoB(3);
595 }
zhanyong.wan470df422010-02-02 22:34:58 +0000596 EXPECT_STREQ("", GetCapturedStdout().c_str());
shiqiane35fdd92008-12-10 05:08:54 +0000597}
598
599// Tests that Google Mock warns on having too many actions in an
600// expectation compared to its cardinality.
601TEST(ExpectCallSyntaxTest, WarnsOnTooManyActions) {
zhanyong.wan470df422010-02-02 22:34:58 +0000602 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000603 {
604 MockB b;
605
606 // Warns when the number of WillOnce()s is larger than the upper bound.
607 EXPECT_CALL(b, DoB())
608 .Times(0)
609 .WillOnce(Return(1)); // #1
610 EXPECT_CALL(b, DoB())
611 .Times(AtMost(1))
612 .WillOnce(Return(1))
613 .WillOnce(Return(2)); // #2
614 EXPECT_CALL(b, DoB(1))
615 .Times(1)
616 .WillOnce(Return(1))
617 .WillOnce(Return(2))
618 .RetiresOnSaturation(); // #3
619
620 // Warns when the number of WillOnce()s equals the upper bound and
621 // there is a WillRepeatedly().
622 EXPECT_CALL(b, DoB())
623 .Times(0)
624 .WillRepeatedly(Return(1)); // #4
625 EXPECT_CALL(b, DoB(2))
626 .Times(1)
627 .WillOnce(Return(1))
628 .WillRepeatedly(Return(2)); // #5
629
630 // Satisfies the above expectations.
631 b.DoB(1);
632 b.DoB(2);
633 }
jgm38513a82012-11-15 15:50:36 +0000634 const std::string output = GetCapturedStdout();
vladlosev6c54a5e2009-10-21 06:15:34 +0000635 EXPECT_PRED_FORMAT2(
636 IsSubstring,
637 "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
638 "Expected to be never called, but has 1 WillOnce().",
639 output); // #1
640 EXPECT_PRED_FORMAT2(
641 IsSubstring,
642 "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
643 "Expected to be called at most once, "
644 "but has 2 WillOnce()s.",
645 output); // #2
646 EXPECT_PRED_FORMAT2(
647 IsSubstring,
648 "Too many actions specified in EXPECT_CALL(b, DoB(1))...\n"
649 "Expected to be called once, but has 2 WillOnce()s.",
650 output); // #3
651 EXPECT_PRED_FORMAT2(
652 IsSubstring,
653 "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
654 "Expected to be never called, but has 0 WillOnce()s "
655 "and a WillRepeatedly().",
656 output); // #4
657 EXPECT_PRED_FORMAT2(
658 IsSubstring,
659 "Too many actions specified in EXPECT_CALL(b, DoB(2))...\n"
660 "Expected to be called once, but has 1 WillOnce() "
661 "and a WillRepeatedly().",
662 output); // #5
shiqiane35fdd92008-12-10 05:08:54 +0000663}
664
665// Tests that Google Mock warns on having too few actions in an
666// expectation compared to its cardinality.
667TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) {
668 MockB b;
669
670 EXPECT_CALL(b, DoB())
671 .Times(Between(2, 3))
672 .WillOnce(Return(1));
673
zhanyong.wan470df422010-02-02 22:34:58 +0000674 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000675 b.DoB();
jgm38513a82012-11-15 15:50:36 +0000676 const std::string output = GetCapturedStdout();
vladlosev6c54a5e2009-10-21 06:15:34 +0000677 EXPECT_PRED_FORMAT2(
678 IsSubstring,
679 "Too few actions specified in EXPECT_CALL(b, DoB())...\n"
680 "Expected to be called between 2 and 3 times, "
681 "but has only 1 WillOnce().",
682 output);
shiqiane35fdd92008-12-10 05:08:54 +0000683 b.DoB();
684}
685
zhanyong.wan2516f602010-08-31 18:28:02 +0000686#endif // GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000687
688// Tests the semantics of ON_CALL().
689
690// Tests that the built-in default action is taken when no ON_CALL()
691// is specified.
692TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCall) {
693 MockB b;
694 EXPECT_CALL(b, DoB());
695
696 EXPECT_EQ(0, b.DoB());
697}
698
699// Tests that the built-in default action is taken when no ON_CALL()
700// matches the invocation.
701TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCallMatches) {
702 MockB b;
703 ON_CALL(b, DoB(1))
704 .WillByDefault(Return(1));
705 EXPECT_CALL(b, DoB(_));
706
707 EXPECT_EQ(0, b.DoB(2));
708}
709
710// Tests that the last matching ON_CALL() action is taken.
711TEST(OnCallTest, PicksLastMatchingOnCall) {
712 MockB b;
713 ON_CALL(b, DoB(_))
714 .WillByDefault(Return(3));
715 ON_CALL(b, DoB(2))
716 .WillByDefault(Return(2));
717 ON_CALL(b, DoB(1))
718 .WillByDefault(Return(1));
719 EXPECT_CALL(b, DoB(_));
720
721 EXPECT_EQ(2, b.DoB(2));
722}
723
724// Tests the semantics of EXPECT_CALL().
725
726// Tests that any call is allowed when no EXPECT_CALL() is specified.
727TEST(ExpectCallTest, AllowsAnyCallWhenNoSpec) {
728 MockB b;
729 EXPECT_CALL(b, DoB());
730 // There is no expectation on DoB(int).
731
732 b.DoB();
733
734 // DoB(int) can be called any number of times.
735 b.DoB(1);
736 b.DoB(2);
737}
738
739// Tests that the last matching EXPECT_CALL() fires.
740TEST(ExpectCallTest, PicksLastMatchingExpectCall) {
741 MockB b;
742 EXPECT_CALL(b, DoB(_))
743 .WillRepeatedly(Return(2));
744 EXPECT_CALL(b, DoB(1))
745 .WillRepeatedly(Return(1));
746
747 EXPECT_EQ(1, b.DoB(1));
748}
749
750// Tests lower-bound violation.
751TEST(ExpectCallTest, CatchesTooFewCalls) {
752 EXPECT_NONFATAL_FAILURE({ // NOLINT
753 MockB b;
754 EXPECT_CALL(b, DoB(5))
755 .Times(AtLeast(2));
756
757 b.DoB(5);
vladlosev6c54a5e2009-10-21 06:15:34 +0000758 }, "Actual function call count doesn't match EXPECT_CALL(b, DoB(5))...\n"
shiqiane35fdd92008-12-10 05:08:54 +0000759 " Expected: to be called at least twice\n"
760 " Actual: called once - unsatisfied and active");
761}
762
763// Tests that the cardinality can be inferred when no Times(...) is
764// specified.
765TEST(ExpectCallTest, InfersCardinalityWhenThereIsNoWillRepeatedly) {
766 {
767 MockB b;
768 EXPECT_CALL(b, DoB())
769 .WillOnce(Return(1))
770 .WillOnce(Return(2));
771
772 EXPECT_EQ(1, b.DoB());
773 EXPECT_EQ(2, b.DoB());
774 }
775
776 EXPECT_NONFATAL_FAILURE({ // NOLINT
777 MockB b;
778 EXPECT_CALL(b, DoB())
779 .WillOnce(Return(1))
780 .WillOnce(Return(2));
781
782 EXPECT_EQ(1, b.DoB());
783 }, "to be called twice");
784
785 { // NOLINT
786 MockB b;
787 EXPECT_CALL(b, DoB())
788 .WillOnce(Return(1))
789 .WillOnce(Return(2));
790
791 EXPECT_EQ(1, b.DoB());
792 EXPECT_EQ(2, b.DoB());
793 EXPECT_NONFATAL_FAILURE(b.DoB(), "to be called twice");
794 }
795}
796
797TEST(ExpectCallTest, InfersCardinality1WhenThereIsWillRepeatedly) {
798 {
799 MockB b;
800 EXPECT_CALL(b, DoB())
801 .WillOnce(Return(1))
802 .WillRepeatedly(Return(2));
803
804 EXPECT_EQ(1, b.DoB());
805 }
806
807 { // NOLINT
808 MockB b;
809 EXPECT_CALL(b, DoB())
810 .WillOnce(Return(1))
811 .WillRepeatedly(Return(2));
812
813 EXPECT_EQ(1, b.DoB());
814 EXPECT_EQ(2, b.DoB());
815 EXPECT_EQ(2, b.DoB());
816 }
817
818 EXPECT_NONFATAL_FAILURE({ // NOLINT
819 MockB b;
820 EXPECT_CALL(b, DoB())
821 .WillOnce(Return(1))
822 .WillRepeatedly(Return(2));
823 }, "to be called at least once");
824}
825
826// Tests that the n-th action is taken for the n-th matching
827// invocation.
828TEST(ExpectCallTest, NthMatchTakesNthAction) {
829 MockB b;
830 EXPECT_CALL(b, DoB())
831 .WillOnce(Return(1))
832 .WillOnce(Return(2))
833 .WillOnce(Return(3));
834
835 EXPECT_EQ(1, b.DoB());
836 EXPECT_EQ(2, b.DoB());
837 EXPECT_EQ(3, b.DoB());
838}
839
vladloseve5121b52011-02-11 23:50:38 +0000840// Tests that the WillRepeatedly() action is taken when the WillOnce(...)
841// list is exhausted.
842TEST(ExpectCallTest, TakesRepeatedActionWhenWillListIsExhausted) {
843 MockB b;
844 EXPECT_CALL(b, DoB())
845 .WillOnce(Return(1))
846 .WillRepeatedly(Return(2));
847
848 EXPECT_EQ(1, b.DoB());
849 EXPECT_EQ(2, b.DoB());
850 EXPECT_EQ(2, b.DoB());
851}
852
zhanyong.wan2516f602010-08-31 18:28:02 +0000853#if GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000854
855// Tests that the default action is taken when the WillOnce(...) list is
856// exhausted and there is no WillRepeatedly().
857TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) {
858 MockB b;
859 EXPECT_CALL(b, DoB(_))
860 .Times(1);
861 EXPECT_CALL(b, DoB())
862 .Times(AnyNumber())
863 .WillOnce(Return(1))
864 .WillOnce(Return(2));
865
zhanyong.wan470df422010-02-02 22:34:58 +0000866 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000867 EXPECT_EQ(0, b.DoB(1)); // Shouldn't generate a warning as the
868 // expectation has no action clause at all.
869 EXPECT_EQ(1, b.DoB());
870 EXPECT_EQ(2, b.DoB());
jgm38513a82012-11-15 15:50:36 +0000871 const std::string output1 = GetCapturedStdout();
zhanyong.wan470df422010-02-02 22:34:58 +0000872 EXPECT_STREQ("", output1.c_str());
shiqiane35fdd92008-12-10 05:08:54 +0000873
zhanyong.wan470df422010-02-02 22:34:58 +0000874 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000875 EXPECT_EQ(0, b.DoB());
876 EXPECT_EQ(0, b.DoB());
jgm38513a82012-11-15 15:50:36 +0000877 const std::string output2 = GetCapturedStdout();
zhanyong.wan470df422010-02-02 22:34:58 +0000878 EXPECT_THAT(output2.c_str(),
879 HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n"
880 "Called 3 times, but only 2 WillOnce()s are specified"
881 " - returning default value."));
882 EXPECT_THAT(output2.c_str(),
883 HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n"
884 "Called 4 times, but only 2 WillOnce()s are specified"
885 " - returning default value."));
shiqiane35fdd92008-12-10 05:08:54 +0000886}
887
vladloseve5121b52011-02-11 23:50:38 +0000888TEST(FunctionMockerTest, ReportsExpectCallLocationForExhausedActions) {
shiqiane35fdd92008-12-10 05:08:54 +0000889 MockB b;
vladloseve5121b52011-02-11 23:50:38 +0000890 std::string expect_call_location = FormatFileLocation(__FILE__, __LINE__ + 1);
891 EXPECT_CALL(b, DoB()).Times(AnyNumber()).WillOnce(Return(1));
shiqiane35fdd92008-12-10 05:08:54 +0000892
893 EXPECT_EQ(1, b.DoB());
vladloseve5121b52011-02-11 23:50:38 +0000894
895 CaptureStdout();
896 EXPECT_EQ(0, b.DoB());
jgm38513a82012-11-15 15:50:36 +0000897 const std::string output = GetCapturedStdout();
vladloseve5121b52011-02-11 23:50:38 +0000898 // The warning message should contain the call location.
899 EXPECT_PRED_FORMAT2(IsSubstring, expect_call_location, output);
shiqiane35fdd92008-12-10 05:08:54 +0000900}
901
vladloseve5121b52011-02-11 23:50:38 +0000902TEST(FunctionMockerTest, ReportsDefaultActionLocationOfUninterestingCalls) {
903 std::string on_call_location;
904 CaptureStdout();
905 {
906 MockB b;
907 on_call_location = FormatFileLocation(__FILE__, __LINE__ + 1);
908 ON_CALL(b, DoB(_)).WillByDefault(Return(0));
909 b.DoB(0);
910 }
911 EXPECT_PRED_FORMAT2(IsSubstring, on_call_location, GetCapturedStdout());
912}
913
914#endif // GTEST_HAS_STREAM_REDIRECTION
915
shiqiane35fdd92008-12-10 05:08:54 +0000916// Tests that an uninteresting call performs the default action.
917TEST(UninterestingCallTest, DoesDefaultAction) {
918 // When there is an ON_CALL() statement, the action specified by it
919 // should be taken.
920 MockA a;
921 ON_CALL(a, Binary(_, _))
922 .WillByDefault(Return(true));
923 EXPECT_TRUE(a.Binary(1, 2));
924
925 // When there is no ON_CALL(), the default value for the return type
926 // should be returned.
927 MockB b;
928 EXPECT_EQ(0, b.DoB());
929}
930
931// Tests that an unexpected call performs the default action.
932TEST(UnexpectedCallTest, DoesDefaultAction) {
933 // When there is an ON_CALL() statement, the action specified by it
934 // should be taken.
935 MockA a;
936 ON_CALL(a, Binary(_, _))
937 .WillByDefault(Return(true));
938 EXPECT_CALL(a, Binary(0, 0));
939 a.Binary(0, 0);
940 bool result = false;
941 EXPECT_NONFATAL_FAILURE(result = a.Binary(1, 2),
942 "Unexpected mock function call");
943 EXPECT_TRUE(result);
944
945 // When there is no ON_CALL(), the default value for the return type
946 // should be returned.
947 MockB b;
948 EXPECT_CALL(b, DoB(0))
949 .Times(0);
950 int n = -1;
951 EXPECT_NONFATAL_FAILURE(n = b.DoB(1),
952 "Unexpected mock function call");
953 EXPECT_EQ(0, n);
954}
955
956// Tests that when an unexpected void function generates the right
957// failure message.
958TEST(UnexpectedCallTest, GeneratesFailureForVoidFunction) {
959 // First, tests the message when there is only one EXPECT_CALL().
960 MockA a1;
961 EXPECT_CALL(a1, DoA(1));
962 a1.DoA(1);
963 // Ideally we should match the failure message against a regex, but
964 // EXPECT_NONFATAL_FAILURE doesn't support that, so we test for
965 // multiple sub-strings instead.
966 EXPECT_NONFATAL_FAILURE(
967 a1.DoA(9),
968 "Unexpected mock function call - returning directly.\n"
969 " Function call: DoA(9)\n"
970 "Google Mock tried the following 1 expectation, but it didn't match:");
971 EXPECT_NONFATAL_FAILURE(
972 a1.DoA(9),
973 " Expected arg #0: is equal to 1\n"
974 " Actual: 9\n"
975 " Expected: to be called once\n"
976 " Actual: called once - saturated and active");
977
978 // Next, tests the message when there are more than one EXPECT_CALL().
979 MockA a2;
980 EXPECT_CALL(a2, DoA(1));
981 EXPECT_CALL(a2, DoA(3));
982 a2.DoA(1);
983 EXPECT_NONFATAL_FAILURE(
984 a2.DoA(2),
985 "Unexpected mock function call - returning directly.\n"
986 " Function call: DoA(2)\n"
987 "Google Mock tried the following 2 expectations, but none matched:");
988 EXPECT_NONFATAL_FAILURE(
989 a2.DoA(2),
vladlosev6c54a5e2009-10-21 06:15:34 +0000990 "tried expectation #0: EXPECT_CALL(a2, DoA(1))...\n"
shiqiane35fdd92008-12-10 05:08:54 +0000991 " Expected arg #0: is equal to 1\n"
992 " Actual: 2\n"
993 " Expected: to be called once\n"
994 " Actual: called once - saturated and active");
995 EXPECT_NONFATAL_FAILURE(
996 a2.DoA(2),
vladlosev6c54a5e2009-10-21 06:15:34 +0000997 "tried expectation #1: EXPECT_CALL(a2, DoA(3))...\n"
shiqiane35fdd92008-12-10 05:08:54 +0000998 " Expected arg #0: is equal to 3\n"
999 " Actual: 2\n"
1000 " Expected: to be called once\n"
1001 " Actual: never called - unsatisfied and active");
1002 a2.DoA(3);
1003}
1004
1005// Tests that an unexpected non-void function generates the right
1006// failure message.
1007TEST(UnexpectedCallTest, GeneartesFailureForNonVoidFunction) {
1008 MockB b1;
1009 EXPECT_CALL(b1, DoB(1));
1010 b1.DoB(1);
1011 EXPECT_NONFATAL_FAILURE(
1012 b1.DoB(2),
1013 "Unexpected mock function call - returning default value.\n"
1014 " Function call: DoB(2)\n"
1015 " Returns: 0\n"
1016 "Google Mock tried the following 1 expectation, but it didn't match:");
1017 EXPECT_NONFATAL_FAILURE(
1018 b1.DoB(2),
1019 " Expected arg #0: is equal to 1\n"
1020 " Actual: 2\n"
1021 " Expected: to be called once\n"
1022 " Actual: called once - saturated and active");
1023}
1024
1025// Tests that Google Mock explains that an retired expectation doesn't
1026// match the call.
1027TEST(UnexpectedCallTest, RetiredExpectation) {
1028 MockB b;
1029 EXPECT_CALL(b, DoB(1))
1030 .RetiresOnSaturation();
1031
1032 b.DoB(1);
1033 EXPECT_NONFATAL_FAILURE(
1034 b.DoB(1),
1035 " Expected: the expectation is active\n"
1036 " Actual: it is retired");
1037}
1038
1039// Tests that Google Mock explains that an expectation that doesn't
1040// match the arguments doesn't match the call.
1041TEST(UnexpectedCallTest, UnmatchedArguments) {
1042 MockB b;
1043 EXPECT_CALL(b, DoB(1));
1044
1045 EXPECT_NONFATAL_FAILURE(
1046 b.DoB(2),
1047 " Expected arg #0: is equal to 1\n"
1048 " Actual: 2\n");
1049 b.DoB(1);
1050}
1051
shiqiane35fdd92008-12-10 05:08:54 +00001052// Tests that Google Mock explains that an expectation with
1053// unsatisfied pre-requisites doesn't match the call.
1054TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) {
1055 Sequence s1, s2;
1056 MockB b;
1057 EXPECT_CALL(b, DoB(1))
1058 .InSequence(s1);
1059 EXPECT_CALL(b, DoB(2))
1060 .Times(AnyNumber())
1061 .InSequence(s1);
1062 EXPECT_CALL(b, DoB(3))
1063 .InSequence(s2);
1064 EXPECT_CALL(b, DoB(4))
1065 .InSequence(s1, s2);
1066
1067 ::testing::TestPartResultArray failures;
1068 {
1069 ::testing::ScopedFakeTestPartResultReporter reporter(&failures);
1070 b.DoB(4);
1071 // Now 'failures' contains the Google Test failures generated by
1072 // the above statement.
1073 }
1074
1075 // There should be one non-fatal failure.
1076 ASSERT_EQ(1, failures.size());
1077 const ::testing::TestPartResult& r = failures.GetTestPartResult(0);
zhanyong.wanbbd6e102009-09-18 18:17:19 +00001078 EXPECT_EQ(::testing::TestPartResult::kNonFatalFailure, r.type());
shiqiane35fdd92008-12-10 05:08:54 +00001079
1080 // Verifies that the failure message contains the two unsatisfied
1081 // pre-requisites but not the satisfied one.
zhanyong.wand14aaed2010-01-14 05:36:32 +00001082#if GTEST_USES_PCRE
1083 EXPECT_THAT(r.message(), ContainsRegex(
shiqiane35fdd92008-12-10 05:08:54 +00001084 // PCRE has trouble using (.|\n) to match any character, but
1085 // supports the (?s) prefix for using . to match any character.
1086 "(?s)the following immediate pre-requisites are not satisfied:\n"
1087 ".*: pre-requisite #0\n"
zhanyong.wand14aaed2010-01-14 05:36:32 +00001088 ".*: pre-requisite #1"));
1089#elif GTEST_USES_POSIX_RE
1090 EXPECT_THAT(r.message(), ContainsRegex(
shiqiane35fdd92008-12-10 05:08:54 +00001091 // POSIX RE doesn't understand the (?s) prefix, but has no trouble
1092 // with (.|\n).
1093 "the following immediate pre-requisites are not satisfied:\n"
1094 "(.|\n)*: pre-requisite #0\n"
zhanyong.wand14aaed2010-01-14 05:36:32 +00001095 "(.|\n)*: pre-requisite #1"));
1096#else
1097 // We can only use Google Test's own simple regex.
1098 EXPECT_THAT(r.message(), ContainsRegex(
1099 "the following immediate pre-requisites are not satisfied:"));
1100 EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #0"));
1101 EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #1"));
1102#endif // GTEST_USES_PCRE
shiqiane35fdd92008-12-10 05:08:54 +00001103
shiqiane35fdd92008-12-10 05:08:54 +00001104 b.DoB(1);
1105 b.DoB(3);
1106 b.DoB(4);
1107}
1108
zhanyong.wan5b95fa72009-01-27 22:28:45 +00001109TEST(UndefinedReturnValueTest, ReturnValueIsMandatory) {
1110 MockA a;
1111 // TODO(wan@google.com): We should really verify the output message,
1112 // but we cannot yet due to that EXPECT_DEATH only captures stderr
1113 // while Google Mock logs to stdout.
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001114#if GTEST_HAS_EXCEPTIONS
1115 EXPECT_ANY_THROW(a.ReturnResult(1));
1116#else
zhanyong.wan04d6ed82009-09-11 07:01:08 +00001117 EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(1), "");
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001118#endif
zhanyong.wan5b95fa72009-01-27 22:28:45 +00001119}
1120
shiqiane35fdd92008-12-10 05:08:54 +00001121// Tests that an excessive call (one whose arguments match the
1122// matchers but is called too many times) performs the default action.
1123TEST(ExcessiveCallTest, DoesDefaultAction) {
1124 // When there is an ON_CALL() statement, the action specified by it
1125 // should be taken.
1126 MockA a;
1127 ON_CALL(a, Binary(_, _))
1128 .WillByDefault(Return(true));
1129 EXPECT_CALL(a, Binary(0, 0));
1130 a.Binary(0, 0);
1131 bool result = false;
1132 EXPECT_NONFATAL_FAILURE(result = a.Binary(0, 0),
1133 "Mock function called more times than expected");
1134 EXPECT_TRUE(result);
1135
1136 // When there is no ON_CALL(), the default value for the return type
1137 // should be returned.
1138 MockB b;
1139 EXPECT_CALL(b, DoB(0))
1140 .Times(0);
1141 int n = -1;
1142 EXPECT_NONFATAL_FAILURE(n = b.DoB(0),
1143 "Mock function called more times than expected");
1144 EXPECT_EQ(0, n);
1145}
1146
1147// Tests that when a void function is called too many times,
1148// the failure message contains the argument values.
1149TEST(ExcessiveCallTest, GeneratesFailureForVoidFunction) {
1150 MockA a;
1151 EXPECT_CALL(a, DoA(_))
1152 .Times(0);
1153 EXPECT_NONFATAL_FAILURE(
1154 a.DoA(9),
1155 "Mock function called more times than expected - returning directly.\n"
1156 " Function call: DoA(9)\n"
1157 " Expected: to be never called\n"
1158 " Actual: called once - over-saturated and active");
1159}
1160
1161// Tests that when a non-void function is called too many times, the
1162// failure message contains the argument values and the return value.
1163TEST(ExcessiveCallTest, GeneratesFailureForNonVoidFunction) {
1164 MockB b;
1165 EXPECT_CALL(b, DoB(_));
1166 b.DoB(1);
1167 EXPECT_NONFATAL_FAILURE(
1168 b.DoB(2),
1169 "Mock function called more times than expected - "
1170 "returning default value.\n"
1171 " Function call: DoB(2)\n"
1172 " Returns: 0\n"
1173 " Expected: to be called once\n"
1174 " Actual: called twice - over-saturated and active");
1175}
1176
1177// Tests using sequences.
1178
1179TEST(InSequenceTest, AllExpectationInScopeAreInSequence) {
1180 MockA a;
1181 {
1182 InSequence dummy;
1183
1184 EXPECT_CALL(a, DoA(1));
1185 EXPECT_CALL(a, DoA(2));
1186 }
1187
1188 EXPECT_NONFATAL_FAILURE({ // NOLINT
1189 a.DoA(2);
1190 }, "Unexpected mock function call");
1191
1192 a.DoA(1);
1193 a.DoA(2);
1194}
1195
1196TEST(InSequenceTest, NestedInSequence) {
1197 MockA a;
1198 {
1199 InSequence dummy;
1200
1201 EXPECT_CALL(a, DoA(1));
1202 {
1203 InSequence dummy2;
1204
1205 EXPECT_CALL(a, DoA(2));
1206 EXPECT_CALL(a, DoA(3));
1207 }
1208 }
1209
1210 EXPECT_NONFATAL_FAILURE({ // NOLINT
1211 a.DoA(1);
1212 a.DoA(3);
1213 }, "Unexpected mock function call");
1214
1215 a.DoA(2);
1216 a.DoA(3);
1217}
1218
1219TEST(InSequenceTest, ExpectationsOutOfScopeAreNotAffected) {
1220 MockA a;
1221 {
1222 InSequence dummy;
1223
1224 EXPECT_CALL(a, DoA(1));
1225 EXPECT_CALL(a, DoA(2));
1226 }
1227 EXPECT_CALL(a, DoA(3));
1228
1229 EXPECT_NONFATAL_FAILURE({ // NOLINT
1230 a.DoA(2);
1231 }, "Unexpected mock function call");
1232
1233 a.DoA(3);
1234 a.DoA(1);
1235 a.DoA(2);
1236}
1237
1238// Tests that any order is allowed when no sequence is used.
1239TEST(SequenceTest, AnyOrderIsOkByDefault) {
1240 {
1241 MockA a;
1242 MockB b;
1243
1244 EXPECT_CALL(a, DoA(1));
1245 EXPECT_CALL(b, DoB())
1246 .Times(AnyNumber());
1247
1248 a.DoA(1);
1249 b.DoB();
1250 }
1251
1252 { // NOLINT
1253 MockA a;
1254 MockB b;
1255
1256 EXPECT_CALL(a, DoA(1));
1257 EXPECT_CALL(b, DoB())
1258 .Times(AnyNumber());
1259
1260 b.DoB();
1261 a.DoA(1);
1262 }
1263}
1264
shiqiane35fdd92008-12-10 05:08:54 +00001265// Tests that the calls must be in strict order when a complete order
1266// is specified.
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001267TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo1) {
shiqiane35fdd92008-12-10 05:08:54 +00001268 MockA a;
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001269 ON_CALL(a, ReturnResult(_))
1270 .WillByDefault(Return(Result()));
1271
shiqiane35fdd92008-12-10 05:08:54 +00001272 Sequence s;
shiqiane35fdd92008-12-10 05:08:54 +00001273 EXPECT_CALL(a, ReturnResult(1))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001274 .InSequence(s);
shiqiane35fdd92008-12-10 05:08:54 +00001275 EXPECT_CALL(a, ReturnResult(2))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001276 .InSequence(s);
shiqiane35fdd92008-12-10 05:08:54 +00001277 EXPECT_CALL(a, ReturnResult(3))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001278 .InSequence(s);
shiqiane35fdd92008-12-10 05:08:54 +00001279
1280 a.ReturnResult(1);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001281
1282 // May only be called after a.ReturnResult(2).
1283 EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call");
1284
shiqiane35fdd92008-12-10 05:08:54 +00001285 a.ReturnResult(2);
1286 a.ReturnResult(3);
1287}
1288
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001289// Tests that the calls must be in strict order when a complete order
1290// is specified.
1291TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo2) {
shiqiane35fdd92008-12-10 05:08:54 +00001292 MockA a;
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001293 ON_CALL(a, ReturnResult(_))
1294 .WillByDefault(Return(Result()));
shiqiane35fdd92008-12-10 05:08:54 +00001295
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001296 Sequence s;
shiqiane35fdd92008-12-10 05:08:54 +00001297 EXPECT_CALL(a, ReturnResult(1))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001298 .InSequence(s);
shiqiane35fdd92008-12-10 05:08:54 +00001299 EXPECT_CALL(a, ReturnResult(2))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001300 .InSequence(s);
shiqiane35fdd92008-12-10 05:08:54 +00001301
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001302 // May only be called after a.ReturnResult(1).
1303 EXPECT_NONFATAL_FAILURE(a.ReturnResult(2), "Unexpected mock function call");
shiqiane35fdd92008-12-10 05:08:54 +00001304
shiqiane35fdd92008-12-10 05:08:54 +00001305 a.ReturnResult(1);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001306 a.ReturnResult(2);
1307}
1308
1309// Tests specifying a DAG using multiple sequences.
1310class PartialOrderTest : public testing::Test {
1311 protected:
1312 PartialOrderTest() {
1313 ON_CALL(a_, ReturnResult(_))
1314 .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))
1323 .InSequence(x);
1324 EXPECT_CALL(b_, DoB())
1325 .Times(2)
1326 .InSequence(y);
1327 EXPECT_CALL(a_, ReturnResult(2))
1328 .Times(AnyNumber())
1329 .InSequence(x, y);
1330 EXPECT_CALL(a_, ReturnResult(3))
1331 .InSequence(x);
1332 }
1333
1334 MockA a_;
1335 MockB b_;
1336};
1337
1338TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag1) {
1339 a_.ReturnResult(1);
1340 b_.DoB();
1341
1342 // May only be called after the second DoB().
1343 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call");
1344
1345 b_.DoB();
1346 a_.ReturnResult(3);
1347}
1348
1349TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag2) {
1350 // May only be called after ReturnResult(1).
1351 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call");
1352
1353 a_.ReturnResult(1);
1354 b_.DoB();
1355 b_.DoB();
1356 a_.ReturnResult(3);
1357}
1358
1359TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag3) {
1360 // May only be called last.
1361 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(3), "Unexpected mock function call");
1362
1363 a_.ReturnResult(1);
1364 b_.DoB();
1365 b_.DoB();
1366 a_.ReturnResult(3);
1367}
1368
1369TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag4) {
1370 a_.ReturnResult(1);
1371 b_.DoB();
1372 b_.DoB();
1373 a_.ReturnResult(3);
1374
1375 // May only be called before ReturnResult(3).
1376 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call");
shiqiane35fdd92008-12-10 05:08:54 +00001377}
1378
shiqiane35fdd92008-12-10 05:08:54 +00001379TEST(SequenceTest, Retirement) {
1380 MockA a;
1381 Sequence s;
1382
1383 EXPECT_CALL(a, DoA(1))
1384 .InSequence(s);
1385 EXPECT_CALL(a, DoA(_))
1386 .InSequence(s)
1387 .RetiresOnSaturation();
1388 EXPECT_CALL(a, DoA(1))
1389 .InSequence(s);
1390
1391 a.DoA(1);
1392 a.DoA(2);
1393 a.DoA(1);
1394}
1395
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001396// Tests Expectation.
1397
1398TEST(ExpectationTest, ConstrutorsWork) {
1399 MockA a;
1400 Expectation e1; // Default ctor.
zhanyong.waned6c9272011-02-23 19:39:27 +00001401
1402 // Ctor from various forms of EXPECT_CALL.
1403 Expectation e2 = EXPECT_CALL(a, DoA(2));
1404 Expectation e3 = EXPECT_CALL(a, DoA(3)).With(_);
1405 {
1406 Sequence s;
1407 Expectation e4 = EXPECT_CALL(a, DoA(4)).Times(1);
1408 Expectation e5 = EXPECT_CALL(a, DoA(5)).InSequence(s);
1409 }
1410 Expectation e6 = EXPECT_CALL(a, DoA(6)).After(e2);
1411 Expectation e7 = EXPECT_CALL(a, DoA(7)).WillOnce(Return());
1412 Expectation e8 = EXPECT_CALL(a, DoA(8)).WillRepeatedly(Return());
1413 Expectation e9 = EXPECT_CALL(a, DoA(9)).RetiresOnSaturation();
1414
1415 Expectation e10 = e2; // Copy ctor.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001416
1417 EXPECT_THAT(e1, Ne(e2));
zhanyong.waned6c9272011-02-23 19:39:27 +00001418 EXPECT_THAT(e2, Eq(e10));
1419
1420 a.DoA(2);
1421 a.DoA(3);
1422 a.DoA(4);
1423 a.DoA(5);
1424 a.DoA(6);
1425 a.DoA(7);
1426 a.DoA(8);
1427 a.DoA(9);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001428}
1429
1430TEST(ExpectationTest, AssignmentWorks) {
1431 MockA a;
1432 Expectation e1;
1433 Expectation e2 = EXPECT_CALL(a, DoA(1));
1434
1435 EXPECT_THAT(e1, Ne(e2));
1436
1437 e1 = e2;
1438 EXPECT_THAT(e1, Eq(e2));
1439
1440 a.DoA(1);
1441}
1442
1443// Tests ExpectationSet.
1444
1445TEST(ExpectationSetTest, MemberTypesAreCorrect) {
1446 ::testing::StaticAssertTypeEq<Expectation, ExpectationSet::value_type>();
1447}
1448
1449TEST(ExpectationSetTest, ConstructorsWork) {
1450 MockA a;
1451
1452 Expectation e1;
1453 const Expectation e2;
1454 ExpectationSet es1; // Default ctor.
1455 ExpectationSet es2 = EXPECT_CALL(a, DoA(1)); // Ctor from EXPECT_CALL.
1456 ExpectationSet es3 = e1; // Ctor from Expectation.
1457 ExpectationSet es4(e1); // Ctor from Expectation; alternative syntax.
1458 ExpectationSet es5 = e2; // Ctor from const Expectation.
1459 ExpectationSet es6(e2); // Ctor from const Expectation; alternative syntax.
1460 ExpectationSet es7 = es2; // Copy ctor.
1461
1462 EXPECT_EQ(0, es1.size());
1463 EXPECT_EQ(1, es2.size());
1464 EXPECT_EQ(1, es3.size());
1465 EXPECT_EQ(1, es4.size());
1466 EXPECT_EQ(1, es5.size());
1467 EXPECT_EQ(1, es6.size());
1468 EXPECT_EQ(1, es7.size());
1469
1470 EXPECT_THAT(es3, Ne(es2));
1471 EXPECT_THAT(es4, Eq(es3));
1472 EXPECT_THAT(es5, Eq(es4));
1473 EXPECT_THAT(es6, Eq(es5));
1474 EXPECT_THAT(es7, Eq(es2));
1475 a.DoA(1);
1476}
1477
1478TEST(ExpectationSetTest, AssignmentWorks) {
1479 ExpectationSet es1;
1480 ExpectationSet es2 = Expectation();
1481
1482 es1 = es2;
1483 EXPECT_EQ(1, es1.size());
1484 EXPECT_THAT(*(es1.begin()), Eq(Expectation()));
1485 EXPECT_THAT(es1, Eq(es2));
1486}
1487
1488TEST(ExpectationSetTest, InsertionWorks) {
1489 ExpectationSet es1;
1490 Expectation e1;
1491 es1 += e1;
1492 EXPECT_EQ(1, es1.size());
1493 EXPECT_THAT(*(es1.begin()), Eq(e1));
1494
1495 MockA a;
1496 Expectation e2 = EXPECT_CALL(a, DoA(1));
1497 es1 += e2;
1498 EXPECT_EQ(2, es1.size());
1499
1500 ExpectationSet::const_iterator it1 = es1.begin();
1501 ExpectationSet::const_iterator it2 = it1;
1502 ++it2;
1503 EXPECT_TRUE(*it1 == e1 || *it2 == e1); // e1 must be in the set.
1504 EXPECT_TRUE(*it1 == e2 || *it2 == e2); // e2 must be in the set too.
1505 a.DoA(1);
1506}
1507
1508TEST(ExpectationSetTest, SizeWorks) {
1509 ExpectationSet es;
1510 EXPECT_EQ(0, es.size());
1511
1512 es += Expectation();
1513 EXPECT_EQ(1, es.size());
1514
1515 MockA a;
1516 es += EXPECT_CALL(a, DoA(1));
1517 EXPECT_EQ(2, es.size());
1518
1519 a.DoA(1);
1520}
1521
1522TEST(ExpectationSetTest, IsEnumerable) {
1523 ExpectationSet es;
1524 EXPECT_THAT(es.begin(), Eq(es.end()));
1525
1526 es += Expectation();
1527 ExpectationSet::const_iterator it = es.begin();
1528 EXPECT_THAT(it, Ne(es.end()));
1529 EXPECT_THAT(*it, Eq(Expectation()));
1530 ++it;
1531 EXPECT_THAT(it, Eq(es.end()));
1532}
1533
1534// Tests the .After() clause.
1535
1536TEST(AfterTest, SucceedsWhenPartialOrderIsSatisfied) {
1537 MockA a;
1538 ExpectationSet es;
1539 es += EXPECT_CALL(a, DoA(1));
1540 es += EXPECT_CALL(a, DoA(2));
1541 EXPECT_CALL(a, DoA(3))
1542 .After(es);
1543
1544 a.DoA(1);
1545 a.DoA(2);
1546 a.DoA(3);
1547}
1548
1549TEST(AfterTest, SucceedsWhenTotalOrderIsSatisfied) {
1550 MockA a;
1551 MockB b;
1552 // The following also verifies that const Expectation objects work
1553 // too. Do not remove the const modifiers.
1554 const Expectation e1 = EXPECT_CALL(a, DoA(1));
1555 const Expectation e2 = EXPECT_CALL(b, DoB())
1556 .Times(2)
1557 .After(e1);
1558 EXPECT_CALL(a, DoA(2)).After(e2);
1559
1560 a.DoA(1);
1561 b.DoB();
1562 b.DoB();
1563 a.DoA(2);
1564}
1565
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001566// Calls must be in strict order when specified so using .After().
1567TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo1) {
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001568 MockA a;
1569 MockB b;
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001570
1571 // Define ordering:
1572 // a.DoA(1) ==> b.DoB() ==> a.DoA(2)
1573 Expectation e1 = EXPECT_CALL(a, DoA(1));
1574 Expectation e2 = EXPECT_CALL(b, DoB())
1575 .After(e1);
1576 EXPECT_CALL(a, DoA(2))
1577 .After(e2);
1578
1579 a.DoA(1);
1580
1581 // May only be called after DoB().
1582 EXPECT_NONFATAL_FAILURE(a.DoA(2), "Unexpected mock function call");
1583
1584 b.DoB();
1585 a.DoA(2);
1586}
1587
1588// Calls must be in strict order when specified so using .After().
1589TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo2) {
1590 MockA a;
1591 MockB b;
1592
1593 // Define ordering:
1594 // a.DoA(1) ==> b.DoB() * 2 ==> a.DoA(2)
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001595 Expectation e1 = EXPECT_CALL(a, DoA(1));
1596 Expectation e2 = EXPECT_CALL(b, DoB())
1597 .Times(2)
1598 .After(e1);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001599 EXPECT_CALL(a, DoA(2))
1600 .After(e2);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001601
1602 a.DoA(1);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001603 b.DoB();
1604
1605 // May only be called after the second DoB().
1606 EXPECT_NONFATAL_FAILURE(a.DoA(2), "Unexpected mock function call");
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001607
1608 b.DoB();
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001609 a.DoA(2);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001610}
1611
1612// Calls must satisfy the partial order when specified so.
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001613TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo) {
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001614 MockA a;
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001615 ON_CALL(a, ReturnResult(_))
1616 .WillByDefault(Return(Result()));
1617
1618 // Define ordering:
1619 // a.DoA(1) ==>
1620 // a.DoA(2) ==> a.ReturnResult(3)
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001621 Expectation e = EXPECT_CALL(a, DoA(1));
1622 const ExpectationSet es = EXPECT_CALL(a, DoA(2));
1623 EXPECT_CALL(a, ReturnResult(3))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001624 .After(e, es);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001625
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001626 // May only be called last.
1627 EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call");
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001628
1629 a.DoA(2);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001630 a.DoA(1);
1631 a.ReturnResult(3);
1632}
1633
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001634// Calls must satisfy the partial order when specified so.
1635TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo2) {
1636 MockA a;
1637
1638 // Define ordering:
1639 // a.DoA(1) ==>
1640 // a.DoA(2) ==> a.DoA(3)
1641 Expectation e = EXPECT_CALL(a, DoA(1));
1642 const ExpectationSet es = EXPECT_CALL(a, DoA(2));
1643 EXPECT_CALL(a, DoA(3))
1644 .After(e, es);
1645
1646 a.DoA(2);
1647
1648 // May only be called last.
1649 EXPECT_NONFATAL_FAILURE(a.DoA(3), "Unexpected mock function call");
1650
1651 a.DoA(1);
1652 a.DoA(3);
1653}
1654
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001655// .After() can be combined with .InSequence().
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001656TEST(AfterTest, CanBeUsedWithInSequence) {
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001657 MockA a;
1658 Sequence s;
1659 Expectation e = EXPECT_CALL(a, DoA(1));
1660 EXPECT_CALL(a, DoA(2)).InSequence(s);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001661 EXPECT_CALL(a, DoA(3))
1662 .InSequence(s)
1663 .After(e);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001664
1665 a.DoA(1);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001666
1667 // May only be after DoA(2).
1668 EXPECT_NONFATAL_FAILURE(a.DoA(3), "Unexpected mock function call");
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001669
1670 a.DoA(2);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001671 a.DoA(3);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001672}
1673
1674// .After() can be called multiple times.
1675TEST(AfterTest, CanBeCalledManyTimes) {
1676 MockA a;
1677 Expectation e1 = EXPECT_CALL(a, DoA(1));
1678 Expectation e2 = EXPECT_CALL(a, DoA(2));
1679 Expectation e3 = EXPECT_CALL(a, DoA(3));
1680 EXPECT_CALL(a, DoA(4))
1681 .After(e1)
1682 .After(e2)
1683 .After(e3);
1684
1685 a.DoA(3);
1686 a.DoA(1);
1687 a.DoA(2);
1688 a.DoA(4);
1689}
1690
1691// .After() accepts up to 5 arguments.
1692TEST(AfterTest, AcceptsUpToFiveArguments) {
1693 MockA a;
1694 Expectation e1 = EXPECT_CALL(a, DoA(1));
1695 Expectation e2 = EXPECT_CALL(a, DoA(2));
1696 Expectation e3 = EXPECT_CALL(a, DoA(3));
1697 ExpectationSet es1 = EXPECT_CALL(a, DoA(4));
1698 ExpectationSet es2 = EXPECT_CALL(a, DoA(5));
1699 EXPECT_CALL(a, DoA(6))
1700 .After(e1, e2, e3, es1, es2);
1701
1702 a.DoA(5);
1703 a.DoA(2);
1704 a.DoA(4);
1705 a.DoA(1);
1706 a.DoA(3);
1707 a.DoA(6);
1708}
1709
1710// .After() allows input to contain duplicated Expectations.
1711TEST(AfterTest, AcceptsDuplicatedInput) {
1712 MockA a;
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001713 ON_CALL(a, ReturnResult(_))
1714 .WillByDefault(Return(Result()));
1715
1716 // Define ordering:
1717 // DoA(1) ==>
1718 // DoA(2) ==> ReturnResult(3)
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001719 Expectation e1 = EXPECT_CALL(a, DoA(1));
1720 Expectation e2 = EXPECT_CALL(a, DoA(2));
1721 ExpectationSet es;
1722 es += e1;
1723 es += e2;
1724 EXPECT_CALL(a, ReturnResult(3))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001725 .After(e1, e2, es, e1);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001726
1727 a.DoA(1);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001728
1729 // May only be after DoA(2).
1730 EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call");
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001731
1732 a.DoA(2);
1733 a.ReturnResult(3);
1734}
1735
1736// An Expectation added to an ExpectationSet after it has been used in
1737// an .After() has no effect.
1738TEST(AfterTest, ChangesToExpectationSetHaveNoEffectAfterwards) {
1739 MockA a;
1740 ExpectationSet es1 = EXPECT_CALL(a, DoA(1));
1741 Expectation e2 = EXPECT_CALL(a, DoA(2));
1742 EXPECT_CALL(a, DoA(3))
1743 .After(es1);
1744 es1 += e2;
1745
1746 a.DoA(1);
1747 a.DoA(3);
1748 a.DoA(2);
1749}
1750
shiqiane35fdd92008-12-10 05:08:54 +00001751// Tests that Google Mock correctly handles calls to mock functions
1752// after a mock object owning one of their pre-requisites has died.
1753
1754// Tests that calls that satisfy the original spec are successful.
1755TEST(DeletingMockEarlyTest, Success1) {
1756 MockB* const b1 = new MockB;
1757 MockA* const a = new MockA;
1758 MockB* const b2 = new MockB;
1759
1760 {
1761 InSequence dummy;
1762 EXPECT_CALL(*b1, DoB(_))
1763 .WillOnce(Return(1));
1764 EXPECT_CALL(*a, Binary(_, _))
1765 .Times(AnyNumber())
1766 .WillRepeatedly(Return(true));
1767 EXPECT_CALL(*b2, DoB(_))
1768 .Times(AnyNumber())
1769 .WillRepeatedly(Return(2));
1770 }
1771
1772 EXPECT_EQ(1, b1->DoB(1));
1773 delete b1;
1774 // a's pre-requisite has died.
1775 EXPECT_TRUE(a->Binary(0, 1));
1776 delete b2;
1777 // a's successor has died.
1778 EXPECT_TRUE(a->Binary(1, 2));
1779 delete a;
1780}
1781
1782// Tests that calls that satisfy the original spec are successful.
1783TEST(DeletingMockEarlyTest, Success2) {
1784 MockB* const b1 = new MockB;
1785 MockA* const a = new MockA;
1786 MockB* const b2 = new MockB;
1787
1788 {
1789 InSequence dummy;
1790 EXPECT_CALL(*b1, DoB(_))
1791 .WillOnce(Return(1));
1792 EXPECT_CALL(*a, Binary(_, _))
1793 .Times(AnyNumber());
1794 EXPECT_CALL(*b2, DoB(_))
1795 .Times(AnyNumber())
1796 .WillRepeatedly(Return(2));
1797 }
1798
1799 delete a; // a is trivially satisfied.
1800 EXPECT_EQ(1, b1->DoB(1));
1801 EXPECT_EQ(2, b2->DoB(2));
1802 delete b1;
1803 delete b2;
1804}
1805
zhanyong.wan6f147692009-03-03 06:44:08 +00001806// Tests that it's OK to delete a mock object itself in its action.
1807
zhanyong.wan32de5f52009-12-23 00:13:23 +00001808// Suppresses warning on unreferenced formal parameter in MSVC with
1809// -W4.
1810#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +00001811# pragma warning(push)
1812# pragma warning(disable:4100)
zhanyong.wan32de5f52009-12-23 00:13:23 +00001813#endif
1814
zhanyong.wan6f147692009-03-03 06:44:08 +00001815ACTION_P(Delete, ptr) { delete ptr; }
1816
zhanyong.wan32de5f52009-12-23 00:13:23 +00001817#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +00001818# pragma warning(pop)
zhanyong.wan32de5f52009-12-23 00:13:23 +00001819#endif
1820
zhanyong.wan6f147692009-03-03 06:44:08 +00001821TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningVoid) {
1822 MockA* const a = new MockA;
1823 EXPECT_CALL(*a, DoA(_)).WillOnce(Delete(a));
1824 a->DoA(42); // This will cause a to be deleted.
1825}
1826
1827TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningValue) {
1828 MockA* const a = new MockA;
1829 EXPECT_CALL(*a, ReturnResult(_))
1830 .WillOnce(DoAll(Delete(a), Return(Result())));
1831 a->ReturnResult(42); // This will cause a to be deleted.
1832}
1833
1834// Tests that calls that violate the original spec yield failures.
shiqiane35fdd92008-12-10 05:08:54 +00001835TEST(DeletingMockEarlyTest, Failure1) {
1836 MockB* const b1 = new MockB;
1837 MockA* const a = new MockA;
1838 MockB* const b2 = new MockB;
1839
1840 {
1841 InSequence dummy;
1842 EXPECT_CALL(*b1, DoB(_))
1843 .WillOnce(Return(1));
1844 EXPECT_CALL(*a, Binary(_, _))
1845 .Times(AnyNumber());
1846 EXPECT_CALL(*b2, DoB(_))
1847 .Times(AnyNumber())
1848 .WillRepeatedly(Return(2));
1849 }
1850
1851 delete a; // a is trivially satisfied.
1852 EXPECT_NONFATAL_FAILURE({
1853 b2->DoB(2);
1854 }, "Unexpected mock function call");
1855 EXPECT_EQ(1, b1->DoB(1));
1856 delete b1;
1857 delete b2;
1858}
1859
zhanyong.wan6f147692009-03-03 06:44:08 +00001860// Tests that calls that violate the original spec yield failures.
shiqiane35fdd92008-12-10 05:08:54 +00001861TEST(DeletingMockEarlyTest, Failure2) {
1862 MockB* const b1 = new MockB;
1863 MockA* const a = new MockA;
1864 MockB* const b2 = new MockB;
1865
1866 {
1867 InSequence dummy;
1868 EXPECT_CALL(*b1, DoB(_));
1869 EXPECT_CALL(*a, Binary(_, _))
1870 .Times(AnyNumber());
1871 EXPECT_CALL(*b2, DoB(_))
1872 .Times(AnyNumber());
1873 }
1874
1875 EXPECT_NONFATAL_FAILURE(delete b1,
1876 "Actual: never called");
1877 EXPECT_NONFATAL_FAILURE(a->Binary(0, 1),
1878 "Unexpected mock function call");
1879 EXPECT_NONFATAL_FAILURE(b2->DoB(1),
1880 "Unexpected mock function call");
1881 delete a;
1882 delete b2;
1883}
1884
1885class EvenNumberCardinality : public CardinalityInterface {
1886 public:
1887 // Returns true iff call_count calls will satisfy this cardinality.
1888 virtual bool IsSatisfiedByCallCount(int call_count) const {
1889 return call_count % 2 == 0;
1890 }
1891
1892 // Returns true iff call_count calls will saturate this cardinality.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001893 virtual bool IsSaturatedByCallCount(int /* call_count */) const {
1894 return false;
1895 }
shiqiane35fdd92008-12-10 05:08:54 +00001896
1897 // Describes self to an ostream.
1898 virtual void DescribeTo(::std::ostream* os) const {
1899 *os << "called even number of times";
1900 }
1901};
1902
1903Cardinality EvenNumber() {
1904 return Cardinality(new EvenNumberCardinality);
1905}
1906
1907TEST(ExpectationBaseTest,
1908 AllPrerequisitesAreSatisfiedWorksForNonMonotonicCardinality) {
1909 MockA* a = new MockA;
1910 Sequence s;
1911
1912 EXPECT_CALL(*a, DoA(1))
1913 .Times(EvenNumber())
1914 .InSequence(s);
1915 EXPECT_CALL(*a, DoA(2))
1916 .Times(AnyNumber())
1917 .InSequence(s);
1918 EXPECT_CALL(*a, DoA(3))
1919 .Times(AnyNumber());
1920
1921 a->DoA(3);
1922 a->DoA(1);
1923 EXPECT_NONFATAL_FAILURE(a->DoA(2), "Unexpected mock function call");
1924 EXPECT_NONFATAL_FAILURE(delete a, "to be called even number of times");
1925}
1926
1927// The following tests verify the message generated when a mock
1928// function is called.
1929
1930struct Printable {
1931};
1932
1933inline void operator<<(::std::ostream& os, const Printable&) {
1934 os << "Printable";
1935}
1936
1937struct Unprintable {
1938 Unprintable() : value(0) {}
1939 int value;
1940};
1941
1942class MockC {
1943 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00001944 MockC() {}
1945
shiqiane35fdd92008-12-10 05:08:54 +00001946 MOCK_METHOD6(VoidMethod, void(bool cond, int n, string s, void* p,
1947 const Printable& x, Unprintable y));
1948 MOCK_METHOD0(NonVoidMethod, int()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +00001949
1950 private:
1951 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockC);
shiqiane35fdd92008-12-10 05:08:54 +00001952};
1953
vladlosev76c1c612010-05-05 19:47:46 +00001954class VerboseFlagPreservingFixture : public testing::Test {
1955 protected:
vladlosev76c1c612010-05-05 19:47:46 +00001956 VerboseFlagPreservingFixture()
jgm38513a82012-11-15 15:50:36 +00001957 : saved_verbose_flag_(GMOCK_FLAG(verbose)) {}
vladlosev76c1c612010-05-05 19:47:46 +00001958
1959 ~VerboseFlagPreservingFixture() { GMOCK_FLAG(verbose) = saved_verbose_flag_; }
1960
1961 private:
1962 const string saved_verbose_flag_;
1963
1964 GTEST_DISALLOW_COPY_AND_ASSIGN_(VerboseFlagPreservingFixture);
1965};
1966
zhanyong.wan2516f602010-08-31 18:28:02 +00001967#if GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +00001968
1969// Tests that an uninteresting mock function call generates a warning
1970// containing the stack trace.
1971TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) {
1972 MockC c;
zhanyong.wan470df422010-02-02 22:34:58 +00001973 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00001974 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable());
jgm38513a82012-11-15 15:50:36 +00001975 const std::string output = GetCapturedStdout();
shiqiane35fdd92008-12-10 05:08:54 +00001976 EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output);
1977 EXPECT_PRED_FORMAT2(IsSubstring, "Stack trace:", output);
zhanyong.wan658ac0b2011-02-24 07:29:13 +00001978
1979# ifndef NDEBUG
1980
shiqiane35fdd92008-12-10 05:08:54 +00001981 // We check the stack trace content in dbg-mode only, as opt-mode
1982 // may inline the call we are interested in seeing.
1983
1984 // Verifies that a void mock function's name appears in the stack
1985 // trace.
zhanyong.wan470df422010-02-02 22:34:58 +00001986 EXPECT_PRED_FORMAT2(IsSubstring, "VoidMethod(", output);
shiqiane35fdd92008-12-10 05:08:54 +00001987
1988 // Verifies that a non-void mock function's name appears in the
1989 // stack trace.
zhanyong.wan470df422010-02-02 22:34:58 +00001990 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00001991 c.NonVoidMethod();
jgm38513a82012-11-15 15:50:36 +00001992 const std::string output2 = GetCapturedStdout();
zhanyong.wan470df422010-02-02 22:34:58 +00001993 EXPECT_PRED_FORMAT2(IsSubstring, "NonVoidMethod(", output2);
zhanyong.wan658ac0b2011-02-24 07:29:13 +00001994
1995# endif // NDEBUG
shiqiane35fdd92008-12-10 05:08:54 +00001996}
1997
1998// Tests that an uninteresting mock function call causes the function
1999// arguments and return value to be printed.
2000TEST(FunctionCallMessageTest, UninterestingCallPrintsArgumentsAndReturnValue) {
2001 // A non-void mock function.
2002 MockB b;
zhanyong.wan470df422010-02-02 22:34:58 +00002003 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002004 b.DoB();
jgm38513a82012-11-15 15:50:36 +00002005 const std::string output1 = GetCapturedStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002006 EXPECT_PRED_FORMAT2(
2007 IsSubstring,
2008 "Uninteresting mock function call - returning default value.\n"
2009 " Function call: DoB()\n"
zhanyong.wan470df422010-02-02 22:34:58 +00002010 " Returns: 0\n", output1.c_str());
shiqiane35fdd92008-12-10 05:08:54 +00002011 // Makes sure the return value is printed.
2012
2013 // A void mock function.
2014 MockC c;
zhanyong.wan470df422010-02-02 22:34:58 +00002015 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002016 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable());
jgm38513a82012-11-15 15:50:36 +00002017 const std::string output2 = GetCapturedStdout();
zhanyong.wan470df422010-02-02 22:34:58 +00002018 EXPECT_THAT(output2.c_str(),
2019 ContainsRegex(
2020 "Uninteresting mock function call - returning directly\\.\n"
2021 " Function call: VoidMethod"
2022 "\\(false, 5, \"Hi\", NULL, @.+ "
zhanyong.wanc6333dc2010-08-09 18:20:45 +00002023 "Printable, 4-byte object <00-00 00-00>\\)"));
shiqiane35fdd92008-12-10 05:08:54 +00002024 // A void function has no return value to print.
2025}
2026
2027// Tests how the --gmock_verbose flag affects Google Mock's output.
2028
vladlosev76c1c612010-05-05 19:47:46 +00002029class GMockVerboseFlagTest : public VerboseFlagPreservingFixture {
shiqiane35fdd92008-12-10 05:08:54 +00002030 public:
2031 // Verifies that the given Google Mock output is correct. (When
2032 // should_print is true, the output should match the given regex and
2033 // contain the given function name in the stack trace. When it's
2034 // false, the output should be empty.)
jgm38513a82012-11-15 15:50:36 +00002035 void VerifyOutput(const std::string& output, bool should_print,
zhanyong.wan470df422010-02-02 22:34:58 +00002036 const string& expected_substring,
shiqiane35fdd92008-12-10 05:08:54 +00002037 const string& function_name) {
2038 if (should_print) {
zhanyong.wan470df422010-02-02 22:34:58 +00002039 EXPECT_THAT(output.c_str(), HasSubstr(expected_substring));
zhanyong.wan658ac0b2011-02-24 07:29:13 +00002040# ifndef NDEBUG
shiqiane35fdd92008-12-10 05:08:54 +00002041 // We check the stack trace content in dbg-mode only, as opt-mode
2042 // may inline the call we are interested in seeing.
zhanyong.wan470df422010-02-02 22:34:58 +00002043 EXPECT_THAT(output.c_str(), HasSubstr(function_name));
zhanyong.wan658ac0b2011-02-24 07:29:13 +00002044# else
zhanyong.wan470df422010-02-02 22:34:58 +00002045 // Suppresses 'unused function parameter' warnings.
2046 static_cast<void>(function_name);
zhanyong.wan658ac0b2011-02-24 07:29:13 +00002047# endif // NDEBUG
shiqiane35fdd92008-12-10 05:08:54 +00002048 } else {
zhanyong.wan470df422010-02-02 22:34:58 +00002049 EXPECT_STREQ("", output.c_str());
shiqiane35fdd92008-12-10 05:08:54 +00002050 }
2051 }
2052
2053 // Tests how the flag affects expected calls.
2054 void TestExpectedCall(bool should_print) {
2055 MockA a;
2056 EXPECT_CALL(a, DoA(5));
2057 EXPECT_CALL(a, Binary(_, 1))
2058 .WillOnce(Return(true));
2059
2060 // A void-returning function.
zhanyong.wan470df422010-02-02 22:34:58 +00002061 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002062 a.DoA(5);
2063 VerifyOutput(
zhanyong.wan470df422010-02-02 22:34:58 +00002064 GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +00002065 should_print,
zhanyong.wan470df422010-02-02 22:34:58 +00002066 "Mock function call matches EXPECT_CALL(a, DoA(5))...\n"
2067 " Function call: DoA(5)\n"
2068 "Stack trace:\n",
2069 "DoA");
shiqiane35fdd92008-12-10 05:08:54 +00002070
2071 // A non-void-returning function.
zhanyong.wan470df422010-02-02 22:34:58 +00002072 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002073 a.Binary(2, 1);
2074 VerifyOutput(
zhanyong.wan470df422010-02-02 22:34:58 +00002075 GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +00002076 should_print,
zhanyong.wan470df422010-02-02 22:34:58 +00002077 "Mock function call matches EXPECT_CALL(a, Binary(_, 1))...\n"
2078 " Function call: Binary(2, 1)\n"
shiqiane35fdd92008-12-10 05:08:54 +00002079 " Returns: true\n"
zhanyong.wan470df422010-02-02 22:34:58 +00002080 "Stack trace:\n",
2081 "Binary");
shiqiane35fdd92008-12-10 05:08:54 +00002082 }
2083
2084 // Tests how the flag affects uninteresting calls.
2085 void TestUninterestingCall(bool should_print) {
2086 MockA a;
2087
2088 // A void-returning function.
zhanyong.wan470df422010-02-02 22:34:58 +00002089 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002090 a.DoA(5);
2091 VerifyOutput(
zhanyong.wan470df422010-02-02 22:34:58 +00002092 GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +00002093 should_print,
2094 "\nGMOCK WARNING:\n"
zhanyong.wan470df422010-02-02 22:34:58 +00002095 "Uninteresting mock function call - returning directly.\n"
2096 " Function call: DoA(5)\n"
2097 "Stack trace:\n",
2098 "DoA");
shiqiane35fdd92008-12-10 05:08:54 +00002099
2100 // A non-void-returning function.
zhanyong.wan470df422010-02-02 22:34:58 +00002101 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002102 a.Binary(2, 1);
2103 VerifyOutput(
zhanyong.wan470df422010-02-02 22:34:58 +00002104 GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +00002105 should_print,
2106 "\nGMOCK WARNING:\n"
zhanyong.wan470df422010-02-02 22:34:58 +00002107 "Uninteresting mock function call - returning default value.\n"
2108 " Function call: Binary(2, 1)\n"
shiqiane35fdd92008-12-10 05:08:54 +00002109 " Returns: false\n"
zhanyong.wan470df422010-02-02 22:34:58 +00002110 "Stack trace:\n",
2111 "Binary");
shiqiane35fdd92008-12-10 05:08:54 +00002112 }
2113};
2114
2115// Tests that --gmock_verbose=info causes both expected and
2116// uninteresting calls to be reported.
2117TEST_F(GMockVerboseFlagTest, Info) {
2118 GMOCK_FLAG(verbose) = kInfoVerbosity;
2119 TestExpectedCall(true);
2120 TestUninterestingCall(true);
2121}
2122
2123// Tests that --gmock_verbose=warning causes uninteresting calls to be
2124// reported.
2125TEST_F(GMockVerboseFlagTest, Warning) {
2126 GMOCK_FLAG(verbose) = kWarningVerbosity;
2127 TestExpectedCall(false);
2128 TestUninterestingCall(true);
2129}
2130
2131// Tests that --gmock_verbose=warning causes neither expected nor
2132// uninteresting calls to be reported.
2133TEST_F(GMockVerboseFlagTest, Error) {
2134 GMOCK_FLAG(verbose) = kErrorVerbosity;
2135 TestExpectedCall(false);
2136 TestUninterestingCall(false);
2137}
2138
2139// Tests that --gmock_verbose=SOME_INVALID_VALUE has the same effect
2140// as --gmock_verbose=warning.
2141TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) {
2142 GMOCK_FLAG(verbose) = "invalid"; // Treated as "warning".
2143 TestExpectedCall(false);
2144 TestUninterestingCall(true);
2145}
2146
zhanyong.wan2516f602010-08-31 18:28:02 +00002147#endif // GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +00002148
zhanyong.wan9413f2f2009-05-29 19:50:06 +00002149// A helper class that generates a failure when printed. We use it to
2150// ensure that Google Mock doesn't print a value (even to an internal
2151// buffer) when it is not supposed to do so.
2152class PrintMeNot {};
2153
2154void PrintTo(PrintMeNot /* dummy */, ::std::ostream* /* os */) {
2155 ADD_FAILURE() << "Google Mock is printing a value that shouldn't be "
2156 << "printed even to an internal buffer.";
2157}
2158
2159class LogTestHelper {
2160 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002161 LogTestHelper() {}
2162
zhanyong.wan9413f2f2009-05-29 19:50:06 +00002163 MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002164
2165 private:
2166 GTEST_DISALLOW_COPY_AND_ASSIGN_(LogTestHelper);
zhanyong.wan9413f2f2009-05-29 19:50:06 +00002167};
2168
vladlosev76c1c612010-05-05 19:47:46 +00002169class GMockLogTest : public VerboseFlagPreservingFixture {
zhanyong.wan9413f2f2009-05-29 19:50:06 +00002170 protected:
zhanyong.wan9413f2f2009-05-29 19:50:06 +00002171 LogTestHelper helper_;
zhanyong.wan9413f2f2009-05-29 19:50:06 +00002172};
2173
2174TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsWarning) {
2175 GMOCK_FLAG(verbose) = kWarningVerbosity;
2176 EXPECT_CALL(helper_, Foo(_))
2177 .WillOnce(Return(PrintMeNot()));
2178 helper_.Foo(PrintMeNot()); // This is an expected call.
2179}
2180
2181TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsError) {
2182 GMOCK_FLAG(verbose) = kErrorVerbosity;
2183 EXPECT_CALL(helper_, Foo(_))
2184 .WillOnce(Return(PrintMeNot()));
2185 helper_.Foo(PrintMeNot()); // This is an expected call.
2186}
2187
2188TEST_F(GMockLogTest, DoesNotPrintWarningInternallyIfVerbosityIsError) {
2189 GMOCK_FLAG(verbose) = kErrorVerbosity;
2190 ON_CALL(helper_, Foo(_))
2191 .WillByDefault(Return(PrintMeNot()));
2192 helper_.Foo(PrintMeNot()); // This should generate a warning.
2193}
2194
2195// Tests Mock::AllowLeak().
2196
zhanyong.wandf35a762009-04-22 22:25:31 +00002197TEST(AllowLeakTest, AllowsLeakingUnusedMockObject) {
2198 MockA* a = new MockA;
2199 Mock::AllowLeak(a);
2200}
2201
2202TEST(AllowLeakTest, CanBeCalledBeforeOnCall) {
2203 MockA* a = new MockA;
2204 Mock::AllowLeak(a);
2205 ON_CALL(*a, DoA(_)).WillByDefault(Return());
2206 a->DoA(0);
2207}
2208
2209TEST(AllowLeakTest, CanBeCalledAfterOnCall) {
2210 MockA* a = new MockA;
2211 ON_CALL(*a, DoA(_)).WillByDefault(Return());
2212 Mock::AllowLeak(a);
2213}
2214
2215TEST(AllowLeakTest, CanBeCalledBeforeExpectCall) {
2216 MockA* a = new MockA;
2217 Mock::AllowLeak(a);
2218 EXPECT_CALL(*a, DoA(_));
2219 a->DoA(0);
2220}
2221
2222TEST(AllowLeakTest, CanBeCalledAfterExpectCall) {
2223 MockA* a = new MockA;
2224 EXPECT_CALL(*a, DoA(_)).Times(AnyNumber());
2225 Mock::AllowLeak(a);
2226}
2227
2228TEST(AllowLeakTest, WorksWhenBothOnCallAndExpectCallArePresent) {
2229 MockA* a = new MockA;
2230 ON_CALL(*a, DoA(_)).WillByDefault(Return());
2231 EXPECT_CALL(*a, DoA(_)).Times(AnyNumber());
2232 Mock::AllowLeak(a);
2233}
shiqiane35fdd92008-12-10 05:08:54 +00002234
2235// Tests that we can verify and clear a mock object's expectations
2236// when none of its methods has expectations.
2237TEST(VerifyAndClearExpectationsTest, NoMethodHasExpectations) {
2238 MockB b;
2239 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
2240
2241 // There should be no expectations on the methods now, so we can
2242 // freely call them.
2243 EXPECT_EQ(0, b.DoB());
2244 EXPECT_EQ(0, b.DoB(1));
2245}
2246
2247// Tests that we can verify and clear a mock object's expectations
2248// when some, but not all, of its methods have expectations *and* the
2249// verification succeeds.
2250TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndSucceed) {
2251 MockB b;
2252 EXPECT_CALL(b, DoB())
2253 .WillOnce(Return(1));
2254 b.DoB();
2255 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
2256
2257 // There should be no expectations on the methods now, so we can
2258 // freely call them.
2259 EXPECT_EQ(0, b.DoB());
2260 EXPECT_EQ(0, b.DoB(1));
2261}
2262
2263// Tests that we can verify and clear a mock object's expectations
2264// when some, but not all, of its methods have expectations *and* the
2265// verification fails.
2266TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndFail) {
2267 MockB b;
2268 EXPECT_CALL(b, DoB())
2269 .WillOnce(Return(1));
vladlosev6c54a5e2009-10-21 06:15:34 +00002270 bool result = true;
shiqiane35fdd92008-12-10 05:08:54 +00002271 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b),
2272 "Actual: never called");
2273 ASSERT_FALSE(result);
2274
2275 // There should be no expectations on the methods now, so we can
2276 // freely call them.
2277 EXPECT_EQ(0, b.DoB());
2278 EXPECT_EQ(0, b.DoB(1));
2279}
2280
2281// Tests that we can verify and clear a mock object's expectations
2282// when all of its methods have expectations.
2283TEST(VerifyAndClearExpectationsTest, AllMethodsHaveExpectations) {
2284 MockB b;
2285 EXPECT_CALL(b, DoB())
2286 .WillOnce(Return(1));
2287 EXPECT_CALL(b, DoB(_))
2288 .WillOnce(Return(2));
2289 b.DoB();
2290 b.DoB(1);
2291 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
2292
2293 // There should be no expectations on the methods now, so we can
2294 // freely call them.
2295 EXPECT_EQ(0, b.DoB());
2296 EXPECT_EQ(0, b.DoB(1));
2297}
2298
2299// Tests that we can verify and clear a mock object's expectations
2300// when a method has more than one expectation.
2301TEST(VerifyAndClearExpectationsTest, AMethodHasManyExpectations) {
2302 MockB b;
2303 EXPECT_CALL(b, DoB(0))
2304 .WillOnce(Return(1));
2305 EXPECT_CALL(b, DoB(_))
2306 .WillOnce(Return(2));
2307 b.DoB(1);
vladlosev6c54a5e2009-10-21 06:15:34 +00002308 bool result = true;
shiqiane35fdd92008-12-10 05:08:54 +00002309 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b),
2310 "Actual: never called");
2311 ASSERT_FALSE(result);
2312
2313 // There should be no expectations on the methods now, so we can
2314 // freely call them.
2315 EXPECT_EQ(0, b.DoB());
2316 EXPECT_EQ(0, b.DoB(1));
2317}
2318
2319// Tests that we can call VerifyAndClearExpectations() on the same
2320// mock object multiple times.
2321TEST(VerifyAndClearExpectationsTest, CanCallManyTimes) {
2322 MockB b;
2323 EXPECT_CALL(b, DoB());
2324 b.DoB();
2325 Mock::VerifyAndClearExpectations(&b);
2326
2327 EXPECT_CALL(b, DoB(_))
2328 .WillOnce(Return(1));
2329 b.DoB(1);
2330 Mock::VerifyAndClearExpectations(&b);
2331 Mock::VerifyAndClearExpectations(&b);
2332
2333 // There should be no expectations on the methods now, so we can
2334 // freely call them.
2335 EXPECT_EQ(0, b.DoB());
2336 EXPECT_EQ(0, b.DoB(1));
2337}
2338
2339// Tests that we can clear a mock object's default actions when none
2340// of its methods has default actions.
2341TEST(VerifyAndClearTest, NoMethodHasDefaultActions) {
2342 MockB b;
2343 // If this crashes or generates a failure, the test will catch it.
2344 Mock::VerifyAndClear(&b);
2345 EXPECT_EQ(0, b.DoB());
2346}
2347
2348// Tests that we can clear a mock object's default actions when some,
2349// but not all of its methods have default actions.
2350TEST(VerifyAndClearTest, SomeMethodsHaveDefaultActions) {
2351 MockB b;
2352 ON_CALL(b, DoB())
2353 .WillByDefault(Return(1));
2354
2355 Mock::VerifyAndClear(&b);
2356
2357 // Verifies that the default action of int DoB() was removed.
2358 EXPECT_EQ(0, b.DoB());
2359}
2360
2361// Tests that we can clear a mock object's default actions when all of
2362// its methods have default actions.
2363TEST(VerifyAndClearTest, AllMethodsHaveDefaultActions) {
2364 MockB b;
2365 ON_CALL(b, DoB())
2366 .WillByDefault(Return(1));
2367 ON_CALL(b, DoB(_))
2368 .WillByDefault(Return(2));
2369
2370 Mock::VerifyAndClear(&b);
2371
2372 // Verifies that the default action of int DoB() was removed.
2373 EXPECT_EQ(0, b.DoB());
2374
2375 // Verifies that the default action of int DoB(int) was removed.
2376 EXPECT_EQ(0, b.DoB(0));
2377}
2378
2379// Tests that we can clear a mock object's default actions when a
2380// method has more than one ON_CALL() set on it.
2381TEST(VerifyAndClearTest, AMethodHasManyDefaultActions) {
2382 MockB b;
2383 ON_CALL(b, DoB(0))
2384 .WillByDefault(Return(1));
2385 ON_CALL(b, DoB(_))
2386 .WillByDefault(Return(2));
2387
2388 Mock::VerifyAndClear(&b);
2389
2390 // Verifies that the default actions (there are two) of int DoB(int)
2391 // were removed.
2392 EXPECT_EQ(0, b.DoB(0));
2393 EXPECT_EQ(0, b.DoB(1));
2394}
2395
2396// Tests that we can call VerifyAndClear() on a mock object multiple
2397// times.
2398TEST(VerifyAndClearTest, CanCallManyTimes) {
2399 MockB b;
2400 ON_CALL(b, DoB())
2401 .WillByDefault(Return(1));
2402 Mock::VerifyAndClear(&b);
2403 Mock::VerifyAndClear(&b);
2404
2405 ON_CALL(b, DoB(_))
2406 .WillByDefault(Return(1));
2407 Mock::VerifyAndClear(&b);
2408
2409 EXPECT_EQ(0, b.DoB());
2410 EXPECT_EQ(0, b.DoB(1));
2411}
2412
2413// Tests that VerifyAndClear() works when the verification succeeds.
2414TEST(VerifyAndClearTest, Success) {
2415 MockB b;
2416 ON_CALL(b, DoB())
2417 .WillByDefault(Return(1));
2418 EXPECT_CALL(b, DoB(1))
2419 .WillOnce(Return(2));
2420
2421 b.DoB();
2422 b.DoB(1);
2423 ASSERT_TRUE(Mock::VerifyAndClear(&b));
2424
2425 // There should be no expectations on the methods now, so we can
2426 // freely call them.
2427 EXPECT_EQ(0, b.DoB());
2428 EXPECT_EQ(0, b.DoB(1));
2429}
2430
2431// Tests that VerifyAndClear() works when the verification fails.
2432TEST(VerifyAndClearTest, Failure) {
2433 MockB b;
2434 ON_CALL(b, DoB(_))
2435 .WillByDefault(Return(1));
2436 EXPECT_CALL(b, DoB())
2437 .WillOnce(Return(2));
2438
2439 b.DoB(1);
vladlosev6c54a5e2009-10-21 06:15:34 +00002440 bool result = true;
shiqiane35fdd92008-12-10 05:08:54 +00002441 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClear(&b),
2442 "Actual: never called");
2443 ASSERT_FALSE(result);
2444
2445 // There should be no expectations on the methods now, so we can
2446 // freely call them.
2447 EXPECT_EQ(0, b.DoB());
2448 EXPECT_EQ(0, b.DoB(1));
2449}
2450
2451// Tests that VerifyAndClear() works when the default actions and
2452// expectations are set on a const mock object.
2453TEST(VerifyAndClearTest, Const) {
2454 MockB b;
2455 ON_CALL(Const(b), DoB())
2456 .WillByDefault(Return(1));
2457
2458 EXPECT_CALL(Const(b), DoB())
2459 .WillOnce(DoDefault())
2460 .WillOnce(Return(2));
2461
2462 b.DoB();
2463 b.DoB();
2464 ASSERT_TRUE(Mock::VerifyAndClear(&b));
2465
2466 // There should be no expectations on the methods now, so we can
2467 // freely call them.
2468 EXPECT_EQ(0, b.DoB());
2469 EXPECT_EQ(0, b.DoB(1));
2470}
2471
2472// Tests that we can set default actions and expectations on a mock
2473// object after VerifyAndClear() has been called on it.
2474TEST(VerifyAndClearTest, CanSetDefaultActionsAndExpectationsAfterwards) {
2475 MockB b;
2476 ON_CALL(b, DoB())
2477 .WillByDefault(Return(1));
2478 EXPECT_CALL(b, DoB(_))
2479 .WillOnce(Return(2));
2480 b.DoB(1);
2481
2482 Mock::VerifyAndClear(&b);
2483
2484 EXPECT_CALL(b, DoB())
2485 .WillOnce(Return(3));
2486 ON_CALL(b, DoB(_))
2487 .WillByDefault(Return(4));
2488
2489 EXPECT_EQ(3, b.DoB());
2490 EXPECT_EQ(4, b.DoB(1));
2491}
2492
2493// Tests that calling VerifyAndClear() on one mock object does not
2494// affect other mock objects (either of the same type or not).
2495TEST(VerifyAndClearTest, DoesNotAffectOtherMockObjects) {
2496 MockA a;
2497 MockB b1;
2498 MockB b2;
2499
2500 ON_CALL(a, Binary(_, _))
2501 .WillByDefault(Return(true));
2502 EXPECT_CALL(a, Binary(_, _))
2503 .WillOnce(DoDefault())
2504 .WillOnce(Return(false));
2505
2506 ON_CALL(b1, DoB())
2507 .WillByDefault(Return(1));
2508 EXPECT_CALL(b1, DoB(_))
2509 .WillOnce(Return(2));
2510
2511 ON_CALL(b2, DoB())
2512 .WillByDefault(Return(3));
2513 EXPECT_CALL(b2, DoB(_));
2514
2515 b2.DoB(0);
2516 Mock::VerifyAndClear(&b2);
2517
2518 // Verifies that the default actions and expectations of a and b1
2519 // are still in effect.
2520 EXPECT_TRUE(a.Binary(0, 0));
2521 EXPECT_FALSE(a.Binary(0, 0));
2522
2523 EXPECT_EQ(1, b1.DoB());
2524 EXPECT_EQ(2, b1.DoB(0));
2525}
2526
vladlosev9bcb5f92011-10-24 23:41:07 +00002527TEST(VerifyAndClearTest,
2528 DestroyingChainedMocksDoesNotDeadlockThroughExpectations) {
2529 linked_ptr<MockA> a(new MockA);
2530 ReferenceHoldingMock test_mock;
2531
2532 // EXPECT_CALL stores a reference to a inside test_mock.
2533 EXPECT_CALL(test_mock, AcceptReference(_))
2534 .WillRepeatedly(SetArgPointee<0>(a));
2535
2536 // Throw away the reference to the mock that we have in a. After this, the
2537 // only reference to it is stored by test_mock.
2538 a.reset();
2539
2540 // When test_mock goes out of scope, it destroys the last remaining reference
2541 // to the mock object originally pointed to by a. This will cause the MockA
2542 // destructor to be called from inside the ReferenceHoldingMock destructor.
2543 // The state of all mocks is protected by a single global lock, but there
2544 // should be no deadlock.
2545}
2546
2547TEST(VerifyAndClearTest,
2548 DestroyingChainedMocksDoesNotDeadlockThroughDefaultAction) {
2549 linked_ptr<MockA> a(new MockA);
2550 ReferenceHoldingMock test_mock;
2551
2552 // ON_CALL stores a reference to a inside test_mock.
2553 ON_CALL(test_mock, AcceptReference(_))
2554 .WillByDefault(SetArgPointee<0>(a));
2555
2556 // Throw away the reference to the mock that we have in a. After this, the
2557 // only reference to it is stored by test_mock.
2558 a.reset();
2559
2560 // When test_mock goes out of scope, it destroys the last remaining reference
2561 // to the mock object originally pointed to by a. This will cause the MockA
2562 // destructor to be called from inside the ReferenceHoldingMock destructor.
2563 // The state of all mocks is protected by a single global lock, but there
2564 // should be no deadlock.
2565}
2566
shiqiane35fdd92008-12-10 05:08:54 +00002567// Tests that a mock function's action can call a mock function
2568// (either the same function or a different one) either as an explicit
2569// action or as a default action without causing a dead lock. It
2570// verifies that the action is not performed inside the critical
2571// section.
vladlosev54af9ba2010-05-04 16:05:11 +00002572TEST(SynchronizationTest, CanCallMockMethodInAction) {
2573 MockA a;
2574 MockC c;
2575 ON_CALL(a, DoA(_))
2576 .WillByDefault(IgnoreResult(InvokeWithoutArgs(&c,
2577 &MockC::NonVoidMethod)));
2578 EXPECT_CALL(a, DoA(1));
2579 EXPECT_CALL(a, DoA(1))
2580 .WillOnce(Invoke(&a, &MockA::DoA))
2581 .RetiresOnSaturation();
2582 EXPECT_CALL(c, NonVoidMethod());
shiqiane35fdd92008-12-10 05:08:54 +00002583
vladlosev54af9ba2010-05-04 16:05:11 +00002584 a.DoA(1);
2585 // This will match the second EXPECT_CALL() and trigger another a.DoA(1),
2586 // which will in turn match the first EXPECT_CALL() and trigger a call to
2587 // c.NonVoidMethod() that was specified by the ON_CALL() since the first
2588 // EXPECT_CALL() did not specify an action.
shiqiane35fdd92008-12-10 05:08:54 +00002589}
2590
2591} // namespace
zhanyong.wandf35a762009-04-22 22:25:31 +00002592
zhanyong.wan9571b282009-08-07 07:15:56 +00002593// Allows the user to define his own main and then invoke gmock_main
2594// from it. This might be necessary on some platforms which require
2595// specific setup and teardown.
2596#if GMOCK_RENAME_MAIN
2597int gmock_main(int argc, char **argv) {
2598#else
zhanyong.wandf35a762009-04-22 22:25:31 +00002599int main(int argc, char **argv) {
zhanyong.wan9571b282009-08-07 07:15:56 +00002600#endif // GMOCK_RENAME_MAIN
zhanyong.wandf35a762009-04-22 22:25:31 +00002601 testing::InitGoogleMock(&argc, argv);
2602
2603 // Ensures that the tests pass no matter what value of
2604 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
2605 testing::GMOCK_FLAG(catch_leaked_mocks) = true;
2606 testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity;
2607
2608 return RUN_ALL_TESTS();
2609}