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"
43 using testing::AnyNumber;
45 using testing::InSequence;
46 using testing::NaggyMock;
48 using testing::Return;
49 using testing::Sequence;
55 MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
56 MOCK_METHOD2(Bar2, bool(int x, int y));
57 MOCK_METHOD2(Bar3, void(int x, int y));
60 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
63 class GMockOutputTest : public testing::Test {
65 NaggyMock<MockFoo> foo_;
68 TEST_F(GMockOutputTest, ExpectedCall) {
69 testing::GMOCK_FLAG(verbose) = "info";
71 EXPECT_CALL(foo_, Bar2(0, _));
72 foo_.Bar2(0, 0); // Expected call
74 testing::GMOCK_FLAG(verbose) = "warning";
77 TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
78 testing::GMOCK_FLAG(verbose) = "info";
80 EXPECT_CALL(foo_, Bar3(0, _));
81 foo_.Bar3(0, 0); // Expected call
83 testing::GMOCK_FLAG(verbose) = "warning";
86 TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
87 EXPECT_CALL(foo_, Bar2(_, _))
89 .WillOnce(Return(false));
91 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
94 TEST_F(GMockOutputTest, UnexpectedCall) {
95 EXPECT_CALL(foo_, Bar2(0, _));
97 foo_.Bar2(1, 0); // Unexpected call
98 foo_.Bar2(0, 0); // Expected call
101 TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
102 EXPECT_CALL(foo_, Bar3(0, _));
104 foo_.Bar3(1, 0); // Unexpected call
105 foo_.Bar3(0, 0); // Expected call
108 TEST_F(GMockOutputTest, ExcessiveCall) {
109 EXPECT_CALL(foo_, Bar2(0, _));
111 foo_.Bar2(0, 0); // Expected call
112 foo_.Bar2(0, 1); // Excessive call
115 TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
116 EXPECT_CALL(foo_, Bar3(0, _));
118 foo_.Bar3(0, 0); // Expected call
119 foo_.Bar3(0, 1); // Excessive call
122 TEST_F(GMockOutputTest, UninterestingCall) {
123 foo_.Bar2(0, 1); // Uninteresting call
126 TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
127 foo_.Bar3(0, 1); // Uninteresting call
130 TEST_F(GMockOutputTest, RetiredExpectation) {
131 EXPECT_CALL(foo_, Bar2(_, _))
132 .RetiresOnSaturation();
133 EXPECT_CALL(foo_, Bar2(0, 0));
136 foo_.Bar2(1, 1); // Matches a retired expectation
140 TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
143 EXPECT_CALL(foo_, Bar(_, 0, _));
144 EXPECT_CALL(foo_, Bar2(0, 0));
145 EXPECT_CALL(foo_, Bar2(1, _));
148 foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite
149 foo_.Bar("Hi", 0, 0);
154 TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
157 EXPECT_CALL(foo_, Bar(_, 0, _))
159 EXPECT_CALL(foo_, Bar2(0, 0))
161 EXPECT_CALL(foo_, Bar2(1, _))
164 foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites
165 foo_.Bar("Hi", 0, 0);
170 TEST_F(GMockOutputTest, UnsatisfiedWith) {
171 EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
174 TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
175 EXPECT_CALL(foo_, Bar(_, _, _));
176 EXPECT_CALL(foo_, Bar2(0, _))
182 TEST_F(GMockOutputTest, MismatchArguments) {
183 const std::string s = "Hi";
184 EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
186 foo_.Bar("Ho", 0, -0.1); // Mismatch arguments
190 TEST_F(GMockOutputTest, MismatchWith) {
191 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
194 foo_.Bar2(2, 3); // Mismatch With()
198 TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
199 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
202 foo_.Bar2(1, 3); // Mismatch arguments and mismatch With()
206 TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
207 ON_CALL(foo_, Bar2(_, _))
208 .WillByDefault(Return(true)); // Default action #1
209 ON_CALL(foo_, Bar2(1, _))
210 .WillByDefault(Return(false)); // Default action #2
212 EXPECT_CALL(foo_, Bar2(2, 2));
213 foo_.Bar2(1, 0); // Unexpected call, takes default action #2.
214 foo_.Bar2(0, 0); // Unexpected call, takes default action #1.
215 foo_.Bar2(2, 2); // Expected call.
218 TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
219 ON_CALL(foo_, Bar2(_, _))
220 .WillByDefault(Return(true)); // Default action #1
221 ON_CALL(foo_, Bar2(1, _))
222 .WillByDefault(Return(false)); // Default action #2
224 EXPECT_CALL(foo_, Bar2(2, 2));
225 EXPECT_CALL(foo_, Bar2(1, 1));
227 foo_.Bar2(2, 2); // Expected call.
228 foo_.Bar2(2, 2); // Excessive call, takes default action #1.
229 foo_.Bar2(1, 1); // Expected call.
230 foo_.Bar2(1, 1); // Excessive call, takes default action #2.
233 TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
234 ON_CALL(foo_, Bar2(_, _))
235 .WillByDefault(Return(true)); // Default action #1
236 ON_CALL(foo_, Bar2(1, _))
237 .WillByDefault(Return(false)); // Default action #2
239 foo_.Bar2(2, 2); // Uninteresting call, takes default action #1.
240 foo_.Bar2(1, 1); // Uninteresting call, takes default action #2.
243 TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
244 ON_CALL(foo_, Bar2(_, _))
245 .WillByDefault(Return(true)); // Default action #1
247 EXPECT_CALL(foo_, Bar2(_, _))
249 .WillOnce(Return(false));
251 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
254 TEST_F(GMockOutputTest, CatchesLeakedMocks) {
255 MockFoo* foo1 = new MockFoo;
256 MockFoo* foo2 = new MockFoo;
258 // Invokes ON_CALL on foo1.
259 ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
261 // Invokes EXPECT_CALL on foo2.
262 EXPECT_CALL(*foo2, Bar2(_, _));
263 EXPECT_CALL(*foo2, Bar2(1, _));
264 EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
268 // Both foo1 and foo2 are deliberately leaked.
271 void TestCatchesLeakedMocksInAdHocTests() {
272 MockFoo* foo = new MockFoo;
274 // Invokes EXPECT_CALL on foo.
275 EXPECT_CALL(*foo, Bar2(_, _));
278 // foo is deliberately leaked.
281 int main(int argc, char **argv) {
282 testing::InitGoogleMock(&argc, argv);
284 // Ensures that the tests pass no matter what value of
285 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
286 testing::GMOCK_FLAG(catch_leaked_mocks) = true;
287 testing::GMOCK_FLAG(verbose) = "warning";
289 TestCatchesLeakedMocksInAdHocTests();
290 return RUN_ALL_TESTS();