1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Tests Google Mock's output in various scenarios. This ensures that
33 // Google Mock's messages are readable and useful.
35 #include "gmock/gmock.h"
40 #include "gtest/gtest.h"
42 // Silence C4100 (unreferenced formal parameter)
44 # pragma warning(push)
45 # pragma warning(disable:4100)
49 using testing::AnyNumber
;
51 using testing::InSequence
;
52 using testing::NaggyMock
;
54 using testing::Return
;
55 using testing::Sequence
;
62 MOCK_METHOD3(Bar
, char(const std::string
& s
, int i
, double x
));
63 MOCK_METHOD2(Bar2
, bool(int x
, int y
));
64 MOCK_METHOD2(Bar3
, void(int x
, int y
));
67 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo
);
70 class GMockOutputTest
: public testing::Test
{
72 NaggyMock
<MockFoo
> foo_
;
75 TEST_F(GMockOutputTest
, ExpectedCall
) {
76 testing::GMOCK_FLAG(verbose
) = "info";
78 EXPECT_CALL(foo_
, Bar2(0, _
));
79 foo_
.Bar2(0, 0); // Expected call
81 testing::GMOCK_FLAG(verbose
) = "warning";
84 TEST_F(GMockOutputTest
, ExpectedCallToVoidFunction
) {
85 testing::GMOCK_FLAG(verbose
) = "info";
87 EXPECT_CALL(foo_
, Bar3(0, _
));
88 foo_
.Bar3(0, 0); // Expected call
90 testing::GMOCK_FLAG(verbose
) = "warning";
93 TEST_F(GMockOutputTest
, ExplicitActionsRunOut
) {
94 EXPECT_CALL(foo_
, Bar2(_
, _
))
96 .WillOnce(Return(false));
98 foo_
.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
101 TEST_F(GMockOutputTest
, UnexpectedCall
) {
102 EXPECT_CALL(foo_
, Bar2(0, _
));
104 foo_
.Bar2(1, 0); // Unexpected call
105 foo_
.Bar2(0, 0); // Expected call
108 TEST_F(GMockOutputTest
, UnexpectedCallToVoidFunction
) {
109 EXPECT_CALL(foo_
, Bar3(0, _
));
111 foo_
.Bar3(1, 0); // Unexpected call
112 foo_
.Bar3(0, 0); // Expected call
115 TEST_F(GMockOutputTest
, ExcessiveCall
) {
116 EXPECT_CALL(foo_
, Bar2(0, _
));
118 foo_
.Bar2(0, 0); // Expected call
119 foo_
.Bar2(0, 1); // Excessive call
122 TEST_F(GMockOutputTest
, ExcessiveCallToVoidFunction
) {
123 EXPECT_CALL(foo_
, Bar3(0, _
));
125 foo_
.Bar3(0, 0); // Expected call
126 foo_
.Bar3(0, 1); // Excessive call
129 TEST_F(GMockOutputTest
, UninterestingCall
) {
130 foo_
.Bar2(0, 1); // Uninteresting call
133 TEST_F(GMockOutputTest
, UninterestingCallToVoidFunction
) {
134 foo_
.Bar3(0, 1); // Uninteresting call
137 TEST_F(GMockOutputTest
, RetiredExpectation
) {
138 EXPECT_CALL(foo_
, Bar2(_
, _
))
139 .RetiresOnSaturation();
140 EXPECT_CALL(foo_
, Bar2(0, 0));
143 foo_
.Bar2(1, 1); // Matches a retired expectation
147 TEST_F(GMockOutputTest
, UnsatisfiedPrerequisite
) {
150 EXPECT_CALL(foo_
, Bar(_
, 0, _
));
151 EXPECT_CALL(foo_
, Bar2(0, 0));
152 EXPECT_CALL(foo_
, Bar2(1, _
));
155 foo_
.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite
156 foo_
.Bar("Hi", 0, 0);
161 TEST_F(GMockOutputTest
, UnsatisfiedPrerequisites
) {
164 EXPECT_CALL(foo_
, Bar(_
, 0, _
))
166 EXPECT_CALL(foo_
, Bar2(0, 0))
168 EXPECT_CALL(foo_
, Bar2(1, _
))
171 foo_
.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites
172 foo_
.Bar("Hi", 0, 0);
177 TEST_F(GMockOutputTest
, UnsatisfiedWith
) {
178 EXPECT_CALL(foo_
, Bar2(_
, _
)).With(Ge());
181 TEST_F(GMockOutputTest
, UnsatisfiedExpectation
) {
182 EXPECT_CALL(foo_
, Bar(_
, _
, _
));
183 EXPECT_CALL(foo_
, Bar2(0, _
))
189 TEST_F(GMockOutputTest
, MismatchArguments
) {
190 const std::string s
= "Hi";
191 EXPECT_CALL(foo_
, Bar(Ref(s
), _
, Ge(0)));
193 foo_
.Bar("Ho", 0, -0.1); // Mismatch arguments
197 TEST_F(GMockOutputTest
, MismatchWith
) {
198 EXPECT_CALL(foo_
, Bar2(Ge(2), Ge(1)))
201 foo_
.Bar2(2, 3); // Mismatch With()
205 TEST_F(GMockOutputTest
, MismatchArgumentsAndWith
) {
206 EXPECT_CALL(foo_
, Bar2(Ge(2), Ge(1)))
209 foo_
.Bar2(1, 3); // Mismatch arguments and mismatch With()
213 TEST_F(GMockOutputTest
, UnexpectedCallWithDefaultAction
) {
214 ON_CALL(foo_
, Bar2(_
, _
))
215 .WillByDefault(Return(true)); // Default action #1
216 ON_CALL(foo_
, Bar2(1, _
))
217 .WillByDefault(Return(false)); // Default action #2
219 EXPECT_CALL(foo_
, Bar2(2, 2));
220 foo_
.Bar2(1, 0); // Unexpected call, takes default action #2.
221 foo_
.Bar2(0, 0); // Unexpected call, takes default action #1.
222 foo_
.Bar2(2, 2); // Expected call.
225 TEST_F(GMockOutputTest
, ExcessiveCallWithDefaultAction
) {
226 ON_CALL(foo_
, Bar2(_
, _
))
227 .WillByDefault(Return(true)); // Default action #1
228 ON_CALL(foo_
, Bar2(1, _
))
229 .WillByDefault(Return(false)); // Default action #2
231 EXPECT_CALL(foo_
, Bar2(2, 2));
232 EXPECT_CALL(foo_
, Bar2(1, 1));
234 foo_
.Bar2(2, 2); // Expected call.
235 foo_
.Bar2(2, 2); // Excessive call, takes default action #1.
236 foo_
.Bar2(1, 1); // Expected call.
237 foo_
.Bar2(1, 1); // Excessive call, takes default action #2.
240 TEST_F(GMockOutputTest
, UninterestingCallWithDefaultAction
) {
241 ON_CALL(foo_
, Bar2(_
, _
))
242 .WillByDefault(Return(true)); // Default action #1
243 ON_CALL(foo_
, Bar2(1, _
))
244 .WillByDefault(Return(false)); // Default action #2
246 foo_
.Bar2(2, 2); // Uninteresting call, takes default action #1.
247 foo_
.Bar2(1, 1); // Uninteresting call, takes default action #2.
250 TEST_F(GMockOutputTest
, ExplicitActionsRunOutWithDefaultAction
) {
251 ON_CALL(foo_
, Bar2(_
, _
))
252 .WillByDefault(Return(true)); // Default action #1
254 EXPECT_CALL(foo_
, Bar2(_
, _
))
256 .WillOnce(Return(false));
258 foo_
.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
261 TEST_F(GMockOutputTest
, CatchesLeakedMocks
) {
262 MockFoo
* foo1
= new MockFoo
;
263 MockFoo
* foo2
= new MockFoo
;
265 // Invokes ON_CALL on foo1.
266 ON_CALL(*foo1
, Bar(_
, _
, _
)).WillByDefault(Return('a'));
268 // Invokes EXPECT_CALL on foo2.
269 EXPECT_CALL(*foo2
, Bar2(_
, _
));
270 EXPECT_CALL(*foo2
, Bar2(1, _
));
271 EXPECT_CALL(*foo2
, Bar3(_
, _
)).Times(AnyNumber());
275 // Both foo1 and foo2 are deliberately leaked.
278 MATCHER_P2(IsPair
, first
, second
, "") {
279 return Value(arg
.first
, first
) && Value(arg
.second
, second
);
282 TEST_F(GMockOutputTest
, PrintsMatcher
) {
283 const testing::Matcher
<int> m1
= Ge(48);
284 EXPECT_THAT((std::pair
<int, bool>(42, true)), IsPair(m1
, true));
287 void TestCatchesLeakedMocksInAdHocTests() {
288 MockFoo
* foo
= new MockFoo
;
290 // Invokes EXPECT_CALL on foo.
291 EXPECT_CALL(*foo
, Bar2(_
, _
));
294 // foo is deliberately leaked.
297 int main(int argc
, char **argv
) {
298 testing::InitGoogleMock(&argc
, argv
);
299 // Ensures that the tests pass no matter what value of
300 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
301 testing::GMOCK_FLAG(catch_leaked_mocks
) = true;
302 testing::GMOCK_FLAG(verbose
) = "warning";
304 TestCatchesLeakedMocksInAdHocTests();
305 return RUN_ALL_TESTS();
309 # pragma warning(pop)