Allows EXPECT_EQ to accept arguments that don't have operator << (by Zhanyong Wan).

Allows a user to customize how the universal printer prints a pointer of a specific type by overloading << (by Zhanyong Wan).
Works around a bug in Cymbian's C++ compiler (by Vlad Losev).
This commit is contained in:
zhanyong.wan
2010-07-21 22:15:17 +00:00
parent 447ed6474d
commit e2a7f03b80
9 changed files with 261 additions and 218 deletions

View File

@@ -102,7 +102,7 @@ namespace proto2 { class Message; }
namespace testing {
// Forward declaration of classes.
// Forward declarations.
class AssertionResult; // Result of an assertion.
class Message; // Represents a failure message.
@@ -111,6 +111,9 @@ class TestInfo; // Information about a test.
class TestPartResult; // Result of a test part.
class UnitTest; // A collection of test cases.
template <typename T>
::std::string PrintToString(const T& value);
namespace internal {
struct TraceInfo; // Information about a trace point.
@@ -192,72 +195,23 @@ class GTEST_API_ ScopedTrace {
template <typename T>
String StreamableToString(const T& streamable);
// Formats a value to be used in a failure message.
#ifdef GTEST_NEEDS_IS_POINTER_
// These are needed as the Nokia Symbian and IBM XL C/C++ compilers
// cannot decide between const T& and const T* in a function template.
// These compilers _can_ decide between class template specializations
// for T and T*, so a tr1::type_traits-like is_pointer works, and we
// can overload on that.
// This overload makes sure that all pointers (including
// those to char or wchar_t) are printed as raw pointers.
template <typename T>
inline String FormatValueForFailureMessage(internal::true_type /*dummy*/,
T* pointer) {
return StreamableToString(static_cast<const void*>(pointer));
}
template <typename T>
inline String FormatValueForFailureMessage(internal::false_type /*dummy*/,
const T& value) {
return StreamableToString(value);
}
template <typename T>
inline String FormatForFailureMessage(const T& value) {
return FormatValueForFailureMessage(
typename internal::is_pointer<T>::type(), value);
}
#else
// These are needed as the above solution using is_pointer has the
// limitation that T cannot be a type without external linkage, when
// compiled using MSVC.
template <typename T>
inline String FormatForFailureMessage(const T& value) {
return StreamableToString(value);
}
// This overload makes sure that all pointers (including
// those to char or wchar_t) are printed as raw pointers.
template <typename T>
inline String FormatForFailureMessage(T* pointer) {
return StreamableToString(static_cast<const void*>(pointer));
}
#endif // GTEST_NEEDS_IS_POINTER_
// These overloaded versions handle narrow and wide characters.
GTEST_API_ String FormatForFailureMessage(char ch);
GTEST_API_ String FormatForFailureMessage(wchar_t wchar);
// When this operand is a const char* or char*, and the other operand
// When this operand is a const char* or char*, if the other operand
// is a ::std::string or ::string, we print this operand as a C string
// rather than a pointer. We do the same for wide strings.
// rather than a pointer (we do the same for wide strings); otherwise
// we print it as a pointer to be safe.
// This internal macro is used to avoid duplicated code.
// Making the first operand const reference works around a bug in the
// Symbian compiler which is unable to select the correct specialization of
// FormatForComparisonFailureMessage.
#define GTEST_FORMAT_IMPL_(operand2_type, operand1_printer)\
inline String FormatForComparisonFailureMessage(\
operand2_type::value_type* str, const operand2_type& /*operand2*/) {\
operand2_type::value_type* const& str, const operand2_type& /*operand2*/) {\
return operand1_printer(str);\
}\
inline String FormatForComparisonFailureMessage(\
const operand2_type::value_type* str, const operand2_type& /*operand2*/) {\
const operand2_type::value_type* const& str, \
const operand2_type& /*operand2*/) {\
return operand1_printer(str);\
}
@@ -275,6 +229,27 @@ GTEST_FORMAT_IMPL_(::wstring, String::ShowWideCStringQuoted)
#undef GTEST_FORMAT_IMPL_
// The next four overloads handle the case where the operand being
// printed is a char/wchar_t pointer and the other operand is not a
// string/wstring object. In such cases, we just print the operand as
// a pointer to be safe.
//
// Making the first operand const reference works around a bug in the
// Symbian compiler which is unable to select the correct specialization of
// FormatForComparisonFailureMessage.
#define GTEST_FORMAT_CHAR_PTR_IMPL_(CharType) \
template <typename T> \
String FormatForComparisonFailureMessage(CharType* const& p, const T&) { \
return PrintToString(static_cast<const void*>(p)); \
}
GTEST_FORMAT_CHAR_PTR_IMPL_(char)
GTEST_FORMAT_CHAR_PTR_IMPL_(const char)
GTEST_FORMAT_CHAR_PTR_IMPL_(wchar_t)
GTEST_FORMAT_CHAR_PTR_IMPL_(const wchar_t)
#undef GTEST_FORMAT_CHAR_PTR_IMPL_
// Constructs and returns the message for an equality assertion
// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure.
//