gtest-message.h 9.1 KB

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