3 Commits

Author SHA1 Message Date
Abseil Team
7f56ae0f08 Export Test - Do Not Merge
Rename internal color enumerators to avoid conflicts with curses.h macro definitions.
Fixes #2685

PiperOrigin-RevId: 297639382
2020-02-28 16:08:31 -05:00
Abseil Team
75feebda2f Export Test - Do Not Merge
Relax the implementation of MatcherCast to allow conversion of `Matcher<T>` to
`Matcher<const T&>`. They have the same match signature.

PiperOrigin-RevId: 297115843
2020-02-28 16:08:23 -05:00
Abseil Team
c914258241 Export Test - Do Not Merge
Allow construction of an Action from a callable of zero args

Action already allows construction from a callable with the same args as the mocked function, without needing to wrap the callable in Invoke. However, if you don't care about the arguments to the mocked function you need to either accept all of them or wrap your callable in InvokeWithoutArgs. This change makes both of those unnecessary, since it allows you to pass a no-args callable to Action directly.

PiperOrigin-RevId: 296117034
2020-02-28 16:08:12 -05:00
5 changed files with 275 additions and 176 deletions

View File

@@ -421,7 +421,7 @@ sadly they are side effects of C++'s limitations):
`NiceMock<StrictMock<MockFoo> >`) is **not** supported. `NiceMock<StrictMock<MockFoo> >`) is **not** supported.
2. `NiceMock<MockFoo>` and `StrictMock<MockFoo>` may not work correctly if the 2. `NiceMock<MockFoo>` and `StrictMock<MockFoo>` may not work correctly if the
destructor of `MockFoo` is not virtual. We would like to fix this, but it destructor of `MockFoo` is not virtual. We would like to fix this, but it
requires cleaning up existing tests. requires cleaning up existing tests. http://b/28934720 tracks the issue.
3. During the constructor or destructor of `MockFoo`, the mock object is *not* 3. During the constructor or destructor of `MockFoo`, the mock object is *not*
nice or strict. This may cause surprises if the constructor or destructor nice or strict. This may cause surprises if the constructor or destructor
calls a mock method on `this` object. (This behavior, however, is consistent calls a mock method on `this` object. (This behavior, however, is consistent

View File

@@ -189,9 +189,9 @@ or
When Google Test uses pthread, you may need to add flags to your compiler and/or When Google Test uses pthread, you may need to add flags to your compiler and/or
linker to select the pthread library, or you'll get link errors. If you use the linker to select the pthread library, or you'll get link errors. If you use the
CMake script, this is taken care of for you. If you use your own build script, CMake script or the deprecated Autotools script, this is taken care of for you.
you'll need to read your compiler and linker's manual to figure out what flags If you use your own build script, you'll need to read your compiler and linker's
to add. manual to figure out what flags to add.
### As a Shared Library (DLL) ### As a Shared Library (DLL)

View File

@@ -119,91 +119,106 @@
namespace testing { namespace testing {
// Definitions in the internal* namespaces are subject to change without notice. // Definitions in the 'internal' and 'internal2' name spaces are
// DO NOT USE THEM IN USER CODE! // subject to change without notice. DO NOT USE THEM IN USER CODE!
namespace internal { namespace internal2 {
// Prints the given number of bytes in the given object to the given
// ostream.
GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
size_t count,
::std::ostream* os);
// For selecting which printer to use when a given type has neither <<
// nor PrintTo().
enum TypeKind {
kProtobuf, // a protobuf type
kConvertibleToInteger, // a type implicitly convertible to BiggestInt
// (e.g. a named or unnamed enum type)
#if GTEST_INTERNAL_HAS_STRING_VIEW
kConvertibleToStringView, // a type implicitly convertible to
// absl::string_view or std::string_view
#endif
kOtherType // anything else
};
// TypeWithoutFormatter<T, kTypeKind>::PrintValue(value, os) is called
// by the universal printer to print a value of type T when neither
// operator<< nor PrintTo() is defined for T, where kTypeKind is the
// "kind" of T as defined by enum TypeKind.
template <typename T, TypeKind kTypeKind>
class TypeWithoutFormatter {
public:
// This default version is called when kTypeKind is kOtherType.
static void PrintValue(const T& value, ::std::ostream* os) {
PrintBytesInObjectTo(
static_cast<const unsigned char*>(
reinterpret_cast<const void*>(std::addressof(value))),
sizeof(value), os);
}
};
// We print a protobuf using its ShortDebugString() when the string
// doesn't exceed this many characters; otherwise we print it using
// DebugString() for better readability.
const size_t kProtobufOneLinerMaxLength = 50;
template <typename T> template <typename T>
void UniversalPrint(const T& value, ::std::ostream* os); class TypeWithoutFormatter<T, kProtobuf> {
public:
// Used to print an STL-style container when the user doesn't define static void PrintValue(const T& value, ::std::ostream* os) {
// a PrintTo() for it. std::string pretty_str = value.ShortDebugString();
struct ContainerPrinter { if (pretty_str.length() > kProtobufOneLinerMaxLength) {
template <typename T, pretty_str = "\n" + value.DebugString();
typename = typename std::enable_if<
(sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) &&
!IsRecursiveContainer<T>::value>::type>
static void PrintValue(const T& container, std::ostream* os) {
const size_t kMaxCount = 32; // The maximum number of elements to print.
*os << '{';
size_t count = 0;
for (auto&& elem : container) {
if (count > 0) {
*os << ',';
if (count == kMaxCount) { // Enough has been printed.
*os << " ...";
break;
}
}
*os << ' ';
// We cannot call PrintTo(elem, os) here as PrintTo() doesn't
// handle `elem` being a native array.
internal::UniversalPrint(elem, os);
++count;
} }
*os << ("<" + pretty_str + ">");
if (count > 0) {
*os << ' ';
}
*os << '}';
} }
}; };
// Used to print a pointer that is neither a char pointer nor a member template <typename T>
// pointer, when the user doesn't define PrintTo() for it. (A member class TypeWithoutFormatter<T, kConvertibleToInteger> {
// variable pointer or member function pointer doesn't really point to public:
// a location in the address space. Their representation is // Since T has no << operator or PrintTo() but can be implicitly
// implementation-defined. Therefore they will be printed as raw // converted to BiggestInt, we print it as a BiggestInt.
// bytes.) //
struct FunctionPointerPrinter { // Most likely T is an enum type (either named or unnamed), in which
template <typename T, typename = typename std::enable_if< // case printing it as an integer is the desired behavior. In case
std::is_function<T>::value>::type> // T is not an enum, printing it as an integer is the best we can do
static void PrintValue(T* p, ::std::ostream* os) { // given that it has no user-defined printer.
if (p == nullptr) { static void PrintValue(const T& value, ::std::ostream* os) {
*os << "NULL"; const internal::BiggestInt kBigInt = value;
} else { *os << kBigInt;
// T is a function type, so '*os << p' doesn't do what we want
// (it just prints p as bool). We want to print p as a const
// void*.
*os << reinterpret_cast<const void*>(p);
}
} }
}; };
struct PointerPrinter { #if GTEST_INTERNAL_HAS_STRING_VIEW
template <typename T> template <typename T>
static void PrintValue(T* p, ::std::ostream* os) { class TypeWithoutFormatter<T, kConvertibleToStringView> {
if (p == nullptr) { public:
*os << "NULL"; // Since T has neither operator<< nor PrintTo() but can be implicitly
} else { // converted to absl::string_view, we print it as a absl::string_view
// T is not a function type. We just call << to print p, // (or std::string_view).
// relying on ADL to pick up user-defined << for their pointer //
// types, if any. // Note: the implementation is further below, as it depends on
*os << p; // internal::PrintTo symbol which is defined later in the file.
} static void PrintValue(const T& value, ::std::ostream* os);
}
}; };
#endif
namespace internal_stream { // Prints the given value to the given ostream. If the value is a
// protocol message, its debug string is printed; if it's an enum or
struct Sentinel; // of a type implicitly convertible to BiggestInt, it's printed as an
template <typename Char, typename CharTraits, typename T> // integer; otherwise the bytes in the value are printed. This is
Sentinel* operator<<(::std::basic_ostream<Char, CharTraits>& os, const T& x); // what UniversalPrinter<T>::Print() does when it knows nothing about
// type T and T has neither << operator nor PrintTo().
// Check if the user has a user-defined operator<< for their type.
// //
// We put this in its own namespace to inject a custom operator<< that allows us // A user can override this behavior for a class type Foo by defining
// to probe the type's operator. // a << operator in the namespace where Foo is defined.
//
// We put this operator in namespace 'internal2' instead of 'internal'
// to simplify the implementation, as much code in 'internal' needs to
// use << in STL, which would conflict with our own << were it defined
// in 'internal'.
// //
// Note that this operator<< takes a generic std::basic_ostream<Char, // Note that this operator<< takes a generic std::basic_ostream<Char,
// CharTraits> type instead of the more restricted std::ostream. If // CharTraits> type instead of the more restricted std::ostream. If
@@ -214,106 +229,68 @@ Sentinel* operator<<(::std::basic_ostream<Char, CharTraits>& os, const T& x);
// operator<<(std::ostream&, const T&) or // operator<<(std::ostream&, const T&) or
// operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more // operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more
// specific. // specific.
template <typename T> template <typename Char, typename CharTraits, typename T>
constexpr bool UseStreamOperator() { ::std::basic_ostream<Char, CharTraits>& operator<<(
return !std::is_same<decltype(std::declval<std::ostream&>() ::std::basic_ostream<Char, CharTraits>& os, const T& x) {
<< std::declval<const T&>()), TypeWithoutFormatter<T, (internal::IsAProtocolMessage<T>::value
Sentinel*>::value; ? kProtobuf
} : std::is_convertible<
const T&, internal::BiggestInt>::value
} // namespace internal_stream ? kConvertibleToInteger
:
struct StreamPrinter {
template <typename T, typename = typename std::enable_if<
internal_stream::UseStreamOperator<T>()>::type>
static void PrintValue(const T& value, ::std::ostream* os) {
*os << value;
}
};
struct ProtobufPrinter {
// We print a protobuf using its ShortDebugString() when the string
// doesn't exceed this many characters; otherwise we print it using
// DebugString() for better readability.
static const size_t kProtobufOneLinerMaxLength = 50;
template <typename T, typename = typename std::enable_if<
internal::IsAProtocolMessage<T>::value>::type>
static void PrintValue(const T& value, ::std::ostream* os) {
std::string pretty_str = value.ShortDebugString();
if (pretty_str.length() > kProtobufOneLinerMaxLength) {
pretty_str = "\n" + value.DebugString();
}
*os << ("<" + pretty_str + ">");
}
};
struct ConvertibleToIntegerPrinter {
// Since T has no << operator or PrintTo() but can be implicitly
// converted to BiggestInt, we print it as a BiggestInt.
//
// Most likely T is an enum type (either named or unnamed), in which
// case printing it as an integer is the desired behavior. In case
// T is not an enum, printing it as an integer is the best we can do
// given that it has no user-defined printer.
static void PrintValue(internal::BiggestInt value, ::std::ostream* os) {
*os << value;
}
};
struct ConvertibleToStringViewPrinter {
#if GTEST_INTERNAL_HAS_STRING_VIEW #if GTEST_INTERNAL_HAS_STRING_VIEW
static void PrintValue(internal::StringView value, ::std::ostream* os) { std::is_convertible<
internal::UniversalPrint(value, os); const T&, internal::StringView>::value
} ? kConvertibleToStringView
:
#endif #endif
}; kOtherType)>::PrintValue(x, &os);
return os;
// Prints the given number of bytes in the given object to the given
// ostream.
GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
size_t count,
::std::ostream* os);
struct FallbackPrinter {
template <typename T>
static void PrintValue(const T& value, ::std::ostream* os) {
PrintBytesInObjectTo(
static_cast<const unsigned char*>(
reinterpret_cast<const void*>(std::addressof(value))),
sizeof(value), os);
}
};
// Try every printer in order and return the first one that works.
template <typename T, typename E, typename Printer, typename... Printers>
struct FindFirstPrinter : FindFirstPrinter<T, E, Printers...> {};
template <typename T, typename Printer, typename... Printers>
struct FindFirstPrinter<
T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)),
Printer, Printers...> {
using type = Printer;
};
// Select the best printer in the following order:
// - Print containers (they have begin/end/etc).
// - Print function pointers.
// - Print object pointers.
// - Use the stream operator, if available.
// - Print protocol buffers.
// - Print types convertible to BiggestInt.
// - Print types convertible to StringView, if available.
// - Fallback to printing the raw bytes of the object.
template <typename T>
void PrintWithFallback(const T& value, ::std::ostream* os) {
using Printer = typename FindFirstPrinter<
T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter,
StreamPrinter, ProtobufPrinter, ConvertibleToIntegerPrinter,
ConvertibleToStringViewPrinter, FallbackPrinter>::type;
Printer::PrintValue(value, os);
} }
} // namespace internal2
} // namespace testing
// This namespace MUST NOT BE NESTED IN ::testing, or the name look-up
// magic needed for implementing UniversalPrinter won't work.
namespace testing_internal {
// Used to print a value that is not an STL-style container when the
// user doesn't define PrintTo() for it.
template <typename T>
void DefaultPrintNonContainerTo(const T& value, ::std::ostream* os) {
// With the following statement, during unqualified name lookup,
// testing::internal2::operator<< appears as if it was declared in
// the nearest enclosing namespace that contains both
// ::testing_internal and ::testing::internal2, i.e. the global
// namespace. For more details, refer to the C++ Standard section
// 7.3.4-1 [namespace.udir]. This allows us to fall back onto
// testing::internal2::operator<< in case T doesn't come with a <<
// operator.
using ::testing::internal2::operator<<;
// Assuming T is defined in namespace foo, in the next statement,
// the compiler will consider all of:
//
// 1. foo::operator<< (thanks to Koenig look-up),
// 2. ::operator<< (as the current namespace is enclosed in ::),
// 3. testing::internal2::operator<< (thanks to the using statement above).
//
// The operator<< whose type matches T best will be picked.
//
// We deliberately allow #2 to be a candidate, as sometimes it's
// impossible to define #1 (e.g. when foo is ::std, defining
// anything in it is undefined behavior unless you are a compiler
// vendor.).
*os << value;
}
} // namespace testing_internal
namespace testing {
namespace internal {
// FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a // FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
// value of type ToPrint that is an operand of a comparison assertion // value of type ToPrint that is an operand of a comparison assertion
// (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in // (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in
@@ -411,6 +388,85 @@ std::string FormatForComparisonFailureMessage(
template <typename T> template <typename T>
class UniversalPrinter; class UniversalPrinter;
template <typename T>
void UniversalPrint(const T& value, ::std::ostream* os);
enum DefaultPrinterType {
kPrintContainer,
kPrintPointer,
kPrintFunctionPointer,
kPrintOther,
};
template <DefaultPrinterType type> struct WrapPrinterType {};
// Used to print an STL-style container when the user doesn't define
// a PrintTo() for it.
template <typename C>
void DefaultPrintTo(WrapPrinterType<kPrintContainer> /* dummy */,
const C& container, ::std::ostream* os) {
const size_t kMaxCount = 32; // The maximum number of elements to print.
*os << '{';
size_t count = 0;
for (typename C::const_iterator it = container.begin();
it != container.end(); ++it, ++count) {
if (count > 0) {
*os << ',';
if (count == kMaxCount) { // Enough has been printed.
*os << " ...";
break;
}
}
*os << ' ';
// We cannot call PrintTo(*it, os) here as PrintTo() doesn't
// handle *it being a native array.
internal::UniversalPrint(*it, os);
}
if (count > 0) {
*os << ' ';
}
*os << '}';
}
// Used to print a pointer that is neither a char pointer nor a member
// pointer, when the user doesn't define PrintTo() for it. (A member
// variable pointer or member function pointer doesn't really point to
// a location in the address space. Their representation is
// implementation-defined. Therefore they will be printed as raw
// bytes.)
template <typename T>
void DefaultPrintTo(WrapPrinterType<kPrintPointer> /* dummy */,
T* p, ::std::ostream* os) {
if (p == nullptr) {
*os << "NULL";
} else {
// T is not a function type. We just call << to print p,
// relying on ADL to pick up user-defined << for their pointer
// types, if any.
*os << p;
}
}
template <typename T>
void DefaultPrintTo(WrapPrinterType<kPrintFunctionPointer> /* dummy */,
T* p, ::std::ostream* os) {
if (p == nullptr) {
*os << "NULL";
} else {
// T is a function type, so '*os << p' doesn't do what we want
// (it just prints p as bool). We want to print p as a const
// void*.
*os << reinterpret_cast<const void*>(p);
}
}
// Used to print a non-container, non-pointer value when the user
// doesn't define PrintTo() for it.
template <typename T>
void DefaultPrintTo(WrapPrinterType<kPrintOther> /* dummy */,
const T& value, ::std::ostream* os) {
::testing_internal::DefaultPrintNonContainerTo(value, os);
}
// Prints the given value using the << operator if it has one; // Prints the given value using the << operator if it has one;
// otherwise prints the bytes in it. This is what // otherwise prints the bytes in it. This is what
// UniversalPrinter<T>::Print() does when PrintTo() is not specialized // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
@@ -424,7 +480,36 @@ class UniversalPrinter;
// wants). // wants).
template <typename T> template <typename T>
void PrintTo(const T& value, ::std::ostream* os) { void PrintTo(const T& value, ::std::ostream* os) {
internal::PrintWithFallback(value, os); // DefaultPrintTo() is overloaded. The type of its first argument
// determines which version will be picked.
//
// Note that we check for container types here, prior to we check
// for protocol message types in our operator<<. The rationale is:
//
// For protocol messages, we want to give people a chance to
// override Google Mock's format by defining a PrintTo() or
// operator<<. For STL containers, other formats can be
// incompatible with Google Mock's format for the container
// elements; therefore we check for container types here to ensure
// that our format is used.
//
// Note that MSVC and clang-cl do allow an implicit conversion from
// pointer-to-function to pointer-to-object, but clang-cl warns on it.
// So don't use ImplicitlyConvertible if it can be helped since it will
// cause this warning, and use a separate overload of DefaultPrintTo for
// function pointers so that the `*os << p` in the object pointer overload
// doesn't cause that warning either.
DefaultPrintTo(
WrapPrinterType <
(sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) &&
!IsRecursiveContainer<T>::value
? kPrintContainer
: !std::is_pointer<T>::value
? kPrintOther
: std::is_function<typename std::remove_pointer<T>::type>::value
? kPrintFunctionPointer
: kPrintPointer > (),
value, os);
} }
// The following list of PrintTo() overloads tells // The following list of PrintTo() overloads tells
@@ -815,6 +900,16 @@ Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
} // namespace internal } // namespace internal
#if GTEST_INTERNAL_HAS_STRING_VIEW
namespace internal2 {
template <typename T>
void TypeWithoutFormatter<T, kConvertibleToStringView>::PrintValue(
const T& value, ::std::ostream* os) {
internal::PrintTo(internal::StringView(value), os);
}
} // namespace internal2
#endif
template <typename T> template <typename T>
::std::string PrintToString(const T& value) { ::std::string PrintToString(const T& value) {
::std::stringstream ss; ::std::stringstream ss;

View File

@@ -104,7 +104,7 @@ void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count,
} // namespace } // namespace
namespace internal { namespace internal2 {
// Delegates to PrintBytesInObjectToImpl() to print the bytes in the // Delegates to PrintBytesInObjectToImpl() to print the bytes in the
// given object. The delegation simplifies the implementation, which // given object. The delegation simplifies the implementation, which
@@ -116,6 +116,10 @@ void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count,
PrintBytesInObjectToImpl(obj_bytes, count, os); PrintBytesInObjectToImpl(obj_bytes, count, os);
} }
} // namespace internal2
namespace internal {
// Depending on the value of a char (or wchar_t), we print it in one // Depending on the value of a char (or wchar_t), we print it in one
// of three formats: // of three formats:
// - as is if it's a printable ASCII (e.g. 'a', '2', ' '), // - as is if it's a printable ASCII (e.g. 'a', '2', ' '),

View File

@@ -2268,7 +2268,7 @@ static const char* const kReservedOutputTestCaseAttributes[] = {
"classname", "name", "status", "time", "type_param", "classname", "name", "status", "time", "type_param",
"value_param", "file", "line", "result", "timestamp"}; "value_param", "file", "line", "result", "timestamp"};
template <size_t kSize> template <int kSize>
std::vector<std::string> ArrayAsVector(const char* const (&array)[kSize]) { std::vector<std::string> ArrayAsVector(const char* const (&array)[kSize]) {
return std::vector<std::string>(array, array + kSize); return std::vector<std::string>(array, array + kSize);
} }