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