blob: a2c6fe11cd5d9743370fbeaf3b2fcf721edf19e0 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// 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
45namespace {
46
47using ::std::tr1::get;
48using ::std::tr1::make_tuple;
49using ::std::tr1::tuple;
50using ::std::tr1::tuple_element;
51using testing::internal::BuiltInDefaultValue;
52using testing::internal::Int64;
53using testing::internal::UInt64;
54// This list should be kept sorted.
55using testing::_;
56using testing::Action;
57using testing::ActionInterface;
58using testing::Assign;
zhanyong.wana18423e2009-07-22 23:58:19 +000059using testing::ByRef;
shiqiane35fdd92008-12-10 05:08:54 +000060using testing::DefaultValue;
61using testing::DoDefault;
62using testing::IgnoreResult;
63using testing::Invoke;
64using testing::InvokeWithoutArgs;
65using testing::MakePolymorphicAction;
66using testing::Ne;
67using testing::PolymorphicAction;
68using testing::Return;
69using testing::ReturnNull;
70using testing::ReturnRef;
71using testing::SetArgumentPointee;
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000072
zhanyong.wanf7af24c2009-09-24 21:17:24 +000073#if !GTEST_OS_WINDOWS_MOBILE
shiqiane35fdd92008-12-10 05:08:54 +000074using testing::SetErrnoAndReturn;
zhanyong.wanf7af24c2009-09-24 21:17:24 +000075#endif
shiqiane35fdd92008-12-10 05:08:54 +000076
77#if GMOCK_HAS_PROTOBUF_
78using testing::internal::TestMessage;
79#endif // GMOCK_HAS_PROTOBUF_
80
81// Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
82TEST(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
zhanyong.wan5b95fa72009-01-27 22:28:45 +000088// Tests that BuiltInDefaultValue<T*>::Exists() return true.
89TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
90 EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
91 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
92 EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
93}
94
shiqiane35fdd92008-12-10 05:08:54 +000095// Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
96// built-in numeric type.
97TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
zhanyong.wan32de5f52009-12-23 00:13:23 +000098 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
shiqiane35fdd92008-12-10 05:08:54 +000099 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
100 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000101#if GMOCK_HAS_SIGNED_WCHAR_T_
zhanyong.wan32de5f52009-12-23 00:13:23 +0000102 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000103 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000104#endif
105#if GMOCK_WCHAR_T_IS_NATIVE_
shiqiane35fdd92008-12-10 05:08:54 +0000106 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000107#endif
zhanyong.wan32de5f52009-12-23 00:13:23 +0000108 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000109 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT
110 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000111 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000112 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
113 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000114 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000115 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT
116 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000117 EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000118 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
119 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
120 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
121}
122
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000123// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
124// built-in numeric type.
125TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
126 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
127 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
128 EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000129#if GMOCK_HAS_SIGNED_WCHAR_T_
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000130 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
131 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000132#endif
133#if GMOCK_WCHAR_T_IS_NATIVE_
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000134 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000135#endif
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000136 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT
137 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT
138 EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT
139 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
140 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
141 EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
142 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT
143 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT
144 EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT
145 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
146 EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists());
147 EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
148 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
149}
150
shiqiane35fdd92008-12-10 05:08:54 +0000151// Tests that BuiltInDefaultValue<bool>::Get() returns false.
152TEST(BuiltInDefaultValueTest, IsFalseForBool) {
153 EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
154}
155
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000156// Tests that BuiltInDefaultValue<bool>::Exists() returns true.
157TEST(BuiltInDefaultValueTest, BoolExists) {
158 EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
159}
160
shiqiane35fdd92008-12-10 05:08:54 +0000161// Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
162// string type.
163TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
164#if GTEST_HAS_GLOBAL_STRING
165 EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get());
166#endif // GTEST_HAS_GLOBAL_STRING
167
shiqiane35fdd92008-12-10 05:08:54 +0000168 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000169}
170
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000171// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
172// string type.
173TEST(BuiltInDefaultValueTest, ExistsForString) {
174#if GTEST_HAS_GLOBAL_STRING
175 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
176#endif // GTEST_HAS_GLOBAL_STRING
177
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000178 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000179}
180
shiqiane35fdd92008-12-10 05:08:54 +0000181// Tests that BuiltInDefaultValue<const T>::Get() returns the same
182// value as BuiltInDefaultValue<T>::Get() does.
183TEST(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.
192struct UserType {
193 UserType() : value(0) {}
194
195 int value;
196};
197
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000198TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) {
199 EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists());
200}
201
shiqiane35fdd92008-12-10 05:08:54 +0000202// Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
203TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000204 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000205 BuiltInDefaultValue<int&>::Get();
206 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000207 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000208 BuiltInDefaultValue<const char&>::Get();
209 }, "");
210}
211
212TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000213 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000214 BuiltInDefaultValue<UserType>::Get();
215 }, "");
216}
217
shiqiane35fdd92008-12-10 05:08:54 +0000218// Tests that DefaultValue<T>::IsSet() is false initially.
219TEST(DefaultValueTest, IsInitiallyUnset) {
220 EXPECT_FALSE(DefaultValue<int>::IsSet());
221 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
222}
223
224// Tests that DefaultValue<T> can be set and then unset.
225TEST(DefaultValueTest, CanBeSetAndUnset) {
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000226 EXPECT_TRUE(DefaultValue<int>::Exists());
227 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
228
shiqiane35fdd92008-12-10 05:08:54 +0000229 DefaultValue<int>::Set(1);
230 DefaultValue<const UserType>::Set(UserType());
231
232 EXPECT_EQ(1, DefaultValue<int>::Get());
233 EXPECT_EQ(0, DefaultValue<const UserType>::Get().value);
234
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000235 EXPECT_TRUE(DefaultValue<int>::Exists());
236 EXPECT_TRUE(DefaultValue<const UserType>::Exists());
237
shiqiane35fdd92008-12-10 05:08:54 +0000238 DefaultValue<int>::Clear();
239 DefaultValue<const UserType>::Clear();
240
241 EXPECT_FALSE(DefaultValue<int>::IsSet());
242 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000243
244 EXPECT_TRUE(DefaultValue<int>::Exists());
245 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000246}
247
248// Tests that DefaultValue<T>::Get() returns the
249// BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
250// false.
251TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
252 EXPECT_FALSE(DefaultValue<int>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000253 EXPECT_TRUE(DefaultValue<int>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000254 EXPECT_FALSE(DefaultValue<UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000255 EXPECT_FALSE(DefaultValue<UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000256
257 EXPECT_EQ(0, DefaultValue<int>::Get());
258
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000259 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000260 DefaultValue<UserType>::Get();
261 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000262}
263
264// Tests that DefaultValue<void>::Get() returns void.
265TEST(DefaultValueTest, GetWorksForVoid) {
266 return DefaultValue<void>::Get();
267}
268
269// Tests using DefaultValue with a reference type.
270
271// Tests that DefaultValue<T&>::IsSet() is false initially.
272TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
273 EXPECT_FALSE(DefaultValue<int&>::IsSet());
274 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
275}
276
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000277// Tests that DefaultValue<T&>::Exists is false initiallly.
278TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
279 EXPECT_FALSE(DefaultValue<int&>::Exists());
280 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
281}
282
shiqiane35fdd92008-12-10 05:08:54 +0000283// Tests that DefaultValue<T&> can be set and then unset.
284TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
285 int n = 1;
286 DefaultValue<const int&>::Set(n);
287 UserType u;
288 DefaultValue<UserType&>::Set(u);
289
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000290 EXPECT_TRUE(DefaultValue<const int&>::Exists());
291 EXPECT_TRUE(DefaultValue<UserType&>::Exists());
292
shiqiane35fdd92008-12-10 05:08:54 +0000293 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
294 EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get()));
295
296 DefaultValue<const int&>::Clear();
297 DefaultValue<UserType&>::Clear();
298
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000299 EXPECT_FALSE(DefaultValue<const int&>::Exists());
300 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
301
shiqiane35fdd92008-12-10 05:08:54 +0000302 EXPECT_FALSE(DefaultValue<const int&>::IsSet());
303 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
304}
305
306// Tests that DefaultValue<T&>::Get() returns the
307// BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
308// false.
309TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
310 EXPECT_FALSE(DefaultValue<int&>::IsSet());
311 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
312
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000313 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000314 DefaultValue<int&>::Get();
315 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000316 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000317 DefaultValue<UserType>::Get();
318 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000319}
320
321// Tests that ActionInterface can be implemented by defining the
322// Perform method.
323
324typedef int MyFunction(bool, int);
325
326class MyActionImpl : public ActionInterface<MyFunction> {
327 public:
328 virtual int Perform(const tuple<bool, int>& args) {
329 return get<0>(args) ? get<1>(args) : 0;
330 }
331};
332
333TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
334 MyActionImpl my_action_impl;
335
336 EXPECT_FALSE(my_action_impl.IsDoDefault());
337}
338
339TEST(ActionInterfaceTest, MakeAction) {
340 Action<MyFunction> action = MakeAction(new MyActionImpl);
341
342 // When exercising the Perform() method of Action<F>, we must pass
343 // it a tuple whose size and type are compatible with F's argument
344 // types. For example, if F is int(), then Perform() takes a
345 // 0-tuple; if F is void(bool, int), then Perform() takes a
346 // tuple<bool, int>, and so on.
347 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
348}
349
350// Tests that Action<F> can be contructed from a pointer to
351// ActionInterface<F>.
352TEST(ActionTest, CanBeConstructedFromActionInterface) {
353 Action<MyFunction> action(new MyActionImpl);
354}
355
356// Tests that Action<F> delegates actual work to ActionInterface<F>.
357TEST(ActionTest, DelegatesWorkToActionInterface) {
358 const Action<MyFunction> action(new MyActionImpl);
359
360 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
361 EXPECT_EQ(0, action.Perform(make_tuple(false, 1)));
362}
363
364// Tests that Action<F> can be copied.
365TEST(ActionTest, IsCopyable) {
366 Action<MyFunction> a1(new MyActionImpl);
367 Action<MyFunction> a2(a1); // Tests the copy constructor.
368
369 // a1 should continue to work after being copied from.
370 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
371 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
372
373 // a2 should work like the action it was copied from.
374 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
375 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
376
377 a2 = a1; // Tests the assignment operator.
378
379 // a1 should continue to work after being copied from.
380 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
381 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
382
383 // a2 should work like the action it was copied from.
384 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
385 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
386}
387
388// Tests that an Action<From> object can be converted to a
389// compatible Action<To> object.
390
391class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
392 public:
393 virtual bool Perform(const tuple<int>& arg) {
394 return get<0>(arg) != 0;
395 }
396};
397
zhanyong.wan95b12332009-09-25 18:55:50 +0000398#if !GTEST_OS_SYMBIAN
399// Compiling this test on Nokia's Symbian compiler fails with:
400// 'Result' is not a member of class 'testing::internal::Function<int>'
401// (point of instantiation: '@unnamed@gmock_actions_test_cc@::
402// ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()')
403// with no obvious fix.
shiqiane35fdd92008-12-10 05:08:54 +0000404TEST(ActionTest, CanBeConvertedToOtherActionType) {
405 const Action<bool(int)> a1(new IsNotZero); // NOLINT
406 const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT
407 EXPECT_EQ(1, a2.Perform(make_tuple('a')));
408 EXPECT_EQ(0, a2.Perform(make_tuple('\0')));
409}
zhanyong.wan95b12332009-09-25 18:55:50 +0000410#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +0000411
412// The following two classes are for testing MakePolymorphicAction().
413
414// Implements a polymorphic action that returns the second of the
415// arguments it receives.
416class ReturnSecondArgumentAction {
417 public:
418 // We want to verify that MakePolymorphicAction() can work with a
419 // polymorphic action whose Perform() method template is either
420 // const or not. This lets us verify the non-const case.
421 template <typename Result, typename ArgumentTuple>
422 Result Perform(const ArgumentTuple& args) { return get<1>(args); }
423};
424
425// Implements a polymorphic action that can be used in a nullary
426// function to return 0.
427class ReturnZeroFromNullaryFunctionAction {
428 public:
429 // For testing that MakePolymorphicAction() works when the
430 // implementation class' Perform() method template takes only one
431 // template parameter.
432 //
433 // We want to verify that MakePolymorphicAction() can work with a
434 // polymorphic action whose Perform() method template is either
435 // const or not. This lets us verify the const case.
436 template <typename Result>
437 Result Perform(const tuple<>&) const { return 0; }
438};
439
440// These functions verify that MakePolymorphicAction() returns a
441// PolymorphicAction<T> where T is the argument's type.
442
443PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
444 return MakePolymorphicAction(ReturnSecondArgumentAction());
445}
446
447PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
448ReturnZeroFromNullaryFunction() {
449 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
450}
451
452// Tests that MakePolymorphicAction() turns a polymorphic action
453// implementation class into a polymorphic action.
454TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
455 Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT
456 EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0)));
457}
458
459// Tests that MakePolymorphicAction() works when the implementation
460// class' Perform() method template has only one template parameter.
461TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
462 Action<int()> a1 = ReturnZeroFromNullaryFunction();
463 EXPECT_EQ(0, a1.Perform(make_tuple()));
464
465 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
466 EXPECT_TRUE(a2.Perform(make_tuple()) == NULL);
467}
468
469// Tests that Return() works as an action for void-returning
470// functions.
471TEST(ReturnTest, WorksForVoid) {
472 const Action<void(int)> ret = Return(); // NOLINT
473 return ret.Perform(make_tuple(1));
474}
475
476// Tests that Return(v) returns v.
477TEST(ReturnTest, ReturnsGivenValue) {
478 Action<int()> ret = Return(1); // NOLINT
479 EXPECT_EQ(1, ret.Perform(make_tuple()));
480
481 ret = Return(-5);
482 EXPECT_EQ(-5, ret.Perform(make_tuple()));
483}
484
485// Tests that Return("string literal") works.
486TEST(ReturnTest, AcceptsStringLiteral) {
487 Action<const char*()> a1 = Return("Hello");
488 EXPECT_STREQ("Hello", a1.Perform(make_tuple()));
489
490 Action<std::string()> a2 = Return("world");
491 EXPECT_EQ("world", a2.Perform(make_tuple()));
492}
493
494// Tests that Return(v) is covaraint.
495
496struct Base {
497 bool operator==(const Base&) { return true; }
498};
499
500struct Derived : public Base {
501 bool operator==(const Derived&) { return true; }
502};
503
504TEST(ReturnTest, IsCovariant) {
505 Base base;
506 Derived derived;
507 Action<Base*()> ret = Return(&base);
508 EXPECT_EQ(&base, ret.Perform(make_tuple()));
509
510 ret = Return(&derived);
511 EXPECT_EQ(&derived, ret.Perform(make_tuple()));
512}
513
vladloseva070cbd2009-11-18 00:09:28 +0000514// Tests that the type of the value passed into Return is converted into T
515// when the action is cast to Action<T(...)> rather than when the action is
516// performed. See comments on testing::internal::ReturnAction in
517// gmock-actions.h for more information.
518class FromType {
519 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000520 FromType(bool* is_converted) : converted_(is_converted) {}
vladloseva070cbd2009-11-18 00:09:28 +0000521 bool* converted() const { return converted_; }
522
523 private:
524 bool* const converted_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000525
526 GTEST_DISALLOW_ASSIGN_(FromType);
vladloseva070cbd2009-11-18 00:09:28 +0000527};
528
529class ToType {
530 public:
531 ToType(const FromType& x) { *x.converted() = true; }
532};
533
534TEST(ReturnTest, ConvertsArgumentWhenConverted) {
535 bool converted = false;
536 FromType x(&converted);
537 Action<ToType()> action(Return(x));
538 EXPECT_TRUE(converted) << "Return must convert its argument in its own "
539 << "conversion operator.";
540 converted = false;
541 action.Perform(tuple<>());
542 EXPECT_FALSE(converted) << "Action must NOT convert its argument "
543 << "when performed." ;
544}
545
vladloseva070cbd2009-11-18 00:09:28 +0000546class DestinationType {};
547
548class SourceType {
549 public:
550 // Note: a non-const typecast operator.
551 operator DestinationType() { return DestinationType(); }
552};
553
554TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
555 SourceType s;
556 Action<DestinationType()> action(Return(s));
557}
vladloseva070cbd2009-11-18 00:09:28 +0000558
shiqiane35fdd92008-12-10 05:08:54 +0000559// Tests that ReturnNull() returns NULL in a pointer-returning function.
560TEST(ReturnNullTest, WorksInPointerReturningFunction) {
561 const Action<int*()> a1 = ReturnNull();
562 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
563
564 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
565 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
566}
567
568// Tests that ReturnRef(v) works for reference types.
569TEST(ReturnRefTest, WorksForReference) {
570 const int n = 0;
571 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
572
573 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
574}
575
576// Tests that ReturnRef(v) is covariant.
577TEST(ReturnRefTest, IsCovariant) {
578 Base base;
579 Derived derived;
580 Action<Base&()> a = ReturnRef(base);
581 EXPECT_EQ(&base, &a.Perform(make_tuple()));
582
583 a = ReturnRef(derived);
584 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
585}
586
587// Tests that DoDefault() does the default action for the mock method.
588
589class MyClass {};
590
591class MockClass {
592 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000593 MockClass() {}
594
shiqiane35fdd92008-12-10 05:08:54 +0000595 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
596 MOCK_METHOD0(Foo, MyClass());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000597
598 private:
599 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
shiqiane35fdd92008-12-10 05:08:54 +0000600};
601
602// Tests that DoDefault() returns the built-in default value for the
603// return type by default.
604TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
605 MockClass mock;
606 EXPECT_CALL(mock, IntFunc(_))
607 .WillOnce(DoDefault());
608 EXPECT_EQ(0, mock.IntFunc(true));
609}
610
shiqiane35fdd92008-12-10 05:08:54 +0000611// Tests that DoDefault() aborts the process when there is no built-in
612// default value for the return type.
613TEST(DoDefaultDeathTest, DiesForUnknowType) {
614 MockClass mock;
615 EXPECT_CALL(mock, Foo())
616 .WillRepeatedly(DoDefault());
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000617 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000618 mock.Foo();
619 }, "");
620}
621
622// Tests that using DoDefault() inside a composite action leads to a
623// run-time error.
624
zhanyong.wan32de5f52009-12-23 00:13:23 +0000625void VoidFunc(bool /* flag */) {}
shiqiane35fdd92008-12-10 05:08:54 +0000626
627TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
628 MockClass mock;
629 EXPECT_CALL(mock, IntFunc(_))
630 .WillRepeatedly(DoAll(Invoke(VoidFunc),
631 DoDefault()));
632
633 // Ideally we should verify the error message as well. Sadly,
634 // EXPECT_DEATH() can only capture stderr, while Google Mock's
635 // errors are printed on stdout. Therefore we have to settle for
636 // not verifying the message.
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000637 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000638 mock.IntFunc(true);
639 }, "");
640}
641
shiqiane35fdd92008-12-10 05:08:54 +0000642// Tests that DoDefault() returns the default value set by
643// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
644TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
645 DefaultValue<int>::Set(1);
646 MockClass mock;
647 EXPECT_CALL(mock, IntFunc(_))
648 .WillOnce(DoDefault());
649 EXPECT_EQ(1, mock.IntFunc(false));
650 DefaultValue<int>::Clear();
651}
652
653// Tests that DoDefault() does the action specified by ON_CALL().
654TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
655 MockClass mock;
656 ON_CALL(mock, IntFunc(_))
657 .WillByDefault(Return(2));
658 EXPECT_CALL(mock, IntFunc(_))
659 .WillOnce(DoDefault());
660 EXPECT_EQ(2, mock.IntFunc(false));
661}
662
663// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
664TEST(DoDefaultTest, CannotBeUsedInOnCall) {
665 MockClass mock;
666 EXPECT_NONFATAL_FAILURE({ // NOLINT
667 ON_CALL(mock, IntFunc(_))
668 .WillByDefault(DoDefault());
669 }, "DoDefault() cannot be used in ON_CALL()");
670}
671
672// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
673// the N-th (0-based) argument to v.
674TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
675 typedef void MyFunction(bool, int*, char*);
676 Action<MyFunction> a = SetArgumentPointee<1>(2);
677
678 int n = 0;
679 char ch = '\0';
680 a.Perform(make_tuple(true, &n, &ch));
681 EXPECT_EQ(2, n);
682 EXPECT_EQ('\0', ch);
683
684 a = SetArgumentPointee<2>('a');
685 n = 0;
686 ch = '\0';
687 a.Perform(make_tuple(true, &n, &ch));
688 EXPECT_EQ(0, n);
689 EXPECT_EQ('a', ch);
690}
691
692#if GMOCK_HAS_PROTOBUF_
693
zhanyong.wanc6a41232009-05-13 23:38:40 +0000694// Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
695// variable pointed to by the N-th (0-based) argument to proto_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000696TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
shiqiane35fdd92008-12-10 05:08:54 +0000697 TestMessage* const msg = new TestMessage;
698 msg->set_member("yes");
699 TestMessage orig_msg;
700 orig_msg.CopyFrom(*msg);
701
zhanyong.wanc6a41232009-05-13 23:38:40 +0000702 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000703 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
704 // s.t. the action works even when the original proto_buffer has
705 // died. We ensure this behavior by deleting msg before using the
706 // action.
707 delete msg;
708
709 TestMessage dest;
710 EXPECT_FALSE(orig_msg.Equals(dest));
711 a.Perform(make_tuple(true, &dest));
712 EXPECT_TRUE(orig_msg.Equals(dest));
713}
714
zhanyong.wanc6a41232009-05-13 23:38:40 +0000715// Tests that SetArgumentPointee<N>(proto_buffer) sets the
716// ::ProtocolMessage variable pointed to by the N-th (0-based)
717// argument to proto_buffer.
718TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
719 TestMessage* const msg = new TestMessage;
720 msg->set_member("yes");
721 TestMessage orig_msg;
722 orig_msg.CopyFrom(*msg);
723
724 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
725 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
726 // s.t. the action works even when the original proto_buffer has
727 // died. We ensure this behavior by deleting msg before using the
728 // action.
729 delete msg;
730
731 TestMessage dest;
732 ::ProtocolMessage* const dest_base = &dest;
733 EXPECT_FALSE(orig_msg.Equals(dest));
734 a.Perform(make_tuple(true, dest_base));
735 EXPECT_TRUE(orig_msg.Equals(dest));
736}
737
738// Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
739// protobuf variable pointed to by the N-th (0-based) argument to
740// proto2_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000741TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
742 using testing::internal::FooMessage;
shiqiane35fdd92008-12-10 05:08:54 +0000743 FooMessage* const msg = new FooMessage;
744 msg->set_int_field(2);
745 msg->set_string_field("hi");
746 FooMessage orig_msg;
747 orig_msg.CopyFrom(*msg);
748
zhanyong.wanc6a41232009-05-13 23:38:40 +0000749 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000750 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
751 // proto2_buffer s.t. the action works even when the original
752 // proto2_buffer has died. We ensure this behavior by deleting msg
753 // before using the action.
754 delete msg;
755
756 FooMessage dest;
757 dest.set_int_field(0);
758 a.Perform(make_tuple(true, &dest));
759 EXPECT_EQ(2, dest.int_field());
760 EXPECT_EQ("hi", dest.string_field());
761}
762
zhanyong.wanc6a41232009-05-13 23:38:40 +0000763// Tests that SetArgumentPointee<N>(proto2_buffer) sets the
764// proto2::Message variable pointed to by the N-th (0-based) argument
765// to proto2_buffer.
766TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
767 using testing::internal::FooMessage;
768 FooMessage* const msg = new FooMessage;
769 msg->set_int_field(2);
770 msg->set_string_field("hi");
771 FooMessage orig_msg;
772 orig_msg.CopyFrom(*msg);
773
774 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
775 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
776 // proto2_buffer s.t. the action works even when the original
777 // proto2_buffer has died. We ensure this behavior by deleting msg
778 // before using the action.
779 delete msg;
780
781 FooMessage dest;
782 dest.set_int_field(0);
783 ::proto2::Message* const dest_base = &dest;
784 a.Perform(make_tuple(true, dest_base));
785 EXPECT_EQ(2, dest.int_field());
786 EXPECT_EQ("hi", dest.string_field());
787}
788
shiqiane35fdd92008-12-10 05:08:54 +0000789#endif // GMOCK_HAS_PROTOBUF_
790
shiqiane35fdd92008-12-10 05:08:54 +0000791// Sample functions and functors for testing Invoke() and etc.
792int Nullary() { return 1; }
793
794class NullaryFunctor {
795 public:
796 int operator()() { return 2; }
797};
798
799bool g_done = false;
800void VoidNullary() { g_done = true; }
801
802class VoidNullaryFunctor {
803 public:
804 void operator()() { g_done = true; }
805};
806
807bool Unary(int x) { return x < 0; }
808
809const char* Plus1(const char* s) { return s + 1; }
810
zhanyong.wan32de5f52009-12-23 00:13:23 +0000811void VoidUnary(int /* n */) { g_done = true; }
shiqiane35fdd92008-12-10 05:08:54 +0000812
813bool ByConstRef(const std::string& s) { return s == "Hi"; }
814
815const double g_double = 0;
816bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
817
818std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT
819
820struct UnaryFunctor {
821 int operator()(bool x) { return x ? 1 : -1; }
822};
823
824const char* Binary(const char* input, short n) { return input + n; } // NOLINT
825
826void VoidBinary(int, char) { g_done = true; }
827
828int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
829
830void VoidTernary(int, char, bool) { g_done = true; }
831
832int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
833
834void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
835
836int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
837
838struct SumOf5Functor {
839 int operator()(int a, int b, int c, int d, int e) {
840 return a + b + c + d + e;
841 }
842};
843
844int SumOf6(int a, int b, int c, int d, int e, int f) {
845 return a + b + c + d + e + f;
846}
847
848struct SumOf6Functor {
849 int operator()(int a, int b, int c, int d, int e, int f) {
850 return a + b + c + d + e + f;
851 }
852};
853
854class Foo {
855 public:
856 Foo() : value_(123) {}
857
858 int Nullary() const { return value_; }
859 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
860 std::string Binary(const std::string& str, char c) const { return str + c; }
861 int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
862 int SumOf4(int a, int b, int c, int d) const {
863 return a + b + c + d + value_;
864 }
865 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
866 int SumOf6(int a, int b, int c, int d, int e, int f) {
867 return a + b + c + d + e + f;
868 }
869 private:
870 int value_;
871};
872
873// Tests InvokeWithoutArgs(function).
874TEST(InvokeWithoutArgsTest, Function) {
875 // As an action that takes one argument.
876 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
877 EXPECT_EQ(1, a.Perform(make_tuple(2)));
878
879 // As an action that takes two arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000880 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000881 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
882
883 // As an action that returns void.
884 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
885 g_done = false;
886 a3.Perform(make_tuple(1));
887 EXPECT_TRUE(g_done);
888}
889
890// Tests InvokeWithoutArgs(functor).
891TEST(InvokeWithoutArgsTest, Functor) {
892 // As an action that takes no argument.
893 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
894 EXPECT_EQ(2, a.Perform(make_tuple()));
895
896 // As an action that takes three arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000897 Action<int(int, double, char)> a2 = // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000898 InvokeWithoutArgs(NullaryFunctor());
899 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
900
901 // As an action that returns void.
902 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
903 g_done = false;
904 a3.Perform(make_tuple());
905 EXPECT_TRUE(g_done);
906}
907
908// Tests InvokeWithoutArgs(obj_ptr, method).
909TEST(InvokeWithoutArgsTest, Method) {
910 Foo foo;
911 Action<int(bool, char)> a = // NOLINT
912 InvokeWithoutArgs(&foo, &Foo::Nullary);
913 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
914}
915
916// Tests using IgnoreResult() on a polymorphic action.
917TEST(IgnoreResultTest, PolymorphicAction) {
918 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
919 a.Perform(make_tuple(1));
920}
921
922// Tests using IgnoreResult() on a monomorphic action.
923
924int ReturnOne() {
925 g_done = true;
926 return 1;
927}
928
929TEST(IgnoreResultTest, MonomorphicAction) {
930 g_done = false;
931 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
932 a.Perform(make_tuple());
933 EXPECT_TRUE(g_done);
934}
935
936// Tests using IgnoreResult() on an action that returns a class type.
937
zhanyong.wan32de5f52009-12-23 00:13:23 +0000938MyClass ReturnMyClass(double /* x */) {
shiqiane35fdd92008-12-10 05:08:54 +0000939 g_done = true;
940 return MyClass();
941}
942
943TEST(IgnoreResultTest, ActionReturningClass) {
944 g_done = false;
945 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
946 a.Perform(make_tuple(2));
947 EXPECT_TRUE(g_done);
948}
949
950TEST(AssignTest, Int) {
951 int x = 0;
952 Action<void(int)> a = Assign(&x, 5);
953 a.Perform(make_tuple(0));
954 EXPECT_EQ(5, x);
955}
956
957TEST(AssignTest, String) {
958 ::std::string x;
959 Action<void(void)> a = Assign(&x, "Hello, world");
960 a.Perform(make_tuple());
961 EXPECT_EQ("Hello, world", x);
962}
963
964TEST(AssignTest, CompatibleTypes) {
965 double x = 0;
966 Action<void(int)> a = Assign(&x, 5);
967 a.Perform(make_tuple(0));
968 EXPECT_DOUBLE_EQ(5, x);
969}
970
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000971#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000972
shiqiane35fdd92008-12-10 05:08:54 +0000973class SetErrnoAndReturnTest : public testing::Test {
974 protected:
975 virtual void SetUp() { errno = 0; }
976 virtual void TearDown() { errno = 0; }
977};
978
979TEST_F(SetErrnoAndReturnTest, Int) {
980 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
981 EXPECT_EQ(-5, a.Perform(make_tuple()));
982 EXPECT_EQ(ENOTTY, errno);
983}
984
985TEST_F(SetErrnoAndReturnTest, Ptr) {
986 int x;
987 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
988 EXPECT_EQ(&x, a.Perform(make_tuple()));
989 EXPECT_EQ(ENOTTY, errno);
990}
991
992TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
993 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
994 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
995 EXPECT_EQ(EINVAL, errno);
996}
997
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000998#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000999
zhanyong.wana18423e2009-07-22 23:58:19 +00001000// Tests ByRef().
1001
1002// Tests that ReferenceWrapper<T> is copyable.
1003TEST(ByRefTest, IsCopyable) {
1004 const std::string s1 = "Hi";
1005 const std::string s2 = "Hello";
1006
1007 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper = ByRef(s1);
1008 const std::string& r1 = ref_wrapper;
1009 EXPECT_EQ(&s1, &r1);
1010
1011 // Assigns a new value to ref_wrapper.
1012 ref_wrapper = ByRef(s2);
1013 const std::string& r2 = ref_wrapper;
1014 EXPECT_EQ(&s2, &r2);
1015
1016 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = ByRef(s1);
1017 // Copies ref_wrapper1 to ref_wrapper.
1018 ref_wrapper = ref_wrapper1;
1019 const std::string& r3 = ref_wrapper;
1020 EXPECT_EQ(&s1, &r3);
1021}
1022
1023// Tests using ByRef() on a const value.
1024TEST(ByRefTest, ConstValue) {
1025 const int n = 0;
1026 // int& ref = ByRef(n); // This shouldn't compile - we have a
1027 // negative compilation test to catch it.
1028 const int& const_ref = ByRef(n);
1029 EXPECT_EQ(&n, &const_ref);
1030}
1031
1032// Tests using ByRef() on a non-const value.
1033TEST(ByRefTest, NonConstValue) {
1034 int n = 0;
1035
1036 // ByRef(n) can be used as either an int&,
1037 int& ref = ByRef(n);
1038 EXPECT_EQ(&n, &ref);
1039
1040 // or a const int&.
1041 const int& const_ref = ByRef(n);
1042 EXPECT_EQ(&n, &const_ref);
1043}
1044
1045// Tests explicitly specifying the type when using ByRef().
1046TEST(ByRefTest, ExplicitType) {
1047 int n = 0;
1048 const int& r1 = ByRef<const int>(n);
1049 EXPECT_EQ(&n, &r1);
1050
1051 // ByRef<char>(n); // This shouldn't compile - we have a negative
1052 // compilation test to catch it.
1053
1054 Derived d;
1055 Derived& r2 = ByRef<Derived>(d);
1056 EXPECT_EQ(&d, &r2);
1057
1058 const Derived& r3 = ByRef<const Derived>(d);
1059 EXPECT_EQ(&d, &r3);
1060
1061 Base& r4 = ByRef<Base>(d);
1062 EXPECT_EQ(&d, &r4);
1063
1064 const Base& r5 = ByRef<const Base>(d);
1065 EXPECT_EQ(&d, &r5);
1066
1067 // The following shouldn't compile - we have a negative compilation
1068 // test for it.
1069 //
1070 // Base b;
1071 // ByRef<Derived>(b);
1072}
1073
1074// Tests that Google Mock prints expression ByRef(x) as a reference to x.
1075TEST(ByRefTest, PrintsCorrectly) {
1076 int n = 42;
1077 ::std::stringstream expected, actual;
1078 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1079 testing::internal::UniversalPrint(ByRef(n), &actual);
1080 EXPECT_EQ(expected.str(), actual.str());
1081}
1082
shiqiane35fdd92008-12-10 05:08:54 +00001083} // Unnamed namespace