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