blob: c33dc6fb3237fb16c9bea616667f6241b15f0857 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
32// Tests Google Mock's output in various scenarios. This ensures that
33// Google Mock's messages are readable and useful.
34
35#include <gmock/gmock.h>
36
37#include <stdio.h>
38#include <string>
39
40#include <gtest/gtest.h>
41
42using testing::_;
zhanyong.wandf35a762009-04-22 22:25:31 +000043using testing::AnyNumber;
shiqiane35fdd92008-12-10 05:08:54 +000044using testing::Ge;
45using testing::InSequence;
46using testing::Ref;
47using testing::Return;
48using testing::Sequence;
49
50class MockFoo {
51 public:
52 MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
53 MOCK_METHOD2(Bar2, bool(int x, int y));
54 MOCK_METHOD2(Bar3, void(int x, int y));
55};
56
57class GMockOutputTest : public testing::Test {
58 protected:
59 MockFoo foo_;
60};
61
62TEST_F(GMockOutputTest, ExpectedCall) {
63 testing::GMOCK_FLAG(verbose) = "info";
64
65 EXPECT_CALL(foo_, Bar2(0, _));
66 foo_.Bar2(0, 0); // Expected call
67
68 testing::GMOCK_FLAG(verbose) = "warning";
69}
70
71TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
72 testing::GMOCK_FLAG(verbose) = "info";
73
74 EXPECT_CALL(foo_, Bar3(0, _));
75 foo_.Bar3(0, 0); // Expected call
76
77 testing::GMOCK_FLAG(verbose) = "warning";
78}
79
80TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
81 EXPECT_CALL(foo_, Bar2(_, _))
82 .Times(2)
83 .WillOnce(Return(false));
84 foo_.Bar2(2, 2);
85 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
86}
87
88TEST_F(GMockOutputTest, UnexpectedCall) {
89 EXPECT_CALL(foo_, Bar2(0, _));
90
91 foo_.Bar2(1, 0); // Unexpected call
92 foo_.Bar2(0, 0); // Expected call
93}
94
95TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
96 EXPECT_CALL(foo_, Bar3(0, _));
97
98 foo_.Bar3(1, 0); // Unexpected call
99 foo_.Bar3(0, 0); // Expected call
100}
101
102TEST_F(GMockOutputTest, ExcessiveCall) {
103 EXPECT_CALL(foo_, Bar2(0, _));
104
105 foo_.Bar2(0, 0); // Expected call
106 foo_.Bar2(0, 1); // Excessive call
107}
108
109TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
110 EXPECT_CALL(foo_, Bar3(0, _));
111
112 foo_.Bar3(0, 0); // Expected call
113 foo_.Bar3(0, 1); // Excessive call
114}
115
116TEST_F(GMockOutputTest, UninterestingCall) {
117 foo_.Bar2(0, 1); // Uninteresting call
118}
119
120TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
121 foo_.Bar3(0, 1); // Uninteresting call
122}
123
124TEST_F(GMockOutputTest, RetiredExpectation) {
125 EXPECT_CALL(foo_, Bar2(_, _))
126 .RetiresOnSaturation();
127 EXPECT_CALL(foo_, Bar2(0, 0));
128
129 foo_.Bar2(1, 1);
130 foo_.Bar2(1, 1); // Matches a retired expectation
131 foo_.Bar2(0, 0);
132}
133
134TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
135 {
136 InSequence s;
137 EXPECT_CALL(foo_, Bar(_, 0, _));
138 EXPECT_CALL(foo_, Bar2(0, 0));
139 EXPECT_CALL(foo_, Bar2(1, _));
140 }
141
142 foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite
143 foo_.Bar("Hi", 0, 0);
144 foo_.Bar2(0, 0);
145 foo_.Bar2(1, 0);
146}
147
148TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
149 Sequence s1, s2;
150
151 EXPECT_CALL(foo_, Bar(_, 0, _))
152 .InSequence(s1);
153 EXPECT_CALL(foo_, Bar2(0, 0))
154 .InSequence(s2);
155 EXPECT_CALL(foo_, Bar2(1, _))
156 .InSequence(s1, s2);
157
158 foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites
159 foo_.Bar("Hi", 0, 0);
160 foo_.Bar2(0, 0);
161 foo_.Bar2(1, 0);
162}
163
vladlosev6c54a5e2009-10-21 06:15:34 +0000164TEST_F(GMockOutputTest, UnsatisfiedWith) {
165 EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
166}
167
shiqiane35fdd92008-12-10 05:08:54 +0000168TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
169 EXPECT_CALL(foo_, Bar(_, _, _));
170 EXPECT_CALL(foo_, Bar2(0, _))
171 .Times(2);
172
173 foo_.Bar2(0, 1);
174}
175
176TEST_F(GMockOutputTest, MismatchArguments) {
177 const std::string s = "Hi";
178 EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
179
180 foo_.Bar("Ho", 0, -0.1); // Mismatch arguments
181 foo_.Bar(s, 0, 0);
182}
183
zhanyong.wanbf550852009-06-09 06:09:53 +0000184TEST_F(GMockOutputTest, MismatchWith) {
shiqiane35fdd92008-12-10 05:08:54 +0000185 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
zhanyong.wanbf550852009-06-09 06:09:53 +0000186 .With(Ge());
shiqiane35fdd92008-12-10 05:08:54 +0000187
zhanyong.wanbf550852009-06-09 06:09:53 +0000188 foo_.Bar2(2, 3); // Mismatch With()
shiqiane35fdd92008-12-10 05:08:54 +0000189 foo_.Bar2(2, 1);
190}
191
zhanyong.wanbf550852009-06-09 06:09:53 +0000192TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
shiqiane35fdd92008-12-10 05:08:54 +0000193 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
zhanyong.wanbf550852009-06-09 06:09:53 +0000194 .With(Ge());
shiqiane35fdd92008-12-10 05:08:54 +0000195
zhanyong.wanbf550852009-06-09 06:09:53 +0000196 foo_.Bar2(1, 3); // Mismatch arguments and mismatch With()
shiqiane35fdd92008-12-10 05:08:54 +0000197 foo_.Bar2(2, 1);
198}
199
200TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
201 ON_CALL(foo_, Bar2(_, _))
202 .WillByDefault(Return(true)); // Default action #1
203 ON_CALL(foo_, Bar2(1, _))
204 .WillByDefault(Return(false)); // Default action #2
205
206 EXPECT_CALL(foo_, Bar2(2, 2));
207 foo_.Bar2(1, 0); // Unexpected call, takes default action #2.
208 foo_.Bar2(0, 0); // Unexpected call, takes default action #1.
209 foo_.Bar2(2, 2); // Expected call.
210}
211
212TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
213 ON_CALL(foo_, Bar2(_, _))
214 .WillByDefault(Return(true)); // Default action #1
215 ON_CALL(foo_, Bar2(1, _))
216 .WillByDefault(Return(false)); // Default action #2
217
218 EXPECT_CALL(foo_, Bar2(2, 2));
219 EXPECT_CALL(foo_, Bar2(1, 1));
220
221 foo_.Bar2(2, 2); // Expected call.
222 foo_.Bar2(2, 2); // Excessive call, takes default action #1.
223 foo_.Bar2(1, 1); // Expected call.
224 foo_.Bar2(1, 1); // Excessive call, takes default action #2.
225}
226
227TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
228 ON_CALL(foo_, Bar2(_, _))
229 .WillByDefault(Return(true)); // Default action #1
230 ON_CALL(foo_, Bar2(1, _))
231 .WillByDefault(Return(false)); // Default action #2
232
233 foo_.Bar2(2, 2); // Uninteresting call, takes default action #1.
234 foo_.Bar2(1, 1); // Uninteresting call, takes default action #2.
235}
236
237TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
238 ON_CALL(foo_, Bar2(_, _))
239 .WillByDefault(Return(true)); // Default action #1
240
241 EXPECT_CALL(foo_, Bar2(_, _))
242 .Times(2)
243 .WillOnce(Return(false));
244 foo_.Bar2(2, 2);
245 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
246}
zhanyong.wandf35a762009-04-22 22:25:31 +0000247
248TEST_F(GMockOutputTest, CatchesLeakedMocks) {
249 MockFoo* foo1 = new MockFoo;
250 MockFoo* foo2 = new MockFoo;
251
252 // Invokes ON_CALL on foo1.
253 ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
254
255 // Invokes EXPECT_CALL on foo2.
256 EXPECT_CALL(*foo2, Bar2(_, _));
257 EXPECT_CALL(*foo2, Bar2(1, _));
258 EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
259 foo2->Bar2(2, 1);
260 foo2->Bar2(1, 1);
261
262 // Both foo1 and foo2 are deliberately leaked.
263}
264
zhanyong.wane7bb5ed2009-05-05 23:14:47 +0000265void TestCatchesLeakedMocksInAdHocTests() {
266 MockFoo* foo = new MockFoo;
267
268 // Invokes EXPECT_CALL on foo.
269 EXPECT_CALL(*foo, Bar2(_, _));
270 foo->Bar2(2, 1);
271
272 // foo is deliberately leaked.
273}
274
zhanyong.wandf35a762009-04-22 22:25:31 +0000275int main(int argc, char **argv) {
276 testing::InitGoogleMock(&argc, argv);
277
278 // Ensures that the tests pass no matter what value of
279 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
280 testing::GMOCK_FLAG(catch_leaked_mocks) = true;
281 testing::GMOCK_FLAG(verbose) = "warning";
282
zhanyong.wane7bb5ed2009-05-05 23:14:47 +0000283 TestCatchesLeakedMocksInAdHocTests();
zhanyong.wandf35a762009-04-22 22:25:31 +0000284 return RUN_ALL_TESTS();
285}