OLD | NEW |
| (Empty) |
1 // Copyright 2007, 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 // Google Mock - a framework for writing C++ mock classes. | |
33 // | |
34 // This file implements a universal value printer that can print a | |
35 // value of any type T: | |
36 // | |
37 // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr); | |
38 // | |
39 // It uses the << operator when possible, and prints the bytes in the | |
40 // object otherwise. A user can override its behavior for a class | |
41 // type Foo by defining either operator<<(::std::ostream&, const Foo&) | |
42 // or void PrintTo(const Foo&, ::std::ostream*) in the namespace that | |
43 // defines Foo. | |
44 | |
45 #include <gmock/gmock-printers.h> | |
46 #include <ctype.h> | |
47 #include <stdio.h> | |
48 #include <ostream> // NOLINT | |
49 #include <string> | |
50 #include <gmock/internal/gmock-port.h> | |
51 | |
52 namespace testing { | |
53 | |
54 namespace { | |
55 | |
56 using ::std::ostream; | |
57 | |
58 #if GTEST_OS_WINDOWS_MOBILE // Windows CE does not define _snprintf_s. | |
59 #define snprintf _snprintf | |
60 #elif _MSC_VER >= 1400 // VC 8.0 and later deprecate snprintf and _snprintf. | |
61 #define snprintf _snprintf_s | |
62 #elif _MSC_VER | |
63 #define snprintf _snprintf | |
64 #endif // GTEST_OS_WINDOWS_MOBILE | |
65 | |
66 // Prints a segment of bytes in the given object. | |
67 void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start, | |
68 size_t count, ostream* os) { | |
69 char text[5] = ""; | |
70 for (size_t i = 0; i != count; i++) { | |
71 const size_t j = start + i; | |
72 if (i != 0) { | |
73 // Organizes the bytes into groups of 2 for easy parsing by | |
74 // human. | |
75 if ((j % 2) == 0) { | |
76 *os << " "; | |
77 } | |
78 } | |
79 snprintf(text, sizeof(text), "%02X", obj_bytes[j]); | |
80 *os << text; | |
81 } | |
82 } | |
83 | |
84 // Prints the bytes in the given value to the given ostream. | |
85 void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count, | |
86 ostream* os) { | |
87 // Tells the user how big the object is. | |
88 *os << count << "-byte object <"; | |
89 | |
90 const size_t kThreshold = 132; | |
91 const size_t kChunkSize = 64; | |
92 // If the object size is bigger than kThreshold, we'll have to omit | |
93 // some details by printing only the first and the last kChunkSize | |
94 // bytes. | |
95 // TODO(wan): let the user control the threshold using a flag. | |
96 if (count < kThreshold) { | |
97 PrintByteSegmentInObjectTo(obj_bytes, 0, count, os); | |
98 } else { | |
99 PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os); | |
100 *os << " ... "; | |
101 // Rounds up to 2-byte boundary. | |
102 const size_t resume_pos = (count - kChunkSize + 1)/2*2; | |
103 PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os); | |
104 } | |
105 *os << ">"; | |
106 } | |
107 | |
108 } // namespace | |
109 | |
110 namespace internal2 { | |
111 | |
112 // Delegates to PrintBytesInObjectToImpl() to print the bytes in the | |
113 // given object. The delegation simplifies the implementation, which | |
114 // uses the << operator and thus is easier done outside of the | |
115 // ::testing::internal namespace, which contains a << operator that | |
116 // sometimes conflicts with the one in STL. | |
117 void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count, | |
118 ostream* os) { | |
119 PrintBytesInObjectToImpl(obj_bytes, count, os); | |
120 } | |
121 | |
122 } // namespace internal2 | |
123 | |
124 namespace internal { | |
125 | |
126 // Prints a wide char as a char literal without the quotes, escaping it | |
127 // when necessary. | |
128 static void PrintAsWideCharLiteralTo(wchar_t c, ostream* os) { | |
129 switch (c) { | |
130 case L'\0': | |
131 *os << "\\0"; | |
132 break; | |
133 case L'\'': | |
134 *os << "\\'"; | |
135 break; | |
136 case L'\?': | |
137 *os << "\\?"; | |
138 break; | |
139 case L'\\': | |
140 *os << "\\\\"; | |
141 break; | |
142 case L'\a': | |
143 *os << "\\a"; | |
144 break; | |
145 case L'\b': | |
146 *os << "\\b"; | |
147 break; | |
148 case L'\f': | |
149 *os << "\\f"; | |
150 break; | |
151 case L'\n': | |
152 *os << "\\n"; | |
153 break; | |
154 case L'\r': | |
155 *os << "\\r"; | |
156 break; | |
157 case L'\t': | |
158 *os << "\\t"; | |
159 break; | |
160 case L'\v': | |
161 *os << "\\v"; | |
162 break; | |
163 default: | |
164 // Checks whether c is printable or not. Printable characters are in | |
165 // the range [0x20,0x7E]. | |
166 // We test the value of c directly instead of calling isprint(), as | |
167 // isprint() is buggy on Windows mobile. | |
168 if (0x20 <= c && c <= 0x7E) { | |
169 *os << static_cast<char>(c); | |
170 } else { | |
171 // Buffer size enough for the maximum number of digits and \0. | |
172 char text[2 * sizeof(unsigned long) + 1] = ""; | |
173 snprintf(text, sizeof(text), "%lX", static_cast<unsigned long>(c)); | |
174 *os << "\\x" << text; | |
175 } | |
176 } | |
177 } | |
178 | |
179 // Prints a char as if it's part of a string literal, escaping it when | |
180 // necessary. | |
181 static void PrintAsWideStringLiteralTo(wchar_t c, ostream* os) { | |
182 switch (c) { | |
183 case L'\'': | |
184 *os << "'"; | |
185 break; | |
186 case L'"': | |
187 *os << "\\\""; | |
188 break; | |
189 default: | |
190 PrintAsWideCharLiteralTo(c, os); | |
191 } | |
192 } | |
193 | |
194 // Prints a char as a char literal without the quotes, escaping it | |
195 // when necessary. | |
196 static void PrintAsCharLiteralTo(char c, ostream* os) { | |
197 PrintAsWideCharLiteralTo(static_cast<unsigned char>(c), os); | |
198 } | |
199 | |
200 // Prints a char as if it's part of a string literal, escaping it when | |
201 // necessary. | |
202 static void PrintAsStringLiteralTo(char c, ostream* os) { | |
203 PrintAsWideStringLiteralTo(static_cast<unsigned char>(c), os); | |
204 } | |
205 | |
206 // Prints a char and its code. The '\0' char is printed as "'\\0'", | |
207 // other unprintable characters are also properly escaped using the | |
208 // standard C++ escape sequence. | |
209 void PrintCharTo(char c, int char_code, ostream* os) { | |
210 *os << "'"; | |
211 PrintAsCharLiteralTo(c, os); | |
212 *os << "'"; | |
213 if (c != '\0') | |
214 *os << " (" << char_code << ")"; | |
215 } | |
216 | |
217 // Prints a wchar_t as a symbol if it is printable or as its internal | |
218 // code otherwise and also as its decimal code (except for L'\0'). | |
219 // The L'\0' char is printed as "L'\\0'". The decimal code is printed | |
220 // as signed integer when wchar_t is implemented by the compiler | |
221 // as a signed type and is printed as an unsigned integer when wchar_t | |
222 // is implemented as an unsigned type. | |
223 void PrintTo(wchar_t wc, ostream* os) { | |
224 *os << "L'"; | |
225 PrintAsWideCharLiteralTo(wc, os); | |
226 *os << "'"; | |
227 if (wc != L'\0') { | |
228 // Type Int64 is used because it provides more storage than wchar_t thus | |
229 // when the compiler converts signed or unsigned implementation of wchar_t | |
230 // to Int64 it fills higher bits with either zeros or the sign bit | |
231 // passing it to operator <<() as either signed or unsigned integer. | |
232 *os << " (" << static_cast<Int64>(wc) << ")"; | |
233 } | |
234 } | |
235 | |
236 // Prints the given array of characters to the ostream. | |
237 // The array starts at *begin, the length is len, it may include '\0' characters | |
238 // and may not be null-terminated. | |
239 static void PrintCharsAsStringTo(const char* begin, size_t len, ostream* os) { | |
240 *os << "\""; | |
241 for (size_t index = 0; index < len; ++index) { | |
242 PrintAsStringLiteralTo(begin[index], os); | |
243 } | |
244 *os << "\""; | |
245 } | |
246 | |
247 // Prints a (const) char array of 'len' elements, starting at address 'begin'. | |
248 void UniversalPrintArray(const char* begin, size_t len, ostream* os) { | |
249 PrintCharsAsStringTo(begin, len, os); | |
250 } | |
251 | |
252 // Prints the given array of wide characters to the ostream. | |
253 // The array starts at *begin, the length is len, it may include L'\0' | |
254 // characters and may not be null-terminated. | |
255 static void PrintWideCharsAsStringTo(const wchar_t* begin, size_t len, | |
256 ostream* os) { | |
257 *os << "L\""; | |
258 for (size_t index = 0; index < len; ++index) { | |
259 PrintAsWideStringLiteralTo(begin[index], os); | |
260 } | |
261 *os << "\""; | |
262 } | |
263 | |
264 // Prints the given C string to the ostream. | |
265 void PrintTo(const char* s, ostream* os) { | |
266 if (s == NULL) { | |
267 *os << "NULL"; | |
268 } else { | |
269 *os << implicit_cast<const void*>(s) << " pointing to "; | |
270 PrintCharsAsStringTo(s, strlen(s), os); | |
271 } | |
272 } | |
273 | |
274 // MSVC compiler can be configured to define whar_t as a typedef | |
275 // of unsigned short. Defining an overload for const wchar_t* in that case | |
276 // would cause pointers to unsigned shorts be printed as wide strings, | |
277 // possibly accessing more memory than intended and causing invalid | |
278 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when | |
279 // wchar_t is implemented as a native type. | |
280 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) | |
281 // Prints the given wide C string to the ostream. | |
282 void PrintTo(const wchar_t* s, ostream* os) { | |
283 if (s == NULL) { | |
284 *os << "NULL"; | |
285 } else { | |
286 *os << implicit_cast<const void*>(s) << " pointing to "; | |
287 PrintWideCharsAsStringTo(s, wcslen(s), os); | |
288 } | |
289 } | |
290 #endif // wchar_t is native | |
291 | |
292 // Prints a ::string object. | |
293 #if GTEST_HAS_GLOBAL_STRING | |
294 void PrintStringTo(const ::string& s, ostream* os) { | |
295 PrintCharsAsStringTo(s.data(), s.size(), os); | |
296 } | |
297 #endif // GTEST_HAS_GLOBAL_STRING | |
298 | |
299 void PrintStringTo(const ::std::string& s, ostream* os) { | |
300 PrintCharsAsStringTo(s.data(), s.size(), os); | |
301 } | |
302 | |
303 // Prints a ::wstring object. | |
304 #if GTEST_HAS_GLOBAL_WSTRING | |
305 void PrintWideStringTo(const ::wstring& s, ostream* os) { | |
306 PrintWideCharsAsStringTo(s.data(), s.size(), os); | |
307 } | |
308 #endif // GTEST_HAS_GLOBAL_WSTRING | |
309 | |
310 #if GTEST_HAS_STD_WSTRING | |
311 void PrintWideStringTo(const ::std::wstring& s, ostream* os) { | |
312 PrintWideCharsAsStringTo(s.data(), s.size(), os); | |
313 } | |
314 #endif // GTEST_HAS_STD_WSTRING | |
315 | |
316 } // namespace internal | |
317 | |
318 } // namespace testing | |
OLD | NEW |