Skip to content

Commit

Permalink
Update Gtest/Gmock to latest release, 1.8.0 (#1508)
Browse files Browse the repository at this point in the history
* Update gtest and gmock to 1.8.0

* Patch gtest/gmock to work with TB directory structure

Replaced:
#include "src/
with:
#include "
  • Loading branch information
ericwa authored and kduske committed Nov 8, 2016
1 parent c12320d commit 6156a72
Show file tree
Hide file tree
Showing 65 changed files with 10,117 additions and 4,491 deletions.
199 changes: 164 additions & 35 deletions lib/include/gmock/gmock-actions.h
Expand Up @@ -36,16 +36,20 @@
#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_
#define GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_

#include <algorithm>
#include <string>

#ifndef _WIN32_WCE
# include <errno.h>
#endif

#include <algorithm>
#include <string>

#include "gmock/internal/gmock-internal-utils.h"
#include "gmock/internal/gmock-port.h"

#if GTEST_HAS_STD_TYPE_TRAITS_ // Defined by gtest-port.h via gmock-port.h.
#include <type_traits>
#endif

namespace testing {

// To implement an action Foo, define:
Expand All @@ -62,16 +66,17 @@ namespace internal {
template <typename F1, typename F2>
class ActionAdaptor;

// BuiltInDefaultValue<T>::Get() returns the "built-in" default
// value for type T, which is NULL when T is a pointer type, 0 when T
// is a numeric type, false when T is bool, or "" when T is string or
// std::string. For any other type T, this value is undefined and the
// function will abort the process.
// BuiltInDefaultValueGetter<T, true>::Get() returns a
// default-constructed T value. BuiltInDefaultValueGetter<T,
// false>::Get() crashes with an error.
//
// This primary template is used when kDefaultConstructible is true.
template <typename T, bool kDefaultConstructible>
struct BuiltInDefaultValueGetter {
static T Get() { return T(); }
};
template <typename T>
class BuiltInDefaultValue {
public:
// This function returns true iff type T has a built-in default value.
static bool Exists() { return false; }
struct BuiltInDefaultValueGetter<T, false> {
static T Get() {
Assert(false, __FILE__, __LINE__,
"Default action undefined for the function return type.");
Expand All @@ -81,6 +86,40 @@ class BuiltInDefaultValue {
}
};

// BuiltInDefaultValue<T>::Get() returns the "built-in" default value
// for type T, which is NULL when T is a raw pointer type, 0 when T is
// a numeric type, false when T is bool, or "" when T is string or
// std::string. In addition, in C++11 and above, it turns a
// default-constructed T value if T is default constructible. For any
// other type T, the built-in default T value is undefined, and the
// function will abort the process.
template <typename T>
class BuiltInDefaultValue {
public:
#if GTEST_HAS_STD_TYPE_TRAITS_
// This function returns true iff type T has a built-in default value.
static bool Exists() {
return ::std::is_default_constructible<T>::value;
}

static T Get() {
return BuiltInDefaultValueGetter<
T, ::std::is_default_constructible<T>::value>::Get();
}

#else // GTEST_HAS_STD_TYPE_TRAITS_
// This function returns true iff type T has a built-in default value.
static bool Exists() {
return false;
}

static T Get() {
return BuiltInDefaultValueGetter<T, false>::Get();
}

#endif // GTEST_HAS_STD_TYPE_TRAITS_
};

// This partial specialization says that we use the same built-in
// default value for T and const T.
template <typename T>
Expand Down Expand Up @@ -163,18 +202,27 @@ class DefaultValue {
// Sets the default value for type T; requires T to be
// copy-constructable and have a public destructor.
static void Set(T x) {
delete value_;
value_ = new T(x);
delete producer_;
producer_ = new FixedValueProducer(x);
}

// Provides a factory function to be called to generate the default value.
// This method can be used even if T is only move-constructible, but it is not
// limited to that case.
typedef T (*FactoryFunction)();
static void SetFactory(FactoryFunction factory) {
delete producer_;
producer_ = new FactoryValueProducer(factory);
}

// Unsets the default value for type T.
static void Clear() {
delete value_;
value_ = NULL;
delete producer_;
producer_ = NULL;
}

// Returns true iff the user has set the default value for type T.
static bool IsSet() { return value_ != NULL; }
static bool IsSet() { return producer_ != NULL; }

// Returns true if T has a default return value set by the user or there
// exists a built-in default value.
Expand All @@ -183,14 +231,42 @@ class DefaultValue {
}

// Returns the default value for type T if the user has set one;
// otherwise returns the built-in default value if there is one;
// otherwise aborts the process.
// otherwise returns the built-in default value. Requires that Exists()
// is true, which ensures that the return value is well-defined.
static T Get() {
return value_ == NULL ?
internal::BuiltInDefaultValue<T>::Get() : *value_;
return producer_ == NULL ?
internal::BuiltInDefaultValue<T>::Get() : producer_->Produce();
}

private:
static const T* value_;
class ValueProducer {
public:
virtual ~ValueProducer() {}
virtual T Produce() = 0;
};

class FixedValueProducer : public ValueProducer {
public:
explicit FixedValueProducer(T value) : value_(value) {}
virtual T Produce() { return value_; }

private:
const T value_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(FixedValueProducer);
};

class FactoryValueProducer : public ValueProducer {
public:
explicit FactoryValueProducer(FactoryFunction factory)
: factory_(factory) {}
virtual T Produce() { return factory_(); }

private:
const FactoryFunction factory_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(FactoryValueProducer);
};

static ValueProducer* producer_;
};

// This partial specialization allows a user to set default values for
Expand Down Expand Up @@ -224,6 +300,7 @@ class DefaultValue<T&> {
return address_ == NULL ?
internal::BuiltInDefaultValue<T&>::Get() : *address_;
}

private:
static T* address_;
};
Expand All @@ -239,7 +316,7 @@ class DefaultValue<void> {

// Points to the user-set default value for type T.
template <typename T>
const T* DefaultValue<T>::value_ = NULL;
typename DefaultValue<T>::ValueProducer* DefaultValue<T>::producer_ = NULL;

// Points to the user-set default value for type T&.
template <typename T>
Expand Down Expand Up @@ -421,6 +498,14 @@ class ActionAdaptor : public ActionInterface<F1> {
GTEST_DISALLOW_ASSIGN_(ActionAdaptor);
};

// Helper struct to specialize ReturnAction to execute a move instead of a copy
// on return. Useful for move-only types, but could be used on any type.
template <typename T>
struct ByMoveWrapper {
explicit ByMoveWrapper(T value) : payload(internal::move(value)) {}
T payload;
};

// Implements the polymorphic Return(x) action, which can be used in
// any function that returns the type of x, regardless of the argument
// types.
Expand Down Expand Up @@ -451,7 +536,7 @@ class ReturnAction {
// Constructs a ReturnAction object from the value to be returned.
// 'value' is passed by value instead of by const reference in order
// to allow Return("string literal") to compile.
explicit ReturnAction(R value) : value_(value) {}
explicit ReturnAction(R value) : value_(new R(internal::move(value))) {}

// This template type conversion operator allows Return(x) to be
// used in ANY function that returns x's type.
Expand All @@ -467,14 +552,14 @@ class ReturnAction {
// in the Impl class. But both definitions must be the same.
typedef typename Function<F>::Result Result;
GTEST_COMPILE_ASSERT_(
!internal::is_reference<Result>::value,
!is_reference<Result>::value,
use_ReturnRef_instead_of_Return_to_return_a_reference);
return Action<F>(new Impl<F>(value_));
return Action<F>(new Impl<R, F>(value_));
}

private:
// Implements the Return(x) action for a particular function type F.
template <typename F>
template <typename R_, typename F>
class Impl : public ActionInterface<F> {
public:
typedef typename Function<F>::Result Result;
Expand All @@ -487,33 +572,68 @@ class ReturnAction {
// Result to call. ImplicitCast_ forces the compiler to convert R to
// Result without considering explicit constructors, thus resolving the
// ambiguity. value_ is then initialized using its copy constructor.
explicit Impl(R value)
: value_(::testing::internal::ImplicitCast_<Result>(value)) {}
explicit Impl(const linked_ptr<R>& value)
: value_before_cast_(*value),
value_(ImplicitCast_<Result>(value_before_cast_)) {}

virtual Result Perform(const ArgumentTuple&) { return value_; }

private:
GTEST_COMPILE_ASSERT_(!internal::is_reference<Result>::value,
GTEST_COMPILE_ASSERT_(!is_reference<Result>::value,
Result_cannot_be_a_reference_type);
// We save the value before casting just in case it is being cast to a
// wrapper type.
R value_before_cast_;
Result value_;

GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl);
};

// Partially specialize for ByMoveWrapper. This version of ReturnAction will
// move its contents instead.
template <typename R_, typename F>
class Impl<ByMoveWrapper<R_>, F> : public ActionInterface<F> {
public:
typedef typename Function<F>::Result Result;
typedef typename Function<F>::ArgumentTuple ArgumentTuple;

explicit Impl(const linked_ptr<R>& wrapper)
: performed_(false), wrapper_(wrapper) {}

virtual Result Perform(const ArgumentTuple&) {
GTEST_CHECK_(!performed_)
<< "A ByMove() action should only be performed once.";
performed_ = true;
return internal::move(wrapper_->payload);
}

private:
bool performed_;
const linked_ptr<R> wrapper_;

GTEST_DISALLOW_ASSIGN_(Impl);
};

R value_;
const linked_ptr<R> value_;

GTEST_DISALLOW_ASSIGN_(ReturnAction);
};

// Implements the ReturnNull() action.
class ReturnNullAction {
public:
// Allows ReturnNull() to be used in any pointer-returning function.
// Allows ReturnNull() to be used in any pointer-returning function. In C++11
// this is enforced by returning nullptr, and in non-C++11 by asserting a
// pointer type on compile time.
template <typename Result, typename ArgumentTuple>
static Result Perform(const ArgumentTuple&) {
#if GTEST_LANG_CXX11
return nullptr;
#else
GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value,
ReturnNull_can_be_used_to_return_a_pointer_only);
return NULL;
#endif // GTEST_LANG_CXX11
}
};

Expand Down Expand Up @@ -690,7 +810,7 @@ class SetArgumentPointeeAction {
template <typename Result, typename ArgumentTuple>
void Perform(const ArgumentTuple& args) const {
CompileAssertTypesEqual<void, Result>();
*::std::tr1::get<N>(args) = value_;
*::testing::get<N>(args) = value_;
}

private:
Expand All @@ -713,7 +833,7 @@ class SetArgumentPointeeAction<N, Proto, true> {
template <typename Result, typename ArgumentTuple>
void Perform(const ArgumentTuple& args) const {
CompileAssertTypesEqual<void, Result>();
::std::tr1::get<N>(args)->CopyFrom(*proto_);
::testing::get<N>(args)->CopyFrom(*proto_);
}

private:
Expand Down Expand Up @@ -939,7 +1059,7 @@ Action<To>::Action(const Action<From>& from)
// will trigger a compiler error about using array as initializer.
template <typename R>
internal::ReturnAction<R> Return(R value) {
return internal::ReturnAction<R>(value);
return internal::ReturnAction<R>(internal::move(value));
}

// Creates an action that returns NULL.
Expand All @@ -966,6 +1086,15 @@ inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
return internal::ReturnRefOfCopyAction<R>(x);
}

// Modifies the parent action (a Return() action) to perform a move of the
// argument instead of a copy.
// Return(ByMove()) actions can only be executed once and will assert this
// invariant.
template <typename R>
internal::ByMoveWrapper<R> ByMove(R x) {
return internal::ByMoveWrapper<R>(internal::move(x));
}

// Creates an action that does the default action for the give mock function.
inline internal::DoDefaultAction DoDefault() {
return internal::DoDefaultAction();
Expand Down
13 changes: 7 additions & 6 deletions lib/include/gmock/gmock-cardinalities.h
Expand Up @@ -80,7 +80,7 @@ class CardinalityInterface {
// be called. The implementation of Cardinality is just a linked_ptr
// to const CardinalityInterface, so copying is fairly cheap.
// Don't inherit from Cardinality!
class Cardinality {
class GTEST_API_ Cardinality {
public:
// Constructs a null cardinality. Needed for storing Cardinality
// objects in STL containers.
Expand Down Expand Up @@ -117,24 +117,25 @@ class Cardinality {
// Describes the given actual call count to an ostream.
static void DescribeActualCallCountTo(int actual_call_count,
::std::ostream* os);

private:
internal::linked_ptr<const CardinalityInterface> impl_;
};

// Creates a cardinality that allows at least n calls.
Cardinality AtLeast(int n);
GTEST_API_ Cardinality AtLeast(int n);

// Creates a cardinality that allows at most n calls.
Cardinality AtMost(int n);
GTEST_API_ Cardinality AtMost(int n);

// Creates a cardinality that allows any number of calls.
Cardinality AnyNumber();
GTEST_API_ Cardinality AnyNumber();

// Creates a cardinality that allows between min and max calls.
Cardinality Between(int min, int max);
GTEST_API_ Cardinality Between(int min, int max);

// Creates a cardinality that allows exactly n calls.
Cardinality Exactly(int n);
GTEST_API_ Cardinality Exactly(int n);

// Creates a cardinality from its implementation.
inline Cardinality MakeCardinality(const CardinalityInterface* c) {
Expand Down

0 comments on commit 6156a72

Please sign in to comment.