blob: e5e3ff12199651834818c1db8ea675326ca5ade2 [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.
56#ifdef GTEST_OS_WINDOWS
57#define GMOCK_HAS_HASH_MAP_ // Indicates that hash_map is available.
58#include <hash_map> // NOLINT
59#define GMOCK_HAS_HASH_SET_ // Indicates that hash_set is available.
60#include <hash_set> // NOLINT
61#endif // GTEST_OS_WINDOWS
62
63// Some user-defined types for testing the universal value printer.
64
65// A user-defined unprintable class template in the global namespace.
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
163#ifdef GTEST_OS_WINDOWS
164// 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.
282#ifndef GTEST_OS_WINDOWS
283 // Windows has no ssize_t type.
284 EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t.
285#endif // GTEST_OS_WINDOWS
286}
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
488// One-dimensional array.
489
490void ArrayHelper1(int (&a)[5]) { // NOLINT
491 EXPECT_EQ("{ 1, 2, 3, 4, 5 }", Print(a));
492}
493
494TEST(PrintArrayTest, OneDimensionalArray) {
495 int a[5] = { 1, 2, 3, 4, 5 };
496 ArrayHelper1(a);
497}
498
499// Two-dimensional array.
500
501void ArrayHelper2(int (&a)[2][5]) { // NOLINT
502 EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", Print(a));
503}
504
505TEST(PrintArrayTest, TwoDimensionalArray) {
506 int a[2][5] = {
507 { 1, 2, 3, 4, 5 },
508 { 6, 7, 8, 9, 0 }
509 };
510 ArrayHelper2(a);
511}
512
513// Array of const elements.
514
515void ArrayHelper3(const bool (&a)[1]) { // NOLINT
516 EXPECT_EQ("{ false }", Print(a));
517}
518
519TEST(PrintArrayTest, ConstArray) {
520 const bool a[1] = { false };
521 ArrayHelper3(a);
522}
523
524// Char array.
525
526void ArrayHelper4(char (&a)[3]) { // NOLINT
527 EXPECT_EQ(PrintPointer(a) + " pointing to \"Hi\"", Print(a));
528}
529
530TEST(PrintArrayTest, CharArray) {
531 char a[3] = "Hi";
532 ArrayHelper4(a);
533}
534
535// Const char array.
536
537void ArrayHelper5(const char (&a)[3]) { // NOLINT
538 EXPECT_EQ(Print(a), PrintPointer(a) + " pointing to \"Hi\"");
539}
540
541TEST(PrintArrayTest, ConstCharArray) {
542 const char a[3] = "Hi";
543 ArrayHelper5(a);
544}
545
546// Array of objects.
547TEST(PrintArrayTest, ObjectArray) {
548 string a[3] = { "Hi", "Hello", "Ni hao" };
549 EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", Print(a));
550}
551
552// Array with many elements.
553TEST(PrintArrayTest, BigArray) {
554 int a[100] = { 1, 2, 3 };
555 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
556 Print(a));
557}
558
559// Tests printing ::string and ::std::string.
560
561#if GTEST_HAS_GLOBAL_STRING
562// ::string.
563TEST(PrintStringTest, StringInGlobalNamespace) {
564 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
565 const ::string str(s, sizeof(s));
566 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
567 Print(str));
568}
569#endif // GTEST_HAS_GLOBAL_STRING
570
571#if GTEST_HAS_STD_STRING
572// ::std::string.
573TEST(PrintStringTest, StringInStdNamespace) {
574 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
575 const ::std::string str(s, sizeof(s));
576 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
577 Print(str));
578}
579#endif // GTEST_HAS_STD_STRING
580
581// Tests printing ::wstring and ::std::wstring.
582
583#if GTEST_HAS_GLOBAL_WSTRING
584// ::wstring.
585TEST(PrintWideStringTest, StringInGlobalNamespace) {
586 const wchar_t s[] = L"'\"\?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
587 const ::wstring str(s, sizeof(s)/sizeof(wchar_t));
588 EXPECT_EQ("L\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
589 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
590 Print(str));
591}
592#endif // GTEST_HAS_GLOBAL_WSTRING
593
594#if GTEST_HAS_STD_WSTRING
595// ::std::wstring.
596TEST(PrintWideStringTest, StringInStdNamespace) {
597 const wchar_t s[] = L"'\"\?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
598 const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t));
599 EXPECT_EQ("L\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
600 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
601 Print(str));
602}
603#endif // GTEST_HAS_STD_WSTRING
604
zhanyong.wan2f0849f2009-02-11 18:06:37 +0000605// Tests printing types that support generic streaming (i.e. streaming
606// to std::basic_ostream<Char, CharTraits> for any valid Char and
607// CharTraits types).
608
609// Tests printing a non-template type that supports generic streaming.
610
611class AllowsGenericStreaming {};
612
613template <typename Char, typename CharTraits>
614std::basic_ostream<Char, CharTraits>& operator<<(
615 std::basic_ostream<Char, CharTraits>& os,
616 const AllowsGenericStreaming& a) {
617 return os << "AllowsGenericStreaming";
618}
619
620TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
621 AllowsGenericStreaming a;
622 EXPECT_EQ("AllowsGenericStreaming", Print(a));
623}
624
625// Tests printing a template type that supports generic streaming.
626
627template <typename T>
628class AllowsGenericStreamingTemplate {};
629
630template <typename Char, typename CharTraits, typename T>
631std::basic_ostream<Char, CharTraits>& operator<<(
632 std::basic_ostream<Char, CharTraits>& os,
633 const AllowsGenericStreamingTemplate<T>& a) {
634 return os << "AllowsGenericStreamingTemplate";
635}
636
637TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
638 AllowsGenericStreamingTemplate<int> a;
639 EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a));
640}
641
642// Tests printing a type that supports generic streaming and can be
643// implicitly converted to another printable type.
644
645template <typename T>
646class AllowsGenericStreamingAndImplicitConversionTemplate {
647 public:
648 operator bool() const { return false; }
649};
650
651template <typename Char, typename CharTraits, typename T>
652std::basic_ostream<Char, CharTraits>& operator<<(
653 std::basic_ostream<Char, CharTraits>& os,
654 const AllowsGenericStreamingAndImplicitConversionTemplate<T>& a) {
655 return os << "AllowsGenericStreamingAndImplicitConversionTemplate";
656}
657
658TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
659 AllowsGenericStreamingAndImplicitConversionTemplate<int> a;
660 EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
661}
662
shiqiane35fdd92008-12-10 05:08:54 +0000663// Tests printing STL containers.
664
665TEST(PrintStlContainerTest, EmptyDeque) {
666 deque<char> empty;
667 EXPECT_EQ("{}", Print(empty));
668}
669
670TEST(PrintStlContainerTest, NonEmptyDeque) {
671 deque<int> non_empty;
672 non_empty.push_back(1);
673 non_empty.push_back(3);
674 EXPECT_EQ("{ 1, 3 }", Print(non_empty));
675}
676
677#ifdef GMOCK_HAS_HASH_MAP_
678
679TEST(PrintStlContainerTest, OneElementHashMap) {
680 hash_map<int, char> map1;
681 map1[1] = 'a';
682 EXPECT_EQ("{ (1, 'a' (97)) }", Print(map1));
683}
684
685TEST(PrintStlContainerTest, HashMultiMap) {
686 hash_multimap<int, bool> map1;
687 map1.insert(make_pair(5, true));
688 map1.insert(make_pair(5, false));
689
690 // Elements of hash_multimap can be printed in any order.
691 const string result = Print(map1);
692 EXPECT_TRUE(result == "{ (5, true), (5, false) }" ||
693 result == "{ (5, false), (5, true) }")
694 << " where Print(map1) returns \"" << result << "\".";
695}
696
697#endif // GMOCK_HAS_HASH_MAP_
698
699#ifdef GMOCK_HAS_HASH_SET_
700
701TEST(PrintStlContainerTest, HashSet) {
702 hash_set<string> set1;
703 set1.insert("hello");
704 EXPECT_EQ("{ \"hello\" }", Print(set1));
705}
706
707TEST(PrintStlContainerTest, HashMultiSet) {
708 const int kSize = 5;
709 int a[kSize] = { 1, 1, 2, 5, 1 };
710 hash_multiset<int> set1(a, a + kSize);
711
712 // Elements of hash_multiset can be printed in any order.
713 const string result = Print(set1);
714 const string expected_pattern = "{ d, d, d, d, d }"; // d means a digit.
715
716 // Verifies the result matches the expected pattern; also extracts
717 // the numbers in the result.
718 ASSERT_EQ(expected_pattern.length(), result.length());
719 std::vector<int> numbers;
720 for (size_t i = 0; i != result.length(); i++) {
721 if (expected_pattern[i] == 'd') {
722 ASSERT_TRUE(isdigit(result[i]));
723 numbers.push_back(result[i] - '0');
724 } else {
725 EXPECT_EQ(expected_pattern[i], result[i]) << " where result is "
726 << result;
727 }
728 }
729
730 // Makes sure the result contains the right numbers.
731 std::sort(numbers.begin(), numbers.end());
732 std::sort(a, a + kSize);
733 EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
734}
735
736#endif // GMOCK_HAS_HASH_SET_
737
738TEST(PrintStlContainerTest, List) {
739 const char* a[] = {
740 "hello",
741 "world"
742 };
743 const list<string> strings(a, a + 2);
744 EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings));
745}
746
747TEST(PrintStlContainerTest, Map) {
748 map<int, bool> map1;
749 map1[1] = true;
750 map1[5] = false;
751 map1[3] = true;
752 EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1));
753}
754
755TEST(PrintStlContainerTest, MultiMap) {
756 multimap<bool, int> map1;
757 map1.insert(make_pair(true, 0));
758 map1.insert(make_pair(true, 1));
759 map1.insert(make_pair(false, 2));
760 EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1));
761}
762
763TEST(PrintStlContainerTest, Set) {
764 const unsigned int a[] = { 3, 0, 5 };
765 set<unsigned int> set1(a, a + 3);
766 EXPECT_EQ("{ 0, 3, 5 }", Print(set1));
767}
768
769TEST(PrintStlContainerTest, MultiSet) {
770 const int a[] = { 1, 1, 2, 5, 1 };
771 multiset<int> set1(a, a + 5);
772 EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1));
773}
774
775TEST(PrintStlContainerTest, Pair) {
776 pair<const bool, int> p(true, 5);
777 EXPECT_EQ("(true, 5)", Print(p));
778}
779
780TEST(PrintStlContainerTest, Vector) {
781 vector<int> v;
782 v.push_back(1);
783 v.push_back(2);
784 EXPECT_EQ("{ 1, 2 }", Print(v));
785}
786
787TEST(PrintStlContainerTest, LongSequence) {
788 const int a[100] = { 1, 2, 3 };
789 const vector<int> v(a, a + 100);
790 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
791 "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v));
792}
793
794TEST(PrintStlContainerTest, NestedContainer) {
795 const int a1[] = { 1, 2 };
796 const int a2[] = { 3, 4, 5 };
797 const list<int> l1(a1, a1 + 2);
798 const list<int> l2(a2, a2 + 3);
799
800 vector<list<int> > v;
801 v.push_back(l1);
802 v.push_back(l2);
803 EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v));
804}
805
806
807// Tests printing tuples.
808
809// Tuples of various arities.
810TEST(PrintTupleTest, VariousSizes) {
811 tuple<> t0;
812 EXPECT_EQ("()", Print(t0));
813
814 tuple<int> t1(5);
815 EXPECT_EQ("(5)", Print(t1));
816
817 tuple<char, bool> t2('a', true);
818 EXPECT_EQ("('a' (97), true)", Print(t2));
819
shiqianc97f2f52008-12-11 17:22:59 +0000820 tuple<bool, int, int> t3(false, 2, 3);
821 EXPECT_EQ("(false, 2, 3)", Print(t3));
822
823 tuple<bool, int, int, int> t4(false, 2, 3, 4);
824 EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
825
826 tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
827 EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
828
829 tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
830 EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
831
832 tuple<bool, int, int, int, bool, int, int> t7(false, 2, 3, 4, true, 6, 7);
833 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
834
835 tuple<bool, int, int, int, bool, int, int, bool> t8(
836 false, 2, 3, 4, true, 6, 7, true);
837 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
838
839 tuple<bool, int, int, int, bool, int, int, bool, int> t9(
840 false, 2, 3, 4, true, 6, 7, true, 9);
841 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
842
shiqiane35fdd92008-12-10 05:08:54 +0000843 const char* const str = "8";
844 tuple<bool, char, short, testing::internal::Int32, // NOLINT
845 testing::internal::Int64, float, double, const char*, void*, string>
846 t10(false, 'a', 3, 4, 5, 6.5F, 7.5, str, NULL, "10");
847 EXPECT_EQ("(false, 'a' (97), 3, 4, 5, 6.5, 7.5, " + PrintPointer(str) +
848 " pointing to \"8\", NULL, \"10\")",
849 Print(t10));
850}
851
852// Nested tuples.
853TEST(PrintTupleTest, NestedTuple) {
854 tuple<tuple<int, double>, char> nested(make_tuple(5, 9.5), 'a');
855 EXPECT_EQ("((5, 9.5), 'a' (97))", Print(nested));
856}
857
858// Tests printing user-defined unprintable types.
859
860// Unprintable types in the global namespace.
861TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
862 EXPECT_EQ("1-byte object <00>",
863 Print(UnprintableTemplateInGlobal<bool>()));
864}
865
866// Unprintable types in a user namespace.
867TEST(PrintUnprintableTypeTest, InUserNamespace) {
868 EXPECT_EQ("16-byte object <EF12 0000 34AB 0000 0000 0000 0000 0000>",
869 Print(::foo::UnprintableInFoo()));
870}
871
872// Unprintable types are that too big to be printed completely.
873
874struct Big {
875 Big() { memset(array, 0, sizeof(array)); }
876 char array[257];
877};
878
879TEST(PrintUnpritableTypeTest, BigObject) {
880 EXPECT_EQ("257-byte object <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 0000 0000 "
883 "0000 0000 0000 0000 0000 0000 ... 0000 0000 0000 "
884 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 "
885 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 "
886 "0000 0000 0000 0000 0000 0000 0000 0000 00>",
887 Print(Big()));
888}
889
890// Tests printing user-defined streamable types.
891
892// Streamable types in the global namespace.
893TEST(PrintStreamableTypeTest, InGlobalNamespace) {
894 EXPECT_EQ("StreamableInGlobal",
895 Print(StreamableInGlobal()));
896}
897
898// Printable template types in a user namespace.
899TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
900 EXPECT_EQ("StreamableTemplateInFoo: 0",
901 Print(::foo::StreamableTemplateInFoo<int>()));
902}
903
904// Tests printing user-defined types that have a PrintTo() function.
905TEST(PrintPrintableTypeTest, InUserNamespace) {
906 EXPECT_EQ("PrintableViaPrintTo: 0",
907 Print(::foo::PrintableViaPrintTo()));
908}
909
910// Tests printing user-defined class template that have a PrintTo() function.
911TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
912 EXPECT_EQ("PrintableViaPrintToTemplate: 5",
913 Print(::foo::PrintableViaPrintToTemplate<int>(5)));
914}
915
916#if GMOCK_HAS_PROTOBUF_
917
918// Tests printing a protocol message.
919TEST(PrintProtocolMessageTest, PrintsShortDebugString) {
920 testing::internal::TestMessage msg;
921 msg.set_member("yes");
922 EXPECT_EQ("<member:\"yes\">", Print(msg));
923}
924
925// Tests printing a proto2 message.
926TEST(PrintProto2MessageTest, PrintsShortDebugString) {
927 testing::internal::FooMessage msg;
928 msg.set_int_field(2);
929 EXPECT_PRED2(RE::FullMatch, Print(msg),
930 "<int_field:\\s*2\\s*>");
931}
932
933#endif // GMOCK_HAS_PROTOBUF_
934
935// Tests that the universal printer prints both the address and the
936// value of a reference.
937TEST(PrintReferenceTest, PrintsAddressAndValue) {
938 int n = 5;
939 EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
940
941 int a[2][3] = {
942 { 0, 1, 2 },
943 { 3, 4, 5 }
944 };
945 EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
946 PrintByRef(a));
947
948 const ::foo::UnprintableInFoo x;
949 EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object "
950 "<EF12 0000 34AB 0000 0000 0000 0000 0000>",
951 PrintByRef(x));
952}
953
954// Tests that the universal printer prints a function pointer passed by
955// reference.
956TEST(PrintReferenceTest, HandlesFunctionPointer) {
957 void (*fp)(int n) = &MyFunction;
958 const string fp_pointer_string =
959 PrintPointer(reinterpret_cast<const void*>(&fp));
960 const string fp_string = PrintPointer(reinterpret_cast<const void*>(fp));
961 EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
962 PrintByRef(fp));
963}
964
965// Tests that the universal printer prints a member function pointer
966// passed by reference.
967TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
968 int (Foo::*p)(char ch) = &Foo::MyMethod;
969 EXPECT_THAT(PrintByRef(p),
970 StartsWith("@" + PrintPointer(reinterpret_cast<const void*>(&p))
971 + " " + Print(sizeof(p)) + "-byte object "));
972
973 char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
974 EXPECT_THAT(PrintByRef(p2),
975 StartsWith("@" + PrintPointer(reinterpret_cast<const void*>(&p2))
976 + " " + Print(sizeof(p2)) + "-byte object "));
977}
978
979// Tests that the universal printer prints a member variable pointer
980// passed by reference.
981TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
982 int (Foo::*p) = &Foo::value; // NOLINT
983 EXPECT_THAT(PrintByRef(p),
984 StartsWith("@" + PrintPointer(&p)
985 + " " + Print(sizeof(p)) + "-byte object "));
986}
987
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000988TEST(PrintToStringTest, WorksForNonReference) {
989 EXPECT_EQ("123", UniversalPrinter<int>::PrintToString(123));
zhanyong.wance198ff2009-02-12 01:34:27 +0000990}
991
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000992TEST(PrintToStringTest, WorksForReference) {
zhanyong.wance198ff2009-02-12 01:34:27 +0000993 int n = 123;
994 EXPECT_EQ("@" + PrintPointer(&n) + " 123",
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000995 UniversalPrinter<const int&>::PrintToString(n));
zhanyong.wance198ff2009-02-12 01:34:27 +0000996}
997
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000998TEST(UniversalTersePrintTest, WorksForNonReference) {
zhanyong.wance198ff2009-02-12 01:34:27 +0000999 ::std::stringstream ss;
zhanyong.wan4a5330d2009-02-19 00:36:44 +00001000 UniversalTersePrint(123, &ss);
zhanyong.wance198ff2009-02-12 01:34:27 +00001001 EXPECT_EQ("123", ss.str());
1002}
1003
zhanyong.wan4a5330d2009-02-19 00:36:44 +00001004TEST(UniversalTersePrintTest, WorksForReference) {
zhanyong.wance198ff2009-02-12 01:34:27 +00001005 const int& n = 123;
1006 ::std::stringstream ss;
zhanyong.wan4a5330d2009-02-19 00:36:44 +00001007 UniversalTersePrint(n, &ss);
zhanyong.wance198ff2009-02-12 01:34:27 +00001008 EXPECT_EQ("123", ss.str());
1009}
1010
zhanyong.wan4a5330d2009-02-19 00:36:44 +00001011TEST(UniversalTersePrintTest, WorksForCString) {
1012 const char* s1 = "abc";
1013 ::std::stringstream ss1;
1014 UniversalTersePrint(s1, &ss1);
1015 EXPECT_EQ("\"abc\"", ss1.str());
1016
1017 char* s2 = const_cast<char*>(s1);
1018 ::std::stringstream ss2;
1019 UniversalTersePrint(s2, &ss2);
1020 EXPECT_EQ("\"abc\"", ss2.str());
1021
1022 const char* s3 = NULL;
1023 ::std::stringstream ss3;
1024 UniversalTersePrint(s3, &ss3);
1025 EXPECT_EQ("NULL", ss3.str());
1026}
1027
1028TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) {
1029 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple()),
1030 ElementsAre());
1031}
1032
1033TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) {
1034 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple(1)),
1035 ElementsAre("1"));
1036}
1037
1038TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) {
1039 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a')),
1040 ElementsAre("1", "'a' (97)"));
1041}
1042
1043TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) {
1044 const int n = 1;
1045 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(
1046 tuple<const int&, const char*>(n, "a")),
1047 ElementsAre("1", "\"a\""));
1048}
1049
shiqiane35fdd92008-12-10 05:08:54 +00001050} // namespace gmock_printers_test
1051} // namespace testing