Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
gmock_output_test_.cc
Go to the documentation of this file.
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// Silence C4100 (unreferenced formal parameter)
43#ifdef _MSC_VER
44# pragma warning(push)
45# pragma warning(disable:4100)
46#endif
47
48using testing::_;
50using testing::Ge;
53using testing::Ref;
54using testing::Return;
56using testing::Value;
57
58class MockFoo {
59 public:
61
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));
65
66 private:
68};
69
71 protected:
73};
74
75TEST_F(GMockOutputTest, ExpectedCall) {
76 testing::GMOCK_FLAG(verbose) = "info";
77
78 EXPECT_CALL(foo_, Bar2(0, _));
79 foo_.Bar2(0, 0); // Expected call
80
81 testing::GMOCK_FLAG(verbose) = "warning";
82}
83
84TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
85 testing::GMOCK_FLAG(verbose) = "info";
86
87 EXPECT_CALL(foo_, Bar3(0, _));
88 foo_.Bar3(0, 0); // Expected call
89
90 testing::GMOCK_FLAG(verbose) = "warning";
91}
92
93TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
94 EXPECT_CALL(foo_, Bar2(_, _))
95 .Times(2)
96 .WillOnce(Return(false));
97 foo_.Bar2(2, 2);
98 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
99}
100
101TEST_F(GMockOutputTest, UnexpectedCall) {
102 EXPECT_CALL(foo_, Bar2(0, _));
103
104 foo_.Bar2(1, 0); // Unexpected call
105 foo_.Bar2(0, 0); // Expected call
106}
107
108TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
109 EXPECT_CALL(foo_, Bar3(0, _));
110
111 foo_.Bar3(1, 0); // Unexpected call
112 foo_.Bar3(0, 0); // Expected call
113}
114
115TEST_F(GMockOutputTest, ExcessiveCall) {
116 EXPECT_CALL(foo_, Bar2(0, _));
117
118 foo_.Bar2(0, 0); // Expected call
119 foo_.Bar2(0, 1); // Excessive call
120}
121
122TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
123 EXPECT_CALL(foo_, Bar3(0, _));
124
125 foo_.Bar3(0, 0); // Expected call
126 foo_.Bar3(0, 1); // Excessive call
127}
128
129TEST_F(GMockOutputTest, UninterestingCall) {
130 foo_.Bar2(0, 1); // Uninteresting call
131}
132
133TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
134 foo_.Bar3(0, 1); // Uninteresting call
135}
136
137TEST_F(GMockOutputTest, RetiredExpectation) {
138 EXPECT_CALL(foo_, Bar2(_, _))
139 .RetiresOnSaturation();
140 EXPECT_CALL(foo_, Bar2(0, 0));
141
142 foo_.Bar2(1, 1);
143 foo_.Bar2(1, 1); // Matches a retired expectation
144 foo_.Bar2(0, 0);
145}
146
147TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
148 {
150 EXPECT_CALL(foo_, Bar(_, 0, _));
151 EXPECT_CALL(foo_, Bar2(0, 0));
152 EXPECT_CALL(foo_, Bar2(1, _));
153 }
154
155 foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite
156 foo_.Bar("Hi", 0, 0);
157 foo_.Bar2(0, 0);
158 foo_.Bar2(1, 0);
159}
160
161TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
162 Sequence s1, s2;
163
164 EXPECT_CALL(foo_, Bar(_, 0, _))
165 .InSequence(s1);
166 EXPECT_CALL(foo_, Bar2(0, 0))
167 .InSequence(s2);
168 EXPECT_CALL(foo_, Bar2(1, _))
169 .InSequence(s1, s2);
170
171 foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites
172 foo_.Bar("Hi", 0, 0);
173 foo_.Bar2(0, 0);
174 foo_.Bar2(1, 0);
175}
176
177TEST_F(GMockOutputTest, UnsatisfiedWith) {
178 EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
179}
180
181TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
182 EXPECT_CALL(foo_, Bar(_, _, _));
183 EXPECT_CALL(foo_, Bar2(0, _))
184 .Times(2);
185
186 foo_.Bar2(0, 1);
187}
188
189TEST_F(GMockOutputTest, MismatchArguments) {
190 const std::string s = "Hi";
191 EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
192
193 foo_.Bar("Ho", 0, -0.1); // Mismatch arguments
194 foo_.Bar(s, 0, 0);
195}
196
197TEST_F(GMockOutputTest, MismatchWith) {
198 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
199 .With(Ge());
200
201 foo_.Bar2(2, 3); // Mismatch With()
202 foo_.Bar2(2, 1);
203}
204
205TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
206 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
207 .With(Ge());
208
209 foo_.Bar2(1, 3); // Mismatch arguments and mismatch With()
210 foo_.Bar2(2, 1);
211}
212
213TEST_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
218
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.
223}
224
225TEST_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
230
231 EXPECT_CALL(foo_, Bar2(2, 2));
232 EXPECT_CALL(foo_, Bar2(1, 1));
233
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.
238}
239
240TEST_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
245
246 foo_.Bar2(2, 2); // Uninteresting call, takes default action #1.
247 foo_.Bar2(1, 1); // Uninteresting call, takes default action #2.
248}
249
250TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
251 ON_CALL(foo_, Bar2(_, _))
252 .WillByDefault(Return(true)); // Default action #1
253
254 EXPECT_CALL(foo_, Bar2(_, _))
255 .Times(2)
256 .WillOnce(Return(false));
257 foo_.Bar2(2, 2);
258 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
259}
260
261TEST_F(GMockOutputTest, CatchesLeakedMocks) {
262 MockFoo* foo1 = new MockFoo;
263 MockFoo* foo2 = new MockFoo;
264
265 // Invokes ON_CALL on foo1.
266 ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
267
268 // Invokes EXPECT_CALL on foo2.
269 EXPECT_CALL(*foo2, Bar2(_, _));
270 EXPECT_CALL(*foo2, Bar2(1, _));
271 EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
272 foo2->Bar2(2, 1);
273 foo2->Bar2(1, 1);
274
275 // Both foo1 and foo2 are deliberately leaked.
276}
277
278MATCHER_P2(IsPair, first, second, "") {
279 return Value(arg.first, first) && Value(arg.second, second);
280}
281
282TEST_F(GMockOutputTest, PrintsMatcher) {
283 const testing::Matcher<int> m1 = Ge(48);
284 EXPECT_THAT((std::pair<int, bool>(42, true)), IsPair(m1, true));
285}
286
288 MockFoo* foo = new MockFoo;
289
290 // Invokes EXPECT_CALL on foo.
291 EXPECT_CALL(*foo, Bar2(_, _));
292 foo->Bar2(2, 1);
293
294 // foo is deliberately leaked.
295}
296
297int main(int argc, char **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";
303
305 return RUN_ALL_TESTS();
306}
307
308#ifdef _MSC_VER
309# pragma warning(pop)
310#endif
NaggyMock< MockFoo > foo_
MOCK_METHOD3(Bar, char(const std::string &s, int i, double x))
MOCK_METHOD2(Bar2, bool(int x, int y))
MOCK_METHOD2(Bar3, void(int x, int y))
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition document.h:2110
#define MATCHER_P2(name, p0, p1, description)
#define EXPECT_THAT(value, matcher)
#define EXPECT_CALL(obj, call)
#define ON_CALL(obj, call)
void TestCatchesLeakedMocksInAdHocTests()
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition gtest-port.h:917
#define TEST_F(test_fixture, test_name)
Definition gtest.h:2304
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition gtest.h:2328
char ** argv
GTEST_API_ void InitGoogleMock(int *argc, char **argv)
Definition gmock.cc:195
PolymorphicAction< internal::ReturnVoidAction > Return()
const internal::AnythingMatcher _
internal::Ge2Matcher Ge()
GTEST_API_ Cardinality AnyNumber()
bool Value(const T &value, M matcher)
internal::RefMatcher< T & > Ref(T &x)
char * s