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