Moves the universal printer from gmock to gtest and refactors the cmake script for reusing in gmock (by Vlad Losev).
This commit is contained in:
@@ -97,6 +97,9 @@ inline void GTestStreamToHelper(std::ostream* os, const T& val) {
|
||||
*os << val;
|
||||
}
|
||||
|
||||
class ProtocolMessage;
|
||||
namespace proto2 { class Message; }
|
||||
|
||||
namespace testing {
|
||||
|
||||
// Forward declaration of classes.
|
||||
@@ -784,6 +787,292 @@ class GTEST_API_ Random {
|
||||
GTEST_DISALLOW_COPY_AND_ASSIGN_(Random);
|
||||
};
|
||||
|
||||
// Defining a variable of type CompileAssertTypesEqual<T1, T2> will cause a
|
||||
// compiler error iff T1 and T2 are different types.
|
||||
template <typename T1, typename T2>
|
||||
struct CompileAssertTypesEqual;
|
||||
|
||||
template <typename T>
|
||||
struct CompileAssertTypesEqual<T, T> {
|
||||
};
|
||||
|
||||
// Removes the reference from a type if it is a reference type,
|
||||
// otherwise leaves it unchanged. This is the same as
|
||||
// tr1::remove_reference, which is not widely available yet.
|
||||
template <typename T>
|
||||
struct RemoveReference { typedef T type; }; // NOLINT
|
||||
template <typename T>
|
||||
struct RemoveReference<T&> { typedef T type; }; // NOLINT
|
||||
|
||||
// A handy wrapper around RemoveReference that works when the argument
|
||||
// T depends on template parameters.
|
||||
#define GTEST_REMOVE_REFERENCE_(T) \
|
||||
typename ::testing::internal::RemoveReference<T>::type
|
||||
|
||||
// Removes const from a type if it is a const type, otherwise leaves
|
||||
// it unchanged. This is the same as tr1::remove_const, which is not
|
||||
// widely available yet.
|
||||
template <typename T>
|
||||
struct RemoveConst { typedef T type; }; // NOLINT
|
||||
template <typename T>
|
||||
struct RemoveConst<const T> { typedef T type; }; // NOLINT
|
||||
|
||||
// MSVC 8.0 has a bug which causes the above definition to fail to
|
||||
// remove the const in 'const int[3]'. The following specialization
|
||||
// works around the bug. However, it causes trouble with gcc and thus
|
||||
// needs to be conditionally compiled.
|
||||
#ifdef _MSC_VER
|
||||
template <typename T, size_t N>
|
||||
struct RemoveConst<T[N]> {
|
||||
typedef typename RemoveConst<T>::type type[N];
|
||||
};
|
||||
#endif // _MSC_VER
|
||||
|
||||
// A handy wrapper around RemoveConst that works when the argument
|
||||
// T depends on template parameters.
|
||||
#define GTEST_REMOVE_CONST_(T) \
|
||||
typename ::testing::internal::RemoveConst<T>::type
|
||||
|
||||
// Adds reference to a type if it is not a reference type,
|
||||
// otherwise leaves it unchanged. This is the same as
|
||||
// tr1::add_reference, which is not widely available yet.
|
||||
template <typename T>
|
||||
struct AddReference { typedef T& type; }; // NOLINT
|
||||
template <typename T>
|
||||
struct AddReference<T&> { typedef T& type; }; // NOLINT
|
||||
|
||||
// A handy wrapper around AddReference that works when the argument T
|
||||
// depends on template parameters.
|
||||
#define GTEST_ADD_REFERENCE_(T) \
|
||||
typename ::testing::internal::AddReference<T>::type
|
||||
|
||||
// Adds a reference to const on top of T as necessary. For example,
|
||||
// it transforms
|
||||
//
|
||||
// char ==> const char&
|
||||
// const char ==> const char&
|
||||
// char& ==> const char&
|
||||
// const char& ==> const char&
|
||||
//
|
||||
// The argument T must depend on some template parameters.
|
||||
#define GTEST_REFERENCE_TO_CONST_(T) \
|
||||
GTEST_ADD_REFERENCE_(const GTEST_REMOVE_REFERENCE_(T))
|
||||
|
||||
// ImplicitlyConvertible<From, To>::value is a compile-time bool
|
||||
// constant that's true iff type From can be implicitly converted to
|
||||
// type To.
|
||||
template <typename From, typename To>
|
||||
class ImplicitlyConvertible {
|
||||
private:
|
||||
// We need the following helper functions only for their types.
|
||||
// They have no implementations.
|
||||
|
||||
// MakeFrom() is an expression whose type is From. We cannot simply
|
||||
// use From(), as the type From may not have a public default
|
||||
// constructor.
|
||||
static From MakeFrom();
|
||||
|
||||
// These two functions are overloaded. Given an expression
|
||||
// Helper(x), the compiler will pick the first version if x can be
|
||||
// implicitly converted to type To; otherwise it will pick the
|
||||
// second version.
|
||||
//
|
||||
// The first version returns a value of size 1, and the second
|
||||
// version returns a value of size 2. Therefore, by checking the
|
||||
// size of Helper(x), which can be done at compile time, we can tell
|
||||
// which version of Helper() is used, and hence whether x can be
|
||||
// implicitly converted to type To.
|
||||
static char Helper(To);
|
||||
static char (&Helper(...))[2]; // NOLINT
|
||||
|
||||
// We have to put the 'public' section after the 'private' section,
|
||||
// or MSVC refuses to compile the code.
|
||||
public:
|
||||
// MSVC warns about implicitly converting from double to int for
|
||||
// possible loss of data, so we need to temporarily disable the
|
||||
// warning.
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push) // Saves the current warning state.
|
||||
#pragma warning(disable:4244) // Temporarily disables warning 4244.
|
||||
static const bool value =
|
||||
sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1;
|
||||
#pragma warning(pop) // Restores the warning state.
|
||||
#else
|
||||
static const bool value =
|
||||
sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1;
|
||||
#endif // _MSV_VER
|
||||
};
|
||||
template <typename From, typename To>
|
||||
const bool ImplicitlyConvertible<From, To>::value;
|
||||
|
||||
// IsAProtocolMessage<T>::value is a compile-time bool constant that's
|
||||
// true iff T is type ProtocolMessage, proto2::Message, or a subclass
|
||||
// of those.
|
||||
template <typename T>
|
||||
struct IsAProtocolMessage
|
||||
: public bool_constant<
|
||||
ImplicitlyConvertible<const T*, const ::ProtocolMessage*>::value ||
|
||||
ImplicitlyConvertible<const T*, const ::proto2::Message*>::value> {
|
||||
};
|
||||
|
||||
// When the compiler sees expression IsContainerTest<C>(0), the first
|
||||
// overload of IsContainerTest will be picked if C is an STL-style
|
||||
// container class (since C::const_iterator* is a valid type and 0 can
|
||||
// be converted to it), while the second overload will be picked
|
||||
// otherwise (since C::const_iterator will be an invalid type in this
|
||||
// case). Therefore, we can determine whether C is a container class
|
||||
// by checking the type of IsContainerTest<C>(0). The value of the
|
||||
// expression is insignificant.
|
||||
typedef int IsContainer;
|
||||
template <class C>
|
||||
IsContainer IsContainerTest(typename C::const_iterator*) { return 0; }
|
||||
|
||||
typedef char IsNotContainer;
|
||||
template <class C>
|
||||
IsNotContainer IsContainerTest(...) { return '\0'; }
|
||||
|
||||
// Utilities for native arrays.
|
||||
|
||||
// ArrayEq() compares two k-dimensional native arrays using the
|
||||
// elements' operator==, where k can be any integer >= 0. When k is
|
||||
// 0, ArrayEq() degenerates into comparing a single pair of values.
|
||||
|
||||
template <typename T, typename U>
|
||||
bool ArrayEq(const T* lhs, size_t size, const U* rhs);
|
||||
|
||||
// This generic version is used when k is 0.
|
||||
template <typename T, typename U>
|
||||
inline bool ArrayEq(const T& lhs, const U& rhs) { return lhs == rhs; }
|
||||
|
||||
// This overload is used when k >= 1.
|
||||
template <typename T, typename U, size_t N>
|
||||
inline bool ArrayEq(const T(&lhs)[N], const U(&rhs)[N]) {
|
||||
return internal::ArrayEq(lhs, N, rhs);
|
||||
}
|
||||
|
||||
// This helper reduces code bloat. If we instead put its logic inside
|
||||
// the previous ArrayEq() function, arrays with different sizes would
|
||||
// lead to different copies of the template code.
|
||||
template <typename T, typename U>
|
||||
bool ArrayEq(const T* lhs, size_t size, const U* rhs) {
|
||||
for (size_t i = 0; i != size; i++) {
|
||||
if (!internal::ArrayEq(lhs[i], rhs[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Finds the first element in the iterator range [begin, end) that
|
||||
// equals elem. Element may be a native array type itself.
|
||||
template <typename Iter, typename Element>
|
||||
Iter ArrayAwareFind(Iter begin, Iter end, const Element& elem) {
|
||||
for (Iter it = begin; it != end; ++it) {
|
||||
if (internal::ArrayEq(*it, elem))
|
||||
return it;
|
||||
}
|
||||
return end;
|
||||
}
|
||||
|
||||
// CopyArray() copies a k-dimensional native array using the elements'
|
||||
// operator=, where k can be any integer >= 0. When k is 0,
|
||||
// CopyArray() degenerates into copying a single value.
|
||||
|
||||
template <typename T, typename U>
|
||||
void CopyArray(const T* from, size_t size, U* to);
|
||||
|
||||
// This generic version is used when k is 0.
|
||||
template <typename T, typename U>
|
||||
inline void CopyArray(const T& from, U* to) { *to = from; }
|
||||
|
||||
// This overload is used when k >= 1.
|
||||
template <typename T, typename U, size_t N>
|
||||
inline void CopyArray(const T(&from)[N], U(*to)[N]) {
|
||||
internal::CopyArray(from, N, *to);
|
||||
}
|
||||
|
||||
// This helper reduces code bloat. If we instead put its logic inside
|
||||
// the previous CopyArray() function, arrays with different sizes
|
||||
// would lead to different copies of the template code.
|
||||
template <typename T, typename U>
|
||||
void CopyArray(const T* from, size_t size, U* to) {
|
||||
for (size_t i = 0; i != size; i++) {
|
||||
internal::CopyArray(from[i], to + i);
|
||||
}
|
||||
}
|
||||
|
||||
// The relation between an NativeArray object (see below) and the
|
||||
// native array it represents.
|
||||
enum RelationToSource {
|
||||
kReference, // The NativeArray references the native array.
|
||||
kCopy // The NativeArray makes a copy of the native array and
|
||||
// owns the copy.
|
||||
};
|
||||
|
||||
// Adapts a native array to a read-only STL-style container. Instead
|
||||
// of the complete STL container concept, this adaptor only implements
|
||||
// members useful for Google Mock's container matchers. New members
|
||||
// should be added as needed. To simplify the implementation, we only
|
||||
// support Element being a raw type (i.e. having no top-level const or
|
||||
// reference modifier). It's the client's responsibility to satisfy
|
||||
// this requirement. Element can be an array type itself (hence
|
||||
// multi-dimensional arrays are supported).
|
||||
template <typename Element>
|
||||
class NativeArray {
|
||||
public:
|
||||
// STL-style container typedefs.
|
||||
typedef Element value_type;
|
||||
typedef const Element* const_iterator;
|
||||
|
||||
// Constructs from a native array.
|
||||
NativeArray(const Element* array, size_t count, RelationToSource relation) {
|
||||
Init(array, count, relation);
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
NativeArray(const NativeArray& rhs) {
|
||||
Init(rhs.array_, rhs.size_, rhs.relation_to_source_);
|
||||
}
|
||||
|
||||
~NativeArray() {
|
||||
// Ensures that the user doesn't instantiate NativeArray with a
|
||||
// const or reference type.
|
||||
static_cast<void>(StaticAssertTypeEqHelper<Element,
|
||||
GTEST_REMOVE_CONST_(GTEST_REMOVE_REFERENCE_(Element))>());
|
||||
if (relation_to_source_ == kCopy)
|
||||
delete[] array_;
|
||||
}
|
||||
|
||||
// STL-style container methods.
|
||||
size_t size() const { return size_; }
|
||||
const_iterator begin() const { return array_; }
|
||||
const_iterator end() const { return array_ + size_; }
|
||||
bool operator==(const NativeArray& rhs) const {
|
||||
return size() == rhs.size() &&
|
||||
ArrayEq(begin(), size(), rhs.begin());
|
||||
}
|
||||
|
||||
private:
|
||||
// Initializes this object; makes a copy of the input array if
|
||||
// 'relation' is kCopy.
|
||||
void Init(const Element* array, size_t a_size, RelationToSource relation) {
|
||||
if (relation == kReference) {
|
||||
array_ = array;
|
||||
} else {
|
||||
Element* const copy = new Element[a_size];
|
||||
CopyArray(array, a_size, copy);
|
||||
array_ = copy;
|
||||
}
|
||||
size_ = a_size;
|
||||
relation_to_source_ = relation;
|
||||
}
|
||||
|
||||
const Element* array_;
|
||||
size_t size_;
|
||||
RelationToSource relation_to_source_;
|
||||
|
||||
GTEST_DISALLOW_ASSIGN_(NativeArray);
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace testing
|
||||
|
||||
|
||||
@@ -609,6 +609,91 @@ namespace internal {
|
||||
|
||||
class String;
|
||||
|
||||
// The GTEST_COMPILE_ASSERT_ macro can be used to verify that a compile time
|
||||
// expression is true. For example, you could use it to verify the
|
||||
// size of a static array:
|
||||
//
|
||||
// GTEST_COMPILE_ASSERT_(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
|
||||
// content_type_names_incorrect_size);
|
||||
//
|
||||
// or to make sure a struct is smaller than a certain size:
|
||||
//
|
||||
// GTEST_COMPILE_ASSERT_(sizeof(foo) < 128, foo_too_large);
|
||||
//
|
||||
// The second argument to the macro is the name of the variable. If
|
||||
// the expression is false, most compilers will issue a warning/error
|
||||
// containing the name of the variable.
|
||||
|
||||
template <bool>
|
||||
struct CompileAssert {
|
||||
};
|
||||
|
||||
#define GTEST_COMPILE_ASSERT_(expr, msg) \
|
||||
typedef ::testing::internal::CompileAssert<(bool(expr))> \
|
||||
msg[bool(expr) ? 1 : -1]
|
||||
|
||||
// Implementation details of GTEST_COMPILE_ASSERT_:
|
||||
//
|
||||
// - GTEST_COMPILE_ASSERT_ works by defining an array type that has -1
|
||||
// elements (and thus is invalid) when the expression is false.
|
||||
//
|
||||
// - The simpler definition
|
||||
//
|
||||
// #define GTEST_COMPILE_ASSERT_(expr, msg) typedef char msg[(expr) ? 1 : -1]
|
||||
//
|
||||
// does not work, as gcc supports variable-length arrays whose sizes
|
||||
// are determined at run-time (this is gcc's extension and not part
|
||||
// of the C++ standard). As a result, gcc fails to reject the
|
||||
// following code with the simple definition:
|
||||
//
|
||||
// int foo;
|
||||
// GTEST_COMPILE_ASSERT_(foo, msg); // not supposed to compile as foo is
|
||||
// // not a compile-time constant.
|
||||
//
|
||||
// - By using the type CompileAssert<(bool(expr))>, we ensures that
|
||||
// expr is a compile-time constant. (Template arguments must be
|
||||
// determined at compile-time.)
|
||||
//
|
||||
// - The outter parentheses in CompileAssert<(bool(expr))> are necessary
|
||||
// to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
|
||||
//
|
||||
// CompileAssert<bool(expr)>
|
||||
//
|
||||
// instead, these compilers will refuse to compile
|
||||
//
|
||||
// GTEST_COMPILE_ASSERT_(5 > 0, some_message);
|
||||
//
|
||||
// (They seem to think the ">" in "5 > 0" marks the end of the
|
||||
// template argument list.)
|
||||
//
|
||||
// - The array size is (bool(expr) ? 1 : -1), instead of simply
|
||||
//
|
||||
// ((expr) ? 1 : -1).
|
||||
//
|
||||
// This is to avoid running into a bug in MS VC 7.1, which
|
||||
// causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
|
||||
|
||||
// StaticAssertTypeEqHelper is used by StaticAssertTypeEq defined in gtest.h.
|
||||
//
|
||||
// This template is declared, but intentionally undefined.
|
||||
template <typename T1, typename T2>
|
||||
struct StaticAssertTypeEqHelper;
|
||||
|
||||
template <typename T>
|
||||
struct StaticAssertTypeEqHelper<T, T> {};
|
||||
|
||||
#if GTEST_HAS_GLOBAL_STRING
|
||||
typedef ::string string;
|
||||
#else
|
||||
typedef ::std::string string;
|
||||
#endif // GTEST_HAS_GLOBAL_STRING
|
||||
|
||||
#if GTEST_HAS_GLOBAL_WSTRING
|
||||
typedef ::wstring wstring;
|
||||
#elif GTEST_HAS_STD_WSTRING
|
||||
typedef ::std::wstring wstring;
|
||||
#endif // GTEST_HAS_GLOBAL_WSTRING
|
||||
|
||||
typedef ::std::stringstream StrStream;
|
||||
|
||||
// A helper for suppressing warnings on constant condition. It just
|
||||
@@ -790,6 +875,58 @@ inline void FlushInfoLog() { fflush(NULL); }
|
||||
|
||||
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
|
||||
//
|
||||
// Use implicit_cast as a safe version of static_cast for upcasting in
|
||||
// the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a
|
||||
// const Foo*). When you use implicit_cast, the compiler checks that
|
||||
// the cast is safe. Such explicit implicit_casts are necessary in
|
||||
// surprisingly many situations where C++ demands an exact type match
|
||||
// instead of an argument type convertable to a target type.
|
||||
//
|
||||
// The syntax for using implicit_cast is the same as for static_cast:
|
||||
//
|
||||
// implicit_cast<ToType>(expr)
|
||||
//
|
||||
// implicit_cast would have been part of the C++ standard library,
|
||||
// but the proposal was submitted too late. It will probably make
|
||||
// its way into the language in the future.
|
||||
template<typename To>
|
||||
inline To implicit_cast(To x) { return x; }
|
||||
|
||||
// When you upcast (that is, cast a pointer from type Foo to type
|
||||
// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
|
||||
// always succeed. When you downcast (that is, cast a pointer from
|
||||
// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
|
||||
// how do you know the pointer is really of type SubclassOfFoo? It
|
||||
// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
|
||||
// when you downcast, you should use this macro. In debug mode, we
|
||||
// use dynamic_cast<> to double-check the downcast is legal (we die
|
||||
// if it's not). In normal mode, we do the efficient static_cast<>
|
||||
// instead. Thus, it's important to test in debug mode to make sure
|
||||
// the cast is legal!
|
||||
// This is the only place in the code we should use dynamic_cast<>.
|
||||
// In particular, you SHOULDN'T be using dynamic_cast<> in order to
|
||||
// do RTTI (eg code like this:
|
||||
// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
|
||||
// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
|
||||
// You should design the code some other way not to need this.
|
||||
template<typename To, typename From> // use like this: down_cast<T*>(foo);
|
||||
inline To down_cast(From* f) { // so we only accept pointers
|
||||
// Ensures that To is a sub-type of From *. This test is here only
|
||||
// for compile-time type checking, and has no overhead in an
|
||||
// optimized build at run-time, as it will be optimized away
|
||||
// completely.
|
||||
if (false) {
|
||||
const To to = NULL;
|
||||
::testing::internal::implicit_cast<From*>(to);
|
||||
}
|
||||
|
||||
#if GTEST_HAS_RTTI
|
||||
// RTTI: debug mode only!
|
||||
GTEST_CHECK_(f == NULL || dynamic_cast<To>(f) != NULL);
|
||||
#endif
|
||||
return static_cast<To>(f);
|
||||
}
|
||||
|
||||
// Downcasts the pointer of type Base to Derived.
|
||||
// Derived must be a subclass of Base. The parameter MUST
|
||||
// point to a class of type Derived, not any subclass of it.
|
||||
|
||||
Reference in New Issue
Block a user