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