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