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.

250 lines
9.0 KiB

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