1b89a7cc2SEnji Cooper // Copyright 2005, Google Inc. 2b89a7cc2SEnji Cooper // All rights reserved. 3b89a7cc2SEnji Cooper // 4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without 5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are 6b89a7cc2SEnji Cooper // met: 7b89a7cc2SEnji Cooper // 8b89a7cc2SEnji Cooper // * Redistributions of source code must retain the above copyright 9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer. 10b89a7cc2SEnji Cooper // * Redistributions in binary form must reproduce the above 11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer 12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the 13b89a7cc2SEnji Cooper // distribution. 14b89a7cc2SEnji Cooper // * Neither the name of Google Inc. nor the names of its 15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from 16b89a7cc2SEnji Cooper // this software without specific prior written permission. 17b89a7cc2SEnji Cooper // 18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29b89a7cc2SEnji Cooper 30b89a7cc2SEnji Cooper // The Google C++ Testing and Mocking Framework (Google Test) 31b89a7cc2SEnji Cooper // 32b89a7cc2SEnji Cooper // This header file defines the Message class. 33b89a7cc2SEnji Cooper // 34b89a7cc2SEnji Cooper // IMPORTANT NOTE: Due to limitation of the C++ language, we have to 35b89a7cc2SEnji Cooper // leave some internal implementation details in this header file. 36b89a7cc2SEnji Cooper // They are clearly marked by comments like this: 37b89a7cc2SEnji Cooper // 38b89a7cc2SEnji Cooper // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 39b89a7cc2SEnji Cooper // 40b89a7cc2SEnji Cooper // Such code is NOT meant to be used by a user directly, and is subject 41b89a7cc2SEnji Cooper // to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user 42b89a7cc2SEnji Cooper // program! 43b89a7cc2SEnji Cooper 4428f6c2f2SEnji Cooper // IWYU pragma: private, include "gtest/gtest.h" 4528f6c2f2SEnji Cooper // IWYU pragma: friend gtest/.* 4628f6c2f2SEnji Cooper // IWYU pragma: friend gmock/.* 47b89a7cc2SEnji Cooper 4828f6c2f2SEnji Cooper #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ 4928f6c2f2SEnji Cooper #define GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ 50b89a7cc2SEnji Cooper 51b89a7cc2SEnji Cooper #include <limits> 5228f6c2f2SEnji Cooper #include <memory> 5328f6c2f2SEnji Cooper #include <ostream> 5428f6c2f2SEnji Cooper #include <sstream> 5528f6c2f2SEnji Cooper #include <string> 56b89a7cc2SEnji Cooper 57b89a7cc2SEnji Cooper #include "gtest/internal/gtest-port.h" 58b89a7cc2SEnji Cooper 5928f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL 6028f6c2f2SEnji Cooper #include <type_traits> 6128f6c2f2SEnji Cooper 62*5ca8c28cSEnji Cooper #include "absl/strings/has_absl_stringify.h" 6328f6c2f2SEnji Cooper #include "absl/strings/str_cat.h" 6428f6c2f2SEnji Cooper #endif // GTEST_HAS_ABSL 6528f6c2f2SEnji Cooper 66b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 67b89a7cc2SEnji Cooper /* class A needs to have dll-interface to be used by clients of class B */) 68b89a7cc2SEnji Cooper 69b89a7cc2SEnji Cooper // Ensures that there is at least one operator<< in the global namespace. 70b89a7cc2SEnji Cooper // See Message& operator<<(...) below for why. 71b89a7cc2SEnji Cooper void operator<<(const testing::internal::Secret&, int); 72b89a7cc2SEnji Cooper 73b89a7cc2SEnji Cooper namespace testing { 74b89a7cc2SEnji Cooper 75b89a7cc2SEnji Cooper // The Message class works like an ostream repeater. 76b89a7cc2SEnji Cooper // 77b89a7cc2SEnji Cooper // Typical usage: 78b89a7cc2SEnji Cooper // 79b89a7cc2SEnji Cooper // 1. You stream a bunch of values to a Message object. 80b89a7cc2SEnji Cooper // It will remember the text in a stringstream. 81b89a7cc2SEnji Cooper // 2. Then you stream the Message object to an ostream. 82b89a7cc2SEnji Cooper // This causes the text in the Message to be streamed 83b89a7cc2SEnji Cooper // to the ostream. 84b89a7cc2SEnji Cooper // 85b89a7cc2SEnji Cooper // For example; 86b89a7cc2SEnji Cooper // 87b89a7cc2SEnji Cooper // testing::Message foo; 88b89a7cc2SEnji Cooper // foo << 1 << " != " << 2; 89b89a7cc2SEnji Cooper // std::cout << foo; 90b89a7cc2SEnji Cooper // 91b89a7cc2SEnji Cooper // will print "1 != 2". 92b89a7cc2SEnji Cooper // 93b89a7cc2SEnji Cooper // Message is not intended to be inherited from. In particular, its 94b89a7cc2SEnji Cooper // destructor is not virtual. 95b89a7cc2SEnji Cooper // 96b89a7cc2SEnji Cooper // Note that stringstream behaves differently in gcc and in MSVC. You 97b89a7cc2SEnji Cooper // can stream a NULL char pointer to it in the former, but not in the 98b89a7cc2SEnji Cooper // latter (it causes an access violation if you do). The Message 99b89a7cc2SEnji Cooper // class hides this difference by treating a NULL char pointer as 100b89a7cc2SEnji Cooper // "(null)". 101b89a7cc2SEnji Cooper class GTEST_API_ Message { 102b89a7cc2SEnji Cooper private: 103b89a7cc2SEnji Cooper // The type of basic IO manipulators (endl, ends, and flush) for 104b89a7cc2SEnji Cooper // narrow streams. 105b89a7cc2SEnji Cooper typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&); 106b89a7cc2SEnji Cooper 107b89a7cc2SEnji Cooper public: 108b89a7cc2SEnji Cooper // Constructs an empty Message. 109b89a7cc2SEnji Cooper Message(); 110b89a7cc2SEnji Cooper 111b89a7cc2SEnji Cooper // Copy constructor. 112b89a7cc2SEnji Cooper Message(const Message& msg) : ss_(new ::std::stringstream) { // NOLINT 113b89a7cc2SEnji Cooper *ss_ << msg.GetString(); 114b89a7cc2SEnji Cooper } 115b89a7cc2SEnji Cooper 116b89a7cc2SEnji Cooper // Constructs a Message from a C-string. 117b89a7cc2SEnji Cooper explicit Message(const char* str) : ss_(new ::std::stringstream) { 118b89a7cc2SEnji Cooper *ss_ << str; 119b89a7cc2SEnji Cooper } 120b89a7cc2SEnji Cooper 12128f6c2f2SEnji Cooper // Streams a non-pointer value to this object. If building a version of 12228f6c2f2SEnji Cooper // GoogleTest with ABSL, this overload is only enabled if the value does not 12328f6c2f2SEnji Cooper // have an AbslStringify definition. 124*5ca8c28cSEnji Cooper template < 125*5ca8c28cSEnji Cooper typename T 12628f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL 12728f6c2f2SEnji Cooper , 128*5ca8c28cSEnji Cooper typename std::enable_if<!absl::HasAbslStringify<T>::value, // NOLINT 12928f6c2f2SEnji Cooper int>::type = 0 13028f6c2f2SEnji Cooper #endif // GTEST_HAS_ABSL 13128f6c2f2SEnji Cooper > 132b89a7cc2SEnji Cooper inline Message& operator<<(const T& val) { 133b89a7cc2SEnji Cooper // Some libraries overload << for STL containers. These 134b89a7cc2SEnji Cooper // overloads are defined in the global namespace instead of ::std. 135b89a7cc2SEnji Cooper // 136b89a7cc2SEnji Cooper // C++'s symbol lookup rule (i.e. Koenig lookup) says that these 137b89a7cc2SEnji Cooper // overloads are visible in either the std namespace or the global 138b89a7cc2SEnji Cooper // namespace, but not other namespaces, including the testing 139b89a7cc2SEnji Cooper // namespace which Google Test's Message class is in. 140b89a7cc2SEnji Cooper // 141b89a7cc2SEnji Cooper // To allow STL containers (and other types that has a << operator 142b89a7cc2SEnji Cooper // defined in the global namespace) to be used in Google Test 143b89a7cc2SEnji Cooper // assertions, testing::Message must access the custom << operator 144b89a7cc2SEnji Cooper // from the global namespace. With this using declaration, 145b89a7cc2SEnji Cooper // overloads of << defined in the global namespace and those 146b89a7cc2SEnji Cooper // visible via Koenig lookup are both exposed in this function. 147b89a7cc2SEnji Cooper using ::operator<<; 148b89a7cc2SEnji Cooper *ss_ << val; 149b89a7cc2SEnji Cooper return *this; 150b89a7cc2SEnji Cooper } 151b89a7cc2SEnji Cooper 15228f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL 15328f6c2f2SEnji Cooper // Streams a non-pointer value with an AbslStringify definition to this 15428f6c2f2SEnji Cooper // object. 15528f6c2f2SEnji Cooper template <typename T, 156*5ca8c28cSEnji Cooper typename std::enable_if<absl::HasAbslStringify<T>::value, // NOLINT 15728f6c2f2SEnji Cooper int>::type = 0> 15828f6c2f2SEnji Cooper inline Message& operator<<(const T& val) { 15928f6c2f2SEnji Cooper // ::operator<< is needed here for a similar reason as with the non-Abseil 16028f6c2f2SEnji Cooper // version above 16128f6c2f2SEnji Cooper using ::operator<<; 16228f6c2f2SEnji Cooper *ss_ << absl::StrCat(val); 16328f6c2f2SEnji Cooper return *this; 16428f6c2f2SEnji Cooper } 16528f6c2f2SEnji Cooper #endif // GTEST_HAS_ABSL 16628f6c2f2SEnji Cooper 167b89a7cc2SEnji Cooper // Streams a pointer value to this object. 168b89a7cc2SEnji Cooper // 169b89a7cc2SEnji Cooper // This function is an overload of the previous one. When you 170b89a7cc2SEnji Cooper // stream a pointer to a Message, this definition will be used as it 171b89a7cc2SEnji Cooper // is more specialized. (The C++ Standard, section 172b89a7cc2SEnji Cooper // [temp.func.order].) If you stream a non-pointer, then the 173b89a7cc2SEnji Cooper // previous definition will be used. 174b89a7cc2SEnji Cooper // 175b89a7cc2SEnji Cooper // The reason for this overload is that streaming a NULL pointer to 176b89a7cc2SEnji Cooper // ostream is undefined behavior. Depending on the compiler, you 177b89a7cc2SEnji Cooper // may get "0", "(nil)", "(null)", or an access violation. To 178b89a7cc2SEnji Cooper // ensure consistent result across compilers, we always treat NULL 179b89a7cc2SEnji Cooper // as "(null)". 180b89a7cc2SEnji Cooper template <typename T> 181b89a7cc2SEnji Cooper inline Message& operator<<(T* const& pointer) { // NOLINT 18228f6c2f2SEnji Cooper if (pointer == nullptr) { 183b89a7cc2SEnji Cooper *ss_ << "(null)"; 184b89a7cc2SEnji Cooper } else { 185b89a7cc2SEnji Cooper *ss_ << pointer; 186b89a7cc2SEnji Cooper } 187b89a7cc2SEnji Cooper return *this; 188b89a7cc2SEnji Cooper } 189b89a7cc2SEnji Cooper 190b89a7cc2SEnji Cooper // Since the basic IO manipulators are overloaded for both narrow 191b89a7cc2SEnji Cooper // and wide streams, we have to provide this specialized definition 192b89a7cc2SEnji Cooper // of operator <<, even though its body is the same as the 193b89a7cc2SEnji Cooper // templatized version above. Without this definition, streaming 194b89a7cc2SEnji Cooper // endl or other basic IO manipulators to Message will confuse the 195b89a7cc2SEnji Cooper // compiler. 196b89a7cc2SEnji Cooper Message& operator<<(BasicNarrowIoManip val) { 197b89a7cc2SEnji Cooper *ss_ << val; 198b89a7cc2SEnji Cooper return *this; 199b89a7cc2SEnji Cooper } 200b89a7cc2SEnji Cooper 201b89a7cc2SEnji Cooper // Instead of 1/0, we want to see true/false for bool values. 20228f6c2f2SEnji Cooper Message& operator<<(bool b) { return *this << (b ? "true" : "false"); } 203b89a7cc2SEnji Cooper 204b89a7cc2SEnji Cooper // These two overloads allow streaming a wide C string to a Message 205b89a7cc2SEnji Cooper // using the UTF-8 encoding. 206b89a7cc2SEnji Cooper Message& operator<<(const wchar_t* wide_c_str); 207b89a7cc2SEnji Cooper Message& operator<<(wchar_t* wide_c_str); 208b89a7cc2SEnji Cooper 209b89a7cc2SEnji Cooper #if GTEST_HAS_STD_WSTRING 210b89a7cc2SEnji Cooper // Converts the given wide string to a narrow string using the UTF-8 211b89a7cc2SEnji Cooper // encoding, and streams the result to this Message object. 212b89a7cc2SEnji Cooper Message& operator<<(const ::std::wstring& wstr); 213b89a7cc2SEnji Cooper #endif // GTEST_HAS_STD_WSTRING 214b89a7cc2SEnji Cooper 215b89a7cc2SEnji Cooper // Gets the text streamed to this object so far as an std::string. 216b89a7cc2SEnji Cooper // Each '\0' character in the buffer is replaced with "\\0". 217b89a7cc2SEnji Cooper // 218b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 219b89a7cc2SEnji Cooper std::string GetString() const; 220b89a7cc2SEnji Cooper 221b89a7cc2SEnji Cooper private: 222b89a7cc2SEnji Cooper // We'll hold the text streamed to this object here. 22328f6c2f2SEnji Cooper const std::unique_ptr< ::std::stringstream> ss_; 224b89a7cc2SEnji Cooper 225b89a7cc2SEnji Cooper // We declare (but don't implement) this to prevent the compiler 226b89a7cc2SEnji Cooper // from implementing the assignment operator. 227b89a7cc2SEnji Cooper void operator=(const Message&); 228b89a7cc2SEnji Cooper }; 229b89a7cc2SEnji Cooper 230b89a7cc2SEnji Cooper // Streams a Message to an ostream. 231b89a7cc2SEnji Cooper inline std::ostream& operator<<(std::ostream& os, const Message& sb) { 232b89a7cc2SEnji Cooper return os << sb.GetString(); 233b89a7cc2SEnji Cooper } 234b89a7cc2SEnji Cooper 235b89a7cc2SEnji Cooper namespace internal { 236b89a7cc2SEnji Cooper 237b89a7cc2SEnji Cooper // Converts a streamable value to an std::string. A NULL pointer is 238b89a7cc2SEnji Cooper // converted to "(null)". When the input value is a ::string, 239b89a7cc2SEnji Cooper // ::std::string, ::wstring, or ::std::wstring object, each NUL 240b89a7cc2SEnji Cooper // character in it is replaced with "\\0". 241b89a7cc2SEnji Cooper template <typename T> 242b89a7cc2SEnji Cooper std::string StreamableToString(const T& streamable) { 243b89a7cc2SEnji Cooper return (Message() << streamable).GetString(); 244b89a7cc2SEnji Cooper } 245b89a7cc2SEnji Cooper 246b89a7cc2SEnji Cooper } // namespace internal 247b89a7cc2SEnji Cooper } // namespace testing 248b89a7cc2SEnji Cooper 249b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 250b89a7cc2SEnji Cooper 25128f6c2f2SEnji Cooper #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ 252