Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
31 // Tests Google Mock's output in various scenarios. This ensures that
32 // Google Mock's messages are readable and useful.
33 
34 #include "gmock/gmock.h"
35 
36 #include <stdio.h>
37 #include <string>
38 
39 #include "gtest/gtest.h"
40 
41 // Silence C4100 (unreferenced formal parameter)
42 #ifdef _MSC_VER
43 # pragma warning(push)
44 # pragma warning(disable:4100)
45 #endif
46 
47 using testing::_;
48 using testing::AnyNumber;
49 using testing::Ge;
50 using testing::InSequence;
51 using testing::NaggyMock;
52 using testing::Ref;
53 using testing::Return;
54 using testing::Sequence;
55 using testing::Value;
56 
57 class MockFoo {
58  public:
59  MockFoo() {}
60 
61  MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
62  MOCK_METHOD2(Bar2, bool(int x, int y));
63  MOCK_METHOD2(Bar3, void(int x, int y));
64 
65  private:
67 };
68 
70  protected:
72 };
73 
74 TEST_F(GMockOutputTest, ExpectedCall) {
75  testing::GMOCK_FLAG(verbose) = "info";
76 
77  EXPECT_CALL(foo_, Bar2(0, _));
78  foo_.Bar2(0, 0); // Expected call
79 
80  testing::GMOCK_FLAG(verbose) = "warning";
81 }
82 
83 TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
84  testing::GMOCK_FLAG(verbose) = "info";
85 
86  EXPECT_CALL(foo_, Bar3(0, _));
87  foo_.Bar3(0, 0); // Expected call
88 
89  testing::GMOCK_FLAG(verbose) = "warning";
90 }
91 
92 TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
93  EXPECT_CALL(foo_, Bar2(_, _))
94  .Times(2)
95  .WillOnce(Return(false));
96  foo_.Bar2(2, 2);
97  foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
98 }
99 
100 TEST_F(GMockOutputTest, UnexpectedCall) {
101  EXPECT_CALL(foo_, Bar2(0, _));
102 
103  foo_.Bar2(1, 0); // Unexpected call
104  foo_.Bar2(0, 0); // Expected call
105 }
106 
107 TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
108  EXPECT_CALL(foo_, Bar3(0, _));
109 
110  foo_.Bar3(1, 0); // Unexpected call
111  foo_.Bar3(0, 0); // Expected call
112 }
113 
114 TEST_F(GMockOutputTest, ExcessiveCall) {
115  EXPECT_CALL(foo_, Bar2(0, _));
116 
117  foo_.Bar2(0, 0); // Expected call
118  foo_.Bar2(0, 1); // Excessive call
119 }
120 
121 TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
122  EXPECT_CALL(foo_, Bar3(0, _));
123 
124  foo_.Bar3(0, 0); // Expected call
125  foo_.Bar3(0, 1); // Excessive call
126 }
127 
128 TEST_F(GMockOutputTest, UninterestingCall) {
129  foo_.Bar2(0, 1); // Uninteresting call
130 }
131 
132 TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
133  foo_.Bar3(0, 1); // Uninteresting call
134 }
135 
136 TEST_F(GMockOutputTest, RetiredExpectation) {
137  EXPECT_CALL(foo_, Bar2(_, _))
138  .RetiresOnSaturation();
139  EXPECT_CALL(foo_, Bar2(0, 0));
140 
141  foo_.Bar2(1, 1);
142  foo_.Bar2(1, 1); // Matches a retired expectation
143  foo_.Bar2(0, 0);
144 }
145 
146 TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
147  {
148  InSequence s;
149  EXPECT_CALL(foo_, Bar(_, 0, _));
150  EXPECT_CALL(foo_, Bar2(0, 0));
151  EXPECT_CALL(foo_, Bar2(1, _));
152  }
153 
154  foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite
155  foo_.Bar("Hi", 0, 0);
156  foo_.Bar2(0, 0);
157  foo_.Bar2(1, 0);
158 }
159 
160 TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
161  Sequence s1, s2;
162 
163  EXPECT_CALL(foo_, Bar(_, 0, _))
164  .InSequence(s1);
165  EXPECT_CALL(foo_, Bar2(0, 0))
166  .InSequence(s2);
167  EXPECT_CALL(foo_, Bar2(1, _))
168  .InSequence(s1, s2);
169 
170  foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites
171  foo_.Bar("Hi", 0, 0);
172  foo_.Bar2(0, 0);
173  foo_.Bar2(1, 0);
174 }
175 
176 TEST_F(GMockOutputTest, UnsatisfiedWith) {
177  EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
178 }
179 
180 TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
181  EXPECT_CALL(foo_, Bar(_, _, _));
182  EXPECT_CALL(foo_, Bar2(0, _))
183  .Times(2);
184 
185  foo_.Bar2(0, 1);
186 }
187 
188 TEST_F(GMockOutputTest, MismatchArguments) {
189  const std::string s = "Hi";
190  EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
191 
192  foo_.Bar("Ho", 0, -0.1); // Mismatch arguments
193  foo_.Bar(s, 0, 0);
194 }
195 
196 TEST_F(GMockOutputTest, MismatchWith) {
197  EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
198  .With(Ge());
199 
200  foo_.Bar2(2, 3); // Mismatch With()
201  foo_.Bar2(2, 1);
202 }
203 
204 TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
205  EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
206  .With(Ge());
207 
208  foo_.Bar2(1, 3); // Mismatch arguments and mismatch With()
209  foo_.Bar2(2, 1);
210 }
211 
212 TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
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  foo_.Bar2(1, 0); // Unexpected call, takes default action #2.
220  foo_.Bar2(0, 0); // Unexpected call, takes default action #1.
221  foo_.Bar2(2, 2); // Expected call.
222 }
223 
224 TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
225  ON_CALL(foo_, Bar2(_, _))
226  .WillByDefault(Return(true)); // Default action #1
227  ON_CALL(foo_, Bar2(1, _))
228  .WillByDefault(Return(false)); // Default action #2
229 
230  EXPECT_CALL(foo_, Bar2(2, 2));
231  EXPECT_CALL(foo_, Bar2(1, 1));
232 
233  foo_.Bar2(2, 2); // Expected call.
234  foo_.Bar2(2, 2); // Excessive call, takes default action #1.
235  foo_.Bar2(1, 1); // Expected call.
236  foo_.Bar2(1, 1); // Excessive call, takes default action #2.
237 }
238 
239 TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
240  ON_CALL(foo_, Bar2(_, _))
241  .WillByDefault(Return(true)); // Default action #1
242  ON_CALL(foo_, Bar2(1, _))
243  .WillByDefault(Return(false)); // Default action #2
244 
245  foo_.Bar2(2, 2); // Uninteresting call, takes default action #1.
246  foo_.Bar2(1, 1); // Uninteresting call, takes default action #2.
247 }
248 
249 TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
250  ON_CALL(foo_, Bar2(_, _))
251  .WillByDefault(Return(true)); // Default action #1
252 
253  EXPECT_CALL(foo_, Bar2(_, _))
254  .Times(2)
255  .WillOnce(Return(false));
256  foo_.Bar2(2, 2);
257  foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
258 }
259 
260 TEST_F(GMockOutputTest, CatchesLeakedMocks) {
261  MockFoo* foo1 = new MockFoo;
262  MockFoo* foo2 = new MockFoo;
263 
264  // Invokes ON_CALL on foo1.
265  ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
266 
267  // Invokes EXPECT_CALL on foo2.
268  EXPECT_CALL(*foo2, Bar2(_, _));
269  EXPECT_CALL(*foo2, Bar2(1, _));
270  EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
271  foo2->Bar2(2, 1);
272  foo2->Bar2(1, 1);
273 
274  // Both foo1 and foo2 are deliberately leaked.
275 }
276 
277 MATCHER_P2(IsPair, first, second, "") {
278  return Value(arg.first, first) && Value(arg.second, second);
279 }
280 
281 TEST_F(GMockOutputTest, PrintsMatcher) {
282  const testing::Matcher<int> m1 = Ge(48);
283  EXPECT_THAT((std::pair<int, bool>(42, true)), IsPair(m1, true));
284 }
285 
287  MockFoo* foo = new MockFoo;
288 
289  // Invokes EXPECT_CALL on foo.
290  EXPECT_CALL(*foo, Bar2(_, _));
291  foo->Bar2(2, 1);
292 
293  // foo is deliberately leaked.
294 }
295 
296 int main(int argc, char **argv) {
297  testing::InitGoogleMock(&argc, argv);
298  // Ensures that the tests pass no matter what value of
299  // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
300  testing::GMOCK_FLAG(catch_leaked_mocks) = true;
301  testing::GMOCK_FLAG(verbose) = "warning";
302 
304  return RUN_ALL_TESTS();
305 }
306 
307 #ifdef _MSC_VER
308 # pragma warning(pop)
309 #endif
GTEST_API_ void InitGoogleMock(int *argc, char **argv)
Definition: gmock.cc:191
#define TEST_F(test_fixture, test_name)
Definition: gtest.h:2379
void TestCatchesLeakedMocksInAdHocTests()
#define ON_CALL(obj, call)
MATCHER_P2(IsPair, first, second,"")
#define MOCK_METHOD2(m,...)
int main()
Definition: ad_example.cpp:171
ADVar foo(double d, ADVar x, ADVar y)
#define MOCK_METHOD3(m,...)
#define EXPECT_THAT(value, matcher)
GTEST_API_ Cardinality AnyNumber()
internal::ReturnAction< R > Return(R value)
#define EXPECT_CALL(obj, call)
NaggyMock< MockFoo > foo_
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition: gtest-port.h:693
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2484
#define GMOCK_FLAG(name)
Definition: gmock-port.h:67
const double y