Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: testing/gmock/test/gmock-generated-function-mockers_test.cc

Issue 115846: Retry to checkin a version of gmock, modified to use our boost_tuple in VS2005. (Closed)
Patch Set: Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2007, 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 // Google Mock - a framework for writing C++ mock classes.
33 //
34 // This file tests the function mocker classes.
35
36 #include <gmock/gmock-generated-function-mockers.h>
37
38 #include <map>
39 #include <string>
40 #include <gmock/gmock.h>
41 #include <gtest/gtest.h>
42
43 #if GTEST_OS_WINDOWS
44 // MSDN says the header file to be included for STDMETHOD is BaseTyps.h but
45 // we are getting compiler errors if we use basetyps.h, hence including
46 // objbase.h for definition of STDMETHOD.
47 #include <objbase.h>
48 #endif // GTEST_OS_WINDOWS
49
50 // There is a bug in MSVC (fixed in VS 2008) that prevents creating a
51 // mock for a function with const arguments, so we don't test such
52 // cases for MSVC versions older than 2008.
53 #if !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
54 #define GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
55 #endif // !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
56
57 namespace testing {
58 namespace gmock_generated_function_mockers_test {
59
60 using testing::internal::string;
61 using testing::_;
62 using testing::A;
63 using testing::An;
64 using testing::AnyNumber;
65 using testing::Const;
66 using testing::DoDefault;
67 using testing::Eq;
68 using testing::Lt;
69 using testing::Ref;
70 using testing::Return;
71 using testing::ReturnRef;
72 using testing::TypedEq;
73
74 class FooInterface {
75 public:
76 virtual ~FooInterface() {}
77
78 virtual void VoidReturning(int x) = 0;
79
80 virtual int Nullary() = 0;
81 virtual bool Unary(int x) = 0;
82 virtual long Binary(short x, int y) = 0; // NOLINT
83 virtual int Decimal(bool b, char c, short d, int e, long f, // NOLINT
84 float g, double h, unsigned i, char* j, const string& k)
85 = 0;
86
87 virtual bool TakesNonConstReference(int& n) = 0; // NOLINT
88 virtual string TakesConstReference(const int& n) = 0;
89 #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
90 virtual bool TakesConst(const int x) = 0;
91 #endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
92
93 virtual int OverloadedOnArgumentNumber() = 0;
94 virtual int OverloadedOnArgumentNumber(int n) = 0;
95
96 virtual int OverloadedOnArgumentType(int n) = 0;
97 virtual char OverloadedOnArgumentType(char c) = 0;
98
99 virtual int OverloadedOnConstness() = 0;
100 virtual char OverloadedOnConstness() const = 0;
101
102 virtual int TypeWithHole(int (*func)()) = 0;
103 virtual int TypeWithComma(const std::map<int, string>& a_map) = 0;
104
105 #if GTEST_OS_WINDOWS
106 STDMETHOD_(int, CTNullary)() = 0;
107 STDMETHOD_(bool, CTUnary)(int x) = 0;
108 STDMETHOD_(int, CTDecimal)(bool b, char c, short d, int e, long f, // NOLINT
109 float g, double h, unsigned i, char* j, const string& k) = 0;
110 STDMETHOD_(char, CTConst)(int x) const = 0;
111 #endif // GTEST_OS_WINDOWS
112 };
113
114 class MockFoo : public FooInterface {
115 public:
116 // Makes sure that a mock function parameter can be named.
117 MOCK_METHOD1(VoidReturning, void(int n)); // NOLINT
118
119 MOCK_METHOD0(Nullary, int()); // NOLINT
120
121 // Makes sure that a mock function parameter can be unnamed.
122 MOCK_METHOD1(Unary, bool(int)); // NOLINT
123 MOCK_METHOD2(Binary, long(short, int)); // NOLINT
124 MOCK_METHOD10(Decimal, int(bool, char, short, int, long, float, // NOLINT
125 double, unsigned, char*, const string& str));
126
127 MOCK_METHOD1(TakesNonConstReference, bool(int&)); // NOLINT
128 MOCK_METHOD1(TakesConstReference, string(const int&));
129 #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
130 MOCK_METHOD1(TakesConst, bool(const int)); // NOLINT
131 #endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
132 MOCK_METHOD0(OverloadedOnArgumentNumber, int()); // NOLINT
133 MOCK_METHOD1(OverloadedOnArgumentNumber, int(int)); // NOLINT
134
135 MOCK_METHOD1(OverloadedOnArgumentType, int(int)); // NOLINT
136 MOCK_METHOD1(OverloadedOnArgumentType, char(char)); // NOLINT
137
138 MOCK_METHOD0(OverloadedOnConstness, int()); // NOLINT
139 MOCK_CONST_METHOD0(OverloadedOnConstness, char()); // NOLINT
140
141 MOCK_METHOD1(TypeWithHole, int(int (*)())); // NOLINT
142 MOCK_METHOD1(TypeWithComma, int(const std::map<int, string>&)); // NOLINT
143 #if GTEST_OS_WINDOWS
144 MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTNullary, int());
145 MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTUnary, bool(int));
146 MOCK_METHOD10_WITH_CALLTYPE(STDMETHODCALLTYPE, CTDecimal, int(bool b, char c,
147 short d, int e, long f, float g, double h, unsigned i, char* j,
148 const string& k));
149 MOCK_CONST_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTConst, char(int));
150 #endif // GTEST_OS_WINDOWS
151 };
152
153 class FunctionMockerTest : public testing::Test {
154 protected:
155 FunctionMockerTest() : foo_(&mock_foo_) {}
156
157 FooInterface* const foo_;
158 MockFoo mock_foo_;
159 };
160
161 // Tests mocking a void-returning function.
162 TEST_F(FunctionMockerTest, MocksVoidFunction) {
163 EXPECT_CALL(mock_foo_, VoidReturning(Lt(100)));
164 foo_->VoidReturning(0);
165 }
166
167 // Tests mocking a nullary function.
168 TEST_F(FunctionMockerTest, MocksNullaryFunction) {
169 EXPECT_CALL(mock_foo_, Nullary())
170 .WillOnce(DoDefault())
171 .WillOnce(Return(1));
172
173 EXPECT_EQ(0, foo_->Nullary());
174 EXPECT_EQ(1, foo_->Nullary());
175 }
176
177 // Tests mocking a unary function.
178 TEST_F(FunctionMockerTest, MocksUnaryFunction) {
179 EXPECT_CALL(mock_foo_, Unary(Eq(2)))
180 .Times(2)
181 .WillOnce(Return(true));
182
183 EXPECT_TRUE(foo_->Unary(2));
184 EXPECT_FALSE(foo_->Unary(2));
185 }
186
187 // Tests mocking a binary function.
188 TEST_F(FunctionMockerTest, MocksBinaryFunction) {
189 EXPECT_CALL(mock_foo_, Binary(2, _))
190 .WillOnce(Return(3));
191
192 EXPECT_EQ(3, foo_->Binary(2, 1));
193 }
194
195 // Tests mocking a decimal function.
196 TEST_F(FunctionMockerTest, MocksDecimalFunction) {
197 EXPECT_CALL(mock_foo_, Decimal(true, 'a', 0, 0, 1L, A<float>(),
198 Lt(100), 5U, NULL, "hi"))
199 .WillOnce(Return(5));
200
201 EXPECT_EQ(5, foo_->Decimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
202 }
203
204 // Tests mocking a function that takes a non-const reference.
205 TEST_F(FunctionMockerTest, MocksFunctionWithNonConstReferenceArgument) {
206 int a = 0;
207 EXPECT_CALL(mock_foo_, TakesNonConstReference(Ref(a)))
208 .WillOnce(Return(true));
209
210 EXPECT_TRUE(foo_->TakesNonConstReference(a));
211 }
212
213 // Tests mocking a function that takes a const reference.
214 TEST_F(FunctionMockerTest, MocksFunctionWithConstReferenceArgument) {
215 int a = 0;
216 EXPECT_CALL(mock_foo_, TakesConstReference(Ref(a)))
217 .WillOnce(Return("Hello"));
218
219 EXPECT_EQ("Hello", foo_->TakesConstReference(a));
220 }
221
222 #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
223 // Tests mocking a function that takes a const variable.
224 TEST_F(FunctionMockerTest, MocksFunctionWithConstArgument) {
225 EXPECT_CALL(mock_foo_, TakesConst(Lt(10)))
226 .WillOnce(DoDefault());
227
228 EXPECT_FALSE(foo_->TakesConst(5));
229 }
230 #endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
231
232 // Tests mocking functions overloaded on the number of arguments.
233 TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentNumber) {
234 EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber())
235 .WillOnce(Return(1));
236 EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber(_))
237 .WillOnce(Return(2));
238
239 EXPECT_EQ(2, foo_->OverloadedOnArgumentNumber(1));
240 EXPECT_EQ(1, foo_->OverloadedOnArgumentNumber());
241 }
242
243 // Tests mocking functions overloaded on the types of argument.
244 TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentType) {
245 EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(An<int>()))
246 .WillOnce(Return(1));
247 EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(TypedEq<char>('a')))
248 .WillOnce(Return('b'));
249
250 EXPECT_EQ(1, foo_->OverloadedOnArgumentType(0));
251 EXPECT_EQ('b', foo_->OverloadedOnArgumentType('a'));
252 }
253
254 // Tests mocking functions overloaded on the const-ness of this object.
255 TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnConstnessOfThis) {
256 EXPECT_CALL(mock_foo_, OverloadedOnConstness());
257 EXPECT_CALL(Const(mock_foo_), OverloadedOnConstness())
258 .WillOnce(Return('a'));
259
260 EXPECT_EQ(0, foo_->OverloadedOnConstness());
261 EXPECT_EQ('a', Const(*foo_).OverloadedOnConstness());
262 }
263
264 #if GTEST_OS_WINDOWS
265 // Tests mocking a nullary function with calltype.
266 TEST_F(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
267 EXPECT_CALL(mock_foo_, CTNullary())
268 .WillOnce(Return(-1))
269 .WillOnce(Return(0));
270
271 EXPECT_EQ(-1, foo_->CTNullary());
272 EXPECT_EQ(0, foo_->CTNullary());
273 }
274
275 // Tests mocking a unary function with calltype.
276 TEST_F(FunctionMockerTest, MocksUnaryFunctionWithCallType) {
277 EXPECT_CALL(mock_foo_, CTUnary(Eq(2)))
278 .Times(2)
279 .WillOnce(Return(true))
280 .WillOnce(Return(false));
281
282 EXPECT_TRUE(foo_->CTUnary(2));
283 EXPECT_FALSE(foo_->CTUnary(2));
284 }
285
286 // Tests mocking a decimal function with calltype.
287 TEST_F(FunctionMockerTest, MocksDecimalFunctionWithCallType) {
288 EXPECT_CALL(mock_foo_, CTDecimal(true, 'a', 0, 0, 1L, A<float>(),
289 Lt(100), 5U, NULL, "hi"))
290 .WillOnce(Return(10));
291
292 EXPECT_EQ(10, foo_->CTDecimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
293 }
294
295 // Tests mocking functions overloaded on the const-ness of this object.
296 TEST_F(FunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
297 EXPECT_CALL(Const(mock_foo_), CTConst(_))
298 .WillOnce(Return('a'));
299
300 EXPECT_EQ('a', Const(*foo_).CTConst(0));
301 }
302
303 #endif // GTEST_OS_WINDOWS
304
305 class MockB {
306 public:
307 MOCK_METHOD0(DoB, void());
308 };
309
310 // Tests that functions with no EXPECT_CALL() ruls can be called any
311 // number of times.
312 TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
313 {
314 MockB b;
315 }
316
317 {
318 MockB b;
319 b.DoB();
320 }
321
322 {
323 MockB b;
324 b.DoB();
325 b.DoB();
326 }
327 }
328
329 // Tests mocking template interfaces.
330
331 template <typename T>
332 class StackInterface {
333 public:
334 virtual ~StackInterface() {}
335
336 // Template parameter appears in function parameter.
337 virtual void Push(const T& value) = 0;
338 virtual void Pop() = 0;
339 virtual int GetSize() const = 0;
340 // Template parameter appears in function return type.
341 virtual const T& GetTop() const = 0;
342 };
343
344 template <typename T>
345 class MockStack : public StackInterface<T> {
346 public:
347 MOCK_METHOD1_T(Push, void(const T& elem));
348 MOCK_METHOD0_T(Pop, void());
349 MOCK_CONST_METHOD0_T(GetSize, int()); // NOLINT
350 MOCK_CONST_METHOD0_T(GetTop, const T&());
351 };
352
353 // Tests that template mock works.
354 TEST(TemplateMockTest, Works) {
355 MockStack<int> mock;
356
357 EXPECT_CALL(mock, GetSize())
358 .WillOnce(Return(0))
359 .WillOnce(Return(1))
360 .WillOnce(Return(0));
361 EXPECT_CALL(mock, Push(_));
362 int n = 5;
363 EXPECT_CALL(mock, GetTop())
364 .WillOnce(ReturnRef(n));
365 EXPECT_CALL(mock, Pop())
366 .Times(AnyNumber());
367
368 EXPECT_EQ(0, mock.GetSize());
369 mock.Push(5);
370 EXPECT_EQ(1, mock.GetSize());
371 EXPECT_EQ(5, mock.GetTop());
372 mock.Pop();
373 EXPECT_EQ(0, mock.GetSize());
374 }
375
376 #if GTEST_OS_WINDOWS
377 // Tests mocking template interfaces with calltype.
378
379 template <typename T>
380 class StackInterfaceWithCallType {
381 public:
382 virtual ~StackInterfaceWithCallType() {}
383
384 // Template parameter appears in function parameter.
385 STDMETHOD_(void, Push)(const T& value) = 0;
386 STDMETHOD_(void, Pop)() = 0;
387 STDMETHOD_(int, GetSize)() const = 0;
388 // Template parameter appears in function return type.
389 STDMETHOD_(const T&, GetTop)() const = 0;
390 };
391
392 template <typename T>
393 class MockStackWithCallType : public StackInterfaceWithCallType<T> {
394 public:
395 MOCK_METHOD1_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Push, void(const T& elem));
396 MOCK_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Pop, void());
397 MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetSize, int());
398 MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetTop, const T&());
399 };
400
401 // Tests that template mock with calltype works.
402 TEST(TemplateMockTestWithCallType, Works) {
403 MockStackWithCallType<int> mock;
404
405 EXPECT_CALL(mock, GetSize())
406 .WillOnce(Return(0))
407 .WillOnce(Return(1))
408 .WillOnce(Return(0));
409 EXPECT_CALL(mock, Push(_));
410 int n = 5;
411 EXPECT_CALL(mock, GetTop())
412 .WillOnce(ReturnRef(n));
413 EXPECT_CALL(mock, Pop())
414 .Times(AnyNumber());
415
416 EXPECT_EQ(0, mock.GetSize());
417 mock.Push(5);
418 EXPECT_EQ(1, mock.GetSize());
419 EXPECT_EQ(5, mock.GetTop());
420 mock.Pop();
421 EXPECT_EQ(0, mock.GetSize());
422 }
423 #endif // GTEST_OS_WINDOWS
424
425 #define MY_MOCK_METHODS1_ \
426 MOCK_METHOD0(Overloaded, void()); \
427 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
428 MOCK_METHOD2(Overloaded, bool(bool f, int n))
429
430 class MockOverloadedOnArgNumber {
431 public:
432 MY_MOCK_METHODS1_;
433 };
434
435 TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
436 MockOverloadedOnArgNumber mock;
437 EXPECT_CALL(mock, Overloaded());
438 EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
439 EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true));
440
441 mock.Overloaded();
442 EXPECT_EQ(2, mock.Overloaded(1));
443 EXPECT_TRUE(mock.Overloaded(true, 1));
444 }
445
446 #define MY_MOCK_METHODS2_ \
447 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
448 MOCK_METHOD1(Overloaded, int(int n));
449
450 class MockOverloadedOnConstness {
451 public:
452 MY_MOCK_METHODS2_;
453 };
454
455 TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
456 MockOverloadedOnConstness mock;
457 const MockOverloadedOnConstness* const_mock = &mock;
458 EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
459 EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3));
460
461 EXPECT_EQ(2, mock.Overloaded(1));
462 EXPECT_EQ(3, const_mock->Overloaded(1));
463 }
464
465 } // namespace gmock_generated_function_mockers_test
466 } // namespace testing
OLDNEW
« no previous file with comments | « testing/gmock/test/gmock-generated-actions_test.cc ('k') | testing/gmock/test/gmock-generated-internal-utils_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698