blob: d3d96c6d078039f070168284e5ba247336b2ba5e [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
518// Tests that ReturnNull() returns NULL in a pointer-returning function.
519TEST(ReturnNullTest, WorksInPointerReturningFunction) {
520 const Action<int*()> a1 = ReturnNull();
521 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
522
523 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
524 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
525}
526
527// Tests that ReturnRef(v) works for reference types.
528TEST(ReturnRefTest, WorksForReference) {
529 const int n = 0;
530 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
531
532 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
533}
534
535// Tests that ReturnRef(v) is covariant.
536TEST(ReturnRefTest, IsCovariant) {
537 Base base;
538 Derived derived;
539 Action<Base&()> a = ReturnRef(base);
540 EXPECT_EQ(&base, &a.Perform(make_tuple()));
541
542 a = ReturnRef(derived);
543 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
544}
545
546// Tests that DoDefault() does the default action for the mock method.
547
548class MyClass {};
549
550class MockClass {
551 public:
552 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
553 MOCK_METHOD0(Foo, MyClass());
554};
555
556// Tests that DoDefault() returns the built-in default value for the
557// return type by default.
558TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
559 MockClass mock;
560 EXPECT_CALL(mock, IntFunc(_))
561 .WillOnce(DoDefault());
562 EXPECT_EQ(0, mock.IntFunc(true));
563}
564
shiqiane35fdd92008-12-10 05:08:54 +0000565// Tests that DoDefault() aborts the process when there is no built-in
566// default value for the return type.
567TEST(DoDefaultDeathTest, DiesForUnknowType) {
568 MockClass mock;
569 EXPECT_CALL(mock, Foo())
570 .WillRepeatedly(DoDefault());
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000571 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000572 mock.Foo();
573 }, "");
574}
575
576// Tests that using DoDefault() inside a composite action leads to a
577// run-time error.
578
579void VoidFunc(bool flag) {}
580
581TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
582 MockClass mock;
583 EXPECT_CALL(mock, IntFunc(_))
584 .WillRepeatedly(DoAll(Invoke(VoidFunc),
585 DoDefault()));
586
587 // Ideally we should verify the error message as well. Sadly,
588 // EXPECT_DEATH() can only capture stderr, while Google Mock's
589 // errors are printed on stdout. Therefore we have to settle for
590 // not verifying the message.
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000591 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000592 mock.IntFunc(true);
593 }, "");
594}
595
shiqiane35fdd92008-12-10 05:08:54 +0000596// Tests that DoDefault() returns the default value set by
597// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
598TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
599 DefaultValue<int>::Set(1);
600 MockClass mock;
601 EXPECT_CALL(mock, IntFunc(_))
602 .WillOnce(DoDefault());
603 EXPECT_EQ(1, mock.IntFunc(false));
604 DefaultValue<int>::Clear();
605}
606
607// Tests that DoDefault() does the action specified by ON_CALL().
608TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
609 MockClass mock;
610 ON_CALL(mock, IntFunc(_))
611 .WillByDefault(Return(2));
612 EXPECT_CALL(mock, IntFunc(_))
613 .WillOnce(DoDefault());
614 EXPECT_EQ(2, mock.IntFunc(false));
615}
616
617// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
618TEST(DoDefaultTest, CannotBeUsedInOnCall) {
619 MockClass mock;
620 EXPECT_NONFATAL_FAILURE({ // NOLINT
621 ON_CALL(mock, IntFunc(_))
622 .WillByDefault(DoDefault());
623 }, "DoDefault() cannot be used in ON_CALL()");
624}
625
626// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
627// the N-th (0-based) argument to v.
628TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
629 typedef void MyFunction(bool, int*, char*);
630 Action<MyFunction> a = SetArgumentPointee<1>(2);
631
632 int n = 0;
633 char ch = '\0';
634 a.Perform(make_tuple(true, &n, &ch));
635 EXPECT_EQ(2, n);
636 EXPECT_EQ('\0', ch);
637
638 a = SetArgumentPointee<2>('a');
639 n = 0;
640 ch = '\0';
641 a.Perform(make_tuple(true, &n, &ch));
642 EXPECT_EQ(0, n);
643 EXPECT_EQ('a', ch);
644}
645
646#if GMOCK_HAS_PROTOBUF_
647
zhanyong.wanc6a41232009-05-13 23:38:40 +0000648// Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
649// variable pointed to by the N-th (0-based) argument to proto_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000650TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
shiqiane35fdd92008-12-10 05:08:54 +0000651 TestMessage* const msg = new TestMessage;
652 msg->set_member("yes");
653 TestMessage orig_msg;
654 orig_msg.CopyFrom(*msg);
655
zhanyong.wanc6a41232009-05-13 23:38:40 +0000656 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000657 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
658 // s.t. the action works even when the original proto_buffer has
659 // died. We ensure this behavior by deleting msg before using the
660 // action.
661 delete msg;
662
663 TestMessage dest;
664 EXPECT_FALSE(orig_msg.Equals(dest));
665 a.Perform(make_tuple(true, &dest));
666 EXPECT_TRUE(orig_msg.Equals(dest));
667}
668
zhanyong.wanc6a41232009-05-13 23:38:40 +0000669// Tests that SetArgumentPointee<N>(proto_buffer) sets the
670// ::ProtocolMessage variable pointed to by the N-th (0-based)
671// argument to proto_buffer.
672TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
673 TestMessage* const msg = new TestMessage;
674 msg->set_member("yes");
675 TestMessage orig_msg;
676 orig_msg.CopyFrom(*msg);
677
678 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
679 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
680 // s.t. the action works even when the original proto_buffer has
681 // died. We ensure this behavior by deleting msg before using the
682 // action.
683 delete msg;
684
685 TestMessage dest;
686 ::ProtocolMessage* const dest_base = &dest;
687 EXPECT_FALSE(orig_msg.Equals(dest));
688 a.Perform(make_tuple(true, dest_base));
689 EXPECT_TRUE(orig_msg.Equals(dest));
690}
691
692// Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
693// protobuf variable pointed to by the N-th (0-based) argument to
694// proto2_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000695TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
696 using testing::internal::FooMessage;
shiqiane35fdd92008-12-10 05:08:54 +0000697 FooMessage* const msg = new FooMessage;
698 msg->set_int_field(2);
699 msg->set_string_field("hi");
700 FooMessage orig_msg;
701 orig_msg.CopyFrom(*msg);
702
zhanyong.wanc6a41232009-05-13 23:38:40 +0000703 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000704 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
705 // proto2_buffer s.t. the action works even when the original
706 // proto2_buffer has died. We ensure this behavior by deleting msg
707 // before using the action.
708 delete msg;
709
710 FooMessage dest;
711 dest.set_int_field(0);
712 a.Perform(make_tuple(true, &dest));
713 EXPECT_EQ(2, dest.int_field());
714 EXPECT_EQ("hi", dest.string_field());
715}
716
zhanyong.wanc6a41232009-05-13 23:38:40 +0000717// Tests that SetArgumentPointee<N>(proto2_buffer) sets the
718// proto2::Message variable pointed to by the N-th (0-based) argument
719// to proto2_buffer.
720TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
721 using testing::internal::FooMessage;
722 FooMessage* const msg = new FooMessage;
723 msg->set_int_field(2);
724 msg->set_string_field("hi");
725 FooMessage orig_msg;
726 orig_msg.CopyFrom(*msg);
727
728 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
729 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
730 // proto2_buffer s.t. the action works even when the original
731 // proto2_buffer has died. We ensure this behavior by deleting msg
732 // before using the action.
733 delete msg;
734
735 FooMessage dest;
736 dest.set_int_field(0);
737 ::proto2::Message* const dest_base = &dest;
738 a.Perform(make_tuple(true, dest_base));
739 EXPECT_EQ(2, dest.int_field());
740 EXPECT_EQ("hi", dest.string_field());
741}
742
shiqiane35fdd92008-12-10 05:08:54 +0000743#endif // GMOCK_HAS_PROTOBUF_
744
shiqiane35fdd92008-12-10 05:08:54 +0000745// Sample functions and functors for testing Invoke() and etc.
746int Nullary() { return 1; }
747
748class NullaryFunctor {
749 public:
750 int operator()() { return 2; }
751};
752
753bool g_done = false;
754void VoidNullary() { g_done = true; }
755
756class VoidNullaryFunctor {
757 public:
758 void operator()() { g_done = true; }
759};
760
761bool Unary(int x) { return x < 0; }
762
763const char* Plus1(const char* s) { return s + 1; }
764
765void VoidUnary(int n) { g_done = true; }
766
767bool ByConstRef(const std::string& s) { return s == "Hi"; }
768
769const double g_double = 0;
770bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
771
772std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT
773
774struct UnaryFunctor {
775 int operator()(bool x) { return x ? 1 : -1; }
776};
777
778const char* Binary(const char* input, short n) { return input + n; } // NOLINT
779
780void VoidBinary(int, char) { g_done = true; }
781
782int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
783
784void VoidTernary(int, char, bool) { g_done = true; }
785
786int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
787
788void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
789
790int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
791
792struct SumOf5Functor {
793 int operator()(int a, int b, int c, int d, int e) {
794 return a + b + c + d + e;
795 }
796};
797
798int SumOf6(int a, int b, int c, int d, int e, int f) {
799 return a + b + c + d + e + f;
800}
801
802struct SumOf6Functor {
803 int operator()(int a, int b, int c, int d, int e, int f) {
804 return a + b + c + d + e + f;
805 }
806};
807
808class Foo {
809 public:
810 Foo() : value_(123) {}
811
812 int Nullary() const { return value_; }
813 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
814 std::string Binary(const std::string& str, char c) const { return str + c; }
815 int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
816 int SumOf4(int a, int b, int c, int d) const {
817 return a + b + c + d + value_;
818 }
819 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
820 int SumOf6(int a, int b, int c, int d, int e, int f) {
821 return a + b + c + d + e + f;
822 }
823 private:
824 int value_;
825};
826
827// Tests InvokeWithoutArgs(function).
828TEST(InvokeWithoutArgsTest, Function) {
829 // As an action that takes one argument.
830 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
831 EXPECT_EQ(1, a.Perform(make_tuple(2)));
832
833 // As an action that takes two arguments.
834 Action<short(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
835 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
836
837 // As an action that returns void.
838 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
839 g_done = false;
840 a3.Perform(make_tuple(1));
841 EXPECT_TRUE(g_done);
842}
843
844// Tests InvokeWithoutArgs(functor).
845TEST(InvokeWithoutArgsTest, Functor) {
846 // As an action that takes no argument.
847 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
848 EXPECT_EQ(2, a.Perform(make_tuple()));
849
850 // As an action that takes three arguments.
851 Action<short(int, double, char)> a2 = // NOLINT
852 InvokeWithoutArgs(NullaryFunctor());
853 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
854
855 // As an action that returns void.
856 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
857 g_done = false;
858 a3.Perform(make_tuple());
859 EXPECT_TRUE(g_done);
860}
861
862// Tests InvokeWithoutArgs(obj_ptr, method).
863TEST(InvokeWithoutArgsTest, Method) {
864 Foo foo;
865 Action<int(bool, char)> a = // NOLINT
866 InvokeWithoutArgs(&foo, &Foo::Nullary);
867 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
868}
869
870// Tests using IgnoreResult() on a polymorphic action.
871TEST(IgnoreResultTest, PolymorphicAction) {
872 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
873 a.Perform(make_tuple(1));
874}
875
876// Tests using IgnoreResult() on a monomorphic action.
877
878int ReturnOne() {
879 g_done = true;
880 return 1;
881}
882
883TEST(IgnoreResultTest, MonomorphicAction) {
884 g_done = false;
885 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
886 a.Perform(make_tuple());
887 EXPECT_TRUE(g_done);
888}
889
890// Tests using IgnoreResult() on an action that returns a class type.
891
892MyClass ReturnMyClass(double x) {
893 g_done = true;
894 return MyClass();
895}
896
897TEST(IgnoreResultTest, ActionReturningClass) {
898 g_done = false;
899 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
900 a.Perform(make_tuple(2));
901 EXPECT_TRUE(g_done);
902}
903
904TEST(AssignTest, Int) {
905 int x = 0;
906 Action<void(int)> a = Assign(&x, 5);
907 a.Perform(make_tuple(0));
908 EXPECT_EQ(5, x);
909}
910
911TEST(AssignTest, String) {
912 ::std::string x;
913 Action<void(void)> a = Assign(&x, "Hello, world");
914 a.Perform(make_tuple());
915 EXPECT_EQ("Hello, world", x);
916}
917
918TEST(AssignTest, CompatibleTypes) {
919 double x = 0;
920 Action<void(int)> a = Assign(&x, 5);
921 a.Perform(make_tuple(0));
922 EXPECT_DOUBLE_EQ(5, x);
923}
924
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000925#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000926
shiqiane35fdd92008-12-10 05:08:54 +0000927class SetErrnoAndReturnTest : public testing::Test {
928 protected:
929 virtual void SetUp() { errno = 0; }
930 virtual void TearDown() { errno = 0; }
931};
932
933TEST_F(SetErrnoAndReturnTest, Int) {
934 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
935 EXPECT_EQ(-5, a.Perform(make_tuple()));
936 EXPECT_EQ(ENOTTY, errno);
937}
938
939TEST_F(SetErrnoAndReturnTest, Ptr) {
940 int x;
941 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
942 EXPECT_EQ(&x, a.Perform(make_tuple()));
943 EXPECT_EQ(ENOTTY, errno);
944}
945
946TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
947 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
948 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
949 EXPECT_EQ(EINVAL, errno);
950}
951
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000952#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000953
zhanyong.wana18423e2009-07-22 23:58:19 +0000954// Tests ByRef().
955
956// Tests that ReferenceWrapper<T> is copyable.
957TEST(ByRefTest, IsCopyable) {
958 const std::string s1 = "Hi";
959 const std::string s2 = "Hello";
960
961 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper = ByRef(s1);
962 const std::string& r1 = ref_wrapper;
963 EXPECT_EQ(&s1, &r1);
964
965 // Assigns a new value to ref_wrapper.
966 ref_wrapper = ByRef(s2);
967 const std::string& r2 = ref_wrapper;
968 EXPECT_EQ(&s2, &r2);
969
970 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = ByRef(s1);
971 // Copies ref_wrapper1 to ref_wrapper.
972 ref_wrapper = ref_wrapper1;
973 const std::string& r3 = ref_wrapper;
974 EXPECT_EQ(&s1, &r3);
975}
976
977// Tests using ByRef() on a const value.
978TEST(ByRefTest, ConstValue) {
979 const int n = 0;
980 // int& ref = ByRef(n); // This shouldn't compile - we have a
981 // negative compilation test to catch it.
982 const int& const_ref = ByRef(n);
983 EXPECT_EQ(&n, &const_ref);
984}
985
986// Tests using ByRef() on a non-const value.
987TEST(ByRefTest, NonConstValue) {
988 int n = 0;
989
990 // ByRef(n) can be used as either an int&,
991 int& ref = ByRef(n);
992 EXPECT_EQ(&n, &ref);
993
994 // or a const int&.
995 const int& const_ref = ByRef(n);
996 EXPECT_EQ(&n, &const_ref);
997}
998
999// Tests explicitly specifying the type when using ByRef().
1000TEST(ByRefTest, ExplicitType) {
1001 int n = 0;
1002 const int& r1 = ByRef<const int>(n);
1003 EXPECT_EQ(&n, &r1);
1004
1005 // ByRef<char>(n); // This shouldn't compile - we have a negative
1006 // compilation test to catch it.
1007
1008 Derived d;
1009 Derived& r2 = ByRef<Derived>(d);
1010 EXPECT_EQ(&d, &r2);
1011
1012 const Derived& r3 = ByRef<const Derived>(d);
1013 EXPECT_EQ(&d, &r3);
1014
1015 Base& r4 = ByRef<Base>(d);
1016 EXPECT_EQ(&d, &r4);
1017
1018 const Base& r5 = ByRef<const Base>(d);
1019 EXPECT_EQ(&d, &r5);
1020
1021 // The following shouldn't compile - we have a negative compilation
1022 // test for it.
1023 //
1024 // Base b;
1025 // ByRef<Derived>(b);
1026}
1027
1028// Tests that Google Mock prints expression ByRef(x) as a reference to x.
1029TEST(ByRefTest, PrintsCorrectly) {
1030 int n = 42;
1031 ::std::stringstream expected, actual;
1032 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1033 testing::internal::UniversalPrint(ByRef(n), &actual);
1034 EXPECT_EQ(expected.str(), actual.str());
1035}
1036
shiqiane35fdd92008-12-10 05:08:54 +00001037} // Unnamed namespace