1b89a7cc2SEnji Cooper // Copyright 2007, Google Inc.
2b89a7cc2SEnji Cooper // All rights reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper // * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper // * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper // * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b89a7cc2SEnji Cooper
30b89a7cc2SEnji Cooper // Google Mock - a framework for writing C++ mock classes.
31b89a7cc2SEnji Cooper //
32b89a7cc2SEnji Cooper // This file tests the spec builder syntax.
33b89a7cc2SEnji Cooper
34b89a7cc2SEnji Cooper #include "gmock/gmock-spec-builders.h"
35b89a7cc2SEnji Cooper
3628f6c2f2SEnji Cooper #include <memory>
37b89a7cc2SEnji Cooper #include <ostream> // NOLINT
38b89a7cc2SEnji Cooper #include <sstream>
39b89a7cc2SEnji Cooper #include <string>
4028f6c2f2SEnji Cooper #include <type_traits>
41b89a7cc2SEnji Cooper
42b89a7cc2SEnji Cooper #include "gmock/gmock.h"
43b89a7cc2SEnji Cooper #include "gmock/internal/gmock-port.h"
44b89a7cc2SEnji Cooper #include "gtest/gtest-spi.h"
4528f6c2f2SEnji Cooper #include "gtest/gtest.h"
46b89a7cc2SEnji Cooper #include "gtest/internal/gtest-port.h"
47b89a7cc2SEnji Cooper
48b89a7cc2SEnji Cooper namespace testing {
49b89a7cc2SEnji Cooper namespace {
50b89a7cc2SEnji Cooper
5128f6c2f2SEnji Cooper using ::testing::internal::FormatFileLocation;
5228f6c2f2SEnji Cooper using ::testing::internal::kAllow;
5328f6c2f2SEnji Cooper using ::testing::internal::kErrorVerbosity;
5428f6c2f2SEnji Cooper using ::testing::internal::kFail;
5528f6c2f2SEnji Cooper using ::testing::internal::kInfoVerbosity;
5628f6c2f2SEnji Cooper using ::testing::internal::kWarn;
5728f6c2f2SEnji Cooper using ::testing::internal::kWarningVerbosity;
58b89a7cc2SEnji Cooper
59b89a7cc2SEnji Cooper #if GTEST_HAS_STREAM_REDIRECTION
6028f6c2f2SEnji Cooper using ::testing::internal::CaptureStdout;
6128f6c2f2SEnji Cooper using ::testing::internal::GetCapturedStdout;
62b89a7cc2SEnji Cooper #endif
63b89a7cc2SEnji Cooper
64c950e612SDimitry Andric class Incomplete {
65c950e612SDimitry Andric };
66b89a7cc2SEnji Cooper
67b89a7cc2SEnji Cooper class MockIncomplete {
68b89a7cc2SEnji Cooper public:
69b89a7cc2SEnji Cooper // This line verifies that a mock method can take a by-reference
70b89a7cc2SEnji Cooper // argument of an incomplete type.
71b89a7cc2SEnji Cooper MOCK_METHOD1(ByRefFunc, void(const Incomplete& x));
72b89a7cc2SEnji Cooper };
73b89a7cc2SEnji Cooper
74b89a7cc2SEnji Cooper // Tells Google Mock how to print a value of type Incomplete.
75b89a7cc2SEnji Cooper void PrintTo(const Incomplete& x, ::std::ostream* os);
76b89a7cc2SEnji Cooper
TEST(MockMethodTest,CanInstantiateWithIncompleteArgType)77b89a7cc2SEnji Cooper TEST(MockMethodTest, CanInstantiateWithIncompleteArgType) {
78b89a7cc2SEnji Cooper // Even though this mock class contains a mock method that takes
79b89a7cc2SEnji Cooper // by-reference an argument whose type is incomplete, we can still
80b89a7cc2SEnji Cooper // use the mock, as long as Google Mock knows how to print the
81b89a7cc2SEnji Cooper // argument.
82b89a7cc2SEnji Cooper MockIncomplete incomplete;
8328f6c2f2SEnji Cooper EXPECT_CALL(incomplete, ByRefFunc(_)).Times(AnyNumber());
84b89a7cc2SEnji Cooper }
85b89a7cc2SEnji Cooper
86b89a7cc2SEnji Cooper // The definition of the printer for the argument type doesn't have to
87b89a7cc2SEnji Cooper // be visible where the mock is used.
PrintTo(const Incomplete &,::std::ostream * os)88b89a7cc2SEnji Cooper void PrintTo(const Incomplete& /* x */, ::std::ostream* os) {
89b89a7cc2SEnji Cooper *os << "incomplete";
90b89a7cc2SEnji Cooper }
91b89a7cc2SEnji Cooper
92b89a7cc2SEnji Cooper class Result {};
93b89a7cc2SEnji Cooper
94b89a7cc2SEnji Cooper // A type that's not default constructible.
95b89a7cc2SEnji Cooper class NonDefaultConstructible {
96b89a7cc2SEnji Cooper public:
NonDefaultConstructible(int)97b89a7cc2SEnji Cooper explicit NonDefaultConstructible(int /* dummy */) {}
98b89a7cc2SEnji Cooper };
99b89a7cc2SEnji Cooper
100b89a7cc2SEnji Cooper class MockA {
101b89a7cc2SEnji Cooper public:
10228f6c2f2SEnji Cooper MockA() = default;
103b89a7cc2SEnji Cooper
104b89a7cc2SEnji Cooper MOCK_METHOD1(DoA, void(int n));
105b89a7cc2SEnji Cooper MOCK_METHOD1(ReturnResult, Result(int n));
106b89a7cc2SEnji Cooper MOCK_METHOD0(ReturnNonDefaultConstructible, NonDefaultConstructible());
107b89a7cc2SEnji Cooper MOCK_METHOD2(Binary, bool(int x, int y));
108b89a7cc2SEnji Cooper MOCK_METHOD2(ReturnInt, int(int x, int y));
109b89a7cc2SEnji Cooper
110b89a7cc2SEnji Cooper private:
11128f6c2f2SEnji Cooper MockA(const MockA&) = delete;
11228f6c2f2SEnji Cooper MockA& operator=(const MockA&) = delete;
113b89a7cc2SEnji Cooper };
114b89a7cc2SEnji Cooper
115b89a7cc2SEnji Cooper class MockB {
116b89a7cc2SEnji Cooper public:
11728f6c2f2SEnji Cooper MockB() = default;
118b89a7cc2SEnji Cooper
119b89a7cc2SEnji Cooper MOCK_CONST_METHOD0(DoB, int()); // NOLINT
120b89a7cc2SEnji Cooper MOCK_METHOD1(DoB, int(int n)); // NOLINT
121b89a7cc2SEnji Cooper
122b89a7cc2SEnji Cooper private:
12328f6c2f2SEnji Cooper MockB(const MockB&) = delete;
12428f6c2f2SEnji Cooper MockB& operator=(const MockB&) = delete;
125b89a7cc2SEnji Cooper };
126b89a7cc2SEnji Cooper
127b89a7cc2SEnji Cooper class ReferenceHoldingMock {
128b89a7cc2SEnji Cooper public:
12928f6c2f2SEnji Cooper ReferenceHoldingMock() = default;
130b89a7cc2SEnji Cooper
13128f6c2f2SEnji Cooper MOCK_METHOD1(AcceptReference, void(std::shared_ptr<MockA>*));
132b89a7cc2SEnji Cooper
133b89a7cc2SEnji Cooper private:
13428f6c2f2SEnji Cooper ReferenceHoldingMock(const ReferenceHoldingMock&) = delete;
13528f6c2f2SEnji Cooper ReferenceHoldingMock& operator=(const ReferenceHoldingMock&) = delete;
136b89a7cc2SEnji Cooper };
137b89a7cc2SEnji Cooper
138b89a7cc2SEnji Cooper // Tests that EXPECT_CALL and ON_CALL compile in a presence of macro
139b89a7cc2SEnji Cooper // redefining a mock method name. This could happen, for example, when
140b89a7cc2SEnji Cooper // the tested code #includes Win32 API headers which define many APIs
141b89a7cc2SEnji Cooper // as macros, e.g. #define TextOut TextOutW.
142b89a7cc2SEnji Cooper
143b89a7cc2SEnji Cooper #define Method MethodW
144b89a7cc2SEnji Cooper
145b89a7cc2SEnji Cooper class CC {
146b89a7cc2SEnji Cooper public:
14728f6c2f2SEnji Cooper virtual ~CC() = default;
148b89a7cc2SEnji Cooper virtual int Method() = 0;
149b89a7cc2SEnji Cooper };
150b89a7cc2SEnji Cooper class MockCC : public CC {
151b89a7cc2SEnji Cooper public:
15228f6c2f2SEnji Cooper MockCC() = default;
153b89a7cc2SEnji Cooper
154b89a7cc2SEnji Cooper MOCK_METHOD0(Method, int());
155b89a7cc2SEnji Cooper
156b89a7cc2SEnji Cooper private:
15728f6c2f2SEnji Cooper MockCC(const MockCC&) = delete;
15828f6c2f2SEnji Cooper MockCC& operator=(const MockCC&) = delete;
159b89a7cc2SEnji Cooper };
160b89a7cc2SEnji Cooper
161b89a7cc2SEnji Cooper // Tests that a method with expanded name compiles.
TEST(OnCallSyntaxTest,CompilesWithMethodNameExpandedFromMacro)162b89a7cc2SEnji Cooper TEST(OnCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) {
163b89a7cc2SEnji Cooper MockCC cc;
164b89a7cc2SEnji Cooper ON_CALL(cc, Method());
165b89a7cc2SEnji Cooper }
166b89a7cc2SEnji Cooper
167b89a7cc2SEnji Cooper // Tests that the method with expanded name not only compiles but runs
168b89a7cc2SEnji Cooper // and returns a correct value, too.
TEST(OnCallSyntaxTest,WorksWithMethodNameExpandedFromMacro)169b89a7cc2SEnji Cooper TEST(OnCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) {
170b89a7cc2SEnji Cooper MockCC cc;
171b89a7cc2SEnji Cooper ON_CALL(cc, Method()).WillByDefault(Return(42));
172b89a7cc2SEnji Cooper EXPECT_EQ(42, cc.Method());
173b89a7cc2SEnji Cooper }
174b89a7cc2SEnji Cooper
175b89a7cc2SEnji Cooper // Tests that a method with expanded name compiles.
TEST(ExpectCallSyntaxTest,CompilesWithMethodNameExpandedFromMacro)176b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) {
177b89a7cc2SEnji Cooper MockCC cc;
178b89a7cc2SEnji Cooper EXPECT_CALL(cc, Method());
179b89a7cc2SEnji Cooper cc.Method();
180b89a7cc2SEnji Cooper }
181b89a7cc2SEnji Cooper
182b89a7cc2SEnji Cooper // Tests that it works, too.
TEST(ExpectCallSyntaxTest,WorksWithMethodNameExpandedFromMacro)183b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) {
184b89a7cc2SEnji Cooper MockCC cc;
185b89a7cc2SEnji Cooper EXPECT_CALL(cc, Method()).WillOnce(Return(42));
186b89a7cc2SEnji Cooper EXPECT_EQ(42, cc.Method());
187b89a7cc2SEnji Cooper }
188b89a7cc2SEnji Cooper
189b89a7cc2SEnji Cooper #undef Method // Done with macro redefinition tests.
190b89a7cc2SEnji Cooper
191b89a7cc2SEnji Cooper // Tests that ON_CALL evaluates its arguments exactly once as promised
192b89a7cc2SEnji Cooper // by Google Mock.
TEST(OnCallSyntaxTest,EvaluatesFirstArgumentOnce)193b89a7cc2SEnji Cooper TEST(OnCallSyntaxTest, EvaluatesFirstArgumentOnce) {
194b89a7cc2SEnji Cooper MockA a;
195b89a7cc2SEnji Cooper MockA* pa = &a;
196b89a7cc2SEnji Cooper
197b89a7cc2SEnji Cooper ON_CALL(*pa++, DoA(_));
198b89a7cc2SEnji Cooper EXPECT_EQ(&a + 1, pa);
199b89a7cc2SEnji Cooper }
200b89a7cc2SEnji Cooper
TEST(OnCallSyntaxTest,EvaluatesSecondArgumentOnce)201b89a7cc2SEnji Cooper TEST(OnCallSyntaxTest, EvaluatesSecondArgumentOnce) {
202b89a7cc2SEnji Cooper MockA a;
203b89a7cc2SEnji Cooper int n = 0;
204b89a7cc2SEnji Cooper
205b89a7cc2SEnji Cooper ON_CALL(a, DoA(n++));
206b89a7cc2SEnji Cooper EXPECT_EQ(1, n);
207b89a7cc2SEnji Cooper }
208b89a7cc2SEnji Cooper
209b89a7cc2SEnji Cooper // Tests that the syntax of ON_CALL() is enforced at run time.
210b89a7cc2SEnji Cooper
TEST(OnCallSyntaxTest,WithIsOptional)211b89a7cc2SEnji Cooper TEST(OnCallSyntaxTest, WithIsOptional) {
212b89a7cc2SEnji Cooper MockA a;
213b89a7cc2SEnji Cooper
21428f6c2f2SEnji Cooper ON_CALL(a, DoA(5)).WillByDefault(Return());
21528f6c2f2SEnji Cooper ON_CALL(a, DoA(_)).With(_).WillByDefault(Return());
216b89a7cc2SEnji Cooper }
217b89a7cc2SEnji Cooper
TEST(OnCallSyntaxTest,WithCanAppearAtMostOnce)218b89a7cc2SEnji Cooper TEST(OnCallSyntaxTest, WithCanAppearAtMostOnce) {
219b89a7cc2SEnji Cooper MockA a;
220b89a7cc2SEnji Cooper
22128f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
22228f6c2f2SEnji Cooper { // NOLINT
223b89a7cc2SEnji Cooper ON_CALL(a, ReturnResult(_))
224b89a7cc2SEnji Cooper .With(_)
225b89a7cc2SEnji Cooper .With(_)
226b89a7cc2SEnji Cooper .WillByDefault(Return(Result()));
22728f6c2f2SEnji Cooper },
22828f6c2f2SEnji Cooper ".With() cannot appear more than once in an ON_CALL()");
229b89a7cc2SEnji Cooper }
230b89a7cc2SEnji Cooper
TEST(OnCallSyntaxTest,WillByDefaultIsMandatory)231b89a7cc2SEnji Cooper TEST(OnCallSyntaxTest, WillByDefaultIsMandatory) {
232b89a7cc2SEnji Cooper MockA a;
233b89a7cc2SEnji Cooper
23428f6c2f2SEnji Cooper EXPECT_DEATH_IF_SUPPORTED(
23528f6c2f2SEnji Cooper {
236b89a7cc2SEnji Cooper ON_CALL(a, DoA(5));
237b89a7cc2SEnji Cooper a.DoA(5);
23828f6c2f2SEnji Cooper },
23928f6c2f2SEnji Cooper "");
240b89a7cc2SEnji Cooper }
241b89a7cc2SEnji Cooper
TEST(OnCallSyntaxTest,WillByDefaultCanAppearAtMostOnce)242b89a7cc2SEnji Cooper TEST(OnCallSyntaxTest, WillByDefaultCanAppearAtMostOnce) {
243b89a7cc2SEnji Cooper MockA a;
244b89a7cc2SEnji Cooper
24528f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
24628f6c2f2SEnji Cooper { // NOLINT
24728f6c2f2SEnji Cooper ON_CALL(a, DoA(5)).WillByDefault(Return()).WillByDefault(Return());
24828f6c2f2SEnji Cooper },
24928f6c2f2SEnji Cooper ".WillByDefault() must appear exactly once in an ON_CALL()");
250b89a7cc2SEnji Cooper }
251b89a7cc2SEnji Cooper
252b89a7cc2SEnji Cooper // Tests that EXPECT_CALL evaluates its arguments exactly once as
253b89a7cc2SEnji Cooper // promised by Google Mock.
TEST(ExpectCallSyntaxTest,EvaluatesFirstArgumentOnce)254b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, EvaluatesFirstArgumentOnce) {
255b89a7cc2SEnji Cooper MockA a;
256b89a7cc2SEnji Cooper MockA* pa = &a;
257b89a7cc2SEnji Cooper
258b89a7cc2SEnji Cooper EXPECT_CALL(*pa++, DoA(_));
259b89a7cc2SEnji Cooper a.DoA(0);
260b89a7cc2SEnji Cooper EXPECT_EQ(&a + 1, pa);
261b89a7cc2SEnji Cooper }
262b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,EvaluatesSecondArgumentOnce)263b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, EvaluatesSecondArgumentOnce) {
264b89a7cc2SEnji Cooper MockA a;
265b89a7cc2SEnji Cooper int n = 0;
266b89a7cc2SEnji Cooper
267b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(n++));
268b89a7cc2SEnji Cooper a.DoA(0);
269b89a7cc2SEnji Cooper EXPECT_EQ(1, n);
270b89a7cc2SEnji Cooper }
271b89a7cc2SEnji Cooper
272b89a7cc2SEnji Cooper // Tests that the syntax of EXPECT_CALL() is enforced at run time.
273b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,WithIsOptional)274b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, WithIsOptional) {
275b89a7cc2SEnji Cooper MockA a;
276b89a7cc2SEnji Cooper
27728f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(5)).Times(0);
27828f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(6)).With(_).Times(0);
279b89a7cc2SEnji Cooper }
280b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,WithCanAppearAtMostOnce)281b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, WithCanAppearAtMostOnce) {
282b89a7cc2SEnji Cooper MockA a;
283b89a7cc2SEnji Cooper
28428f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
28528f6c2f2SEnji Cooper { // NOLINT
28628f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(6)).With(_).With(_);
28728f6c2f2SEnji Cooper },
28828f6c2f2SEnji Cooper ".With() cannot appear more than once in an EXPECT_CALL()");
289b89a7cc2SEnji Cooper
290b89a7cc2SEnji Cooper a.DoA(6);
291b89a7cc2SEnji Cooper }
292b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,WithMustBeFirstClause)293b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, WithMustBeFirstClause) {
294b89a7cc2SEnji Cooper MockA a;
295b89a7cc2SEnji Cooper
29628f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
29728f6c2f2SEnji Cooper { // NOLINT
29828f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).Times(1).With(_);
29928f6c2f2SEnji Cooper },
30028f6c2f2SEnji Cooper ".With() must be the first clause in an EXPECT_CALL()");
301b89a7cc2SEnji Cooper
302b89a7cc2SEnji Cooper a.DoA(1);
303b89a7cc2SEnji Cooper
30428f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
30528f6c2f2SEnji Cooper { // NOLINT
30628f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(2)).WillOnce(Return()).With(_);
30728f6c2f2SEnji Cooper },
30828f6c2f2SEnji Cooper ".With() must be the first clause in an EXPECT_CALL()");
309b89a7cc2SEnji Cooper
310b89a7cc2SEnji Cooper a.DoA(2);
311b89a7cc2SEnji Cooper }
312b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,TimesCanBeInferred)313b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, TimesCanBeInferred) {
314b89a7cc2SEnji Cooper MockA a;
315b89a7cc2SEnji Cooper
31628f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).WillOnce(Return());
317b89a7cc2SEnji Cooper
31828f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(2)).WillOnce(Return()).WillRepeatedly(Return());
319b89a7cc2SEnji Cooper
320b89a7cc2SEnji Cooper a.DoA(1);
321b89a7cc2SEnji Cooper a.DoA(2);
322b89a7cc2SEnji Cooper a.DoA(2);
323b89a7cc2SEnji Cooper }
324b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,TimesCanAppearAtMostOnce)325b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, TimesCanAppearAtMostOnce) {
326b89a7cc2SEnji Cooper MockA a;
327b89a7cc2SEnji Cooper
32828f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
32928f6c2f2SEnji Cooper { // NOLINT
33028f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).Times(1).Times(2);
33128f6c2f2SEnji Cooper },
33228f6c2f2SEnji Cooper ".Times() cannot appear more than once in an EXPECT_CALL()");
333b89a7cc2SEnji Cooper
334b89a7cc2SEnji Cooper a.DoA(1);
335b89a7cc2SEnji Cooper a.DoA(1);
336b89a7cc2SEnji Cooper }
337b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,TimesMustBeBeforeInSequence)338b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, TimesMustBeBeforeInSequence) {
339b89a7cc2SEnji Cooper MockA a;
340b89a7cc2SEnji Cooper Sequence s;
341b89a7cc2SEnji Cooper
34228f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
34328f6c2f2SEnji Cooper { // NOLINT
34428f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).InSequence(s).Times(1);
34528f6c2f2SEnji Cooper },
34628f6c2f2SEnji Cooper ".Times() may only appear *before* ");
347b89a7cc2SEnji Cooper
348b89a7cc2SEnji Cooper a.DoA(1);
349b89a7cc2SEnji Cooper }
350b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,InSequenceIsOptional)351b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, InSequenceIsOptional) {
352b89a7cc2SEnji Cooper MockA a;
353b89a7cc2SEnji Cooper Sequence s;
354b89a7cc2SEnji Cooper
355b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1));
35628f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(2)).InSequence(s);
357b89a7cc2SEnji Cooper
358b89a7cc2SEnji Cooper a.DoA(1);
359b89a7cc2SEnji Cooper a.DoA(2);
360b89a7cc2SEnji Cooper }
361b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,InSequenceCanAppearMultipleTimes)362b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, InSequenceCanAppearMultipleTimes) {
363b89a7cc2SEnji Cooper MockA a;
364b89a7cc2SEnji Cooper Sequence s1, s2;
365b89a7cc2SEnji Cooper
36628f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).InSequence(s1, s2).InSequence(s1);
367b89a7cc2SEnji Cooper
368b89a7cc2SEnji Cooper a.DoA(1);
369b89a7cc2SEnji Cooper }
370b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,InSequenceMustBeBeforeAfter)371b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeAfter) {
372b89a7cc2SEnji Cooper MockA a;
373b89a7cc2SEnji Cooper Sequence s;
374b89a7cc2SEnji Cooper
37528f6c2f2SEnji Cooper Expectation e = EXPECT_CALL(a, DoA(1)).Times(AnyNumber());
37628f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
37728f6c2f2SEnji Cooper { // NOLINT
37828f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(2)).After(e).InSequence(s);
37928f6c2f2SEnji Cooper },
38028f6c2f2SEnji Cooper ".InSequence() cannot appear after ");
381b89a7cc2SEnji Cooper
382b89a7cc2SEnji Cooper a.DoA(2);
383b89a7cc2SEnji Cooper }
384b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,InSequenceMustBeBeforeWillOnce)385b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWillOnce) {
386b89a7cc2SEnji Cooper MockA a;
387b89a7cc2SEnji Cooper Sequence s;
388b89a7cc2SEnji Cooper
38928f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
39028f6c2f2SEnji Cooper { // NOLINT
39128f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).WillOnce(Return()).InSequence(s);
39228f6c2f2SEnji Cooper },
39328f6c2f2SEnji Cooper ".InSequence() cannot appear after ");
394b89a7cc2SEnji Cooper
395b89a7cc2SEnji Cooper a.DoA(1);
396b89a7cc2SEnji Cooper }
397b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,AfterMustBeBeforeWillOnce)398b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, AfterMustBeBeforeWillOnce) {
399b89a7cc2SEnji Cooper MockA a;
400b89a7cc2SEnji Cooper
401b89a7cc2SEnji Cooper Expectation e = EXPECT_CALL(a, DoA(1));
40228f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
40328f6c2f2SEnji Cooper { EXPECT_CALL(a, DoA(2)).WillOnce(Return()).After(e); },
40428f6c2f2SEnji Cooper ".After() cannot appear after ");
405b89a7cc2SEnji Cooper
406b89a7cc2SEnji Cooper a.DoA(1);
407b89a7cc2SEnji Cooper a.DoA(2);
408b89a7cc2SEnji Cooper }
409b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,WillIsOptional)410b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, WillIsOptional) {
411b89a7cc2SEnji Cooper MockA a;
412b89a7cc2SEnji Cooper
413b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1));
41428f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(2)).WillOnce(Return());
415b89a7cc2SEnji Cooper
416b89a7cc2SEnji Cooper a.DoA(1);
417b89a7cc2SEnji Cooper a.DoA(2);
418b89a7cc2SEnji Cooper }
419b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,WillCanAppearMultipleTimes)420b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, WillCanAppearMultipleTimes) {
421b89a7cc2SEnji Cooper MockA a;
422b89a7cc2SEnji Cooper
423b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1))
424b89a7cc2SEnji Cooper .Times(AnyNumber())
425b89a7cc2SEnji Cooper .WillOnce(Return())
426b89a7cc2SEnji Cooper .WillOnce(Return())
427b89a7cc2SEnji Cooper .WillOnce(Return());
428b89a7cc2SEnji Cooper }
429b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,WillMustBeBeforeWillRepeatedly)430b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, WillMustBeBeforeWillRepeatedly) {
431b89a7cc2SEnji Cooper MockA a;
432b89a7cc2SEnji Cooper
43328f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
43428f6c2f2SEnji Cooper { // NOLINT
43528f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).WillRepeatedly(Return()).WillOnce(Return());
43628f6c2f2SEnji Cooper },
43728f6c2f2SEnji Cooper ".WillOnce() cannot appear after ");
438b89a7cc2SEnji Cooper
439b89a7cc2SEnji Cooper a.DoA(1);
440b89a7cc2SEnji Cooper }
441b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,WillRepeatedlyIsOptional)442b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, WillRepeatedlyIsOptional) {
443b89a7cc2SEnji Cooper MockA a;
444b89a7cc2SEnji Cooper
44528f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).WillOnce(Return());
44628f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(2)).WillOnce(Return()).WillRepeatedly(Return());
447b89a7cc2SEnji Cooper
448b89a7cc2SEnji Cooper a.DoA(1);
449b89a7cc2SEnji Cooper a.DoA(2);
450b89a7cc2SEnji Cooper a.DoA(2);
451b89a7cc2SEnji Cooper }
452b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,WillRepeatedlyCannotAppearMultipleTimes)453b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, WillRepeatedlyCannotAppearMultipleTimes) {
454b89a7cc2SEnji Cooper MockA a;
455b89a7cc2SEnji Cooper
45628f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
45728f6c2f2SEnji Cooper { // NOLINT
45828f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).WillRepeatedly(Return()).WillRepeatedly(
45928f6c2f2SEnji Cooper Return());
46028f6c2f2SEnji Cooper },
46128f6c2f2SEnji Cooper ".WillRepeatedly() cannot appear more than once in an "
462b89a7cc2SEnji Cooper "EXPECT_CALL()");
463b89a7cc2SEnji Cooper }
464b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,WillRepeatedlyMustBeBeforeRetiresOnSaturation)465b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, WillRepeatedlyMustBeBeforeRetiresOnSaturation) {
466b89a7cc2SEnji Cooper MockA a;
467b89a7cc2SEnji Cooper
46828f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
46928f6c2f2SEnji Cooper { // NOLINT
47028f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).RetiresOnSaturation().WillRepeatedly(Return());
47128f6c2f2SEnji Cooper },
47228f6c2f2SEnji Cooper ".WillRepeatedly() cannot appear after ");
473b89a7cc2SEnji Cooper }
474b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,RetiresOnSaturationIsOptional)475b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, RetiresOnSaturationIsOptional) {
476b89a7cc2SEnji Cooper MockA a;
477b89a7cc2SEnji Cooper
478b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1));
47928f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).RetiresOnSaturation();
480b89a7cc2SEnji Cooper
481b89a7cc2SEnji Cooper a.DoA(1);
482b89a7cc2SEnji Cooper a.DoA(1);
483b89a7cc2SEnji Cooper }
484b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,RetiresOnSaturationCannotAppearMultipleTimes)485b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, RetiresOnSaturationCannotAppearMultipleTimes) {
486b89a7cc2SEnji Cooper MockA a;
487b89a7cc2SEnji Cooper
48828f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
48928f6c2f2SEnji Cooper { // NOLINT
49028f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).RetiresOnSaturation().RetiresOnSaturation();
49128f6c2f2SEnji Cooper },
49228f6c2f2SEnji Cooper ".RetiresOnSaturation() cannot appear more than once");
493b89a7cc2SEnji Cooper
494b89a7cc2SEnji Cooper a.DoA(1);
495b89a7cc2SEnji Cooper }
496b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,DefaultCardinalityIsOnce)497b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, DefaultCardinalityIsOnce) {
498b89a7cc2SEnji Cooper {
499b89a7cc2SEnji Cooper MockA a;
500b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1));
501b89a7cc2SEnji Cooper a.DoA(1);
502b89a7cc2SEnji Cooper }
50328f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
50428f6c2f2SEnji Cooper { // NOLINT
505b89a7cc2SEnji Cooper MockA a;
506b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1));
50728f6c2f2SEnji Cooper },
50828f6c2f2SEnji Cooper "to be called once");
50928f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
51028f6c2f2SEnji Cooper { // NOLINT
511b89a7cc2SEnji Cooper MockA a;
512b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1));
513b89a7cc2SEnji Cooper a.DoA(1);
514b89a7cc2SEnji Cooper a.DoA(1);
51528f6c2f2SEnji Cooper },
51628f6c2f2SEnji Cooper "to be called once");
517b89a7cc2SEnji Cooper }
518b89a7cc2SEnji Cooper
519b89a7cc2SEnji Cooper #if GTEST_HAS_STREAM_REDIRECTION
520b89a7cc2SEnji Cooper
521b89a7cc2SEnji Cooper // Tests that Google Mock doesn't print a warning when the number of
522b89a7cc2SEnji Cooper // WillOnce() is adequate.
TEST(ExpectCallSyntaxTest,DoesNotWarnOnAdequateActionCount)523b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, DoesNotWarnOnAdequateActionCount) {
524b89a7cc2SEnji Cooper CaptureStdout();
525b89a7cc2SEnji Cooper {
526b89a7cc2SEnji Cooper MockB b;
527b89a7cc2SEnji Cooper
528b89a7cc2SEnji Cooper // It's always fine to omit WillOnce() entirely.
52928f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).Times(0);
53028f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(1)).Times(AtMost(1));
53128f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(2)).Times(1).WillRepeatedly(Return(1));
532b89a7cc2SEnji Cooper
533b89a7cc2SEnji Cooper // It's fine for the number of WillOnce()s to equal the upper bound.
534b89a7cc2SEnji Cooper EXPECT_CALL(b, DoB(3))
535b89a7cc2SEnji Cooper .Times(Between(1, 2))
536b89a7cc2SEnji Cooper .WillOnce(Return(1))
537b89a7cc2SEnji Cooper .WillOnce(Return(2));
538b89a7cc2SEnji Cooper
539b89a7cc2SEnji Cooper // It's fine for the number of WillOnce()s to be smaller than the
540b89a7cc2SEnji Cooper // upper bound when there is a WillRepeatedly().
54128f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(4)).Times(AtMost(3)).WillOnce(Return(1)).WillRepeatedly(
54228f6c2f2SEnji Cooper Return(2));
543b89a7cc2SEnji Cooper
544b89a7cc2SEnji Cooper // Satisfies the above expectations.
545b89a7cc2SEnji Cooper b.DoB(2);
546b89a7cc2SEnji Cooper b.DoB(3);
547b89a7cc2SEnji Cooper }
548b89a7cc2SEnji Cooper EXPECT_STREQ("", GetCapturedStdout().c_str());
549b89a7cc2SEnji Cooper }
550b89a7cc2SEnji Cooper
551b89a7cc2SEnji Cooper // Tests that Google Mock warns on having too many actions in an
552b89a7cc2SEnji Cooper // expectation compared to its cardinality.
TEST(ExpectCallSyntaxTest,WarnsOnTooManyActions)553b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, WarnsOnTooManyActions) {
554b89a7cc2SEnji Cooper CaptureStdout();
555b89a7cc2SEnji Cooper {
556b89a7cc2SEnji Cooper MockB b;
557b89a7cc2SEnji Cooper
558b89a7cc2SEnji Cooper // Warns when the number of WillOnce()s is larger than the upper bound.
55928f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).Times(0).WillOnce(Return(1)); // #1
56028f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).Times(AtMost(1)).WillOnce(Return(1)).WillOnce(
56128f6c2f2SEnji Cooper Return(2)); // #2
562b89a7cc2SEnji Cooper EXPECT_CALL(b, DoB(1))
563b89a7cc2SEnji Cooper .Times(1)
564b89a7cc2SEnji Cooper .WillOnce(Return(1))
565b89a7cc2SEnji Cooper .WillOnce(Return(2))
566b89a7cc2SEnji Cooper .RetiresOnSaturation(); // #3
567b89a7cc2SEnji Cooper
568b89a7cc2SEnji Cooper // Warns when the number of WillOnce()s equals the upper bound and
569b89a7cc2SEnji Cooper // there is a WillRepeatedly().
57028f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).Times(0).WillRepeatedly(Return(1)); // #4
57128f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(2)).Times(1).WillOnce(Return(1)).WillRepeatedly(
57228f6c2f2SEnji Cooper Return(2)); // #5
573b89a7cc2SEnji Cooper
574b89a7cc2SEnji Cooper // Satisfies the above expectations.
575b89a7cc2SEnji Cooper b.DoB(1);
576b89a7cc2SEnji Cooper b.DoB(2);
577b89a7cc2SEnji Cooper }
578b89a7cc2SEnji Cooper const std::string output = GetCapturedStdout();
57928f6c2f2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring,
580b89a7cc2SEnji Cooper "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
581b89a7cc2SEnji Cooper "Expected to be never called, but has 1 WillOnce().",
582b89a7cc2SEnji Cooper output); // #1
58328f6c2f2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring,
584b89a7cc2SEnji Cooper "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
585b89a7cc2SEnji Cooper "Expected to be called at most once, "
586b89a7cc2SEnji Cooper "but has 2 WillOnce()s.",
587b89a7cc2SEnji Cooper output); // #2
588b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(
589b89a7cc2SEnji Cooper IsSubstring,
590b89a7cc2SEnji Cooper "Too many actions specified in EXPECT_CALL(b, DoB(1))...\n"
591b89a7cc2SEnji Cooper "Expected to be called once, but has 2 WillOnce()s.",
592b89a7cc2SEnji Cooper output); // #3
59328f6c2f2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring,
594b89a7cc2SEnji Cooper "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
595b89a7cc2SEnji Cooper "Expected to be never called, but has 0 WillOnce()s "
596b89a7cc2SEnji Cooper "and a WillRepeatedly().",
597b89a7cc2SEnji Cooper output); // #4
598b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(
599b89a7cc2SEnji Cooper IsSubstring,
600b89a7cc2SEnji Cooper "Too many actions specified in EXPECT_CALL(b, DoB(2))...\n"
601b89a7cc2SEnji Cooper "Expected to be called once, but has 1 WillOnce() "
602b89a7cc2SEnji Cooper "and a WillRepeatedly().",
603b89a7cc2SEnji Cooper output); // #5
604b89a7cc2SEnji Cooper }
605b89a7cc2SEnji Cooper
606b89a7cc2SEnji Cooper // Tests that Google Mock warns on having too few actions in an
607b89a7cc2SEnji Cooper // expectation compared to its cardinality.
TEST(ExpectCallSyntaxTest,WarnsOnTooFewActions)608b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) {
609b89a7cc2SEnji Cooper MockB b;
610b89a7cc2SEnji Cooper
61128f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).Times(Between(2, 3)).WillOnce(Return(1));
612b89a7cc2SEnji Cooper
613b89a7cc2SEnji Cooper CaptureStdout();
614b89a7cc2SEnji Cooper b.DoB();
615b89a7cc2SEnji Cooper const std::string output = GetCapturedStdout();
61628f6c2f2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring,
617b89a7cc2SEnji Cooper "Too few actions specified in EXPECT_CALL(b, DoB())...\n"
618b89a7cc2SEnji Cooper "Expected to be called between 2 and 3 times, "
619b89a7cc2SEnji Cooper "but has only 1 WillOnce().",
620b89a7cc2SEnji Cooper output);
621b89a7cc2SEnji Cooper b.DoB();
622b89a7cc2SEnji Cooper }
623b89a7cc2SEnji Cooper
TEST(ExpectCallSyntaxTest,WarningIsErrorWithFlag)624b89a7cc2SEnji Cooper TEST(ExpectCallSyntaxTest, WarningIsErrorWithFlag) {
62528f6c2f2SEnji Cooper int original_behavior = GMOCK_FLAG_GET(default_mock_behavior);
626b89a7cc2SEnji Cooper
62728f6c2f2SEnji Cooper GMOCK_FLAG_SET(default_mock_behavior, kAllow);
628b89a7cc2SEnji Cooper CaptureStdout();
629b89a7cc2SEnji Cooper {
630b89a7cc2SEnji Cooper MockA a;
631b89a7cc2SEnji Cooper a.DoA(0);
632b89a7cc2SEnji Cooper }
633b89a7cc2SEnji Cooper std::string output = GetCapturedStdout();
634b89a7cc2SEnji Cooper EXPECT_TRUE(output.empty()) << output;
635b89a7cc2SEnji Cooper
63628f6c2f2SEnji Cooper GMOCK_FLAG_SET(default_mock_behavior, kWarn);
637b89a7cc2SEnji Cooper CaptureStdout();
638b89a7cc2SEnji Cooper {
639b89a7cc2SEnji Cooper MockA a;
640b89a7cc2SEnji Cooper a.DoA(0);
641b89a7cc2SEnji Cooper }
642b89a7cc2SEnji Cooper std::string warning_output = GetCapturedStdout();
643b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", warning_output);
644b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call",
645b89a7cc2SEnji Cooper warning_output);
646b89a7cc2SEnji Cooper
64728f6c2f2SEnji Cooper GMOCK_FLAG_SET(default_mock_behavior, kFail);
64828f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
64928f6c2f2SEnji Cooper {
650b89a7cc2SEnji Cooper MockA a;
651b89a7cc2SEnji Cooper a.DoA(0);
65228f6c2f2SEnji Cooper },
65328f6c2f2SEnji Cooper "Uninteresting mock function call");
654b89a7cc2SEnji Cooper
655b89a7cc2SEnji Cooper // Out of bounds values are converted to kWarn
65628f6c2f2SEnji Cooper GMOCK_FLAG_SET(default_mock_behavior, -1);
657b89a7cc2SEnji Cooper CaptureStdout();
658b89a7cc2SEnji Cooper {
659b89a7cc2SEnji Cooper MockA a;
660b89a7cc2SEnji Cooper a.DoA(0);
661b89a7cc2SEnji Cooper }
662b89a7cc2SEnji Cooper warning_output = GetCapturedStdout();
663b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", warning_output);
664b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call",
665b89a7cc2SEnji Cooper warning_output);
66628f6c2f2SEnji Cooper GMOCK_FLAG_SET(default_mock_behavior, 3);
667b89a7cc2SEnji Cooper CaptureStdout();
668b89a7cc2SEnji Cooper {
669b89a7cc2SEnji Cooper MockA a;
670b89a7cc2SEnji Cooper a.DoA(0);
671b89a7cc2SEnji Cooper }
672b89a7cc2SEnji Cooper warning_output = GetCapturedStdout();
673b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", warning_output);
674b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call",
675b89a7cc2SEnji Cooper warning_output);
676b89a7cc2SEnji Cooper
67728f6c2f2SEnji Cooper GMOCK_FLAG_SET(default_mock_behavior, original_behavior);
678b89a7cc2SEnji Cooper }
679b89a7cc2SEnji Cooper
680b89a7cc2SEnji Cooper #endif // GTEST_HAS_STREAM_REDIRECTION
681b89a7cc2SEnji Cooper
682b89a7cc2SEnji Cooper // Tests the semantics of ON_CALL().
683b89a7cc2SEnji Cooper
684b89a7cc2SEnji Cooper // Tests that the built-in default action is taken when no ON_CALL()
685b89a7cc2SEnji Cooper // is specified.
TEST(OnCallTest,TakesBuiltInDefaultActionWhenNoOnCall)686b89a7cc2SEnji Cooper TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCall) {
687b89a7cc2SEnji Cooper MockB b;
688b89a7cc2SEnji Cooper EXPECT_CALL(b, DoB());
689b89a7cc2SEnji Cooper
690b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
691b89a7cc2SEnji Cooper }
692b89a7cc2SEnji Cooper
693b89a7cc2SEnji Cooper // Tests that the built-in default action is taken when no ON_CALL()
694b89a7cc2SEnji Cooper // matches the invocation.
TEST(OnCallTest,TakesBuiltInDefaultActionWhenNoOnCallMatches)695b89a7cc2SEnji Cooper TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCallMatches) {
696b89a7cc2SEnji Cooper MockB b;
69728f6c2f2SEnji Cooper ON_CALL(b, DoB(1)).WillByDefault(Return(1));
698b89a7cc2SEnji Cooper EXPECT_CALL(b, DoB(_));
699b89a7cc2SEnji Cooper
700b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(2));
701b89a7cc2SEnji Cooper }
702b89a7cc2SEnji Cooper
703b89a7cc2SEnji Cooper // Tests that the last matching ON_CALL() action is taken.
TEST(OnCallTest,PicksLastMatchingOnCall)704b89a7cc2SEnji Cooper TEST(OnCallTest, PicksLastMatchingOnCall) {
705b89a7cc2SEnji Cooper MockB b;
70628f6c2f2SEnji Cooper ON_CALL(b, DoB(_)).WillByDefault(Return(3));
70728f6c2f2SEnji Cooper ON_CALL(b, DoB(2)).WillByDefault(Return(2));
70828f6c2f2SEnji Cooper ON_CALL(b, DoB(1)).WillByDefault(Return(1));
709b89a7cc2SEnji Cooper EXPECT_CALL(b, DoB(_));
710b89a7cc2SEnji Cooper
711b89a7cc2SEnji Cooper EXPECT_EQ(2, b.DoB(2));
712b89a7cc2SEnji Cooper }
713b89a7cc2SEnji Cooper
714b89a7cc2SEnji Cooper // Tests the semantics of EXPECT_CALL().
715b89a7cc2SEnji Cooper
716b89a7cc2SEnji Cooper // Tests that any call is allowed when no EXPECT_CALL() is specified.
TEST(ExpectCallTest,AllowsAnyCallWhenNoSpec)717b89a7cc2SEnji Cooper TEST(ExpectCallTest, AllowsAnyCallWhenNoSpec) {
718b89a7cc2SEnji Cooper MockB b;
719b89a7cc2SEnji Cooper EXPECT_CALL(b, DoB());
720b89a7cc2SEnji Cooper // There is no expectation on DoB(int).
721b89a7cc2SEnji Cooper
722b89a7cc2SEnji Cooper b.DoB();
723b89a7cc2SEnji Cooper
724b89a7cc2SEnji Cooper // DoB(int) can be called any number of times.
725b89a7cc2SEnji Cooper b.DoB(1);
726b89a7cc2SEnji Cooper b.DoB(2);
727b89a7cc2SEnji Cooper }
728b89a7cc2SEnji Cooper
729b89a7cc2SEnji Cooper // Tests that the last matching EXPECT_CALL() fires.
TEST(ExpectCallTest,PicksLastMatchingExpectCall)730b89a7cc2SEnji Cooper TEST(ExpectCallTest, PicksLastMatchingExpectCall) {
731b89a7cc2SEnji Cooper MockB b;
73228f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(_)).WillRepeatedly(Return(2));
73328f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(1)).WillRepeatedly(Return(1));
734b89a7cc2SEnji Cooper
735b89a7cc2SEnji Cooper EXPECT_EQ(1, b.DoB(1));
736b89a7cc2SEnji Cooper }
737b89a7cc2SEnji Cooper
738b89a7cc2SEnji Cooper // Tests lower-bound violation.
TEST(ExpectCallTest,CatchesTooFewCalls)739b89a7cc2SEnji Cooper TEST(ExpectCallTest, CatchesTooFewCalls) {
74028f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
74128f6c2f2SEnji Cooper { // NOLINT
742b89a7cc2SEnji Cooper MockB b;
74328f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(5)).Description("DoB Method").Times(AtLeast(2));
744b89a7cc2SEnji Cooper
745b89a7cc2SEnji Cooper b.DoB(5);
74628f6c2f2SEnji Cooper },
74728f6c2f2SEnji Cooper "Actual function \"DoB Method\" call count "
74828f6c2f2SEnji Cooper "doesn't match EXPECT_CALL(b, DoB(5))...\n"
749b89a7cc2SEnji Cooper " Expected: to be called at least twice\n"
750b89a7cc2SEnji Cooper " Actual: called once - unsatisfied and active");
751b89a7cc2SEnji Cooper }
752b89a7cc2SEnji Cooper
753b89a7cc2SEnji Cooper // Tests that the cardinality can be inferred when no Times(...) is
754b89a7cc2SEnji Cooper // specified.
TEST(ExpectCallTest,InfersCardinalityWhenThereIsNoWillRepeatedly)755b89a7cc2SEnji Cooper TEST(ExpectCallTest, InfersCardinalityWhenThereIsNoWillRepeatedly) {
756b89a7cc2SEnji Cooper {
757b89a7cc2SEnji Cooper MockB b;
75828f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).WillOnce(Return(1)).WillOnce(Return(2));
759b89a7cc2SEnji Cooper
760b89a7cc2SEnji Cooper EXPECT_EQ(1, b.DoB());
761b89a7cc2SEnji Cooper EXPECT_EQ(2, b.DoB());
762b89a7cc2SEnji Cooper }
763b89a7cc2SEnji Cooper
76428f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
76528f6c2f2SEnji Cooper { // NOLINT
766b89a7cc2SEnji Cooper MockB b;
76728f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).WillOnce(Return(1)).WillOnce(Return(2));
768b89a7cc2SEnji Cooper
769b89a7cc2SEnji Cooper EXPECT_EQ(1, b.DoB());
77028f6c2f2SEnji Cooper },
77128f6c2f2SEnji Cooper "to be called twice");
772b89a7cc2SEnji Cooper
773b89a7cc2SEnji Cooper { // NOLINT
774b89a7cc2SEnji Cooper MockB b;
77528f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).WillOnce(Return(1)).WillOnce(Return(2));
776b89a7cc2SEnji Cooper
777b89a7cc2SEnji Cooper EXPECT_EQ(1, b.DoB());
778b89a7cc2SEnji Cooper EXPECT_EQ(2, b.DoB());
779b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(b.DoB(), "to be called twice");
780b89a7cc2SEnji Cooper }
781b89a7cc2SEnji Cooper }
782b89a7cc2SEnji Cooper
TEST(ExpectCallTest,InfersCardinality1WhenThereIsWillRepeatedly)783b89a7cc2SEnji Cooper TEST(ExpectCallTest, InfersCardinality1WhenThereIsWillRepeatedly) {
784b89a7cc2SEnji Cooper {
785b89a7cc2SEnji Cooper MockB b;
78628f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).WillOnce(Return(1)).WillRepeatedly(Return(2));
787b89a7cc2SEnji Cooper
788b89a7cc2SEnji Cooper EXPECT_EQ(1, b.DoB());
789b89a7cc2SEnji Cooper }
790b89a7cc2SEnji Cooper
791b89a7cc2SEnji Cooper { // NOLINT
792b89a7cc2SEnji Cooper MockB b;
79328f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).WillOnce(Return(1)).WillRepeatedly(Return(2));
794b89a7cc2SEnji Cooper
795b89a7cc2SEnji Cooper EXPECT_EQ(1, b.DoB());
796b89a7cc2SEnji Cooper EXPECT_EQ(2, b.DoB());
797b89a7cc2SEnji Cooper EXPECT_EQ(2, b.DoB());
798b89a7cc2SEnji Cooper }
799b89a7cc2SEnji Cooper
80028f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
80128f6c2f2SEnji Cooper { // NOLINT
802b89a7cc2SEnji Cooper MockB b;
80328f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).WillOnce(Return(1)).WillRepeatedly(Return(2));
80428f6c2f2SEnji Cooper },
80528f6c2f2SEnji Cooper "to be called at least once");
806b89a7cc2SEnji Cooper }
807b89a7cc2SEnji Cooper
80828f6c2f2SEnji Cooper #if defined(GTEST_INTERNAL_CPLUSPLUS_LANG) && \
80928f6c2f2SEnji Cooper GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
81028f6c2f2SEnji Cooper
81128f6c2f2SEnji Cooper // It should be possible to return a non-moveable type from a mock action in
81228f6c2f2SEnji Cooper // C++17 and above, where it's guaranteed that such a type can be initialized
81328f6c2f2SEnji Cooper // from a prvalue returned from a function.
TEST(ExpectCallTest,NonMoveableType)81428f6c2f2SEnji Cooper TEST(ExpectCallTest, NonMoveableType) {
81528f6c2f2SEnji Cooper // Define a non-moveable result type.
81628f6c2f2SEnji Cooper struct NonMoveableStruct {
81728f6c2f2SEnji Cooper explicit NonMoveableStruct(int x_in) : x(x_in) {}
81828f6c2f2SEnji Cooper NonMoveableStruct(NonMoveableStruct&&) = delete;
81928f6c2f2SEnji Cooper
82028f6c2f2SEnji Cooper int x;
82128f6c2f2SEnji Cooper };
82228f6c2f2SEnji Cooper
82328f6c2f2SEnji Cooper static_assert(!std::is_move_constructible_v<NonMoveableStruct>);
82428f6c2f2SEnji Cooper static_assert(!std::is_copy_constructible_v<NonMoveableStruct>);
82528f6c2f2SEnji Cooper
82628f6c2f2SEnji Cooper static_assert(!std::is_move_assignable_v<NonMoveableStruct>);
82728f6c2f2SEnji Cooper static_assert(!std::is_copy_assignable_v<NonMoveableStruct>);
82828f6c2f2SEnji Cooper
82928f6c2f2SEnji Cooper // We should be able to use a callable that returns that result as both a
83028f6c2f2SEnji Cooper // OnceAction and an Action, whether the callable ignores arguments or not.
83128f6c2f2SEnji Cooper const auto return_17 = [] { return NonMoveableStruct(17); };
83228f6c2f2SEnji Cooper
83328f6c2f2SEnji Cooper static_cast<void>(OnceAction<NonMoveableStruct()>{return_17});
83428f6c2f2SEnji Cooper static_cast<void>(Action<NonMoveableStruct()>{return_17});
83528f6c2f2SEnji Cooper
83628f6c2f2SEnji Cooper static_cast<void>(OnceAction<NonMoveableStruct(int)>{return_17});
83728f6c2f2SEnji Cooper static_cast<void>(Action<NonMoveableStruct(int)>{return_17});
83828f6c2f2SEnji Cooper
83928f6c2f2SEnji Cooper // It should be possible to return the result end to end through an
84028f6c2f2SEnji Cooper // EXPECT_CALL statement, with both WillOnce and WillRepeatedly.
84128f6c2f2SEnji Cooper MockFunction<NonMoveableStruct()> mock;
84228f6c2f2SEnji Cooper EXPECT_CALL(mock, Call) //
84328f6c2f2SEnji Cooper .WillOnce(return_17) //
84428f6c2f2SEnji Cooper .WillRepeatedly(return_17);
84528f6c2f2SEnji Cooper
84628f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()().x);
84728f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()().x);
84828f6c2f2SEnji Cooper EXPECT_EQ(17, mock.AsStdFunction()().x);
84928f6c2f2SEnji Cooper }
85028f6c2f2SEnji Cooper
85128f6c2f2SEnji Cooper #endif // C++17 and above
85228f6c2f2SEnji Cooper
853b89a7cc2SEnji Cooper // Tests that the n-th action is taken for the n-th matching
854b89a7cc2SEnji Cooper // invocation.
TEST(ExpectCallTest,NthMatchTakesNthAction)855b89a7cc2SEnji Cooper TEST(ExpectCallTest, NthMatchTakesNthAction) {
856b89a7cc2SEnji Cooper MockB b;
85728f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).WillOnce(Return(1)).WillOnce(Return(2)).WillOnce(
85828f6c2f2SEnji Cooper Return(3));
859b89a7cc2SEnji Cooper
860b89a7cc2SEnji Cooper EXPECT_EQ(1, b.DoB());
861b89a7cc2SEnji Cooper EXPECT_EQ(2, b.DoB());
862b89a7cc2SEnji Cooper EXPECT_EQ(3, b.DoB());
863b89a7cc2SEnji Cooper }
864b89a7cc2SEnji Cooper
865b89a7cc2SEnji Cooper // Tests that the WillRepeatedly() action is taken when the WillOnce(...)
866b89a7cc2SEnji Cooper // list is exhausted.
TEST(ExpectCallTest,TakesRepeatedActionWhenWillListIsExhausted)867b89a7cc2SEnji Cooper TEST(ExpectCallTest, TakesRepeatedActionWhenWillListIsExhausted) {
868b89a7cc2SEnji Cooper MockB b;
86928f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).WillOnce(Return(1)).WillRepeatedly(Return(2));
870b89a7cc2SEnji Cooper
871b89a7cc2SEnji Cooper EXPECT_EQ(1, b.DoB());
872b89a7cc2SEnji Cooper EXPECT_EQ(2, b.DoB());
873b89a7cc2SEnji Cooper EXPECT_EQ(2, b.DoB());
874b89a7cc2SEnji Cooper }
875b89a7cc2SEnji Cooper
876b89a7cc2SEnji Cooper #if GTEST_HAS_STREAM_REDIRECTION
877b89a7cc2SEnji Cooper
878b89a7cc2SEnji Cooper // Tests that the default action is taken when the WillOnce(...) list is
879b89a7cc2SEnji Cooper // exhausted and there is no WillRepeatedly().
TEST(ExpectCallTest,TakesDefaultActionWhenWillListIsExhausted)880b89a7cc2SEnji Cooper TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) {
881b89a7cc2SEnji Cooper MockB b;
88228f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(_)).Times(1);
883b89a7cc2SEnji Cooper EXPECT_CALL(b, DoB())
884b89a7cc2SEnji Cooper .Times(AnyNumber())
885b89a7cc2SEnji Cooper .WillOnce(Return(1))
886b89a7cc2SEnji Cooper .WillOnce(Return(2));
887b89a7cc2SEnji Cooper
888b89a7cc2SEnji Cooper CaptureStdout();
889b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(1)); // Shouldn't generate a warning as the
890b89a7cc2SEnji Cooper // expectation has no action clause at all.
891b89a7cc2SEnji Cooper EXPECT_EQ(1, b.DoB());
892b89a7cc2SEnji Cooper EXPECT_EQ(2, b.DoB());
893b89a7cc2SEnji Cooper const std::string output1 = GetCapturedStdout();
894b89a7cc2SEnji Cooper EXPECT_STREQ("", output1.c_str());
895b89a7cc2SEnji Cooper
896b89a7cc2SEnji Cooper CaptureStdout();
897b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
898b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
899b89a7cc2SEnji Cooper const std::string output2 = GetCapturedStdout();
900b89a7cc2SEnji Cooper EXPECT_THAT(output2.c_str(),
901b89a7cc2SEnji Cooper HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n"
902b89a7cc2SEnji Cooper "Called 3 times, but only 2 WillOnce()s are specified"
903b89a7cc2SEnji Cooper " - returning default value."));
904b89a7cc2SEnji Cooper EXPECT_THAT(output2.c_str(),
905b89a7cc2SEnji Cooper HasSubstr("Actions ran out in EXPECT_CALL(b, DoB())...\n"
906b89a7cc2SEnji Cooper "Called 4 times, but only 2 WillOnce()s are specified"
907b89a7cc2SEnji Cooper " - returning default value."));
908b89a7cc2SEnji Cooper }
909b89a7cc2SEnji Cooper
TEST(FunctionMockerMessageTest,ReportsExpectCallLocationForExhaustedActions)910*5ca8c28cSEnji Cooper TEST(FunctionMockerMessageTest, ReportsExpectCallLocationForExhaustedActions) {
911b89a7cc2SEnji Cooper MockB b;
912b89a7cc2SEnji Cooper std::string expect_call_location = FormatFileLocation(__FILE__, __LINE__ + 1);
913b89a7cc2SEnji Cooper EXPECT_CALL(b, DoB()).Times(AnyNumber()).WillOnce(Return(1));
914b89a7cc2SEnji Cooper
915b89a7cc2SEnji Cooper EXPECT_EQ(1, b.DoB());
916b89a7cc2SEnji Cooper
917b89a7cc2SEnji Cooper CaptureStdout();
918b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
919b89a7cc2SEnji Cooper const std::string output = GetCapturedStdout();
920b89a7cc2SEnji Cooper // The warning message should contain the call location.
921b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring, expect_call_location, output);
922b89a7cc2SEnji Cooper }
923b89a7cc2SEnji Cooper
TEST(FunctionMockerMessageTest,ReportsDefaultActionLocationOfUninterestingCallsForNaggyMock)924b89a7cc2SEnji Cooper TEST(FunctionMockerMessageTest,
925b89a7cc2SEnji Cooper ReportsDefaultActionLocationOfUninterestingCallsForNaggyMock) {
926b89a7cc2SEnji Cooper std::string on_call_location;
927b89a7cc2SEnji Cooper CaptureStdout();
928b89a7cc2SEnji Cooper {
929b89a7cc2SEnji Cooper NaggyMock<MockB> b;
930b89a7cc2SEnji Cooper on_call_location = FormatFileLocation(__FILE__, __LINE__ + 1);
931b89a7cc2SEnji Cooper ON_CALL(b, DoB(_)).WillByDefault(Return(0));
932b89a7cc2SEnji Cooper b.DoB(0);
933b89a7cc2SEnji Cooper }
934b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring, on_call_location, GetCapturedStdout());
935b89a7cc2SEnji Cooper }
936b89a7cc2SEnji Cooper
937b89a7cc2SEnji Cooper #endif // GTEST_HAS_STREAM_REDIRECTION
938b89a7cc2SEnji Cooper
939b89a7cc2SEnji Cooper // Tests that an uninteresting call performs the default action.
TEST(UninterestingCallTest,DoesDefaultAction)940b89a7cc2SEnji Cooper TEST(UninterestingCallTest, DoesDefaultAction) {
941b89a7cc2SEnji Cooper // When there is an ON_CALL() statement, the action specified by it
942b89a7cc2SEnji Cooper // should be taken.
943b89a7cc2SEnji Cooper MockA a;
94428f6c2f2SEnji Cooper ON_CALL(a, Binary(_, _)).WillByDefault(Return(true));
945b89a7cc2SEnji Cooper EXPECT_TRUE(a.Binary(1, 2));
946b89a7cc2SEnji Cooper
947b89a7cc2SEnji Cooper // When there is no ON_CALL(), the default value for the return type
948b89a7cc2SEnji Cooper // should be returned.
949b89a7cc2SEnji Cooper MockB b;
950b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
951b89a7cc2SEnji Cooper }
952b89a7cc2SEnji Cooper
953b89a7cc2SEnji Cooper // Tests that an unexpected call performs the default action.
TEST(UnexpectedCallTest,DoesDefaultAction)954b89a7cc2SEnji Cooper TEST(UnexpectedCallTest, DoesDefaultAction) {
955b89a7cc2SEnji Cooper // When there is an ON_CALL() statement, the action specified by it
956b89a7cc2SEnji Cooper // should be taken.
957b89a7cc2SEnji Cooper MockA a;
95828f6c2f2SEnji Cooper ON_CALL(a, Binary(_, _)).WillByDefault(Return(true));
959b89a7cc2SEnji Cooper EXPECT_CALL(a, Binary(0, 0));
960b89a7cc2SEnji Cooper a.Binary(0, 0);
961b89a7cc2SEnji Cooper bool result = false;
962b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(result = a.Binary(1, 2),
963b89a7cc2SEnji Cooper "Unexpected mock function call");
964b89a7cc2SEnji Cooper EXPECT_TRUE(result);
965b89a7cc2SEnji Cooper
966b89a7cc2SEnji Cooper // When there is no ON_CALL(), the default value for the return type
967b89a7cc2SEnji Cooper // should be returned.
968b89a7cc2SEnji Cooper MockB b;
96928f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(0)).Times(0);
970b89a7cc2SEnji Cooper int n = -1;
97128f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(n = b.DoB(1), "Unexpected mock function call");
972b89a7cc2SEnji Cooper EXPECT_EQ(0, n);
973b89a7cc2SEnji Cooper }
974b89a7cc2SEnji Cooper
975b89a7cc2SEnji Cooper // Tests that when an unexpected void function generates the right
976b89a7cc2SEnji Cooper // failure message.
TEST(UnexpectedCallTest,GeneratesFailureForVoidFunction)977b89a7cc2SEnji Cooper TEST(UnexpectedCallTest, GeneratesFailureForVoidFunction) {
978b89a7cc2SEnji Cooper // First, tests the message when there is only one EXPECT_CALL().
979b89a7cc2SEnji Cooper MockA a1;
980b89a7cc2SEnji Cooper EXPECT_CALL(a1, DoA(1));
981b89a7cc2SEnji Cooper a1.DoA(1);
982b89a7cc2SEnji Cooper // Ideally we should match the failure message against a regex, but
983b89a7cc2SEnji Cooper // EXPECT_NONFATAL_FAILURE doesn't support that, so we test for
984b89a7cc2SEnji Cooper // multiple sub-strings instead.
985b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(
986b89a7cc2SEnji Cooper a1.DoA(9),
987b89a7cc2SEnji Cooper "Unexpected mock function call - returning directly.\n"
988b89a7cc2SEnji Cooper " Function call: DoA(9)\n"
989b89a7cc2SEnji Cooper "Google Mock tried the following 1 expectation, but it didn't match:");
990b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(
991b89a7cc2SEnji Cooper a1.DoA(9),
992b89a7cc2SEnji Cooper " Expected arg #0: is equal to 1\n"
993b89a7cc2SEnji Cooper " Actual: 9\n"
994b89a7cc2SEnji Cooper " Expected: to be called once\n"
995b89a7cc2SEnji Cooper " Actual: called once - saturated and active");
996b89a7cc2SEnji Cooper
997b89a7cc2SEnji Cooper // Next, tests the message when there are more than one EXPECT_CALL().
998b89a7cc2SEnji Cooper MockA a2;
999b89a7cc2SEnji Cooper EXPECT_CALL(a2, DoA(1));
1000b89a7cc2SEnji Cooper EXPECT_CALL(a2, DoA(3));
1001b89a7cc2SEnji Cooper a2.DoA(1);
1002b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(
1003b89a7cc2SEnji Cooper a2.DoA(2),
1004b89a7cc2SEnji Cooper "Unexpected mock function call - returning directly.\n"
1005b89a7cc2SEnji Cooper " Function call: DoA(2)\n"
1006b89a7cc2SEnji Cooper "Google Mock tried the following 2 expectations, but none matched:");
1007b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(
1008b89a7cc2SEnji Cooper a2.DoA(2),
1009b89a7cc2SEnji Cooper "tried expectation #0: EXPECT_CALL(a2, DoA(1))...\n"
1010b89a7cc2SEnji Cooper " Expected arg #0: is equal to 1\n"
1011b89a7cc2SEnji Cooper " Actual: 2\n"
1012b89a7cc2SEnji Cooper " Expected: to be called once\n"
1013b89a7cc2SEnji Cooper " Actual: called once - saturated and active");
1014b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(
1015b89a7cc2SEnji Cooper a2.DoA(2),
1016b89a7cc2SEnji Cooper "tried expectation #1: EXPECT_CALL(a2, DoA(3))...\n"
1017b89a7cc2SEnji Cooper " Expected arg #0: is equal to 3\n"
1018b89a7cc2SEnji Cooper " Actual: 2\n"
1019b89a7cc2SEnji Cooper " Expected: to be called once\n"
1020b89a7cc2SEnji Cooper " Actual: never called - unsatisfied and active");
1021b89a7cc2SEnji Cooper a2.DoA(3);
1022b89a7cc2SEnji Cooper }
1023b89a7cc2SEnji Cooper
1024b89a7cc2SEnji Cooper // Tests that an unexpected non-void function generates the right
1025b89a7cc2SEnji Cooper // failure message.
TEST(UnexpectedCallTest,GeneartesFailureForNonVoidFunction)1026b89a7cc2SEnji Cooper TEST(UnexpectedCallTest, GeneartesFailureForNonVoidFunction) {
1027b89a7cc2SEnji Cooper MockB b1;
1028b89a7cc2SEnji Cooper EXPECT_CALL(b1, DoB(1));
1029b89a7cc2SEnji Cooper b1.DoB(1);
1030b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(
1031b89a7cc2SEnji Cooper b1.DoB(2),
1032b89a7cc2SEnji Cooper "Unexpected mock function call - returning default value.\n"
1033b89a7cc2SEnji Cooper " Function call: DoB(2)\n"
1034b89a7cc2SEnji Cooper " Returns: 0\n"
1035b89a7cc2SEnji Cooper "Google Mock tried the following 1 expectation, but it didn't match:");
1036b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(
1037b89a7cc2SEnji Cooper b1.DoB(2),
1038b89a7cc2SEnji Cooper " Expected arg #0: is equal to 1\n"
1039b89a7cc2SEnji Cooper " Actual: 2\n"
1040b89a7cc2SEnji Cooper " Expected: to be called once\n"
1041b89a7cc2SEnji Cooper " Actual: called once - saturated and active");
1042b89a7cc2SEnji Cooper }
1043b89a7cc2SEnji Cooper
1044b89a7cc2SEnji Cooper // Tests that Google Mock explains that an retired expectation doesn't
1045b89a7cc2SEnji Cooper // match the call.
TEST(UnexpectedCallTest,RetiredExpectation)1046b89a7cc2SEnji Cooper TEST(UnexpectedCallTest, RetiredExpectation) {
1047b89a7cc2SEnji Cooper MockB b;
104828f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(1)).RetiresOnSaturation();
1049b89a7cc2SEnji Cooper
1050b89a7cc2SEnji Cooper b.DoB(1);
105128f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(b.DoB(1),
1052b89a7cc2SEnji Cooper " Expected: the expectation is active\n"
1053b89a7cc2SEnji Cooper " Actual: it is retired");
1054b89a7cc2SEnji Cooper }
1055b89a7cc2SEnji Cooper
1056b89a7cc2SEnji Cooper // Tests that Google Mock explains that an expectation that doesn't
1057b89a7cc2SEnji Cooper // match the arguments doesn't match the call.
TEST(UnexpectedCallTest,UnmatchedArguments)1058b89a7cc2SEnji Cooper TEST(UnexpectedCallTest, UnmatchedArguments) {
1059b89a7cc2SEnji Cooper MockB b;
1060b89a7cc2SEnji Cooper EXPECT_CALL(b, DoB(1));
1061b89a7cc2SEnji Cooper
106228f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(b.DoB(2),
1063b89a7cc2SEnji Cooper " Expected arg #0: is equal to 1\n"
1064b89a7cc2SEnji Cooper " Actual: 2\n");
1065b89a7cc2SEnji Cooper b.DoB(1);
1066b89a7cc2SEnji Cooper }
1067b89a7cc2SEnji Cooper
1068b89a7cc2SEnji Cooper // Tests that Google Mock explains that an expectation with
1069b89a7cc2SEnji Cooper // unsatisfied pre-requisites doesn't match the call.
TEST(UnexpectedCallTest,UnsatisfiedPrerequisites)107028f6c2f2SEnji Cooper TEST(UnexpectedCallTest, UnsatisfiedPrerequisites) {
1071b89a7cc2SEnji Cooper Sequence s1, s2;
1072b89a7cc2SEnji Cooper MockB b;
107328f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(1)).InSequence(s1);
107428f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(2)).Times(AnyNumber()).InSequence(s1);
107528f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(3)).InSequence(s2);
107628f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(4)).InSequence(s1, s2);
1077b89a7cc2SEnji Cooper
1078b89a7cc2SEnji Cooper ::testing::TestPartResultArray failures;
1079b89a7cc2SEnji Cooper {
1080b89a7cc2SEnji Cooper ::testing::ScopedFakeTestPartResultReporter reporter(&failures);
1081b89a7cc2SEnji Cooper b.DoB(4);
1082b89a7cc2SEnji Cooper // Now 'failures' contains the Google Test failures generated by
1083b89a7cc2SEnji Cooper // the above statement.
1084b89a7cc2SEnji Cooper }
1085b89a7cc2SEnji Cooper
1086b89a7cc2SEnji Cooper // There should be one non-fatal failure.
1087b89a7cc2SEnji Cooper ASSERT_EQ(1, failures.size());
1088b89a7cc2SEnji Cooper const ::testing::TestPartResult& r = failures.GetTestPartResult(0);
1089b89a7cc2SEnji Cooper EXPECT_EQ(::testing::TestPartResult::kNonFatalFailure, r.type());
1090b89a7cc2SEnji Cooper
1091b89a7cc2SEnji Cooper // Verifies that the failure message contains the two unsatisfied
1092b89a7cc2SEnji Cooper // pre-requisites but not the satisfied one.
109328f6c2f2SEnji Cooper #ifdef GTEST_USES_POSIX_RE
109428f6c2f2SEnji Cooper EXPECT_THAT(r.message(),
109528f6c2f2SEnji Cooper ContainsRegex(
109628f6c2f2SEnji Cooper // POSIX RE doesn't understand the (?s) prefix, but has no
109728f6c2f2SEnji Cooper // trouble with (.|\n).
1098b89a7cc2SEnji Cooper "the following immediate pre-requisites are not satisfied:\n"
1099b89a7cc2SEnji Cooper "(.|\n)*: pre-requisite #0\n"
1100b89a7cc2SEnji Cooper "(.|\n)*: pre-requisite #1"));
1101b89a7cc2SEnji Cooper #else
1102b89a7cc2SEnji Cooper // We can only use Google Test's own simple regex.
110328f6c2f2SEnji Cooper EXPECT_THAT(r.message(),
110428f6c2f2SEnji Cooper ContainsRegex(
1105b89a7cc2SEnji Cooper "the following immediate pre-requisites are not satisfied:"));
1106b89a7cc2SEnji Cooper EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #0"));
1107b89a7cc2SEnji Cooper EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #1"));
110828f6c2f2SEnji Cooper #endif // GTEST_USES_POSIX_RE
1109b89a7cc2SEnji Cooper
1110b89a7cc2SEnji Cooper b.DoB(1);
1111b89a7cc2SEnji Cooper b.DoB(3);
1112b89a7cc2SEnji Cooper b.DoB(4);
1113b89a7cc2SEnji Cooper }
1114b89a7cc2SEnji Cooper
TEST(UndefinedReturnValueTest,ReturnValueIsMandatoryWhenNotDefaultConstructible)1115b89a7cc2SEnji Cooper TEST(UndefinedReturnValueTest,
1116b89a7cc2SEnji Cooper ReturnValueIsMandatoryWhenNotDefaultConstructible) {
1117b89a7cc2SEnji Cooper MockA a;
1118b89a7cc2SEnji Cooper // FIXME: We should really verify the output message,
1119b89a7cc2SEnji Cooper // but we cannot yet due to that EXPECT_DEATH only captures stderr
1120b89a7cc2SEnji Cooper // while Google Mock logs to stdout.
1121b89a7cc2SEnji Cooper #if GTEST_HAS_EXCEPTIONS
1122b89a7cc2SEnji Cooper EXPECT_ANY_THROW(a.ReturnNonDefaultConstructible());
1123b89a7cc2SEnji Cooper #else
1124b89a7cc2SEnji Cooper EXPECT_DEATH_IF_SUPPORTED(a.ReturnNonDefaultConstructible(), "");
1125b89a7cc2SEnji Cooper #endif
1126b89a7cc2SEnji Cooper }
1127b89a7cc2SEnji Cooper
1128b89a7cc2SEnji Cooper // Tests that an excessive call (one whose arguments match the
1129b89a7cc2SEnji Cooper // matchers but is called too many times) performs the default action.
TEST(ExcessiveCallTest,DoesDefaultAction)1130b89a7cc2SEnji Cooper TEST(ExcessiveCallTest, DoesDefaultAction) {
1131b89a7cc2SEnji Cooper // When there is an ON_CALL() statement, the action specified by it
1132b89a7cc2SEnji Cooper // should be taken.
1133b89a7cc2SEnji Cooper MockA a;
113428f6c2f2SEnji Cooper ON_CALL(a, Binary(_, _)).WillByDefault(Return(true));
1135b89a7cc2SEnji Cooper EXPECT_CALL(a, Binary(0, 0));
1136b89a7cc2SEnji Cooper a.Binary(0, 0);
1137b89a7cc2SEnji Cooper bool result = false;
1138b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(result = a.Binary(0, 0),
1139b89a7cc2SEnji Cooper "Mock function called more times than expected");
1140b89a7cc2SEnji Cooper EXPECT_TRUE(result);
1141b89a7cc2SEnji Cooper
1142b89a7cc2SEnji Cooper // When there is no ON_CALL(), the default value for the return type
1143b89a7cc2SEnji Cooper // should be returned.
1144b89a7cc2SEnji Cooper MockB b;
114528f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(0)).Description("DoB Method").Times(0);
1146b89a7cc2SEnji Cooper int n = -1;
114728f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
114828f6c2f2SEnji Cooper n = b.DoB(0),
114928f6c2f2SEnji Cooper "Mock function \"DoB Method\" called more times than expected");
1150b89a7cc2SEnji Cooper EXPECT_EQ(0, n);
1151b89a7cc2SEnji Cooper }
1152b89a7cc2SEnji Cooper
1153b89a7cc2SEnji Cooper // Tests that when a void function is called too many times,
1154b89a7cc2SEnji Cooper // the failure message contains the argument values.
TEST(ExcessiveCallTest,GeneratesFailureForVoidFunction)1155b89a7cc2SEnji Cooper TEST(ExcessiveCallTest, GeneratesFailureForVoidFunction) {
1156b89a7cc2SEnji Cooper MockA a;
115728f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(_)).Description("DoA Method").Times(0);
1158b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(
1159b89a7cc2SEnji Cooper a.DoA(9),
116028f6c2f2SEnji Cooper "Mock function \"DoA Method\" called more times than expected - "
116128f6c2f2SEnji Cooper "returning directly.\n"
1162b89a7cc2SEnji Cooper " Function call: DoA(9)\n"
1163b89a7cc2SEnji Cooper " Expected: to be never called\n"
1164b89a7cc2SEnji Cooper " Actual: called once - over-saturated and active");
1165b89a7cc2SEnji Cooper }
1166b89a7cc2SEnji Cooper
1167b89a7cc2SEnji Cooper // Tests that when a non-void function is called too many times, the
1168b89a7cc2SEnji Cooper // failure message contains the argument values and the return value.
TEST(ExcessiveCallTest,GeneratesFailureForNonVoidFunction)1169b89a7cc2SEnji Cooper TEST(ExcessiveCallTest, GeneratesFailureForNonVoidFunction) {
1170b89a7cc2SEnji Cooper MockB b;
1171b89a7cc2SEnji Cooper EXPECT_CALL(b, DoB(_));
1172b89a7cc2SEnji Cooper b.DoB(1);
1173b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(
1174b89a7cc2SEnji Cooper b.DoB(2),
1175b89a7cc2SEnji Cooper "Mock function called more times than expected - "
1176b89a7cc2SEnji Cooper "returning default value.\n"
1177b89a7cc2SEnji Cooper " Function call: DoB(2)\n"
1178b89a7cc2SEnji Cooper " Returns: 0\n"
1179b89a7cc2SEnji Cooper " Expected: to be called once\n"
1180b89a7cc2SEnji Cooper " Actual: called twice - over-saturated and active");
1181b89a7cc2SEnji Cooper }
1182b89a7cc2SEnji Cooper
1183b89a7cc2SEnji Cooper // Tests using sequences.
1184b89a7cc2SEnji Cooper
TEST(InSequenceTest,AllExpectationInScopeAreInSequence)1185b89a7cc2SEnji Cooper TEST(InSequenceTest, AllExpectationInScopeAreInSequence) {
1186b89a7cc2SEnji Cooper MockA a;
1187b89a7cc2SEnji Cooper {
1188b89a7cc2SEnji Cooper InSequence dummy;
1189b89a7cc2SEnji Cooper
1190b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1));
1191b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(2));
1192b89a7cc2SEnji Cooper }
1193b89a7cc2SEnji Cooper
119428f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
119528f6c2f2SEnji Cooper { // NOLINT
1196b89a7cc2SEnji Cooper a.DoA(2);
119728f6c2f2SEnji Cooper },
119828f6c2f2SEnji Cooper "Unexpected mock function call");
1199b89a7cc2SEnji Cooper
1200b89a7cc2SEnji Cooper a.DoA(1);
1201b89a7cc2SEnji Cooper a.DoA(2);
1202b89a7cc2SEnji Cooper }
1203b89a7cc2SEnji Cooper
TEST(InSequenceTest,NestedInSequence)1204b89a7cc2SEnji Cooper TEST(InSequenceTest, NestedInSequence) {
1205b89a7cc2SEnji Cooper MockA a;
1206b89a7cc2SEnji Cooper {
1207b89a7cc2SEnji Cooper InSequence dummy;
1208b89a7cc2SEnji Cooper
1209b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1));
1210b89a7cc2SEnji Cooper {
1211b89a7cc2SEnji Cooper InSequence dummy2;
1212b89a7cc2SEnji Cooper
1213b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(2));
1214b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(3));
1215b89a7cc2SEnji Cooper }
1216b89a7cc2SEnji Cooper }
1217b89a7cc2SEnji Cooper
121828f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
121928f6c2f2SEnji Cooper { // NOLINT
1220b89a7cc2SEnji Cooper a.DoA(1);
1221b89a7cc2SEnji Cooper a.DoA(3);
122228f6c2f2SEnji Cooper },
122328f6c2f2SEnji Cooper "Unexpected mock function call");
1224b89a7cc2SEnji Cooper
1225b89a7cc2SEnji Cooper a.DoA(2);
1226b89a7cc2SEnji Cooper a.DoA(3);
1227b89a7cc2SEnji Cooper }
1228b89a7cc2SEnji Cooper
TEST(InSequenceTest,ExpectationsOutOfScopeAreNotAffected)1229b89a7cc2SEnji Cooper TEST(InSequenceTest, ExpectationsOutOfScopeAreNotAffected) {
1230b89a7cc2SEnji Cooper MockA a;
1231b89a7cc2SEnji Cooper {
1232b89a7cc2SEnji Cooper InSequence dummy;
1233b89a7cc2SEnji Cooper
1234b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1));
1235b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(2));
1236b89a7cc2SEnji Cooper }
1237b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(3));
1238b89a7cc2SEnji Cooper
123928f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(
124028f6c2f2SEnji Cooper { // NOLINT
1241b89a7cc2SEnji Cooper a.DoA(2);
124228f6c2f2SEnji Cooper },
124328f6c2f2SEnji Cooper "Unexpected mock function call");
1244b89a7cc2SEnji Cooper
1245b89a7cc2SEnji Cooper a.DoA(3);
1246b89a7cc2SEnji Cooper a.DoA(1);
1247b89a7cc2SEnji Cooper a.DoA(2);
1248b89a7cc2SEnji Cooper }
1249b89a7cc2SEnji Cooper
1250b89a7cc2SEnji Cooper // Tests that any order is allowed when no sequence is used.
TEST(SequenceTest,AnyOrderIsOkByDefault)1251b89a7cc2SEnji Cooper TEST(SequenceTest, AnyOrderIsOkByDefault) {
1252b89a7cc2SEnji Cooper {
1253b89a7cc2SEnji Cooper MockA a;
1254b89a7cc2SEnji Cooper MockB b;
1255b89a7cc2SEnji Cooper
1256b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1));
125728f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).Times(AnyNumber());
1258b89a7cc2SEnji Cooper
1259b89a7cc2SEnji Cooper a.DoA(1);
1260b89a7cc2SEnji Cooper b.DoB();
1261b89a7cc2SEnji Cooper }
1262b89a7cc2SEnji Cooper
1263b89a7cc2SEnji Cooper { // NOLINT
1264b89a7cc2SEnji Cooper MockA a;
1265b89a7cc2SEnji Cooper MockB b;
1266b89a7cc2SEnji Cooper
1267b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1));
126828f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).Times(AnyNumber());
1269b89a7cc2SEnji Cooper
1270b89a7cc2SEnji Cooper b.DoB();
1271b89a7cc2SEnji Cooper a.DoA(1);
1272b89a7cc2SEnji Cooper }
1273b89a7cc2SEnji Cooper }
1274b89a7cc2SEnji Cooper
1275b89a7cc2SEnji Cooper // Tests that the calls must be in strict order when a complete order
1276b89a7cc2SEnji Cooper // is specified.
TEST(SequenceTest,CallsMustBeInStrictOrderWhenSaidSo1)1277b89a7cc2SEnji Cooper TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo1) {
1278b89a7cc2SEnji Cooper MockA a;
127928f6c2f2SEnji Cooper ON_CALL(a, ReturnResult(_)).WillByDefault(Return(Result()));
1280b89a7cc2SEnji Cooper
1281b89a7cc2SEnji Cooper Sequence s;
128228f6c2f2SEnji Cooper EXPECT_CALL(a, ReturnResult(1)).InSequence(s);
128328f6c2f2SEnji Cooper EXPECT_CALL(a, ReturnResult(2)).InSequence(s);
128428f6c2f2SEnji Cooper EXPECT_CALL(a, ReturnResult(3)).InSequence(s);
1285b89a7cc2SEnji Cooper
1286b89a7cc2SEnji Cooper a.ReturnResult(1);
1287b89a7cc2SEnji Cooper
1288b89a7cc2SEnji Cooper // May only be called after a.ReturnResult(2).
1289b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call");
1290b89a7cc2SEnji Cooper
1291b89a7cc2SEnji Cooper a.ReturnResult(2);
1292b89a7cc2SEnji Cooper a.ReturnResult(3);
1293b89a7cc2SEnji Cooper }
1294b89a7cc2SEnji Cooper
1295b89a7cc2SEnji Cooper // Tests that the calls must be in strict order when a complete order
1296b89a7cc2SEnji Cooper // is specified.
TEST(SequenceTest,CallsMustBeInStrictOrderWhenSaidSo2)1297b89a7cc2SEnji Cooper TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo2) {
1298b89a7cc2SEnji Cooper MockA a;
129928f6c2f2SEnji Cooper ON_CALL(a, ReturnResult(_)).WillByDefault(Return(Result()));
1300b89a7cc2SEnji Cooper
1301b89a7cc2SEnji Cooper Sequence s;
130228f6c2f2SEnji Cooper EXPECT_CALL(a, ReturnResult(1)).InSequence(s);
130328f6c2f2SEnji Cooper EXPECT_CALL(a, ReturnResult(2)).InSequence(s);
1304b89a7cc2SEnji Cooper
1305b89a7cc2SEnji Cooper // May only be called after a.ReturnResult(1).
1306b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(a.ReturnResult(2), "Unexpected mock function call");
1307b89a7cc2SEnji Cooper
1308b89a7cc2SEnji Cooper a.ReturnResult(1);
1309b89a7cc2SEnji Cooper a.ReturnResult(2);
1310b89a7cc2SEnji Cooper }
1311b89a7cc2SEnji Cooper
1312b89a7cc2SEnji Cooper // Tests specifying a DAG using multiple sequences.
1313b89a7cc2SEnji Cooper class PartialOrderTest : public testing::Test {
1314b89a7cc2SEnji Cooper protected:
PartialOrderTest()1315b89a7cc2SEnji Cooper PartialOrderTest() {
131628f6c2f2SEnji Cooper ON_CALL(a_, ReturnResult(_)).WillByDefault(Return(Result()));
1317b89a7cc2SEnji Cooper
1318b89a7cc2SEnji Cooper // Specifies this partial ordering:
1319b89a7cc2SEnji Cooper //
1320b89a7cc2SEnji Cooper // a.ReturnResult(1) ==>
1321b89a7cc2SEnji Cooper // a.ReturnResult(2) * n ==> a.ReturnResult(3)
1322b89a7cc2SEnji Cooper // b.DoB() * 2 ==>
1323b89a7cc2SEnji Cooper Sequence x, y;
132428f6c2f2SEnji Cooper EXPECT_CALL(a_, ReturnResult(1)).InSequence(x);
132528f6c2f2SEnji Cooper EXPECT_CALL(b_, DoB()).Times(2).InSequence(y);
132628f6c2f2SEnji Cooper EXPECT_CALL(a_, ReturnResult(2)).Times(AnyNumber()).InSequence(x, y);
132728f6c2f2SEnji Cooper EXPECT_CALL(a_, ReturnResult(3)).InSequence(x);
1328b89a7cc2SEnji Cooper }
1329b89a7cc2SEnji Cooper
1330b89a7cc2SEnji Cooper MockA a_;
1331b89a7cc2SEnji Cooper MockB b_;
1332b89a7cc2SEnji Cooper };
1333b89a7cc2SEnji Cooper
TEST_F(PartialOrderTest,CallsMustConformToSpecifiedDag1)1334b89a7cc2SEnji Cooper TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag1) {
1335b89a7cc2SEnji Cooper a_.ReturnResult(1);
1336b89a7cc2SEnji Cooper b_.DoB();
1337b89a7cc2SEnji Cooper
1338b89a7cc2SEnji Cooper // May only be called after the second DoB().
1339b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call");
1340b89a7cc2SEnji Cooper
1341b89a7cc2SEnji Cooper b_.DoB();
1342b89a7cc2SEnji Cooper a_.ReturnResult(3);
1343b89a7cc2SEnji Cooper }
1344b89a7cc2SEnji Cooper
TEST_F(PartialOrderTest,CallsMustConformToSpecifiedDag2)1345b89a7cc2SEnji Cooper TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag2) {
1346b89a7cc2SEnji Cooper // May only be called after ReturnResult(1).
1347b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call");
1348b89a7cc2SEnji Cooper
1349b89a7cc2SEnji Cooper a_.ReturnResult(1);
1350b89a7cc2SEnji Cooper b_.DoB();
1351b89a7cc2SEnji Cooper b_.DoB();
1352b89a7cc2SEnji Cooper a_.ReturnResult(3);
1353b89a7cc2SEnji Cooper }
1354b89a7cc2SEnji Cooper
TEST_F(PartialOrderTest,CallsMustConformToSpecifiedDag3)1355b89a7cc2SEnji Cooper TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag3) {
1356b89a7cc2SEnji Cooper // May only be called last.
1357b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(a_.ReturnResult(3), "Unexpected mock function call");
1358b89a7cc2SEnji Cooper
1359b89a7cc2SEnji Cooper a_.ReturnResult(1);
1360b89a7cc2SEnji Cooper b_.DoB();
1361b89a7cc2SEnji Cooper b_.DoB();
1362b89a7cc2SEnji Cooper a_.ReturnResult(3);
1363b89a7cc2SEnji Cooper }
1364b89a7cc2SEnji Cooper
TEST_F(PartialOrderTest,CallsMustConformToSpecifiedDag4)1365b89a7cc2SEnji Cooper TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag4) {
1366b89a7cc2SEnji Cooper a_.ReturnResult(1);
1367b89a7cc2SEnji Cooper b_.DoB();
1368b89a7cc2SEnji Cooper b_.DoB();
1369b89a7cc2SEnji Cooper a_.ReturnResult(3);
1370b89a7cc2SEnji Cooper
1371b89a7cc2SEnji Cooper // May only be called before ReturnResult(3).
1372b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(a_.ReturnResult(2), "Unexpected mock function call");
1373b89a7cc2SEnji Cooper }
1374b89a7cc2SEnji Cooper
TEST(SequenceTest,Retirement)1375b89a7cc2SEnji Cooper TEST(SequenceTest, Retirement) {
1376b89a7cc2SEnji Cooper MockA a;
1377b89a7cc2SEnji Cooper Sequence s;
1378b89a7cc2SEnji Cooper
137928f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).InSequence(s);
138028f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(_)).InSequence(s).RetiresOnSaturation();
138128f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(1)).InSequence(s);
1382b89a7cc2SEnji Cooper
1383b89a7cc2SEnji Cooper a.DoA(1);
1384b89a7cc2SEnji Cooper a.DoA(2);
1385b89a7cc2SEnji Cooper a.DoA(1);
1386b89a7cc2SEnji Cooper }
1387b89a7cc2SEnji Cooper
1388b89a7cc2SEnji Cooper // Tests Expectation.
1389b89a7cc2SEnji Cooper
TEST(ExpectationTest,ConstrutorsWork)1390b89a7cc2SEnji Cooper TEST(ExpectationTest, ConstrutorsWork) {
1391b89a7cc2SEnji Cooper MockA a;
1392b89a7cc2SEnji Cooper Expectation e1; // Default ctor.
1393b89a7cc2SEnji Cooper
1394b89a7cc2SEnji Cooper // Ctor from various forms of EXPECT_CALL.
1395b89a7cc2SEnji Cooper Expectation e2 = EXPECT_CALL(a, DoA(2));
1396b89a7cc2SEnji Cooper Expectation e3 = EXPECT_CALL(a, DoA(3)).With(_);
1397b89a7cc2SEnji Cooper {
1398b89a7cc2SEnji Cooper Sequence s;
1399b89a7cc2SEnji Cooper Expectation e4 = EXPECT_CALL(a, DoA(4)).Times(1);
1400b89a7cc2SEnji Cooper Expectation e5 = EXPECT_CALL(a, DoA(5)).InSequence(s);
1401b89a7cc2SEnji Cooper }
1402b89a7cc2SEnji Cooper Expectation e6 = EXPECT_CALL(a, DoA(6)).After(e2);
1403b89a7cc2SEnji Cooper Expectation e7 = EXPECT_CALL(a, DoA(7)).WillOnce(Return());
1404b89a7cc2SEnji Cooper Expectation e8 = EXPECT_CALL(a, DoA(8)).WillRepeatedly(Return());
1405b89a7cc2SEnji Cooper Expectation e9 = EXPECT_CALL(a, DoA(9)).RetiresOnSaturation();
1406b89a7cc2SEnji Cooper
1407b89a7cc2SEnji Cooper Expectation e10 = e2; // Copy ctor.
1408b89a7cc2SEnji Cooper
1409b89a7cc2SEnji Cooper EXPECT_THAT(e1, Ne(e2));
1410b89a7cc2SEnji Cooper EXPECT_THAT(e2, Eq(e10));
1411b89a7cc2SEnji Cooper
1412b89a7cc2SEnji Cooper a.DoA(2);
1413b89a7cc2SEnji Cooper a.DoA(3);
1414b89a7cc2SEnji Cooper a.DoA(4);
1415b89a7cc2SEnji Cooper a.DoA(5);
1416b89a7cc2SEnji Cooper a.DoA(6);
1417b89a7cc2SEnji Cooper a.DoA(7);
1418b89a7cc2SEnji Cooper a.DoA(8);
1419b89a7cc2SEnji Cooper a.DoA(9);
1420b89a7cc2SEnji Cooper }
1421b89a7cc2SEnji Cooper
TEST(ExpectationTest,AssignmentWorks)1422b89a7cc2SEnji Cooper TEST(ExpectationTest, AssignmentWorks) {
1423b89a7cc2SEnji Cooper MockA a;
1424b89a7cc2SEnji Cooper Expectation e1;
1425b89a7cc2SEnji Cooper Expectation e2 = EXPECT_CALL(a, DoA(1));
1426b89a7cc2SEnji Cooper
1427b89a7cc2SEnji Cooper EXPECT_THAT(e1, Ne(e2));
1428b89a7cc2SEnji Cooper
1429b89a7cc2SEnji Cooper e1 = e2;
1430b89a7cc2SEnji Cooper EXPECT_THAT(e1, Eq(e2));
1431b89a7cc2SEnji Cooper
1432b89a7cc2SEnji Cooper a.DoA(1);
1433b89a7cc2SEnji Cooper }
1434b89a7cc2SEnji Cooper
1435b89a7cc2SEnji Cooper // Tests ExpectationSet.
1436b89a7cc2SEnji Cooper
TEST(ExpectationSetTest,MemberTypesAreCorrect)1437b89a7cc2SEnji Cooper TEST(ExpectationSetTest, MemberTypesAreCorrect) {
1438b89a7cc2SEnji Cooper ::testing::StaticAssertTypeEq<Expectation, ExpectationSet::value_type>();
1439b89a7cc2SEnji Cooper }
1440b89a7cc2SEnji Cooper
TEST(ExpectationSetTest,ConstructorsWork)1441b89a7cc2SEnji Cooper TEST(ExpectationSetTest, ConstructorsWork) {
1442b89a7cc2SEnji Cooper MockA a;
1443b89a7cc2SEnji Cooper
1444b89a7cc2SEnji Cooper Expectation e1;
1445b89a7cc2SEnji Cooper const Expectation e2;
1446b89a7cc2SEnji Cooper ExpectationSet es1; // Default ctor.
1447b89a7cc2SEnji Cooper ExpectationSet es2 = EXPECT_CALL(a, DoA(1)); // Ctor from EXPECT_CALL.
1448b89a7cc2SEnji Cooper ExpectationSet es3 = e1; // Ctor from Expectation.
1449b89a7cc2SEnji Cooper ExpectationSet es4(e1); // Ctor from Expectation; alternative syntax.
1450b89a7cc2SEnji Cooper ExpectationSet es5 = e2; // Ctor from const Expectation.
1451b89a7cc2SEnji Cooper ExpectationSet es6(e2); // Ctor from const Expectation; alternative syntax.
1452b89a7cc2SEnji Cooper ExpectationSet es7 = es2; // Copy ctor.
1453b89a7cc2SEnji Cooper
1454b89a7cc2SEnji Cooper EXPECT_EQ(0, es1.size());
1455b89a7cc2SEnji Cooper EXPECT_EQ(1, es2.size());
1456b89a7cc2SEnji Cooper EXPECT_EQ(1, es3.size());
1457b89a7cc2SEnji Cooper EXPECT_EQ(1, es4.size());
1458b89a7cc2SEnji Cooper EXPECT_EQ(1, es5.size());
1459b89a7cc2SEnji Cooper EXPECT_EQ(1, es6.size());
1460b89a7cc2SEnji Cooper EXPECT_EQ(1, es7.size());
1461b89a7cc2SEnji Cooper
1462b89a7cc2SEnji Cooper EXPECT_THAT(es3, Ne(es2));
1463b89a7cc2SEnji Cooper EXPECT_THAT(es4, Eq(es3));
1464b89a7cc2SEnji Cooper EXPECT_THAT(es5, Eq(es4));
1465b89a7cc2SEnji Cooper EXPECT_THAT(es6, Eq(es5));
1466b89a7cc2SEnji Cooper EXPECT_THAT(es7, Eq(es2));
1467b89a7cc2SEnji Cooper a.DoA(1);
1468b89a7cc2SEnji Cooper }
1469b89a7cc2SEnji Cooper
TEST(ExpectationSetTest,AssignmentWorks)1470b89a7cc2SEnji Cooper TEST(ExpectationSetTest, AssignmentWorks) {
1471b89a7cc2SEnji Cooper ExpectationSet es1;
1472b89a7cc2SEnji Cooper ExpectationSet es2 = Expectation();
1473b89a7cc2SEnji Cooper
1474b89a7cc2SEnji Cooper es1 = es2;
1475b89a7cc2SEnji Cooper EXPECT_EQ(1, es1.size());
1476b89a7cc2SEnji Cooper EXPECT_THAT(*(es1.begin()), Eq(Expectation()));
1477b89a7cc2SEnji Cooper EXPECT_THAT(es1, Eq(es2));
1478b89a7cc2SEnji Cooper }
1479b89a7cc2SEnji Cooper
TEST(ExpectationSetTest,InsertionWorks)1480b89a7cc2SEnji Cooper TEST(ExpectationSetTest, InsertionWorks) {
1481b89a7cc2SEnji Cooper ExpectationSet es1;
1482b89a7cc2SEnji Cooper Expectation e1;
1483b89a7cc2SEnji Cooper es1 += e1;
1484b89a7cc2SEnji Cooper EXPECT_EQ(1, es1.size());
1485b89a7cc2SEnji Cooper EXPECT_THAT(*(es1.begin()), Eq(e1));
1486b89a7cc2SEnji Cooper
1487b89a7cc2SEnji Cooper MockA a;
1488b89a7cc2SEnji Cooper Expectation e2 = EXPECT_CALL(a, DoA(1));
1489b89a7cc2SEnji Cooper es1 += e2;
1490b89a7cc2SEnji Cooper EXPECT_EQ(2, es1.size());
1491b89a7cc2SEnji Cooper
1492b89a7cc2SEnji Cooper ExpectationSet::const_iterator it1 = es1.begin();
1493b89a7cc2SEnji Cooper ExpectationSet::const_iterator it2 = it1;
1494b89a7cc2SEnji Cooper ++it2;
1495b89a7cc2SEnji Cooper EXPECT_TRUE(*it1 == e1 || *it2 == e1); // e1 must be in the set.
1496b89a7cc2SEnji Cooper EXPECT_TRUE(*it1 == e2 || *it2 == e2); // e2 must be in the set too.
1497b89a7cc2SEnji Cooper a.DoA(1);
1498b89a7cc2SEnji Cooper }
1499b89a7cc2SEnji Cooper
TEST(ExpectationSetTest,SizeWorks)1500b89a7cc2SEnji Cooper TEST(ExpectationSetTest, SizeWorks) {
1501b89a7cc2SEnji Cooper ExpectationSet es;
1502b89a7cc2SEnji Cooper EXPECT_EQ(0, es.size());
1503b89a7cc2SEnji Cooper
1504b89a7cc2SEnji Cooper es += Expectation();
1505b89a7cc2SEnji Cooper EXPECT_EQ(1, es.size());
1506b89a7cc2SEnji Cooper
1507b89a7cc2SEnji Cooper MockA a;
1508b89a7cc2SEnji Cooper es += EXPECT_CALL(a, DoA(1));
1509b89a7cc2SEnji Cooper EXPECT_EQ(2, es.size());
1510b89a7cc2SEnji Cooper
1511b89a7cc2SEnji Cooper a.DoA(1);
1512b89a7cc2SEnji Cooper }
1513b89a7cc2SEnji Cooper
TEST(ExpectationSetTest,IsEnumerable)1514b89a7cc2SEnji Cooper TEST(ExpectationSetTest, IsEnumerable) {
1515b89a7cc2SEnji Cooper ExpectationSet es;
1516b89a7cc2SEnji Cooper EXPECT_TRUE(es.begin() == es.end());
1517b89a7cc2SEnji Cooper
1518b89a7cc2SEnji Cooper es += Expectation();
1519b89a7cc2SEnji Cooper ExpectationSet::const_iterator it = es.begin();
1520b89a7cc2SEnji Cooper EXPECT_TRUE(it != es.end());
1521b89a7cc2SEnji Cooper EXPECT_THAT(*it, Eq(Expectation()));
1522b89a7cc2SEnji Cooper ++it;
1523b89a7cc2SEnji Cooper EXPECT_TRUE(it == es.end());
1524b89a7cc2SEnji Cooper }
1525b89a7cc2SEnji Cooper
1526b89a7cc2SEnji Cooper // Tests the .After() clause.
1527b89a7cc2SEnji Cooper
TEST(AfterTest,SucceedsWhenPartialOrderIsSatisfied)1528b89a7cc2SEnji Cooper TEST(AfterTest, SucceedsWhenPartialOrderIsSatisfied) {
1529b89a7cc2SEnji Cooper MockA a;
1530b89a7cc2SEnji Cooper ExpectationSet es;
1531b89a7cc2SEnji Cooper es += EXPECT_CALL(a, DoA(1));
1532b89a7cc2SEnji Cooper es += EXPECT_CALL(a, DoA(2));
153328f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(3)).After(es);
1534b89a7cc2SEnji Cooper
1535b89a7cc2SEnji Cooper a.DoA(1);
1536b89a7cc2SEnji Cooper a.DoA(2);
1537b89a7cc2SEnji Cooper a.DoA(3);
1538b89a7cc2SEnji Cooper }
1539b89a7cc2SEnji Cooper
TEST(AfterTest,SucceedsWhenTotalOrderIsSatisfied)1540b89a7cc2SEnji Cooper TEST(AfterTest, SucceedsWhenTotalOrderIsSatisfied) {
1541b89a7cc2SEnji Cooper MockA a;
1542b89a7cc2SEnji Cooper MockB b;
1543b89a7cc2SEnji Cooper // The following also verifies that const Expectation objects work
1544b89a7cc2SEnji Cooper // too. Do not remove the const modifiers.
1545b89a7cc2SEnji Cooper const Expectation e1 = EXPECT_CALL(a, DoA(1));
154628f6c2f2SEnji Cooper const Expectation e2 = EXPECT_CALL(b, DoB()).Times(2).After(e1);
1547b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(2)).After(e2);
1548b89a7cc2SEnji Cooper
1549b89a7cc2SEnji Cooper a.DoA(1);
1550b89a7cc2SEnji Cooper b.DoB();
1551b89a7cc2SEnji Cooper b.DoB();
1552b89a7cc2SEnji Cooper a.DoA(2);
1553b89a7cc2SEnji Cooper }
1554b89a7cc2SEnji Cooper
1555b89a7cc2SEnji Cooper // Calls must be in strict order when specified so using .After().
TEST(AfterTest,CallsMustBeInStrictOrderWhenSpecifiedSo1)1556b89a7cc2SEnji Cooper TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo1) {
1557b89a7cc2SEnji Cooper MockA a;
1558b89a7cc2SEnji Cooper MockB b;
1559b89a7cc2SEnji Cooper
1560b89a7cc2SEnji Cooper // Define ordering:
1561b89a7cc2SEnji Cooper // a.DoA(1) ==> b.DoB() ==> a.DoA(2)
1562b89a7cc2SEnji Cooper Expectation e1 = EXPECT_CALL(a, DoA(1));
156328f6c2f2SEnji Cooper Expectation e2 = EXPECT_CALL(b, DoB()).After(e1);
156428f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(2)).After(e2);
1565b89a7cc2SEnji Cooper
1566b89a7cc2SEnji Cooper a.DoA(1);
1567b89a7cc2SEnji Cooper
1568b89a7cc2SEnji Cooper // May only be called after DoB().
1569b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(a.DoA(2), "Unexpected mock function call");
1570b89a7cc2SEnji Cooper
1571b89a7cc2SEnji Cooper b.DoB();
1572b89a7cc2SEnji Cooper a.DoA(2);
1573b89a7cc2SEnji Cooper }
1574b89a7cc2SEnji Cooper
1575b89a7cc2SEnji Cooper // Calls must be in strict order when specified so using .After().
TEST(AfterTest,CallsMustBeInStrictOrderWhenSpecifiedSo2)1576b89a7cc2SEnji Cooper TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo2) {
1577b89a7cc2SEnji Cooper MockA a;
1578b89a7cc2SEnji Cooper MockB b;
1579b89a7cc2SEnji Cooper
1580b89a7cc2SEnji Cooper // Define ordering:
1581b89a7cc2SEnji Cooper // a.DoA(1) ==> b.DoB() * 2 ==> a.DoA(2)
1582b89a7cc2SEnji Cooper Expectation e1 = EXPECT_CALL(a, DoA(1));
158328f6c2f2SEnji Cooper Expectation e2 = EXPECT_CALL(b, DoB()).Times(2).After(e1);
158428f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(2)).After(e2);
1585b89a7cc2SEnji Cooper
1586b89a7cc2SEnji Cooper a.DoA(1);
1587b89a7cc2SEnji Cooper b.DoB();
1588b89a7cc2SEnji Cooper
1589b89a7cc2SEnji Cooper // May only be called after the second DoB().
1590b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(a.DoA(2), "Unexpected mock function call");
1591b89a7cc2SEnji Cooper
1592b89a7cc2SEnji Cooper b.DoB();
1593b89a7cc2SEnji Cooper a.DoA(2);
1594b89a7cc2SEnji Cooper }
1595b89a7cc2SEnji Cooper
1596b89a7cc2SEnji Cooper // Calls must satisfy the partial order when specified so.
TEST(AfterTest,CallsMustSatisfyPartialOrderWhenSpecifiedSo)1597b89a7cc2SEnji Cooper TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo) {
1598b89a7cc2SEnji Cooper MockA a;
159928f6c2f2SEnji Cooper ON_CALL(a, ReturnResult(_)).WillByDefault(Return(Result()));
1600b89a7cc2SEnji Cooper
1601b89a7cc2SEnji Cooper // Define ordering:
1602b89a7cc2SEnji Cooper // a.DoA(1) ==>
1603b89a7cc2SEnji Cooper // a.DoA(2) ==> a.ReturnResult(3)
1604b89a7cc2SEnji Cooper Expectation e = EXPECT_CALL(a, DoA(1));
1605b89a7cc2SEnji Cooper const ExpectationSet es = EXPECT_CALL(a, DoA(2));
160628f6c2f2SEnji Cooper EXPECT_CALL(a, ReturnResult(3)).After(e, es);
1607b89a7cc2SEnji Cooper
1608b89a7cc2SEnji Cooper // May only be called last.
1609b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call");
1610b89a7cc2SEnji Cooper
1611b89a7cc2SEnji Cooper a.DoA(2);
1612b89a7cc2SEnji Cooper a.DoA(1);
1613b89a7cc2SEnji Cooper a.ReturnResult(3);
1614b89a7cc2SEnji Cooper }
1615b89a7cc2SEnji Cooper
1616b89a7cc2SEnji Cooper // Calls must satisfy the partial order when specified so.
TEST(AfterTest,CallsMustSatisfyPartialOrderWhenSpecifiedSo2)1617b89a7cc2SEnji Cooper TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo2) {
1618b89a7cc2SEnji Cooper MockA a;
1619b89a7cc2SEnji Cooper
1620b89a7cc2SEnji Cooper // Define ordering:
1621b89a7cc2SEnji Cooper // a.DoA(1) ==>
1622b89a7cc2SEnji Cooper // a.DoA(2) ==> a.DoA(3)
1623b89a7cc2SEnji Cooper Expectation e = EXPECT_CALL(a, DoA(1));
1624b89a7cc2SEnji Cooper const ExpectationSet es = EXPECT_CALL(a, DoA(2));
162528f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(3)).After(e, es);
1626b89a7cc2SEnji Cooper
1627b89a7cc2SEnji Cooper a.DoA(2);
1628b89a7cc2SEnji Cooper
1629b89a7cc2SEnji Cooper // May only be called last.
1630b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(a.DoA(3), "Unexpected mock function call");
1631b89a7cc2SEnji Cooper
1632b89a7cc2SEnji Cooper a.DoA(1);
1633b89a7cc2SEnji Cooper a.DoA(3);
1634b89a7cc2SEnji Cooper }
1635b89a7cc2SEnji Cooper
1636b89a7cc2SEnji Cooper // .After() can be combined with .InSequence().
TEST(AfterTest,CanBeUsedWithInSequence)1637b89a7cc2SEnji Cooper TEST(AfterTest, CanBeUsedWithInSequence) {
1638b89a7cc2SEnji Cooper MockA a;
1639b89a7cc2SEnji Cooper Sequence s;
1640b89a7cc2SEnji Cooper Expectation e = EXPECT_CALL(a, DoA(1));
1641b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(2)).InSequence(s);
164228f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(3)).InSequence(s).After(e);
1643b89a7cc2SEnji Cooper
1644b89a7cc2SEnji Cooper a.DoA(1);
1645b89a7cc2SEnji Cooper
1646b89a7cc2SEnji Cooper // May only be after DoA(2).
1647b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(a.DoA(3), "Unexpected mock function call");
1648b89a7cc2SEnji Cooper
1649b89a7cc2SEnji Cooper a.DoA(2);
1650b89a7cc2SEnji Cooper a.DoA(3);
1651b89a7cc2SEnji Cooper }
1652b89a7cc2SEnji Cooper
1653b89a7cc2SEnji Cooper // .After() can be called multiple times.
TEST(AfterTest,CanBeCalledManyTimes)1654b89a7cc2SEnji Cooper TEST(AfterTest, CanBeCalledManyTimes) {
1655b89a7cc2SEnji Cooper MockA a;
1656b89a7cc2SEnji Cooper Expectation e1 = EXPECT_CALL(a, DoA(1));
1657b89a7cc2SEnji Cooper Expectation e2 = EXPECT_CALL(a, DoA(2));
1658b89a7cc2SEnji Cooper Expectation e3 = EXPECT_CALL(a, DoA(3));
165928f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(4)).After(e1).After(e2).After(e3);
1660b89a7cc2SEnji Cooper
1661b89a7cc2SEnji Cooper a.DoA(3);
1662b89a7cc2SEnji Cooper a.DoA(1);
1663b89a7cc2SEnji Cooper a.DoA(2);
1664b89a7cc2SEnji Cooper a.DoA(4);
1665b89a7cc2SEnji Cooper }
1666b89a7cc2SEnji Cooper
1667b89a7cc2SEnji Cooper // .After() accepts up to 5 arguments.
TEST(AfterTest,AcceptsUpToFiveArguments)1668b89a7cc2SEnji Cooper TEST(AfterTest, AcceptsUpToFiveArguments) {
1669b89a7cc2SEnji Cooper MockA a;
1670b89a7cc2SEnji Cooper Expectation e1 = EXPECT_CALL(a, DoA(1));
1671b89a7cc2SEnji Cooper Expectation e2 = EXPECT_CALL(a, DoA(2));
1672b89a7cc2SEnji Cooper Expectation e3 = EXPECT_CALL(a, DoA(3));
1673b89a7cc2SEnji Cooper ExpectationSet es1 = EXPECT_CALL(a, DoA(4));
1674b89a7cc2SEnji Cooper ExpectationSet es2 = EXPECT_CALL(a, DoA(5));
167528f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(6)).After(e1, e2, e3, es1, es2);
1676b89a7cc2SEnji Cooper
1677b89a7cc2SEnji Cooper a.DoA(5);
1678b89a7cc2SEnji Cooper a.DoA(2);
1679b89a7cc2SEnji Cooper a.DoA(4);
1680b89a7cc2SEnji Cooper a.DoA(1);
1681b89a7cc2SEnji Cooper a.DoA(3);
1682b89a7cc2SEnji Cooper a.DoA(6);
1683b89a7cc2SEnji Cooper }
1684b89a7cc2SEnji Cooper
1685b89a7cc2SEnji Cooper // .After() allows input to contain duplicated Expectations.
TEST(AfterTest,AcceptsDuplicatedInput)1686b89a7cc2SEnji Cooper TEST(AfterTest, AcceptsDuplicatedInput) {
1687b89a7cc2SEnji Cooper MockA a;
168828f6c2f2SEnji Cooper ON_CALL(a, ReturnResult(_)).WillByDefault(Return(Result()));
1689b89a7cc2SEnji Cooper
1690b89a7cc2SEnji Cooper // Define ordering:
1691b89a7cc2SEnji Cooper // DoA(1) ==>
1692b89a7cc2SEnji Cooper // DoA(2) ==> ReturnResult(3)
1693b89a7cc2SEnji Cooper Expectation e1 = EXPECT_CALL(a, DoA(1));
1694b89a7cc2SEnji Cooper Expectation e2 = EXPECT_CALL(a, DoA(2));
1695b89a7cc2SEnji Cooper ExpectationSet es;
1696b89a7cc2SEnji Cooper es += e1;
1697b89a7cc2SEnji Cooper es += e2;
169828f6c2f2SEnji Cooper EXPECT_CALL(a, ReturnResult(3)).After(e1, e2, es, e1);
1699b89a7cc2SEnji Cooper
1700b89a7cc2SEnji Cooper a.DoA(1);
1701b89a7cc2SEnji Cooper
1702b89a7cc2SEnji Cooper // May only be after DoA(2).
1703b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(a.ReturnResult(3), "Unexpected mock function call");
1704b89a7cc2SEnji Cooper
1705b89a7cc2SEnji Cooper a.DoA(2);
1706b89a7cc2SEnji Cooper a.ReturnResult(3);
1707b89a7cc2SEnji Cooper }
1708b89a7cc2SEnji Cooper
1709b89a7cc2SEnji Cooper // An Expectation added to an ExpectationSet after it has been used in
1710b89a7cc2SEnji Cooper // an .After() has no effect.
TEST(AfterTest,ChangesToExpectationSetHaveNoEffectAfterwards)1711b89a7cc2SEnji Cooper TEST(AfterTest, ChangesToExpectationSetHaveNoEffectAfterwards) {
1712b89a7cc2SEnji Cooper MockA a;
1713b89a7cc2SEnji Cooper ExpectationSet es1 = EXPECT_CALL(a, DoA(1));
1714b89a7cc2SEnji Cooper Expectation e2 = EXPECT_CALL(a, DoA(2));
171528f6c2f2SEnji Cooper EXPECT_CALL(a, DoA(3)).After(es1);
1716b89a7cc2SEnji Cooper es1 += e2;
1717b89a7cc2SEnji Cooper
1718b89a7cc2SEnji Cooper a.DoA(1);
1719b89a7cc2SEnji Cooper a.DoA(3);
1720b89a7cc2SEnji Cooper a.DoA(2);
1721b89a7cc2SEnji Cooper }
1722b89a7cc2SEnji Cooper
1723b89a7cc2SEnji Cooper // Tests that Google Mock correctly handles calls to mock functions
1724b89a7cc2SEnji Cooper // after a mock object owning one of their pre-requisites has died.
1725b89a7cc2SEnji Cooper
1726b89a7cc2SEnji Cooper // Tests that calls that satisfy the original spec are successful.
TEST(DeletingMockEarlyTest,Success1)1727b89a7cc2SEnji Cooper TEST(DeletingMockEarlyTest, Success1) {
1728b89a7cc2SEnji Cooper MockB* const b1 = new MockB;
1729b89a7cc2SEnji Cooper MockA* const a = new MockA;
1730b89a7cc2SEnji Cooper MockB* const b2 = new MockB;
1731b89a7cc2SEnji Cooper
1732b89a7cc2SEnji Cooper {
1733b89a7cc2SEnji Cooper InSequence dummy;
173428f6c2f2SEnji Cooper EXPECT_CALL(*b1, DoB(_)).WillOnce(Return(1));
1735b89a7cc2SEnji Cooper EXPECT_CALL(*a, Binary(_, _))
1736b89a7cc2SEnji Cooper .Times(AnyNumber())
1737b89a7cc2SEnji Cooper .WillRepeatedly(Return(true));
173828f6c2f2SEnji Cooper EXPECT_CALL(*b2, DoB(_)).Times(AnyNumber()).WillRepeatedly(Return(2));
1739b89a7cc2SEnji Cooper }
1740b89a7cc2SEnji Cooper
1741b89a7cc2SEnji Cooper EXPECT_EQ(1, b1->DoB(1));
1742b89a7cc2SEnji Cooper delete b1;
1743b89a7cc2SEnji Cooper // a's pre-requisite has died.
1744b89a7cc2SEnji Cooper EXPECT_TRUE(a->Binary(0, 1));
1745b89a7cc2SEnji Cooper delete b2;
1746b89a7cc2SEnji Cooper // a's successor has died.
1747b89a7cc2SEnji Cooper EXPECT_TRUE(a->Binary(1, 2));
1748b89a7cc2SEnji Cooper delete a;
1749b89a7cc2SEnji Cooper }
1750b89a7cc2SEnji Cooper
1751b89a7cc2SEnji Cooper // Tests that calls that satisfy the original spec are successful.
TEST(DeletingMockEarlyTest,Success2)1752b89a7cc2SEnji Cooper TEST(DeletingMockEarlyTest, Success2) {
1753b89a7cc2SEnji Cooper MockB* const b1 = new MockB;
1754b89a7cc2SEnji Cooper MockA* const a = new MockA;
1755b89a7cc2SEnji Cooper MockB* const b2 = new MockB;
1756b89a7cc2SEnji Cooper
1757b89a7cc2SEnji Cooper {
1758b89a7cc2SEnji Cooper InSequence dummy;
175928f6c2f2SEnji Cooper EXPECT_CALL(*b1, DoB(_)).WillOnce(Return(1));
176028f6c2f2SEnji Cooper EXPECT_CALL(*a, Binary(_, _)).Times(AnyNumber());
176128f6c2f2SEnji Cooper EXPECT_CALL(*b2, DoB(_)).Times(AnyNumber()).WillRepeatedly(Return(2));
1762b89a7cc2SEnji Cooper }
1763b89a7cc2SEnji Cooper
1764b89a7cc2SEnji Cooper delete a; // a is trivially satisfied.
1765b89a7cc2SEnji Cooper EXPECT_EQ(1, b1->DoB(1));
1766b89a7cc2SEnji Cooper EXPECT_EQ(2, b2->DoB(2));
1767b89a7cc2SEnji Cooper delete b1;
1768b89a7cc2SEnji Cooper delete b2;
1769b89a7cc2SEnji Cooper }
1770b89a7cc2SEnji Cooper
1771b89a7cc2SEnji Cooper // Tests that it's OK to delete a mock object itself in its action.
1772b89a7cc2SEnji Cooper
1773b89a7cc2SEnji Cooper // Suppresses warning on unreferenced formal parameter in MSVC with
1774b89a7cc2SEnji Cooper // -W4.
177528f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
1776b89a7cc2SEnji Cooper
ACTION_P(Delete,ptr)1777b89a7cc2SEnji Cooper ACTION_P(Delete, ptr) { delete ptr; }
1778b89a7cc2SEnji Cooper
177928f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
1780b89a7cc2SEnji Cooper
TEST(DeletingMockEarlyTest,CanDeleteSelfInActionReturningVoid)1781b89a7cc2SEnji Cooper TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningVoid) {
1782b89a7cc2SEnji Cooper MockA* const a = new MockA;
1783b89a7cc2SEnji Cooper EXPECT_CALL(*a, DoA(_)).WillOnce(Delete(a));
1784b89a7cc2SEnji Cooper a->DoA(42); // This will cause a to be deleted.
1785b89a7cc2SEnji Cooper }
1786b89a7cc2SEnji Cooper
TEST(DeletingMockEarlyTest,CanDeleteSelfInActionReturningValue)1787b89a7cc2SEnji Cooper TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningValue) {
1788b89a7cc2SEnji Cooper MockA* const a = new MockA;
178928f6c2f2SEnji Cooper EXPECT_CALL(*a, ReturnResult(_)).WillOnce(DoAll(Delete(a), Return(Result())));
1790b89a7cc2SEnji Cooper a->ReturnResult(42); // This will cause a to be deleted.
1791b89a7cc2SEnji Cooper }
1792b89a7cc2SEnji Cooper
1793b89a7cc2SEnji Cooper // Tests that calls that violate the original spec yield failures.
TEST(DeletingMockEarlyTest,Failure1)1794b89a7cc2SEnji Cooper TEST(DeletingMockEarlyTest, Failure1) {
1795b89a7cc2SEnji Cooper MockB* const b1 = new MockB;
1796b89a7cc2SEnji Cooper MockA* const a = new MockA;
1797b89a7cc2SEnji Cooper MockB* const b2 = new MockB;
1798b89a7cc2SEnji Cooper
1799b89a7cc2SEnji Cooper {
1800b89a7cc2SEnji Cooper InSequence dummy;
180128f6c2f2SEnji Cooper EXPECT_CALL(*b1, DoB(_)).WillOnce(Return(1));
180228f6c2f2SEnji Cooper EXPECT_CALL(*a, Binary(_, _)).Times(AnyNumber());
180328f6c2f2SEnji Cooper EXPECT_CALL(*b2, DoB(_)).Times(AnyNumber()).WillRepeatedly(Return(2));
1804b89a7cc2SEnji Cooper }
1805b89a7cc2SEnji Cooper
1806b89a7cc2SEnji Cooper delete a; // a is trivially satisfied.
180728f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE({ b2->DoB(2); }, "Unexpected mock function call");
1808b89a7cc2SEnji Cooper EXPECT_EQ(1, b1->DoB(1));
1809b89a7cc2SEnji Cooper delete b1;
1810b89a7cc2SEnji Cooper delete b2;
1811b89a7cc2SEnji Cooper }
1812b89a7cc2SEnji Cooper
1813b89a7cc2SEnji Cooper // Tests that calls that violate the original spec yield failures.
TEST(DeletingMockEarlyTest,Failure2)1814b89a7cc2SEnji Cooper TEST(DeletingMockEarlyTest, Failure2) {
1815b89a7cc2SEnji Cooper MockB* const b1 = new MockB;
1816b89a7cc2SEnji Cooper MockA* const a = new MockA;
1817b89a7cc2SEnji Cooper MockB* const b2 = new MockB;
1818b89a7cc2SEnji Cooper
1819b89a7cc2SEnji Cooper {
1820b89a7cc2SEnji Cooper InSequence dummy;
1821b89a7cc2SEnji Cooper EXPECT_CALL(*b1, DoB(_));
182228f6c2f2SEnji Cooper EXPECT_CALL(*a, Binary(_, _)).Times(AnyNumber());
182328f6c2f2SEnji Cooper EXPECT_CALL(*b2, DoB(_)).Times(AnyNumber());
1824b89a7cc2SEnji Cooper }
1825b89a7cc2SEnji Cooper
182628f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(delete b1, "Actual: never called");
182728f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(a->Binary(0, 1), "Unexpected mock function call");
182828f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE(b2->DoB(1), "Unexpected mock function call");
1829b89a7cc2SEnji Cooper delete a;
1830b89a7cc2SEnji Cooper delete b2;
1831b89a7cc2SEnji Cooper }
1832b89a7cc2SEnji Cooper
1833b89a7cc2SEnji Cooper class EvenNumberCardinality : public CardinalityInterface {
1834b89a7cc2SEnji Cooper public:
183528f6c2f2SEnji Cooper // Returns true if and only if call_count calls will satisfy this
183628f6c2f2SEnji Cooper // cardinality.
IsSatisfiedByCallCount(int call_count) const183728f6c2f2SEnji Cooper bool IsSatisfiedByCallCount(int call_count) const override {
1838b89a7cc2SEnji Cooper return call_count % 2 == 0;
1839b89a7cc2SEnji Cooper }
1840b89a7cc2SEnji Cooper
184128f6c2f2SEnji Cooper // Returns true if and only if call_count calls will saturate this
184228f6c2f2SEnji Cooper // cardinality.
IsSaturatedByCallCount(int) const184328f6c2f2SEnji Cooper bool IsSaturatedByCallCount(int /* call_count */) const override {
1844b89a7cc2SEnji Cooper return false;
1845b89a7cc2SEnji Cooper }
1846b89a7cc2SEnji Cooper
1847b89a7cc2SEnji Cooper // Describes self to an ostream.
DescribeTo(::std::ostream * os) const184828f6c2f2SEnji Cooper void DescribeTo(::std::ostream* os) const override {
1849b89a7cc2SEnji Cooper *os << "called even number of times";
1850b89a7cc2SEnji Cooper }
1851b89a7cc2SEnji Cooper };
1852b89a7cc2SEnji Cooper
EvenNumber()185328f6c2f2SEnji Cooper Cardinality EvenNumber() { return Cardinality(new EvenNumberCardinality); }
1854b89a7cc2SEnji Cooper
TEST(ExpectationBaseTest,AllPrerequisitesAreSatisfiedWorksForNonMonotonicCardinality)1855b89a7cc2SEnji Cooper TEST(ExpectationBaseTest,
1856b89a7cc2SEnji Cooper AllPrerequisitesAreSatisfiedWorksForNonMonotonicCardinality) {
1857b89a7cc2SEnji Cooper MockA* a = new MockA;
1858b89a7cc2SEnji Cooper Sequence s;
1859b89a7cc2SEnji Cooper
186028f6c2f2SEnji Cooper EXPECT_CALL(*a, DoA(1)).Times(EvenNumber()).InSequence(s);
186128f6c2f2SEnji Cooper EXPECT_CALL(*a, DoA(2)).Times(AnyNumber()).InSequence(s);
186228f6c2f2SEnji Cooper EXPECT_CALL(*a, DoA(3)).Times(AnyNumber());
1863b89a7cc2SEnji Cooper
1864b89a7cc2SEnji Cooper a->DoA(3);
1865b89a7cc2SEnji Cooper a->DoA(1);
1866b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(a->DoA(2), "Unexpected mock function call");
1867b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(delete a, "to be called even number of times");
1868b89a7cc2SEnji Cooper }
1869b89a7cc2SEnji Cooper
1870b89a7cc2SEnji Cooper // The following tests verify the message generated when a mock
1871b89a7cc2SEnji Cooper // function is called.
1872b89a7cc2SEnji Cooper
187328f6c2f2SEnji Cooper struct Printable {};
1874b89a7cc2SEnji Cooper
operator <<(::std::ostream & os,const Printable &)1875b89a7cc2SEnji Cooper inline void operator<<(::std::ostream& os, const Printable&) {
1876b89a7cc2SEnji Cooper os << "Printable";
1877b89a7cc2SEnji Cooper }
1878b89a7cc2SEnji Cooper
1879b89a7cc2SEnji Cooper struct Unprintable {
Unprintabletesting::__anon061b47f50111::Unprintable1880b89a7cc2SEnji Cooper Unprintable() : value(0) {}
1881b89a7cc2SEnji Cooper int value;
1882b89a7cc2SEnji Cooper };
1883b89a7cc2SEnji Cooper
1884b89a7cc2SEnji Cooper class MockC {
1885b89a7cc2SEnji Cooper public:
188628f6c2f2SEnji Cooper MockC() = default;
1887b89a7cc2SEnji Cooper
1888b89a7cc2SEnji Cooper MOCK_METHOD6(VoidMethod, void(bool cond, int n, std::string s, void* p,
1889b89a7cc2SEnji Cooper const Printable& x, Unprintable y));
1890b89a7cc2SEnji Cooper MOCK_METHOD0(NonVoidMethod, int()); // NOLINT
1891b89a7cc2SEnji Cooper
1892b89a7cc2SEnji Cooper private:
189328f6c2f2SEnji Cooper MockC(const MockC&) = delete;
189428f6c2f2SEnji Cooper MockC& operator=(const MockC&) = delete;
1895b89a7cc2SEnji Cooper };
1896b89a7cc2SEnji Cooper
1897b89a7cc2SEnji Cooper class VerboseFlagPreservingFixture : public testing::Test {
1898b89a7cc2SEnji Cooper protected:
VerboseFlagPreservingFixture()1899b89a7cc2SEnji Cooper VerboseFlagPreservingFixture()
190028f6c2f2SEnji Cooper : saved_verbose_flag_(GMOCK_FLAG_GET(verbose)) {}
1901b89a7cc2SEnji Cooper
~VerboseFlagPreservingFixture()190228f6c2f2SEnji Cooper ~VerboseFlagPreservingFixture() override {
190328f6c2f2SEnji Cooper GMOCK_FLAG_SET(verbose, saved_verbose_flag_);
190428f6c2f2SEnji Cooper }
1905b89a7cc2SEnji Cooper
1906b89a7cc2SEnji Cooper private:
1907b89a7cc2SEnji Cooper const std::string saved_verbose_flag_;
1908b89a7cc2SEnji Cooper
190928f6c2f2SEnji Cooper VerboseFlagPreservingFixture(const VerboseFlagPreservingFixture&) = delete;
191028f6c2f2SEnji Cooper VerboseFlagPreservingFixture& operator=(const VerboseFlagPreservingFixture&) =
191128f6c2f2SEnji Cooper delete;
1912b89a7cc2SEnji Cooper };
1913b89a7cc2SEnji Cooper
1914b89a7cc2SEnji Cooper #if GTEST_HAS_STREAM_REDIRECTION
1915b89a7cc2SEnji Cooper
1916b89a7cc2SEnji Cooper // Tests that an uninteresting mock function call on a naggy mock
1917b89a7cc2SEnji Cooper // generates a warning without the stack trace when
1918b89a7cc2SEnji Cooper // --gmock_verbose=warning is specified.
TEST(FunctionCallMessageTest,UninterestingCallOnNaggyMockGeneratesNoStackTraceWhenVerboseWarning)1919b89a7cc2SEnji Cooper TEST(FunctionCallMessageTest,
1920b89a7cc2SEnji Cooper UninterestingCallOnNaggyMockGeneratesNoStackTraceWhenVerboseWarning) {
192128f6c2f2SEnji Cooper GMOCK_FLAG_SET(verbose, kWarningVerbosity);
1922b89a7cc2SEnji Cooper NaggyMock<MockC> c;
1923b89a7cc2SEnji Cooper CaptureStdout();
192428f6c2f2SEnji Cooper c.VoidMethod(false, 5, "Hi", nullptr, Printable(), Unprintable());
1925b89a7cc2SEnji Cooper const std::string output = GetCapturedStdout();
1926b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output);
1927b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsNotSubstring, "Stack trace:", output);
1928b89a7cc2SEnji Cooper }
1929b89a7cc2SEnji Cooper
1930b89a7cc2SEnji Cooper // Tests that an uninteresting mock function call on a naggy mock
1931b89a7cc2SEnji Cooper // generates a warning containing the stack trace when
1932b89a7cc2SEnji Cooper // --gmock_verbose=info is specified.
TEST(FunctionCallMessageTest,UninterestingCallOnNaggyMockGeneratesFyiWithStackTraceWhenVerboseInfo)1933b89a7cc2SEnji Cooper TEST(FunctionCallMessageTest,
1934b89a7cc2SEnji Cooper UninterestingCallOnNaggyMockGeneratesFyiWithStackTraceWhenVerboseInfo) {
193528f6c2f2SEnji Cooper GMOCK_FLAG_SET(verbose, kInfoVerbosity);
1936b89a7cc2SEnji Cooper NaggyMock<MockC> c;
1937b89a7cc2SEnji Cooper CaptureStdout();
193828f6c2f2SEnji Cooper c.VoidMethod(false, 5, "Hi", nullptr, Printable(), Unprintable());
1939b89a7cc2SEnji Cooper const std::string output = GetCapturedStdout();
1940b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output);
1941b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring, "Stack trace:", output);
1942b89a7cc2SEnji Cooper
1943b89a7cc2SEnji Cooper #ifndef NDEBUG
1944b89a7cc2SEnji Cooper
1945b89a7cc2SEnji Cooper // We check the stack trace content in dbg-mode only, as opt-mode
1946b89a7cc2SEnji Cooper // may inline the call we are interested in seeing.
1947b89a7cc2SEnji Cooper
1948b89a7cc2SEnji Cooper // Verifies that a void mock function's name appears in the stack
1949b89a7cc2SEnji Cooper // trace.
1950b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring, "VoidMethod(", output);
1951b89a7cc2SEnji Cooper
1952b89a7cc2SEnji Cooper // Verifies that a non-void mock function's name appears in the
1953b89a7cc2SEnji Cooper // stack trace.
1954b89a7cc2SEnji Cooper CaptureStdout();
1955b89a7cc2SEnji Cooper c.NonVoidMethod();
1956b89a7cc2SEnji Cooper const std::string output2 = GetCapturedStdout();
1957b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(IsSubstring, "NonVoidMethod(", output2);
1958b89a7cc2SEnji Cooper
1959b89a7cc2SEnji Cooper #endif // NDEBUG
1960b89a7cc2SEnji Cooper }
1961b89a7cc2SEnji Cooper
1962b89a7cc2SEnji Cooper // Tests that an uninteresting mock function call on a naggy mock
1963b89a7cc2SEnji Cooper // causes the function arguments and return value to be printed.
TEST(FunctionCallMessageTest,UninterestingCallOnNaggyMockPrintsArgumentsAndReturnValue)1964b89a7cc2SEnji Cooper TEST(FunctionCallMessageTest,
1965b89a7cc2SEnji Cooper UninterestingCallOnNaggyMockPrintsArgumentsAndReturnValue) {
1966b89a7cc2SEnji Cooper // A non-void mock function.
1967b89a7cc2SEnji Cooper NaggyMock<MockB> b;
1968b89a7cc2SEnji Cooper CaptureStdout();
1969b89a7cc2SEnji Cooper b.DoB();
1970b89a7cc2SEnji Cooper const std::string output1 = GetCapturedStdout();
1971b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(
1972b89a7cc2SEnji Cooper IsSubstring,
1973b89a7cc2SEnji Cooper "Uninteresting mock function call - returning default value.\n"
1974b89a7cc2SEnji Cooper " Function call: DoB()\n"
197528f6c2f2SEnji Cooper " Returns: 0\n",
197628f6c2f2SEnji Cooper output1.c_str());
1977b89a7cc2SEnji Cooper // Makes sure the return value is printed.
1978b89a7cc2SEnji Cooper
1979b89a7cc2SEnji Cooper // A void mock function.
1980b89a7cc2SEnji Cooper NaggyMock<MockC> c;
1981b89a7cc2SEnji Cooper CaptureStdout();
198228f6c2f2SEnji Cooper c.VoidMethod(false, 5, "Hi", nullptr, Printable(), Unprintable());
1983b89a7cc2SEnji Cooper const std::string output2 = GetCapturedStdout();
198428f6c2f2SEnji Cooper EXPECT_THAT(
198528f6c2f2SEnji Cooper output2.c_str(),
198628f6c2f2SEnji Cooper ContainsRegex("Uninteresting mock function call - returning directly\\.\n"
1987b89a7cc2SEnji Cooper " Function call: VoidMethod"
1988b89a7cc2SEnji Cooper "\\(false, 5, \"Hi\", NULL, @.+ "
1989b89a7cc2SEnji Cooper "Printable, 4-byte object <00-00 00-00>\\)"));
1990b89a7cc2SEnji Cooper // A void function has no return value to print.
1991b89a7cc2SEnji Cooper }
1992b89a7cc2SEnji Cooper
1993b89a7cc2SEnji Cooper // Tests how the --gmock_verbose flag affects Google Mock's output.
1994b89a7cc2SEnji Cooper
1995b89a7cc2SEnji Cooper class GMockVerboseFlagTest : public VerboseFlagPreservingFixture {
1996b89a7cc2SEnji Cooper public:
1997b89a7cc2SEnji Cooper // Verifies that the given Google Mock output is correct. (When
1998b89a7cc2SEnji Cooper // should_print is true, the output should match the given regex and
1999b89a7cc2SEnji Cooper // contain the given function name in the stack trace. When it's
2000b89a7cc2SEnji Cooper // false, the output should be empty.)
VerifyOutput(const std::string & output,bool should_print,const std::string & expected_substring,const std::string & function_name)2001b89a7cc2SEnji Cooper void VerifyOutput(const std::string& output, bool should_print,
2002b89a7cc2SEnji Cooper const std::string& expected_substring,
2003b89a7cc2SEnji Cooper const std::string& function_name) {
2004b89a7cc2SEnji Cooper if (should_print) {
2005b89a7cc2SEnji Cooper EXPECT_THAT(output.c_str(), HasSubstr(expected_substring));
2006b89a7cc2SEnji Cooper #ifndef NDEBUG
2007b89a7cc2SEnji Cooper // We check the stack trace content in dbg-mode only, as opt-mode
2008b89a7cc2SEnji Cooper // may inline the call we are interested in seeing.
2009b89a7cc2SEnji Cooper EXPECT_THAT(output.c_str(), HasSubstr(function_name));
2010b89a7cc2SEnji Cooper #else
2011b89a7cc2SEnji Cooper // Suppresses 'unused function parameter' warnings.
2012b89a7cc2SEnji Cooper static_cast<void>(function_name);
2013b89a7cc2SEnji Cooper #endif // NDEBUG
2014b89a7cc2SEnji Cooper } else {
2015b89a7cc2SEnji Cooper EXPECT_STREQ("", output.c_str());
2016b89a7cc2SEnji Cooper }
2017b89a7cc2SEnji Cooper }
2018b89a7cc2SEnji Cooper
2019b89a7cc2SEnji Cooper // Tests how the flag affects expected calls.
TestExpectedCall(bool should_print)2020b89a7cc2SEnji Cooper void TestExpectedCall(bool should_print) {
2021b89a7cc2SEnji Cooper MockA a;
2022b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(5));
202328f6c2f2SEnji Cooper EXPECT_CALL(a, Binary(_, 1)).WillOnce(Return(true));
2024b89a7cc2SEnji Cooper
2025b89a7cc2SEnji Cooper // A void-returning function.
2026b89a7cc2SEnji Cooper CaptureStdout();
2027b89a7cc2SEnji Cooper a.DoA(5);
202828f6c2f2SEnji Cooper VerifyOutput(GetCapturedStdout(), should_print,
2029b89a7cc2SEnji Cooper "Mock function call matches EXPECT_CALL(a, DoA(5))...\n"
2030b89a7cc2SEnji Cooper " Function call: DoA(5)\n"
2031b89a7cc2SEnji Cooper "Stack trace:\n",
2032b89a7cc2SEnji Cooper "DoA");
2033b89a7cc2SEnji Cooper
2034b89a7cc2SEnji Cooper // A non-void-returning function.
2035b89a7cc2SEnji Cooper CaptureStdout();
2036b89a7cc2SEnji Cooper a.Binary(2, 1);
203728f6c2f2SEnji Cooper VerifyOutput(GetCapturedStdout(), should_print,
2038b89a7cc2SEnji Cooper "Mock function call matches EXPECT_CALL(a, Binary(_, 1))...\n"
2039b89a7cc2SEnji Cooper " Function call: Binary(2, 1)\n"
2040b89a7cc2SEnji Cooper " Returns: true\n"
2041b89a7cc2SEnji Cooper "Stack trace:\n",
2042b89a7cc2SEnji Cooper "Binary");
2043b89a7cc2SEnji Cooper }
2044b89a7cc2SEnji Cooper
2045b89a7cc2SEnji Cooper // Tests how the flag affects uninteresting calls on a naggy mock.
TestUninterestingCallOnNaggyMock(bool should_print)2046b89a7cc2SEnji Cooper void TestUninterestingCallOnNaggyMock(bool should_print) {
2047b89a7cc2SEnji Cooper NaggyMock<MockA> a;
2048b89a7cc2SEnji Cooper const std::string note =
2049b89a7cc2SEnji Cooper "NOTE: You can safely ignore the above warning unless this "
2050b89a7cc2SEnji Cooper "call should not happen. Do not suppress it by blindly adding "
2051b89a7cc2SEnji Cooper "an EXPECT_CALL() if you don't mean to enforce the call. "
2052b89a7cc2SEnji Cooper "See "
205328f6c2f2SEnji Cooper "https://github.com/google/googletest/blob/main/docs/"
205428f6c2f2SEnji Cooper "gmock_cook_book.md#"
205528f6c2f2SEnji Cooper "knowing-when-to-expect-useoncall for details.";
2056b89a7cc2SEnji Cooper
2057b89a7cc2SEnji Cooper // A void-returning function.
2058b89a7cc2SEnji Cooper CaptureStdout();
2059b89a7cc2SEnji Cooper a.DoA(5);
206028f6c2f2SEnji Cooper VerifyOutput(GetCapturedStdout(), should_print,
2061b89a7cc2SEnji Cooper "\nGMOCK WARNING:\n"
2062b89a7cc2SEnji Cooper "Uninteresting mock function call - returning directly.\n"
2063b89a7cc2SEnji Cooper " Function call: DoA(5)\n" +
2064b89a7cc2SEnji Cooper note,
2065b89a7cc2SEnji Cooper "DoA");
2066b89a7cc2SEnji Cooper
2067b89a7cc2SEnji Cooper // A non-void-returning function.
2068b89a7cc2SEnji Cooper CaptureStdout();
2069b89a7cc2SEnji Cooper a.Binary(2, 1);
207028f6c2f2SEnji Cooper VerifyOutput(GetCapturedStdout(), should_print,
2071b89a7cc2SEnji Cooper "\nGMOCK WARNING:\n"
2072b89a7cc2SEnji Cooper "Uninteresting mock function call - returning default value.\n"
2073b89a7cc2SEnji Cooper " Function call: Binary(2, 1)\n"
2074b89a7cc2SEnji Cooper " Returns: false\n" +
2075b89a7cc2SEnji Cooper note,
2076b89a7cc2SEnji Cooper "Binary");
2077b89a7cc2SEnji Cooper }
2078b89a7cc2SEnji Cooper };
2079b89a7cc2SEnji Cooper
2080b89a7cc2SEnji Cooper // Tests that --gmock_verbose=info causes both expected and
2081b89a7cc2SEnji Cooper // uninteresting calls to be reported.
TEST_F(GMockVerboseFlagTest,Info)2082b89a7cc2SEnji Cooper TEST_F(GMockVerboseFlagTest, Info) {
208328f6c2f2SEnji Cooper GMOCK_FLAG_SET(verbose, kInfoVerbosity);
2084b89a7cc2SEnji Cooper TestExpectedCall(true);
2085b89a7cc2SEnji Cooper TestUninterestingCallOnNaggyMock(true);
2086b89a7cc2SEnji Cooper }
2087b89a7cc2SEnji Cooper
2088b89a7cc2SEnji Cooper // Tests that --gmock_verbose=warning causes uninteresting calls to be
2089b89a7cc2SEnji Cooper // reported.
TEST_F(GMockVerboseFlagTest,Warning)2090b89a7cc2SEnji Cooper TEST_F(GMockVerboseFlagTest, Warning) {
209128f6c2f2SEnji Cooper GMOCK_FLAG_SET(verbose, kWarningVerbosity);
2092b89a7cc2SEnji Cooper TestExpectedCall(false);
2093b89a7cc2SEnji Cooper TestUninterestingCallOnNaggyMock(true);
2094b89a7cc2SEnji Cooper }
2095b89a7cc2SEnji Cooper
2096b89a7cc2SEnji Cooper // Tests that --gmock_verbose=warning causes neither expected nor
2097b89a7cc2SEnji Cooper // uninteresting calls to be reported.
TEST_F(GMockVerboseFlagTest,Error)2098b89a7cc2SEnji Cooper TEST_F(GMockVerboseFlagTest, Error) {
209928f6c2f2SEnji Cooper GMOCK_FLAG_SET(verbose, kErrorVerbosity);
2100b89a7cc2SEnji Cooper TestExpectedCall(false);
2101b89a7cc2SEnji Cooper TestUninterestingCallOnNaggyMock(false);
2102b89a7cc2SEnji Cooper }
2103b89a7cc2SEnji Cooper
2104b89a7cc2SEnji Cooper // Tests that --gmock_verbose=SOME_INVALID_VALUE has the same effect
2105b89a7cc2SEnji Cooper // as --gmock_verbose=warning.
TEST_F(GMockVerboseFlagTest,InvalidFlagIsTreatedAsWarning)2106b89a7cc2SEnji Cooper TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) {
210728f6c2f2SEnji Cooper GMOCK_FLAG_SET(verbose, "invalid"); // Treated as "warning".
2108b89a7cc2SEnji Cooper TestExpectedCall(false);
2109b89a7cc2SEnji Cooper TestUninterestingCallOnNaggyMock(true);
2110b89a7cc2SEnji Cooper }
2111b89a7cc2SEnji Cooper
2112b89a7cc2SEnji Cooper #endif // GTEST_HAS_STREAM_REDIRECTION
2113b89a7cc2SEnji Cooper
2114b89a7cc2SEnji Cooper // A helper class that generates a failure when printed. We use it to
2115b89a7cc2SEnji Cooper // ensure that Google Mock doesn't print a value (even to an internal
2116b89a7cc2SEnji Cooper // buffer) when it is not supposed to do so.
2117b89a7cc2SEnji Cooper class PrintMeNot {};
2118b89a7cc2SEnji Cooper
PrintTo(PrintMeNot,::std::ostream *)2119b89a7cc2SEnji Cooper void PrintTo(PrintMeNot /* dummy */, ::std::ostream* /* os */) {
2120b89a7cc2SEnji Cooper ADD_FAILURE() << "Google Mock is printing a value that shouldn't be "
2121b89a7cc2SEnji Cooper << "printed even to an internal buffer.";
2122b89a7cc2SEnji Cooper }
2123b89a7cc2SEnji Cooper
2124b89a7cc2SEnji Cooper class LogTestHelper {
2125b89a7cc2SEnji Cooper public:
212628f6c2f2SEnji Cooper LogTestHelper() = default;
2127b89a7cc2SEnji Cooper
2128b89a7cc2SEnji Cooper MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot));
2129b89a7cc2SEnji Cooper
2130b89a7cc2SEnji Cooper private:
213128f6c2f2SEnji Cooper LogTestHelper(const LogTestHelper&) = delete;
213228f6c2f2SEnji Cooper LogTestHelper& operator=(const LogTestHelper&) = delete;
2133b89a7cc2SEnji Cooper };
2134b89a7cc2SEnji Cooper
2135b89a7cc2SEnji Cooper class GMockLogTest : public VerboseFlagPreservingFixture {
2136b89a7cc2SEnji Cooper protected:
2137b89a7cc2SEnji Cooper LogTestHelper helper_;
2138b89a7cc2SEnji Cooper };
2139b89a7cc2SEnji Cooper
TEST_F(GMockLogTest,DoesNotPrintGoodCallInternallyIfVerbosityIsWarning)2140b89a7cc2SEnji Cooper TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsWarning) {
214128f6c2f2SEnji Cooper GMOCK_FLAG_SET(verbose, kWarningVerbosity);
214228f6c2f2SEnji Cooper EXPECT_CALL(helper_, Foo(_)).WillOnce(Return(PrintMeNot()));
2143b89a7cc2SEnji Cooper helper_.Foo(PrintMeNot()); // This is an expected call.
2144b89a7cc2SEnji Cooper }
2145b89a7cc2SEnji Cooper
TEST_F(GMockLogTest,DoesNotPrintGoodCallInternallyIfVerbosityIsError)2146b89a7cc2SEnji Cooper TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsError) {
214728f6c2f2SEnji Cooper GMOCK_FLAG_SET(verbose, kErrorVerbosity);
214828f6c2f2SEnji Cooper EXPECT_CALL(helper_, Foo(_)).WillOnce(Return(PrintMeNot()));
2149b89a7cc2SEnji Cooper helper_.Foo(PrintMeNot()); // This is an expected call.
2150b89a7cc2SEnji Cooper }
2151b89a7cc2SEnji Cooper
TEST_F(GMockLogTest,DoesNotPrintWarningInternallyIfVerbosityIsError)2152b89a7cc2SEnji Cooper TEST_F(GMockLogTest, DoesNotPrintWarningInternallyIfVerbosityIsError) {
215328f6c2f2SEnji Cooper GMOCK_FLAG_SET(verbose, kErrorVerbosity);
215428f6c2f2SEnji Cooper ON_CALL(helper_, Foo(_)).WillByDefault(Return(PrintMeNot()));
2155b89a7cc2SEnji Cooper helper_.Foo(PrintMeNot()); // This should generate a warning.
2156b89a7cc2SEnji Cooper }
2157b89a7cc2SEnji Cooper
2158b89a7cc2SEnji Cooper // Tests Mock::AllowLeak().
2159b89a7cc2SEnji Cooper
TEST(AllowLeakTest,AllowsLeakingUnusedMockObject)2160b89a7cc2SEnji Cooper TEST(AllowLeakTest, AllowsLeakingUnusedMockObject) {
2161b89a7cc2SEnji Cooper MockA* a = new MockA;
2162b89a7cc2SEnji Cooper Mock::AllowLeak(a);
2163b89a7cc2SEnji Cooper }
2164b89a7cc2SEnji Cooper
TEST(AllowLeakTest,CanBeCalledBeforeOnCall)2165b89a7cc2SEnji Cooper TEST(AllowLeakTest, CanBeCalledBeforeOnCall) {
2166b89a7cc2SEnji Cooper MockA* a = new MockA;
2167b89a7cc2SEnji Cooper Mock::AllowLeak(a);
2168b89a7cc2SEnji Cooper ON_CALL(*a, DoA(_)).WillByDefault(Return());
2169b89a7cc2SEnji Cooper a->DoA(0);
2170b89a7cc2SEnji Cooper }
2171b89a7cc2SEnji Cooper
TEST(AllowLeakTest,CanBeCalledAfterOnCall)2172b89a7cc2SEnji Cooper TEST(AllowLeakTest, CanBeCalledAfterOnCall) {
2173b89a7cc2SEnji Cooper MockA* a = new MockA;
2174b89a7cc2SEnji Cooper ON_CALL(*a, DoA(_)).WillByDefault(Return());
2175b89a7cc2SEnji Cooper Mock::AllowLeak(a);
2176b89a7cc2SEnji Cooper }
2177b89a7cc2SEnji Cooper
TEST(AllowLeakTest,CanBeCalledBeforeExpectCall)2178b89a7cc2SEnji Cooper TEST(AllowLeakTest, CanBeCalledBeforeExpectCall) {
2179b89a7cc2SEnji Cooper MockA* a = new MockA;
2180b89a7cc2SEnji Cooper Mock::AllowLeak(a);
2181b89a7cc2SEnji Cooper EXPECT_CALL(*a, DoA(_));
2182b89a7cc2SEnji Cooper a->DoA(0);
2183b89a7cc2SEnji Cooper }
2184b89a7cc2SEnji Cooper
TEST(AllowLeakTest,CanBeCalledAfterExpectCall)2185b89a7cc2SEnji Cooper TEST(AllowLeakTest, CanBeCalledAfterExpectCall) {
2186b89a7cc2SEnji Cooper MockA* a = new MockA;
2187b89a7cc2SEnji Cooper EXPECT_CALL(*a, DoA(_)).Times(AnyNumber());
2188b89a7cc2SEnji Cooper Mock::AllowLeak(a);
2189b89a7cc2SEnji Cooper }
2190b89a7cc2SEnji Cooper
TEST(AllowLeakTest,WorksWhenBothOnCallAndExpectCallArePresent)2191b89a7cc2SEnji Cooper TEST(AllowLeakTest, WorksWhenBothOnCallAndExpectCallArePresent) {
2192b89a7cc2SEnji Cooper MockA* a = new MockA;
2193b89a7cc2SEnji Cooper ON_CALL(*a, DoA(_)).WillByDefault(Return());
2194b89a7cc2SEnji Cooper EXPECT_CALL(*a, DoA(_)).Times(AnyNumber());
2195b89a7cc2SEnji Cooper Mock::AllowLeak(a);
2196b89a7cc2SEnji Cooper }
2197b89a7cc2SEnji Cooper
2198b89a7cc2SEnji Cooper // Tests that we can verify and clear a mock object's expectations
2199b89a7cc2SEnji Cooper // when none of its methods has expectations.
TEST(VerifyAndClearExpectationsTest,NoMethodHasExpectations)2200b89a7cc2SEnji Cooper TEST(VerifyAndClearExpectationsTest, NoMethodHasExpectations) {
2201b89a7cc2SEnji Cooper MockB b;
2202b89a7cc2SEnji Cooper ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
2203b89a7cc2SEnji Cooper
2204b89a7cc2SEnji Cooper // There should be no expectations on the methods now, so we can
2205b89a7cc2SEnji Cooper // freely call them.
2206b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
2207b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(1));
2208b89a7cc2SEnji Cooper }
2209b89a7cc2SEnji Cooper
2210b89a7cc2SEnji Cooper // Tests that we can verify and clear a mock object's expectations
2211b89a7cc2SEnji Cooper // when some, but not all, of its methods have expectations *and* the
2212b89a7cc2SEnji Cooper // verification succeeds.
TEST(VerifyAndClearExpectationsTest,SomeMethodsHaveExpectationsAndSucceed)2213b89a7cc2SEnji Cooper TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndSucceed) {
2214b89a7cc2SEnji Cooper MockB b;
221528f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).WillOnce(Return(1));
2216b89a7cc2SEnji Cooper b.DoB();
2217b89a7cc2SEnji Cooper ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
2218b89a7cc2SEnji Cooper
2219b89a7cc2SEnji Cooper // There should be no expectations on the methods now, so we can
2220b89a7cc2SEnji Cooper // freely call them.
2221b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
2222b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(1));
2223b89a7cc2SEnji Cooper }
2224b89a7cc2SEnji Cooper
2225b89a7cc2SEnji Cooper // Tests that we can verify and clear a mock object's expectations
2226b89a7cc2SEnji Cooper // when some, but not all, of its methods have expectations *and* the
2227b89a7cc2SEnji Cooper // verification fails.
TEST(VerifyAndClearExpectationsTest,SomeMethodsHaveExpectationsAndFail)2228b89a7cc2SEnji Cooper TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndFail) {
2229b89a7cc2SEnji Cooper MockB b;
223028f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).WillOnce(Return(1));
2231b89a7cc2SEnji Cooper bool result = true;
2232b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b),
2233b89a7cc2SEnji Cooper "Actual: never called");
2234b89a7cc2SEnji Cooper ASSERT_FALSE(result);
2235b89a7cc2SEnji Cooper
2236b89a7cc2SEnji Cooper // There should be no expectations on the methods now, so we can
2237b89a7cc2SEnji Cooper // freely call them.
2238b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
2239b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(1));
2240b89a7cc2SEnji Cooper }
2241b89a7cc2SEnji Cooper
2242b89a7cc2SEnji Cooper // Tests that we can verify and clear a mock object's expectations
2243b89a7cc2SEnji Cooper // when all of its methods have expectations.
TEST(VerifyAndClearExpectationsTest,AllMethodsHaveExpectations)2244b89a7cc2SEnji Cooper TEST(VerifyAndClearExpectationsTest, AllMethodsHaveExpectations) {
2245b89a7cc2SEnji Cooper MockB b;
224628f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).WillOnce(Return(1));
224728f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(_)).WillOnce(Return(2));
2248b89a7cc2SEnji Cooper b.DoB();
2249b89a7cc2SEnji Cooper b.DoB(1);
2250b89a7cc2SEnji Cooper ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
2251b89a7cc2SEnji Cooper
2252b89a7cc2SEnji Cooper // There should be no expectations on the methods now, so we can
2253b89a7cc2SEnji Cooper // freely call them.
2254b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
2255b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(1));
2256b89a7cc2SEnji Cooper }
2257b89a7cc2SEnji Cooper
2258b89a7cc2SEnji Cooper // Tests that we can verify and clear a mock object's expectations
2259b89a7cc2SEnji Cooper // when a method has more than one expectation.
TEST(VerifyAndClearExpectationsTest,AMethodHasManyExpectations)2260b89a7cc2SEnji Cooper TEST(VerifyAndClearExpectationsTest, AMethodHasManyExpectations) {
2261b89a7cc2SEnji Cooper MockB b;
226228f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(0)).WillOnce(Return(1));
226328f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(_)).WillOnce(Return(2));
2264b89a7cc2SEnji Cooper b.DoB(1);
2265b89a7cc2SEnji Cooper bool result = true;
2266b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClearExpectations(&b),
2267b89a7cc2SEnji Cooper "Actual: never called");
2268b89a7cc2SEnji Cooper ASSERT_FALSE(result);
2269b89a7cc2SEnji Cooper
2270b89a7cc2SEnji Cooper // There should be no expectations on the methods now, so we can
2271b89a7cc2SEnji Cooper // freely call them.
2272b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
2273b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(1));
2274b89a7cc2SEnji Cooper }
2275b89a7cc2SEnji Cooper
2276b89a7cc2SEnji Cooper // Tests that we can call VerifyAndClearExpectations() on the same
2277b89a7cc2SEnji Cooper // mock object multiple times.
TEST(VerifyAndClearExpectationsTest,CanCallManyTimes)2278b89a7cc2SEnji Cooper TEST(VerifyAndClearExpectationsTest, CanCallManyTimes) {
2279b89a7cc2SEnji Cooper MockB b;
2280b89a7cc2SEnji Cooper EXPECT_CALL(b, DoB());
2281b89a7cc2SEnji Cooper b.DoB();
2282b89a7cc2SEnji Cooper Mock::VerifyAndClearExpectations(&b);
2283b89a7cc2SEnji Cooper
228428f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(_)).WillOnce(Return(1));
2285b89a7cc2SEnji Cooper b.DoB(1);
2286b89a7cc2SEnji Cooper Mock::VerifyAndClearExpectations(&b);
2287b89a7cc2SEnji Cooper Mock::VerifyAndClearExpectations(&b);
2288b89a7cc2SEnji Cooper
2289b89a7cc2SEnji Cooper // There should be no expectations on the methods now, so we can
2290b89a7cc2SEnji Cooper // freely call them.
2291b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
2292b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(1));
2293b89a7cc2SEnji Cooper }
2294b89a7cc2SEnji Cooper
2295b89a7cc2SEnji Cooper // Tests that we can clear a mock object's default actions when none
2296b89a7cc2SEnji Cooper // of its methods has default actions.
TEST(VerifyAndClearTest,NoMethodHasDefaultActions)2297b89a7cc2SEnji Cooper TEST(VerifyAndClearTest, NoMethodHasDefaultActions) {
2298b89a7cc2SEnji Cooper MockB b;
2299b89a7cc2SEnji Cooper // If this crashes or generates a failure, the test will catch it.
2300b89a7cc2SEnji Cooper Mock::VerifyAndClear(&b);
2301b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
2302b89a7cc2SEnji Cooper }
2303b89a7cc2SEnji Cooper
2304b89a7cc2SEnji Cooper // Tests that we can clear a mock object's default actions when some,
2305b89a7cc2SEnji Cooper // but not all of its methods have default actions.
TEST(VerifyAndClearTest,SomeMethodsHaveDefaultActions)2306b89a7cc2SEnji Cooper TEST(VerifyAndClearTest, SomeMethodsHaveDefaultActions) {
2307b89a7cc2SEnji Cooper MockB b;
230828f6c2f2SEnji Cooper ON_CALL(b, DoB()).WillByDefault(Return(1));
2309b89a7cc2SEnji Cooper
2310b89a7cc2SEnji Cooper Mock::VerifyAndClear(&b);
2311b89a7cc2SEnji Cooper
2312b89a7cc2SEnji Cooper // Verifies that the default action of int DoB() was removed.
2313b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
2314b89a7cc2SEnji Cooper }
2315b89a7cc2SEnji Cooper
2316b89a7cc2SEnji Cooper // Tests that we can clear a mock object's default actions when all of
2317b89a7cc2SEnji Cooper // its methods have default actions.
TEST(VerifyAndClearTest,AllMethodsHaveDefaultActions)2318b89a7cc2SEnji Cooper TEST(VerifyAndClearTest, AllMethodsHaveDefaultActions) {
2319b89a7cc2SEnji Cooper MockB b;
232028f6c2f2SEnji Cooper ON_CALL(b, DoB()).WillByDefault(Return(1));
232128f6c2f2SEnji Cooper ON_CALL(b, DoB(_)).WillByDefault(Return(2));
2322b89a7cc2SEnji Cooper
2323b89a7cc2SEnji Cooper Mock::VerifyAndClear(&b);
2324b89a7cc2SEnji Cooper
2325b89a7cc2SEnji Cooper // Verifies that the default action of int DoB() was removed.
2326b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
2327b89a7cc2SEnji Cooper
2328b89a7cc2SEnji Cooper // Verifies that the default action of int DoB(int) was removed.
2329b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(0));
2330b89a7cc2SEnji Cooper }
2331b89a7cc2SEnji Cooper
2332b89a7cc2SEnji Cooper // Tests that we can clear a mock object's default actions when a
2333b89a7cc2SEnji Cooper // method has more than one ON_CALL() set on it.
TEST(VerifyAndClearTest,AMethodHasManyDefaultActions)2334b89a7cc2SEnji Cooper TEST(VerifyAndClearTest, AMethodHasManyDefaultActions) {
2335b89a7cc2SEnji Cooper MockB b;
233628f6c2f2SEnji Cooper ON_CALL(b, DoB(0)).WillByDefault(Return(1));
233728f6c2f2SEnji Cooper ON_CALL(b, DoB(_)).WillByDefault(Return(2));
2338b89a7cc2SEnji Cooper
2339b89a7cc2SEnji Cooper Mock::VerifyAndClear(&b);
2340b89a7cc2SEnji Cooper
2341b89a7cc2SEnji Cooper // Verifies that the default actions (there are two) of int DoB(int)
2342b89a7cc2SEnji Cooper // were removed.
2343b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(0));
2344b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(1));
2345b89a7cc2SEnji Cooper }
2346b89a7cc2SEnji Cooper
2347b89a7cc2SEnji Cooper // Tests that we can call VerifyAndClear() on a mock object multiple
2348b89a7cc2SEnji Cooper // times.
TEST(VerifyAndClearTest,CanCallManyTimes)2349b89a7cc2SEnji Cooper TEST(VerifyAndClearTest, CanCallManyTimes) {
2350b89a7cc2SEnji Cooper MockB b;
235128f6c2f2SEnji Cooper ON_CALL(b, DoB()).WillByDefault(Return(1));
2352b89a7cc2SEnji Cooper Mock::VerifyAndClear(&b);
2353b89a7cc2SEnji Cooper Mock::VerifyAndClear(&b);
2354b89a7cc2SEnji Cooper
235528f6c2f2SEnji Cooper ON_CALL(b, DoB(_)).WillByDefault(Return(1));
2356b89a7cc2SEnji Cooper Mock::VerifyAndClear(&b);
2357b89a7cc2SEnji Cooper
2358b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
2359b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(1));
2360b89a7cc2SEnji Cooper }
2361b89a7cc2SEnji Cooper
2362b89a7cc2SEnji Cooper // Tests that VerifyAndClear() works when the verification succeeds.
TEST(VerifyAndClearTest,Success)2363b89a7cc2SEnji Cooper TEST(VerifyAndClearTest, Success) {
2364b89a7cc2SEnji Cooper MockB b;
236528f6c2f2SEnji Cooper ON_CALL(b, DoB()).WillByDefault(Return(1));
236628f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(1)).WillOnce(Return(2));
2367b89a7cc2SEnji Cooper
2368b89a7cc2SEnji Cooper b.DoB();
2369b89a7cc2SEnji Cooper b.DoB(1);
2370b89a7cc2SEnji Cooper ASSERT_TRUE(Mock::VerifyAndClear(&b));
2371b89a7cc2SEnji Cooper
2372b89a7cc2SEnji Cooper // There should be no expectations on the methods now, so we can
2373b89a7cc2SEnji Cooper // freely call them.
2374b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
2375b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(1));
2376b89a7cc2SEnji Cooper }
2377b89a7cc2SEnji Cooper
2378b89a7cc2SEnji Cooper // Tests that VerifyAndClear() works when the verification fails.
TEST(VerifyAndClearTest,Failure)2379b89a7cc2SEnji Cooper TEST(VerifyAndClearTest, Failure) {
2380b89a7cc2SEnji Cooper MockB b;
238128f6c2f2SEnji Cooper ON_CALL(b, DoB(_)).WillByDefault(Return(1));
238228f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).WillOnce(Return(2));
2383b89a7cc2SEnji Cooper
2384b89a7cc2SEnji Cooper b.DoB(1);
2385b89a7cc2SEnji Cooper bool result = true;
2386b89a7cc2SEnji Cooper EXPECT_NONFATAL_FAILURE(result = Mock::VerifyAndClear(&b),
2387b89a7cc2SEnji Cooper "Actual: never called");
2388b89a7cc2SEnji Cooper ASSERT_FALSE(result);
2389b89a7cc2SEnji Cooper
2390b89a7cc2SEnji Cooper // There should be no expectations on the methods now, so we can
2391b89a7cc2SEnji Cooper // freely call them.
2392b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
2393b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(1));
2394b89a7cc2SEnji Cooper }
2395b89a7cc2SEnji Cooper
2396b89a7cc2SEnji Cooper // Tests that VerifyAndClear() works when the default actions and
2397b89a7cc2SEnji Cooper // expectations are set on a const mock object.
TEST(VerifyAndClearTest,Const)2398b89a7cc2SEnji Cooper TEST(VerifyAndClearTest, Const) {
2399b89a7cc2SEnji Cooper MockB b;
240028f6c2f2SEnji Cooper ON_CALL(Const(b), DoB()).WillByDefault(Return(1));
2401b89a7cc2SEnji Cooper
240228f6c2f2SEnji Cooper EXPECT_CALL(Const(b), DoB()).WillOnce(DoDefault()).WillOnce(Return(2));
2403b89a7cc2SEnji Cooper
2404b89a7cc2SEnji Cooper b.DoB();
2405b89a7cc2SEnji Cooper b.DoB();
2406b89a7cc2SEnji Cooper ASSERT_TRUE(Mock::VerifyAndClear(&b));
2407b89a7cc2SEnji Cooper
2408b89a7cc2SEnji Cooper // There should be no expectations on the methods now, so we can
2409b89a7cc2SEnji Cooper // freely call them.
2410b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB());
2411b89a7cc2SEnji Cooper EXPECT_EQ(0, b.DoB(1));
2412b89a7cc2SEnji Cooper }
2413b89a7cc2SEnji Cooper
2414b89a7cc2SEnji Cooper // Tests that we can set default actions and expectations on a mock
2415b89a7cc2SEnji Cooper // object after VerifyAndClear() has been called on it.
TEST(VerifyAndClearTest,CanSetDefaultActionsAndExpectationsAfterwards)2416b89a7cc2SEnji Cooper TEST(VerifyAndClearTest, CanSetDefaultActionsAndExpectationsAfterwards) {
2417b89a7cc2SEnji Cooper MockB b;
241828f6c2f2SEnji Cooper ON_CALL(b, DoB()).WillByDefault(Return(1));
241928f6c2f2SEnji Cooper EXPECT_CALL(b, DoB(_)).WillOnce(Return(2));
2420b89a7cc2SEnji Cooper b.DoB(1);
2421b89a7cc2SEnji Cooper
2422b89a7cc2SEnji Cooper Mock::VerifyAndClear(&b);
2423b89a7cc2SEnji Cooper
242428f6c2f2SEnji Cooper EXPECT_CALL(b, DoB()).WillOnce(Return(3));
242528f6c2f2SEnji Cooper ON_CALL(b, DoB(_)).WillByDefault(Return(4));
2426b89a7cc2SEnji Cooper
2427b89a7cc2SEnji Cooper EXPECT_EQ(3, b.DoB());
2428b89a7cc2SEnji Cooper EXPECT_EQ(4, b.DoB(1));
2429b89a7cc2SEnji Cooper }
2430b89a7cc2SEnji Cooper
2431b89a7cc2SEnji Cooper // Tests that calling VerifyAndClear() on one mock object does not
2432b89a7cc2SEnji Cooper // affect other mock objects (either of the same type or not).
TEST(VerifyAndClearTest,DoesNotAffectOtherMockObjects)2433b89a7cc2SEnji Cooper TEST(VerifyAndClearTest, DoesNotAffectOtherMockObjects) {
2434b89a7cc2SEnji Cooper MockA a;
2435b89a7cc2SEnji Cooper MockB b1;
2436b89a7cc2SEnji Cooper MockB b2;
2437b89a7cc2SEnji Cooper
243828f6c2f2SEnji Cooper ON_CALL(a, Binary(_, _)).WillByDefault(Return(true));
243928f6c2f2SEnji Cooper EXPECT_CALL(a, Binary(_, _)).WillOnce(DoDefault()).WillOnce(Return(false));
2440b89a7cc2SEnji Cooper
244128f6c2f2SEnji Cooper ON_CALL(b1, DoB()).WillByDefault(Return(1));
244228f6c2f2SEnji Cooper EXPECT_CALL(b1, DoB(_)).WillOnce(Return(2));
2443b89a7cc2SEnji Cooper
244428f6c2f2SEnji Cooper ON_CALL(b2, DoB()).WillByDefault(Return(3));
2445b89a7cc2SEnji Cooper EXPECT_CALL(b2, DoB(_));
2446b89a7cc2SEnji Cooper
2447b89a7cc2SEnji Cooper b2.DoB(0);
2448b89a7cc2SEnji Cooper Mock::VerifyAndClear(&b2);
2449b89a7cc2SEnji Cooper
2450b89a7cc2SEnji Cooper // Verifies that the default actions and expectations of a and b1
2451b89a7cc2SEnji Cooper // are still in effect.
2452b89a7cc2SEnji Cooper EXPECT_TRUE(a.Binary(0, 0));
2453b89a7cc2SEnji Cooper EXPECT_FALSE(a.Binary(0, 0));
2454b89a7cc2SEnji Cooper
2455b89a7cc2SEnji Cooper EXPECT_EQ(1, b1.DoB());
2456b89a7cc2SEnji Cooper EXPECT_EQ(2, b1.DoB(0));
2457b89a7cc2SEnji Cooper }
2458b89a7cc2SEnji Cooper
TEST(VerifyAndClearTest,DestroyingChainedMocksDoesNotDeadlockThroughExpectations)2459b89a7cc2SEnji Cooper TEST(VerifyAndClearTest,
2460b89a7cc2SEnji Cooper DestroyingChainedMocksDoesNotDeadlockThroughExpectations) {
246128f6c2f2SEnji Cooper std::shared_ptr<MockA> a(new MockA);
2462b89a7cc2SEnji Cooper ReferenceHoldingMock test_mock;
2463b89a7cc2SEnji Cooper
2464b89a7cc2SEnji Cooper // EXPECT_CALL stores a reference to a inside test_mock.
2465b89a7cc2SEnji Cooper EXPECT_CALL(test_mock, AcceptReference(_))
2466b89a7cc2SEnji Cooper .WillRepeatedly(SetArgPointee<0>(a));
2467b89a7cc2SEnji Cooper
2468b89a7cc2SEnji Cooper // Throw away the reference to the mock that we have in a. After this, the
2469b89a7cc2SEnji Cooper // only reference to it is stored by test_mock.
2470b89a7cc2SEnji Cooper a.reset();
2471b89a7cc2SEnji Cooper
2472b89a7cc2SEnji Cooper // When test_mock goes out of scope, it destroys the last remaining reference
2473b89a7cc2SEnji Cooper // to the mock object originally pointed to by a. This will cause the MockA
2474b89a7cc2SEnji Cooper // destructor to be called from inside the ReferenceHoldingMock destructor.
2475b89a7cc2SEnji Cooper // The state of all mocks is protected by a single global lock, but there
2476b89a7cc2SEnji Cooper // should be no deadlock.
2477b89a7cc2SEnji Cooper }
2478b89a7cc2SEnji Cooper
TEST(VerifyAndClearTest,DestroyingChainedMocksDoesNotDeadlockThroughDefaultAction)2479b89a7cc2SEnji Cooper TEST(VerifyAndClearTest,
2480b89a7cc2SEnji Cooper DestroyingChainedMocksDoesNotDeadlockThroughDefaultAction) {
248128f6c2f2SEnji Cooper std::shared_ptr<MockA> a(new MockA);
2482b89a7cc2SEnji Cooper ReferenceHoldingMock test_mock;
2483b89a7cc2SEnji Cooper
2484b89a7cc2SEnji Cooper // ON_CALL stores a reference to a inside test_mock.
248528f6c2f2SEnji Cooper ON_CALL(test_mock, AcceptReference(_)).WillByDefault(SetArgPointee<0>(a));
2486b89a7cc2SEnji Cooper
2487b89a7cc2SEnji Cooper // Throw away the reference to the mock that we have in a. After this, the
2488b89a7cc2SEnji Cooper // only reference to it is stored by test_mock.
2489b89a7cc2SEnji Cooper a.reset();
2490b89a7cc2SEnji Cooper
2491b89a7cc2SEnji Cooper // When test_mock goes out of scope, it destroys the last remaining reference
2492b89a7cc2SEnji Cooper // to the mock object originally pointed to by a. This will cause the MockA
2493b89a7cc2SEnji Cooper // destructor to be called from inside the ReferenceHoldingMock destructor.
2494b89a7cc2SEnji Cooper // The state of all mocks is protected by a single global lock, but there
2495b89a7cc2SEnji Cooper // should be no deadlock.
2496b89a7cc2SEnji Cooper }
2497b89a7cc2SEnji Cooper
2498b89a7cc2SEnji Cooper // Tests that a mock function's action can call a mock function
2499b89a7cc2SEnji Cooper // (either the same function or a different one) either as an explicit
2500b89a7cc2SEnji Cooper // action or as a default action without causing a dead lock. It
2501b89a7cc2SEnji Cooper // verifies that the action is not performed inside the critical
2502b89a7cc2SEnji Cooper // section.
TEST(SynchronizationTest,CanCallMockMethodInAction)2503b89a7cc2SEnji Cooper TEST(SynchronizationTest, CanCallMockMethodInAction) {
2504b89a7cc2SEnji Cooper MockA a;
2505b89a7cc2SEnji Cooper MockC c;
250628f6c2f2SEnji Cooper ON_CALL(a, DoA(_)).WillByDefault(
250728f6c2f2SEnji Cooper IgnoreResult(InvokeWithoutArgs(&c, &MockC::NonVoidMethod)));
2508b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1));
2509b89a7cc2SEnji Cooper EXPECT_CALL(a, DoA(1))
2510b89a7cc2SEnji Cooper .WillOnce(Invoke(&a, &MockA::DoA))
2511b89a7cc2SEnji Cooper .RetiresOnSaturation();
2512b89a7cc2SEnji Cooper EXPECT_CALL(c, NonVoidMethod());
2513b89a7cc2SEnji Cooper
2514b89a7cc2SEnji Cooper a.DoA(1);
2515b89a7cc2SEnji Cooper // This will match the second EXPECT_CALL() and trigger another a.DoA(1),
2516b89a7cc2SEnji Cooper // which will in turn match the first EXPECT_CALL() and trigger a call to
2517b89a7cc2SEnji Cooper // c.NonVoidMethod() that was specified by the ON_CALL() since the first
2518b89a7cc2SEnji Cooper // EXPECT_CALL() did not specify an action.
2519b89a7cc2SEnji Cooper }
2520b89a7cc2SEnji Cooper
TEST(ParameterlessExpectationsTest,CanSetExpectationsWithoutMatchers)2521b89a7cc2SEnji Cooper TEST(ParameterlessExpectationsTest, CanSetExpectationsWithoutMatchers) {
2522b89a7cc2SEnji Cooper MockA a;
2523b89a7cc2SEnji Cooper int do_a_arg0 = 0;
2524b89a7cc2SEnji Cooper ON_CALL(a, DoA).WillByDefault(SaveArg<0>(&do_a_arg0));
2525b89a7cc2SEnji Cooper int do_a_47_arg0 = 0;
2526b89a7cc2SEnji Cooper ON_CALL(a, DoA(47)).WillByDefault(SaveArg<0>(&do_a_47_arg0));
2527b89a7cc2SEnji Cooper
2528b89a7cc2SEnji Cooper a.DoA(17);
2529b89a7cc2SEnji Cooper EXPECT_THAT(do_a_arg0, 17);
2530b89a7cc2SEnji Cooper EXPECT_THAT(do_a_47_arg0, 0);
2531b89a7cc2SEnji Cooper a.DoA(47);
2532b89a7cc2SEnji Cooper EXPECT_THAT(do_a_arg0, 17);
2533b89a7cc2SEnji Cooper EXPECT_THAT(do_a_47_arg0, 47);
2534b89a7cc2SEnji Cooper
2535b89a7cc2SEnji Cooper ON_CALL(a, Binary).WillByDefault(Return(true));
2536b89a7cc2SEnji Cooper ON_CALL(a, Binary(_, 14)).WillByDefault(Return(false));
2537b89a7cc2SEnji Cooper EXPECT_THAT(a.Binary(14, 17), true);
2538b89a7cc2SEnji Cooper EXPECT_THAT(a.Binary(17, 14), false);
2539b89a7cc2SEnji Cooper }
2540b89a7cc2SEnji Cooper
TEST(ParameterlessExpectationsTest,CanSetExpectationsForOverloadedMethods)2541b89a7cc2SEnji Cooper TEST(ParameterlessExpectationsTest, CanSetExpectationsForOverloadedMethods) {
2542b89a7cc2SEnji Cooper MockB b;
2543b89a7cc2SEnji Cooper ON_CALL(b, DoB()).WillByDefault(Return(9));
2544b89a7cc2SEnji Cooper ON_CALL(b, DoB(5)).WillByDefault(Return(11));
2545b89a7cc2SEnji Cooper
2546b89a7cc2SEnji Cooper EXPECT_THAT(b.DoB(), 9);
2547b89a7cc2SEnji Cooper EXPECT_THAT(b.DoB(1), 0); // default value
2548b89a7cc2SEnji Cooper EXPECT_THAT(b.DoB(5), 11);
2549b89a7cc2SEnji Cooper }
2550b89a7cc2SEnji Cooper
2551b89a7cc2SEnji Cooper struct MockWithConstMethods {
2552b89a7cc2SEnji Cooper public:
2553b89a7cc2SEnji Cooper MOCK_CONST_METHOD1(Foo, int(int));
2554b89a7cc2SEnji Cooper MOCK_CONST_METHOD2(Bar, int(int, const char*));
2555b89a7cc2SEnji Cooper };
2556b89a7cc2SEnji Cooper
TEST(ParameterlessExpectationsTest,CanSetExpectationsForConstMethods)2557b89a7cc2SEnji Cooper TEST(ParameterlessExpectationsTest, CanSetExpectationsForConstMethods) {
2558b89a7cc2SEnji Cooper MockWithConstMethods mock;
2559b89a7cc2SEnji Cooper ON_CALL(mock, Foo).WillByDefault(Return(7));
2560b89a7cc2SEnji Cooper ON_CALL(mock, Bar).WillByDefault(Return(33));
2561b89a7cc2SEnji Cooper
2562b89a7cc2SEnji Cooper EXPECT_THAT(mock.Foo(17), 7);
2563b89a7cc2SEnji Cooper EXPECT_THAT(mock.Bar(27, "purple"), 33);
2564b89a7cc2SEnji Cooper }
2565b89a7cc2SEnji Cooper
2566b89a7cc2SEnji Cooper class MockConstOverload {
2567b89a7cc2SEnji Cooper public:
2568b89a7cc2SEnji Cooper MOCK_METHOD1(Overloaded, int(int));
2569b89a7cc2SEnji Cooper MOCK_CONST_METHOD1(Overloaded, int(int));
2570b89a7cc2SEnji Cooper };
2571b89a7cc2SEnji Cooper
TEST(ParameterlessExpectationsTest,CanSetExpectationsForConstOverloadedMethods)2572b89a7cc2SEnji Cooper TEST(ParameterlessExpectationsTest,
2573b89a7cc2SEnji Cooper CanSetExpectationsForConstOverloadedMethods) {
2574b89a7cc2SEnji Cooper MockConstOverload mock;
2575b89a7cc2SEnji Cooper ON_CALL(mock, Overloaded(_)).WillByDefault(Return(7));
2576b89a7cc2SEnji Cooper ON_CALL(mock, Overloaded(5)).WillByDefault(Return(9));
2577b89a7cc2SEnji Cooper ON_CALL(Const(mock), Overloaded(5)).WillByDefault(Return(11));
2578b89a7cc2SEnji Cooper ON_CALL(Const(mock), Overloaded(7)).WillByDefault(Return(13));
2579b89a7cc2SEnji Cooper
2580b89a7cc2SEnji Cooper EXPECT_THAT(mock.Overloaded(1), 7);
2581b89a7cc2SEnji Cooper EXPECT_THAT(mock.Overloaded(5), 9);
2582b89a7cc2SEnji Cooper EXPECT_THAT(mock.Overloaded(7), 7);
2583b89a7cc2SEnji Cooper
2584b89a7cc2SEnji Cooper const MockConstOverload& const_mock = mock;
2585b89a7cc2SEnji Cooper EXPECT_THAT(const_mock.Overloaded(1), 0);
2586b89a7cc2SEnji Cooper EXPECT_THAT(const_mock.Overloaded(5), 11);
2587b89a7cc2SEnji Cooper EXPECT_THAT(const_mock.Overloaded(7), 13);
2588b89a7cc2SEnji Cooper }
2589b89a7cc2SEnji Cooper
2590b89a7cc2SEnji Cooper } // namespace
259128f6c2f2SEnji Cooper } // namespace testing
2592b89a7cc2SEnji Cooper
main(int argc,char ** argv)2593b89a7cc2SEnji Cooper int main(int argc, char** argv) {
2594b89a7cc2SEnji Cooper testing::InitGoogleMock(&argc, argv);
2595b89a7cc2SEnji Cooper // Ensures that the tests pass no matter what value of
2596b89a7cc2SEnji Cooper // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
259728f6c2f2SEnji Cooper GMOCK_FLAG_SET(catch_leaked_mocks, true);
259828f6c2f2SEnji Cooper GMOCK_FLAG_SET(verbose, testing::internal::kWarningVerbosity);
2599b89a7cc2SEnji Cooper
2600b89a7cc2SEnji Cooper return RUN_ALL_TESTS();
2601b89a7cc2SEnji Cooper }
2602