Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(332)

Side by Side Diff: testing/gmock/test/gmock-printers_test.cc

Issue 521012: Update gmock and gtest. (Closed)
Patch Set: update readme Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « testing/gmock/test/gmock-port_test.cc ('k') | testing/gmock/test/gmock-spec-builders_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2007, Google Inc. 1 // Copyright 2007, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 private: 70 private:
71 T value_; 71 T value_;
72 }; 72 };
73 73
74 // A user-defined streamable type in the global namespace. 74 // A user-defined streamable type in the global namespace.
75 class StreamableInGlobal { 75 class StreamableInGlobal {
76 public: 76 public:
77 virtual ~StreamableInGlobal() {} 77 virtual ~StreamableInGlobal() {}
78 }; 78 };
79 79
80 inline void operator<<(::std::ostream& os, const StreamableInGlobal& x) { 80 inline void operator<<(::std::ostream& os, const StreamableInGlobal& /* x */) {
81 os << "StreamableInGlobal"; 81 os << "StreamableInGlobal";
82 } 82 }
83 83
84 namespace foo { 84 namespace foo {
85 85
86 // A user-defined unprintable type in a user namespace. 86 // A user-defined unprintable type in a user namespace.
87 class UnprintableInFoo { 87 class UnprintableInFoo {
88 public: 88 public:
89 UnprintableInFoo() : x_(0x12EF), y_(0xAB34), z_(0) {} 89 UnprintableInFoo() : x_(0x12EF), y_(0xAB34), z_(0) {}
90 private: 90 private:
91 testing::internal::Int32 x_; 91 testing::internal::Int32 x_;
92 testing::internal::Int32 y_; 92 testing::internal::Int32 y_;
93 double z_; 93 double z_;
94 }; 94 };
95 95
96 // A user-defined printable type in a user-chosen namespace. 96 // A user-defined printable type in a user-chosen namespace.
97 struct PrintableViaPrintTo { 97 struct PrintableViaPrintTo {
98 PrintableViaPrintTo() : value() {} 98 PrintableViaPrintTo() : value() {}
99 int value; 99 int value;
100 }; 100 };
101 101
102 void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) { 102 void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) {
103 *os << "PrintableViaPrintTo: " << x.value; 103 *os << "PrintableViaPrintTo: " << x.value;
104 } 104 }
105 105
106 // A user-defined printable class template in a user-chosen namespace. 106 // A user-defined printable class template in a user-chosen namespace.
107 template <typename T> 107 template <typename T>
108 class PrintableViaPrintToTemplate { 108 class PrintableViaPrintToTemplate {
109 public: 109 public:
110 explicit PrintableViaPrintToTemplate(const T& value) : value_(value) {} 110 explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {}
111 111
112 const T& value() const { return value_; } 112 const T& value() const { return value_; }
113 private: 113 private:
114 T value_; 114 T value_;
115 }; 115 };
116 116
117 template <typename T> 117 template <typename T>
118 void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) { 118 void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) {
119 *os << "PrintableViaPrintToTemplate: " << x.value(); 119 *os << "PrintableViaPrintToTemplate: " << x.value();
120 } 120 }
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 // Tests printing pointers to pointers. 433 // Tests printing pointers to pointers.
434 TEST(PrintPointerToPointerTest, IntPointerPointer) { 434 TEST(PrintPointerToPointerTest, IntPointerPointer) {
435 int** p = reinterpret_cast<int**>(0xABCD); 435 int** p = reinterpret_cast<int**>(0xABCD);
436 EXPECT_EQ(PrintPointer(p), Print(p)); 436 EXPECT_EQ(PrintPointer(p), Print(p));
437 p = NULL; 437 p = NULL;
438 EXPECT_EQ("NULL", Print(p)); 438 EXPECT_EQ("NULL", Print(p));
439 } 439 }
440 440
441 // Tests printing (non-member) function pointers. 441 // Tests printing (non-member) function pointers.
442 442
443 void MyFunction(int n) {} 443 void MyFunction(int /* n */) {}
444 444
445 TEST(PrintPointerTest, NonMemberFunctionPointer) { 445 TEST(PrintPointerTest, NonMemberFunctionPointer) {
446 EXPECT_EQ(PrintPointer(reinterpret_cast<const void*>(&MyFunction)), 446 // We cannot directly cast &MyFunction to const void* because the
447 Print(&MyFunction)); 447 // standard disallows casting between pointers to functions and
448 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
449 // this limitation.
450 EXPECT_EQ(
451 PrintPointer(reinterpret_cast<const void*>(
452 reinterpret_cast<internal::BiggestInt>(&MyFunction))),
453 Print(&MyFunction));
448 int (*p)(bool) = NULL; // NOLINT 454 int (*p)(bool) = NULL; // NOLINT
449 EXPECT_EQ("NULL", Print(p)); 455 EXPECT_EQ("NULL", Print(p));
450 } 456 }
451 457
452 // Tests printing member variable pointers. Although they are called 458 // Tests printing member variable pointers. Although they are called
453 // pointers, they don't point to a location in the address space. 459 // pointers, they don't point to a location in the address space.
454 // Their representation is implementation-defined. Thus they will be 460 // Their representation is implementation-defined. Thus they will be
455 // printed as raw bytes. 461 // printed as raw bytes.
456 462
457 struct Foo { 463 struct Foo {
458 public: 464 public:
459 virtual ~Foo() {} 465 virtual ~Foo() {}
460 int MyMethod(char x) { return x + 1; } 466 int MyMethod(char x) { return x + 1; }
461 virtual char MyVirtualMethod(int n) { return 'a'; } 467 virtual char MyVirtualMethod(int /* n */) { return 'a'; }
462 468
463 int value; 469 int value;
464 }; 470 };
465 471
466 TEST(PrintPointerTest, MemberVariablePointer) { 472 TEST(PrintPointerTest, MemberVariablePointer) {
467 EXPECT_THAT(Print(&Foo::value), 473 EXPECT_THAT(Print(&Foo::value),
468 StartsWith(Print(sizeof(&Foo::value)) + "-byte object ")); 474 StartsWith(Print(sizeof(&Foo::value)) + "-byte object "));
469 int (Foo::*p) = NULL; // NOLINT 475 int (Foo::*p) = NULL; // NOLINT
470 EXPECT_THAT(Print(p), 476 EXPECT_THAT(Print(p),
471 StartsWith(Print(sizeof(p)) + "-byte object ")); 477 StartsWith(Print(sizeof(p)) + "-byte object "));
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 #if GTEST_HAS_GLOBAL_STRING 553 #if GTEST_HAS_GLOBAL_STRING
548 // ::string. 554 // ::string.
549 TEST(PrintStringTest, StringInGlobalNamespace) { 555 TEST(PrintStringTest, StringInGlobalNamespace) {
550 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; 556 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
551 const ::string str(s, sizeof(s)); 557 const ::string str(s, sizeof(s));
552 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", 558 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
553 Print(str)); 559 Print(str));
554 } 560 }
555 #endif // GTEST_HAS_GLOBAL_STRING 561 #endif // GTEST_HAS_GLOBAL_STRING
556 562
557 #if GTEST_HAS_STD_STRING
558 // ::std::string. 563 // ::std::string.
559 TEST(PrintStringTest, StringInStdNamespace) { 564 TEST(PrintStringTest, StringInStdNamespace) {
560 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; 565 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
561 const ::std::string str(s, sizeof(s)); 566 const ::std::string str(s, sizeof(s));
562 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", 567 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
563 Print(str)); 568 Print(str));
564 } 569 }
565 #endif // GTEST_HAS_STD_STRING
566 570
567 // Tests printing ::wstring and ::std::wstring. 571 // Tests printing ::wstring and ::std::wstring.
568 572
569 #if GTEST_HAS_GLOBAL_WSTRING 573 #if GTEST_HAS_GLOBAL_WSTRING
570 // ::wstring. 574 // ::wstring.
571 TEST(PrintWideStringTest, StringInGlobalNamespace) { 575 TEST(PrintWideStringTest, StringInGlobalNamespace) {
572 const wchar_t s[] = L"'\"\?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; 576 const wchar_t s[] = L"'\"\?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
573 const ::wstring str(s, sizeof(s)/sizeof(wchar_t)); 577 const ::wstring str(s, sizeof(s)/sizeof(wchar_t));
574 EXPECT_EQ("L\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" 578 EXPECT_EQ("L\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
575 "\\xD3\\x576\\x8D3\\xC74D a\\0\"", 579 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
(...skipping 16 matching lines...) Expand all
592 // to std::basic_ostream<Char, CharTraits> for any valid Char and 596 // to std::basic_ostream<Char, CharTraits> for any valid Char and
593 // CharTraits types). 597 // CharTraits types).
594 598
595 // Tests printing a non-template type that supports generic streaming. 599 // Tests printing a non-template type that supports generic streaming.
596 600
597 class AllowsGenericStreaming {}; 601 class AllowsGenericStreaming {};
598 602
599 template <typename Char, typename CharTraits> 603 template <typename Char, typename CharTraits>
600 std::basic_ostream<Char, CharTraits>& operator<<( 604 std::basic_ostream<Char, CharTraits>& operator<<(
601 std::basic_ostream<Char, CharTraits>& os, 605 std::basic_ostream<Char, CharTraits>& os,
602 const AllowsGenericStreaming& a) { 606 const AllowsGenericStreaming& /* a */) {
603 return os << "AllowsGenericStreaming"; 607 return os << "AllowsGenericStreaming";
604 } 608 }
605 609
606 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) { 610 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
607 AllowsGenericStreaming a; 611 AllowsGenericStreaming a;
608 EXPECT_EQ("AllowsGenericStreaming", Print(a)); 612 EXPECT_EQ("AllowsGenericStreaming", Print(a));
609 } 613 }
610 614
611 // Tests printing a template type that supports generic streaming. 615 // Tests printing a template type that supports generic streaming.
612 616
613 template <typename T> 617 template <typename T>
614 class AllowsGenericStreamingTemplate {}; 618 class AllowsGenericStreamingTemplate {};
615 619
616 template <typename Char, typename CharTraits, typename T> 620 template <typename Char, typename CharTraits, typename T>
617 std::basic_ostream<Char, CharTraits>& operator<<( 621 std::basic_ostream<Char, CharTraits>& operator<<(
618 std::basic_ostream<Char, CharTraits>& os, 622 std::basic_ostream<Char, CharTraits>& os,
619 const AllowsGenericStreamingTemplate<T>& a) { 623 const AllowsGenericStreamingTemplate<T>& /* a */) {
620 return os << "AllowsGenericStreamingTemplate"; 624 return os << "AllowsGenericStreamingTemplate";
621 } 625 }
622 626
623 TEST(PrintTypeWithGenericStreamingTest, TemplateType) { 627 TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
624 AllowsGenericStreamingTemplate<int> a; 628 AllowsGenericStreamingTemplate<int> a;
625 EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a)); 629 EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a));
626 } 630 }
627 631
628 // Tests printing a type that supports generic streaming and can be 632 // Tests printing a type that supports generic streaming and can be
629 // implicitly converted to another printable type. 633 // implicitly converted to another printable type.
630 634
631 template <typename T> 635 template <typename T>
632 class AllowsGenericStreamingAndImplicitConversionTemplate { 636 class AllowsGenericStreamingAndImplicitConversionTemplate {
633 public: 637 public:
634 operator bool() const { return false; } 638 operator bool() const { return false; }
635 }; 639 };
636 640
637 template <typename Char, typename CharTraits, typename T> 641 template <typename Char, typename CharTraits, typename T>
638 std::basic_ostream<Char, CharTraits>& operator<<( 642 std::basic_ostream<Char, CharTraits>& operator<<(
639 std::basic_ostream<Char, CharTraits>& os, 643 std::basic_ostream<Char, CharTraits>& os,
640 const AllowsGenericStreamingAndImplicitConversionTemplate<T>& a) { 644 const AllowsGenericStreamingAndImplicitConversionTemplate<T>& /* a */) {
641 return os << "AllowsGenericStreamingAndImplicitConversionTemplate"; 645 return os << "AllowsGenericStreamingAndImplicitConversionTemplate";
642 } 646 }
643 647
644 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) { 648 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
645 AllowsGenericStreamingAndImplicitConversionTemplate<int> a; 649 AllowsGenericStreamingAndImplicitConversionTemplate<int> a;
646 EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a)); 650 EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
647 } 651 }
648 652
649 // Tests printing STL containers. 653 // Tests printing STL containers.
650 654
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
966 "<EF12 0000 34AB 0000 0000 0000 0000 0000>", 970 "<EF12 0000 34AB 0000 0000 0000 0000 0000>",
967 PrintByRef(x)); 971 PrintByRef(x));
968 } 972 }
969 973
970 // Tests that the universal printer prints a function pointer passed by 974 // Tests that the universal printer prints a function pointer passed by
971 // reference. 975 // reference.
972 TEST(PrintReferenceTest, HandlesFunctionPointer) { 976 TEST(PrintReferenceTest, HandlesFunctionPointer) {
973 void (*fp)(int n) = &MyFunction; 977 void (*fp)(int n) = &MyFunction;
974 const string fp_pointer_string = 978 const string fp_pointer_string =
975 PrintPointer(reinterpret_cast<const void*>(&fp)); 979 PrintPointer(reinterpret_cast<const void*>(&fp));
976 const string fp_string = PrintPointer(reinterpret_cast<const void*>(fp)); 980 // We cannot directly cast &MyFunction to const void* because the
981 // standard disallows casting between pointers to functions and
982 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
983 // this limitation.
984 const string fp_string = PrintPointer(reinterpret_cast<const void*>(
985 reinterpret_cast<internal::BiggestInt>(fp)));
977 EXPECT_EQ("@" + fp_pointer_string + " " + fp_string, 986 EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
978 PrintByRef(fp)); 987 PrintByRef(fp));
979 } 988 }
980 989
981 // Tests that the universal printer prints a member function pointer 990 // Tests that the universal printer prints a member function pointer
982 // passed by reference. 991 // passed by reference.
983 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) { 992 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
984 int (Foo::*p)(char ch) = &Foo::MyMethod; 993 int (Foo::*p)(char ch) = &Foo::MyMethod;
985 EXPECT_THAT(PrintByRef(p), 994 EXPECT_THAT(PrintByRef(p),
986 StartsWith("@" + PrintPointer(reinterpret_cast<const void*>(&p)) 995 StartsWith("@" + PrintPointer(reinterpret_cast<const void*>(&p))
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
1094 1103
1095 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) { 1104 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) {
1096 const int n = 1; 1105 const int n = 1;
1097 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings( 1106 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(
1098 tuple<const int&, const char*>(n, "a")), 1107 tuple<const int&, const char*>(n, "a")),
1099 ElementsAre("1", "\"a\"")); 1108 ElementsAre("1", "\"a\""));
1100 } 1109 }
1101 1110
1102 } // namespace gmock_printers_test 1111 } // namespace gmock_printers_test
1103 } // namespace testing 1112 } // namespace testing
OLDNEW
« no previous file with comments | « testing/gmock/test/gmock-port_test.cc ('k') | testing/gmock/test/gmock-spec-builders_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698