blob: 50cc6f98d03d50af3d2d21c1e473852093c10f54 [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;
zhanyong.wane3bd0982010-07-03 00:16:42 +000071using testing::ReturnRefOfCopy;
shiqiane35fdd92008-12-10 05:08:54 +000072using testing::SetArgumentPointee;
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000073
zhanyong.wanf7af24c2009-09-24 21:17:24 +000074#if !GTEST_OS_WINDOWS_MOBILE
shiqiane35fdd92008-12-10 05:08:54 +000075using testing::SetErrnoAndReturn;
zhanyong.wanf7af24c2009-09-24 21:17:24 +000076#endif
shiqiane35fdd92008-12-10 05:08:54 +000077
zhanyong.wan02f71062010-05-10 17:14:29 +000078#if GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +000079using testing::internal::TestMessage;
zhanyong.wan02f71062010-05-10 17:14:29 +000080#endif // GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +000081
82// Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
83TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
84 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL);
85 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL);
86 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL);
87}
88
zhanyong.wan5b95fa72009-01-27 22:28:45 +000089// Tests that BuiltInDefaultValue<T*>::Exists() return true.
90TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
91 EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
92 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
93 EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
94}
95
shiqiane35fdd92008-12-10 05:08:54 +000096// Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
97// built-in numeric type.
98TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
zhanyong.wan32de5f52009-12-23 00:13:23 +000099 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000100 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
101 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000102#if GMOCK_HAS_SIGNED_WCHAR_T_
zhanyong.wan32de5f52009-12-23 00:13:23 +0000103 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000104 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000105#endif
106#if GMOCK_WCHAR_T_IS_NATIVE_
shiqiane35fdd92008-12-10 05:08:54 +0000107 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000108#endif
zhanyong.wan32de5f52009-12-23 00:13:23 +0000109 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000110 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT
111 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000112 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000113 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
114 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000115 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000116 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT
117 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000118 EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000119 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
120 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
121 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
122}
123
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000124// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
125// built-in numeric type.
126TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
127 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
128 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
129 EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000130#if GMOCK_HAS_SIGNED_WCHAR_T_
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000131 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
132 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000133#endif
134#if GMOCK_WCHAR_T_IS_NATIVE_
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000135 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000136#endif
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000137 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT
138 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT
139 EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT
140 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
141 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
142 EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
143 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT
144 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT
145 EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT
146 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
147 EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists());
148 EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
149 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
150}
151
shiqiane35fdd92008-12-10 05:08:54 +0000152// Tests that BuiltInDefaultValue<bool>::Get() returns false.
153TEST(BuiltInDefaultValueTest, IsFalseForBool) {
154 EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
155}
156
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000157// Tests that BuiltInDefaultValue<bool>::Exists() returns true.
158TEST(BuiltInDefaultValueTest, BoolExists) {
159 EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
160}
161
shiqiane35fdd92008-12-10 05:08:54 +0000162// Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
163// string type.
164TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
165#if GTEST_HAS_GLOBAL_STRING
166 EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get());
167#endif // GTEST_HAS_GLOBAL_STRING
168
shiqiane35fdd92008-12-10 05:08:54 +0000169 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000170}
171
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000172// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
173// string type.
174TEST(BuiltInDefaultValueTest, ExistsForString) {
175#if GTEST_HAS_GLOBAL_STRING
176 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
177#endif // GTEST_HAS_GLOBAL_STRING
178
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000179 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000180}
181
shiqiane35fdd92008-12-10 05:08:54 +0000182// Tests that BuiltInDefaultValue<const T>::Get() returns the same
183// value as BuiltInDefaultValue<T>::Get() does.
184TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
185 EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
186 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
187 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL);
188 EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
189}
190
191// Tests that BuiltInDefaultValue<T>::Get() aborts the program with
192// the correct error message when T is a user-defined type.
193struct UserType {
194 UserType() : value(0) {}
195
196 int value;
197};
198
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000199TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) {
200 EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists());
201}
202
shiqiane35fdd92008-12-10 05:08:54 +0000203// Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
204TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000205 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000206 BuiltInDefaultValue<int&>::Get();
207 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000208 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000209 BuiltInDefaultValue<const char&>::Get();
210 }, "");
211}
212
213TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000214 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000215 BuiltInDefaultValue<UserType>::Get();
216 }, "");
217}
218
shiqiane35fdd92008-12-10 05:08:54 +0000219// Tests that DefaultValue<T>::IsSet() is false initially.
220TEST(DefaultValueTest, IsInitiallyUnset) {
221 EXPECT_FALSE(DefaultValue<int>::IsSet());
222 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
223}
224
225// Tests that DefaultValue<T> can be set and then unset.
226TEST(DefaultValueTest, CanBeSetAndUnset) {
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000227 EXPECT_TRUE(DefaultValue<int>::Exists());
228 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
229
shiqiane35fdd92008-12-10 05:08:54 +0000230 DefaultValue<int>::Set(1);
231 DefaultValue<const UserType>::Set(UserType());
232
233 EXPECT_EQ(1, DefaultValue<int>::Get());
234 EXPECT_EQ(0, DefaultValue<const UserType>::Get().value);
235
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000236 EXPECT_TRUE(DefaultValue<int>::Exists());
237 EXPECT_TRUE(DefaultValue<const UserType>::Exists());
238
shiqiane35fdd92008-12-10 05:08:54 +0000239 DefaultValue<int>::Clear();
240 DefaultValue<const UserType>::Clear();
241
242 EXPECT_FALSE(DefaultValue<int>::IsSet());
243 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000244
245 EXPECT_TRUE(DefaultValue<int>::Exists());
246 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000247}
248
249// Tests that DefaultValue<T>::Get() returns the
250// BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
251// false.
252TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
253 EXPECT_FALSE(DefaultValue<int>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000254 EXPECT_TRUE(DefaultValue<int>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000255 EXPECT_FALSE(DefaultValue<UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000256 EXPECT_FALSE(DefaultValue<UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000257
258 EXPECT_EQ(0, DefaultValue<int>::Get());
259
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000260 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000261 DefaultValue<UserType>::Get();
262 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000263}
264
265// Tests that DefaultValue<void>::Get() returns void.
266TEST(DefaultValueTest, GetWorksForVoid) {
267 return DefaultValue<void>::Get();
268}
269
270// Tests using DefaultValue with a reference type.
271
272// Tests that DefaultValue<T&>::IsSet() is false initially.
273TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
274 EXPECT_FALSE(DefaultValue<int&>::IsSet());
275 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
276}
277
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000278// Tests that DefaultValue<T&>::Exists is false initiallly.
279TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
280 EXPECT_FALSE(DefaultValue<int&>::Exists());
281 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
282}
283
shiqiane35fdd92008-12-10 05:08:54 +0000284// Tests that DefaultValue<T&> can be set and then unset.
285TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
286 int n = 1;
287 DefaultValue<const int&>::Set(n);
288 UserType u;
289 DefaultValue<UserType&>::Set(u);
290
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000291 EXPECT_TRUE(DefaultValue<const int&>::Exists());
292 EXPECT_TRUE(DefaultValue<UserType&>::Exists());
293
shiqiane35fdd92008-12-10 05:08:54 +0000294 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
295 EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get()));
296
297 DefaultValue<const int&>::Clear();
298 DefaultValue<UserType&>::Clear();
299
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000300 EXPECT_FALSE(DefaultValue<const int&>::Exists());
301 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
302
shiqiane35fdd92008-12-10 05:08:54 +0000303 EXPECT_FALSE(DefaultValue<const int&>::IsSet());
304 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
305}
306
307// Tests that DefaultValue<T&>::Get() returns the
308// BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
309// false.
310TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
311 EXPECT_FALSE(DefaultValue<int&>::IsSet());
312 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
313
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000314 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000315 DefaultValue<int&>::Get();
316 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000317 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000318 DefaultValue<UserType>::Get();
319 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000320}
321
322// Tests that ActionInterface can be implemented by defining the
323// Perform method.
324
325typedef int MyFunction(bool, int);
326
327class MyActionImpl : public ActionInterface<MyFunction> {
328 public:
329 virtual int Perform(const tuple<bool, int>& args) {
330 return get<0>(args) ? get<1>(args) : 0;
331 }
332};
333
334TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
335 MyActionImpl my_action_impl;
336
337 EXPECT_FALSE(my_action_impl.IsDoDefault());
338}
339
340TEST(ActionInterfaceTest, MakeAction) {
341 Action<MyFunction> action = MakeAction(new MyActionImpl);
342
343 // When exercising the Perform() method of Action<F>, we must pass
344 // it a tuple whose size and type are compatible with F's argument
345 // types. For example, if F is int(), then Perform() takes a
346 // 0-tuple; if F is void(bool, int), then Perform() takes a
347 // tuple<bool, int>, and so on.
348 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
349}
350
351// Tests that Action<F> can be contructed from a pointer to
352// ActionInterface<F>.
353TEST(ActionTest, CanBeConstructedFromActionInterface) {
354 Action<MyFunction> action(new MyActionImpl);
355}
356
357// Tests that Action<F> delegates actual work to ActionInterface<F>.
358TEST(ActionTest, DelegatesWorkToActionInterface) {
359 const Action<MyFunction> action(new MyActionImpl);
360
361 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
362 EXPECT_EQ(0, action.Perform(make_tuple(false, 1)));
363}
364
365// Tests that Action<F> can be copied.
366TEST(ActionTest, IsCopyable) {
367 Action<MyFunction> a1(new MyActionImpl);
368 Action<MyFunction> a2(a1); // Tests the copy constructor.
369
370 // a1 should continue to work after being copied from.
371 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
372 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
373
374 // a2 should work like the action it was copied from.
375 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
376 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
377
378 a2 = a1; // Tests the assignment operator.
379
380 // a1 should continue to work after being copied from.
381 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
382 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
383
384 // a2 should work like the action it was copied from.
385 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
386 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
387}
388
389// Tests that an Action<From> object can be converted to a
390// compatible Action<To> object.
391
392class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
393 public:
394 virtual bool Perform(const tuple<int>& arg) {
395 return get<0>(arg) != 0;
396 }
397};
398
zhanyong.wan95b12332009-09-25 18:55:50 +0000399#if !GTEST_OS_SYMBIAN
400// Compiling this test on Nokia's Symbian compiler fails with:
401// 'Result' is not a member of class 'testing::internal::Function<int>'
402// (point of instantiation: '@unnamed@gmock_actions_test_cc@::
403// ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()')
404// with no obvious fix.
shiqiane35fdd92008-12-10 05:08:54 +0000405TEST(ActionTest, CanBeConvertedToOtherActionType) {
406 const Action<bool(int)> a1(new IsNotZero); // NOLINT
407 const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT
408 EXPECT_EQ(1, a2.Perform(make_tuple('a')));
409 EXPECT_EQ(0, a2.Perform(make_tuple('\0')));
410}
zhanyong.wan95b12332009-09-25 18:55:50 +0000411#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +0000412
413// The following two classes are for testing MakePolymorphicAction().
414
415// Implements a polymorphic action that returns the second of the
416// arguments it receives.
417class ReturnSecondArgumentAction {
418 public:
419 // We want to verify that MakePolymorphicAction() can work with a
420 // polymorphic action whose Perform() method template is either
421 // const or not. This lets us verify the non-const case.
422 template <typename Result, typename ArgumentTuple>
423 Result Perform(const ArgumentTuple& args) { return get<1>(args); }
424};
425
426// Implements a polymorphic action that can be used in a nullary
427// function to return 0.
428class ReturnZeroFromNullaryFunctionAction {
429 public:
430 // For testing that MakePolymorphicAction() works when the
431 // implementation class' Perform() method template takes only one
432 // template parameter.
433 //
434 // We want to verify that MakePolymorphicAction() can work with a
435 // polymorphic action whose Perform() method template is either
436 // const or not. This lets us verify the const case.
437 template <typename Result>
438 Result Perform(const tuple<>&) const { return 0; }
439};
440
441// These functions verify that MakePolymorphicAction() returns a
442// PolymorphicAction<T> where T is the argument's type.
443
444PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
445 return MakePolymorphicAction(ReturnSecondArgumentAction());
446}
447
448PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
449ReturnZeroFromNullaryFunction() {
450 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
451}
452
453// Tests that MakePolymorphicAction() turns a polymorphic action
454// implementation class into a polymorphic action.
455TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
456 Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT
457 EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0)));
458}
459
460// Tests that MakePolymorphicAction() works when the implementation
461// class' Perform() method template has only one template parameter.
462TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
463 Action<int()> a1 = ReturnZeroFromNullaryFunction();
464 EXPECT_EQ(0, a1.Perform(make_tuple()));
465
466 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
467 EXPECT_TRUE(a2.Perform(make_tuple()) == NULL);
468}
469
470// Tests that Return() works as an action for void-returning
471// functions.
472TEST(ReturnTest, WorksForVoid) {
473 const Action<void(int)> ret = Return(); // NOLINT
474 return ret.Perform(make_tuple(1));
475}
476
477// Tests that Return(v) returns v.
478TEST(ReturnTest, ReturnsGivenValue) {
479 Action<int()> ret = Return(1); // NOLINT
480 EXPECT_EQ(1, ret.Perform(make_tuple()));
481
482 ret = Return(-5);
483 EXPECT_EQ(-5, ret.Perform(make_tuple()));
484}
485
486// Tests that Return("string literal") works.
487TEST(ReturnTest, AcceptsStringLiteral) {
488 Action<const char*()> a1 = Return("Hello");
489 EXPECT_STREQ("Hello", a1.Perform(make_tuple()));
490
491 Action<std::string()> a2 = Return("world");
492 EXPECT_EQ("world", a2.Perform(make_tuple()));
493}
494
495// Tests that Return(v) is covaraint.
496
497struct Base {
498 bool operator==(const Base&) { return true; }
499};
500
501struct Derived : public Base {
502 bool operator==(const Derived&) { return true; }
503};
504
505TEST(ReturnTest, IsCovariant) {
506 Base base;
507 Derived derived;
508 Action<Base*()> ret = Return(&base);
509 EXPECT_EQ(&base, ret.Perform(make_tuple()));
510
511 ret = Return(&derived);
512 EXPECT_EQ(&derived, ret.Perform(make_tuple()));
513}
514
vladloseva070cbd2009-11-18 00:09:28 +0000515// Tests that the type of the value passed into Return is converted into T
516// when the action is cast to Action<T(...)> rather than when the action is
517// performed. See comments on testing::internal::ReturnAction in
518// gmock-actions.h for more information.
519class FromType {
520 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000521 FromType(bool* is_converted) : converted_(is_converted) {}
vladloseva070cbd2009-11-18 00:09:28 +0000522 bool* converted() const { return converted_; }
523
524 private:
525 bool* const converted_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000526
527 GTEST_DISALLOW_ASSIGN_(FromType);
vladloseva070cbd2009-11-18 00:09:28 +0000528};
529
530class ToType {
531 public:
532 ToType(const FromType& x) { *x.converted() = true; }
533};
534
535TEST(ReturnTest, ConvertsArgumentWhenConverted) {
536 bool converted = false;
537 FromType x(&converted);
538 Action<ToType()> action(Return(x));
539 EXPECT_TRUE(converted) << "Return must convert its argument in its own "
540 << "conversion operator.";
541 converted = false;
542 action.Perform(tuple<>());
543 EXPECT_FALSE(converted) << "Action must NOT convert its argument "
544 << "when performed." ;
545}
546
vladloseva070cbd2009-11-18 00:09:28 +0000547class DestinationType {};
548
549class SourceType {
550 public:
551 // Note: a non-const typecast operator.
552 operator DestinationType() { return DestinationType(); }
553};
554
555TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
556 SourceType s;
557 Action<DestinationType()> action(Return(s));
558}
vladloseva070cbd2009-11-18 00:09:28 +0000559
shiqiane35fdd92008-12-10 05:08:54 +0000560// Tests that ReturnNull() returns NULL in a pointer-returning function.
561TEST(ReturnNullTest, WorksInPointerReturningFunction) {
562 const Action<int*()> a1 = ReturnNull();
563 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
564
565 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
566 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
567}
568
569// Tests that ReturnRef(v) works for reference types.
570TEST(ReturnRefTest, WorksForReference) {
571 const int n = 0;
572 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
573
574 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
575}
576
577// Tests that ReturnRef(v) is covariant.
578TEST(ReturnRefTest, IsCovariant) {
579 Base base;
580 Derived derived;
581 Action<Base&()> a = ReturnRef(base);
582 EXPECT_EQ(&base, &a.Perform(make_tuple()));
583
584 a = ReturnRef(derived);
585 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
586}
587
zhanyong.wane3bd0982010-07-03 00:16:42 +0000588// Tests that ReturnRefOfCopy(v) works for reference types.
589TEST(ReturnRefOfCopyTest, WorksForReference) {
590 int n = 42;
591 const Action<const int&()> ret = ReturnRefOfCopy(n);
592
593 EXPECT_NE(&n, &ret.Perform(make_tuple()));
594 EXPECT_EQ(42, ret.Perform(make_tuple()));
595
596 n = 43;
597 EXPECT_NE(&n, &ret.Perform(make_tuple()));
598 EXPECT_EQ(42, ret.Perform(make_tuple()));
599}
600
601// Tests that ReturnRefOfCopy(v) is covariant.
602TEST(ReturnRefOfCopyTest, IsCovariant) {
603 Base base;
604 Derived derived;
605 Action<Base&()> a = ReturnRefOfCopy(base);
606 EXPECT_NE(&base, &a.Perform(make_tuple()));
607
608 a = ReturnRefOfCopy(derived);
609 EXPECT_NE(&derived, &a.Perform(make_tuple()));
610}
611
shiqiane35fdd92008-12-10 05:08:54 +0000612// Tests that DoDefault() does the default action for the mock method.
613
614class MyClass {};
615
616class MockClass {
617 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000618 MockClass() {}
619
shiqiane35fdd92008-12-10 05:08:54 +0000620 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
621 MOCK_METHOD0(Foo, MyClass());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000622
623 private:
624 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
shiqiane35fdd92008-12-10 05:08:54 +0000625};
626
627// Tests that DoDefault() returns the built-in default value for the
628// return type by default.
629TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
630 MockClass mock;
631 EXPECT_CALL(mock, IntFunc(_))
632 .WillOnce(DoDefault());
633 EXPECT_EQ(0, mock.IntFunc(true));
634}
635
shiqiane35fdd92008-12-10 05:08:54 +0000636// Tests that DoDefault() aborts the process when there is no built-in
637// default value for the return type.
638TEST(DoDefaultDeathTest, DiesForUnknowType) {
639 MockClass mock;
640 EXPECT_CALL(mock, Foo())
641 .WillRepeatedly(DoDefault());
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000642 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000643 mock.Foo();
644 }, "");
645}
646
647// Tests that using DoDefault() inside a composite action leads to a
648// run-time error.
649
zhanyong.wan32de5f52009-12-23 00:13:23 +0000650void VoidFunc(bool /* flag */) {}
shiqiane35fdd92008-12-10 05:08:54 +0000651
652TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
653 MockClass mock;
654 EXPECT_CALL(mock, IntFunc(_))
655 .WillRepeatedly(DoAll(Invoke(VoidFunc),
656 DoDefault()));
657
658 // Ideally we should verify the error message as well. Sadly,
659 // EXPECT_DEATH() can only capture stderr, while Google Mock's
660 // errors are printed on stdout. Therefore we have to settle for
661 // not verifying the message.
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000662 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000663 mock.IntFunc(true);
664 }, "");
665}
666
shiqiane35fdd92008-12-10 05:08:54 +0000667// Tests that DoDefault() returns the default value set by
668// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
669TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
670 DefaultValue<int>::Set(1);
671 MockClass mock;
672 EXPECT_CALL(mock, IntFunc(_))
673 .WillOnce(DoDefault());
674 EXPECT_EQ(1, mock.IntFunc(false));
675 DefaultValue<int>::Clear();
676}
677
678// Tests that DoDefault() does the action specified by ON_CALL().
679TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
680 MockClass mock;
681 ON_CALL(mock, IntFunc(_))
682 .WillByDefault(Return(2));
683 EXPECT_CALL(mock, IntFunc(_))
684 .WillOnce(DoDefault());
685 EXPECT_EQ(2, mock.IntFunc(false));
686}
687
688// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
689TEST(DoDefaultTest, CannotBeUsedInOnCall) {
690 MockClass mock;
691 EXPECT_NONFATAL_FAILURE({ // NOLINT
692 ON_CALL(mock, IntFunc(_))
693 .WillByDefault(DoDefault());
694 }, "DoDefault() cannot be used in ON_CALL()");
695}
696
697// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
698// the N-th (0-based) argument to v.
699TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
700 typedef void MyFunction(bool, int*, char*);
701 Action<MyFunction> a = SetArgumentPointee<1>(2);
702
703 int n = 0;
704 char ch = '\0';
705 a.Perform(make_tuple(true, &n, &ch));
706 EXPECT_EQ(2, n);
707 EXPECT_EQ('\0', ch);
708
709 a = SetArgumentPointee<2>('a');
710 n = 0;
711 ch = '\0';
712 a.Perform(make_tuple(true, &n, &ch));
713 EXPECT_EQ(0, n);
714 EXPECT_EQ('a', ch);
715}
716
zhanyong.wan02f71062010-05-10 17:14:29 +0000717#if GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +0000718
zhanyong.wanc6a41232009-05-13 23:38:40 +0000719// Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
720// variable pointed to by the N-th (0-based) argument to proto_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000721TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
shiqiane35fdd92008-12-10 05:08:54 +0000722 TestMessage* const msg = new TestMessage;
723 msg->set_member("yes");
724 TestMessage orig_msg;
725 orig_msg.CopyFrom(*msg);
726
zhanyong.wanc6a41232009-05-13 23:38:40 +0000727 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000728 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
729 // s.t. the action works even when the original proto_buffer has
730 // died. We ensure this behavior by deleting msg before using the
731 // action.
732 delete msg;
733
734 TestMessage dest;
735 EXPECT_FALSE(orig_msg.Equals(dest));
736 a.Perform(make_tuple(true, &dest));
737 EXPECT_TRUE(orig_msg.Equals(dest));
738}
739
zhanyong.wanc6a41232009-05-13 23:38:40 +0000740// Tests that SetArgumentPointee<N>(proto_buffer) sets the
741// ::ProtocolMessage variable pointed to by the N-th (0-based)
742// argument to proto_buffer.
743TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
744 TestMessage* const msg = new TestMessage;
745 msg->set_member("yes");
746 TestMessage orig_msg;
747 orig_msg.CopyFrom(*msg);
748
749 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
750 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
751 // s.t. the action works even when the original proto_buffer has
752 // died. We ensure this behavior by deleting msg before using the
753 // action.
754 delete msg;
755
756 TestMessage dest;
757 ::ProtocolMessage* const dest_base = &dest;
758 EXPECT_FALSE(orig_msg.Equals(dest));
759 a.Perform(make_tuple(true, dest_base));
760 EXPECT_TRUE(orig_msg.Equals(dest));
761}
762
763// Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
764// protobuf variable pointed to by the N-th (0-based) argument to
765// proto2_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000766TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
767 using testing::internal::FooMessage;
shiqiane35fdd92008-12-10 05:08:54 +0000768 FooMessage* const msg = new FooMessage;
769 msg->set_int_field(2);
770 msg->set_string_field("hi");
771 FooMessage orig_msg;
772 orig_msg.CopyFrom(*msg);
773
zhanyong.wanc6a41232009-05-13 23:38:40 +0000774 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000775 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
776 // proto2_buffer s.t. the action works even when the original
777 // proto2_buffer has died. We ensure this behavior by deleting msg
778 // before using the action.
779 delete msg;
780
781 FooMessage dest;
782 dest.set_int_field(0);
783 a.Perform(make_tuple(true, &dest));
784 EXPECT_EQ(2, dest.int_field());
785 EXPECT_EQ("hi", dest.string_field());
786}
787
zhanyong.wanc6a41232009-05-13 23:38:40 +0000788// Tests that SetArgumentPointee<N>(proto2_buffer) sets the
789// proto2::Message variable pointed to by the N-th (0-based) argument
790// to proto2_buffer.
791TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
792 using testing::internal::FooMessage;
793 FooMessage* const msg = new FooMessage;
794 msg->set_int_field(2);
795 msg->set_string_field("hi");
796 FooMessage orig_msg;
797 orig_msg.CopyFrom(*msg);
798
799 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
800 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
801 // proto2_buffer s.t. the action works even when the original
802 // proto2_buffer has died. We ensure this behavior by deleting msg
803 // before using the action.
804 delete msg;
805
806 FooMessage dest;
807 dest.set_int_field(0);
808 ::proto2::Message* const dest_base = &dest;
809 a.Perform(make_tuple(true, dest_base));
810 EXPECT_EQ(2, dest.int_field());
811 EXPECT_EQ("hi", dest.string_field());
812}
813
zhanyong.wan02f71062010-05-10 17:14:29 +0000814#endif // GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +0000815
shiqiane35fdd92008-12-10 05:08:54 +0000816// Sample functions and functors for testing Invoke() and etc.
817int Nullary() { return 1; }
818
819class NullaryFunctor {
820 public:
821 int operator()() { return 2; }
822};
823
824bool g_done = false;
825void VoidNullary() { g_done = true; }
826
827class VoidNullaryFunctor {
828 public:
829 void operator()() { g_done = true; }
830};
831
832bool Unary(int x) { return x < 0; }
833
834const char* Plus1(const char* s) { return s + 1; }
835
zhanyong.wan32de5f52009-12-23 00:13:23 +0000836void VoidUnary(int /* n */) { g_done = true; }
shiqiane35fdd92008-12-10 05:08:54 +0000837
838bool ByConstRef(const std::string& s) { return s == "Hi"; }
839
840const double g_double = 0;
841bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
842
843std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT
844
845struct UnaryFunctor {
846 int operator()(bool x) { return x ? 1 : -1; }
847};
848
849const char* Binary(const char* input, short n) { return input + n; } // NOLINT
850
851void VoidBinary(int, char) { g_done = true; }
852
853int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
854
855void VoidTernary(int, char, bool) { g_done = true; }
856
857int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
858
859void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
860
861int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
862
863struct SumOf5Functor {
864 int operator()(int a, int b, int c, int d, int e) {
865 return a + b + c + d + e;
866 }
867};
868
869int SumOf6(int a, int b, int c, int d, int e, int f) {
870 return a + b + c + d + e + f;
871}
872
873struct SumOf6Functor {
874 int operator()(int a, int b, int c, int d, int e, int f) {
875 return a + b + c + d + e + f;
876 }
877};
878
879class Foo {
880 public:
881 Foo() : value_(123) {}
882
883 int Nullary() const { return value_; }
884 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
885 std::string Binary(const std::string& str, char c) const { return str + c; }
886 int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
887 int SumOf4(int a, int b, int c, int d) const {
888 return a + b + c + d + value_;
889 }
890 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
891 int SumOf6(int a, int b, int c, int d, int e, int f) {
892 return a + b + c + d + e + f;
893 }
894 private:
895 int value_;
896};
897
898// Tests InvokeWithoutArgs(function).
899TEST(InvokeWithoutArgsTest, Function) {
900 // As an action that takes one argument.
901 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
902 EXPECT_EQ(1, a.Perform(make_tuple(2)));
903
904 // As an action that takes two arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000905 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000906 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
907
908 // As an action that returns void.
909 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
910 g_done = false;
911 a3.Perform(make_tuple(1));
912 EXPECT_TRUE(g_done);
913}
914
915// Tests InvokeWithoutArgs(functor).
916TEST(InvokeWithoutArgsTest, Functor) {
917 // As an action that takes no argument.
918 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
919 EXPECT_EQ(2, a.Perform(make_tuple()));
920
921 // As an action that takes three arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000922 Action<int(int, double, char)> a2 = // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000923 InvokeWithoutArgs(NullaryFunctor());
924 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
925
926 // As an action that returns void.
927 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
928 g_done = false;
929 a3.Perform(make_tuple());
930 EXPECT_TRUE(g_done);
931}
932
933// Tests InvokeWithoutArgs(obj_ptr, method).
934TEST(InvokeWithoutArgsTest, Method) {
935 Foo foo;
936 Action<int(bool, char)> a = // NOLINT
937 InvokeWithoutArgs(&foo, &Foo::Nullary);
938 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
939}
940
941// Tests using IgnoreResult() on a polymorphic action.
942TEST(IgnoreResultTest, PolymorphicAction) {
943 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
944 a.Perform(make_tuple(1));
945}
946
947// Tests using IgnoreResult() on a monomorphic action.
948
949int ReturnOne() {
950 g_done = true;
951 return 1;
952}
953
954TEST(IgnoreResultTest, MonomorphicAction) {
955 g_done = false;
956 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
957 a.Perform(make_tuple());
958 EXPECT_TRUE(g_done);
959}
960
961// Tests using IgnoreResult() on an action that returns a class type.
962
zhanyong.wan32de5f52009-12-23 00:13:23 +0000963MyClass ReturnMyClass(double /* x */) {
shiqiane35fdd92008-12-10 05:08:54 +0000964 g_done = true;
965 return MyClass();
966}
967
968TEST(IgnoreResultTest, ActionReturningClass) {
969 g_done = false;
970 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
971 a.Perform(make_tuple(2));
972 EXPECT_TRUE(g_done);
973}
974
975TEST(AssignTest, Int) {
976 int x = 0;
977 Action<void(int)> a = Assign(&x, 5);
978 a.Perform(make_tuple(0));
979 EXPECT_EQ(5, x);
980}
981
982TEST(AssignTest, String) {
983 ::std::string x;
984 Action<void(void)> a = Assign(&x, "Hello, world");
985 a.Perform(make_tuple());
986 EXPECT_EQ("Hello, world", x);
987}
988
989TEST(AssignTest, CompatibleTypes) {
990 double x = 0;
991 Action<void(int)> a = Assign(&x, 5);
992 a.Perform(make_tuple(0));
993 EXPECT_DOUBLE_EQ(5, x);
994}
995
zhanyong.wanf7af24c2009-09-24 21:17:24 +0000996#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000997
shiqiane35fdd92008-12-10 05:08:54 +0000998class SetErrnoAndReturnTest : public testing::Test {
999 protected:
1000 virtual void SetUp() { errno = 0; }
1001 virtual void TearDown() { errno = 0; }
1002};
1003
1004TEST_F(SetErrnoAndReturnTest, Int) {
1005 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
1006 EXPECT_EQ(-5, a.Perform(make_tuple()));
1007 EXPECT_EQ(ENOTTY, errno);
1008}
1009
1010TEST_F(SetErrnoAndReturnTest, Ptr) {
1011 int x;
1012 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
1013 EXPECT_EQ(&x, a.Perform(make_tuple()));
1014 EXPECT_EQ(ENOTTY, errno);
1015}
1016
1017TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1018 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
1019 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
1020 EXPECT_EQ(EINVAL, errno);
1021}
1022
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001023#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001024
zhanyong.wana18423e2009-07-22 23:58:19 +00001025// Tests ByRef().
1026
1027// Tests that ReferenceWrapper<T> is copyable.
1028TEST(ByRefTest, IsCopyable) {
1029 const std::string s1 = "Hi";
1030 const std::string s2 = "Hello";
1031
1032 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper = ByRef(s1);
1033 const std::string& r1 = ref_wrapper;
1034 EXPECT_EQ(&s1, &r1);
1035
1036 // Assigns a new value to ref_wrapper.
1037 ref_wrapper = ByRef(s2);
1038 const std::string& r2 = ref_wrapper;
1039 EXPECT_EQ(&s2, &r2);
1040
1041 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = ByRef(s1);
1042 // Copies ref_wrapper1 to ref_wrapper.
1043 ref_wrapper = ref_wrapper1;
1044 const std::string& r3 = ref_wrapper;
1045 EXPECT_EQ(&s1, &r3);
1046}
1047
1048// Tests using ByRef() on a const value.
1049TEST(ByRefTest, ConstValue) {
1050 const int n = 0;
1051 // int& ref = ByRef(n); // This shouldn't compile - we have a
1052 // negative compilation test to catch it.
1053 const int& const_ref = ByRef(n);
1054 EXPECT_EQ(&n, &const_ref);
1055}
1056
1057// Tests using ByRef() on a non-const value.
1058TEST(ByRefTest, NonConstValue) {
1059 int n = 0;
1060
1061 // ByRef(n) can be used as either an int&,
1062 int& ref = ByRef(n);
1063 EXPECT_EQ(&n, &ref);
1064
1065 // or a const int&.
1066 const int& const_ref = ByRef(n);
1067 EXPECT_EQ(&n, &const_ref);
1068}
1069
1070// Tests explicitly specifying the type when using ByRef().
1071TEST(ByRefTest, ExplicitType) {
1072 int n = 0;
1073 const int& r1 = ByRef<const int>(n);
1074 EXPECT_EQ(&n, &r1);
1075
1076 // ByRef<char>(n); // This shouldn't compile - we have a negative
1077 // compilation test to catch it.
1078
1079 Derived d;
1080 Derived& r2 = ByRef<Derived>(d);
1081 EXPECT_EQ(&d, &r2);
1082
1083 const Derived& r3 = ByRef<const Derived>(d);
1084 EXPECT_EQ(&d, &r3);
1085
1086 Base& r4 = ByRef<Base>(d);
1087 EXPECT_EQ(&d, &r4);
1088
1089 const Base& r5 = ByRef<const Base>(d);
1090 EXPECT_EQ(&d, &r5);
1091
1092 // The following shouldn't compile - we have a negative compilation
1093 // test for it.
1094 //
1095 // Base b;
1096 // ByRef<Derived>(b);
1097}
1098
1099// Tests that Google Mock prints expression ByRef(x) as a reference to x.
1100TEST(ByRefTest, PrintsCorrectly) {
1101 int n = 42;
1102 ::std::stringstream expected, actual;
1103 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1104 testing::internal::UniversalPrint(ByRef(n), &actual);
1105 EXPECT_EQ(expected.str(), actual.str());
1106}
1107
shiqiane35fdd92008-12-10 05:08:54 +00001108} // Unnamed namespace