]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // 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 | ||
42 | using testing::_; | |
43 | using testing::AnyNumber; | |
44 | using testing::Ge; | |
45 | using testing::InSequence; | |
46 | using testing::NaggyMock; | |
47 | using testing::Ref; | |
48 | using testing::Return; | |
49 | using testing::Sequence; | |
50 | ||
51 | class MockFoo { | |
52 | public: | |
53 | MockFoo() {} | |
54 | ||
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)); | |
58 | ||
59 | private: | |
60 | GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo); | |
61 | }; | |
62 | ||
63 | class GMockOutputTest : public testing::Test { | |
64 | protected: | |
65 | NaggyMock<MockFoo> foo_; | |
66 | }; | |
67 | ||
68 | TEST_F(GMockOutputTest, ExpectedCall) { | |
69 | testing::GMOCK_FLAG(verbose) = "info"; | |
70 | ||
71 | EXPECT_CALL(foo_, Bar2(0, _)); | |
72 | foo_.Bar2(0, 0); // Expected call | |
73 | ||
74 | testing::GMOCK_FLAG(verbose) = "warning"; | |
75 | } | |
76 | ||
77 | TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) { | |
78 | testing::GMOCK_FLAG(verbose) = "info"; | |
79 | ||
80 | EXPECT_CALL(foo_, Bar3(0, _)); | |
81 | foo_.Bar3(0, 0); // Expected call | |
82 | ||
83 | testing::GMOCK_FLAG(verbose) = "warning"; | |
84 | } | |
85 | ||
86 | TEST_F(GMockOutputTest, ExplicitActionsRunOut) { | |
87 | EXPECT_CALL(foo_, Bar2(_, _)) | |
88 | .Times(2) | |
89 | .WillOnce(Return(false)); | |
90 | foo_.Bar2(2, 2); | |
91 | foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out. | |
92 | } | |
93 | ||
94 | TEST_F(GMockOutputTest, UnexpectedCall) { | |
95 | EXPECT_CALL(foo_, Bar2(0, _)); | |
96 | ||
97 | foo_.Bar2(1, 0); // Unexpected call | |
98 | foo_.Bar2(0, 0); // Expected call | |
99 | } | |
100 | ||
101 | TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) { | |
102 | EXPECT_CALL(foo_, Bar3(0, _)); | |
103 | ||
104 | foo_.Bar3(1, 0); // Unexpected call | |
105 | foo_.Bar3(0, 0); // Expected call | |
106 | } | |
107 | ||
108 | TEST_F(GMockOutputTest, ExcessiveCall) { | |
109 | EXPECT_CALL(foo_, Bar2(0, _)); | |
110 | ||
111 | foo_.Bar2(0, 0); // Expected call | |
112 | foo_.Bar2(0, 1); // Excessive call | |
113 | } | |
114 | ||
115 | TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) { | |
116 | EXPECT_CALL(foo_, Bar3(0, _)); | |
117 | ||
118 | foo_.Bar3(0, 0); // Expected call | |
119 | foo_.Bar3(0, 1); // Excessive call | |
120 | } | |
121 | ||
122 | TEST_F(GMockOutputTest, UninterestingCall) { | |
123 | foo_.Bar2(0, 1); // Uninteresting call | |
124 | } | |
125 | ||
126 | TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) { | |
127 | foo_.Bar3(0, 1); // Uninteresting call | |
128 | } | |
129 | ||
130 | TEST_F(GMockOutputTest, RetiredExpectation) { | |
131 | EXPECT_CALL(foo_, Bar2(_, _)) | |
132 | .RetiresOnSaturation(); | |
133 | EXPECT_CALL(foo_, Bar2(0, 0)); | |
134 | ||
135 | foo_.Bar2(1, 1); | |
136 | foo_.Bar2(1, 1); // Matches a retired expectation | |
137 | foo_.Bar2(0, 0); | |
138 | } | |
139 | ||
140 | TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) { | |
141 | { | |
142 | InSequence s; | |
143 | EXPECT_CALL(foo_, Bar(_, 0, _)); | |
144 | EXPECT_CALL(foo_, Bar2(0, 0)); | |
145 | EXPECT_CALL(foo_, Bar2(1, _)); | |
146 | } | |
147 | ||
148 | foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite | |
149 | foo_.Bar("Hi", 0, 0); | |
150 | foo_.Bar2(0, 0); | |
151 | foo_.Bar2(1, 0); | |
152 | } | |
153 | ||
154 | TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) { | |
155 | Sequence s1, s2; | |
156 | ||
157 | EXPECT_CALL(foo_, Bar(_, 0, _)) | |
158 | .InSequence(s1); | |
159 | EXPECT_CALL(foo_, Bar2(0, 0)) | |
160 | .InSequence(s2); | |
161 | EXPECT_CALL(foo_, Bar2(1, _)) | |
162 | .InSequence(s1, s2); | |
163 | ||
164 | foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites | |
165 | foo_.Bar("Hi", 0, 0); | |
166 | foo_.Bar2(0, 0); | |
167 | foo_.Bar2(1, 0); | |
168 | } | |
169 | ||
170 | TEST_F(GMockOutputTest, UnsatisfiedWith) { | |
171 | EXPECT_CALL(foo_, Bar2(_, _)).With(Ge()); | |
172 | } | |
173 | ||
174 | TEST_F(GMockOutputTest, UnsatisfiedExpectation) { | |
175 | EXPECT_CALL(foo_, Bar(_, _, _)); | |
176 | EXPECT_CALL(foo_, Bar2(0, _)) | |
177 | .Times(2); | |
178 | ||
179 | foo_.Bar2(0, 1); | |
180 | } | |
181 | ||
182 | TEST_F(GMockOutputTest, MismatchArguments) { | |
183 | const std::string s = "Hi"; | |
184 | EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0))); | |
185 | ||
186 | foo_.Bar("Ho", 0, -0.1); // Mismatch arguments | |
187 | foo_.Bar(s, 0, 0); | |
188 | } | |
189 | ||
190 | TEST_F(GMockOutputTest, MismatchWith) { | |
191 | EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))) | |
192 | .With(Ge()); | |
193 | ||
194 | foo_.Bar2(2, 3); // Mismatch With() | |
195 | foo_.Bar2(2, 1); | |
196 | } | |
197 | ||
198 | TEST_F(GMockOutputTest, MismatchArgumentsAndWith) { | |
199 | EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))) | |
200 | .With(Ge()); | |
201 | ||
202 | foo_.Bar2(1, 3); // Mismatch arguments and mismatch With() | |
203 | foo_.Bar2(2, 1); | |
204 | } | |
205 | ||
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 | |
211 | ||
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. | |
216 | } | |
217 | ||
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 | |
223 | ||
224 | EXPECT_CALL(foo_, Bar2(2, 2)); | |
225 | EXPECT_CALL(foo_, Bar2(1, 1)); | |
226 | ||
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. | |
231 | } | |
232 | ||
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 | |
238 | ||
239 | foo_.Bar2(2, 2); // Uninteresting call, takes default action #1. | |
240 | foo_.Bar2(1, 1); // Uninteresting call, takes default action #2. | |
241 | } | |
242 | ||
243 | TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) { | |
244 | ON_CALL(foo_, Bar2(_, _)) | |
245 | .WillByDefault(Return(true)); // Default action #1 | |
246 | ||
247 | EXPECT_CALL(foo_, Bar2(_, _)) | |
248 | .Times(2) | |
249 | .WillOnce(Return(false)); | |
250 | foo_.Bar2(2, 2); | |
251 | foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out. | |
252 | } | |
253 | ||
254 | TEST_F(GMockOutputTest, CatchesLeakedMocks) { | |
255 | MockFoo* foo1 = new MockFoo; | |
256 | MockFoo* foo2 = new MockFoo; | |
257 | ||
258 | // Invokes ON_CALL on foo1. | |
259 | ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a')); | |
260 | ||
261 | // Invokes EXPECT_CALL on foo2. | |
262 | EXPECT_CALL(*foo2, Bar2(_, _)); | |
263 | EXPECT_CALL(*foo2, Bar2(1, _)); | |
264 | EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber()); | |
265 | foo2->Bar2(2, 1); | |
266 | foo2->Bar2(1, 1); | |
267 | ||
268 | // Both foo1 and foo2 are deliberately leaked. | |
269 | } | |
270 | ||
271 | void TestCatchesLeakedMocksInAdHocTests() { | |
272 | MockFoo* foo = new MockFoo; | |
273 | ||
274 | // Invokes EXPECT_CALL on foo. | |
275 | EXPECT_CALL(*foo, Bar2(_, _)); | |
276 | foo->Bar2(2, 1); | |
277 | ||
278 | // foo is deliberately leaked. | |
279 | } | |
280 | ||
281 | int main(int argc, char **argv) { | |
282 | testing::InitGoogleMock(&argc, argv); | |
283 | ||
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"; | |
288 | ||
289 | TestCatchesLeakedMocksInAdHocTests(); | |
290 | return RUN_ALL_TESTS(); | |
291 | } |