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

Side by Side Diff: testing/gmock/test/gmock-actions_test.cc

Issue 113807: Checkin a version of gmock, modified to use our boost_tuple in VS2005. (Closed)
Patch Set: Fix grammar issue. Created 11 years, 7 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
« no previous file with comments | « testing/gmock/src/gmock_main.cc ('k') | testing/gmock/test/gmock-cardinalities_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 built-in actions.
35
36 #include <gmock/gmock-actions.h>
37 #include <algorithm>
38 #include <iterator>
39 #include <string>
40 #include <gmock/gmock.h>
41 #include <gmock/internal/gmock-port.h>
42 #include <gtest/gtest.h>
43 #include <gtest/gtest-spi.h>
44
45 namespace {
46
47 using ::std::tr1::get;
48 using ::std::tr1::make_tuple;
49 using ::std::tr1::tuple;
50 using ::std::tr1::tuple_element;
51 using testing::internal::BuiltInDefaultValue;
52 using testing::internal::Int64;
53 using testing::internal::UInt64;
54 // This list should be kept sorted.
55 using testing::_;
56 using testing::Action;
57 using testing::ActionInterface;
58 using testing::Assign;
59 using testing::DefaultValue;
60 using testing::DoDefault;
61 using testing::IgnoreResult;
62 using testing::Invoke;
63 using testing::InvokeWithoutArgs;
64 using testing::MakePolymorphicAction;
65 using testing::Ne;
66 using testing::PolymorphicAction;
67 using testing::Return;
68 using testing::ReturnNull;
69 using testing::ReturnRef;
70 using testing::SetArgumentPointee;
71 using testing::SetArrayArgument;
72
73 #ifndef _WIN32_WCE
74 using testing::SetErrnoAndReturn;
75 #endif // _WIN32_WCE
76
77 #if GMOCK_HAS_PROTOBUF_
78 using testing::internal::TestMessage;
79 #endif // GMOCK_HAS_PROTOBUF_
80
81 // Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
82 TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
83 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL);
84 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL);
85 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL);
86 }
87
88 // Tests that BuiltInDefaultValue<T*>::Exists() return true.
89 TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
90 EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
91 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
92 EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
93 }
94
95 // Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
96 // built-in numeric type.
97 TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
98 EXPECT_EQ(0, BuiltInDefaultValue<unsigned char>::Get());
99 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
100 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
101 #if !GTEST_OS_WINDOWS
102 EXPECT_EQ(0, BuiltInDefaultValue<unsigned wchar_t>::Get());
103 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
104 #endif // !GTEST_OS_WINDOWS
105 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
106 EXPECT_EQ(0, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT
107 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT
108 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT
109 EXPECT_EQ(0, BuiltInDefaultValue<unsigned int>::Get());
110 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
111 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
112 EXPECT_EQ(0, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT
113 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT
114 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT
115 EXPECT_EQ(0, BuiltInDefaultValue<UInt64>::Get());
116 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
117 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
118 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
119 }
120
121 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
122 // built-in numeric type.
123 TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
124 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
125 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
126 EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
127 #if !GTEST_OS_WINDOWS
128 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
129 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
130 #endif // !GTEST_OS_WINDOWS
131 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
132 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT
133 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT
134 EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT
135 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
136 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
137 EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
138 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT
139 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT
140 EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT
141 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
142 EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists());
143 EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
144 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
145 }
146
147 // Tests that BuiltInDefaultValue<bool>::Get() returns false.
148 TEST(BuiltInDefaultValueTest, IsFalseForBool) {
149 EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
150 }
151
152 // Tests that BuiltInDefaultValue<bool>::Exists() returns true.
153 TEST(BuiltInDefaultValueTest, BoolExists) {
154 EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
155 }
156
157 // Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
158 // string type.
159 TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
160 #if GTEST_HAS_GLOBAL_STRING
161 EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get());
162 #endif // GTEST_HAS_GLOBAL_STRING
163
164 #if GTEST_HAS_STD_STRING
165 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
166 #endif // GTEST_HAS_STD_STRING
167 }
168
169 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
170 // string type.
171 TEST(BuiltInDefaultValueTest, ExistsForString) {
172 #if GTEST_HAS_GLOBAL_STRING
173 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
174 #endif // GTEST_HAS_GLOBAL_STRING
175
176 #if GTEST_HAS_STD_STRING
177 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
178 #endif // GTEST_HAS_STD_STRING
179 }
180
181 // Tests that BuiltInDefaultValue<const T>::Get() returns the same
182 // value as BuiltInDefaultValue<T>::Get() does.
183 TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
184 EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
185 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
186 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL);
187 EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
188 }
189
190 // Tests that BuiltInDefaultValue<T>::Get() aborts the program with
191 // the correct error message when T is a user-defined type.
192 struct UserType {
193 UserType() : value(0) {}
194
195 int value;
196 };
197
198 TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) {
199 EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists());
200 }
201
202 #if GTEST_HAS_DEATH_TEST
203
204 // Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
205 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
206 EXPECT_DEATH({ // NOLINT
207 BuiltInDefaultValue<int&>::Get();
208 }, "");
209 EXPECT_DEATH({ // NOLINT
210 BuiltInDefaultValue<const char&>::Get();
211 }, "");
212 }
213
214 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) {
215 EXPECT_DEATH({ // NOLINT
216 BuiltInDefaultValue<UserType>::Get();
217 }, "");
218 }
219
220 #endif // GTEST_HAS_DEATH_TEST
221
222 // Tests that DefaultValue<T>::IsSet() is false initially.
223 TEST(DefaultValueTest, IsInitiallyUnset) {
224 EXPECT_FALSE(DefaultValue<int>::IsSet());
225 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
226 }
227
228 // Tests that DefaultValue<T> can be set and then unset.
229 TEST(DefaultValueTest, CanBeSetAndUnset) {
230 EXPECT_TRUE(DefaultValue<int>::Exists());
231 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
232
233 DefaultValue<int>::Set(1);
234 DefaultValue<const UserType>::Set(UserType());
235
236 EXPECT_EQ(1, DefaultValue<int>::Get());
237 EXPECT_EQ(0, DefaultValue<const UserType>::Get().value);
238
239 EXPECT_TRUE(DefaultValue<int>::Exists());
240 EXPECT_TRUE(DefaultValue<const UserType>::Exists());
241
242 DefaultValue<int>::Clear();
243 DefaultValue<const UserType>::Clear();
244
245 EXPECT_FALSE(DefaultValue<int>::IsSet());
246 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
247
248 EXPECT_TRUE(DefaultValue<int>::Exists());
249 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
250 }
251
252 // Tests that DefaultValue<T>::Get() returns the
253 // BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
254 // false.
255 TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
256 EXPECT_FALSE(DefaultValue<int>::IsSet());
257 EXPECT_TRUE(DefaultValue<int>::Exists());
258 EXPECT_FALSE(DefaultValue<UserType>::IsSet());
259 EXPECT_FALSE(DefaultValue<UserType>::Exists());
260
261 EXPECT_EQ(0, DefaultValue<int>::Get());
262
263 #if GTEST_HAS_DEATH_TEST
264 EXPECT_DEATH({ // NOLINT
265 DefaultValue<UserType>::Get();
266 }, "");
267 #endif // GTEST_HAS_DEATH_TEST
268 }
269
270 // Tests that DefaultValue<void>::Get() returns void.
271 TEST(DefaultValueTest, GetWorksForVoid) {
272 return DefaultValue<void>::Get();
273 }
274
275 // Tests using DefaultValue with a reference type.
276
277 // Tests that DefaultValue<T&>::IsSet() is false initially.
278 TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
279 EXPECT_FALSE(DefaultValue<int&>::IsSet());
280 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
281 }
282
283 // Tests that DefaultValue<T&>::Exists is false initiallly.
284 TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
285 EXPECT_FALSE(DefaultValue<int&>::Exists());
286 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
287 }
288
289 // Tests that DefaultValue<T&> can be set and then unset.
290 TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
291 int n = 1;
292 DefaultValue<const int&>::Set(n);
293 UserType u;
294 DefaultValue<UserType&>::Set(u);
295
296 EXPECT_TRUE(DefaultValue<const int&>::Exists());
297 EXPECT_TRUE(DefaultValue<UserType&>::Exists());
298
299 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
300 EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get()));
301
302 DefaultValue<const int&>::Clear();
303 DefaultValue<UserType&>::Clear();
304
305 EXPECT_FALSE(DefaultValue<const int&>::Exists());
306 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
307
308 EXPECT_FALSE(DefaultValue<const int&>::IsSet());
309 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
310 }
311
312 // Tests that DefaultValue<T&>::Get() returns the
313 // BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
314 // false.
315 TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
316 EXPECT_FALSE(DefaultValue<int&>::IsSet());
317 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
318
319 #if GTEST_HAS_DEATH_TEST
320 EXPECT_DEATH({ // NOLINT
321 DefaultValue<int&>::Get();
322 }, "");
323 EXPECT_DEATH({ // NOLINT
324 DefaultValue<UserType>::Get();
325 }, "");
326 #endif // GTEST_HAS_DEATH_TEST
327 }
328
329 // Tests that ActionInterface can be implemented by defining the
330 // Perform method.
331
332 typedef int MyFunction(bool, int);
333
334 class MyActionImpl : public ActionInterface<MyFunction> {
335 public:
336 virtual int Perform(const tuple<bool, int>& args) {
337 return get<0>(args) ? get<1>(args) : 0;
338 }
339 };
340
341 TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
342 MyActionImpl my_action_impl;
343
344 EXPECT_FALSE(my_action_impl.IsDoDefault());
345 }
346
347 TEST(ActionInterfaceTest, MakeAction) {
348 Action<MyFunction> action = MakeAction(new MyActionImpl);
349
350 // When exercising the Perform() method of Action<F>, we must pass
351 // it a tuple whose size and type are compatible with F's argument
352 // types. For example, if F is int(), then Perform() takes a
353 // 0-tuple; if F is void(bool, int), then Perform() takes a
354 // tuple<bool, int>, and so on.
355 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
356 }
357
358 // Tests that Action<F> can be contructed from a pointer to
359 // ActionInterface<F>.
360 TEST(ActionTest, CanBeConstructedFromActionInterface) {
361 Action<MyFunction> action(new MyActionImpl);
362 }
363
364 // Tests that Action<F> delegates actual work to ActionInterface<F>.
365 TEST(ActionTest, DelegatesWorkToActionInterface) {
366 const Action<MyFunction> action(new MyActionImpl);
367
368 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
369 EXPECT_EQ(0, action.Perform(make_tuple(false, 1)));
370 }
371
372 // Tests that Action<F> can be copied.
373 TEST(ActionTest, IsCopyable) {
374 Action<MyFunction> a1(new MyActionImpl);
375 Action<MyFunction> a2(a1); // Tests the copy constructor.
376
377 // a1 should continue to work after being copied from.
378 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
379 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
380
381 // a2 should work like the action it was copied from.
382 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
383 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
384
385 a2 = a1; // Tests the assignment operator.
386
387 // a1 should continue to work after being copied from.
388 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
389 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
390
391 // a2 should work like the action it was copied from.
392 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
393 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
394 }
395
396 // Tests that an Action<From> object can be converted to a
397 // compatible Action<To> object.
398
399 class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
400 public:
401 virtual bool Perform(const tuple<int>& arg) {
402 return get<0>(arg) != 0;
403 }
404 };
405
406 TEST(ActionTest, CanBeConvertedToOtherActionType) {
407 const Action<bool(int)> a1(new IsNotZero); // NOLINT
408 const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT
409 EXPECT_EQ(1, a2.Perform(make_tuple('a')));
410 EXPECT_EQ(0, a2.Perform(make_tuple('\0')));
411 }
412
413 // The following two classes are for testing MakePolymorphicAction().
414
415 // Implements a polymorphic action that returns the second of the
416 // arguments it receives.
417 class ReturnSecondArgumentAction {
418 public:
419 // We want to verify that MakePolymorphicAction() can work with a
420 // polymorphic action whose Perform() method template is either
421 // const or not. This lets us verify the non-const case.
422 template <typename Result, typename ArgumentTuple>
423 Result Perform(const ArgumentTuple& args) { return get<1>(args); }
424 };
425
426 // Implements a polymorphic action that can be used in a nullary
427 // function to return 0.
428 class ReturnZeroFromNullaryFunctionAction {
429 public:
430 // For testing that MakePolymorphicAction() works when the
431 // implementation class' Perform() method template takes only one
432 // template parameter.
433 //
434 // We want to verify that MakePolymorphicAction() can work with a
435 // polymorphic action whose Perform() method template is either
436 // const or not. This lets us verify the const case.
437 template <typename Result>
438 Result Perform(const tuple<>&) const { return 0; }
439 };
440
441 // These functions verify that MakePolymorphicAction() returns a
442 // PolymorphicAction<T> where T is the argument's type.
443
444 PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
445 return MakePolymorphicAction(ReturnSecondArgumentAction());
446 }
447
448 PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
449 ReturnZeroFromNullaryFunction() {
450 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
451 }
452
453 // Tests that MakePolymorphicAction() turns a polymorphic action
454 // implementation class into a polymorphic action.
455 TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
456 Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT
457 EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0)));
458 }
459
460 // Tests that MakePolymorphicAction() works when the implementation
461 // class' Perform() method template has only one template parameter.
462 TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
463 Action<int()> a1 = ReturnZeroFromNullaryFunction();
464 EXPECT_EQ(0, a1.Perform(make_tuple()));
465
466 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
467 EXPECT_TRUE(a2.Perform(make_tuple()) == NULL);
468 }
469
470 // Tests that Return() works as an action for void-returning
471 // functions.
472 TEST(ReturnTest, WorksForVoid) {
473 const Action<void(int)> ret = Return(); // NOLINT
474 return ret.Perform(make_tuple(1));
475 }
476
477 // Tests that Return(v) returns v.
478 TEST(ReturnTest, ReturnsGivenValue) {
479 Action<int()> ret = Return(1); // NOLINT
480 EXPECT_EQ(1, ret.Perform(make_tuple()));
481
482 ret = Return(-5);
483 EXPECT_EQ(-5, ret.Perform(make_tuple()));
484 }
485
486 // Tests that Return("string literal") works.
487 TEST(ReturnTest, AcceptsStringLiteral) {
488 Action<const char*()> a1 = Return("Hello");
489 EXPECT_STREQ("Hello", a1.Perform(make_tuple()));
490
491 Action<std::string()> a2 = Return("world");
492 EXPECT_EQ("world", a2.Perform(make_tuple()));
493 }
494
495 // Tests that Return(v) is covaraint.
496
497 struct Base {
498 bool operator==(const Base&) { return true; }
499 };
500
501 struct Derived : public Base {
502 bool operator==(const Derived&) { return true; }
503 };
504
505 TEST(ReturnTest, IsCovariant) {
506 Base base;
507 Derived derived;
508 Action<Base*()> ret = Return(&base);
509 EXPECT_EQ(&base, ret.Perform(make_tuple()));
510
511 ret = Return(&derived);
512 EXPECT_EQ(&derived, ret.Perform(make_tuple()));
513 }
514
515 // Tests that ReturnNull() returns NULL in a pointer-returning function.
516 TEST(ReturnNullTest, WorksInPointerReturningFunction) {
517 const Action<int*()> a1 = ReturnNull();
518 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
519
520 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
521 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
522 }
523
524 // Tests that ReturnRef(v) works for reference types.
525 TEST(ReturnRefTest, WorksForReference) {
526 const int n = 0;
527 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
528
529 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
530 }
531
532 // Tests that ReturnRef(v) is covariant.
533 TEST(ReturnRefTest, IsCovariant) {
534 Base base;
535 Derived derived;
536 Action<Base&()> a = ReturnRef(base);
537 EXPECT_EQ(&base, &a.Perform(make_tuple()));
538
539 a = ReturnRef(derived);
540 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
541 }
542
543 // Tests that DoDefault() does the default action for the mock method.
544
545 class MyClass {};
546
547 class MockClass {
548 public:
549 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
550 MOCK_METHOD0(Foo, MyClass());
551 };
552
553 // Tests that DoDefault() returns the built-in default value for the
554 // return type by default.
555 TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
556 MockClass mock;
557 EXPECT_CALL(mock, IntFunc(_))
558 .WillOnce(DoDefault());
559 EXPECT_EQ(0, mock.IntFunc(true));
560 }
561
562 #if GTEST_HAS_DEATH_TEST
563
564 // Tests that DoDefault() aborts the process when there is no built-in
565 // default value for the return type.
566 TEST(DoDefaultDeathTest, DiesForUnknowType) {
567 MockClass mock;
568 EXPECT_CALL(mock, Foo())
569 .WillRepeatedly(DoDefault());
570 EXPECT_DEATH({ // NOLINT
571 mock.Foo();
572 }, "");
573 }
574
575 // Tests that using DoDefault() inside a composite action leads to a
576 // run-time error.
577
578 void VoidFunc(bool flag) {}
579
580 TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
581 MockClass mock;
582 EXPECT_CALL(mock, IntFunc(_))
583 .WillRepeatedly(DoAll(Invoke(VoidFunc),
584 DoDefault()));
585
586 // Ideally we should verify the error message as well. Sadly,
587 // EXPECT_DEATH() can only capture stderr, while Google Mock's
588 // errors are printed on stdout. Therefore we have to settle for
589 // not verifying the message.
590 EXPECT_DEATH({ // NOLINT
591 mock.IntFunc(true);
592 }, "");
593 }
594
595 #endif // GTEST_HAS_DEATH_TEST
596
597 // Tests that DoDefault() returns the default value set by
598 // DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
599 TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
600 DefaultValue<int>::Set(1);
601 MockClass mock;
602 EXPECT_CALL(mock, IntFunc(_))
603 .WillOnce(DoDefault());
604 EXPECT_EQ(1, mock.IntFunc(false));
605 DefaultValue<int>::Clear();
606 }
607
608 // Tests that DoDefault() does the action specified by ON_CALL().
609 TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
610 MockClass mock;
611 ON_CALL(mock, IntFunc(_))
612 .WillByDefault(Return(2));
613 EXPECT_CALL(mock, IntFunc(_))
614 .WillOnce(DoDefault());
615 EXPECT_EQ(2, mock.IntFunc(false));
616 }
617
618 // Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
619 TEST(DoDefaultTest, CannotBeUsedInOnCall) {
620 MockClass mock;
621 EXPECT_NONFATAL_FAILURE({ // NOLINT
622 ON_CALL(mock, IntFunc(_))
623 .WillByDefault(DoDefault());
624 }, "DoDefault() cannot be used in ON_CALL()");
625 }
626
627 // Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
628 // the N-th (0-based) argument to v.
629 TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
630 typedef void MyFunction(bool, int*, char*);
631 Action<MyFunction> a = SetArgumentPointee<1>(2);
632
633 int n = 0;
634 char ch = '\0';
635 a.Perform(make_tuple(true, &n, &ch));
636 EXPECT_EQ(2, n);
637 EXPECT_EQ('\0', ch);
638
639 a = SetArgumentPointee<2>('a');
640 n = 0;
641 ch = '\0';
642 a.Perform(make_tuple(true, &n, &ch));
643 EXPECT_EQ(0, n);
644 EXPECT_EQ('a', ch);
645 }
646
647 #if GMOCK_HAS_PROTOBUF_
648
649 // Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
650 // variable pointed to by the N-th (0-based) argument to proto_buffer.
651 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
652 TestMessage* const msg = new TestMessage;
653 msg->set_member("yes");
654 TestMessage orig_msg;
655 orig_msg.CopyFrom(*msg);
656
657 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
658 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
659 // s.t. the action works even when the original proto_buffer has
660 // died. We ensure this behavior by deleting msg before using the
661 // action.
662 delete msg;
663
664 TestMessage dest;
665 EXPECT_FALSE(orig_msg.Equals(dest));
666 a.Perform(make_tuple(true, &dest));
667 EXPECT_TRUE(orig_msg.Equals(dest));
668 }
669
670 // Tests that SetArgumentPointee<N>(proto_buffer) sets the
671 // ::ProtocolMessage variable pointed to by the N-th (0-based)
672 // argument to proto_buffer.
673 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
674 TestMessage* const msg = new TestMessage;
675 msg->set_member("yes");
676 TestMessage orig_msg;
677 orig_msg.CopyFrom(*msg);
678
679 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
680 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
681 // s.t. the action works even when the original proto_buffer has
682 // died. We ensure this behavior by deleting msg before using the
683 // action.
684 delete msg;
685
686 TestMessage dest;
687 ::ProtocolMessage* const dest_base = &dest;
688 EXPECT_FALSE(orig_msg.Equals(dest));
689 a.Perform(make_tuple(true, dest_base));
690 EXPECT_TRUE(orig_msg.Equals(dest));
691 }
692
693 // Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
694 // protobuf variable pointed to by the N-th (0-based) argument to
695 // proto2_buffer.
696 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
697 using testing::internal::FooMessage;
698 FooMessage* const msg = new FooMessage;
699 msg->set_int_field(2);
700 msg->set_string_field("hi");
701 FooMessage orig_msg;
702 orig_msg.CopyFrom(*msg);
703
704 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
705 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
706 // proto2_buffer s.t. the action works even when the original
707 // proto2_buffer has died. We ensure this behavior by deleting msg
708 // before using the action.
709 delete msg;
710
711 FooMessage dest;
712 dest.set_int_field(0);
713 a.Perform(make_tuple(true, &dest));
714 EXPECT_EQ(2, dest.int_field());
715 EXPECT_EQ("hi", dest.string_field());
716 }
717
718 // Tests that SetArgumentPointee<N>(proto2_buffer) sets the
719 // proto2::Message variable pointed to by the N-th (0-based) argument
720 // to proto2_buffer.
721 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
722 using testing::internal::FooMessage;
723 FooMessage* const msg = new FooMessage;
724 msg->set_int_field(2);
725 msg->set_string_field("hi");
726 FooMessage orig_msg;
727 orig_msg.CopyFrom(*msg);
728
729 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
730 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
731 // proto2_buffer s.t. the action works even when the original
732 // proto2_buffer has died. We ensure this behavior by deleting msg
733 // before using the action.
734 delete msg;
735
736 FooMessage dest;
737 dest.set_int_field(0);
738 ::proto2::Message* const dest_base = &dest;
739 a.Perform(make_tuple(true, dest_base));
740 EXPECT_EQ(2, dest.int_field());
741 EXPECT_EQ("hi", dest.string_field());
742 }
743
744 #endif // GMOCK_HAS_PROTOBUF_
745
746 // Tests that SetArrayArgument<N>(first, last) sets the elements of the array
747 // pointed to by the N-th (0-based) argument to values in range [first, last).
748 TEST(SetArrayArgumentTest, SetsTheNthArray) {
749 typedef void MyFunction(bool, int*, char*);
750 int numbers[] = { 1, 2, 3 };
751 Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3);
752
753 int n[4] = {};
754 int* pn = n;
755 char ch[4] = {};
756 char* pch = ch;
757 a.Perform(make_tuple(true, pn, pch));
758 EXPECT_EQ(1, n[0]);
759 EXPECT_EQ(2, n[1]);
760 EXPECT_EQ(3, n[2]);
761 EXPECT_EQ(0, n[3]);
762 EXPECT_EQ('\0', ch[0]);
763 EXPECT_EQ('\0', ch[1]);
764 EXPECT_EQ('\0', ch[2]);
765 EXPECT_EQ('\0', ch[3]);
766
767 // Tests first and last are iterators.
768 std::string letters = "abc";
769 a = SetArrayArgument<2>(letters.begin(), letters.end());
770 std::fill_n(n, 4, 0);
771 std::fill_n(ch, 4, '\0');
772 a.Perform(make_tuple(true, pn, pch));
773 EXPECT_EQ(0, n[0]);
774 EXPECT_EQ(0, n[1]);
775 EXPECT_EQ(0, n[2]);
776 EXPECT_EQ(0, n[3]);
777 EXPECT_EQ('a', ch[0]);
778 EXPECT_EQ('b', ch[1]);
779 EXPECT_EQ('c', ch[2]);
780 EXPECT_EQ('\0', ch[3]);
781 }
782
783 // Tests SetArrayArgument<N>(first, last) where first == last.
784 TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) {
785 typedef void MyFunction(bool, int*);
786 int numbers[] = { 1, 2, 3 };
787 Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers);
788
789 int n[4] = {};
790 int* pn = n;
791 a.Perform(make_tuple(true, pn));
792 EXPECT_EQ(0, n[0]);
793 EXPECT_EQ(0, n[1]);
794 EXPECT_EQ(0, n[2]);
795 EXPECT_EQ(0, n[3]);
796 }
797
798 // Tests SetArrayArgument<N>(first, last) where *first is convertible
799 // (but not equal) to the argument type.
800 TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) {
801 typedef void MyFunction(bool, char*);
802 int codes[] = { 97, 98, 99 };
803 Action<MyFunction> a = SetArrayArgument<1>(codes, codes + 3);
804
805 char ch[4] = {};
806 char* pch = ch;
807 a.Perform(make_tuple(true, pch));
808 EXPECT_EQ('a', ch[0]);
809 EXPECT_EQ('b', ch[1]);
810 EXPECT_EQ('c', ch[2]);
811 EXPECT_EQ('\0', ch[3]);
812 }
813
814 // Test SetArrayArgument<N>(first, last) with iterator as argument.
815 TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) {
816 typedef void MyFunction(bool, std::back_insert_iterator<std::string>);
817 std::string letters = "abc";
818 Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end());
819
820 std::string s;
821 a.Perform(make_tuple(true, back_inserter(s)));
822 EXPECT_EQ(letters, s);
823 }
824
825 // Sample functions and functors for testing Invoke() and etc.
826 int Nullary() { return 1; }
827
828 class NullaryFunctor {
829 public:
830 int operator()() { return 2; }
831 };
832
833 bool g_done = false;
834 void VoidNullary() { g_done = true; }
835
836 class VoidNullaryFunctor {
837 public:
838 void operator()() { g_done = true; }
839 };
840
841 bool Unary(int x) { return x < 0; }
842
843 const char* Plus1(const char* s) { return s + 1; }
844
845 void VoidUnary(int n) { g_done = true; }
846
847 bool ByConstRef(const std::string& s) { return s == "Hi"; }
848
849 const double g_double = 0;
850 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
851
852 std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT
853
854 struct UnaryFunctor {
855 int operator()(bool x) { return x ? 1 : -1; }
856 };
857
858 const char* Binary(const char* input, short n) { return input + n; } // NOLINT
859
860 void VoidBinary(int, char) { g_done = true; }
861
862 int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
863
864 void VoidTernary(int, char, bool) { g_done = true; }
865
866 int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
867
868 void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
869
870 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
871
872 struct SumOf5Functor {
873 int operator()(int a, int b, int c, int d, int e) {
874 return a + b + c + d + e;
875 }
876 };
877
878 int SumOf6(int a, int b, int c, int d, int e, int f) {
879 return a + b + c + d + e + f;
880 }
881
882 struct SumOf6Functor {
883 int operator()(int a, int b, int c, int d, int e, int f) {
884 return a + b + c + d + e + f;
885 }
886 };
887
888 class Foo {
889 public:
890 Foo() : value_(123) {}
891
892 int Nullary() const { return value_; }
893 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
894 std::string Binary(const std::string& str, char c) const { return str + c; }
895 int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
896 int SumOf4(int a, int b, int c, int d) const {
897 return a + b + c + d + value_;
898 }
899 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
900 int SumOf6(int a, int b, int c, int d, int e, int f) {
901 return a + b + c + d + e + f;
902 }
903 private:
904 int value_;
905 };
906
907 // Tests InvokeWithoutArgs(function).
908 TEST(InvokeWithoutArgsTest, Function) {
909 // As an action that takes one argument.
910 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
911 EXPECT_EQ(1, a.Perform(make_tuple(2)));
912
913 // As an action that takes two arguments.
914 Action<short(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
915 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
916
917 // As an action that returns void.
918 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
919 g_done = false;
920 a3.Perform(make_tuple(1));
921 EXPECT_TRUE(g_done);
922 }
923
924 // Tests InvokeWithoutArgs(functor).
925 TEST(InvokeWithoutArgsTest, Functor) {
926 // As an action that takes no argument.
927 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
928 EXPECT_EQ(2, a.Perform(make_tuple()));
929
930 // As an action that takes three arguments.
931 Action<short(int, double, char)> a2 = // NOLINT
932 InvokeWithoutArgs(NullaryFunctor());
933 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
934
935 // As an action that returns void.
936 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
937 g_done = false;
938 a3.Perform(make_tuple());
939 EXPECT_TRUE(g_done);
940 }
941
942 // Tests InvokeWithoutArgs(obj_ptr, method).
943 TEST(InvokeWithoutArgsTest, Method) {
944 Foo foo;
945 Action<int(bool, char)> a = // NOLINT
946 InvokeWithoutArgs(&foo, &Foo::Nullary);
947 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
948 }
949
950 // Tests using IgnoreResult() on a polymorphic action.
951 TEST(IgnoreResultTest, PolymorphicAction) {
952 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
953 a.Perform(make_tuple(1));
954 }
955
956 // Tests using IgnoreResult() on a monomorphic action.
957
958 int ReturnOne() {
959 g_done = true;
960 return 1;
961 }
962
963 TEST(IgnoreResultTest, MonomorphicAction) {
964 g_done = false;
965 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
966 a.Perform(make_tuple());
967 EXPECT_TRUE(g_done);
968 }
969
970 // Tests using IgnoreResult() on an action that returns a class type.
971
972 MyClass ReturnMyClass(double x) {
973 g_done = true;
974 return MyClass();
975 }
976
977 TEST(IgnoreResultTest, ActionReturningClass) {
978 g_done = false;
979 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
980 a.Perform(make_tuple(2));
981 EXPECT_TRUE(g_done);
982 }
983
984 TEST(AssignTest, Int) {
985 int x = 0;
986 Action<void(int)> a = Assign(&x, 5);
987 a.Perform(make_tuple(0));
988 EXPECT_EQ(5, x);
989 }
990
991 TEST(AssignTest, String) {
992 ::std::string x;
993 Action<void(void)> a = Assign(&x, "Hello, world");
994 a.Perform(make_tuple());
995 EXPECT_EQ("Hello, world", x);
996 }
997
998 TEST(AssignTest, CompatibleTypes) {
999 double x = 0;
1000 Action<void(int)> a = Assign(&x, 5);
1001 a.Perform(make_tuple(0));
1002 EXPECT_DOUBLE_EQ(5, x);
1003 }
1004
1005 #ifndef _WIN32_WCE
1006
1007 class SetErrnoAndReturnTest : public testing::Test {
1008 protected:
1009 virtual void SetUp() { errno = 0; }
1010 virtual void TearDown() { errno = 0; }
1011 };
1012
1013 TEST_F(SetErrnoAndReturnTest, Int) {
1014 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
1015 EXPECT_EQ(-5, a.Perform(make_tuple()));
1016 EXPECT_EQ(ENOTTY, errno);
1017 }
1018
1019 TEST_F(SetErrnoAndReturnTest, Ptr) {
1020 int x;
1021 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
1022 EXPECT_EQ(&x, a.Perform(make_tuple()));
1023 EXPECT_EQ(ENOTTY, errno);
1024 }
1025
1026 TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1027 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
1028 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
1029 EXPECT_EQ(EINVAL, errno);
1030 }
1031
1032 #endif // _WIN32_WCE
1033
1034 } // Unnamed namespace
OLDNEW
« no previous file with comments | « testing/gmock/src/gmock_main.cc ('k') | testing/gmock/test/gmock-cardinalities_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698