blob: b8f7a11d54f18dbf4739022ca6bce04b0549dd99 [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.wanc8965042013-03-01 07:10:07 +000088using testing::NaggyMock;
zhanyong.wan41b9b0b2009-07-01 19:04:51 +000089using testing::Ne;
shiqiane35fdd92008-12-10 05:08:54 +000090using testing::Return;
91using testing::Sequence;
vladlosev9bcb5f92011-10-24 23:41:07 +000092using testing::SetArgPointee;
zhanyong.wan470df422010-02-02 22:34:58 +000093using testing::internal::ExpectationTester;
vladloseve5121b52011-02-11 23:50:38 +000094using testing::internal::FormatFileLocation;
shiqiane35fdd92008-12-10 05:08:54 +000095using testing::internal::kErrorVerbosity;
96using testing::internal::kInfoVerbosity;
97using testing::internal::kWarningVerbosity;
vladlosev9bcb5f92011-10-24 23:41:07 +000098using testing::internal::linked_ptr;
shiqiane35fdd92008-12-10 05:08:54 +000099using testing::internal::string;
100
zhanyong.wan2516f602010-08-31 18:28:02 +0000101#if GTEST_HAS_STREAM_REDIRECTION
zhanyong.wan470df422010-02-02 22:34:58 +0000102using testing::HasSubstr;
103using testing::internal::CaptureStdout;
104using testing::internal::GetCapturedStdout;
zhanyong.wan2516f602010-08-31 18:28:02 +0000105#endif
zhanyong.wan470df422010-02-02 22:34:58 +0000106
zhanyong.waned6c9272011-02-23 19:39:27 +0000107class Incomplete;
108
109class MockIncomplete {
110 public:
111 // This line verifies that a mock method can take a by-reference
112 // argument of an incomplete type.
113 MOCK_METHOD1(ByRefFunc, void(const Incomplete& x));
114};
115
116// Tells Google Mock how to print a value of type Incomplete.
117void PrintTo(const Incomplete& x, ::std::ostream* os);
118
119TEST(MockMethodTest, CanInstantiateWithIncompleteArgType) {
120 // Even though this mock class contains a mock method that takes
121 // by-reference an argument whose type is incomplete, we can still
122 // use the mock, as long as Google Mock knows how to print the
123 // argument.
124 MockIncomplete incomplete;
125 EXPECT_CALL(incomplete, ByRefFunc(_))
126 .Times(AnyNumber());
127}
128
129// The definition of the printer for the argument type doesn't have to
130// be visible where the mock is used.
131void PrintTo(const Incomplete& /* x */, ::std::ostream* os) {
132 *os << "incomplete";
133}
134
shiqiane35fdd92008-12-10 05:08:54 +0000135class Result {};
136
kosakd478a1f2015-02-14 02:45:40 +0000137// A type that's not default constructible.
138class NonDefaultConstructible {
139 public:
140 explicit NonDefaultConstructible(int /* dummy */) {}
141};
142
shiqiane35fdd92008-12-10 05:08:54 +0000143class MockA {
144 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000145 MockA() {}
146
kosakd478a1f2015-02-14 02:45:40 +0000147 MOCK_METHOD1(DoA, void(int n));
148 MOCK_METHOD1(ReturnResult, Result(int n));
149 MOCK_METHOD0(ReturnNonDefaultConstructible, NonDefaultConstructible());
150 MOCK_METHOD2(Binary, bool(int x, int y));
151 MOCK_METHOD2(ReturnInt, int(int x, int y));
zhanyong.wan32de5f52009-12-23 00:13:23 +0000152
153 private:
154 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockA);
shiqiane35fdd92008-12-10 05:08:54 +0000155};
156
157class MockB {
158 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000159 MockB() {}
160
shiqiane35fdd92008-12-10 05:08:54 +0000161 MOCK_CONST_METHOD0(DoB, int()); // NOLINT
162 MOCK_METHOD1(DoB, int(int n)); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000163
164 private:
165 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB);
shiqiane35fdd92008-12-10 05:08:54 +0000166};
167
vladlosev9bcb5f92011-10-24 23:41:07 +0000168class ReferenceHoldingMock {
169 public:
170 ReferenceHoldingMock() {}
171
172 MOCK_METHOD1(AcceptReference, void(linked_ptr<MockA>*));
173
174 private:
175 GTEST_DISALLOW_COPY_AND_ASSIGN_(ReferenceHoldingMock);
176};
177
shiqiane35fdd92008-12-10 05:08:54 +0000178// Tests that EXPECT_CALL and ON_CALL compile in a presence of macro
179// redefining a mock method name. This could happen, for example, when
180// the tested code #includes Win32 API headers which define many APIs
181// as macros, e.g. #define TextOut TextOutW.
182
183#define Method MethodW
184
185class CC {
186 public:
187 virtual ~CC() {}
188 virtual int Method() = 0;
189};
190class MockCC : public CC {
191 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000192 MockCC() {}
193
shiqiane35fdd92008-12-10 05:08:54 +0000194 MOCK_METHOD0(Method, int());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000195
196 private:
197 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockCC);
shiqiane35fdd92008-12-10 05:08:54 +0000198};
199
200// Tests that a method with expanded name compiles.
201TEST(OnCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) {
202 MockCC cc;
203 ON_CALL(cc, Method());
204}
205
206// Tests that the method with expanded name not only compiles but runs
207// and returns a correct value, too.
208TEST(OnCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) {
209 MockCC cc;
210 ON_CALL(cc, Method()).WillByDefault(Return(42));
211 EXPECT_EQ(42, cc.Method());
212}
213
214// Tests that a method with expanded name compiles.
215TEST(ExpectCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) {
216 MockCC cc;
217 EXPECT_CALL(cc, Method());
218 cc.Method();
219}
220
221// Tests that it works, too.
222TEST(ExpectCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) {
223 MockCC cc;
224 EXPECT_CALL(cc, Method()).WillOnce(Return(42));
225 EXPECT_EQ(42, cc.Method());
226}
227
228#undef Method // Done with macro redefinition tests.
229
230// Tests that ON_CALL evaluates its arguments exactly once as promised
231// by Google Mock.
232TEST(OnCallSyntaxTest, EvaluatesFirstArgumentOnce) {
233 MockA a;
234 MockA* pa = &a;
235
236 ON_CALL(*pa++, DoA(_));
237 EXPECT_EQ(&a + 1, pa);
238}
239
240TEST(OnCallSyntaxTest, EvaluatesSecondArgumentOnce) {
241 MockA a;
242 int n = 0;
243
244 ON_CALL(a, DoA(n++));
245 EXPECT_EQ(1, n);
246}
247
248// Tests that the syntax of ON_CALL() is enforced at run time.
249
zhanyong.wanbf550852009-06-09 06:09:53 +0000250TEST(OnCallSyntaxTest, WithIsOptional) {
shiqiane35fdd92008-12-10 05:08:54 +0000251 MockA a;
252
253 ON_CALL(a, DoA(5))
254 .WillByDefault(Return());
255 ON_CALL(a, DoA(_))
zhanyong.wanbf550852009-06-09 06:09:53 +0000256 .With(_)
shiqiane35fdd92008-12-10 05:08:54 +0000257 .WillByDefault(Return());
258}
259
zhanyong.wanbf550852009-06-09 06:09:53 +0000260TEST(OnCallSyntaxTest, WithCanAppearAtMostOnce) {
shiqiane35fdd92008-12-10 05:08:54 +0000261 MockA a;
262
263 EXPECT_NONFATAL_FAILURE({ // NOLINT
264 ON_CALL(a, ReturnResult(_))
zhanyong.wanbf550852009-06-09 06:09:53 +0000265 .With(_)
266 .With(_)
shiqiane35fdd92008-12-10 05:08:54 +0000267 .WillByDefault(Return(Result()));
zhanyong.wanbf550852009-06-09 06:09:53 +0000268 }, ".With() cannot appear more than once in an ON_CALL()");
269}
270
shiqiane35fdd92008-12-10 05:08:54 +0000271TEST(OnCallSyntaxTest, WillByDefaultIsMandatory) {
272 MockA a;
273
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000274 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000275 ON_CALL(a, DoA(5));
276 a.DoA(5);
277 }, "");
278}
279
shiqiane35fdd92008-12-10 05:08:54 +0000280TEST(OnCallSyntaxTest, WillByDefaultCanAppearAtMostOnce) {
281 MockA a;
282
283 EXPECT_NONFATAL_FAILURE({ // NOLINT
284 ON_CALL(a, DoA(5))
285 .WillByDefault(Return())
286 .WillByDefault(Return());
287 }, ".WillByDefault() must appear exactly once in an ON_CALL()");
288}
289
290// Tests that EXPECT_CALL evaluates its arguments exactly once as
291// promised by Google Mock.
292TEST(ExpectCallSyntaxTest, EvaluatesFirstArgumentOnce) {
293 MockA a;
294 MockA* pa = &a;
295
296 EXPECT_CALL(*pa++, DoA(_));
297 a.DoA(0);
298 EXPECT_EQ(&a + 1, pa);
299}
300
301TEST(ExpectCallSyntaxTest, EvaluatesSecondArgumentOnce) {
302 MockA a;
303 int n = 0;
304
305 EXPECT_CALL(a, DoA(n++));
306 a.DoA(0);
307 EXPECT_EQ(1, n);
308}
309
310// Tests that the syntax of EXPECT_CALL() is enforced at run time.
311
zhanyong.wanbf550852009-06-09 06:09:53 +0000312TEST(ExpectCallSyntaxTest, WithIsOptional) {
shiqiane35fdd92008-12-10 05:08:54 +0000313 MockA a;
314
315 EXPECT_CALL(a, DoA(5))
316 .Times(0);
317 EXPECT_CALL(a, DoA(6))
zhanyong.wanbf550852009-06-09 06:09:53 +0000318 .With(_)
shiqiane35fdd92008-12-10 05:08:54 +0000319 .Times(0);
320}
321
zhanyong.wanbf550852009-06-09 06:09:53 +0000322TEST(ExpectCallSyntaxTest, WithCanAppearAtMostOnce) {
shiqiane35fdd92008-12-10 05:08:54 +0000323 MockA a;
324
325 EXPECT_NONFATAL_FAILURE({ // NOLINT
326 EXPECT_CALL(a, DoA(6))
zhanyong.wanbf550852009-06-09 06:09:53 +0000327 .With(_)
328 .With(_);
329 }, ".With() cannot appear more than once in an EXPECT_CALL()");
shiqiane35fdd92008-12-10 05:08:54 +0000330
331 a.DoA(6);
332}
333
zhanyong.wanbf550852009-06-09 06:09:53 +0000334TEST(ExpectCallSyntaxTest, WithMustBeFirstClause) {
shiqiane35fdd92008-12-10 05:08:54 +0000335 MockA a;
336
337 EXPECT_NONFATAL_FAILURE({ // NOLINT
338 EXPECT_CALL(a, DoA(1))
339 .Times(1)
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(1);
344
345 EXPECT_NONFATAL_FAILURE({ // NOLINT
346 EXPECT_CALL(a, DoA(2))
347 .WillOnce(Return())
zhanyong.wanbf550852009-06-09 06:09:53 +0000348 .With(_);
349 }, ".With() must be the first clause in an EXPECT_CALL()");
shiqiane35fdd92008-12-10 05:08:54 +0000350
351 a.DoA(2);
352}
353
354TEST(ExpectCallSyntaxTest, TimesCanBeInferred) {
355 MockA a;
356
357 EXPECT_CALL(a, DoA(1))
358 .WillOnce(Return());
359
360 EXPECT_CALL(a, DoA(2))
361 .WillOnce(Return())
362 .WillRepeatedly(Return());
363
364 a.DoA(1);
365 a.DoA(2);
366 a.DoA(2);
367}
368
369TEST(ExpectCallSyntaxTest, TimesCanAppearAtMostOnce) {
370 MockA a;
371
372 EXPECT_NONFATAL_FAILURE({ // NOLINT
373 EXPECT_CALL(a, DoA(1))
374 .Times(1)
375 .Times(2);
376 }, ".Times() cannot appear more than once in an EXPECT_CALL()");
377
378 a.DoA(1);
379 a.DoA(1);
380}
381
382TEST(ExpectCallSyntaxTest, TimesMustBeBeforeInSequence) {
383 MockA a;
384 Sequence s;
385
386 EXPECT_NONFATAL_FAILURE({ // NOLINT
387 EXPECT_CALL(a, DoA(1))
388 .InSequence(s)
389 .Times(1);
390 }, ".Times() cannot appear after ");
391
392 a.DoA(1);
393}
394
395TEST(ExpectCallSyntaxTest, InSequenceIsOptional) {
396 MockA a;
397 Sequence s;
398
399 EXPECT_CALL(a, DoA(1));
400 EXPECT_CALL(a, DoA(2))
401 .InSequence(s);
402
403 a.DoA(1);
404 a.DoA(2);
405}
406
407TEST(ExpectCallSyntaxTest, InSequenceCanAppearMultipleTimes) {
408 MockA a;
409 Sequence s1, s2;
410
411 EXPECT_CALL(a, DoA(1))
412 .InSequence(s1, s2)
413 .InSequence(s1);
414
415 a.DoA(1);
416}
417
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000418TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeAfter) {
419 MockA a;
420 Sequence s;
421
422 Expectation e = EXPECT_CALL(a, DoA(1))
423 .Times(AnyNumber());
424 EXPECT_NONFATAL_FAILURE({ // NOLINT
425 EXPECT_CALL(a, DoA(2))
426 .After(e)
427 .InSequence(s);
428 }, ".InSequence() cannot appear after ");
429
430 a.DoA(2);
431}
432
433TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWillOnce) {
shiqiane35fdd92008-12-10 05:08:54 +0000434 MockA a;
435 Sequence s;
436
437 EXPECT_NONFATAL_FAILURE({ // NOLINT
438 EXPECT_CALL(a, DoA(1))
439 .WillOnce(Return())
440 .InSequence(s);
441 }, ".InSequence() cannot appear after ");
442
443 a.DoA(1);
444}
445
zhanyong.wan41b9b0b2009-07-01 19:04:51 +0000446TEST(ExpectCallSyntaxTest, AfterMustBeBeforeWillOnce) {
447 MockA a;
448
449 Expectation e = EXPECT_CALL(a, DoA(1));
450 EXPECT_NONFATAL_FAILURE({
451 EXPECT_CALL(a, DoA(2))
452 .WillOnce(Return())
453 .After(e);
454 }, ".After() cannot appear after ");
455
456 a.DoA(1);
457 a.DoA(2);
458}
459
shiqiane35fdd92008-12-10 05:08:54 +0000460TEST(ExpectCallSyntaxTest, WillIsOptional) {
461 MockA a;
462
463 EXPECT_CALL(a, DoA(1));
464 EXPECT_CALL(a, DoA(2))
465 .WillOnce(Return());
466
467 a.DoA(1);
468 a.DoA(2);
469}
470
471TEST(ExpectCallSyntaxTest, WillCanAppearMultipleTimes) {
472 MockA a;
473
474 EXPECT_CALL(a, DoA(1))
475 .Times(AnyNumber())
476 .WillOnce(Return())
477 .WillOnce(Return())
478 .WillOnce(Return());
479}
480
481TEST(ExpectCallSyntaxTest, WillMustBeBeforeWillRepeatedly) {
482 MockA a;
483
484 EXPECT_NONFATAL_FAILURE({ // NOLINT
485 EXPECT_CALL(a, DoA(1))
486 .WillRepeatedly(Return())
487 .WillOnce(Return());
488 }, ".WillOnce() cannot appear after ");
489
490 a.DoA(1);
491}
492
493TEST(ExpectCallSyntaxTest, WillRepeatedlyIsOptional) {
494 MockA a;
495
496 EXPECT_CALL(a, DoA(1))
497 .WillOnce(Return());
498 EXPECT_CALL(a, DoA(2))
499 .WillOnce(Return())
500 .WillRepeatedly(Return());
501
502 a.DoA(1);
503 a.DoA(2);
504 a.DoA(2);
505}
506
507TEST(ExpectCallSyntaxTest, WillRepeatedlyCannotAppearMultipleTimes) {
508 MockA a;
509
510 EXPECT_NONFATAL_FAILURE({ // NOLINT
511 EXPECT_CALL(a, DoA(1))
512 .WillRepeatedly(Return())
513 .WillRepeatedly(Return());
514 }, ".WillRepeatedly() cannot appear more than once in an "
515 "EXPECT_CALL()");
516}
517
518TEST(ExpectCallSyntaxTest, WillRepeatedlyMustBeBeforeRetiresOnSaturation) {
519 MockA a;
520
521 EXPECT_NONFATAL_FAILURE({ // NOLINT
522 EXPECT_CALL(a, DoA(1))
523 .RetiresOnSaturation()
524 .WillRepeatedly(Return());
525 }, ".WillRepeatedly() cannot appear after ");
526}
527
528TEST(ExpectCallSyntaxTest, RetiresOnSaturationIsOptional) {
529 MockA a;
530
531 EXPECT_CALL(a, DoA(1));
532 EXPECT_CALL(a, DoA(1))
533 .RetiresOnSaturation();
534
535 a.DoA(1);
536 a.DoA(1);
537}
538
539TEST(ExpectCallSyntaxTest, RetiresOnSaturationCannotAppearMultipleTimes) {
540 MockA a;
541
542 EXPECT_NONFATAL_FAILURE({ // NOLINT
543 EXPECT_CALL(a, DoA(1))
544 .RetiresOnSaturation()
545 .RetiresOnSaturation();
546 }, ".RetiresOnSaturation() cannot appear more than once");
547
548 a.DoA(1);
549}
550
551TEST(ExpectCallSyntaxTest, DefaultCardinalityIsOnce) {
552 {
553 MockA a;
554 EXPECT_CALL(a, DoA(1));
555 a.DoA(1);
556 }
557 EXPECT_NONFATAL_FAILURE({ // NOLINT
558 MockA a;
559 EXPECT_CALL(a, DoA(1));
560 }, "to be called once");
561 EXPECT_NONFATAL_FAILURE({ // NOLINT
562 MockA a;
563 EXPECT_CALL(a, DoA(1));
564 a.DoA(1);
565 a.DoA(1);
566 }, "to be called once");
567}
568
zhanyong.wan2516f602010-08-31 18:28:02 +0000569#if GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000570
571// Tests that Google Mock doesn't print a warning when the number of
572// WillOnce() is adequate.
573TEST(ExpectCallSyntaxTest, DoesNotWarnOnAdequateActionCount) {
zhanyong.wan470df422010-02-02 22:34:58 +0000574 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000575 {
576 MockB b;
577
578 // It's always fine to omit WillOnce() entirely.
579 EXPECT_CALL(b, DoB())
580 .Times(0);
581 EXPECT_CALL(b, DoB(1))
582 .Times(AtMost(1));
583 EXPECT_CALL(b, DoB(2))
584 .Times(1)
585 .WillRepeatedly(Return(1));
586
587 // It's fine for the number of WillOnce()s to equal the upper bound.
588 EXPECT_CALL(b, DoB(3))
589 .Times(Between(1, 2))
590 .WillOnce(Return(1))
591 .WillOnce(Return(2));
592
593 // It's fine for the number of WillOnce()s to be smaller than the
594 // upper bound when there is a WillRepeatedly().
595 EXPECT_CALL(b, DoB(4))
596 .Times(AtMost(3))
597 .WillOnce(Return(1))
598 .WillRepeatedly(Return(2));
599
600 // Satisfies the above expectations.
601 b.DoB(2);
602 b.DoB(3);
603 }
zhanyong.wan470df422010-02-02 22:34:58 +0000604 EXPECT_STREQ("", GetCapturedStdout().c_str());
shiqiane35fdd92008-12-10 05:08:54 +0000605}
606
607// Tests that Google Mock warns on having too many actions in an
608// expectation compared to its cardinality.
609TEST(ExpectCallSyntaxTest, WarnsOnTooManyActions) {
zhanyong.wan470df422010-02-02 22:34:58 +0000610 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000611 {
612 MockB b;
613
614 // Warns when the number of WillOnce()s is larger than the upper bound.
615 EXPECT_CALL(b, DoB())
616 .Times(0)
617 .WillOnce(Return(1)); // #1
618 EXPECT_CALL(b, DoB())
619 .Times(AtMost(1))
620 .WillOnce(Return(1))
621 .WillOnce(Return(2)); // #2
622 EXPECT_CALL(b, DoB(1))
623 .Times(1)
624 .WillOnce(Return(1))
625 .WillOnce(Return(2))
626 .RetiresOnSaturation(); // #3
627
628 // Warns when the number of WillOnce()s equals the upper bound and
629 // there is a WillRepeatedly().
630 EXPECT_CALL(b, DoB())
631 .Times(0)
632 .WillRepeatedly(Return(1)); // #4
633 EXPECT_CALL(b, DoB(2))
634 .Times(1)
635 .WillOnce(Return(1))
636 .WillRepeatedly(Return(2)); // #5
637
638 // Satisfies the above expectations.
639 b.DoB(1);
640 b.DoB(2);
641 }
jgm38513a82012-11-15 15:50:36 +0000642 const std::string output = GetCapturedStdout();
vladlosev6c54a5e2009-10-21 06:15:34 +0000643 EXPECT_PRED_FORMAT2(
644 IsSubstring,
645 "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
646 "Expected to be never called, but has 1 WillOnce().",
647 output); // #1
648 EXPECT_PRED_FORMAT2(
649 IsSubstring,
650 "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
651 "Expected to be called at most once, "
652 "but has 2 WillOnce()s.",
653 output); // #2
654 EXPECT_PRED_FORMAT2(
655 IsSubstring,
656 "Too many actions specified in EXPECT_CALL(b, DoB(1))...\n"
657 "Expected to be called once, but has 2 WillOnce()s.",
658 output); // #3
659 EXPECT_PRED_FORMAT2(
660 IsSubstring,
661 "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
662 "Expected to be never called, but has 0 WillOnce()s "
663 "and a WillRepeatedly().",
664 output); // #4
665 EXPECT_PRED_FORMAT2(
666 IsSubstring,
667 "Too many actions specified in EXPECT_CALL(b, DoB(2))...\n"
668 "Expected to be called once, but has 1 WillOnce() "
669 "and a WillRepeatedly().",
670 output); // #5
shiqiane35fdd92008-12-10 05:08:54 +0000671}
672
673// Tests that Google Mock warns on having too few actions in an
674// expectation compared to its cardinality.
675TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) {
676 MockB b;
677
678 EXPECT_CALL(b, DoB())
679 .Times(Between(2, 3))
680 .WillOnce(Return(1));
681
zhanyong.wan470df422010-02-02 22:34:58 +0000682 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000683 b.DoB();
jgm38513a82012-11-15 15:50:36 +0000684 const std::string output = GetCapturedStdout();
vladlosev6c54a5e2009-10-21 06:15:34 +0000685 EXPECT_PRED_FORMAT2(
686 IsSubstring,
687 "Too few actions specified in EXPECT_CALL(b, DoB())...\n"
688 "Expected to be called between 2 and 3 times, "
689 "but has only 1 WillOnce().",
690 output);
shiqiane35fdd92008-12-10 05:08:54 +0000691 b.DoB();
692}
693
zhanyong.wan2516f602010-08-31 18:28:02 +0000694#endif // GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000695
696// Tests the semantics of ON_CALL().
697
698// Tests that the built-in default action is taken when no ON_CALL()
699// is specified.
700TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCall) {
701 MockB b;
702 EXPECT_CALL(b, DoB());
703
704 EXPECT_EQ(0, b.DoB());
705}
706
707// Tests that the built-in default action is taken when no ON_CALL()
708// matches the invocation.
709TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCallMatches) {
710 MockB b;
711 ON_CALL(b, DoB(1))
712 .WillByDefault(Return(1));
713 EXPECT_CALL(b, DoB(_));
714
715 EXPECT_EQ(0, b.DoB(2));
716}
717
718// Tests that the last matching ON_CALL() action is taken.
719TEST(OnCallTest, PicksLastMatchingOnCall) {
720 MockB b;
721 ON_CALL(b, DoB(_))
722 .WillByDefault(Return(3));
723 ON_CALL(b, DoB(2))
724 .WillByDefault(Return(2));
725 ON_CALL(b, DoB(1))
726 .WillByDefault(Return(1));
727 EXPECT_CALL(b, DoB(_));
728
729 EXPECT_EQ(2, b.DoB(2));
730}
731
732// Tests the semantics of EXPECT_CALL().
733
734// Tests that any call is allowed when no EXPECT_CALL() is specified.
735TEST(ExpectCallTest, AllowsAnyCallWhenNoSpec) {
736 MockB b;
737 EXPECT_CALL(b, DoB());
738 // There is no expectation on DoB(int).
739
740 b.DoB();
741
742 // DoB(int) can be called any number of times.
743 b.DoB(1);
744 b.DoB(2);
745}
746
747// Tests that the last matching EXPECT_CALL() fires.
748TEST(ExpectCallTest, PicksLastMatchingExpectCall) {
749 MockB b;
750 EXPECT_CALL(b, DoB(_))
751 .WillRepeatedly(Return(2));
752 EXPECT_CALL(b, DoB(1))
753 .WillRepeatedly(Return(1));
754
755 EXPECT_EQ(1, b.DoB(1));
756}
757
758// Tests lower-bound violation.
759TEST(ExpectCallTest, CatchesTooFewCalls) {
760 EXPECT_NONFATAL_FAILURE({ // NOLINT
761 MockB b;
762 EXPECT_CALL(b, DoB(5))
763 .Times(AtLeast(2));
764
765 b.DoB(5);
vladlosev6c54a5e2009-10-21 06:15:34 +0000766 }, "Actual function call count doesn't match EXPECT_CALL(b, DoB(5))...\n"
shiqiane35fdd92008-12-10 05:08:54 +0000767 " Expected: to be called at least twice\n"
768 " Actual: called once - unsatisfied and active");
769}
770
771// Tests that the cardinality can be inferred when no Times(...) is
772// specified.
773TEST(ExpectCallTest, InfersCardinalityWhenThereIsNoWillRepeatedly) {
774 {
775 MockB b;
776 EXPECT_CALL(b, DoB())
777 .WillOnce(Return(1))
778 .WillOnce(Return(2));
779
780 EXPECT_EQ(1, b.DoB());
781 EXPECT_EQ(2, b.DoB());
782 }
783
784 EXPECT_NONFATAL_FAILURE({ // NOLINT
785 MockB b;
786 EXPECT_CALL(b, DoB())
787 .WillOnce(Return(1))
788 .WillOnce(Return(2));
789
790 EXPECT_EQ(1, b.DoB());
791 }, "to be called twice");
792
793 { // NOLINT
794 MockB b;
795 EXPECT_CALL(b, DoB())
796 .WillOnce(Return(1))
797 .WillOnce(Return(2));
798
799 EXPECT_EQ(1, b.DoB());
800 EXPECT_EQ(2, b.DoB());
801 EXPECT_NONFATAL_FAILURE(b.DoB(), "to be called twice");
802 }
803}
804
805TEST(ExpectCallTest, InfersCardinality1WhenThereIsWillRepeatedly) {
806 {
807 MockB b;
808 EXPECT_CALL(b, DoB())
809 .WillOnce(Return(1))
810 .WillRepeatedly(Return(2));
811
812 EXPECT_EQ(1, b.DoB());
813 }
814
815 { // NOLINT
816 MockB b;
817 EXPECT_CALL(b, DoB())
818 .WillOnce(Return(1))
819 .WillRepeatedly(Return(2));
820
821 EXPECT_EQ(1, b.DoB());
822 EXPECT_EQ(2, b.DoB());
823 EXPECT_EQ(2, b.DoB());
824 }
825
826 EXPECT_NONFATAL_FAILURE({ // NOLINT
827 MockB b;
828 EXPECT_CALL(b, DoB())
829 .WillOnce(Return(1))
830 .WillRepeatedly(Return(2));
831 }, "to be called at least once");
832}
833
834// Tests that the n-th action is taken for the n-th matching
835// invocation.
836TEST(ExpectCallTest, NthMatchTakesNthAction) {
837 MockB b;
838 EXPECT_CALL(b, DoB())
839 .WillOnce(Return(1))
840 .WillOnce(Return(2))
841 .WillOnce(Return(3));
842
843 EXPECT_EQ(1, b.DoB());
844 EXPECT_EQ(2, b.DoB());
845 EXPECT_EQ(3, b.DoB());
846}
847
vladloseve5121b52011-02-11 23:50:38 +0000848// Tests that the WillRepeatedly() action is taken when the WillOnce(...)
849// list is exhausted.
850TEST(ExpectCallTest, TakesRepeatedActionWhenWillListIsExhausted) {
851 MockB b;
852 EXPECT_CALL(b, DoB())
853 .WillOnce(Return(1))
854 .WillRepeatedly(Return(2));
855
856 EXPECT_EQ(1, b.DoB());
857 EXPECT_EQ(2, b.DoB());
858 EXPECT_EQ(2, b.DoB());
859}
860
zhanyong.wan2516f602010-08-31 18:28:02 +0000861#if GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000862
863// Tests that the default action is taken when the WillOnce(...) list is
864// exhausted and there is no WillRepeatedly().
865TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) {
866 MockB b;
867 EXPECT_CALL(b, DoB(_))
868 .Times(1);
869 EXPECT_CALL(b, DoB())
870 .Times(AnyNumber())
871 .WillOnce(Return(1))
872 .WillOnce(Return(2));
873
zhanyong.wan470df422010-02-02 22:34:58 +0000874 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000875 EXPECT_EQ(0, b.DoB(1)); // Shouldn't generate a warning as the
876 // expectation has no action clause at all.
877 EXPECT_EQ(1, b.DoB());
878 EXPECT_EQ(2, b.DoB());
jgm38513a82012-11-15 15:50:36 +0000879 const std::string output1 = GetCapturedStdout();
zhanyong.wan470df422010-02-02 22:34:58 +0000880 EXPECT_STREQ("", output1.c_str());
shiqiane35fdd92008-12-10 05:08:54 +0000881
zhanyong.wan470df422010-02-02 22:34:58 +0000882 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000883 EXPECT_EQ(0, b.DoB());
884 EXPECT_EQ(0, b.DoB());
jgm38513a82012-11-15 15:50:36 +0000885 const std::string output2 = GetCapturedStdout();
zhanyong.wan470df422010-02-02 22:34:58 +0000886 EXPECT_THAT(output2.c_str(),
887 HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n"
888 "Called 3 times, but only 2 WillOnce()s are specified"
889 " - returning default value."));
890 EXPECT_THAT(output2.c_str(),
891 HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n"
892 "Called 4 times, but only 2 WillOnce()s are specified"
893 " - returning default value."));
shiqiane35fdd92008-12-10 05:08:54 +0000894}
895
zhanyong.wan6e0fba42013-09-16 05:50:53 +0000896TEST(FunctionMockerMessageTest, ReportsExpectCallLocationForExhausedActions) {
shiqiane35fdd92008-12-10 05:08:54 +0000897 MockB b;
vladloseve5121b52011-02-11 23:50:38 +0000898 std::string expect_call_location = FormatFileLocation(__FILE__, __LINE__ + 1);
899 EXPECT_CALL(b, DoB()).Times(AnyNumber()).WillOnce(Return(1));
shiqiane35fdd92008-12-10 05:08:54 +0000900
901 EXPECT_EQ(1, b.DoB());
vladloseve5121b52011-02-11 23:50:38 +0000902
903 CaptureStdout();
904 EXPECT_EQ(0, b.DoB());
jgm38513a82012-11-15 15:50:36 +0000905 const std::string output = GetCapturedStdout();
vladloseve5121b52011-02-11 23:50:38 +0000906 // The warning message should contain the call location.
907 EXPECT_PRED_FORMAT2(IsSubstring, expect_call_location, output);
shiqiane35fdd92008-12-10 05:08:54 +0000908}
909
zhanyong.wan6e0fba42013-09-16 05:50:53 +0000910TEST(FunctionMockerMessageTest,
zhanyong.wanc8965042013-03-01 07:10:07 +0000911 ReportsDefaultActionLocationOfUninterestingCallsForNaggyMock) {
vladloseve5121b52011-02-11 23:50:38 +0000912 std::string on_call_location;
913 CaptureStdout();
914 {
zhanyong.wanc8965042013-03-01 07:10:07 +0000915 NaggyMock<MockB> b;
vladloseve5121b52011-02-11 23:50:38 +0000916 on_call_location = FormatFileLocation(__FILE__, __LINE__ + 1);
917 ON_CALL(b, DoB(_)).WillByDefault(Return(0));
918 b.DoB(0);
919 }
920 EXPECT_PRED_FORMAT2(IsSubstring, on_call_location, GetCapturedStdout());
921}
922
923#endif // GTEST_HAS_STREAM_REDIRECTION
924
shiqiane35fdd92008-12-10 05:08:54 +0000925// Tests that an uninteresting call performs the default action.
926TEST(UninterestingCallTest, DoesDefaultAction) {
927 // When there is an ON_CALL() statement, the action specified by it
928 // should be taken.
929 MockA a;
930 ON_CALL(a, Binary(_, _))
931 .WillByDefault(Return(true));
932 EXPECT_TRUE(a.Binary(1, 2));
933
934 // When there is no ON_CALL(), the default value for the return type
935 // should be returned.
936 MockB b;
937 EXPECT_EQ(0, b.DoB());
938}
939
940// Tests that an unexpected call performs the default action.
941TEST(UnexpectedCallTest, DoesDefaultAction) {
942 // When there is an ON_CALL() statement, the action specified by it
943 // should be taken.
944 MockA a;
945 ON_CALL(a, Binary(_, _))
946 .WillByDefault(Return(true));
947 EXPECT_CALL(a, Binary(0, 0));
948 a.Binary(0, 0);
949 bool result = false;
950 EXPECT_NONFATAL_FAILURE(result = a.Binary(1, 2),
951 "Unexpected mock function call");
952 EXPECT_TRUE(result);
953
954 // When there is no ON_CALL(), the default value for the return type
955 // should be returned.
956 MockB b;
957 EXPECT_CALL(b, DoB(0))
958 .Times(0);
959 int n = -1;
960 EXPECT_NONFATAL_FAILURE(n = b.DoB(1),
961 "Unexpected mock function call");
962 EXPECT_EQ(0, n);
963}
964
965// Tests that when an unexpected void function generates the right
966// failure message.
967TEST(UnexpectedCallTest, GeneratesFailureForVoidFunction) {
968 // First, tests the message when there is only one EXPECT_CALL().
969 MockA a1;
970 EXPECT_CALL(a1, DoA(1));
971 a1.DoA(1);
972 // Ideally we should match the failure message against a regex, but
973 // EXPECT_NONFATAL_FAILURE doesn't support that, so we test for
974 // multiple sub-strings instead.
975 EXPECT_NONFATAL_FAILURE(
976 a1.DoA(9),
977 "Unexpected mock function call - returning directly.\n"
978 " Function call: DoA(9)\n"
979 "Google Mock tried the following 1 expectation, but it didn't match:");
980 EXPECT_NONFATAL_FAILURE(
981 a1.DoA(9),
982 " Expected arg #0: is equal to 1\n"
983 " Actual: 9\n"
984 " Expected: to be called once\n"
985 " Actual: called once - saturated and active");
986
987 // Next, tests the message when there are more than one EXPECT_CALL().
988 MockA a2;
989 EXPECT_CALL(a2, DoA(1));
990 EXPECT_CALL(a2, DoA(3));
991 a2.DoA(1);
992 EXPECT_NONFATAL_FAILURE(
993 a2.DoA(2),
994 "Unexpected mock function call - returning directly.\n"
995 " Function call: DoA(2)\n"
996 "Google Mock tried the following 2 expectations, but none matched:");
997 EXPECT_NONFATAL_FAILURE(
998 a2.DoA(2),
vladlosev6c54a5e2009-10-21 06:15:34 +0000999 "tried expectation #0: EXPECT_CALL(a2, DoA(1))...\n"
shiqiane35fdd92008-12-10 05:08:54 +00001000 " Expected arg #0: is equal to 1\n"
1001 " Actual: 2\n"
1002 " Expected: to be called once\n"
1003 " Actual: called once - saturated and active");
1004 EXPECT_NONFATAL_FAILURE(
1005 a2.DoA(2),
vladlosev6c54a5e2009-10-21 06:15:34 +00001006 "tried expectation #1: EXPECT_CALL(a2, DoA(3))...\n"
shiqiane35fdd92008-12-10 05:08:54 +00001007 " Expected arg #0: is equal to 3\n"
1008 " Actual: 2\n"
1009 " Expected: to be called once\n"
1010 " Actual: never called - unsatisfied and active");
1011 a2.DoA(3);
1012}
1013
1014// Tests that an unexpected non-void function generates the right
1015// failure message.
1016TEST(UnexpectedCallTest, GeneartesFailureForNonVoidFunction) {
1017 MockB b1;
1018 EXPECT_CALL(b1, DoB(1));
1019 b1.DoB(1);
1020 EXPECT_NONFATAL_FAILURE(
1021 b1.DoB(2),
1022 "Unexpected mock function call - returning default value.\n"
1023 " Function call: DoB(2)\n"
1024 " Returns: 0\n"
1025 "Google Mock tried the following 1 expectation, but it didn't match:");
1026 EXPECT_NONFATAL_FAILURE(
1027 b1.DoB(2),
1028 " Expected arg #0: is equal to 1\n"
1029 " Actual: 2\n"
1030 " Expected: to be called once\n"
1031 " Actual: called once - saturated and active");
1032}
1033
1034// Tests that Google Mock explains that an retired expectation doesn't
1035// match the call.
1036TEST(UnexpectedCallTest, RetiredExpectation) {
1037 MockB b;
1038 EXPECT_CALL(b, DoB(1))
1039 .RetiresOnSaturation();
1040
1041 b.DoB(1);
1042 EXPECT_NONFATAL_FAILURE(
1043 b.DoB(1),
1044 " Expected: the expectation is active\n"
1045 " Actual: it is retired");
1046}
1047
1048// Tests that Google Mock explains that an expectation that doesn't
1049// match the arguments doesn't match the call.
1050TEST(UnexpectedCallTest, UnmatchedArguments) {
1051 MockB b;
1052 EXPECT_CALL(b, DoB(1));
1053
1054 EXPECT_NONFATAL_FAILURE(
1055 b.DoB(2),
1056 " Expected arg #0: is equal to 1\n"
1057 " Actual: 2\n");
1058 b.DoB(1);
1059}
1060
shiqiane35fdd92008-12-10 05:08:54 +00001061// Tests that Google Mock explains that an expectation with
1062// unsatisfied pre-requisites doesn't match the call.
1063TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) {
1064 Sequence s1, s2;
1065 MockB b;
1066 EXPECT_CALL(b, DoB(1))
1067 .InSequence(s1);
1068 EXPECT_CALL(b, DoB(2))
1069 .Times(AnyNumber())
1070 .InSequence(s1);
1071 EXPECT_CALL(b, DoB(3))
1072 .InSequence(s2);
1073 EXPECT_CALL(b, DoB(4))
1074 .InSequence(s1, s2);
1075
1076 ::testing::TestPartResultArray failures;
1077 {
1078 ::testing::ScopedFakeTestPartResultReporter reporter(&failures);
1079 b.DoB(4);
1080 // Now 'failures' contains the Google Test failures generated by
1081 // the above statement.
1082 }
1083
1084 // There should be one non-fatal failure.
1085 ASSERT_EQ(1, failures.size());
1086 const ::testing::TestPartResult& r = failures.GetTestPartResult(0);
zhanyong.wanbbd6e102009-09-18 18:17:19 +00001087 EXPECT_EQ(::testing::TestPartResult::kNonFatalFailure, r.type());
shiqiane35fdd92008-12-10 05:08:54 +00001088
1089 // Verifies that the failure message contains the two unsatisfied
1090 // pre-requisites but not the satisfied one.
zhanyong.wand14aaed2010-01-14 05:36:32 +00001091#if GTEST_USES_PCRE
1092 EXPECT_THAT(r.message(), ContainsRegex(
shiqiane35fdd92008-12-10 05:08:54 +00001093 // PCRE has trouble using (.|\n) to match any character, but
1094 // supports the (?s) prefix for using . to match any character.
1095 "(?s)the following immediate pre-requisites are not satisfied:\n"
1096 ".*: pre-requisite #0\n"
zhanyong.wand14aaed2010-01-14 05:36:32 +00001097 ".*: pre-requisite #1"));
1098#elif GTEST_USES_POSIX_RE
1099 EXPECT_THAT(r.message(), ContainsRegex(
shiqiane35fdd92008-12-10 05:08:54 +00001100 // POSIX RE doesn't understand the (?s) prefix, but has no trouble
1101 // with (.|\n).
1102 "the following immediate pre-requisites are not satisfied:\n"
1103 "(.|\n)*: pre-requisite #0\n"
zhanyong.wand14aaed2010-01-14 05:36:32 +00001104 "(.|\n)*: pre-requisite #1"));
1105#else
1106 // We can only use Google Test's own simple regex.
1107 EXPECT_THAT(r.message(), ContainsRegex(
1108 "the following immediate pre-requisites are not satisfied:"));
1109 EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #0"));
1110 EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #1"));
1111#endif // GTEST_USES_PCRE
shiqiane35fdd92008-12-10 05:08:54 +00001112
shiqiane35fdd92008-12-10 05:08:54 +00001113 b.DoB(1);
1114 b.DoB(3);
1115 b.DoB(4);
1116}
1117
kosakd478a1f2015-02-14 02:45:40 +00001118TEST(UndefinedReturnValueTest,
1119 ReturnValueIsMandatoryWhenNotDefaultConstructible) {
zhanyong.wan5b95fa72009-01-27 22:28:45 +00001120 MockA a;
1121 // TODO(wan@google.com): We should really verify the output message,
1122 // but we cannot yet due to that EXPECT_DEATH only captures stderr
1123 // while Google Mock logs to stdout.
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001124#if GTEST_HAS_EXCEPTIONS
kosakd478a1f2015-02-14 02:45:40 +00001125 EXPECT_ANY_THROW(a.ReturnNonDefaultConstructible());
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001126#else
kosakd478a1f2015-02-14 02:45:40 +00001127 EXPECT_DEATH_IF_SUPPORTED(a.ReturnNonDefaultConstructible(), "");
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001128#endif
zhanyong.wan5b95fa72009-01-27 22:28:45 +00001129}
1130
shiqiane35fdd92008-12-10 05:08:54 +00001131// Tests that an excessive call (one whose arguments match the
1132// matchers but is called too many times) performs the default action.
1133TEST(ExcessiveCallTest, DoesDefaultAction) {
1134 // When there is an ON_CALL() statement, the action specified by it
1135 // should be taken.
1136 MockA a;
1137 ON_CALL(a, Binary(_, _))
1138 .WillByDefault(Return(true));
1139 EXPECT_CALL(a, Binary(0, 0));
1140 a.Binary(0, 0);
1141 bool result = false;
1142 EXPECT_NONFATAL_FAILURE(result = a.Binary(0, 0),
1143 "Mock function called more times than expected");
1144 EXPECT_TRUE(result);
1145
1146 // When there is no ON_CALL(), the default value for the return type
1147 // should be returned.
1148 MockB b;
1149 EXPECT_CALL(b, DoB(0))
1150 .Times(0);
1151 int n = -1;
1152 EXPECT_NONFATAL_FAILURE(n = b.DoB(0),
1153 "Mock function called more times than expected");
1154 EXPECT_EQ(0, n);
1155}
1156
1157// Tests that when a void function is called too many times,
1158// the failure message contains the argument values.
1159TEST(ExcessiveCallTest, GeneratesFailureForVoidFunction) {
1160 MockA a;
1161 EXPECT_CALL(a, DoA(_))
1162 .Times(0);
1163 EXPECT_NONFATAL_FAILURE(
1164 a.DoA(9),
1165 "Mock function called more times than expected - returning directly.\n"
1166 " Function call: DoA(9)\n"
1167 " Expected: to be never called\n"
1168 " Actual: called once - over-saturated and active");
1169}
1170
1171// Tests that when a non-void function is called too many times, the
1172// failure message contains the argument values and the return value.
1173TEST(ExcessiveCallTest, GeneratesFailureForNonVoidFunction) {
1174 MockB b;
1175 EXPECT_CALL(b, DoB(_));
1176 b.DoB(1);
1177 EXPECT_NONFATAL_FAILURE(
1178 b.DoB(2),
1179 "Mock function called more times than expected - "
1180 "returning default value.\n"
1181 " Function call: DoB(2)\n"
1182 " Returns: 0\n"
1183 " Expected: to be called once\n"
1184 " Actual: called twice - over-saturated and active");
1185}
1186
1187// Tests using sequences.
1188
1189TEST(InSequenceTest, AllExpectationInScopeAreInSequence) {
1190 MockA a;
1191 {
1192 InSequence dummy;
1193
1194 EXPECT_CALL(a, DoA(1));
1195 EXPECT_CALL(a, DoA(2));
1196 }
1197
1198 EXPECT_NONFATAL_FAILURE({ // NOLINT
1199 a.DoA(2);
1200 }, "Unexpected mock function call");
1201
1202 a.DoA(1);
1203 a.DoA(2);
1204}
1205
1206TEST(InSequenceTest, NestedInSequence) {
1207 MockA a;
1208 {
1209 InSequence dummy;
1210
1211 EXPECT_CALL(a, DoA(1));
1212 {
1213 InSequence dummy2;
1214
1215 EXPECT_CALL(a, DoA(2));
1216 EXPECT_CALL(a, DoA(3));
1217 }
1218 }
1219
1220 EXPECT_NONFATAL_FAILURE({ // NOLINT
1221 a.DoA(1);
1222 a.DoA(3);
1223 }, "Unexpected mock function call");
1224
1225 a.DoA(2);
1226 a.DoA(3);
1227}
1228
1229TEST(InSequenceTest, ExpectationsOutOfScopeAreNotAffected) {
1230 MockA a;
1231 {
1232 InSequence dummy;
1233
1234 EXPECT_CALL(a, DoA(1));
1235 EXPECT_CALL(a, DoA(2));
1236 }
1237 EXPECT_CALL(a, DoA(3));
1238
1239 EXPECT_NONFATAL_FAILURE({ // NOLINT
1240 a.DoA(2);
1241 }, "Unexpected mock function call");
1242
1243 a.DoA(3);
1244 a.DoA(1);
1245 a.DoA(2);
1246}
1247
1248// Tests that any order is allowed when no sequence is used.
1249TEST(SequenceTest, AnyOrderIsOkByDefault) {
1250 {
1251 MockA a;
1252 MockB b;
1253
1254 EXPECT_CALL(a, DoA(1));
1255 EXPECT_CALL(b, DoB())
1256 .Times(AnyNumber());
1257
1258 a.DoA(1);
1259 b.DoB();
1260 }
1261
1262 { // NOLINT
1263 MockA a;
1264 MockB b;
1265
1266 EXPECT_CALL(a, DoA(1));
1267 EXPECT_CALL(b, DoB())
1268 .Times(AnyNumber());
1269
1270 b.DoB();
1271 a.DoA(1);
1272 }
1273}
1274
shiqiane35fdd92008-12-10 05:08:54 +00001275// Tests that the calls must be in strict order when a complete order
1276// is specified.
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001277TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo1) {
shiqiane35fdd92008-12-10 05:08:54 +00001278 MockA a;
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001279 ON_CALL(a, ReturnResult(_))
1280 .WillByDefault(Return(Result()));
1281
shiqiane35fdd92008-12-10 05:08:54 +00001282 Sequence s;
shiqiane35fdd92008-12-10 05:08:54 +00001283 EXPECT_CALL(a, ReturnResult(1))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001284 .InSequence(s);
shiqiane35fdd92008-12-10 05:08:54 +00001285 EXPECT_CALL(a, ReturnResult(2))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001286 .InSequence(s);
shiqiane35fdd92008-12-10 05:08:54 +00001287 EXPECT_CALL(a, ReturnResult(3))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001288 .InSequence(s);
shiqiane35fdd92008-12-10 05:08:54 +00001289
1290 a.ReturnResult(1);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001291
1292 // May only be called after a.ReturnResult(2).
1293 EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call");
1294
shiqiane35fdd92008-12-10 05:08:54 +00001295 a.ReturnResult(2);
1296 a.ReturnResult(3);
1297}
1298
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001299// Tests that the calls must be in strict order when a complete order
1300// is specified.
1301TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo2) {
shiqiane35fdd92008-12-10 05:08:54 +00001302 MockA a;
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001303 ON_CALL(a, ReturnResult(_))
1304 .WillByDefault(Return(Result()));
shiqiane35fdd92008-12-10 05:08:54 +00001305
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001306 Sequence s;
shiqiane35fdd92008-12-10 05:08:54 +00001307 EXPECT_CALL(a, ReturnResult(1))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001308 .InSequence(s);
shiqiane35fdd92008-12-10 05:08:54 +00001309 EXPECT_CALL(a, ReturnResult(2))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001310 .InSequence(s);
shiqiane35fdd92008-12-10 05:08:54 +00001311
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001312 // May only be called after a.ReturnResult(1).
1313 EXPECT_NONFATAL_FAILURE(a.ReturnResult(2), "Unexpected mock function call");
shiqiane35fdd92008-12-10 05:08:54 +00001314
shiqiane35fdd92008-12-10 05:08:54 +00001315 a.ReturnResult(1);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001316 a.ReturnResult(2);
1317}
1318
1319// Tests specifying a DAG using multiple sequences.
1320class PartialOrderTest : public testing::Test {
1321 protected:
1322 PartialOrderTest() {
1323 ON_CALL(a_, ReturnResult(_))
1324 .WillByDefault(Return(Result()));
1325
1326 // Specifies this partial ordering:
1327 //
1328 // a.ReturnResult(1) ==>
1329 // a.ReturnResult(2) * n ==> a.ReturnResult(3)
1330 // b.DoB() * 2 ==>
1331 Sequence x, y;
1332 EXPECT_CALL(a_, ReturnResult(1))
1333 .InSequence(x);
1334 EXPECT_CALL(b_, DoB())
1335 .Times(2)
1336 .InSequence(y);
1337 EXPECT_CALL(a_, ReturnResult(2))
1338 .Times(AnyNumber())
1339 .InSequence(x, y);
1340 EXPECT_CALL(a_, ReturnResult(3))
1341 .InSequence(x);
1342 }
1343
1344 MockA a_;
1345 MockB b_;
1346};
1347
1348TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag1) {
1349 a_.ReturnResult(1);
1350 b_.DoB();
1351
1352 // May only be called after the second DoB().
1353 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call");
1354
1355 b_.DoB();
1356 a_.ReturnResult(3);
1357}
1358
1359TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag2) {
1360 // May only be called after ReturnResult(1).
1361 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call");
1362
1363 a_.ReturnResult(1);
1364 b_.DoB();
1365 b_.DoB();
1366 a_.ReturnResult(3);
1367}
1368
1369TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag3) {
1370 // May only be called last.
1371 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(3), "Unexpected mock function call");
1372
1373 a_.ReturnResult(1);
1374 b_.DoB();
1375 b_.DoB();
1376 a_.ReturnResult(3);
1377}
1378
1379TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag4) {
1380 a_.ReturnResult(1);
1381 b_.DoB();
1382 b_.DoB();
1383 a_.ReturnResult(3);
1384
1385 // May only be called before ReturnResult(3).
1386 EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call");
shiqiane35fdd92008-12-10 05:08:54 +00001387}
1388
shiqiane35fdd92008-12-10 05:08:54 +00001389TEST(SequenceTest, Retirement) {
1390 MockA a;
1391 Sequence s;
1392
1393 EXPECT_CALL(a, DoA(1))
1394 .InSequence(s);
1395 EXPECT_CALL(a, DoA(_))
1396 .InSequence(s)
1397 .RetiresOnSaturation();
1398 EXPECT_CALL(a, DoA(1))
1399 .InSequence(s);
1400
1401 a.DoA(1);
1402 a.DoA(2);
1403 a.DoA(1);
1404}
1405
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001406// Tests Expectation.
1407
1408TEST(ExpectationTest, ConstrutorsWork) {
1409 MockA a;
1410 Expectation e1; // Default ctor.
zhanyong.waned6c9272011-02-23 19:39:27 +00001411
1412 // Ctor from various forms of EXPECT_CALL.
1413 Expectation e2 = EXPECT_CALL(a, DoA(2));
1414 Expectation e3 = EXPECT_CALL(a, DoA(3)).With(_);
1415 {
1416 Sequence s;
1417 Expectation e4 = EXPECT_CALL(a, DoA(4)).Times(1);
1418 Expectation e5 = EXPECT_CALL(a, DoA(5)).InSequence(s);
1419 }
1420 Expectation e6 = EXPECT_CALL(a, DoA(6)).After(e2);
1421 Expectation e7 = EXPECT_CALL(a, DoA(7)).WillOnce(Return());
1422 Expectation e8 = EXPECT_CALL(a, DoA(8)).WillRepeatedly(Return());
1423 Expectation e9 = EXPECT_CALL(a, DoA(9)).RetiresOnSaturation();
1424
1425 Expectation e10 = e2; // Copy ctor.
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001426
1427 EXPECT_THAT(e1, Ne(e2));
zhanyong.waned6c9272011-02-23 19:39:27 +00001428 EXPECT_THAT(e2, Eq(e10));
1429
1430 a.DoA(2);
1431 a.DoA(3);
1432 a.DoA(4);
1433 a.DoA(5);
1434 a.DoA(6);
1435 a.DoA(7);
1436 a.DoA(8);
1437 a.DoA(9);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001438}
1439
1440TEST(ExpectationTest, AssignmentWorks) {
1441 MockA a;
1442 Expectation e1;
1443 Expectation e2 = EXPECT_CALL(a, DoA(1));
1444
1445 EXPECT_THAT(e1, Ne(e2));
1446
1447 e1 = e2;
1448 EXPECT_THAT(e1, Eq(e2));
1449
1450 a.DoA(1);
1451}
1452
1453// Tests ExpectationSet.
1454
1455TEST(ExpectationSetTest, MemberTypesAreCorrect) {
1456 ::testing::StaticAssertTypeEq<Expectation, ExpectationSet::value_type>();
1457}
1458
1459TEST(ExpectationSetTest, ConstructorsWork) {
1460 MockA a;
1461
1462 Expectation e1;
1463 const Expectation e2;
1464 ExpectationSet es1; // Default ctor.
1465 ExpectationSet es2 = EXPECT_CALL(a, DoA(1)); // Ctor from EXPECT_CALL.
1466 ExpectationSet es3 = e1; // Ctor from Expectation.
1467 ExpectationSet es4(e1); // Ctor from Expectation; alternative syntax.
1468 ExpectationSet es5 = e2; // Ctor from const Expectation.
1469 ExpectationSet es6(e2); // Ctor from const Expectation; alternative syntax.
1470 ExpectationSet es7 = es2; // Copy ctor.
1471
1472 EXPECT_EQ(0, es1.size());
1473 EXPECT_EQ(1, es2.size());
1474 EXPECT_EQ(1, es3.size());
1475 EXPECT_EQ(1, es4.size());
1476 EXPECT_EQ(1, es5.size());
1477 EXPECT_EQ(1, es6.size());
1478 EXPECT_EQ(1, es7.size());
1479
1480 EXPECT_THAT(es3, Ne(es2));
1481 EXPECT_THAT(es4, Eq(es3));
1482 EXPECT_THAT(es5, Eq(es4));
1483 EXPECT_THAT(es6, Eq(es5));
1484 EXPECT_THAT(es7, Eq(es2));
1485 a.DoA(1);
1486}
1487
1488TEST(ExpectationSetTest, AssignmentWorks) {
1489 ExpectationSet es1;
1490 ExpectationSet es2 = Expectation();
1491
1492 es1 = es2;
1493 EXPECT_EQ(1, es1.size());
1494 EXPECT_THAT(*(es1.begin()), Eq(Expectation()));
1495 EXPECT_THAT(es1, Eq(es2));
1496}
1497
1498TEST(ExpectationSetTest, InsertionWorks) {
1499 ExpectationSet es1;
1500 Expectation e1;
1501 es1 += e1;
1502 EXPECT_EQ(1, es1.size());
1503 EXPECT_THAT(*(es1.begin()), Eq(e1));
1504
1505 MockA a;
1506 Expectation e2 = EXPECT_CALL(a, DoA(1));
1507 es1 += e2;
1508 EXPECT_EQ(2, es1.size());
1509
1510 ExpectationSet::const_iterator it1 = es1.begin();
1511 ExpectationSet::const_iterator it2 = it1;
1512 ++it2;
1513 EXPECT_TRUE(*it1 == e1 || *it2 == e1); // e1 must be in the set.
1514 EXPECT_TRUE(*it1 == e2 || *it2 == e2); // e2 must be in the set too.
1515 a.DoA(1);
1516}
1517
1518TEST(ExpectationSetTest, SizeWorks) {
1519 ExpectationSet es;
1520 EXPECT_EQ(0, es.size());
1521
1522 es += Expectation();
1523 EXPECT_EQ(1, es.size());
1524
1525 MockA a;
1526 es += EXPECT_CALL(a, DoA(1));
1527 EXPECT_EQ(2, es.size());
1528
1529 a.DoA(1);
1530}
1531
1532TEST(ExpectationSetTest, IsEnumerable) {
1533 ExpectationSet es;
zhanyong.wanc10a35a2013-04-04 22:45:59 +00001534 EXPECT_TRUE(es.begin() == es.end());
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001535
1536 es += Expectation();
1537 ExpectationSet::const_iterator it = es.begin();
zhanyong.wanc10a35a2013-04-04 22:45:59 +00001538 EXPECT_TRUE(it != es.end());
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001539 EXPECT_THAT(*it, Eq(Expectation()));
1540 ++it;
zhanyong.wanc10a35a2013-04-04 22:45:59 +00001541 EXPECT_TRUE(it== es.end());
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001542}
1543
1544// Tests the .After() clause.
1545
1546TEST(AfterTest, SucceedsWhenPartialOrderIsSatisfied) {
1547 MockA a;
1548 ExpectationSet es;
1549 es += EXPECT_CALL(a, DoA(1));
1550 es += EXPECT_CALL(a, DoA(2));
1551 EXPECT_CALL(a, DoA(3))
1552 .After(es);
1553
1554 a.DoA(1);
1555 a.DoA(2);
1556 a.DoA(3);
1557}
1558
1559TEST(AfterTest, SucceedsWhenTotalOrderIsSatisfied) {
1560 MockA a;
1561 MockB b;
1562 // The following also verifies that const Expectation objects work
1563 // too. Do not remove the const modifiers.
1564 const Expectation e1 = EXPECT_CALL(a, DoA(1));
1565 const Expectation e2 = EXPECT_CALL(b, DoB())
1566 .Times(2)
1567 .After(e1);
1568 EXPECT_CALL(a, DoA(2)).After(e2);
1569
1570 a.DoA(1);
1571 b.DoB();
1572 b.DoB();
1573 a.DoA(2);
1574}
1575
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001576// Calls must be in strict order when specified so using .After().
1577TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo1) {
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001578 MockA a;
1579 MockB b;
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001580
1581 // Define ordering:
1582 // a.DoA(1) ==> b.DoB() ==> a.DoA(2)
1583 Expectation e1 = EXPECT_CALL(a, DoA(1));
1584 Expectation e2 = EXPECT_CALL(b, DoB())
1585 .After(e1);
1586 EXPECT_CALL(a, DoA(2))
1587 .After(e2);
1588
1589 a.DoA(1);
1590
1591 // May only be called after DoB().
1592 EXPECT_NONFATAL_FAILURE(a.DoA(2), "Unexpected mock function call");
1593
1594 b.DoB();
1595 a.DoA(2);
1596}
1597
1598// Calls must be in strict order when specified so using .After().
1599TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo2) {
1600 MockA a;
1601 MockB b;
1602
1603 // Define ordering:
1604 // a.DoA(1) ==> b.DoB() * 2 ==> a.DoA(2)
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001605 Expectation e1 = EXPECT_CALL(a, DoA(1));
1606 Expectation e2 = EXPECT_CALL(b, DoB())
1607 .Times(2)
1608 .After(e1);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001609 EXPECT_CALL(a, DoA(2))
1610 .After(e2);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001611
1612 a.DoA(1);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001613 b.DoB();
1614
1615 // May only be called after the second DoB().
1616 EXPECT_NONFATAL_FAILURE(a.DoA(2), "Unexpected mock function call");
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001617
1618 b.DoB();
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001619 a.DoA(2);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001620}
1621
1622// Calls must satisfy the partial order when specified so.
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001623TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo) {
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001624 MockA a;
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001625 ON_CALL(a, ReturnResult(_))
1626 .WillByDefault(Return(Result()));
1627
1628 // Define ordering:
1629 // a.DoA(1) ==>
1630 // a.DoA(2) ==> a.ReturnResult(3)
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001631 Expectation e = EXPECT_CALL(a, DoA(1));
1632 const ExpectationSet es = EXPECT_CALL(a, DoA(2));
1633 EXPECT_CALL(a, ReturnResult(3))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001634 .After(e, es);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001635
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001636 // May only be called last.
1637 EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call");
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001638
1639 a.DoA(2);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001640 a.DoA(1);
1641 a.ReturnResult(3);
1642}
1643
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001644// Calls must satisfy the partial order when specified so.
1645TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo2) {
1646 MockA a;
1647
1648 // Define ordering:
1649 // a.DoA(1) ==>
1650 // a.DoA(2) ==> a.DoA(3)
1651 Expectation e = EXPECT_CALL(a, DoA(1));
1652 const ExpectationSet es = EXPECT_CALL(a, DoA(2));
1653 EXPECT_CALL(a, DoA(3))
1654 .After(e, es);
1655
1656 a.DoA(2);
1657
1658 // May only be called last.
1659 EXPECT_NONFATAL_FAILURE(a.DoA(3), "Unexpected mock function call");
1660
1661 a.DoA(1);
1662 a.DoA(3);
1663}
1664
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001665// .After() can be combined with .InSequence().
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001666TEST(AfterTest, CanBeUsedWithInSequence) {
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001667 MockA a;
1668 Sequence s;
1669 Expectation e = EXPECT_CALL(a, DoA(1));
1670 EXPECT_CALL(a, DoA(2)).InSequence(s);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001671 EXPECT_CALL(a, DoA(3))
1672 .InSequence(s)
1673 .After(e);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001674
1675 a.DoA(1);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001676
1677 // May only be after DoA(2).
1678 EXPECT_NONFATAL_FAILURE(a.DoA(3), "Unexpected mock function call");
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001679
1680 a.DoA(2);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001681 a.DoA(3);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001682}
1683
1684// .After() can be called multiple times.
1685TEST(AfterTest, CanBeCalledManyTimes) {
1686 MockA a;
1687 Expectation e1 = EXPECT_CALL(a, DoA(1));
1688 Expectation e2 = EXPECT_CALL(a, DoA(2));
1689 Expectation e3 = EXPECT_CALL(a, DoA(3));
1690 EXPECT_CALL(a, DoA(4))
1691 .After(e1)
1692 .After(e2)
1693 .After(e3);
1694
1695 a.DoA(3);
1696 a.DoA(1);
1697 a.DoA(2);
1698 a.DoA(4);
1699}
1700
1701// .After() accepts up to 5 arguments.
1702TEST(AfterTest, AcceptsUpToFiveArguments) {
1703 MockA a;
1704 Expectation e1 = EXPECT_CALL(a, DoA(1));
1705 Expectation e2 = EXPECT_CALL(a, DoA(2));
1706 Expectation e3 = EXPECT_CALL(a, DoA(3));
1707 ExpectationSet es1 = EXPECT_CALL(a, DoA(4));
1708 ExpectationSet es2 = EXPECT_CALL(a, DoA(5));
1709 EXPECT_CALL(a, DoA(6))
1710 .After(e1, e2, e3, es1, es2);
1711
1712 a.DoA(5);
1713 a.DoA(2);
1714 a.DoA(4);
1715 a.DoA(1);
1716 a.DoA(3);
1717 a.DoA(6);
1718}
1719
1720// .After() allows input to contain duplicated Expectations.
1721TEST(AfterTest, AcceptsDuplicatedInput) {
1722 MockA a;
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001723 ON_CALL(a, ReturnResult(_))
1724 .WillByDefault(Return(Result()));
1725
1726 // Define ordering:
1727 // DoA(1) ==>
1728 // DoA(2) ==> ReturnResult(3)
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001729 Expectation e1 = EXPECT_CALL(a, DoA(1));
1730 Expectation e2 = EXPECT_CALL(a, DoA(2));
1731 ExpectationSet es;
1732 es += e1;
1733 es += e2;
1734 EXPECT_CALL(a, ReturnResult(3))
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001735 .After(e1, e2, es, e1);
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001736
1737 a.DoA(1);
zhanyong.wanedd4ab42013-02-28 22:58:51 +00001738
1739 // May only be after DoA(2).
1740 EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call");
zhanyong.wan41b9b0b2009-07-01 19:04:51 +00001741
1742 a.DoA(2);
1743 a.ReturnResult(3);
1744}
1745
1746// An Expectation added to an ExpectationSet after it has been used in
1747// an .After() has no effect.
1748TEST(AfterTest, ChangesToExpectationSetHaveNoEffectAfterwards) {
1749 MockA a;
1750 ExpectationSet es1 = EXPECT_CALL(a, DoA(1));
1751 Expectation e2 = EXPECT_CALL(a, DoA(2));
1752 EXPECT_CALL(a, DoA(3))
1753 .After(es1);
1754 es1 += e2;
1755
1756 a.DoA(1);
1757 a.DoA(3);
1758 a.DoA(2);
1759}
1760
shiqiane35fdd92008-12-10 05:08:54 +00001761// Tests that Google Mock correctly handles calls to mock functions
1762// after a mock object owning one of their pre-requisites has died.
1763
1764// Tests that calls that satisfy the original spec are successful.
1765TEST(DeletingMockEarlyTest, Success1) {
1766 MockB* const b1 = new MockB;
1767 MockA* const a = new MockA;
1768 MockB* const b2 = new MockB;
1769
1770 {
1771 InSequence dummy;
1772 EXPECT_CALL(*b1, DoB(_))
1773 .WillOnce(Return(1));
1774 EXPECT_CALL(*a, Binary(_, _))
1775 .Times(AnyNumber())
1776 .WillRepeatedly(Return(true));
1777 EXPECT_CALL(*b2, DoB(_))
1778 .Times(AnyNumber())
1779 .WillRepeatedly(Return(2));
1780 }
1781
1782 EXPECT_EQ(1, b1->DoB(1));
1783 delete b1;
1784 // a's pre-requisite has died.
1785 EXPECT_TRUE(a->Binary(0, 1));
1786 delete b2;
1787 // a's successor has died.
1788 EXPECT_TRUE(a->Binary(1, 2));
1789 delete a;
1790}
1791
1792// Tests that calls that satisfy the original spec are successful.
1793TEST(DeletingMockEarlyTest, Success2) {
1794 MockB* const b1 = new MockB;
1795 MockA* const a = new MockA;
1796 MockB* const b2 = new MockB;
1797
1798 {
1799 InSequence dummy;
1800 EXPECT_CALL(*b1, DoB(_))
1801 .WillOnce(Return(1));
1802 EXPECT_CALL(*a, Binary(_, _))
1803 .Times(AnyNumber());
1804 EXPECT_CALL(*b2, DoB(_))
1805 .Times(AnyNumber())
1806 .WillRepeatedly(Return(2));
1807 }
1808
1809 delete a; // a is trivially satisfied.
1810 EXPECT_EQ(1, b1->DoB(1));
1811 EXPECT_EQ(2, b2->DoB(2));
1812 delete b1;
1813 delete b2;
1814}
1815
zhanyong.wan6f147692009-03-03 06:44:08 +00001816// Tests that it's OK to delete a mock object itself in its action.
1817
zhanyong.wan32de5f52009-12-23 00:13:23 +00001818// Suppresses warning on unreferenced formal parameter in MSVC with
1819// -W4.
1820#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +00001821# pragma warning(push)
1822# pragma warning(disable:4100)
zhanyong.wan32de5f52009-12-23 00:13:23 +00001823#endif
1824
zhanyong.wan6f147692009-03-03 06:44:08 +00001825ACTION_P(Delete, ptr) { delete ptr; }
1826
zhanyong.wan32de5f52009-12-23 00:13:23 +00001827#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +00001828# pragma warning(pop)
zhanyong.wan32de5f52009-12-23 00:13:23 +00001829#endif
1830
zhanyong.wan6f147692009-03-03 06:44:08 +00001831TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningVoid) {
1832 MockA* const a = new MockA;
1833 EXPECT_CALL(*a, DoA(_)).WillOnce(Delete(a));
1834 a->DoA(42); // This will cause a to be deleted.
1835}
1836
1837TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningValue) {
1838 MockA* const a = new MockA;
1839 EXPECT_CALL(*a, ReturnResult(_))
1840 .WillOnce(DoAll(Delete(a), Return(Result())));
1841 a->ReturnResult(42); // This will cause a to be deleted.
1842}
1843
1844// Tests that calls that violate the original spec yield failures.
shiqiane35fdd92008-12-10 05:08:54 +00001845TEST(DeletingMockEarlyTest, Failure1) {
1846 MockB* const b1 = new MockB;
1847 MockA* const a = new MockA;
1848 MockB* const b2 = new MockB;
1849
1850 {
1851 InSequence dummy;
1852 EXPECT_CALL(*b1, DoB(_))
1853 .WillOnce(Return(1));
1854 EXPECT_CALL(*a, Binary(_, _))
1855 .Times(AnyNumber());
1856 EXPECT_CALL(*b2, DoB(_))
1857 .Times(AnyNumber())
1858 .WillRepeatedly(Return(2));
1859 }
1860
1861 delete a; // a is trivially satisfied.
1862 EXPECT_NONFATAL_FAILURE({
1863 b2->DoB(2);
1864 }, "Unexpected mock function call");
1865 EXPECT_EQ(1, b1->DoB(1));
1866 delete b1;
1867 delete b2;
1868}
1869
zhanyong.wan6f147692009-03-03 06:44:08 +00001870// Tests that calls that violate the original spec yield failures.
shiqiane35fdd92008-12-10 05:08:54 +00001871TEST(DeletingMockEarlyTest, Failure2) {
1872 MockB* const b1 = new MockB;
1873 MockA* const a = new MockA;
1874 MockB* const b2 = new MockB;
1875
1876 {
1877 InSequence dummy;
1878 EXPECT_CALL(*b1, DoB(_));
1879 EXPECT_CALL(*a, Binary(_, _))
1880 .Times(AnyNumber());
1881 EXPECT_CALL(*b2, DoB(_))
1882 .Times(AnyNumber());
1883 }
1884
1885 EXPECT_NONFATAL_FAILURE(delete b1,
1886 "Actual: never called");
1887 EXPECT_NONFATAL_FAILURE(a->Binary(0, 1),
1888 "Unexpected mock function call");
1889 EXPECT_NONFATAL_FAILURE(b2->DoB(1),
1890 "Unexpected mock function call");
1891 delete a;
1892 delete b2;
1893}
1894
1895class EvenNumberCardinality : public CardinalityInterface {
1896 public:
1897 // Returns true iff call_count calls will satisfy this cardinality.
1898 virtual bool IsSatisfiedByCallCount(int call_count) const {
1899 return call_count % 2 == 0;
1900 }
1901
1902 // Returns true iff call_count calls will saturate this cardinality.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001903 virtual bool IsSaturatedByCallCount(int /* call_count */) const {
1904 return false;
1905 }
shiqiane35fdd92008-12-10 05:08:54 +00001906
1907 // Describes self to an ostream.
1908 virtual void DescribeTo(::std::ostream* os) const {
1909 *os << "called even number of times";
1910 }
1911};
1912
1913Cardinality EvenNumber() {
1914 return Cardinality(new EvenNumberCardinality);
1915}
1916
1917TEST(ExpectationBaseTest,
1918 AllPrerequisitesAreSatisfiedWorksForNonMonotonicCardinality) {
1919 MockA* a = new MockA;
1920 Sequence s;
1921
1922 EXPECT_CALL(*a, DoA(1))
1923 .Times(EvenNumber())
1924 .InSequence(s);
1925 EXPECT_CALL(*a, DoA(2))
1926 .Times(AnyNumber())
1927 .InSequence(s);
1928 EXPECT_CALL(*a, DoA(3))
1929 .Times(AnyNumber());
1930
1931 a->DoA(3);
1932 a->DoA(1);
1933 EXPECT_NONFATAL_FAILURE(a->DoA(2), "Unexpected mock function call");
1934 EXPECT_NONFATAL_FAILURE(delete a, "to be called even number of times");
1935}
1936
1937// The following tests verify the message generated when a mock
1938// function is called.
1939
1940struct Printable {
1941};
1942
1943inline void operator<<(::std::ostream& os, const Printable&) {
1944 os << "Printable";
1945}
1946
1947struct Unprintable {
1948 Unprintable() : value(0) {}
1949 int value;
1950};
1951
1952class MockC {
1953 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00001954 MockC() {}
1955
shiqiane35fdd92008-12-10 05:08:54 +00001956 MOCK_METHOD6(VoidMethod, void(bool cond, int n, string s, void* p,
1957 const Printable& x, Unprintable y));
1958 MOCK_METHOD0(NonVoidMethod, int()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +00001959
1960 private:
1961 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockC);
shiqiane35fdd92008-12-10 05:08:54 +00001962};
1963
vladlosev76c1c612010-05-05 19:47:46 +00001964class VerboseFlagPreservingFixture : public testing::Test {
1965 protected:
vladlosev76c1c612010-05-05 19:47:46 +00001966 VerboseFlagPreservingFixture()
jgm38513a82012-11-15 15:50:36 +00001967 : saved_verbose_flag_(GMOCK_FLAG(verbose)) {}
vladlosev76c1c612010-05-05 19:47:46 +00001968
1969 ~VerboseFlagPreservingFixture() { GMOCK_FLAG(verbose) = saved_verbose_flag_; }
1970
1971 private:
1972 const string saved_verbose_flag_;
1973
1974 GTEST_DISALLOW_COPY_AND_ASSIGN_(VerboseFlagPreservingFixture);
1975};
1976
zhanyong.wan2516f602010-08-31 18:28:02 +00001977#if GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +00001978
zhanyong.wanc8965042013-03-01 07:10:07 +00001979// Tests that an uninteresting mock function call on a naggy mock
1980// generates a warning containing the stack trace.
1981TEST(FunctionCallMessageTest,
1982 UninterestingCallOnNaggyMockGeneratesFyiWithStackTrace) {
1983 NaggyMock<MockC> c;
zhanyong.wan470df422010-02-02 22:34:58 +00001984 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00001985 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable());
jgm38513a82012-11-15 15:50:36 +00001986 const std::string output = GetCapturedStdout();
shiqiane35fdd92008-12-10 05:08:54 +00001987 EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output);
1988 EXPECT_PRED_FORMAT2(IsSubstring, "Stack trace:", output);
zhanyong.wan658ac0b2011-02-24 07:29:13 +00001989
1990# ifndef NDEBUG
1991
shiqiane35fdd92008-12-10 05:08:54 +00001992 // We check the stack trace content in dbg-mode only, as opt-mode
1993 // may inline the call we are interested in seeing.
1994
1995 // Verifies that a void mock function's name appears in the stack
1996 // trace.
zhanyong.wan470df422010-02-02 22:34:58 +00001997 EXPECT_PRED_FORMAT2(IsSubstring, "VoidMethod(", output);
shiqiane35fdd92008-12-10 05:08:54 +00001998
1999 // Verifies that a non-void mock function's name appears in the
2000 // stack trace.
zhanyong.wan470df422010-02-02 22:34:58 +00002001 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002002 c.NonVoidMethod();
jgm38513a82012-11-15 15:50:36 +00002003 const std::string output2 = GetCapturedStdout();
zhanyong.wan470df422010-02-02 22:34:58 +00002004 EXPECT_PRED_FORMAT2(IsSubstring, "NonVoidMethod(", output2);
zhanyong.wan658ac0b2011-02-24 07:29:13 +00002005
2006# endif // NDEBUG
shiqiane35fdd92008-12-10 05:08:54 +00002007}
2008
zhanyong.wanc8965042013-03-01 07:10:07 +00002009// Tests that an uninteresting mock function call on a naggy mock
2010// causes the function arguments and return value to be printed.
2011TEST(FunctionCallMessageTest,
2012 UninterestingCallOnNaggyMockPrintsArgumentsAndReturnValue) {
shiqiane35fdd92008-12-10 05:08:54 +00002013 // A non-void mock function.
zhanyong.wanc8965042013-03-01 07:10:07 +00002014 NaggyMock<MockB> b;
zhanyong.wan470df422010-02-02 22:34:58 +00002015 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002016 b.DoB();
jgm38513a82012-11-15 15:50:36 +00002017 const std::string output1 = GetCapturedStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002018 EXPECT_PRED_FORMAT2(
2019 IsSubstring,
2020 "Uninteresting mock function call - returning default value.\n"
2021 " Function call: DoB()\n"
zhanyong.wan470df422010-02-02 22:34:58 +00002022 " Returns: 0\n", output1.c_str());
shiqiane35fdd92008-12-10 05:08:54 +00002023 // Makes sure the return value is printed.
2024
2025 // A void mock function.
zhanyong.wanc8965042013-03-01 07:10:07 +00002026 NaggyMock<MockC> c;
zhanyong.wan470df422010-02-02 22:34:58 +00002027 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002028 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable());
jgm38513a82012-11-15 15:50:36 +00002029 const std::string output2 = GetCapturedStdout();
zhanyong.wan470df422010-02-02 22:34:58 +00002030 EXPECT_THAT(output2.c_str(),
2031 ContainsRegex(
2032 "Uninteresting mock function call - returning directly\\.\n"
2033 " Function call: VoidMethod"
2034 "\\(false, 5, \"Hi\", NULL, @.+ "
zhanyong.wanc6333dc2010-08-09 18:20:45 +00002035 "Printable, 4-byte object <00-00 00-00>\\)"));
shiqiane35fdd92008-12-10 05:08:54 +00002036 // A void function has no return value to print.
2037}
2038
2039// Tests how the --gmock_verbose flag affects Google Mock's output.
2040
vladlosev76c1c612010-05-05 19:47:46 +00002041class GMockVerboseFlagTest : public VerboseFlagPreservingFixture {
shiqiane35fdd92008-12-10 05:08:54 +00002042 public:
2043 // Verifies that the given Google Mock output is correct. (When
2044 // should_print is true, the output should match the given regex and
2045 // contain the given function name in the stack trace. When it's
2046 // false, the output should be empty.)
jgm38513a82012-11-15 15:50:36 +00002047 void VerifyOutput(const std::string& output, bool should_print,
zhanyong.wan470df422010-02-02 22:34:58 +00002048 const string& expected_substring,
shiqiane35fdd92008-12-10 05:08:54 +00002049 const string& function_name) {
2050 if (should_print) {
zhanyong.wan470df422010-02-02 22:34:58 +00002051 EXPECT_THAT(output.c_str(), HasSubstr(expected_substring));
zhanyong.wan658ac0b2011-02-24 07:29:13 +00002052# ifndef NDEBUG
shiqiane35fdd92008-12-10 05:08:54 +00002053 // We check the stack trace content in dbg-mode only, as opt-mode
2054 // may inline the call we are interested in seeing.
zhanyong.wan470df422010-02-02 22:34:58 +00002055 EXPECT_THAT(output.c_str(), HasSubstr(function_name));
zhanyong.wan658ac0b2011-02-24 07:29:13 +00002056# else
zhanyong.wan470df422010-02-02 22:34:58 +00002057 // Suppresses 'unused function parameter' warnings.
2058 static_cast<void>(function_name);
zhanyong.wan658ac0b2011-02-24 07:29:13 +00002059# endif // NDEBUG
shiqiane35fdd92008-12-10 05:08:54 +00002060 } else {
zhanyong.wan470df422010-02-02 22:34:58 +00002061 EXPECT_STREQ("", output.c_str());
shiqiane35fdd92008-12-10 05:08:54 +00002062 }
2063 }
2064
2065 // Tests how the flag affects expected calls.
2066 void TestExpectedCall(bool should_print) {
2067 MockA a;
2068 EXPECT_CALL(a, DoA(5));
2069 EXPECT_CALL(a, Binary(_, 1))
2070 .WillOnce(Return(true));
2071
2072 // A void-returning function.
zhanyong.wan470df422010-02-02 22:34:58 +00002073 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002074 a.DoA(5);
2075 VerifyOutput(
zhanyong.wan470df422010-02-02 22:34:58 +00002076 GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +00002077 should_print,
zhanyong.wan470df422010-02-02 22:34:58 +00002078 "Mock function call matches EXPECT_CALL(a, DoA(5))...\n"
2079 " Function call: DoA(5)\n"
2080 "Stack trace:\n",
2081 "DoA");
shiqiane35fdd92008-12-10 05:08:54 +00002082
2083 // A non-void-returning function.
zhanyong.wan470df422010-02-02 22:34:58 +00002084 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002085 a.Binary(2, 1);
2086 VerifyOutput(
zhanyong.wan470df422010-02-02 22:34:58 +00002087 GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +00002088 should_print,
zhanyong.wan470df422010-02-02 22:34:58 +00002089 "Mock function call matches EXPECT_CALL(a, Binary(_, 1))...\n"
2090 " Function call: Binary(2, 1)\n"
shiqiane35fdd92008-12-10 05:08:54 +00002091 " Returns: true\n"
zhanyong.wan470df422010-02-02 22:34:58 +00002092 "Stack trace:\n",
2093 "Binary");
shiqiane35fdd92008-12-10 05:08:54 +00002094 }
2095
zhanyong.wanc8965042013-03-01 07:10:07 +00002096 // Tests how the flag affects uninteresting calls on a naggy mock.
2097 void TestUninterestingCallOnNaggyMock(bool should_print) {
2098 NaggyMock<MockA> a;
kosak04ce8522014-01-12 23:42:34 +00002099 const string note =
2100 "NOTE: You can safely ignore the above warning unless this "
2101 "call should not happen. Do not suppress it by blindly adding "
2102 "an EXPECT_CALL() if you don't mean to enforce the call. "
2103 "See http://code.google.com/p/googlemock/wiki/CookBook#"
2104 "Knowing_When_to_Expect for details.";
shiqiane35fdd92008-12-10 05:08:54 +00002105
2106 // A void-returning function.
zhanyong.wan470df422010-02-02 22:34:58 +00002107 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002108 a.DoA(5);
2109 VerifyOutput(
zhanyong.wan470df422010-02-02 22:34:58 +00002110 GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +00002111 should_print,
2112 "\nGMOCK WARNING:\n"
zhanyong.wan470df422010-02-02 22:34:58 +00002113 "Uninteresting mock function call - returning directly.\n"
kosak04ce8522014-01-12 23:42:34 +00002114 " Function call: DoA(5)\n" +
2115 note +
2116 "\nStack trace:\n",
zhanyong.wan470df422010-02-02 22:34:58 +00002117 "DoA");
shiqiane35fdd92008-12-10 05:08:54 +00002118
2119 // A non-void-returning function.
zhanyong.wan470df422010-02-02 22:34:58 +00002120 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +00002121 a.Binary(2, 1);
2122 VerifyOutput(
zhanyong.wan470df422010-02-02 22:34:58 +00002123 GetCapturedStdout(),
shiqiane35fdd92008-12-10 05:08:54 +00002124 should_print,
2125 "\nGMOCK WARNING:\n"
zhanyong.wan470df422010-02-02 22:34:58 +00002126 "Uninteresting mock function call - returning default value.\n"
2127 " Function call: Binary(2, 1)\n"
kosak04ce8522014-01-12 23:42:34 +00002128 " Returns: false\n" +
2129 note +
2130 "\nStack trace:\n",
zhanyong.wan470df422010-02-02 22:34:58 +00002131 "Binary");
shiqiane35fdd92008-12-10 05:08:54 +00002132 }
2133};
2134
2135// Tests that --gmock_verbose=info causes both expected and
2136// uninteresting calls to be reported.
2137TEST_F(GMockVerboseFlagTest, Info) {
2138 GMOCK_FLAG(verbose) = kInfoVerbosity;
2139 TestExpectedCall(true);
zhanyong.wanc8965042013-03-01 07:10:07 +00002140 TestUninterestingCallOnNaggyMock(true);
shiqiane35fdd92008-12-10 05:08:54 +00002141}
2142
2143// Tests that --gmock_verbose=warning causes uninteresting calls to be
2144// reported.
2145TEST_F(GMockVerboseFlagTest, Warning) {
2146 GMOCK_FLAG(verbose) = kWarningVerbosity;
2147 TestExpectedCall(false);
zhanyong.wanc8965042013-03-01 07:10:07 +00002148 TestUninterestingCallOnNaggyMock(true);
shiqiane35fdd92008-12-10 05:08:54 +00002149}
2150
2151// Tests that --gmock_verbose=warning causes neither expected nor
2152// uninteresting calls to be reported.
2153TEST_F(GMockVerboseFlagTest, Error) {
2154 GMOCK_FLAG(verbose) = kErrorVerbosity;
2155 TestExpectedCall(false);
zhanyong.wanc8965042013-03-01 07:10:07 +00002156 TestUninterestingCallOnNaggyMock(false);
shiqiane35fdd92008-12-10 05:08:54 +00002157}
2158
2159// Tests that --gmock_verbose=SOME_INVALID_VALUE has the same effect
2160// as --gmock_verbose=warning.
2161TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) {
2162 GMOCK_FLAG(verbose) = "invalid"; // Treated as "warning".
2163 TestExpectedCall(false);
zhanyong.wanc8965042013-03-01 07:10:07 +00002164 TestUninterestingCallOnNaggyMock(true);
shiqiane35fdd92008-12-10 05:08:54 +00002165}
2166
zhanyong.wan2516f602010-08-31 18:28:02 +00002167#endif // GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +00002168
zhanyong.wan9413f2f2009-05-29 19:50:06 +00002169// A helper class that generates a failure when printed. We use it to
2170// ensure that Google Mock doesn't print a value (even to an internal
2171// buffer) when it is not supposed to do so.
2172class PrintMeNot {};
2173
2174void PrintTo(PrintMeNot /* dummy */, ::std::ostream* /* os */) {
2175 ADD_FAILURE() << "Google Mock is printing a value that shouldn't be "
2176 << "printed even to an internal buffer.";
2177}
2178
2179class LogTestHelper {
2180 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +00002181 LogTestHelper() {}
2182
zhanyong.wan9413f2f2009-05-29 19:50:06 +00002183 MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot));
zhanyong.wan32de5f52009-12-23 00:13:23 +00002184
2185 private:
2186 GTEST_DISALLOW_COPY_AND_ASSIGN_(LogTestHelper);
zhanyong.wan9413f2f2009-05-29 19:50:06 +00002187};
2188
vladlosev76c1c612010-05-05 19:47:46 +00002189class GMockLogTest : public VerboseFlagPreservingFixture {
zhanyong.wan9413f2f2009-05-29 19:50:06 +00002190 protected:
zhanyong.wan9413f2f2009-05-29 19:50:06 +00002191 LogTestHelper helper_;
zhanyong.wan9413f2f2009-05-29 19:50:06 +00002192};
2193
2194TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsWarning) {
2195 GMOCK_FLAG(verbose) = kWarningVerbosity;
2196 EXPECT_CALL(helper_, Foo(_))
2197 .WillOnce(Return(PrintMeNot()));
2198 helper_.Foo(PrintMeNot()); // This is an expected call.
2199}
2200
2201TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsError) {
2202 GMOCK_FLAG(verbose) = kErrorVerbosity;
2203 EXPECT_CALL(helper_, Foo(_))
2204 .WillOnce(Return(PrintMeNot()));
2205 helper_.Foo(PrintMeNot()); // This is an expected call.
2206}
2207
2208TEST_F(GMockLogTest, DoesNotPrintWarningInternallyIfVerbosityIsError) {
2209 GMOCK_FLAG(verbose) = kErrorVerbosity;
2210 ON_CALL(helper_, Foo(_))
2211 .WillByDefault(Return(PrintMeNot()));
2212 helper_.Foo(PrintMeNot()); // This should generate a warning.
2213}
2214
2215// Tests Mock::AllowLeak().
2216
zhanyong.wandf35a762009-04-22 22:25:31 +00002217TEST(AllowLeakTest, AllowsLeakingUnusedMockObject) {
2218 MockA* a = new MockA;
2219 Mock::AllowLeak(a);
2220}
2221
2222TEST(AllowLeakTest, CanBeCalledBeforeOnCall) {
2223 MockA* a = new MockA;
2224 Mock::AllowLeak(a);
2225 ON_CALL(*a, DoA(_)).WillByDefault(Return());
2226 a->DoA(0);
2227}
2228
2229TEST(AllowLeakTest, CanBeCalledAfterOnCall) {
2230 MockA* a = new MockA;
2231 ON_CALL(*a, DoA(_)).WillByDefault(Return());
2232 Mock::AllowLeak(a);
2233}
2234
2235TEST(AllowLeakTest, CanBeCalledBeforeExpectCall) {
2236 MockA* a = new MockA;
2237 Mock::AllowLeak(a);
2238 EXPECT_CALL(*a, DoA(_));
2239 a->DoA(0);
2240}
2241
2242TEST(AllowLeakTest, CanBeCalledAfterExpectCall) {
2243 MockA* a = new MockA;
2244 EXPECT_CALL(*a, DoA(_)).Times(AnyNumber());
2245 Mock::AllowLeak(a);
2246}
2247
2248TEST(AllowLeakTest, WorksWhenBothOnCallAndExpectCallArePresent) {
2249 MockA* a = new MockA;
2250 ON_CALL(*a, DoA(_)).WillByDefault(Return());
2251 EXPECT_CALL(*a, DoA(_)).Times(AnyNumber());
2252 Mock::AllowLeak(a);
2253}
shiqiane35fdd92008-12-10 05:08:54 +00002254
2255// Tests that we can verify and clear a mock object's expectations
2256// when none of its methods has expectations.
2257TEST(VerifyAndClearExpectationsTest, NoMethodHasExpectations) {
2258 MockB b;
2259 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
2260
2261 // There should be no expectations on the methods now, so we can
2262 // freely call them.
2263 EXPECT_EQ(0, b.DoB());
2264 EXPECT_EQ(0, b.DoB(1));
2265}
2266
2267// Tests that we can verify and clear a mock object's expectations
2268// when some, but not all, of its methods have expectations *and* the
2269// verification succeeds.
2270TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndSucceed) {
2271 MockB b;
2272 EXPECT_CALL(b, DoB())
2273 .WillOnce(Return(1));
2274 b.DoB();
2275 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
2276
2277 // There should be no expectations on the methods now, so we can
2278 // freely call them.
2279 EXPECT_EQ(0, b.DoB());
2280 EXPECT_EQ(0, b.DoB(1));
2281}
2282
2283// Tests that we can verify and clear a mock object's expectations
2284// when some, but not all, of its methods have expectations *and* the
2285// verification fails.
2286TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndFail) {
2287 MockB b;
2288 EXPECT_CALL(b, DoB())
2289 .WillOnce(Return(1));
vladlosev6c54a5e2009-10-21 06:15:34 +00002290 bool result = true;
shiqiane35fdd92008-12-10 05:08:54 +00002291 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b),
2292 "Actual: never called");
2293 ASSERT_FALSE(result);
2294
2295 // There should be no expectations on the methods now, so we can
2296 // freely call them.
2297 EXPECT_EQ(0, b.DoB());
2298 EXPECT_EQ(0, b.DoB(1));
2299}
2300
2301// Tests that we can verify and clear a mock object's expectations
2302// when all of its methods have expectations.
2303TEST(VerifyAndClearExpectationsTest, AllMethodsHaveExpectations) {
2304 MockB b;
2305 EXPECT_CALL(b, DoB())
2306 .WillOnce(Return(1));
2307 EXPECT_CALL(b, DoB(_))
2308 .WillOnce(Return(2));
2309 b.DoB();
2310 b.DoB(1);
2311 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
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 verify and clear a mock object's expectations
2320// when a method has more than one expectation.
2321TEST(VerifyAndClearExpectationsTest, AMethodHasManyExpectations) {
2322 MockB b;
2323 EXPECT_CALL(b, DoB(0))
2324 .WillOnce(Return(1));
2325 EXPECT_CALL(b, DoB(_))
2326 .WillOnce(Return(2));
2327 b.DoB(1);
vladlosev6c54a5e2009-10-21 06:15:34 +00002328 bool result = true;
shiqiane35fdd92008-12-10 05:08:54 +00002329 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b),
2330 "Actual: never called");
2331 ASSERT_FALSE(result);
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 call VerifyAndClearExpectations() on the same
2340// mock object multiple times.
2341TEST(VerifyAndClearExpectationsTest, CanCallManyTimes) {
2342 MockB b;
2343 EXPECT_CALL(b, DoB());
2344 b.DoB();
2345 Mock::VerifyAndClearExpectations(&b);
2346
2347 EXPECT_CALL(b, DoB(_))
2348 .WillOnce(Return(1));
2349 b.DoB(1);
2350 Mock::VerifyAndClearExpectations(&b);
2351 Mock::VerifyAndClearExpectations(&b);
2352
2353 // There should be no expectations on the methods now, so we can
2354 // freely call them.
2355 EXPECT_EQ(0, b.DoB());
2356 EXPECT_EQ(0, b.DoB(1));
2357}
2358
2359// Tests that we can clear a mock object's default actions when none
2360// of its methods has default actions.
2361TEST(VerifyAndClearTest, NoMethodHasDefaultActions) {
2362 MockB b;
2363 // If this crashes or generates a failure, the test will catch it.
2364 Mock::VerifyAndClear(&b);
2365 EXPECT_EQ(0, b.DoB());
2366}
2367
2368// Tests that we can clear a mock object's default actions when some,
2369// but not all of its methods have default actions.
2370TEST(VerifyAndClearTest, SomeMethodsHaveDefaultActions) {
2371 MockB b;
2372 ON_CALL(b, DoB())
2373 .WillByDefault(Return(1));
2374
2375 Mock::VerifyAndClear(&b);
2376
2377 // Verifies that the default action of int DoB() was removed.
2378 EXPECT_EQ(0, b.DoB());
2379}
2380
2381// Tests that we can clear a mock object's default actions when all of
2382// its methods have default actions.
2383TEST(VerifyAndClearTest, AllMethodsHaveDefaultActions) {
2384 MockB b;
2385 ON_CALL(b, DoB())
2386 .WillByDefault(Return(1));
2387 ON_CALL(b, DoB(_))
2388 .WillByDefault(Return(2));
2389
2390 Mock::VerifyAndClear(&b);
2391
2392 // Verifies that the default action of int DoB() was removed.
2393 EXPECT_EQ(0, b.DoB());
2394
2395 // Verifies that the default action of int DoB(int) was removed.
2396 EXPECT_EQ(0, b.DoB(0));
2397}
2398
2399// Tests that we can clear a mock object's default actions when a
2400// method has more than one ON_CALL() set on it.
2401TEST(VerifyAndClearTest, AMethodHasManyDefaultActions) {
2402 MockB b;
2403 ON_CALL(b, DoB(0))
2404 .WillByDefault(Return(1));
2405 ON_CALL(b, DoB(_))
2406 .WillByDefault(Return(2));
2407
2408 Mock::VerifyAndClear(&b);
2409
2410 // Verifies that the default actions (there are two) of int DoB(int)
2411 // were removed.
2412 EXPECT_EQ(0, b.DoB(0));
2413 EXPECT_EQ(0, b.DoB(1));
2414}
2415
2416// Tests that we can call VerifyAndClear() on a mock object multiple
2417// times.
2418TEST(VerifyAndClearTest, CanCallManyTimes) {
2419 MockB b;
2420 ON_CALL(b, DoB())
2421 .WillByDefault(Return(1));
2422 Mock::VerifyAndClear(&b);
2423 Mock::VerifyAndClear(&b);
2424
2425 ON_CALL(b, DoB(_))
2426 .WillByDefault(Return(1));
2427 Mock::VerifyAndClear(&b);
2428
2429 EXPECT_EQ(0, b.DoB());
2430 EXPECT_EQ(0, b.DoB(1));
2431}
2432
2433// Tests that VerifyAndClear() works when the verification succeeds.
2434TEST(VerifyAndClearTest, Success) {
2435 MockB b;
2436 ON_CALL(b, DoB())
2437 .WillByDefault(Return(1));
2438 EXPECT_CALL(b, DoB(1))
2439 .WillOnce(Return(2));
2440
2441 b.DoB();
2442 b.DoB(1);
2443 ASSERT_TRUE(Mock::VerifyAndClear(&b));
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 verification fails.
2452TEST(VerifyAndClearTest, Failure) {
2453 MockB b;
2454 ON_CALL(b, DoB(_))
2455 .WillByDefault(Return(1));
2456 EXPECT_CALL(b, DoB())
2457 .WillOnce(Return(2));
2458
2459 b.DoB(1);
vladlosev6c54a5e2009-10-21 06:15:34 +00002460 bool result = true;
shiqiane35fdd92008-12-10 05:08:54 +00002461 EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClear(&b),
2462 "Actual: never called");
2463 ASSERT_FALSE(result);
2464
2465 // There should be no expectations on the methods now, so we can
2466 // freely call them.
2467 EXPECT_EQ(0, b.DoB());
2468 EXPECT_EQ(0, b.DoB(1));
2469}
2470
2471// Tests that VerifyAndClear() works when the default actions and
2472// expectations are set on a const mock object.
2473TEST(VerifyAndClearTest, Const) {
2474 MockB b;
2475 ON_CALL(Const(b), DoB())
2476 .WillByDefault(Return(1));
2477
2478 EXPECT_CALL(Const(b), DoB())
2479 .WillOnce(DoDefault())
2480 .WillOnce(Return(2));
2481
2482 b.DoB();
2483 b.DoB();
2484 ASSERT_TRUE(Mock::VerifyAndClear(&b));
2485
2486 // There should be no expectations on the methods now, so we can
2487 // freely call them.
2488 EXPECT_EQ(0, b.DoB());
2489 EXPECT_EQ(0, b.DoB(1));
2490}
2491
2492// Tests that we can set default actions and expectations on a mock
2493// object after VerifyAndClear() has been called on it.
2494TEST(VerifyAndClearTest, CanSetDefaultActionsAndExpectationsAfterwards) {
2495 MockB b;
2496 ON_CALL(b, DoB())
2497 .WillByDefault(Return(1));
2498 EXPECT_CALL(b, DoB(_))
2499 .WillOnce(Return(2));
2500 b.DoB(1);
2501
2502 Mock::VerifyAndClear(&b);
2503
2504 EXPECT_CALL(b, DoB())
2505 .WillOnce(Return(3));
2506 ON_CALL(b, DoB(_))
2507 .WillByDefault(Return(4));
2508
2509 EXPECT_EQ(3, b.DoB());
2510 EXPECT_EQ(4, b.DoB(1));
2511}
2512
2513// Tests that calling VerifyAndClear() on one mock object does not
2514// affect other mock objects (either of the same type or not).
2515TEST(VerifyAndClearTest, DoesNotAffectOtherMockObjects) {
2516 MockA a;
2517 MockB b1;
2518 MockB b2;
2519
2520 ON_CALL(a, Binary(_, _))
2521 .WillByDefault(Return(true));
2522 EXPECT_CALL(a, Binary(_, _))
2523 .WillOnce(DoDefault())
2524 .WillOnce(Return(false));
2525
2526 ON_CALL(b1, DoB())
2527 .WillByDefault(Return(1));
2528 EXPECT_CALL(b1, DoB(_))
2529 .WillOnce(Return(2));
2530
2531 ON_CALL(b2, DoB())
2532 .WillByDefault(Return(3));
2533 EXPECT_CALL(b2, DoB(_));
2534
2535 b2.DoB(0);
2536 Mock::VerifyAndClear(&b2);
2537
2538 // Verifies that the default actions and expectations of a and b1
2539 // are still in effect.
2540 EXPECT_TRUE(a.Binary(0, 0));
2541 EXPECT_FALSE(a.Binary(0, 0));
2542
2543 EXPECT_EQ(1, b1.DoB());
2544 EXPECT_EQ(2, b1.DoB(0));
2545}
2546
vladlosev9bcb5f92011-10-24 23:41:07 +00002547TEST(VerifyAndClearTest,
2548 DestroyingChainedMocksDoesNotDeadlockThroughExpectations) {
2549 linked_ptr<MockA> a(new MockA);
2550 ReferenceHoldingMock test_mock;
2551
2552 // EXPECT_CALL stores a reference to a inside test_mock.
2553 EXPECT_CALL(test_mock, AcceptReference(_))
2554 .WillRepeatedly(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
2567TEST(VerifyAndClearTest,
2568 DestroyingChainedMocksDoesNotDeadlockThroughDefaultAction) {
2569 linked_ptr<MockA> a(new MockA);
2570 ReferenceHoldingMock test_mock;
2571
2572 // ON_CALL stores a reference to a inside test_mock.
2573 ON_CALL(test_mock, AcceptReference(_))
2574 .WillByDefault(SetArgPointee<0>(a));
2575
2576 // Throw away the reference to the mock that we have in a. After this, the
2577 // only reference to it is stored by test_mock.
2578 a.reset();
2579
2580 // When test_mock goes out of scope, it destroys the last remaining reference
2581 // to the mock object originally pointed to by a. This will cause the MockA
2582 // destructor to be called from inside the ReferenceHoldingMock destructor.
2583 // The state of all mocks is protected by a single global lock, but there
2584 // should be no deadlock.
2585}
2586
shiqiane35fdd92008-12-10 05:08:54 +00002587// Tests that a mock function's action can call a mock function
2588// (either the same function or a different one) either as an explicit
2589// action or as a default action without causing a dead lock. It
2590// verifies that the action is not performed inside the critical
2591// section.
vladlosev54af9ba2010-05-04 16:05:11 +00002592TEST(SynchronizationTest, CanCallMockMethodInAction) {
2593 MockA a;
2594 MockC c;
2595 ON_CALL(a, DoA(_))
2596 .WillByDefault(IgnoreResult(InvokeWithoutArgs(&c,
2597 &MockC::NonVoidMethod)));
2598 EXPECT_CALL(a, DoA(1));
2599 EXPECT_CALL(a, DoA(1))
2600 .WillOnce(Invoke(&a, &MockA::DoA))
2601 .RetiresOnSaturation();
2602 EXPECT_CALL(c, NonVoidMethod());
shiqiane35fdd92008-12-10 05:08:54 +00002603
vladlosev54af9ba2010-05-04 16:05:11 +00002604 a.DoA(1);
2605 // This will match the second EXPECT_CALL() and trigger another a.DoA(1),
2606 // which will in turn match the first EXPECT_CALL() and trigger a call to
2607 // c.NonVoidMethod() that was specified by the ON_CALL() since the first
2608 // EXPECT_CALL() did not specify an action.
shiqiane35fdd92008-12-10 05:08:54 +00002609}
2610
2611} // namespace
zhanyong.wandf35a762009-04-22 22:25:31 +00002612
zhanyong.wan9571b282009-08-07 07:15:56 +00002613// Allows the user to define his own main and then invoke gmock_main
2614// from it. This might be necessary on some platforms which require
2615// specific setup and teardown.
2616#if GMOCK_RENAME_MAIN
2617int gmock_main(int argc, char **argv) {
2618#else
zhanyong.wandf35a762009-04-22 22:25:31 +00002619int main(int argc, char **argv) {
zhanyong.wan9571b282009-08-07 07:15:56 +00002620#endif // GMOCK_RENAME_MAIN
zhanyong.wandf35a762009-04-22 22:25:31 +00002621 testing::InitGoogleMock(&argc, argv);
2622
2623 // Ensures that the tests pass no matter what value of
2624 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
2625 testing::GMOCK_FLAG(catch_leaked_mocks) = true;
2626 testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity;
2627
2628 return RUN_ALL_TESTS();
2629}