You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
7.9 KiB

3 months ago
  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. //
  30. // The Google C++ Testing and Mocking Framework (Google Test)
  31. //
  32. // This header file defines the Message class.
  33. //
  34. // IMPORTANT NOTE: Due to limitation of the C++ language, we have to
  35. // leave some internal implementation details in this header file.
  36. // They are clearly marked by comments like this:
  37. //
  38. // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
  39. //
  40. // Such code is NOT meant to be used by a user directly, and is subject
  41. // to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user
  42. // program!
  43. // GOOGLETEST_CM0001 DO NOT DELETE
  44. #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
  45. #define GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_
  46. #include <limits>
  47. #include <memory>
  48. #include <sstream>
  49. #include "gtest/internal/gtest-port.h"
  50. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
  51. /* class A needs to have dll-interface to be used by clients of class B */)
  52. // Ensures that there is at least one operator<< in the global namespace.
  53. // See Message& operator<<(...) below for why.
  54. void operator<<(const testing::internal::Secret&, int);
  55. namespace testing {
  56. // The Message class works like an ostream repeater.
  57. //
  58. // Typical usage:
  59. //
  60. // 1. You stream a bunch of values to a Message object.
  61. // It will remember the text in a stringstream.
  62. // 2. Then you stream the Message object to an ostream.
  63. // This causes the text in the Message to be streamed
  64. // to the ostream.
  65. //
  66. // For example;
  67. //
  68. // testing::Message foo;
  69. // foo << 1 << " != " << 2;
  70. // std::cout << foo;
  71. //
  72. // will print "1 != 2".
  73. //
  74. // Message is not intended to be inherited from. In particular, its
  75. // destructor is not virtual.
  76. //
  77. // Note that stringstream behaves differently in gcc and in MSVC. You
  78. // can stream a NULL char pointer to it in the former, but not in the
  79. // latter (it causes an access violation if you do). The Message
  80. // class hides this difference by treating a NULL char pointer as
  81. // "(null)".
  82. class GTEST_API_ Message {
  83. private:
  84. // The type of basic IO manipulators (endl, ends, and flush) for
  85. // narrow streams.
  86. typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&);
  87. public:
  88. // Constructs an empty Message.
  89. Message();
  90. // Copy constructor.
  91. Message(const Message& msg) : ss_(new ::std::stringstream) { // NOLINT
  92. *ss_ << msg.GetString();
  93. }
  94. // Constructs a Message from a C-string.
  95. explicit Message(const char* str) : ss_(new ::std::stringstream) {
  96. *ss_ << str;
  97. }
  98. // Streams a non-pointer value to this object.
  99. template <typename T>
  100. inline Message& operator <<(const T& val) {
  101. // Some libraries overload << for STL containers. These
  102. // overloads are defined in the global namespace instead of ::std.
  103. //
  104. // C++'s symbol lookup rule (i.e. Koenig lookup) says that these
  105. // overloads are visible in either the std namespace or the global
  106. // namespace, but not other namespaces, including the testing
  107. // namespace which Google Test's Message class is in.
  108. //
  109. // To allow STL containers (and other types that has a << operator
  110. // defined in the global namespace) to be used in Google Test
  111. // assertions, testing::Message must access the custom << operator
  112. // from the global namespace. With this using declaration,
  113. // overloads of << defined in the global namespace and those
  114. // visible via Koenig lookup are both exposed in this function.
  115. using ::operator <<;
  116. *ss_ << val;
  117. return *this;
  118. }
  119. // Streams a pointer value to this object.
  120. //
  121. // This function is an overload of the previous one. When you
  122. // stream a pointer to a Message, this definition will be used as it
  123. // is more specialized. (The C++ Standard, section
  124. // [temp.func.order].) If you stream a non-pointer, then the
  125. // previous definition will be used.
  126. //
  127. // The reason for this overload is that streaming a NULL pointer to
  128. // ostream is undefined behavior. Depending on the compiler, you
  129. // may get "0", "(nil)", "(null)", or an access violation. To
  130. // ensure consistent result across compilers, we always treat NULL
  131. // as "(null)".
  132. template <typename T>
  133. inline Message& operator <<(T* const& pointer) { // NOLINT
  134. if (pointer == nullptr) {
  135. *ss_ << "(null)";
  136. } else {
  137. *ss_ << pointer;
  138. }
  139. return *this;
  140. }
  141. // Since the basic IO manipulators are overloaded for both narrow
  142. // and wide streams, we have to provide this specialized definition
  143. // of operator <<, even though its body is the same as the
  144. // templatized version above. Without this definition, streaming
  145. // endl or other basic IO manipulators to Message will confuse the
  146. // compiler.
  147. Message& operator <<(BasicNarrowIoManip val) {
  148. *ss_ << val;
  149. return *this;
  150. }
  151. // Instead of 1/0, we want to see true/false for bool values.
  152. Message& operator <<(bool b) {
  153. return *this << (b ? "true" : "false");
  154. }
  155. // These two overloads allow streaming a wide C string to a Message
  156. // using the UTF-8 encoding.
  157. Message& operator <<(const wchar_t* wide_c_str);
  158. Message& operator <<(wchar_t* wide_c_str);
  159. #if GTEST_HAS_STD_WSTRING
  160. // Converts the given wide string to a narrow string using the UTF-8
  161. // encoding, and streams the result to this Message object.
  162. Message& operator <<(const ::std::wstring& wstr);
  163. #endif // GTEST_HAS_STD_WSTRING
  164. // Gets the text streamed to this object so far as an std::string.
  165. // Each '\0' character in the buffer is replaced with "\\0".
  166. //
  167. // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
  168. std::string GetString() const;
  169. private:
  170. // We'll hold the text streamed to this object here.
  171. const std::unique_ptr< ::std::stringstream> ss_;
  172. // We declare (but don't implement) this to prevent the compiler
  173. // from implementing the assignment operator.
  174. void operator=(const Message&);
  175. };
  176. // Streams a Message to an ostream.
  177. inline std::ostream& operator <<(std::ostream& os, const Message& sb) {
  178. return os << sb.GetString();
  179. }
  180. namespace internal {
  181. // Converts a streamable value to an std::string. A NULL pointer is
  182. // converted to "(null)". When the input value is a ::string,
  183. // ::std::string, ::wstring, or ::std::wstring object, each NUL
  184. // character in it is replaced with "\\0".
  185. template <typename T>
  186. std::string StreamableToString(const T& streamable) {
  187. return (Message() << streamable).GetString();
  188. }
  189. } // namespace internal
  190. } // namespace testing
  191. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
  192. #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_