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