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 tests the universal value printer. | |
35 | |
36 #include <gmock/gmock-printers.h> | |
37 | |
38 #include <ctype.h> | |
39 #include <limits.h> | |
40 #include <string.h> | |
41 #include <algorithm> | |
42 #include <deque> | |
43 #include <list> | |
44 #include <map> | |
45 #include <set> | |
46 #include <sstream> | |
47 #include <string> | |
48 #include <utility> | |
49 #include <vector> | |
50 #include <gmock/gmock-generated-matchers.h> | |
51 #include <gmock/gmock-matchers.h> | |
52 #include <gmock/internal/gmock-port.h> | |
53 #include <gtest/gtest.h> | |
54 | |
55 // hash_map and hash_set are available on Windows. | |
56 #if GTEST_OS_WINDOWS | |
57 #define GMOCK_HAS_HASH_MAP_ 1 // Indicates that hash_map is available. | |
58 #include <hash_map> // NOLINT | |
59 #define GMOCK_HAS_HASH_SET_ 1 // Indicates that hash_set is available. | |
60 #include <hash_set> // NOLINT | |
61 #endif // GTEST_OS_WINDOWS | |
62 | |
63 // Some user-defined types for testing the universal value printer. | |
64 | |
65 // A user-defined unprintable class template in the global namespace. | |
66 template <typename T> | |
67 class UnprintableTemplateInGlobal { | |
68 public: | |
69 UnprintableTemplateInGlobal() : value_() {} | |
70 private: | |
71 T value_; | |
72 }; | |
73 | |
74 // A user-defined streamable type in the global namespace. | |
75 class StreamableInGlobal { | |
76 public: | |
77 virtual ~StreamableInGlobal() {} | |
78 }; | |
79 | |
80 inline void operator<<(::std::ostream& os, const StreamableInGlobal& /* x */) { | |
81 os << "StreamableInGlobal"; | |
82 } | |
83 | |
84 namespace foo { | |
85 | |
86 // A user-defined unprintable type in a user namespace. | |
87 class UnprintableInFoo { | |
88 public: | |
89 UnprintableInFoo() : x_(0x12EF), y_(0xAB34), z_(0) {} | |
90 private: | |
91 testing::internal::Int32 x_; | |
92 testing::internal::Int32 y_; | |
93 double z_; | |
94 }; | |
95 | |
96 // A user-defined printable type in a user-chosen namespace. | |
97 struct PrintableViaPrintTo { | |
98 PrintableViaPrintTo() : value() {} | |
99 int value; | |
100 }; | |
101 | |
102 void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) { | |
103 *os << "PrintableViaPrintTo: " << x.value; | |
104 } | |
105 | |
106 // A user-defined printable class template in a user-chosen namespace. | |
107 template <typename T> | |
108 class PrintableViaPrintToTemplate { | |
109 public: | |
110 explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {} | |
111 | |
112 const T& value() const { return value_; } | |
113 private: | |
114 T value_; | |
115 }; | |
116 | |
117 template <typename T> | |
118 void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) { | |
119 *os << "PrintableViaPrintToTemplate: " << x.value(); | |
120 } | |
121 | |
122 // A user-defined streamable class template in a user namespace. | |
123 template <typename T> | |
124 class StreamableTemplateInFoo { | |
125 public: | |
126 StreamableTemplateInFoo() : value_() {} | |
127 | |
128 const T& value() const { return value_; } | |
129 private: | |
130 T value_; | |
131 }; | |
132 | |
133 template <typename T> | |
134 inline ::std::ostream& operator<<(::std::ostream& os, | |
135 const StreamableTemplateInFoo<T>& x) { | |
136 return os << "StreamableTemplateInFoo: " << x.value(); | |
137 } | |
138 | |
139 } // namespace foo | |
140 | |
141 namespace testing { | |
142 namespace gmock_printers_test { | |
143 | |
144 using ::std::deque; | |
145 using ::std::list; | |
146 using ::std::make_pair; | |
147 using ::std::map; | |
148 using ::std::multimap; | |
149 using ::std::multiset; | |
150 using ::std::pair; | |
151 using ::std::set; | |
152 using ::std::tr1::make_tuple; | |
153 using ::std::tr1::tuple; | |
154 using ::std::vector; | |
155 using ::testing::ElementsAre; | |
156 using ::testing::PrintToString; | |
157 using ::testing::StartsWith; | |
158 using ::testing::internal::NativeArray; | |
159 using ::testing::internal::Strings; | |
160 using ::testing::internal::UniversalTersePrint; | |
161 using ::testing::internal::UniversalPrint; | |
162 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings; | |
163 using ::testing::internal::UniversalPrinter; | |
164 using ::testing::internal::kReference; | |
165 using ::testing::internal::string; | |
166 | |
167 #if GTEST_OS_WINDOWS | |
168 // MSVC defines the following classes in the ::stdext namespace while | |
169 // gcc defines them in the :: namespace. Note that they are not part | |
170 // of the C++ standard. | |
171 | |
172 using ::stdext::hash_map; | |
173 using ::stdext::hash_set; | |
174 using ::stdext::hash_multimap; | |
175 using ::stdext::hash_multiset; | |
176 | |
177 #endif // GTEST_OS_WINDOWS | |
178 | |
179 // Prints a value to a string using the universal value printer. This | |
180 // is a helper for testing UniversalPrinter<T>::Print() for various types. | |
181 template <typename T> | |
182 string Print(const T& value) { | |
183 ::std::stringstream ss; | |
184 UniversalPrinter<T>::Print(value, &ss); | |
185 return ss.str(); | |
186 } | |
187 | |
188 // Prints a value passed by reference to a string, using the universal | |
189 // value printer. This is a helper for testing | |
190 // UniversalPrinter<T&>::Print() for various types. | |
191 template <typename T> | |
192 string PrintByRef(const T& value) { | |
193 ::std::stringstream ss; | |
194 UniversalPrinter<T&>::Print(value, &ss); | |
195 return ss.str(); | |
196 } | |
197 | |
198 // Tests printing various char types. | |
199 | |
200 // char. | |
201 TEST(PrintCharTest, PlainChar) { | |
202 EXPECT_EQ("'\\0'", Print('\0')); | |
203 EXPECT_EQ("'\\'' (39)", Print('\'')); | |
204 EXPECT_EQ("'\"' (34)", Print('"')); | |
205 EXPECT_EQ("'\\?' (63)", Print('\?')); | |
206 EXPECT_EQ("'\\\\' (92)", Print('\\')); | |
207 EXPECT_EQ("'\\a' (7)", Print('\a')); | |
208 EXPECT_EQ("'\\b' (8)", Print('\b')); | |
209 EXPECT_EQ("'\\f' (12)", Print('\f')); | |
210 EXPECT_EQ("'\\n' (10)", Print('\n')); | |
211 EXPECT_EQ("'\\r' (13)", Print('\r')); | |
212 EXPECT_EQ("'\\t' (9)", Print('\t')); | |
213 EXPECT_EQ("'\\v' (11)", Print('\v')); | |
214 EXPECT_EQ("'\\x7F' (127)", Print('\x7F')); | |
215 EXPECT_EQ("'\\xFF' (255)", Print('\xFF')); | |
216 EXPECT_EQ("' ' (32)", Print(' ')); | |
217 EXPECT_EQ("'a' (97)", Print('a')); | |
218 } | |
219 | |
220 // signed char. | |
221 TEST(PrintCharTest, SignedChar) { | |
222 EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0'))); | |
223 EXPECT_EQ("'\\xCE' (-50)", | |
224 Print(static_cast<signed char>(-50))); | |
225 } | |
226 | |
227 // unsigned char. | |
228 TEST(PrintCharTest, UnsignedChar) { | |
229 EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0'))); | |
230 EXPECT_EQ("'b' (98)", | |
231 Print(static_cast<unsigned char>('b'))); | |
232 } | |
233 | |
234 // Tests printing other simple, built-in types. | |
235 | |
236 // bool. | |
237 TEST(PrintBuiltInTypeTest, Bool) { | |
238 EXPECT_EQ("false", Print(false)); | |
239 EXPECT_EQ("true", Print(true)); | |
240 } | |
241 | |
242 // wchar_t. | |
243 TEST(PrintBuiltInTypeTest, Wchar_t) { | |
244 EXPECT_EQ("L'\\0'", Print(L'\0')); | |
245 EXPECT_EQ("L'\\'' (39)", Print(L'\'')); | |
246 EXPECT_EQ("L'\"' (34)", Print(L'"')); | |
247 EXPECT_EQ("L'\\?' (63)", Print(L'\?')); | |
248 EXPECT_EQ("L'\\\\' (92)", Print(L'\\')); | |
249 EXPECT_EQ("L'\\a' (7)", Print(L'\a')); | |
250 EXPECT_EQ("L'\\b' (8)", Print(L'\b')); | |
251 EXPECT_EQ("L'\\f' (12)", Print(L'\f')); | |
252 EXPECT_EQ("L'\\n' (10)", Print(L'\n')); | |
253 EXPECT_EQ("L'\\r' (13)", Print(L'\r')); | |
254 EXPECT_EQ("L'\\t' (9)", Print(L'\t')); | |
255 EXPECT_EQ("L'\\v' (11)", Print(L'\v')); | |
256 EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F')); | |
257 EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF')); | |
258 EXPECT_EQ("L' ' (32)", Print(L' ')); | |
259 EXPECT_EQ("L'a' (97)", Print(L'a')); | |
260 EXPECT_EQ("L'\\x576' (1398)", Print(L'\x576')); | |
261 EXPECT_EQ("L'\\xC74D' (51021)", Print(L'\xC74D')); | |
262 } | |
263 | |
264 // Test that Int64 provides more storage than wchar_t. | |
265 TEST(PrintTypeSizeTest, Wchar_t) { | |
266 EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64)); | |
267 } | |
268 | |
269 // Various integer types. | |
270 TEST(PrintBuiltInTypeTest, Integer) { | |
271 EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8 | |
272 EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8 | |
273 EXPECT_EQ("65535", Print(USHRT_MAX)); // uint16 | |
274 EXPECT_EQ("-32768", Print(SHRT_MIN)); // int16 | |
275 EXPECT_EQ("4294967295", Print(UINT_MAX)); // uint32 | |
276 EXPECT_EQ("-2147483648", Print(INT_MIN)); // int32 | |
277 EXPECT_EQ("18446744073709551615", | |
278 Print(static_cast<testing::internal::UInt64>(-1))); // uint64 | |
279 EXPECT_EQ("-9223372036854775808", | |
280 Print(static_cast<testing::internal::Int64>(1) << 63)); // int64 | |
281 } | |
282 | |
283 // Size types. | |
284 TEST(PrintBuiltInTypeTest, Size_t) { | |
285 EXPECT_EQ("1", Print(sizeof('a'))); // size_t. | |
286 #if !GTEST_OS_WINDOWS | |
287 // Windows has no ssize_t type. | |
288 EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t. | |
289 #endif // !GTEST_OS_WINDOWS | |
290 } | |
291 | |
292 // Floating-points. | |
293 TEST(PrintBuiltInTypeTest, FloatingPoints) { | |
294 EXPECT_EQ("1.5", Print(1.5f)); // float | |
295 EXPECT_EQ("-2.5", Print(-2.5)); // double | |
296 } | |
297 | |
298 // Since ::std::stringstream::operator<<(const void *) formats the pointer | |
299 // output differently with different compilers, we have to create the expected | |
300 // output first and use it as our expectation. | |
301 static string PrintPointer(const void *p) { | |
302 ::std::stringstream expected_result_stream; | |
303 expected_result_stream << p; | |
304 return expected_result_stream.str(); | |
305 } | |
306 | |
307 // Tests printing C strings. | |
308 | |
309 // const char*. | |
310 TEST(PrintCStringTest, Const) { | |
311 const char* p = "World"; | |
312 EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p)); | |
313 } | |
314 | |
315 // char*. | |
316 TEST(PrintCStringTest, NonConst) { | |
317 char p[] = "Hi"; | |
318 EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"", | |
319 Print(static_cast<char*>(p))); | |
320 } | |
321 | |
322 // NULL C string. | |
323 TEST(PrintCStringTest, Null) { | |
324 const char* p = NULL; | |
325 EXPECT_EQ("NULL", Print(p)); | |
326 } | |
327 | |
328 // Tests that C strings are escaped properly. | |
329 TEST(PrintCStringTest, EscapesProperly) { | |
330 const char* p = "'\"\?\\\a\b\f\n\r\t\v\x7F\xFF a"; | |
331 EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"\\?\\\\\\a\\b\\f" | |
332 "\\n\\r\\t\\v\\x7F\\xFF a\"", | |
333 Print(p)); | |
334 } | |
335 | |
336 | |
337 | |
338 // MSVC compiler can be configured to define whar_t as a typedef | |
339 // of unsigned short. Defining an overload for const wchar_t* in that case | |
340 // would cause pointers to unsigned shorts be printed as wide strings, | |
341 // possibly accessing more memory than intended and causing invalid | |
342 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when | |
343 // wchar_t is implemented as a native type. | |
344 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) | |
345 | |
346 // const wchar_t*. | |
347 TEST(PrintWideCStringTest, Const) { | |
348 const wchar_t* p = L"World"; | |
349 EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p)); | |
350 } | |
351 | |
352 // wchar_t*. | |
353 TEST(PrintWideCStringTest, NonConst) { | |
354 wchar_t p[] = L"Hi"; | |
355 EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"", | |
356 Print(static_cast<wchar_t*>(p))); | |
357 } | |
358 | |
359 // NULL wide C string. | |
360 TEST(PrintWideCStringTest, Null) { | |
361 const wchar_t* p = NULL; | |
362 EXPECT_EQ("NULL", Print(p)); | |
363 } | |
364 | |
365 // Tests that wide C strings are escaped properly. | |
366 TEST(PrintWideCStringTest, EscapesProperly) { | |
367 const wchar_t* p = L"'\"\?\\\a\b\f\n\r\t\v\xD3\x576\x8D3\xC74D a"; | |
368 EXPECT_EQ(PrintPointer(p) + " pointing to L\"'\\\"\\?\\\\\\a\\b\\f" | |
369 "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"", | |
370 Print(p)); | |
371 } | |
372 #endif // native wchar_t | |
373 | |
374 // Tests printing pointers to other char types. | |
375 | |
376 // signed char*. | |
377 TEST(PrintCharPointerTest, SignedChar) { | |
378 signed char* p = reinterpret_cast<signed char*>(0x1234); | |
379 EXPECT_EQ(PrintPointer(p), Print(p)); | |
380 p = NULL; | |
381 EXPECT_EQ("NULL", Print(p)); | |
382 } | |
383 | |
384 // const signed char*. | |
385 TEST(PrintCharPointerTest, ConstSignedChar) { | |
386 signed char* p = reinterpret_cast<signed char*>(0x1234); | |
387 EXPECT_EQ(PrintPointer(p), Print(p)); | |
388 p = NULL; | |
389 EXPECT_EQ("NULL", Print(p)); | |
390 } | |
391 | |
392 // unsigned char*. | |
393 TEST(PrintCharPointerTest, UnsignedChar) { | |
394 unsigned char* p = reinterpret_cast<unsigned char*>(0x1234); | |
395 EXPECT_EQ(PrintPointer(p), Print(p)); | |
396 p = NULL; | |
397 EXPECT_EQ("NULL", Print(p)); | |
398 } | |
399 | |
400 // const unsigned char*. | |
401 TEST(PrintCharPointerTest, ConstUnsignedChar) { | |
402 const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234); | |
403 EXPECT_EQ(PrintPointer(p), Print(p)); | |
404 p = NULL; | |
405 EXPECT_EQ("NULL", Print(p)); | |
406 } | |
407 | |
408 // Tests printing pointers to simple, built-in types. | |
409 | |
410 // bool*. | |
411 TEST(PrintPointerToBuiltInTypeTest, Bool) { | |
412 bool* p = reinterpret_cast<bool*>(0xABCD); | |
413 EXPECT_EQ(PrintPointer(p), Print(p)); | |
414 p = NULL; | |
415 EXPECT_EQ("NULL", Print(p)); | |
416 } | |
417 | |
418 // void*. | |
419 TEST(PrintPointerToBuiltInTypeTest, Void) { | |
420 void* p = reinterpret_cast<void*>(0xABCD); | |
421 EXPECT_EQ(PrintPointer(p), Print(p)); | |
422 p = NULL; | |
423 EXPECT_EQ("NULL", Print(p)); | |
424 } | |
425 | |
426 // const void*. | |
427 TEST(PrintPointerToBuiltInTypeTest, ConstVoid) { | |
428 const void* p = reinterpret_cast<const void*>(0xABCD); | |
429 EXPECT_EQ(PrintPointer(p), Print(p)); | |
430 p = NULL; | |
431 EXPECT_EQ("NULL", Print(p)); | |
432 } | |
433 | |
434 // Tests printing pointers to pointers. | |
435 TEST(PrintPointerToPointerTest, IntPointerPointer) { | |
436 int** p = reinterpret_cast<int**>(0xABCD); | |
437 EXPECT_EQ(PrintPointer(p), Print(p)); | |
438 p = NULL; | |
439 EXPECT_EQ("NULL", Print(p)); | |
440 } | |
441 | |
442 // Tests printing (non-member) function pointers. | |
443 | |
444 void MyFunction(int /* n */) {} | |
445 | |
446 TEST(PrintPointerTest, NonMemberFunctionPointer) { | |
447 // We cannot directly cast &MyFunction to const void* because the | |
448 // standard disallows casting between pointers to functions and | |
449 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce | |
450 // this limitation. | |
451 EXPECT_EQ( | |
452 PrintPointer(reinterpret_cast<const void*>( | |
453 reinterpret_cast<internal::BiggestInt>(&MyFunction))), | |
454 Print(&MyFunction)); | |
455 int (*p)(bool) = NULL; // NOLINT | |
456 EXPECT_EQ("NULL", Print(p)); | |
457 } | |
458 | |
459 // Tests printing member variable pointers. Although they are called | |
460 // pointers, they don't point to a location in the address space. | |
461 // Their representation is implementation-defined. Thus they will be | |
462 // printed as raw bytes. | |
463 | |
464 struct Foo { | |
465 public: | |
466 virtual ~Foo() {} | |
467 int MyMethod(char x) { return x + 1; } | |
468 virtual char MyVirtualMethod(int /* n */) { return 'a'; } | |
469 | |
470 int value; | |
471 }; | |
472 | |
473 TEST(PrintPointerTest, MemberVariablePointer) { | |
474 EXPECT_THAT(Print(&Foo::value), | |
475 StartsWith(Print(sizeof(&Foo::value)) + "-byte object ")); | |
476 int (Foo::*p) = NULL; // NOLINT | |
477 EXPECT_THAT(Print(p), | |
478 StartsWith(Print(sizeof(p)) + "-byte object ")); | |
479 } | |
480 | |
481 // Tests printing member function pointers. Although they are called | |
482 // pointers, they don't point to a location in the address space. | |
483 // Their representation is implementation-defined. Thus they will be | |
484 // printed as raw bytes. | |
485 TEST(PrintPointerTest, MemberFunctionPointer) { | |
486 EXPECT_THAT(Print(&Foo::MyMethod), | |
487 StartsWith(Print(sizeof(&Foo::MyMethod)) + "-byte object ")); | |
488 EXPECT_THAT(Print(&Foo::MyVirtualMethod), | |
489 StartsWith(Print(sizeof((&Foo::MyVirtualMethod))) | |
490 + "-byte object ")); | |
491 int (Foo::*p)(char) = NULL; // NOLINT | |
492 EXPECT_THAT(Print(p), | |
493 StartsWith(Print(sizeof(p)) + "-byte object ")); | |
494 } | |
495 | |
496 // Tests printing C arrays. | |
497 | |
498 // The difference between this and Print() is that it ensures that the | |
499 // argument is a reference to an array. | |
500 template <typename T, size_t N> | |
501 string PrintArrayHelper(T (&a)[N]) { | |
502 return Print(a); | |
503 } | |
504 | |
505 // One-dimensional array. | |
506 TEST(PrintArrayTest, OneDimensionalArray) { | |
507 int a[5] = { 1, 2, 3, 4, 5 }; | |
508 EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a)); | |
509 } | |
510 | |
511 // Two-dimensional array. | |
512 TEST(PrintArrayTest, TwoDimensionalArray) { | |
513 int a[2][5] = { | |
514 { 1, 2, 3, 4, 5 }, | |
515 { 6, 7, 8, 9, 0 } | |
516 }; | |
517 EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a)); | |
518 } | |
519 | |
520 // Array of const elements. | |
521 TEST(PrintArrayTest, ConstArray) { | |
522 const bool a[1] = { false }; | |
523 EXPECT_EQ("{ false }", PrintArrayHelper(a)); | |
524 } | |
525 | |
526 // Char array. | |
527 TEST(PrintArrayTest, CharArray) { | |
528 // Array a contains '\0' in the middle and doesn't end with '\0'. | |
529 char a[3] = { 'H', '\0', 'i' }; | |
530 EXPECT_EQ("\"H\\0i\"", PrintArrayHelper(a)); | |
531 } | |
532 | |
533 // Const char array. | |
534 TEST(PrintArrayTest, ConstCharArray) { | |
535 const char a[4] = "\0Hi"; | |
536 EXPECT_EQ("\"\\0Hi\\0\"", PrintArrayHelper(a)); | |
537 } | |
538 | |
539 // Array of objects. | |
540 TEST(PrintArrayTest, ObjectArray) { | |
541 string a[3] = { "Hi", "Hello", "Ni hao" }; | |
542 EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a)); | |
543 } | |
544 | |
545 // Array with many elements. | |
546 TEST(PrintArrayTest, BigArray) { | |
547 int a[100] = { 1, 2, 3 }; | |
548 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }", | |
549 PrintArrayHelper(a)); | |
550 } | |
551 | |
552 // Tests printing ::string and ::std::string. | |
553 | |
554 #if GTEST_HAS_GLOBAL_STRING | |
555 // ::string. | |
556 TEST(PrintStringTest, StringInGlobalNamespace) { | |
557 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; | |
558 const ::string str(s, sizeof(s)); | |
559 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", | |
560 Print(str)); | |
561 } | |
562 #endif // GTEST_HAS_GLOBAL_STRING | |
563 | |
564 // ::std::string. | |
565 TEST(PrintStringTest, StringInStdNamespace) { | |
566 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; | |
567 const ::std::string str(s, sizeof(s)); | |
568 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", | |
569 Print(str)); | |
570 } | |
571 | |
572 // Tests printing ::wstring and ::std::wstring. | |
573 | |
574 #if GTEST_HAS_GLOBAL_WSTRING | |
575 // ::wstring. | |
576 TEST(PrintWideStringTest, StringInGlobalNamespace) { | |
577 const wchar_t s[] = L"'\"\?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; | |
578 const ::wstring str(s, sizeof(s)/sizeof(wchar_t)); | |
579 EXPECT_EQ("L\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" | |
580 "\\xD3\\x576\\x8D3\\xC74D a\\0\"", | |
581 Print(str)); | |
582 } | |
583 #endif // GTEST_HAS_GLOBAL_WSTRING | |
584 | |
585 #if GTEST_HAS_STD_WSTRING | |
586 // ::std::wstring. | |
587 TEST(PrintWideStringTest, StringInStdNamespace) { | |
588 const wchar_t s[] = L"'\"\?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; | |
589 const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t)); | |
590 EXPECT_EQ("L\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" | |
591 "\\xD3\\x576\\x8D3\\xC74D a\\0\"", | |
592 Print(str)); | |
593 } | |
594 #endif // GTEST_HAS_STD_WSTRING | |
595 | |
596 // Tests printing types that support generic streaming (i.e. streaming | |
597 // to std::basic_ostream<Char, CharTraits> for any valid Char and | |
598 // CharTraits types). | |
599 | |
600 // Tests printing a non-template type that supports generic streaming. | |
601 | |
602 class AllowsGenericStreaming {}; | |
603 | |
604 template <typename Char, typename CharTraits> | |
605 std::basic_ostream<Char, CharTraits>& operator<<( | |
606 std::basic_ostream<Char, CharTraits>& os, | |
607 const AllowsGenericStreaming& /* a */) { | |
608 return os << "AllowsGenericStreaming"; | |
609 } | |
610 | |
611 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) { | |
612 AllowsGenericStreaming a; | |
613 EXPECT_EQ("AllowsGenericStreaming", Print(a)); | |
614 } | |
615 | |
616 // Tests printing a template type that supports generic streaming. | |
617 | |
618 template <typename T> | |
619 class AllowsGenericStreamingTemplate {}; | |
620 | |
621 template <typename Char, typename CharTraits, typename T> | |
622 std::basic_ostream<Char, CharTraits>& operator<<( | |
623 std::basic_ostream<Char, CharTraits>& os, | |
624 const AllowsGenericStreamingTemplate<T>& /* a */) { | |
625 return os << "AllowsGenericStreamingTemplate"; | |
626 } | |
627 | |
628 TEST(PrintTypeWithGenericStreamingTest, TemplateType) { | |
629 AllowsGenericStreamingTemplate<int> a; | |
630 EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a)); | |
631 } | |
632 | |
633 // Tests printing a type that supports generic streaming and can be | |
634 // implicitly converted to another printable type. | |
635 | |
636 template <typename T> | |
637 class AllowsGenericStreamingAndImplicitConversionTemplate { | |
638 public: | |
639 operator bool() const { return false; } | |
640 }; | |
641 | |
642 template <typename Char, typename CharTraits, typename T> | |
643 std::basic_ostream<Char, CharTraits>& operator<<( | |
644 std::basic_ostream<Char, CharTraits>& os, | |
645 const AllowsGenericStreamingAndImplicitConversionTemplate<T>& /* a */) { | |
646 return os << "AllowsGenericStreamingAndImplicitConversionTemplate"; | |
647 } | |
648 | |
649 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) { | |
650 AllowsGenericStreamingAndImplicitConversionTemplate<int> a; | |
651 EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a)); | |
652 } | |
653 | |
654 // Tests printing STL containers. | |
655 | |
656 TEST(PrintStlContainerTest, EmptyDeque) { | |
657 deque<char> empty; | |
658 EXPECT_EQ("{}", Print(empty)); | |
659 } | |
660 | |
661 TEST(PrintStlContainerTest, NonEmptyDeque) { | |
662 deque<int> non_empty; | |
663 non_empty.push_back(1); | |
664 non_empty.push_back(3); | |
665 EXPECT_EQ("{ 1, 3 }", Print(non_empty)); | |
666 } | |
667 | |
668 #if GMOCK_HAS_HASH_MAP_ | |
669 | |
670 TEST(PrintStlContainerTest, OneElementHashMap) { | |
671 hash_map<int, char> map1; | |
672 map1[1] = 'a'; | |
673 EXPECT_EQ("{ (1, 'a' (97)) }", Print(map1)); | |
674 } | |
675 | |
676 TEST(PrintStlContainerTest, HashMultiMap) { | |
677 hash_multimap<int, bool> map1; | |
678 map1.insert(make_pair(5, true)); | |
679 map1.insert(make_pair(5, false)); | |
680 | |
681 // Elements of hash_multimap can be printed in any order. | |
682 const string result = Print(map1); | |
683 EXPECT_TRUE(result == "{ (5, true), (5, false) }" || | |
684 result == "{ (5, false), (5, true) }") | |
685 << " where Print(map1) returns \"" << result << "\"."; | |
686 } | |
687 | |
688 #endif // GMOCK_HAS_HASH_MAP_ | |
689 | |
690 #if GMOCK_HAS_HASH_SET_ | |
691 | |
692 TEST(PrintStlContainerTest, HashSet) { | |
693 hash_set<string> set1; | |
694 set1.insert("hello"); | |
695 EXPECT_EQ("{ \"hello\" }", Print(set1)); | |
696 } | |
697 | |
698 TEST(PrintStlContainerTest, HashMultiSet) { | |
699 const int kSize = 5; | |
700 int a[kSize] = { 1, 1, 2, 5, 1 }; | |
701 hash_multiset<int> set1(a, a + kSize); | |
702 | |
703 // Elements of hash_multiset can be printed in any order. | |
704 const string result = Print(set1); | |
705 const string expected_pattern = "{ d, d, d, d, d }"; // d means a digit. | |
706 | |
707 // Verifies the result matches the expected pattern; also extracts | |
708 // the numbers in the result. | |
709 ASSERT_EQ(expected_pattern.length(), result.length()); | |
710 std::vector<int> numbers; | |
711 for (size_t i = 0; i != result.length(); i++) { | |
712 if (expected_pattern[i] == 'd') { | |
713 ASSERT_TRUE(isdigit(result[i]) != 0); | |
714 numbers.push_back(result[i] - '0'); | |
715 } else { | |
716 EXPECT_EQ(expected_pattern[i], result[i]) << " where result is " | |
717 << result; | |
718 } | |
719 } | |
720 | |
721 // Makes sure the result contains the right numbers. | |
722 std::sort(numbers.begin(), numbers.end()); | |
723 std::sort(a, a + kSize); | |
724 EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin())); | |
725 } | |
726 | |
727 #endif // GMOCK_HAS_HASH_SET_ | |
728 | |
729 TEST(PrintStlContainerTest, List) { | |
730 const char* a[] = { | |
731 "hello", | |
732 "world" | |
733 }; | |
734 const list<string> strings(a, a + 2); | |
735 EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings)); | |
736 } | |
737 | |
738 TEST(PrintStlContainerTest, Map) { | |
739 map<int, bool> map1; | |
740 map1[1] = true; | |
741 map1[5] = false; | |
742 map1[3] = true; | |
743 EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1)); | |
744 } | |
745 | |
746 TEST(PrintStlContainerTest, MultiMap) { | |
747 multimap<bool, int> map1; | |
748 map1.insert(make_pair(true, 0)); | |
749 map1.insert(make_pair(true, 1)); | |
750 map1.insert(make_pair(false, 2)); | |
751 EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1)); | |
752 } | |
753 | |
754 TEST(PrintStlContainerTest, Set) { | |
755 const unsigned int a[] = { 3, 0, 5 }; | |
756 set<unsigned int> set1(a, a + 3); | |
757 EXPECT_EQ("{ 0, 3, 5 }", Print(set1)); | |
758 } | |
759 | |
760 TEST(PrintStlContainerTest, MultiSet) { | |
761 const int a[] = { 1, 1, 2, 5, 1 }; | |
762 multiset<int> set1(a, a + 5); | |
763 EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1)); | |
764 } | |
765 | |
766 TEST(PrintStlContainerTest, Pair) { | |
767 pair<const bool, int> p(true, 5); | |
768 EXPECT_EQ("(true, 5)", Print(p)); | |
769 } | |
770 | |
771 TEST(PrintStlContainerTest, Vector) { | |
772 vector<int> v; | |
773 v.push_back(1); | |
774 v.push_back(2); | |
775 EXPECT_EQ("{ 1, 2 }", Print(v)); | |
776 } | |
777 | |
778 TEST(PrintStlContainerTest, LongSequence) { | |
779 const int a[100] = { 1, 2, 3 }; | |
780 const vector<int> v(a, a + 100); | |
781 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, " | |
782 "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v)); | |
783 } | |
784 | |
785 TEST(PrintStlContainerTest, NestedContainer) { | |
786 const int a1[] = { 1, 2 }; | |
787 const int a2[] = { 3, 4, 5 }; | |
788 const list<int> l1(a1, a1 + 2); | |
789 const list<int> l2(a2, a2 + 3); | |
790 | |
791 vector<list<int> > v; | |
792 v.push_back(l1); | |
793 v.push_back(l2); | |
794 EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v)); | |
795 } | |
796 | |
797 TEST(PrintStlContainerTest, OneDimensionalNativeArray) { | |
798 const int a[3] = { 1, 2, 3 }; | |
799 NativeArray<int> b(a, 3, kReference); | |
800 EXPECT_EQ("{ 1, 2, 3 }", Print(b)); | |
801 } | |
802 | |
803 TEST(PrintStlContainerTest, TwoDimensionalNativeArray) { | |
804 const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; | |
805 NativeArray<int[3]> b(a, 2, kReference); | |
806 EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b)); | |
807 } | |
808 | |
809 // Tests printing tuples. | |
810 | |
811 // Tuples of various arities. | |
812 TEST(PrintTupleTest, VariousSizes) { | |
813 tuple<> t0; | |
814 EXPECT_EQ("()", Print(t0)); | |
815 | |
816 tuple<int> t1(5); | |
817 EXPECT_EQ("(5)", Print(t1)); | |
818 | |
819 tuple<char, bool> t2('a', true); | |
820 EXPECT_EQ("('a' (97), true)", Print(t2)); | |
821 | |
822 tuple<bool, int, int> t3(false, 2, 3); | |
823 EXPECT_EQ("(false, 2, 3)", Print(t3)); | |
824 | |
825 tuple<bool, int, int, int> t4(false, 2, 3, 4); | |
826 EXPECT_EQ("(false, 2, 3, 4)", Print(t4)); | |
827 | |
828 tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true); | |
829 EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5)); | |
830 | |
831 tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6); | |
832 EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6)); | |
833 | |
834 tuple<bool, int, int, int, bool, int, int> t7(false, 2, 3, 4, true, 6, 7); | |
835 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7)); | |
836 | |
837 tuple<bool, int, int, int, bool, int, int, bool> t8( | |
838 false, 2, 3, 4, true, 6, 7, true); | |
839 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8)); | |
840 | |
841 tuple<bool, int, int, int, bool, int, int, bool, int> t9( | |
842 false, 2, 3, 4, true, 6, 7, true, 9); | |
843 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9)); | |
844 | |
845 const char* const str = "8"; | |
846 tuple<bool, char, short, testing::internal::Int32, // NOLINT | |
847 testing::internal::Int64, float, double, const char*, void*, string> | |
848 t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str, NULL, "10"); | |
849 EXPECT_EQ("(false, 'a' (97), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) + | |
850 " pointing to \"8\", NULL, \"10\")", | |
851 Print(t10)); | |
852 } | |
853 | |
854 // Nested tuples. | |
855 TEST(PrintTupleTest, NestedTuple) { | |
856 tuple<tuple<int, bool>, char> nested(make_tuple(5, true), 'a'); | |
857 EXPECT_EQ("((5, true), 'a' (97))", Print(nested)); | |
858 } | |
859 | |
860 // Tests printing user-defined unprintable types. | |
861 | |
862 // Unprintable types in the global namespace. | |
863 TEST(PrintUnprintableTypeTest, InGlobalNamespace) { | |
864 EXPECT_EQ("1-byte object <00>", | |
865 Print(UnprintableTemplateInGlobal<bool>())); | |
866 } | |
867 | |
868 // Unprintable types in a user namespace. | |
869 TEST(PrintUnprintableTypeTest, InUserNamespace) { | |
870 EXPECT_EQ("16-byte object <EF12 0000 34AB 0000 0000 0000 0000 0000>", | |
871 Print(::foo::UnprintableInFoo())); | |
872 } | |
873 | |
874 // Unprintable types are that too big to be printed completely. | |
875 | |
876 struct Big { | |
877 Big() { memset(array, 0, sizeof(array)); } | |
878 char array[257]; | |
879 }; | |
880 | |
881 TEST(PrintUnpritableTypeTest, BigObject) { | |
882 EXPECT_EQ("257-byte object <0000 0000 0000 0000 0000 0000 " | |
883 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 " | |
884 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 " | |
885 "0000 0000 0000 0000 0000 0000 ... 0000 0000 0000 " | |
886 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 " | |
887 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 " | |
888 "0000 0000 0000 0000 0000 0000 0000 0000 00>", | |
889 Print(Big())); | |
890 } | |
891 | |
892 // Tests printing user-defined streamable types. | |
893 | |
894 // Streamable types in the global namespace. | |
895 TEST(PrintStreamableTypeTest, InGlobalNamespace) { | |
896 EXPECT_EQ("StreamableInGlobal", | |
897 Print(StreamableInGlobal())); | |
898 } | |
899 | |
900 // Printable template types in a user namespace. | |
901 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) { | |
902 EXPECT_EQ("StreamableTemplateInFoo: 0", | |
903 Print(::foo::StreamableTemplateInFoo<int>())); | |
904 } | |
905 | |
906 // Tests printing user-defined types that have a PrintTo() function. | |
907 TEST(PrintPrintableTypeTest, InUserNamespace) { | |
908 EXPECT_EQ("PrintableViaPrintTo: 0", | |
909 Print(::foo::PrintableViaPrintTo())); | |
910 } | |
911 | |
912 // Tests printing user-defined class template that have a PrintTo() function. | |
913 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) { | |
914 EXPECT_EQ("PrintableViaPrintToTemplate: 5", | |
915 Print(::foo::PrintableViaPrintToTemplate<int>(5))); | |
916 } | |
917 | |
918 #if GMOCK_HAS_PROTOBUF_ | |
919 | |
920 // Tests printing a protocol message. | |
921 TEST(PrintProtocolMessageTest, PrintsShortDebugString) { | |
922 testing::internal::TestMessage msg; | |
923 msg.set_member("yes"); | |
924 EXPECT_EQ("<member:\"yes\">", Print(msg)); | |
925 } | |
926 | |
927 // Tests printing a short proto2 message. | |
928 TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) { | |
929 testing::internal::FooMessage msg; | |
930 msg.set_int_field(2); | |
931 msg.set_string_field("hello"); | |
932 EXPECT_PRED2(RE::FullMatch, Print(msg), | |
933 "<int_field:\\s*2\\s+string_field:\\s*\"hello\">"); | |
934 } | |
935 | |
936 // Tests printing a long proto2 message. | |
937 TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) { | |
938 testing::internal::FooMessage msg; | |
939 msg.set_int_field(2); | |
940 msg.set_string_field("hello"); | |
941 msg.add_names("peter"); | |
942 msg.add_names("paul"); | |
943 msg.add_names("mary"); | |
944 EXPECT_PRED2(RE::FullMatch, Print(msg), | |
945 "<\n" | |
946 "int_field:\\s*2\n" | |
947 "string_field:\\s*\"hello\"\n" | |
948 "names:\\s*\"peter\"\n" | |
949 "names:\\s*\"paul\"\n" | |
950 "names:\\s*\"mary\"\n" | |
951 ">"); | |
952 } | |
953 | |
954 #endif // GMOCK_HAS_PROTOBUF_ | |
955 | |
956 // Tests that the universal printer prints both the address and the | |
957 // value of a reference. | |
958 TEST(PrintReferenceTest, PrintsAddressAndValue) { | |
959 int n = 5; | |
960 EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n)); | |
961 | |
962 int a[2][3] = { | |
963 { 0, 1, 2 }, | |
964 { 3, 4, 5 } | |
965 }; | |
966 EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }", | |
967 PrintByRef(a)); | |
968 | |
969 const ::foo::UnprintableInFoo x; | |
970 EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object " | |
971 "<EF12 0000 34AB 0000 0000 0000 0000 0000>", | |
972 PrintByRef(x)); | |
973 } | |
974 | |
975 // Tests that the universal printer prints a function pointer passed by | |
976 // reference. | |
977 TEST(PrintReferenceTest, HandlesFunctionPointer) { | |
978 void (*fp)(int n) = &MyFunction; | |
979 const string fp_pointer_string = | |
980 PrintPointer(reinterpret_cast<const void*>(&fp)); | |
981 // We cannot directly cast &MyFunction to const void* because the | |
982 // standard disallows casting between pointers to functions and | |
983 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce | |
984 // this limitation. | |
985 const string fp_string = PrintPointer(reinterpret_cast<const void*>( | |
986 reinterpret_cast<internal::BiggestInt>(fp))); | |
987 EXPECT_EQ("@" + fp_pointer_string + " " + fp_string, | |
988 PrintByRef(fp)); | |
989 } | |
990 | |
991 // Tests that the universal printer prints a member function pointer | |
992 // passed by reference. | |
993 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) { | |
994 int (Foo::*p)(char ch) = &Foo::MyMethod; | |
995 EXPECT_THAT(PrintByRef(p), | |
996 StartsWith("@" + PrintPointer(reinterpret_cast<const void*>(&p)) | |
997 + " " + Print(sizeof(p)) + "-byte object ")); | |
998 | |
999 char (Foo::*p2)(int n) = &Foo::MyVirtualMethod; | |
1000 EXPECT_THAT(PrintByRef(p2), | |
1001 StartsWith("@" + PrintPointer(reinterpret_cast<const void*>(&p2)) | |
1002 + " " + Print(sizeof(p2)) + "-byte object ")); | |
1003 } | |
1004 | |
1005 // Tests that the universal printer prints a member variable pointer | |
1006 // passed by reference. | |
1007 TEST(PrintReferenceTest, HandlesMemberVariablePointer) { | |
1008 int (Foo::*p) = &Foo::value; // NOLINT | |
1009 EXPECT_THAT(PrintByRef(p), | |
1010 StartsWith("@" + PrintPointer(&p) | |
1011 + " " + Print(sizeof(p)) + "-byte object ")); | |
1012 } | |
1013 | |
1014 TEST(PrintToStringTest, WorksForScalar) { | |
1015 EXPECT_EQ("123", PrintToString(123)); | |
1016 } | |
1017 | |
1018 TEST(PrintToStringTest, WorksForPointerToConstChar) { | |
1019 const char* p = "hello"; | |
1020 EXPECT_EQ("\"hello\"", PrintToString(p)); | |
1021 } | |
1022 | |
1023 TEST(PrintToStringTest, WorksForPointerToNonConstChar) { | |
1024 char s[] = "hello"; | |
1025 char* p = s; | |
1026 EXPECT_EQ("\"hello\"", PrintToString(p)); | |
1027 } | |
1028 | |
1029 TEST(PrintToStringTest, WorksForArray) { | |
1030 int n[3] = { 1, 2, 3 }; | |
1031 EXPECT_EQ("{ 1, 2, 3 }", PrintToString(n)); | |
1032 } | |
1033 | |
1034 TEST(UniversalTersePrintTest, WorksForNonReference) { | |
1035 ::std::stringstream ss; | |
1036 UniversalTersePrint(123, &ss); | |
1037 EXPECT_EQ("123", ss.str()); | |
1038 } | |
1039 | |
1040 TEST(UniversalTersePrintTest, WorksForReference) { | |
1041 const int& n = 123; | |
1042 ::std::stringstream ss; | |
1043 UniversalTersePrint(n, &ss); | |
1044 EXPECT_EQ("123", ss.str()); | |
1045 } | |
1046 | |
1047 TEST(UniversalTersePrintTest, WorksForCString) { | |
1048 const char* s1 = "abc"; | |
1049 ::std::stringstream ss1; | |
1050 UniversalTersePrint(s1, &ss1); | |
1051 EXPECT_EQ("\"abc\"", ss1.str()); | |
1052 | |
1053 char* s2 = const_cast<char*>(s1); | |
1054 ::std::stringstream ss2; | |
1055 UniversalTersePrint(s2, &ss2); | |
1056 EXPECT_EQ("\"abc\"", ss2.str()); | |
1057 | |
1058 const char* s3 = NULL; | |
1059 ::std::stringstream ss3; | |
1060 UniversalTersePrint(s3, &ss3); | |
1061 EXPECT_EQ("NULL", ss3.str()); | |
1062 } | |
1063 | |
1064 TEST(UniversalPrintTest, WorksForNonReference) { | |
1065 ::std::stringstream ss; | |
1066 UniversalPrint(123, &ss); | |
1067 EXPECT_EQ("123", ss.str()); | |
1068 } | |
1069 | |
1070 TEST(UniversalPrintTest, WorksForReference) { | |
1071 const int& n = 123; | |
1072 ::std::stringstream ss; | |
1073 UniversalPrint(n, &ss); | |
1074 EXPECT_EQ("123", ss.str()); | |
1075 } | |
1076 | |
1077 TEST(UniversalPrintTest, WorksForCString) { | |
1078 const char* s1 = "abc"; | |
1079 ::std::stringstream ss1; | |
1080 UniversalPrint(s1, &ss1); | |
1081 EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str())); | |
1082 | |
1083 char* s2 = const_cast<char*>(s1); | |
1084 ::std::stringstream ss2; | |
1085 UniversalPrint(s2, &ss2); | |
1086 EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str())); | |
1087 | |
1088 const char* s3 = NULL; | |
1089 ::std::stringstream ss3; | |
1090 UniversalPrint(s3, &ss3); | |
1091 EXPECT_EQ("NULL", ss3.str()); | |
1092 } | |
1093 | |
1094 | |
1095 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) { | |
1096 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple()), | |
1097 ElementsAre()); | |
1098 } | |
1099 | |
1100 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) { | |
1101 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple(1)), | |
1102 ElementsAre("1")); | |
1103 } | |
1104 | |
1105 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) { | |
1106 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a')), | |
1107 ElementsAre("1", "'a' (97)")); | |
1108 } | |
1109 | |
1110 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) { | |
1111 const int n = 1; | |
1112 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings( | |
1113 tuple<const int&, const char*>(n, "a")), | |
1114 ElementsAre("1", "\"a\"")); | |
1115 } | |
1116 | |
1117 } // namespace gmock_printers_test | |
1118 } // namespace testing | |
OLD | NEW |