blob: e2b1d0527220350fc7e57b42a331cd1bb04cdacd [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) {
98 EXPECT_EQ(0, BuiltInDefaultValue<unsigned char>::Get());
99 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_
shiqiane35fdd92008-12-10 05:08:54 +0000102 EXPECT_EQ(0, BuiltInDefaultValue<unsigned wchar_t>::Get());
103 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
shiqiane35fdd92008-12-10 05:08:54 +0000108 EXPECT_EQ(0, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT
109 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT
110 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT
111 EXPECT_EQ(0, BuiltInDefaultValue<unsigned int>::Get());
112 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
113 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
114 EXPECT_EQ(0, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT
115 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT
116 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT
117 EXPECT_EQ(0, BuiltInDefaultValue<UInt64>::Get());
118 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
168#if GTEST_HAS_STD_STRING
169 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
170#endif // GTEST_HAS_STD_STRING
171}
172
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000173// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
174// string type.
175TEST(BuiltInDefaultValueTest, ExistsForString) {
176#if GTEST_HAS_GLOBAL_STRING
177 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
178#endif // GTEST_HAS_GLOBAL_STRING
179
180#if GTEST_HAS_STD_STRING
181 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
182#endif // GTEST_HAS_STD_STRING
183}
184
shiqiane35fdd92008-12-10 05:08:54 +0000185// Tests that BuiltInDefaultValue<const T>::Get() returns the same
186// value as BuiltInDefaultValue<T>::Get() does.
187TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
188 EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
189 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
190 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL);
191 EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
192}
193
194// Tests that BuiltInDefaultValue<T>::Get() aborts the program with
195// the correct error message when T is a user-defined type.
196struct UserType {
197 UserType() : value(0) {}
198
199 int value;
200};
201
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000202TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) {
203 EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists());
204}
205
shiqiane35fdd92008-12-10 05:08:54 +0000206// Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
207TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000208 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000209 BuiltInDefaultValue<int&>::Get();
210 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000211 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000212 BuiltInDefaultValue<const char&>::Get();
213 }, "");
214}
215
216TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000217 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000218 BuiltInDefaultValue<UserType>::Get();
219 }, "");
220}
221
shiqiane35fdd92008-12-10 05:08:54 +0000222// Tests that DefaultValue<T>::IsSet() is false initially.
223TEST(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.
229TEST(DefaultValueTest, CanBeSetAndUnset) {
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000230 EXPECT_TRUE(DefaultValue<int>::Exists());
231 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
232
shiqiane35fdd92008-12-10 05:08:54 +0000233 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
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000239 EXPECT_TRUE(DefaultValue<int>::Exists());
240 EXPECT_TRUE(DefaultValue<const UserType>::Exists());
241
shiqiane35fdd92008-12-10 05:08:54 +0000242 DefaultValue<int>::Clear();
243 DefaultValue<const UserType>::Clear();
244
245 EXPECT_FALSE(DefaultValue<int>::IsSet());
246 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000247
248 EXPECT_TRUE(DefaultValue<int>::Exists());
249 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000250}
251
252// Tests that DefaultValue<T>::Get() returns the
253// BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
254// false.
255TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
256 EXPECT_FALSE(DefaultValue<int>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000257 EXPECT_TRUE(DefaultValue<int>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000258 EXPECT_FALSE(DefaultValue<UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000259 EXPECT_FALSE(DefaultValue<UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000260
261 EXPECT_EQ(0, DefaultValue<int>::Get());
262
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000263 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000264 DefaultValue<UserType>::Get();
265 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000266}
267
268// Tests that DefaultValue<void>::Get() returns void.
269TEST(DefaultValueTest, GetWorksForVoid) {
270 return DefaultValue<void>::Get();
271}
272
273// Tests using DefaultValue with a reference type.
274
275// Tests that DefaultValue<T&>::IsSet() is false initially.
276TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
277 EXPECT_FALSE(DefaultValue<int&>::IsSet());
278 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
279}
280
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000281// Tests that DefaultValue<T&>::Exists is false initiallly.
282TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
283 EXPECT_FALSE(DefaultValue<int&>::Exists());
284 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
285}
286
shiqiane35fdd92008-12-10 05:08:54 +0000287// Tests that DefaultValue<T&> can be set and then unset.
288TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
289 int n = 1;
290 DefaultValue<const int&>::Set(n);
291 UserType u;
292 DefaultValue<UserType&>::Set(u);
293
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000294 EXPECT_TRUE(DefaultValue<const int&>::Exists());
295 EXPECT_TRUE(DefaultValue<UserType&>::Exists());
296
shiqiane35fdd92008-12-10 05:08:54 +0000297 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
298 EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get()));
299
300 DefaultValue<const int&>::Clear();
301 DefaultValue<UserType&>::Clear();
302
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000303 EXPECT_FALSE(DefaultValue<const int&>::Exists());
304 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
305
shiqiane35fdd92008-12-10 05:08:54 +0000306 EXPECT_FALSE(DefaultValue<const int&>::IsSet());
307 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
308}
309
310// Tests that DefaultValue<T&>::Get() returns the
311// BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
312// false.
313TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
314 EXPECT_FALSE(DefaultValue<int&>::IsSet());
315 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
316
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000317 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000318 DefaultValue<int&>::Get();
319 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000320 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000321 DefaultValue<UserType>::Get();
322 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000323}
324
325// Tests that ActionInterface can be implemented by defining the
326// Perform method.
327
328typedef int MyFunction(bool, int);
329
330class MyActionImpl : public ActionInterface<MyFunction> {
331 public:
332 virtual int Perform(const tuple<bool, int>& args) {
333 return get<0>(args) ? get<1>(args) : 0;
334 }
335};
336
337TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
338 MyActionImpl my_action_impl;
339
340 EXPECT_FALSE(my_action_impl.IsDoDefault());
341}
342
343TEST(ActionInterfaceTest, MakeAction) {
344 Action<MyFunction> action = MakeAction(new MyActionImpl);
345
346 // When exercising the Perform() method of Action<F>, we must pass
347 // it a tuple whose size and type are compatible with F's argument
348 // types. For example, if F is int(), then Perform() takes a
349 // 0-tuple; if F is void(bool, int), then Perform() takes a
350 // tuple<bool, int>, and so on.
351 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
352}
353
354// Tests that Action<F> can be contructed from a pointer to
355// ActionInterface<F>.
356TEST(ActionTest, CanBeConstructedFromActionInterface) {
357 Action<MyFunction> action(new MyActionImpl);
358}
359
360// Tests that Action<F> delegates actual work to ActionInterface<F>.
361TEST(ActionTest, DelegatesWorkToActionInterface) {
362 const Action<MyFunction> action(new MyActionImpl);
363
364 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
365 EXPECT_EQ(0, action.Perform(make_tuple(false, 1)));
366}
367
368// Tests that Action<F> can be copied.
369TEST(ActionTest, IsCopyable) {
370 Action<MyFunction> a1(new MyActionImpl);
371 Action<MyFunction> a2(a1); // Tests the copy constructor.
372
373 // a1 should continue to work after being copied from.
374 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
375 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
376
377 // a2 should work like the action it was copied from.
378 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
379 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
380
381 a2 = a1; // Tests the assignment operator.
382
383 // a1 should continue to work after being copied from.
384 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
385 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
386
387 // a2 should work like the action it was copied from.
388 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
389 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
390}
391
392// Tests that an Action<From> object can be converted to a
393// compatible Action<To> object.
394
395class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
396 public:
397 virtual bool Perform(const tuple<int>& arg) {
398 return get<0>(arg) != 0;
399 }
400};
401
zhanyong.wan95b12332009-09-25 18:55:50 +0000402#if !GTEST_OS_SYMBIAN
403// Compiling this test on Nokia's Symbian compiler fails with:
404// 'Result' is not a member of class 'testing::internal::Function<int>'
405// (point of instantiation: '@unnamed@gmock_actions_test_cc@::
406// ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()')
407// with no obvious fix.
shiqiane35fdd92008-12-10 05:08:54 +0000408TEST(ActionTest, CanBeConvertedToOtherActionType) {
409 const Action<bool(int)> a1(new IsNotZero); // NOLINT
410 const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT
411 EXPECT_EQ(1, a2.Perform(make_tuple('a')));
412 EXPECT_EQ(0, a2.Perform(make_tuple('\0')));
413}
zhanyong.wan95b12332009-09-25 18:55:50 +0000414#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +0000415
416// The following two classes are for testing MakePolymorphicAction().
417
418// Implements a polymorphic action that returns the second of the
419// arguments it receives.
420class ReturnSecondArgumentAction {
421 public:
422 // We want to verify that MakePolymorphicAction() can work with a
423 // polymorphic action whose Perform() method template is either
424 // const or not. This lets us verify the non-const case.
425 template <typename Result, typename ArgumentTuple>
426 Result Perform(const ArgumentTuple& args) { return get<1>(args); }
427};
428
429// Implements a polymorphic action that can be used in a nullary
430// function to return 0.
431class ReturnZeroFromNullaryFunctionAction {
432 public:
433 // For testing that MakePolymorphicAction() works when the
434 // implementation class' Perform() method template takes only one
435 // template parameter.
436 //
437 // We want to verify that MakePolymorphicAction() can work with a
438 // polymorphic action whose Perform() method template is either
439 // const or not. This lets us verify the const case.
440 template <typename Result>
441 Result Perform(const tuple<>&) const { return 0; }
442};
443
444// These functions verify that MakePolymorphicAction() returns a
445// PolymorphicAction<T> where T is the argument's type.
446
447PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
448 return MakePolymorphicAction(ReturnSecondArgumentAction());
449}
450
451PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
452ReturnZeroFromNullaryFunction() {
453 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
454}
455
456// Tests that MakePolymorphicAction() turns a polymorphic action
457// implementation class into a polymorphic action.
458TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
459 Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT
460 EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0)));
461}
462
463// Tests that MakePolymorphicAction() works when the implementation
464// class' Perform() method template has only one template parameter.
465TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
466 Action<int()> a1 = ReturnZeroFromNullaryFunction();
467 EXPECT_EQ(0, a1.Perform(make_tuple()));
468
469 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
470 EXPECT_TRUE(a2.Perform(make_tuple()) == NULL);
471}
472
473// Tests that Return() works as an action for void-returning
474// functions.
475TEST(ReturnTest, WorksForVoid) {
476 const Action<void(int)> ret = Return(); // NOLINT
477 return ret.Perform(make_tuple(1));
478}
479
480// Tests that Return(v) returns v.
481TEST(ReturnTest, ReturnsGivenValue) {
482 Action<int()> ret = Return(1); // NOLINT
483 EXPECT_EQ(1, ret.Perform(make_tuple()));
484
485 ret = Return(-5);
486 EXPECT_EQ(-5, ret.Perform(make_tuple()));
487}
488
489// Tests that Return("string literal") works.
490TEST(ReturnTest, AcceptsStringLiteral) {
491 Action<const char*()> a1 = Return("Hello");
492 EXPECT_STREQ("Hello", a1.Perform(make_tuple()));
493
494 Action<std::string()> a2 = Return("world");
495 EXPECT_EQ("world", a2.Perform(make_tuple()));
496}
497
498// Tests that Return(v) is covaraint.
499
500struct Base {
501 bool operator==(const Base&) { return true; }
502};
503
504struct Derived : public Base {
505 bool operator==(const Derived&) { return true; }
506};
507
508TEST(ReturnTest, IsCovariant) {
509 Base base;
510 Derived derived;
511 Action<Base*()> ret = Return(&base);
512 EXPECT_EQ(&base, ret.Perform(make_tuple()));
513
514 ret = Return(&derived);
515 EXPECT_EQ(&derived, ret.Perform(make_tuple()));
516}
517
vladloseva070cbd2009-11-18 00:09:28 +0000518// Tests that the type of the value passed into Return is converted into T
519// when the action is cast to Action<T(...)> rather than when the action is
520// performed. See comments on testing::internal::ReturnAction in
521// gmock-actions.h for more information.
522class FromType {
523 public:
524 FromType(bool* converted) : converted_(converted) {}
525 bool* converted() const { return converted_; }
526
527 private:
528 bool* const converted_;
529};
530
531class ToType {
532 public:
533 ToType(const FromType& x) { *x.converted() = true; }
534};
535
536TEST(ReturnTest, ConvertsArgumentWhenConverted) {
537 bool converted = false;
538 FromType x(&converted);
539 Action<ToType()> action(Return(x));
540 EXPECT_TRUE(converted) << "Return must convert its argument in its own "
541 << "conversion operator.";
542 converted = false;
543 action.Perform(tuple<>());
544 EXPECT_FALSE(converted) << "Action must NOT convert its argument "
545 << "when performed." ;
546}
547
548// We do not support non-const type conversions on Symbian. See
549// definition of implicit_cast in gmock-port.h for more information.
550#if !GTEST_OS_SYMBIAN
551class DestinationType {};
552
553class SourceType {
554 public:
555 // Note: a non-const typecast operator.
556 operator DestinationType() { return DestinationType(); }
557};
558
559TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
560 SourceType s;
561 Action<DestinationType()> action(Return(s));
562}
563#endif // !GTEST_OS_SYMBIAN
564
shiqiane35fdd92008-12-10 05:08:54 +0000565// Tests that ReturnNull() returns NULL in a pointer-returning function.
566TEST(ReturnNullTest, WorksInPointerReturningFunction) {
567 const Action<int*()> a1 = ReturnNull();
568 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
569
570 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
571 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
572}
573
574// Tests that ReturnRef(v) works for reference types.
575TEST(ReturnRefTest, WorksForReference) {
576 const int n = 0;
577 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
578
579 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
580}
581
582// Tests that ReturnRef(v) is covariant.
583TEST(ReturnRefTest, IsCovariant) {
584 Base base;
585 Derived derived;
586 Action<Base&()> a = ReturnRef(base);
587 EXPECT_EQ(&base, &a.Perform(make_tuple()));
588
589 a = ReturnRef(derived);
590 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
591}
592
593// Tests that DoDefault() does the default action for the mock method.
594
595class MyClass {};
596
597class MockClass {
598 public:
599 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
600 MOCK_METHOD0(Foo, MyClass());
601};
602
603// Tests that DoDefault() returns the built-in default value for the
604// return type by default.
605TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
606 MockClass mock;
607 EXPECT_CALL(mock, IntFunc(_))
608 .WillOnce(DoDefault());
609 EXPECT_EQ(0, mock.IntFunc(true));
610}
611
shiqiane35fdd92008-12-10 05:08:54 +0000612// Tests that DoDefault() aborts the process when there is no built-in
613// default value for the return type.
614TEST(DoDefaultDeathTest, DiesForUnknowType) {
615 MockClass mock;
616 EXPECT_CALL(mock, Foo())
617 .WillRepeatedly(DoDefault());
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000618 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000619 mock.Foo();
620 }, "");
621}
622
623// Tests that using DoDefault() inside a composite action leads to a
624// run-time error.
625
626void VoidFunc(bool flag) {}
627
628TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
629 MockClass mock;
630 EXPECT_CALL(mock, IntFunc(_))
631 .WillRepeatedly(DoAll(Invoke(VoidFunc),
632 DoDefault()));
633
634 // Ideally we should verify the error message as well. Sadly,
635 // EXPECT_DEATH() can only capture stderr, while Google Mock's
636 // errors are printed on stdout. Therefore we have to settle for
637 // not verifying the message.
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000638 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000639 mock.IntFunc(true);
640 }, "");
641}
642
shiqiane35fdd92008-12-10 05:08:54 +0000643// Tests that DoDefault() returns the default value set by
644// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
645TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
646 DefaultValue<int>::Set(1);
647 MockClass mock;
648 EXPECT_CALL(mock, IntFunc(_))
649 .WillOnce(DoDefault());
650 EXPECT_EQ(1, mock.IntFunc(false));
651 DefaultValue<int>::Clear();
652}
653
654// Tests that DoDefault() does the action specified by ON_CALL().
655TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
656 MockClass mock;
657 ON_CALL(mock, IntFunc(_))
658 .WillByDefault(Return(2));
659 EXPECT_CALL(mock, IntFunc(_))
660 .WillOnce(DoDefault());
661 EXPECT_EQ(2, mock.IntFunc(false));
662}
663
664// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
665TEST(DoDefaultTest, CannotBeUsedInOnCall) {
666 MockClass mock;
667 EXPECT_NONFATAL_FAILURE({ // NOLINT
668 ON_CALL(mock, IntFunc(_))
669 .WillByDefault(DoDefault());
670 }, "DoDefault() cannot be used in ON_CALL()");
671}
672
673// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
674// the N-th (0-based) argument to v.
675TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
676 typedef void MyFunction(bool, int*, char*);
677 Action<MyFunction> a = SetArgumentPointee<1>(2);
678
679 int n = 0;
680 char ch = '\0';
681 a.Perform(make_tuple(true, &n, &ch));
682 EXPECT_EQ(2, n);
683 EXPECT_EQ('\0', ch);
684
685 a = SetArgumentPointee<2>('a');
686 n = 0;
687 ch = '\0';
688 a.Perform(make_tuple(true, &n, &ch));
689 EXPECT_EQ(0, n);
690 EXPECT_EQ('a', ch);
691}
692
693#if GMOCK_HAS_PROTOBUF_
694
zhanyong.wanc6a41232009-05-13 23:38:40 +0000695// Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
696// variable pointed to by the N-th (0-based) argument to proto_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000697TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
shiqiane35fdd92008-12-10 05:08:54 +0000698 TestMessage* const msg = new TestMessage;
699 msg->set_member("yes");
700 TestMessage orig_msg;
701 orig_msg.CopyFrom(*msg);
702
zhanyong.wanc6a41232009-05-13 23:38:40 +0000703 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000704 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
705 // s.t. the action works even when the original proto_buffer has
706 // died. We ensure this behavior by deleting msg before using the
707 // action.
708 delete msg;
709
710 TestMessage dest;
711 EXPECT_FALSE(orig_msg.Equals(dest));
712 a.Perform(make_tuple(true, &dest));
713 EXPECT_TRUE(orig_msg.Equals(dest));
714}
715
zhanyong.wanc6a41232009-05-13 23:38:40 +0000716// Tests that SetArgumentPointee<N>(proto_buffer) sets the
717// ::ProtocolMessage variable pointed to by the N-th (0-based)
718// argument to proto_buffer.
719TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
720 TestMessage* const msg = new TestMessage;
721 msg->set_member("yes");
722 TestMessage orig_msg;
723 orig_msg.CopyFrom(*msg);
724
725 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
726 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
727 // s.t. the action works even when the original proto_buffer has
728 // died. We ensure this behavior by deleting msg before using the
729 // action.
730 delete msg;
731
732 TestMessage dest;
733 ::ProtocolMessage* const dest_base = &dest;
734 EXPECT_FALSE(orig_msg.Equals(dest));
735 a.Perform(make_tuple(true, dest_base));
736 EXPECT_TRUE(orig_msg.Equals(dest));
737}
738
739// Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
740// protobuf variable pointed to by the N-th (0-based) argument to
741// proto2_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000742TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
743 using testing::internal::FooMessage;
shiqiane35fdd92008-12-10 05:08:54 +0000744 FooMessage* const msg = new FooMessage;
745 msg->set_int_field(2);
746 msg->set_string_field("hi");
747 FooMessage orig_msg;
748 orig_msg.CopyFrom(*msg);
749
zhanyong.wanc6a41232009-05-13 23:38:40 +0000750 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000751 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
752 // proto2_buffer s.t. the action works even when the original
753 // proto2_buffer has died. We ensure this behavior by deleting msg
754 // before using the action.
755 delete msg;
756
757 FooMessage dest;
758 dest.set_int_field(0);
759 a.Perform(make_tuple(true, &dest));
760 EXPECT_EQ(2, dest.int_field());
761 EXPECT_EQ("hi", dest.string_field());
762}
763
zhanyong.wanc6a41232009-05-13 23:38:40 +0000764// Tests that SetArgumentPointee<N>(proto2_buffer) sets the
765// proto2::Message variable pointed to by the N-th (0-based) argument
766// to proto2_buffer.
767TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
768 using testing::internal::FooMessage;
769 FooMessage* const msg = new FooMessage;
770 msg->set_int_field(2);
771 msg->set_string_field("hi");
772 FooMessage orig_msg;
773 orig_msg.CopyFrom(*msg);
774
775 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
776 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
777 // proto2_buffer s.t. the action works even when the original
778 // proto2_buffer has died. We ensure this behavior by deleting msg
779 // before using the action.
780 delete msg;
781
782 FooMessage dest;
783 dest.set_int_field(0);
784 ::proto2::Message* const dest_base = &dest;
785 a.Perform(make_tuple(true, dest_base));
786 EXPECT_EQ(2, dest.int_field());
787 EXPECT_EQ("hi", dest.string_field());
788}
789
shiqiane35fdd92008-12-10 05:08:54 +0000790#endif // GMOCK_HAS_PROTOBUF_
791
shiqiane35fdd92008-12-10 05:08:54 +0000792// Sample functions and functors for testing Invoke() and etc.
793int Nullary() { return 1; }
794
795class NullaryFunctor {
796 public:
797 int operator()() { return 2; }
798};
799
800bool g_done = false;
801void VoidNullary() { g_done = true; }
802
803class VoidNullaryFunctor {
804 public:
805 void operator()() { g_done = true; }
806};
807
808bool Unary(int x) { return x < 0; }
809
810const char* Plus1(const char* s) { return s + 1; }
811
812void VoidUnary(int n) { g_done = true; }
813
814bool ByConstRef(const std::string& s) { return s == "Hi"; }
815
816const double g_double = 0;
817bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
818
819std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT
820
821struct UnaryFunctor {
822 int operator()(bool x) { return x ? 1 : -1; }
823};
824
825const char* Binary(const char* input, short n) { return input + n; } // NOLINT
826
827void VoidBinary(int, char) { g_done = true; }
828
829int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
830
831void VoidTernary(int, char, bool) { g_done = true; }
832
833int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
834
835void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
836
837int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
838
839struct SumOf5Functor {
840 int operator()(int a, int b, int c, int d, int e) {
841 return a + b + c + d + e;
842 }
843};
844
845int SumOf6(int a, int b, int c, int d, int e, int f) {
846 return a + b + c + d + e + f;
847}
848
849struct SumOf6Functor {
850 int operator()(int a, int b, int c, int d, int e, int f) {
851 return a + b + c + d + e + f;
852 }
853};
854
855class Foo {
856 public:
857 Foo() : value_(123) {}
858
859 int Nullary() const { return value_; }
860 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
861 std::string Binary(const std::string& str, char c) const { return str + c; }
862 int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
863 int SumOf4(int a, int b, int c, int d) const {
864 return a + b + c + d + value_;
865 }
866 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
867 int SumOf6(int a, int b, int c, int d, int e, int f) {
868 return a + b + c + d + e + f;
869 }
870 private:
871 int value_;
872};
873
874// Tests InvokeWithoutArgs(function).
875TEST(InvokeWithoutArgsTest, Function) {
876 // As an action that takes one argument.
877 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
878 EXPECT_EQ(1, a.Perform(make_tuple(2)));
879
880 // As an action that takes two arguments.
881 Action<short(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
882 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
883
884 // As an action that returns void.
885 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
886 g_done = false;
887 a3.Perform(make_tuple(1));
888 EXPECT_TRUE(g_done);
889}
890
891// Tests InvokeWithoutArgs(functor).
892TEST(InvokeWithoutArgsTest, Functor) {
893 // As an action that takes no argument.
894 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
895 EXPECT_EQ(2, a.Perform(make_tuple()));
896
897 // As an action that takes three arguments.
898 Action<short(int, double, char)> a2 = // NOLINT
899 InvokeWithoutArgs(NullaryFunctor());
900 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
901
902 // As an action that returns void.
903 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
904 g_done = false;
905 a3.Perform(make_tuple());
906 EXPECT_TRUE(g_done);
907}
908
909// Tests InvokeWithoutArgs(obj_ptr, method).
910TEST(InvokeWithoutArgsTest, Method) {
911 Foo foo;
912 Action<int(bool, char)> a = // NOLINT
913 InvokeWithoutArgs(&foo, &Foo::Nullary);
914 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
915}
916
917// Tests using IgnoreResult() on a polymorphic action.
918TEST(IgnoreResultTest, PolymorphicAction) {
919 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
920 a.Perform(make_tuple(1));
921}
922
923// Tests using IgnoreResult() on a monomorphic action.
924
925int ReturnOne() {
926 g_done = true;
927 return 1;
928}
929
930TEST(IgnoreResultTest, MonomorphicAction) {
931 g_done = false;
932 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
933 a.Perform(make_tuple());
934 EXPECT_TRUE(g_done);
935}
936
937// Tests using IgnoreResult() on an action that returns a class type.
938
939MyClass ReturnMyClass(double x) {
940 g_done = true;
941 return MyClass();
942}
943
944TEST(IgnoreResultTest, ActionReturningClass) {
945 g_done = false;
946 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
947 a.Perform(make_tuple(2));
948 EXPECT_TRUE(g_done);
949}
950
951TEST(AssignTest, Int) {
952 int x = 0;
953 Action<void(int)> a = Assign(&x, 5);
954 a.Perform(make_tuple(0));
955 EXPECT_EQ(5, x);
956}
957
958TEST(AssignTest, String) {
959 ::std::string x;
960 Action<void(void)> a = Assign(&x, "Hello, world");
961 a.Perform(make_tuple());
962 EXPECT_EQ("Hello, world", x);
963}
964
965TEST(AssignTest, CompatibleTypes) {
966 double x = 0;
967 Action<void(int)> a = Assign(&x, 5);
968 a.Perform(make_tuple(0));
969 EXPECT_DOUBLE_EQ(5, x);
970}
971
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000972#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000973
shiqiane35fdd92008-12-10 05:08:54 +0000974class SetErrnoAndReturnTest : public testing::Test {
975 protected:
976 virtual void SetUp() { errno = 0; }
977 virtual void TearDown() { errno = 0; }
978};
979
980TEST_F(SetErrnoAndReturnTest, Int) {
981 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
982 EXPECT_EQ(-5, a.Perform(make_tuple()));
983 EXPECT_EQ(ENOTTY, errno);
984}
985
986TEST_F(SetErrnoAndReturnTest, Ptr) {
987 int x;
988 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
989 EXPECT_EQ(&x, a.Perform(make_tuple()));
990 EXPECT_EQ(ENOTTY, errno);
991}
992
993TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
994 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
995 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
996 EXPECT_EQ(EINVAL, errno);
997}
998
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000999#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001000
zhanyong.wana18423e2009-07-22 23:58:19 +00001001// Tests ByRef().
1002
1003// Tests that ReferenceWrapper<T> is copyable.
1004TEST(ByRefTest, IsCopyable) {
1005 const std::string s1 = "Hi";
1006 const std::string s2 = "Hello";
1007
1008 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper = ByRef(s1);
1009 const std::string& r1 = ref_wrapper;
1010 EXPECT_EQ(&s1, &r1);
1011
1012 // Assigns a new value to ref_wrapper.
1013 ref_wrapper = ByRef(s2);
1014 const std::string& r2 = ref_wrapper;
1015 EXPECT_EQ(&s2, &r2);
1016
1017 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = ByRef(s1);
1018 // Copies ref_wrapper1 to ref_wrapper.
1019 ref_wrapper = ref_wrapper1;
1020 const std::string& r3 = ref_wrapper;
1021 EXPECT_EQ(&s1, &r3);
1022}
1023
1024// Tests using ByRef() on a const value.
1025TEST(ByRefTest, ConstValue) {
1026 const int n = 0;
1027 // int& ref = ByRef(n); // This shouldn't compile - we have a
1028 // negative compilation test to catch it.
1029 const int& const_ref = ByRef(n);
1030 EXPECT_EQ(&n, &const_ref);
1031}
1032
1033// Tests using ByRef() on a non-const value.
1034TEST(ByRefTest, NonConstValue) {
1035 int n = 0;
1036
1037 // ByRef(n) can be used as either an int&,
1038 int& ref = ByRef(n);
1039 EXPECT_EQ(&n, &ref);
1040
1041 // or a const int&.
1042 const int& const_ref = ByRef(n);
1043 EXPECT_EQ(&n, &const_ref);
1044}
1045
1046// Tests explicitly specifying the type when using ByRef().
1047TEST(ByRefTest, ExplicitType) {
1048 int n = 0;
1049 const int& r1 = ByRef<const int>(n);
1050 EXPECT_EQ(&n, &r1);
1051
1052 // ByRef<char>(n); // This shouldn't compile - we have a negative
1053 // compilation test to catch it.
1054
1055 Derived d;
1056 Derived& r2 = ByRef<Derived>(d);
1057 EXPECT_EQ(&d, &r2);
1058
1059 const Derived& r3 = ByRef<const Derived>(d);
1060 EXPECT_EQ(&d, &r3);
1061
1062 Base& r4 = ByRef<Base>(d);
1063 EXPECT_EQ(&d, &r4);
1064
1065 const Base& r5 = ByRef<const Base>(d);
1066 EXPECT_EQ(&d, &r5);
1067
1068 // The following shouldn't compile - we have a negative compilation
1069 // test for it.
1070 //
1071 // Base b;
1072 // ByRef<Derived>(b);
1073}
1074
1075// Tests that Google Mock prints expression ByRef(x) as a reference to x.
1076TEST(ByRefTest, PrintsCorrectly) {
1077 int n = 42;
1078 ::std::stringstream expected, actual;
1079 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1080 testing::internal::UniversalPrint(ByRef(n), &actual);
1081 EXPECT_EQ(expected.str(), actual.str());
1082}
1083
shiqiane35fdd92008-12-10 05:08:54 +00001084} // Unnamed namespace