gtest-message.h 9.0 KB

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