diff options
author | zhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925> | 2012-06-07 20:34:34 +0000 |
---|---|---|
committer | zhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925> | 2012-06-07 20:34:34 +0000 |
commit | 6b625a794e8a0e314f3ccc2f6a892f4a40a7e788 (patch) | |
tree | 617807532cbbd68ce8e684a570791231b1b9cd92 /include/gtest/gtest-printers.h | |
parent | aa3c420019cb0215b00013cb94658a917ec4f712 (diff) | |
download | googletest-6b625a794e8a0e314f3ccc2f6a892f4a40a7e788.tar.gz |
Improves gtest's failure messages. In particulars, char pointers and
char arrays are not escapped properly.
git-svn-id: http://googletest.googlecode.com/svn/trunk@616 861a406c-534a-0410-8894-cb66d6ee9925
Diffstat (limited to 'include/gtest/gtest-printers.h')
-rw-r--r-- | include/gtest/gtest-printers.h | 86 |
1 files changed, 71 insertions, 15 deletions
diff --git a/include/gtest/gtest-printers.h b/include/gtest/gtest-printers.h index 55d44fa..0639d9f 100644 --- a/include/gtest/gtest-printers.h +++ b/include/gtest/gtest-printers.h @@ -630,9 +630,12 @@ void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) { } } // This overload prints a (const) char array compactly. -GTEST_API_ void UniversalPrintArray(const char* begin, - size_t len, - ::std::ostream* os); +GTEST_API_ void UniversalPrintArray( + const char* begin, size_t len, ::std::ostream* os); + +// This overload prints a (const) wchar_t array compactly. +GTEST_API_ void UniversalPrintArray( + const wchar_t* begin, size_t len, ::std::ostream* os); // Implements printing an array type T[N]. template <typename T, size_t N> @@ -673,19 +676,72 @@ class UniversalPrinter<T&> { // Prints a value tersely: for a reference type, the referenced value // (but not the address) is printed; for a (const) char pointer, the // NUL-terminated string (but not the pointer) is printed. + template <typename T> -void UniversalTersePrint(const T& value, ::std::ostream* os) { - UniversalPrint(value, os); -} -inline void UniversalTersePrint(const char* str, ::std::ostream* os) { - if (str == NULL) { - *os << "NULL"; - } else { - UniversalPrint(string(str), os); +class UniversalTersePrinter { + public: + static void Print(const T& value, ::std::ostream* os) { + UniversalPrint(value, os); } -} -inline void UniversalTersePrint(char* str, ::std::ostream* os) { - UniversalTersePrint(static_cast<const char*>(str), os); +}; +template <typename T> +class UniversalTersePrinter<T&> { + public: + static void Print(const T& value, ::std::ostream* os) { + UniversalPrint(value, os); + } +}; +template <typename T, size_t N> +class UniversalTersePrinter<T[N]> { + public: + static void Print(const T (&value)[N], ::std::ostream* os) { + UniversalPrinter<T[N]>::Print(value, os); + } +}; +template <> +class UniversalTersePrinter<const char*> { + public: + static void Print(const char* str, ::std::ostream* os) { + if (str == NULL) { + *os << "NULL"; + } else { + UniversalPrint(string(str), os); + } + } +}; +template <> +class UniversalTersePrinter<char*> { + public: + static void Print(char* str, ::std::ostream* os) { + UniversalTersePrinter<const char*>::Print(str, os); + } +}; + +#if GTEST_HAS_STD_WSTRING +template <> +class UniversalTersePrinter<const wchar_t*> { + public: + static void Print(const wchar_t* str, ::std::ostream* os) { + if (str == NULL) { + *os << "NULL"; + } else { + UniversalPrint(::std::wstring(str), os); + } + } +}; +#endif + +template <> +class UniversalTersePrinter<wchar_t*> { + public: + static void Print(wchar_t* str, ::std::ostream* os) { + UniversalTersePrinter<const wchar_t*>::Print(str, os); + } +}; + +template <typename T> +void UniversalTersePrint(const T& value, ::std::ostream* os) { + UniversalTersePrinter<T>::Print(value, os); } // Prints a value using the type inferred by the compiler. The @@ -790,7 +846,7 @@ Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) { template <typename T> ::std::string PrintToString(const T& value) { ::std::stringstream ss; - internal::UniversalTersePrint(value, &ss); + internal::UniversalTersePrinter<T>::Print(value, &ss); return ss.str(); } |