2008-12-10 06:08:54 +01:00
|
|
|
// Copyright 2007, Google Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived from
|
|
|
|
// this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2018-08-14 20:04:07 +02:00
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
// Google Mock - a framework for writing C++ mock classes.
|
|
|
|
//
|
|
|
|
// This file tests the built-in actions.
|
|
|
|
|
2018-04-10 21:57:16 +02:00
|
|
|
// Silence C4800 (C4800: 'int *const ': forcing value
|
2018-11-10 15:05:55 +01:00
|
|
|
// to bool 'true' or 'false') for MSVC 15
|
2018-04-10 21:57:16 +02:00
|
|
|
#ifdef _MSC_VER
|
2018-11-10 15:05:55 +01:00
|
|
|
#if _MSC_VER == 1900
|
2018-04-10 21:57:16 +02:00
|
|
|
# pragma warning(push)
|
|
|
|
# pragma warning(disable:4800)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2010-09-14 07:38:21 +02:00
|
|
|
#include "gmock/gmock-actions.h"
|
2008-12-10 06:08:54 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
2014-01-29 07:41:44 +01:00
|
|
|
#include <memory>
|
2008-12-10 06:08:54 +01:00
|
|
|
#include <string>
|
2019-10-23 10:12:48 +02:00
|
|
|
#include <type_traits>
|
2010-09-14 07:38:21 +02:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gmock/internal/gmock-port.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "gtest/gtest-spi.h"
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-04-24 11:17:47 +02:00
|
|
|
using ::testing::_;
|
|
|
|
using ::testing::Action;
|
|
|
|
using ::testing::ActionInterface;
|
|
|
|
using ::testing::Assign;
|
|
|
|
using ::testing::ByMove;
|
|
|
|
using ::testing::ByRef;
|
|
|
|
using ::testing::DefaultValue;
|
|
|
|
using ::testing::DoAll;
|
|
|
|
using ::testing::DoDefault;
|
|
|
|
using ::testing::IgnoreResult;
|
|
|
|
using ::testing::Invoke;
|
|
|
|
using ::testing::InvokeWithoutArgs;
|
|
|
|
using ::testing::MakePolymorphicAction;
|
|
|
|
using ::testing::PolymorphicAction;
|
|
|
|
using ::testing::Return;
|
|
|
|
using ::testing::ReturnNew;
|
|
|
|
using ::testing::ReturnNull;
|
|
|
|
using ::testing::ReturnRef;
|
|
|
|
using ::testing::ReturnRefOfCopy;
|
|
|
|
using ::testing::ReturnRoundRobin;
|
|
|
|
using ::testing::SetArgPointee;
|
|
|
|
using ::testing::SetArgumentPointee;
|
|
|
|
using ::testing::Unused;
|
|
|
|
using ::testing::WithArgs;
|
|
|
|
using ::testing::internal::BuiltInDefaultValue;
|
2009-03-12 00:37:56 +01:00
|
|
|
|
2009-09-24 23:17:24 +02:00
|
|
|
#if !GTEST_OS_WINDOWS_MOBILE
|
2020-04-24 11:17:47 +02:00
|
|
|
using ::testing::SetErrnoAndReturn;
|
2009-09-24 23:17:24 +02:00
|
|
|
#endif
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
// Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
|
|
|
|
TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
|
2018-10-05 00:28:05 +02:00
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == nullptr);
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == nullptr);
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == nullptr);
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 23:28:45 +01:00
|
|
|
// Tests that BuiltInDefaultValue<T*>::Exists() return true.
|
|
|
|
TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
|
|
|
|
}
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
// Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
|
|
|
|
// built-in numeric type.
|
|
|
|
TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
|
2009-12-23 01:13:23 +01:00
|
|
|
EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
|
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
|
2009-09-25 20:55:50 +02:00
|
|
|
#if GMOCK_WCHAR_T_IS_NATIVE_
|
2018-02-23 21:28:09 +01:00
|
|
|
#if !defined(__WCHAR_UNSIGNED__)
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
|
2018-02-23 21:28:09 +01:00
|
|
|
#else
|
|
|
|
EXPECT_EQ(0U, BuiltInDefaultValue<wchar_t>::Get());
|
|
|
|
#endif
|
2009-09-25 20:55:50 +02:00
|
|
|
#endif
|
2009-12-23 01:13:23 +01:00
|
|
|
EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT
|
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT
|
2009-12-23 01:13:23 +01:00
|
|
|
EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
|
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
|
2009-12-23 01:13:23 +01:00
|
|
|
EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT
|
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT
|
2019-11-20 20:36:06 +01:00
|
|
|
EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long long>::Get()); // NOLINT
|
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<signed long long>::Get()); // NOLINT
|
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<long long>::Get()); // NOLINT
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
|
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
|
|
|
|
}
|
|
|
|
|
2009-01-27 23:28:45 +01:00
|
|
|
// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
|
|
|
|
// built-in numeric type.
|
|
|
|
TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
|
2009-09-25 20:55:50 +02:00
|
|
|
#if GMOCK_WCHAR_T_IS_NATIVE_
|
2009-01-27 23:28:45 +01:00
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
|
2009-09-25 20:55:50 +02:00
|
|
|
#endif
|
2009-01-27 23:28:45 +01:00
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT
|
2019-11-20 20:36:06 +01:00
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<unsigned long long>::Exists()); // NOLINT
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<signed long long>::Exists()); // NOLINT
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<long long>::Exists()); // NOLINT
|
2009-01-27 23:28:45 +01:00
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
|
|
|
|
}
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
// Tests that BuiltInDefaultValue<bool>::Get() returns false.
|
|
|
|
TEST(BuiltInDefaultValueTest, IsFalseForBool) {
|
|
|
|
EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
|
|
|
|
}
|
|
|
|
|
2009-01-27 23:28:45 +01:00
|
|
|
// Tests that BuiltInDefaultValue<bool>::Exists() returns true.
|
|
|
|
TEST(BuiltInDefaultValueTest, BoolExists) {
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
|
|
|
|
}
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
// Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
|
|
|
|
// string type.
|
|
|
|
TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
|
|
|
|
EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
|
|
|
|
}
|
|
|
|
|
2009-01-27 23:28:45 +01:00
|
|
|
// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
|
|
|
|
// string type.
|
|
|
|
TEST(BuiltInDefaultValueTest, ExistsForString) {
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
|
|
|
|
}
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
// Tests that BuiltInDefaultValue<const T>::Get() returns the same
|
|
|
|
// value as BuiltInDefaultValue<T>::Get() does.
|
|
|
|
TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
|
|
|
|
EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
|
|
|
|
EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
|
2018-10-05 00:28:05 +02:00
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == nullptr);
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:45:40 +01:00
|
|
|
// A type that's default constructible.
|
|
|
|
class MyDefaultConstructible {
|
|
|
|
public:
|
|
|
|
MyDefaultConstructible() : value_(42) {}
|
|
|
|
|
|
|
|
int value() const { return value_; }
|
2008-12-10 06:08:54 +01:00
|
|
|
|
2015-02-14 03:45:40 +01:00
|
|
|
private:
|
|
|
|
int value_;
|
2008-12-10 06:08:54 +01:00
|
|
|
};
|
|
|
|
|
2015-02-14 03:45:40 +01:00
|
|
|
// A type that's not default constructible.
|
|
|
|
class MyNonDefaultConstructible {
|
|
|
|
public:
|
|
|
|
// Does not have a default ctor.
|
|
|
|
explicit MyNonDefaultConstructible(int a_value) : value_(a_value) {}
|
|
|
|
|
|
|
|
int value() const { return value_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(BuiltInDefaultValueTest, ExistsForDefaultConstructibleType) {
|
|
|
|
EXPECT_TRUE(BuiltInDefaultValue<MyDefaultConstructible>::Exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BuiltInDefaultValueTest, IsDefaultConstructedForDefaultConstructibleType) {
|
|
|
|
EXPECT_EQ(42, BuiltInDefaultValue<MyDefaultConstructible>::Get().value());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(BuiltInDefaultValueTest, DoesNotExistForNonDefaultConstructibleType) {
|
|
|
|
EXPECT_FALSE(BuiltInDefaultValue<MyNonDefaultConstructible>::Exists());
|
2009-01-27 23:28:45 +01:00
|
|
|
}
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
// Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
|
|
|
|
TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
|
2009-09-11 09:01:08 +02:00
|
|
|
EXPECT_DEATH_IF_SUPPORTED({
|
2008-12-10 06:08:54 +01:00
|
|
|
BuiltInDefaultValue<int&>::Get();
|
|
|
|
}, "");
|
2009-09-11 09:01:08 +02:00
|
|
|
EXPECT_DEATH_IF_SUPPORTED({
|
2008-12-10 06:08:54 +01:00
|
|
|
BuiltInDefaultValue<const char&>::Get();
|
|
|
|
}, "");
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:45:40 +01:00
|
|
|
TEST(BuiltInDefaultValueDeathTest, IsUndefinedForNonDefaultConstructibleType) {
|
2009-09-11 09:01:08 +02:00
|
|
|
EXPECT_DEATH_IF_SUPPORTED({
|
2015-02-14 03:45:40 +01:00
|
|
|
BuiltInDefaultValue<MyNonDefaultConstructible>::Get();
|
2008-12-10 06:08:54 +01:00
|
|
|
}, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that DefaultValue<T>::IsSet() is false initially.
|
|
|
|
TEST(DefaultValueTest, IsInitiallyUnset) {
|
|
|
|
EXPECT_FALSE(DefaultValue<int>::IsSet());
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_FALSE(DefaultValue<MyDefaultConstructible>::IsSet());
|
|
|
|
EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that DefaultValue<T> can be set and then unset.
|
|
|
|
TEST(DefaultValueTest, CanBeSetAndUnset) {
|
2009-01-27 23:28:45 +01:00
|
|
|
EXPECT_TRUE(DefaultValue<int>::Exists());
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
|
2009-01-27 23:28:45 +01:00
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
DefaultValue<int>::Set(1);
|
2015-02-14 03:45:40 +01:00
|
|
|
DefaultValue<const MyNonDefaultConstructible>::Set(
|
|
|
|
MyNonDefaultConstructible(42));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(1, DefaultValue<int>::Get());
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_EQ(42, DefaultValue<const MyNonDefaultConstructible>::Get().value());
|
2008-12-10 06:08:54 +01:00
|
|
|
|
2009-01-27 23:28:45 +01:00
|
|
|
EXPECT_TRUE(DefaultValue<int>::Exists());
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_TRUE(DefaultValue<const MyNonDefaultConstructible>::Exists());
|
2009-01-27 23:28:45 +01:00
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
DefaultValue<int>::Clear();
|
2015-02-14 03:45:40 +01:00
|
|
|
DefaultValue<const MyNonDefaultConstructible>::Clear();
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
EXPECT_FALSE(DefaultValue<int>::IsSet());
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
|
2009-01-27 23:28:45 +01:00
|
|
|
|
|
|
|
EXPECT_TRUE(DefaultValue<int>::Exists());
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that DefaultValue<T>::Get() returns the
|
|
|
|
// BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
|
|
|
|
// false.
|
|
|
|
TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
|
|
|
|
EXPECT_FALSE(DefaultValue<int>::IsSet());
|
2009-01-27 23:28:45 +01:00
|
|
|
EXPECT_TRUE(DefaultValue<int>::Exists());
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::IsSet());
|
|
|
|
EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::Exists());
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(0, DefaultValue<int>::Get());
|
|
|
|
|
2009-09-11 09:01:08 +02:00
|
|
|
EXPECT_DEATH_IF_SUPPORTED({
|
2015-02-14 03:45:40 +01:00
|
|
|
DefaultValue<MyNonDefaultConstructible>::Get();
|
2008-12-10 06:08:54 +01:00
|
|
|
}, "");
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:45:40 +01:00
|
|
|
TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) {
|
|
|
|
EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
|
2018-10-05 00:28:05 +02:00
|
|
|
EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Get() == nullptr);
|
2014-01-29 07:41:44 +01:00
|
|
|
DefaultValue<std::unique_ptr<int>>::SetFactory([] {
|
|
|
|
return std::unique_ptr<int>(new int(42));
|
|
|
|
});
|
|
|
|
EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
|
|
|
|
std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get();
|
|
|
|
EXPECT_EQ(42, *i);
|
|
|
|
}
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
// Tests that DefaultValue<void>::Get() returns void.
|
|
|
|
TEST(DefaultValueTest, GetWorksForVoid) {
|
|
|
|
return DefaultValue<void>::Get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests using DefaultValue with a reference type.
|
|
|
|
|
|
|
|
// Tests that DefaultValue<T&>::IsSet() is false initially.
|
|
|
|
TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
|
|
|
|
EXPECT_FALSE(DefaultValue<int&>::IsSet());
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::IsSet());
|
|
|
|
EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 23:28:45 +01:00
|
|
|
// Tests that DefaultValue<T&>::Exists is false initiallly.
|
|
|
|
TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
|
|
|
|
EXPECT_FALSE(DefaultValue<int&>::Exists());
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::Exists());
|
|
|
|
EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
|
2009-01-27 23:28:45 +01:00
|
|
|
}
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
// Tests that DefaultValue<T&> can be set and then unset.
|
|
|
|
TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
|
|
|
|
int n = 1;
|
|
|
|
DefaultValue<const int&>::Set(n);
|
2015-02-14 03:45:40 +01:00
|
|
|
MyNonDefaultConstructible x(42);
|
|
|
|
DefaultValue<MyNonDefaultConstructible&>::Set(x);
|
2008-12-10 06:08:54 +01:00
|
|
|
|
2009-01-27 23:28:45 +01:00
|
|
|
EXPECT_TRUE(DefaultValue<const int&>::Exists());
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_TRUE(DefaultValue<MyNonDefaultConstructible&>::Exists());
|
2009-01-27 23:28:45 +01:00
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_EQ(&x, &(DefaultValue<MyNonDefaultConstructible&>::Get()));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
DefaultValue<const int&>::Clear();
|
2015-02-14 03:45:40 +01:00
|
|
|
DefaultValue<MyNonDefaultConstructible&>::Clear();
|
2008-12-10 06:08:54 +01:00
|
|
|
|
2009-01-27 23:28:45 +01:00
|
|
|
EXPECT_FALSE(DefaultValue<const int&>::Exists());
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
|
2009-01-27 23:28:45 +01:00
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_FALSE(DefaultValue<const int&>::IsSet());
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that DefaultValue<T&>::Get() returns the
|
|
|
|
// BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
|
|
|
|
// false.
|
|
|
|
TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
|
|
|
|
EXPECT_FALSE(DefaultValue<int&>::IsSet());
|
2015-02-14 03:45:40 +01:00
|
|
|
EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
|
2008-12-10 06:08:54 +01:00
|
|
|
|
2009-09-11 09:01:08 +02:00
|
|
|
EXPECT_DEATH_IF_SUPPORTED({
|
2008-12-10 06:08:54 +01:00
|
|
|
DefaultValue<int&>::Get();
|
|
|
|
}, "");
|
2009-09-11 09:01:08 +02:00
|
|
|
EXPECT_DEATH_IF_SUPPORTED({
|
2015-02-14 03:45:40 +01:00
|
|
|
DefaultValue<MyNonDefaultConstructible>::Get();
|
2008-12-10 06:08:54 +01:00
|
|
|
}, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that ActionInterface can be implemented by defining the
|
|
|
|
// Perform method.
|
|
|
|
|
2013-03-01 22:28:40 +01:00
|
|
|
typedef int MyGlobalFunction(bool, int);
|
2008-12-10 06:08:54 +01:00
|
|
|
|
2013-03-01 22:28:40 +01:00
|
|
|
class MyActionImpl : public ActionInterface<MyGlobalFunction> {
|
2008-12-10 06:08:54 +01:00
|
|
|
public:
|
2018-12-03 17:30:02 +01:00
|
|
|
int Perform(const std::tuple<bool, int>& args) override {
|
2018-10-09 20:50:26 +02:00
|
|
|
return std::get<0>(args) ? std::get<1>(args) : 0;
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
|
|
|
|
MyActionImpl my_action_impl;
|
2011-02-23 20:39:27 +01:00
|
|
|
(void)my_action_impl;
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ActionInterfaceTest, MakeAction) {
|
2013-03-01 22:28:40 +01:00
|
|
|
Action<MyGlobalFunction> action = MakeAction(new MyActionImpl);
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
// When exercising the Perform() method of Action<F>, we must pass
|
|
|
|
// it a tuple whose size and type are compatible with F's argument
|
|
|
|
// types. For example, if F is int(), then Perform() takes a
|
|
|
|
// 0-tuple; if F is void(bool, int), then Perform() takes a
|
2018-10-09 20:50:26 +02:00
|
|
|
// std::tuple<bool, int>, and so on.
|
|
|
|
EXPECT_EQ(5, action.Perform(std::make_tuple(true, 5)));
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that Action<F> can be contructed from a pointer to
|
|
|
|
// ActionInterface<F>.
|
|
|
|
TEST(ActionTest, CanBeConstructedFromActionInterface) {
|
2013-03-01 22:28:40 +01:00
|
|
|
Action<MyGlobalFunction> action(new MyActionImpl);
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that Action<F> delegates actual work to ActionInterface<F>.
|
|
|
|
TEST(ActionTest, DelegatesWorkToActionInterface) {
|
2013-03-01 22:28:40 +01:00
|
|
|
const Action<MyGlobalFunction> action(new MyActionImpl);
|
2008-12-10 06:08:54 +01:00
|
|
|
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(5, action.Perform(std::make_tuple(true, 5)));
|
|
|
|
EXPECT_EQ(0, action.Perform(std::make_tuple(false, 1)));
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that Action<F> can be copied.
|
|
|
|
TEST(ActionTest, IsCopyable) {
|
2013-03-01 22:28:40 +01:00
|
|
|
Action<MyGlobalFunction> a1(new MyActionImpl);
|
|
|
|
Action<MyGlobalFunction> a2(a1); // Tests the copy constructor.
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
// a1 should continue to work after being copied from.
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
|
|
|
|
EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 1)));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
// a2 should work like the action it was copied from.
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(5, a2.Perform(std::make_tuple(true, 5)));
|
|
|
|
EXPECT_EQ(0, a2.Perform(std::make_tuple(false, 1)));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
a2 = a1; // Tests the assignment operator.
|
|
|
|
|
|
|
|
// a1 should continue to work after being copied from.
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
|
|
|
|
EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 1)));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
// a2 should work like the action it was copied from.
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(5, a2.Perform(std::make_tuple(true, 5)));
|
|
|
|
EXPECT_EQ(0, a2.Perform(std::make_tuple(false, 1)));
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that an Action<From> object can be converted to a
|
|
|
|
// compatible Action<To> object.
|
|
|
|
|
|
|
|
class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
|
|
|
|
public:
|
2018-12-03 17:30:02 +01:00
|
|
|
bool Perform(const std::tuple<int>& arg) override {
|
2018-10-09 20:50:26 +02:00
|
|
|
return std::get<0>(arg) != 0;
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ActionTest, CanBeConvertedToOtherActionType) {
|
|
|
|
const Action<bool(int)> a1(new IsNotZero); // NOLINT
|
|
|
|
const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(1, a2.Perform(std::make_tuple('a')));
|
|
|
|
EXPECT_EQ(0, a2.Perform(std::make_tuple('\0')));
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The following two classes are for testing MakePolymorphicAction().
|
|
|
|
|
|
|
|
// Implements a polymorphic action that returns the second of the
|
|
|
|
// arguments it receives.
|
|
|
|
class ReturnSecondArgumentAction {
|
|
|
|
public:
|
|
|
|
// We want to verify that MakePolymorphicAction() can work with a
|
|
|
|
// polymorphic action whose Perform() method template is either
|
|
|
|
// const or not. This lets us verify the non-const case.
|
|
|
|
template <typename Result, typename ArgumentTuple>
|
2018-10-09 20:50:26 +02:00
|
|
|
Result Perform(const ArgumentTuple& args) {
|
|
|
|
return std::get<1>(args);
|
|
|
|
}
|
2008-12-10 06:08:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Implements a polymorphic action that can be used in a nullary
|
|
|
|
// function to return 0.
|
|
|
|
class ReturnZeroFromNullaryFunctionAction {
|
|
|
|
public:
|
|
|
|
// For testing that MakePolymorphicAction() works when the
|
|
|
|
// implementation class' Perform() method template takes only one
|
|
|
|
// template parameter.
|
|
|
|
//
|
|
|
|
// We want to verify that MakePolymorphicAction() can work with a
|
|
|
|
// polymorphic action whose Perform() method template is either
|
|
|
|
// const or not. This lets us verify the const case.
|
|
|
|
template <typename Result>
|
2018-10-09 20:50:26 +02:00
|
|
|
Result Perform(const std::tuple<>&) const {
|
|
|
|
return 0;
|
|
|
|
}
|
2008-12-10 06:08:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// These functions verify that MakePolymorphicAction() returns a
|
|
|
|
// PolymorphicAction<T> where T is the argument's type.
|
|
|
|
|
|
|
|
PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
|
|
|
|
return MakePolymorphicAction(ReturnSecondArgumentAction());
|
|
|
|
}
|
|
|
|
|
|
|
|
PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
|
|
|
|
ReturnZeroFromNullaryFunction() {
|
|
|
|
return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that MakePolymorphicAction() turns a polymorphic action
|
|
|
|
// implementation class into a polymorphic action.
|
|
|
|
TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
|
|
|
|
Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(5, a1.Perform(std::make_tuple(false, 5, 2.0)));
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that MakePolymorphicAction() works when the implementation
|
|
|
|
// class' Perform() method template has only one template parameter.
|
|
|
|
TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
|
|
|
|
Action<int()> a1 = ReturnZeroFromNullaryFunction();
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(0, a1.Perform(std::make_tuple()));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
Action<void*()> a2 = ReturnZeroFromNullaryFunction();
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_TRUE(a2.Perform(std::make_tuple()) == nullptr);
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that Return() works as an action for void-returning
|
|
|
|
// functions.
|
|
|
|
TEST(ReturnTest, WorksForVoid) {
|
|
|
|
const Action<void(int)> ret = Return(); // NOLINT
|
2018-10-09 20:50:26 +02:00
|
|
|
return ret.Perform(std::make_tuple(1));
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that Return(v) returns v.
|
|
|
|
TEST(ReturnTest, ReturnsGivenValue) {
|
|
|
|
Action<int()> ret = Return(1); // NOLINT
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(1, ret.Perform(std::make_tuple()));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
ret = Return(-5);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(-5, ret.Perform(std::make_tuple()));
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that Return("string literal") works.
|
|
|
|
TEST(ReturnTest, AcceptsStringLiteral) {
|
|
|
|
Action<const char*()> a1 = Return("Hello");
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_STREQ("Hello", a1.Perform(std::make_tuple()));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
Action<std::string()> a2 = Return("world");
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ("world", a2.Perform(std::make_tuple()));
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
2014-11-17 03:04:46 +01:00
|
|
|
// Test struct which wraps a vector of integers. Used in
|
|
|
|
// 'SupportsWrapperReturnType' test.
|
|
|
|
struct IntegerVectorWrapper {
|
|
|
|
std::vector<int> * v;
|
|
|
|
IntegerVectorWrapper(std::vector<int>& _v) : v(&_v) {} // NOLINT
|
|
|
|
};
|
|
|
|
|
|
|
|
// Tests that Return() works when return type is a wrapper type.
|
|
|
|
TEST(ReturnTest, SupportsWrapperReturnType) {
|
|
|
|
// Initialize vector of integers.
|
|
|
|
std::vector<int> v;
|
|
|
|
for (int i = 0; i < 5; ++i) v.push_back(i);
|
|
|
|
|
|
|
|
// Return() called with 'v' as argument. The Action will return the same data
|
|
|
|
// as 'v' (copy) but it will be wrapped in an IntegerVectorWrapper.
|
|
|
|
Action<IntegerVectorWrapper()> a = Return(v);
|
2018-10-09 20:50:26 +02:00
|
|
|
const std::vector<int>& result = *(a.Perform(std::make_tuple()).v);
|
2014-11-17 03:04:46 +01:00
|
|
|
EXPECT_THAT(result, ::testing::ElementsAre(0, 1, 2, 3, 4));
|
|
|
|
}
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
// Tests that Return(v) is covaraint.
|
|
|
|
|
|
|
|
struct Base {
|
|
|
|
bool operator==(const Base&) { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Derived : public Base {
|
|
|
|
bool operator==(const Derived&) { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ReturnTest, IsCovariant) {
|
|
|
|
Base base;
|
|
|
|
Derived derived;
|
|
|
|
Action<Base*()> ret = Return(&base);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(&base, ret.Perform(std::make_tuple()));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
ret = Return(&derived);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(&derived, ret.Perform(std::make_tuple()));
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
2009-11-18 01:09:28 +01:00
|
|
|
// Tests that the type of the value passed into Return is converted into T
|
|
|
|
// when the action is cast to Action<T(...)> rather than when the action is
|
|
|
|
// performed. See comments on testing::internal::ReturnAction in
|
|
|
|
// gmock-actions.h for more information.
|
|
|
|
class FromType {
|
|
|
|
public:
|
2012-04-10 18:02:11 +02:00
|
|
|
explicit FromType(bool* is_converted) : converted_(is_converted) {}
|
2009-11-18 01:09:28 +01:00
|
|
|
bool* converted() const { return converted_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool* const converted_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ToType {
|
|
|
|
public:
|
2012-04-10 18:02:11 +02:00
|
|
|
// Must allow implicit conversion due to use in ImplicitCast_<T>.
|
|
|
|
ToType(const FromType& x) { *x.converted() = true; } // NOLINT
|
2009-11-18 01:09:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ReturnTest, ConvertsArgumentWhenConverted) {
|
|
|
|
bool converted = false;
|
|
|
|
FromType x(&converted);
|
|
|
|
Action<ToType()> action(Return(x));
|
|
|
|
EXPECT_TRUE(converted) << "Return must convert its argument in its own "
|
|
|
|
<< "conversion operator.";
|
|
|
|
converted = false;
|
2018-10-09 20:50:26 +02:00
|
|
|
action.Perform(std::tuple<>());
|
2009-11-18 01:09:28 +01:00
|
|
|
EXPECT_FALSE(converted) << "Action must NOT convert its argument "
|
2012-04-10 18:02:11 +02:00
|
|
|
<< "when performed.";
|
2009-11-18 01:09:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class DestinationType {};
|
|
|
|
|
|
|
|
class SourceType {
|
|
|
|
public:
|
|
|
|
// Note: a non-const typecast operator.
|
|
|
|
operator DestinationType() { return DestinationType(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
|
|
|
|
SourceType s;
|
|
|
|
Action<DestinationType()> action(Return(s));
|
|
|
|
}
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
// Tests that ReturnNull() returns NULL in a pointer-returning function.
|
|
|
|
TEST(ReturnNullTest, WorksInPointerReturningFunction) {
|
|
|
|
const Action<int*()> a1 = ReturnNull();
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_TRUE(a1.Perform(std::make_tuple()) == nullptr);
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_TRUE(a2.Perform(std::make_tuple(true)) == nullptr);
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
2015-01-08 04:03:09 +01:00
|
|
|
// Tests that ReturnNull() returns NULL for shared_ptr and unique_ptr returning
|
|
|
|
// functions.
|
|
|
|
TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) {
|
|
|
|
const Action<std::unique_ptr<const int>()> a1 = ReturnNull();
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_TRUE(a1.Perform(std::make_tuple()) == nullptr);
|
2015-01-08 04:03:09 +01:00
|
|
|
|
|
|
|
const Action<std::shared_ptr<int>(std::string)> a2 = ReturnNull();
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_TRUE(a2.Perform(std::make_tuple("foo")) == nullptr);
|
2015-01-08 04:03:09 +01:00
|
|
|
}
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
// Tests that ReturnRef(v) works for reference types.
|
|
|
|
TEST(ReturnRefTest, WorksForReference) {
|
|
|
|
const int n = 0;
|
|
|
|
const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
|
|
|
|
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(&n, &ret.Perform(std::make_tuple(true)));
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that ReturnRef(v) is covariant.
|
|
|
|
TEST(ReturnRefTest, IsCovariant) {
|
|
|
|
Base base;
|
|
|
|
Derived derived;
|
|
|
|
Action<Base&()> a = ReturnRef(base);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(&base, &a.Perform(std::make_tuple()));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
a = ReturnRef(derived);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(&derived, &a.Perform(std::make_tuple()));
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
2019-10-22 18:41:35 +02:00
|
|
|
template <typename T, typename = decltype(ReturnRef(std::declval<T&&>()))>
|
|
|
|
bool CanCallReturnRef(T&&) { return true; }
|
|
|
|
bool CanCallReturnRef(Unused) { return false; }
|
2019-10-23 10:12:48 +02:00
|
|
|
|
|
|
|
// Tests that ReturnRef(v) is working with non-temporaries (T&)
|
2019-10-24 10:22:09 +02:00
|
|
|
TEST(ReturnRefTest, WorksForNonTemporary) {
|
2019-10-25 16:14:18 +02:00
|
|
|
int scalar_value = 123;
|
|
|
|
EXPECT_TRUE(CanCallReturnRef(scalar_value));
|
2019-10-23 10:12:48 +02:00
|
|
|
|
2019-10-25 16:14:18 +02:00
|
|
|
std::string non_scalar_value("ABC");
|
|
|
|
EXPECT_TRUE(CanCallReturnRef(non_scalar_value));
|
2019-10-23 10:12:48 +02:00
|
|
|
|
2019-10-25 16:14:18 +02:00
|
|
|
const int const_scalar_value{321};
|
|
|
|
EXPECT_TRUE(CanCallReturnRef(const_scalar_value));
|
2019-10-23 10:12:48 +02:00
|
|
|
|
2019-10-25 16:14:18 +02:00
|
|
|
const std::string const_non_scalar_value("CBA");
|
|
|
|
EXPECT_TRUE(CanCallReturnRef(const_non_scalar_value));
|
2019-10-22 18:41:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that ReturnRef(v) is not working with temporaries (T&&)
|
2019-10-24 10:22:09 +02:00
|
|
|
TEST(ReturnRefTest, DoesNotWorkForTemporary) {
|
2019-10-25 16:14:18 +02:00
|
|
|
auto scalar_value = []() -> int { return 123; };
|
|
|
|
EXPECT_FALSE(CanCallReturnRef(scalar_value()));
|
2019-10-23 10:12:48 +02:00
|
|
|
|
2019-10-25 16:14:18 +02:00
|
|
|
auto non_scalar_value = []() -> std::string { return "ABC"; };
|
|
|
|
EXPECT_FALSE(CanCallReturnRef(non_scalar_value()));
|
2019-10-22 18:41:35 +02:00
|
|
|
|
2019-10-25 10:29:15 +02:00
|
|
|
// cannot use here callable returning "const scalar type",
|
|
|
|
// because such const for scalar return type is ignored
|
2019-10-24 10:22:09 +02:00
|
|
|
EXPECT_FALSE(CanCallReturnRef(static_cast<const int>(321)));
|
2019-10-23 10:12:48 +02:00
|
|
|
|
2019-10-25 16:14:18 +02:00
|
|
|
auto const_non_scalar_value = []() -> const std::string { return "CBA"; };
|
|
|
|
EXPECT_FALSE(CanCallReturnRef(const_non_scalar_value()));
|
2019-10-23 10:12:48 +02:00
|
|
|
}
|
2019-10-22 18:41:35 +02:00
|
|
|
|
2010-07-03 02:16:42 +02:00
|
|
|
// Tests that ReturnRefOfCopy(v) works for reference types.
|
|
|
|
TEST(ReturnRefOfCopyTest, WorksForReference) {
|
|
|
|
int n = 42;
|
|
|
|
const Action<const int&()> ret = ReturnRefOfCopy(n);
|
|
|
|
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_NE(&n, &ret.Perform(std::make_tuple()));
|
|
|
|
EXPECT_EQ(42, ret.Perform(std::make_tuple()));
|
2010-07-03 02:16:42 +02:00
|
|
|
|
|
|
|
n = 43;
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_NE(&n, &ret.Perform(std::make_tuple()));
|
|
|
|
EXPECT_EQ(42, ret.Perform(std::make_tuple()));
|
2010-07-03 02:16:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that ReturnRefOfCopy(v) is covariant.
|
|
|
|
TEST(ReturnRefOfCopyTest, IsCovariant) {
|
|
|
|
Base base;
|
|
|
|
Derived derived;
|
|
|
|
Action<Base&()> a = ReturnRefOfCopy(base);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_NE(&base, &a.Perform(std::make_tuple()));
|
2010-07-03 02:16:42 +02:00
|
|
|
|
|
|
|
a = ReturnRefOfCopy(derived);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_NE(&derived, &a.Perform(std::make_tuple()));
|
2010-07-03 02:16:42 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 20:09:41 +02:00
|
|
|
// Tests that ReturnRoundRobin(v) works with initializer lists
|
|
|
|
TEST(ReturnRoundRobinTest, WorksForInitList) {
|
|
|
|
Action<int()> ret = ReturnRoundRobin({1, 2, 3});
|
|
|
|
|
|
|
|
EXPECT_EQ(1, ret.Perform(std::make_tuple()));
|
|
|
|
EXPECT_EQ(2, ret.Perform(std::make_tuple()));
|
|
|
|
EXPECT_EQ(3, ret.Perform(std::make_tuple()));
|
|
|
|
EXPECT_EQ(1, ret.Perform(std::make_tuple()));
|
|
|
|
EXPECT_EQ(2, ret.Perform(std::make_tuple()));
|
|
|
|
EXPECT_EQ(3, ret.Perform(std::make_tuple()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that ReturnRoundRobin(v) works with vectors
|
|
|
|
TEST(ReturnRoundRobinTest, WorksForVector) {
|
|
|
|
std::vector<double> v = {4.4, 5.5, 6.6};
|
|
|
|
Action<double()> ret = ReturnRoundRobin(v);
|
|
|
|
|
|
|
|
EXPECT_EQ(4.4, ret.Perform(std::make_tuple()));
|
|
|
|
EXPECT_EQ(5.5, ret.Perform(std::make_tuple()));
|
|
|
|
EXPECT_EQ(6.6, ret.Perform(std::make_tuple()));
|
|
|
|
EXPECT_EQ(4.4, ret.Perform(std::make_tuple()));
|
|
|
|
EXPECT_EQ(5.5, ret.Perform(std::make_tuple()));
|
|
|
|
EXPECT_EQ(6.6, ret.Perform(std::make_tuple()));
|
|
|
|
}
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
// Tests that DoDefault() does the default action for the mock method.
|
|
|
|
|
|
|
|
class MockClass {
|
|
|
|
public:
|
2009-12-23 01:13:23 +01:00
|
|
|
MockClass() {}
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
|
2015-02-14 03:45:40 +01:00
|
|
|
MOCK_METHOD0(Foo, MyNonDefaultConstructible());
|
2014-01-29 07:41:44 +01:00
|
|
|
MOCK_METHOD0(MakeUnique, std::unique_ptr<int>());
|
2014-11-17 01:56:52 +01:00
|
|
|
MOCK_METHOD0(MakeUniqueBase, std::unique_ptr<Base>());
|
2014-01-29 07:41:44 +01:00
|
|
|
MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
|
2018-04-05 22:09:17 +02:00
|
|
|
MOCK_METHOD1(TakeUnique, int(std::unique_ptr<int>));
|
2018-04-13 17:02:25 +02:00
|
|
|
MOCK_METHOD2(TakeUnique,
|
|
|
|
int(const std::unique_ptr<int>&, std::unique_ptr<int>));
|
2009-12-23 01:13:23 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
|
2008-12-10 06:08:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Tests that DoDefault() returns the built-in default value for the
|
|
|
|
// return type by default.
|
|
|
|
TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
|
|
|
|
MockClass mock;
|
|
|
|
EXPECT_CALL(mock, IntFunc(_))
|
|
|
|
.WillOnce(DoDefault());
|
|
|
|
EXPECT_EQ(0, mock.IntFunc(true));
|
|
|
|
}
|
|
|
|
|
2013-02-28 23:58:51 +01:00
|
|
|
// Tests that DoDefault() throws (when exceptions are enabled) or aborts
|
|
|
|
// the process when there is no built-in default value for the return type.
|
2008-12-10 06:08:54 +01:00
|
|
|
TEST(DoDefaultDeathTest, DiesForUnknowType) {
|
|
|
|
MockClass mock;
|
|
|
|
EXPECT_CALL(mock, Foo())
|
|
|
|
.WillRepeatedly(DoDefault());
|
2013-02-28 23:58:51 +01:00
|
|
|
#if GTEST_HAS_EXCEPTIONS
|
|
|
|
EXPECT_ANY_THROW(mock.Foo());
|
|
|
|
#else
|
2009-09-11 09:01:08 +02:00
|
|
|
EXPECT_DEATH_IF_SUPPORTED({
|
2008-12-10 06:08:54 +01:00
|
|
|
mock.Foo();
|
|
|
|
}, "");
|
2013-02-28 23:58:51 +01:00
|
|
|
#endif
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that using DoDefault() inside a composite action leads to a
|
|
|
|
// run-time error.
|
|
|
|
|
2009-12-23 01:13:23 +01:00
|
|
|
void VoidFunc(bool /* flag */) {}
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
|
|
|
|
MockClass mock;
|
|
|
|
EXPECT_CALL(mock, IntFunc(_))
|
|
|
|
.WillRepeatedly(DoAll(Invoke(VoidFunc),
|
|
|
|
DoDefault()));
|
|
|
|
|
|
|
|
// Ideally we should verify the error message as well. Sadly,
|
|
|
|
// EXPECT_DEATH() can only capture stderr, while Google Mock's
|
|
|
|
// errors are printed on stdout. Therefore we have to settle for
|
|
|
|
// not verifying the message.
|
2009-09-11 09:01:08 +02:00
|
|
|
EXPECT_DEATH_IF_SUPPORTED({
|
2008-12-10 06:08:54 +01:00
|
|
|
mock.IntFunc(true);
|
|
|
|
}, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that DoDefault() returns the default value set by
|
2018-04-13 17:02:25 +02:00
|
|
|
// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
|
2008-12-10 06:08:54 +01:00
|
|
|
TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
|
|
|
|
DefaultValue<int>::Set(1);
|
|
|
|
MockClass mock;
|
|
|
|
EXPECT_CALL(mock, IntFunc(_))
|
|
|
|
.WillOnce(DoDefault());
|
|
|
|
EXPECT_EQ(1, mock.IntFunc(false));
|
|
|
|
DefaultValue<int>::Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that DoDefault() does the action specified by ON_CALL().
|
|
|
|
TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
|
|
|
|
MockClass mock;
|
|
|
|
ON_CALL(mock, IntFunc(_))
|
|
|
|
.WillByDefault(Return(2));
|
|
|
|
EXPECT_CALL(mock, IntFunc(_))
|
|
|
|
.WillOnce(DoDefault());
|
|
|
|
EXPECT_EQ(2, mock.IntFunc(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
|
|
|
|
TEST(DoDefaultTest, CannotBeUsedInOnCall) {
|
|
|
|
MockClass mock;
|
|
|
|
EXPECT_NONFATAL_FAILURE({ // NOLINT
|
|
|
|
ON_CALL(mock, IntFunc(_))
|
|
|
|
.WillByDefault(DoDefault());
|
|
|
|
}, "DoDefault() cannot be used in ON_CALL()");
|
|
|
|
}
|
|
|
|
|
2010-10-05 07:58:51 +02:00
|
|
|
// Tests that SetArgPointee<N>(v) sets the variable pointed to by
|
|
|
|
// the N-th (0-based) argument to v.
|
|
|
|
TEST(SetArgPointeeTest, SetsTheNthPointee) {
|
|
|
|
typedef void MyFunction(bool, int*, char*);
|
|
|
|
Action<MyFunction> a = SetArgPointee<1>(2);
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
char ch = '\0';
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(true, &n, &ch));
|
2010-10-05 07:58:51 +02:00
|
|
|
EXPECT_EQ(2, n);
|
|
|
|
EXPECT_EQ('\0', ch);
|
|
|
|
|
|
|
|
a = SetArgPointee<2>('a');
|
|
|
|
n = 0;
|
|
|
|
ch = '\0';
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(true, &n, &ch));
|
2010-10-05 07:58:51 +02:00
|
|
|
EXPECT_EQ(0, n);
|
|
|
|
EXPECT_EQ('a', ch);
|
|
|
|
}
|
|
|
|
|
2010-12-03 00:30:50 +01:00
|
|
|
// Tests that SetArgPointee<N>() accepts a string literal.
|
|
|
|
TEST(SetArgPointeeTest, AcceptsStringLiteral) {
|
2011-03-09 02:18:08 +01:00
|
|
|
typedef void MyFunction(std::string*, const char**);
|
|
|
|
Action<MyFunction> a = SetArgPointee<0>("hi");
|
2010-12-03 00:30:50 +01:00
|
|
|
std::string str;
|
2018-10-05 00:28:05 +02:00
|
|
|
const char* ptr = nullptr;
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(&str, &ptr));
|
2010-12-03 00:30:50 +01:00
|
|
|
EXPECT_EQ("hi", str);
|
2018-10-05 00:28:05 +02:00
|
|
|
EXPECT_TRUE(ptr == nullptr);
|
2010-12-03 00:30:50 +01:00
|
|
|
|
2011-03-09 02:18:08 +01:00
|
|
|
a = SetArgPointee<1>("world");
|
2010-12-03 00:30:50 +01:00
|
|
|
str = "";
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(&str, &ptr));
|
2010-12-03 00:30:50 +01:00
|
|
|
EXPECT_EQ("", str);
|
|
|
|
EXPECT_STREQ("world", ptr);
|
|
|
|
}
|
|
|
|
|
2011-03-09 02:18:08 +01:00
|
|
|
TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
|
|
|
|
typedef void MyFunction(const wchar_t**);
|
|
|
|
Action<MyFunction> a = SetArgPointee<0>(L"world");
|
2018-10-05 00:28:05 +02:00
|
|
|
const wchar_t* ptr = nullptr;
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(&ptr));
|
2011-03-09 02:18:08 +01:00
|
|
|
EXPECT_STREQ(L"world", ptr);
|
|
|
|
|
|
|
|
# if GTEST_HAS_STD_WSTRING
|
|
|
|
|
|
|
|
typedef void MyStringFunction(std::wstring*);
|
|
|
|
Action<MyStringFunction> a2 = SetArgPointee<0>(L"world");
|
|
|
|
std::wstring str = L"";
|
2018-10-09 20:50:26 +02:00
|
|
|
a2.Perform(std::make_tuple(&str));
|
2011-03-09 02:18:08 +01:00
|
|
|
EXPECT_EQ(L"world", str);
|
|
|
|
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
2010-12-03 00:30:50 +01:00
|
|
|
// Tests that SetArgPointee<N>() accepts a char pointer.
|
|
|
|
TEST(SetArgPointeeTest, AcceptsCharPointer) {
|
|
|
|
typedef void MyFunction(bool, std::string*, const char**);
|
|
|
|
const char* const hi = "hi";
|
|
|
|
Action<MyFunction> a = SetArgPointee<1>(hi);
|
|
|
|
std::string str;
|
2018-10-05 00:28:05 +02:00
|
|
|
const char* ptr = nullptr;
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(true, &str, &ptr));
|
2010-12-03 00:30:50 +01:00
|
|
|
EXPECT_EQ("hi", str);
|
2018-10-05 00:28:05 +02:00
|
|
|
EXPECT_TRUE(ptr == nullptr);
|
2010-12-03 00:30:50 +01:00
|
|
|
|
|
|
|
char world_array[] = "world";
|
|
|
|
char* const world = world_array;
|
|
|
|
a = SetArgPointee<2>(world);
|
|
|
|
str = "";
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(true, &str, &ptr));
|
2010-12-03 00:30:50 +01:00
|
|
|
EXPECT_EQ("", str);
|
|
|
|
EXPECT_EQ(world, ptr);
|
|
|
|
}
|
|
|
|
|
2011-03-09 02:18:08 +01:00
|
|
|
TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
|
|
|
|
typedef void MyFunction(bool, const wchar_t**);
|
|
|
|
const wchar_t* const hi = L"hi";
|
|
|
|
Action<MyFunction> a = SetArgPointee<1>(hi);
|
2018-10-05 00:28:05 +02:00
|
|
|
const wchar_t* ptr = nullptr;
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(true, &ptr));
|
2011-03-09 02:18:08 +01:00
|
|
|
EXPECT_EQ(hi, ptr);
|
|
|
|
|
|
|
|
# if GTEST_HAS_STD_WSTRING
|
|
|
|
|
|
|
|
typedef void MyStringFunction(bool, std::wstring*);
|
|
|
|
wchar_t world_array[] = L"world";
|
|
|
|
wchar_t* const world = world_array;
|
|
|
|
Action<MyStringFunction> a2 = SetArgPointee<1>(world);
|
|
|
|
std::wstring str;
|
2018-10-09 20:50:26 +02:00
|
|
|
a2.Perform(std::make_tuple(true, &str));
|
2011-03-09 02:18:08 +01:00
|
|
|
EXPECT_EQ(world_array, str);
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
|
|
|
|
// the N-th (0-based) argument to v.
|
|
|
|
TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
|
|
|
|
typedef void MyFunction(bool, int*, char*);
|
|
|
|
Action<MyFunction> a = SetArgumentPointee<1>(2);
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
char ch = '\0';
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(true, &n, &ch));
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ(2, n);
|
|
|
|
EXPECT_EQ('\0', ch);
|
|
|
|
|
|
|
|
a = SetArgumentPointee<2>('a');
|
|
|
|
n = 0;
|
|
|
|
ch = '\0';
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(true, &n, &ch));
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ(0, n);
|
|
|
|
EXPECT_EQ('a', ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sample functions and functors for testing Invoke() and etc.
|
|
|
|
int Nullary() { return 1; }
|
|
|
|
|
|
|
|
class NullaryFunctor {
|
|
|
|
public:
|
|
|
|
int operator()() { return 2; }
|
|
|
|
};
|
|
|
|
|
|
|
|
bool g_done = false;
|
|
|
|
void VoidNullary() { g_done = true; }
|
|
|
|
|
|
|
|
class VoidNullaryFunctor {
|
|
|
|
public:
|
|
|
|
void operator()() { g_done = true; }
|
|
|
|
};
|
|
|
|
|
2018-11-15 21:43:19 +01:00
|
|
|
short Short(short n) { return n; } // NOLINT
|
|
|
|
char Char(char ch) { return ch; }
|
|
|
|
|
|
|
|
const char* CharPtr(const char* s) { return s; }
|
|
|
|
|
|
|
|
bool Unary(int x) { return x < 0; }
|
|
|
|
|
|
|
|
const char* Binary(const char* input, short n) { return input + n; } // NOLINT
|
|
|
|
|
|
|
|
void VoidBinary(int, char) { g_done = true; }
|
|
|
|
|
|
|
|
int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
|
|
|
|
|
|
|
|
int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
class Foo {
|
|
|
|
public:
|
|
|
|
Foo() : value_(123) {}
|
|
|
|
|
|
|
|
int Nullary() const { return value_; }
|
2013-03-01 07:53:35 +01:00
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
private:
|
|
|
|
int value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Tests InvokeWithoutArgs(function).
|
|
|
|
TEST(InvokeWithoutArgsTest, Function) {
|
|
|
|
// As an action that takes one argument.
|
|
|
|
Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
// As an action that takes two arguments.
|
2009-12-23 01:13:23 +01:00
|
|
|
Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(1, a2.Perform(std::make_tuple(2, 3.5)));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
// As an action that returns void.
|
|
|
|
Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
|
|
|
|
g_done = false;
|
2018-10-09 20:50:26 +02:00
|
|
|
a3.Perform(std::make_tuple(1));
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_TRUE(g_done);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests InvokeWithoutArgs(functor).
|
|
|
|
TEST(InvokeWithoutArgsTest, Functor) {
|
|
|
|
// As an action that takes no argument.
|
|
|
|
Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(2, a.Perform(std::make_tuple()));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
// As an action that takes three arguments.
|
2009-12-23 01:13:23 +01:00
|
|
|
Action<int(int, double, char)> a2 = // NOLINT
|
2008-12-10 06:08:54 +01:00
|
|
|
InvokeWithoutArgs(NullaryFunctor());
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(2, a2.Perform(std::make_tuple(3, 3.5, 'a')));
|
2008-12-10 06:08:54 +01:00
|
|
|
|
|
|
|
// As an action that returns void.
|
|
|
|
Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
|
|
|
|
g_done = false;
|
2018-10-09 20:50:26 +02:00
|
|
|
a3.Perform(std::make_tuple());
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_TRUE(g_done);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests InvokeWithoutArgs(obj_ptr, method).
|
|
|
|
TEST(InvokeWithoutArgsTest, Method) {
|
|
|
|
Foo foo;
|
|
|
|
Action<int(bool, char)> a = // NOLINT
|
|
|
|
InvokeWithoutArgs(&foo, &Foo::Nullary);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(123, a.Perform(std::make_tuple(true, 'a')));
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests using IgnoreResult() on a polymorphic action.
|
|
|
|
TEST(IgnoreResultTest, PolymorphicAction) {
|
|
|
|
Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(1));
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests using IgnoreResult() on a monomorphic action.
|
|
|
|
|
|
|
|
int ReturnOne() {
|
|
|
|
g_done = true;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IgnoreResultTest, MonomorphicAction) {
|
|
|
|
g_done = false;
|
|
|
|
Action<void()> a = IgnoreResult(Invoke(ReturnOne));
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple());
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_TRUE(g_done);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests using IgnoreResult() on an action that returns a class type.
|
|
|
|
|
2015-02-14 03:45:40 +01:00
|
|
|
MyNonDefaultConstructible ReturnMyNonDefaultConstructible(double /* x */) {
|
2008-12-10 06:08:54 +01:00
|
|
|
g_done = true;
|
2015-02-14 03:45:40 +01:00
|
|
|
return MyNonDefaultConstructible(42);
|
2008-12-10 06:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IgnoreResultTest, ActionReturningClass) {
|
|
|
|
g_done = false;
|
2015-02-14 03:45:40 +01:00
|
|
|
Action<void(int)> a =
|
|
|
|
IgnoreResult(Invoke(ReturnMyNonDefaultConstructible)); // NOLINT
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(2));
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_TRUE(g_done);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssignTest, Int) {
|
|
|
|
int x = 0;
|
|
|
|
Action<void(int)> a = Assign(&x, 5);
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(0));
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ(5, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssignTest, String) {
|
|
|
|
::std::string x;
|
|
|
|
Action<void(void)> a = Assign(&x, "Hello, world");
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple());
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ("Hello, world", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssignTest, CompatibleTypes) {
|
|
|
|
double x = 0;
|
|
|
|
Action<void(int)> a = Assign(&x, 5);
|
2018-10-09 20:50:26 +02:00
|
|
|
a.Perform(std::make_tuple(0));
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_DOUBLE_EQ(5, x);
|
|
|
|
}
|
|
|
|
|
2018-11-15 21:43:19 +01:00
|
|
|
|
|
|
|
// Tests using WithArgs and with an action that takes 1 argument.
|
|
|
|
TEST(WithArgsTest, OneArg) {
|
|
|
|
Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT
|
|
|
|
EXPECT_TRUE(a.Perform(std::make_tuple(1.5, -1)));
|
|
|
|
EXPECT_FALSE(a.Perform(std::make_tuple(1.5, 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests using WithArgs with an action that takes 2 arguments.
|
|
|
|
TEST(WithArgsTest, TwoArgs) {
|
|
|
|
Action<const char*(const char* s, double x, short n)> a = // NOLINT
|
|
|
|
WithArgs<0, 2>(Invoke(Binary));
|
|
|
|
const char s[] = "Hello";
|
|
|
|
EXPECT_EQ(s + 2, a.Perform(std::make_tuple(CharPtr(s), 0.5, Short(2))));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ConcatAll {
|
|
|
|
std::string operator()() const { return {}; }
|
|
|
|
template <typename... I>
|
|
|
|
std::string operator()(const char* a, I... i) const {
|
|
|
|
return a + ConcatAll()(i...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Tests using WithArgs with an action that takes 10 arguments.
|
|
|
|
TEST(WithArgsTest, TenArgs) {
|
|
|
|
Action<std::string(const char*, const char*, const char*, const char*)> a =
|
|
|
|
WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(ConcatAll{}));
|
|
|
|
EXPECT_EQ("0123210123",
|
|
|
|
a.Perform(std::make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
|
|
|
|
CharPtr("3"))));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests using WithArgs with an action that is not Invoke().
|
|
|
|
class SubtractAction : public ActionInterface<int(int, int)> {
|
|
|
|
public:
|
2018-12-03 17:30:02 +01:00
|
|
|
int Perform(const std::tuple<int, int>& args) override {
|
2018-11-15 21:43:19 +01:00
|
|
|
return std::get<0>(args) - std::get<1>(args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(WithArgsTest, NonInvokeAction) {
|
|
|
|
Action<int(const std::string&, int, int)> a =
|
|
|
|
WithArgs<2, 1>(MakeAction(new SubtractAction));
|
|
|
|
std::tuple<std::string, int, int> dummy =
|
|
|
|
std::make_tuple(std::string("hi"), 2, 10);
|
|
|
|
EXPECT_EQ(8, a.Perform(dummy));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests using WithArgs to pass all original arguments in the original order.
|
|
|
|
TEST(WithArgsTest, Identity) {
|
|
|
|
Action<int(int x, char y, short z)> a = // NOLINT
|
|
|
|
WithArgs<0, 1, 2>(Invoke(Ternary));
|
|
|
|
EXPECT_EQ(123, a.Perform(std::make_tuple(100, Char(20), Short(3))));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests using WithArgs with repeated arguments.
|
|
|
|
TEST(WithArgsTest, RepeatedArguments) {
|
|
|
|
Action<int(bool, int m, int n)> a = // NOLINT
|
|
|
|
WithArgs<1, 1, 1, 1>(Invoke(SumOf4));
|
|
|
|
EXPECT_EQ(4, a.Perform(std::make_tuple(false, 1, 10)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests using WithArgs with reversed argument order.
|
|
|
|
TEST(WithArgsTest, ReversedArgumentOrder) {
|
|
|
|
Action<const char*(short n, const char* input)> a = // NOLINT
|
|
|
|
WithArgs<1, 0>(Invoke(Binary));
|
|
|
|
const char s[] = "Hello";
|
|
|
|
EXPECT_EQ(s + 2, a.Perform(std::make_tuple(Short(2), CharPtr(s))));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests using WithArgs with compatible, but not identical, argument types.
|
|
|
|
TEST(WithArgsTest, ArgsOfCompatibleTypes) {
|
|
|
|
Action<long(short x, char y, double z, char c)> a = // NOLINT
|
|
|
|
WithArgs<0, 1, 3>(Invoke(Ternary));
|
|
|
|
EXPECT_EQ(123,
|
|
|
|
a.Perform(std::make_tuple(Short(100), Char(20), 5.6, Char(3))));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests using WithArgs with an action that returns void.
|
|
|
|
TEST(WithArgsTest, VoidAction) {
|
|
|
|
Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary));
|
|
|
|
g_done = false;
|
|
|
|
a.Perform(std::make_tuple(1.5, 'a', 3));
|
|
|
|
EXPECT_TRUE(g_done);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WithArgsTest, ReturnReference) {
|
2018-11-20 16:42:17 +01:00
|
|
|
Action<int&(int&, void*)> aa = WithArgs<0>([](int& a) -> int& { return a; });
|
2018-11-15 21:43:19 +01:00
|
|
|
int i = 0;
|
2018-11-20 16:42:17 +01:00
|
|
|
const int& res = aa.Perform(std::forward_as_tuple(i, nullptr));
|
2018-11-15 21:43:19 +01:00
|
|
|
EXPECT_EQ(&i, &res);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WithArgsTest, InnerActionWithConversion) {
|
|
|
|
Action<Derived*()> inner = [] { return nullptr; };
|
|
|
|
Action<Base*(double)> a = testing::WithoutArgs(inner);
|
|
|
|
EXPECT_EQ(nullptr, a.Perform(std::make_tuple(1.1)));
|
|
|
|
}
|
|
|
|
|
2009-09-24 23:17:24 +02:00
|
|
|
#if !GTEST_OS_WINDOWS_MOBILE
|
2009-03-12 00:37:56 +01:00
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
class SetErrnoAndReturnTest : public testing::Test {
|
|
|
|
protected:
|
2018-12-03 17:30:02 +01:00
|
|
|
void SetUp() override { errno = 0; }
|
|
|
|
void TearDown() override { errno = 0; }
|
2008-12-10 06:08:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(SetErrnoAndReturnTest, Int) {
|
|
|
|
Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(-5, a.Perform(std::make_tuple()));
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ(ENOTTY, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SetErrnoAndReturnTest, Ptr) {
|
|
|
|
int x;
|
|
|
|
Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(&x, a.Perform(std::make_tuple()));
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ(ENOTTY, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
|
|
|
|
Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_DOUBLE_EQ(5.0, a.Perform(std::make_tuple()));
|
2008-12-10 06:08:54 +01:00
|
|
|
EXPECT_EQ(EINVAL, errno);
|
|
|
|
}
|
|
|
|
|
2009-09-24 23:17:24 +02:00
|
|
|
#endif // !GTEST_OS_WINDOWS_MOBILE
|
2009-03-12 00:37:56 +01:00
|
|
|
|
2009-07-23 01:58:19 +02:00
|
|
|
// Tests ByRef().
|
|
|
|
|
2019-01-12 18:26:37 +01:00
|
|
|
// Tests that the result of ByRef() is copyable.
|
2009-07-23 01:58:19 +02:00
|
|
|
TEST(ByRefTest, IsCopyable) {
|
|
|
|
const std::string s1 = "Hi";
|
|
|
|
const std::string s2 = "Hello";
|
|
|
|
|
2019-01-12 18:26:37 +01:00
|
|
|
auto ref_wrapper = ByRef(s1);
|
2009-07-23 01:58:19 +02:00
|
|
|
const std::string& r1 = ref_wrapper;
|
|
|
|
EXPECT_EQ(&s1, &r1);
|
|
|
|
|
|
|
|
// Assigns a new value to ref_wrapper.
|
|
|
|
ref_wrapper = ByRef(s2);
|
|
|
|
const std::string& r2 = ref_wrapper;
|
|
|
|
EXPECT_EQ(&s2, &r2);
|
|
|
|
|
2019-01-12 18:26:37 +01:00
|
|
|
auto ref_wrapper1 = ByRef(s1);
|
2009-07-23 01:58:19 +02:00
|
|
|
// Copies ref_wrapper1 to ref_wrapper.
|
|
|
|
ref_wrapper = ref_wrapper1;
|
|
|
|
const std::string& r3 = ref_wrapper;
|
|
|
|
EXPECT_EQ(&s1, &r3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests using ByRef() on a const value.
|
|
|
|
TEST(ByRefTest, ConstValue) {
|
|
|
|
const int n = 0;
|
|
|
|
// int& ref = ByRef(n); // This shouldn't compile - we have a
|
|
|
|
// negative compilation test to catch it.
|
|
|
|
const int& const_ref = ByRef(n);
|
|
|
|
EXPECT_EQ(&n, &const_ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests using ByRef() on a non-const value.
|
|
|
|
TEST(ByRefTest, NonConstValue) {
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
// ByRef(n) can be used as either an int&,
|
|
|
|
int& ref = ByRef(n);
|
|
|
|
EXPECT_EQ(&n, &ref);
|
|
|
|
|
|
|
|
// or a const int&.
|
|
|
|
const int& const_ref = ByRef(n);
|
|
|
|
EXPECT_EQ(&n, &const_ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests explicitly specifying the type when using ByRef().
|
|
|
|
TEST(ByRefTest, ExplicitType) {
|
|
|
|
int n = 0;
|
|
|
|
const int& r1 = ByRef<const int>(n);
|
|
|
|
EXPECT_EQ(&n, &r1);
|
|
|
|
|
|
|
|
// ByRef<char>(n); // This shouldn't compile - we have a negative
|
|
|
|
// compilation test to catch it.
|
|
|
|
|
|
|
|
Derived d;
|
|
|
|
Derived& r2 = ByRef<Derived>(d);
|
|
|
|
EXPECT_EQ(&d, &r2);
|
|
|
|
|
|
|
|
const Derived& r3 = ByRef<const Derived>(d);
|
|
|
|
EXPECT_EQ(&d, &r3);
|
|
|
|
|
|
|
|
Base& r4 = ByRef<Base>(d);
|
|
|
|
EXPECT_EQ(&d, &r4);
|
|
|
|
|
|
|
|
const Base& r5 = ByRef<const Base>(d);
|
|
|
|
EXPECT_EQ(&d, &r5);
|
|
|
|
|
|
|
|
// The following shouldn't compile - we have a negative compilation
|
|
|
|
// test for it.
|
|
|
|
//
|
|
|
|
// Base b;
|
|
|
|
// ByRef<Derived>(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that Google Mock prints expression ByRef(x) as a reference to x.
|
|
|
|
TEST(ByRefTest, PrintsCorrectly) {
|
|
|
|
int n = 42;
|
|
|
|
::std::stringstream expected, actual;
|
|
|
|
testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
|
|
|
|
testing::internal::UniversalPrint(ByRef(n), &actual);
|
|
|
|
EXPECT_EQ(expected.str(), actual.str());
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:17:47 +02:00
|
|
|
struct UnaryConstructorClass {
|
|
|
|
explicit UnaryConstructorClass(int v) : value(v) {}
|
|
|
|
int value;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Tests using ReturnNew() with a unary constructor.
|
|
|
|
TEST(ReturnNewTest, Unary) {
|
|
|
|
Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000);
|
|
|
|
UnaryConstructorClass* c = a.Perform(std::make_tuple());
|
|
|
|
EXPECT_EQ(4000, c->value);
|
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
|
|
|
|
Action<UnaryConstructorClass*(bool, int)> a =
|
|
|
|
ReturnNew<UnaryConstructorClass>(4000);
|
|
|
|
UnaryConstructorClass* c = a.Perform(std::make_tuple(false, 5));
|
|
|
|
EXPECT_EQ(4000, c->value);
|
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
|
|
|
|
Action<const UnaryConstructorClass*()> a =
|
|
|
|
ReturnNew<UnaryConstructorClass>(4000);
|
|
|
|
const UnaryConstructorClass* c = a.Perform(std::make_tuple());
|
|
|
|
EXPECT_EQ(4000, c->value);
|
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
|
|
|
|
class TenArgConstructorClass {
|
|
|
|
public:
|
|
|
|
TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5, int a6, int a7,
|
|
|
|
int a8, int a9, int a10)
|
|
|
|
: value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {}
|
|
|
|
int value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Tests using ReturnNew() with a 10-argument constructor.
|
|
|
|
TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
|
|
|
|
Action<TenArgConstructorClass*()> a = ReturnNew<TenArgConstructorClass>(
|
|
|
|
1000000000, 200000000, 30000000, 4000000, 500000, 60000, 7000, 800, 90,
|
|
|
|
0);
|
|
|
|
TenArgConstructorClass* c = a.Perform(std::make_tuple());
|
|
|
|
EXPECT_EQ(1234567890, c->value_);
|
|
|
|
delete c;
|
|
|
|
}
|
2014-01-29 07:41:44 +01:00
|
|
|
|
|
|
|
std::unique_ptr<int> UniquePtrSource() {
|
|
|
|
return std::unique_ptr<int>(new int(19));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
|
|
|
|
std::vector<std::unique_ptr<int>> out;
|
|
|
|
out.emplace_back(new int(7));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2014-11-17 01:56:52 +01:00
|
|
|
TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
|
|
|
|
MockClass mock;
|
|
|
|
std::unique_ptr<int> i(new int(19));
|
|
|
|
EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i))));
|
|
|
|
EXPECT_CALL(mock, MakeVectorUnique())
|
|
|
|
.WillOnce(Return(ByMove(VectorUniquePtrSource())));
|
|
|
|
Derived* d = new Derived;
|
|
|
|
EXPECT_CALL(mock, MakeUniqueBase())
|
|
|
|
.WillOnce(Return(ByMove(std::unique_ptr<Derived>(d))));
|
|
|
|
|
|
|
|
std::unique_ptr<int> result1 = mock.MakeUnique();
|
|
|
|
EXPECT_EQ(19, *result1);
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
|
2014-11-17 02:08:51 +01:00
|
|
|
EXPECT_EQ(1u, vresult.size());
|
2014-11-17 01:56:52 +01:00
|
|
|
EXPECT_NE(nullptr, vresult[0]);
|
|
|
|
EXPECT_EQ(7, *vresult[0]);
|
|
|
|
|
|
|
|
std::unique_ptr<Base> result2 = mock.MakeUniqueBase();
|
|
|
|
EXPECT_EQ(d, result2.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
|
|
|
|
testing::MockFunction<void()> mock_function;
|
|
|
|
MockClass mock;
|
|
|
|
std::unique_ptr<int> i(new int(19));
|
|
|
|
EXPECT_CALL(mock_function, Call());
|
|
|
|
EXPECT_CALL(mock, MakeUnique()).WillOnce(DoAll(
|
|
|
|
InvokeWithoutArgs(&mock_function, &testing::MockFunction<void()>::Call),
|
|
|
|
Return(ByMove(std::move(i)))));
|
|
|
|
|
|
|
|
std::unique_ptr<int> result1 = mock.MakeUnique();
|
|
|
|
EXPECT_EQ(19, *result1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
|
2014-01-29 07:41:44 +01:00
|
|
|
MockClass mock;
|
|
|
|
|
|
|
|
// Check default value
|
|
|
|
DefaultValue<std::unique_ptr<int>>::SetFactory([] {
|
|
|
|
return std::unique_ptr<int>(new int(42));
|
|
|
|
});
|
|
|
|
EXPECT_EQ(42, *mock.MakeUnique());
|
|
|
|
|
2014-11-17 01:56:52 +01:00
|
|
|
EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource));
|
2014-01-29 07:41:44 +01:00
|
|
|
EXPECT_CALL(mock, MakeVectorUnique())
|
|
|
|
.WillRepeatedly(Invoke(VectorUniquePtrSource));
|
|
|
|
std::unique_ptr<int> result1 = mock.MakeUnique();
|
|
|
|
EXPECT_EQ(19, *result1);
|
|
|
|
std::unique_ptr<int> result2 = mock.MakeUnique();
|
|
|
|
EXPECT_EQ(19, *result2);
|
|
|
|
EXPECT_NE(result1, result2);
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
|
2014-11-17 02:08:51 +01:00
|
|
|
EXPECT_EQ(1u, vresult.size());
|
2014-01-29 07:41:44 +01:00
|
|
|
EXPECT_NE(nullptr, vresult[0]);
|
|
|
|
EXPECT_EQ(7, *vresult[0]);
|
|
|
|
}
|
|
|
|
|
2018-04-13 17:02:25 +02:00
|
|
|
TEST(MockMethodTest, CanTakeMoveOnlyValue) {
|
|
|
|
MockClass mock;
|
|
|
|
auto make = [](int i) { return std::unique_ptr<int>(new int(i)); };
|
|
|
|
|
|
|
|
EXPECT_CALL(mock, TakeUnique(_)).WillRepeatedly([](std::unique_ptr<int> i) {
|
|
|
|
return *i;
|
|
|
|
});
|
|
|
|
// DoAll() does not compile, since it would move from its arguments twice.
|
|
|
|
// EXPECT_CALL(mock, TakeUnique(_, _))
|
|
|
|
// .WillRepeatedly(DoAll(Invoke([](std::unique_ptr<int> j) {}),
|
|
|
|
// Return(1)));
|
|
|
|
EXPECT_CALL(mock, TakeUnique(testing::Pointee(7)))
|
|
|
|
.WillOnce(Return(-7))
|
|
|
|
.RetiresOnSaturation();
|
|
|
|
EXPECT_CALL(mock, TakeUnique(testing::IsNull()))
|
|
|
|
.WillOnce(Return(-1))
|
|
|
|
.RetiresOnSaturation();
|
|
|
|
|
|
|
|
EXPECT_EQ(5, mock.TakeUnique(make(5)));
|
|
|
|
EXPECT_EQ(-7, mock.TakeUnique(make(7)));
|
|
|
|
EXPECT_EQ(7, mock.TakeUnique(make(7)));
|
|
|
|
EXPECT_EQ(7, mock.TakeUnique(make(7)));
|
|
|
|
EXPECT_EQ(-1, mock.TakeUnique({}));
|
|
|
|
|
|
|
|
// Some arguments are moved, some passed by reference.
|
|
|
|
auto lvalue = make(6);
|
|
|
|
EXPECT_CALL(mock, TakeUnique(_, _))
|
|
|
|
.WillOnce([](const std::unique_ptr<int>& i, std::unique_ptr<int> j) {
|
|
|
|
return *i * *j;
|
|
|
|
});
|
|
|
|
EXPECT_EQ(42, mock.TakeUnique(lvalue, make(7)));
|
|
|
|
|
|
|
|
// The unique_ptr can be saved by the action.
|
|
|
|
std::unique_ptr<int> saved;
|
|
|
|
EXPECT_CALL(mock, TakeUnique(_)).WillOnce([&saved](std::unique_ptr<int> i) {
|
|
|
|
saved = std::move(i);
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
EXPECT_EQ(0, mock.TakeUnique(make(42)));
|
|
|
|
EXPECT_EQ(42, *saved);
|
|
|
|
}
|
|
|
|
|
2014-01-29 07:41:44 +01:00
|
|
|
|
2018-04-13 17:02:25 +02:00
|
|
|
// Tests for std::function based action.
|
|
|
|
|
|
|
|
int Add(int val, int& ref, int* ptr) { // NOLINT
|
|
|
|
int result = val + ref + *ptr;
|
|
|
|
ref = 42;
|
|
|
|
*ptr = 43;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Deref(std::unique_ptr<int> ptr) { return *ptr; }
|
|
|
|
|
|
|
|
struct Double {
|
|
|
|
template <typename T>
|
|
|
|
T operator()(T t) { return 2 * t; }
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<int> UniqueInt(int i) {
|
|
|
|
return std::unique_ptr<int>(new int(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FunctorActionTest, ActionFromFunction) {
|
|
|
|
Action<int(int, int&, int*)> a = &Add;
|
|
|
|
int x = 1, y = 2, z = 3;
|
|
|
|
EXPECT_EQ(6, a.Perform(std::forward_as_tuple(x, y, &z)));
|
|
|
|
EXPECT_EQ(42, y);
|
|
|
|
EXPECT_EQ(43, z);
|
|
|
|
|
|
|
|
Action<int(std::unique_ptr<int>)> a1 = &Deref;
|
|
|
|
EXPECT_EQ(7, a1.Perform(std::make_tuple(UniqueInt(7))));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FunctorActionTest, ActionFromLambda) {
|
|
|
|
Action<int(bool, int)> a1 = [](bool b, int i) { return b ? i : 0; };
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
|
|
|
|
EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 5)));
|
2018-04-13 17:02:25 +02:00
|
|
|
|
|
|
|
std::unique_ptr<int> saved;
|
|
|
|
Action<void(std::unique_ptr<int>)> a2 = [&saved](std::unique_ptr<int> p) {
|
|
|
|
saved = std::move(p);
|
|
|
|
};
|
2018-10-09 20:50:26 +02:00
|
|
|
a2.Perform(std::make_tuple(UniqueInt(5)));
|
2018-04-13 17:02:25 +02:00
|
|
|
EXPECT_EQ(5, *saved);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FunctorActionTest, PolymorphicFunctor) {
|
|
|
|
Action<int(int)> ai = Double();
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(2, ai.Perform(std::make_tuple(1)));
|
2018-04-13 17:02:25 +02:00
|
|
|
Action<double(double)> ad = Double(); // Double? Double double!
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(3.0, ad.Perform(std::make_tuple(1.5)));
|
2018-04-13 17:02:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FunctorActionTest, TypeConversion) {
|
|
|
|
// Numeric promotions are allowed.
|
|
|
|
const Action<bool(int)> a1 = [](int i) { return i > 1; };
|
|
|
|
const Action<int(bool)> a2 = Action<int(bool)>(a1);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(1, a1.Perform(std::make_tuple(42)));
|
|
|
|
EXPECT_EQ(0, a2.Perform(std::make_tuple(42)));
|
2018-04-13 17:02:25 +02:00
|
|
|
|
|
|
|
// Implicit constructors are allowed.
|
|
|
|
const Action<bool(std::string)> s1 = [](std::string s) { return !s.empty(); };
|
|
|
|
const Action<int(const char*)> s2 = Action<int(const char*)>(s1);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(0, s2.Perform(std::make_tuple("")));
|
|
|
|
EXPECT_EQ(1, s2.Perform(std::make_tuple("hello")));
|
2018-04-13 17:02:25 +02:00
|
|
|
|
|
|
|
// Also between the lambda and the action itself.
|
2020-02-20 05:05:01 +01:00
|
|
|
const Action<bool(std::string)> x1 = [](Unused) { return 42; };
|
|
|
|
const Action<bool(std::string)> x2 = [] { return 42; };
|
|
|
|
EXPECT_TRUE(x1.Perform(std::make_tuple("hello")));
|
|
|
|
EXPECT_TRUE(x2.Perform(std::make_tuple("hello")));
|
|
|
|
|
|
|
|
// Ensure decay occurs where required.
|
|
|
|
std::function<int()> f = [] { return 7; };
|
|
|
|
Action<int(int)> d = f;
|
|
|
|
f = nullptr;
|
|
|
|
EXPECT_EQ(7, d.Perform(std::make_tuple(1)));
|
|
|
|
|
|
|
|
// Ensure creation of an empty action succeeds.
|
|
|
|
Action<void(int)>(nullptr);
|
2018-04-13 17:02:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FunctorActionTest, UnusedArguments) {
|
|
|
|
// Verify that users can ignore uninteresting arguments.
|
2018-04-16 16:34:07 +02:00
|
|
|
Action<int(int, double y, double z)> a =
|
2018-04-13 17:02:25 +02:00
|
|
|
[](int i, Unused, Unused) { return 2 * i; };
|
2018-10-09 20:50:26 +02:00
|
|
|
std::tuple<int, double, double> dummy = std::make_tuple(3, 7.3, 9.44);
|
2018-04-13 17:49:37 +02:00
|
|
|
EXPECT_EQ(6, a.Perform(dummy));
|
2018-04-13 17:02:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that basic built-in actions work with move-only arguments.
|
|
|
|
TEST(MoveOnlyArgumentsTest, ReturningActions) {
|
|
|
|
Action<int(std::unique_ptr<int>)> a = Return(1);
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(1, a.Perform(std::make_tuple(nullptr)));
|
2018-04-13 17:02:25 +02:00
|
|
|
|
|
|
|
a = testing::WithoutArgs([]() { return 7; });
|
2018-10-09 20:50:26 +02:00
|
|
|
EXPECT_EQ(7, a.Perform(std::make_tuple(nullptr)));
|
2018-04-13 17:02:25 +02:00
|
|
|
|
|
|
|
Action<void(std::unique_ptr<int>, int*)> a2 = testing::SetArgPointee<1>(3);
|
|
|
|
int x = 0;
|
2018-10-09 20:50:26 +02:00
|
|
|
a2.Perform(std::make_tuple(nullptr, &x));
|
2018-04-13 17:02:25 +02:00
|
|
|
EXPECT_EQ(x, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-10 06:08:54 +01:00
|
|
|
} // Unnamed namespace
|
2018-04-10 21:57:16 +02:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#if _MSC_VER == 1900
|
|
|
|
# pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|