| OLD | NEW |
| 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 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 using ::std::map; | 147 using ::std::map; |
| 148 using ::std::multimap; | 148 using ::std::multimap; |
| 149 using ::std::multiset; | 149 using ::std::multiset; |
| 150 using ::std::pair; | 150 using ::std::pair; |
| 151 using ::std::set; | 151 using ::std::set; |
| 152 using ::std::tr1::make_tuple; | 152 using ::std::tr1::make_tuple; |
| 153 using ::std::tr1::tuple; | 153 using ::std::tr1::tuple; |
| 154 using ::std::vector; | 154 using ::std::vector; |
| 155 using ::testing::ElementsAre; | 155 using ::testing::ElementsAre; |
| 156 using ::testing::StartsWith; | 156 using ::testing::StartsWith; |
| 157 using ::testing::internal::NativeArray; |
| 157 using ::testing::internal::Strings; | 158 using ::testing::internal::Strings; |
| 158 using ::testing::internal::UniversalTersePrint; | 159 using ::testing::internal::UniversalTersePrint; |
| 160 using ::testing::internal::UniversalPrint; |
| 159 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings; | 161 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings; |
| 160 using ::testing::internal::UniversalPrinter; | 162 using ::testing::internal::UniversalPrinter; |
| 163 using ::testing::internal::kReference; |
| 161 using ::testing::internal::string; | 164 using ::testing::internal::string; |
| 162 | 165 |
| 163 #if GTEST_OS_WINDOWS | 166 #if GTEST_OS_WINDOWS |
| 164 // MSVC defines the following classes in the ::stdext namespace while | 167 // MSVC defines the following classes in the ::stdext namespace while |
| 165 // gcc defines them in the :: namespace. Note that they are not part | 168 // gcc defines them in the :: namespace. Note that they are not part |
| 166 // of the C++ standard. | 169 // of the C++ standard. |
| 167 | 170 |
| 168 using ::stdext::hash_map; | 171 using ::stdext::hash_map; |
| 169 using ::stdext::hash_set; | 172 using ::stdext::hash_set; |
| 170 using ::stdext::hash_multimap; | 173 using ::stdext::hash_multimap; |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 EXPECT_THAT(Print(&Foo::MyVirtualMethod), | 481 EXPECT_THAT(Print(&Foo::MyVirtualMethod), |
| 479 StartsWith(Print(sizeof((&Foo::MyVirtualMethod))) | 482 StartsWith(Print(sizeof((&Foo::MyVirtualMethod))) |
| 480 + "-byte object ")); | 483 + "-byte object ")); |
| 481 int (Foo::*p)(char) = NULL; // NOLINT | 484 int (Foo::*p)(char) = NULL; // NOLINT |
| 482 EXPECT_THAT(Print(p), | 485 EXPECT_THAT(Print(p), |
| 483 StartsWith(Print(sizeof(p)) + "-byte object ")); | 486 StartsWith(Print(sizeof(p)) + "-byte object ")); |
| 484 } | 487 } |
| 485 | 488 |
| 486 // Tests printing C arrays. | 489 // Tests printing C arrays. |
| 487 | 490 |
| 488 // One-dimensional array. | 491 // The difference between this and Print() is that it ensures that the |
| 489 | 492 // argument is a reference to an array. |
| 490 void ArrayHelper1(int (&a)[5]) { // NOLINT | 493 template <typename T, size_t N> |
| 491 EXPECT_EQ("{ 1, 2, 3, 4, 5 }", Print(a)); | 494 string PrintArrayHelper(T (&a)[N]) { |
| 495 return Print(a); |
| 492 } | 496 } |
| 493 | 497 |
| 498 // One-dimensional array. |
| 494 TEST(PrintArrayTest, OneDimensionalArray) { | 499 TEST(PrintArrayTest, OneDimensionalArray) { |
| 495 int a[5] = { 1, 2, 3, 4, 5 }; | 500 int a[5] = { 1, 2, 3, 4, 5 }; |
| 496 ArrayHelper1(a); | 501 EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a)); |
| 497 } | 502 } |
| 498 | 503 |
| 499 // Two-dimensional array. | 504 // Two-dimensional array. |
| 500 | |
| 501 void ArrayHelper2(int (&a)[2][5]) { // NOLINT | |
| 502 EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", Print(a)); | |
| 503 } | |
| 504 | |
| 505 TEST(PrintArrayTest, TwoDimensionalArray) { | 505 TEST(PrintArrayTest, TwoDimensionalArray) { |
| 506 int a[2][5] = { | 506 int a[2][5] = { |
| 507 { 1, 2, 3, 4, 5 }, | 507 { 1, 2, 3, 4, 5 }, |
| 508 { 6, 7, 8, 9, 0 } | 508 { 6, 7, 8, 9, 0 } |
| 509 }; | 509 }; |
| 510 ArrayHelper2(a); | 510 EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a)); |
| 511 } | 511 } |
| 512 | 512 |
| 513 // Array of const elements. | 513 // Array of const elements. |
| 514 | |
| 515 void ArrayHelper3(const bool (&a)[1]) { // NOLINT | |
| 516 EXPECT_EQ("{ false }", Print(a)); | |
| 517 } | |
| 518 | |
| 519 TEST(PrintArrayTest, ConstArray) { | 514 TEST(PrintArrayTest, ConstArray) { |
| 520 const bool a[1] = { false }; | 515 const bool a[1] = { false }; |
| 521 ArrayHelper3(a); | 516 EXPECT_EQ("{ false }", PrintArrayHelper(a)); |
| 522 } | 517 } |
| 523 | 518 |
| 524 // Char array. | 519 // Char array. |
| 525 | |
| 526 void ArrayHelper4(char (&a)[3]) { // NOLINT | |
| 527 EXPECT_EQ(PrintPointer(a) + " pointing to \"Hi\"", Print(a)); | |
| 528 } | |
| 529 | |
| 530 TEST(PrintArrayTest, CharArray) { | 520 TEST(PrintArrayTest, CharArray) { |
| 531 char a[3] = "Hi"; | 521 // Array a contains '\0' in the middle and doesn't end with '\0'. |
| 532 ArrayHelper4(a); | 522 char a[3] = { 'H', '\0', 'i' }; |
| 523 EXPECT_EQ("\"H\\0i\"", PrintArrayHelper(a)); |
| 533 } | 524 } |
| 534 | 525 |
| 535 // Const char array. | 526 // Const char array. |
| 536 | |
| 537 void ArrayHelper5(const char (&a)[3]) { // NOLINT | |
| 538 EXPECT_EQ(Print(a), PrintPointer(a) + " pointing to \"Hi\""); | |
| 539 } | |
| 540 | |
| 541 TEST(PrintArrayTest, ConstCharArray) { | 527 TEST(PrintArrayTest, ConstCharArray) { |
| 542 const char a[3] = "Hi"; | 528 const char a[4] = "\0Hi"; |
| 543 ArrayHelper5(a); | 529 EXPECT_EQ("\"\\0Hi\\0\"", PrintArrayHelper(a)); |
| 544 } | 530 } |
| 545 | 531 |
| 546 // Array of objects. | 532 // Array of objects. |
| 547 TEST(PrintArrayTest, ObjectArray) { | 533 TEST(PrintArrayTest, ObjectArray) { |
| 548 string a[3] = { "Hi", "Hello", "Ni hao" }; | 534 string a[3] = { "Hi", "Hello", "Ni hao" }; |
| 549 EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", Print(a)); | 535 EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a)); |
| 550 } | 536 } |
| 551 | 537 |
| 552 // Array with many elements. | 538 // Array with many elements. |
| 553 TEST(PrintArrayTest, BigArray) { | 539 TEST(PrintArrayTest, BigArray) { |
| 554 int a[100] = { 1, 2, 3 }; | 540 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 }", | 541 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }", |
| 556 Print(a)); | 542 PrintArrayHelper(a)); |
| 557 } | 543 } |
| 558 | 544 |
| 559 // Tests printing ::string and ::std::string. | 545 // Tests printing ::string and ::std::string. |
| 560 | 546 |
| 561 #if GTEST_HAS_GLOBAL_STRING | 547 #if GTEST_HAS_GLOBAL_STRING |
| 562 // ::string. | 548 // ::string. |
| 563 TEST(PrintStringTest, StringInGlobalNamespace) { | 549 TEST(PrintStringTest, StringInGlobalNamespace) { |
| 564 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; | 550 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; |
| 565 const ::string str(s, sizeof(s)); | 551 const ::string str(s, sizeof(s)); |
| 566 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", | 552 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 796 const int a2[] = { 3, 4, 5 }; | 782 const int a2[] = { 3, 4, 5 }; |
| 797 const list<int> l1(a1, a1 + 2); | 783 const list<int> l1(a1, a1 + 2); |
| 798 const list<int> l2(a2, a2 + 3); | 784 const list<int> l2(a2, a2 + 3); |
| 799 | 785 |
| 800 vector<list<int> > v; | 786 vector<list<int> > v; |
| 801 v.push_back(l1); | 787 v.push_back(l1); |
| 802 v.push_back(l2); | 788 v.push_back(l2); |
| 803 EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v)); | 789 EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v)); |
| 804 } | 790 } |
| 805 | 791 |
| 792 TEST(PrintStlContainerTest, OneDimensionalNativeArray) { |
| 793 const int a[] = { 1, 2, 3 }; |
| 794 NativeArray<int> b(a, kReference); |
| 795 EXPECT_EQ("{ 1, 2, 3 }", Print(b)); |
| 796 } |
| 797 |
| 798 TEST(PrintStlContainerTest, TwoDimensionalNativeArray) { |
| 799 const int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
| 800 NativeArray<int[3]> b(a, kReference); |
| 801 EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b)); |
| 802 } |
| 806 | 803 |
| 807 // Tests printing tuples. | 804 // Tests printing tuples. |
| 808 | 805 |
| 809 // Tuples of various arities. | 806 // Tuples of various arities. |
| 810 TEST(PrintTupleTest, VariousSizes) { | 807 TEST(PrintTupleTest, VariousSizes) { |
| 811 tuple<> t0; | 808 tuple<> t0; |
| 812 EXPECT_EQ("()", Print(t0)); | 809 EXPECT_EQ("()", Print(t0)); |
| 813 | 810 |
| 814 tuple<int> t1(5); | 811 tuple<int> t1(5); |
| 815 EXPECT_EQ("(5)", Print(t1)); | 812 EXPECT_EQ("(5)", Print(t1)); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 988 TEST(PrintToStringTest, WorksForNonReference) { | 985 TEST(PrintToStringTest, WorksForNonReference) { |
| 989 EXPECT_EQ("123", UniversalPrinter<int>::PrintToString(123)); | 986 EXPECT_EQ("123", UniversalPrinter<int>::PrintToString(123)); |
| 990 } | 987 } |
| 991 | 988 |
| 992 TEST(PrintToStringTest, WorksForReference) { | 989 TEST(PrintToStringTest, WorksForReference) { |
| 993 int n = 123; | 990 int n = 123; |
| 994 EXPECT_EQ("@" + PrintPointer(&n) + " 123", | 991 EXPECT_EQ("@" + PrintPointer(&n) + " 123", |
| 995 UniversalPrinter<const int&>::PrintToString(n)); | 992 UniversalPrinter<const int&>::PrintToString(n)); |
| 996 } | 993 } |
| 997 | 994 |
| 995 TEST(PrintToStringTest, WorksForArray) { |
| 996 int n[3] = { 1, 2, 3 }; |
| 997 EXPECT_EQ("{ 1, 2, 3 }", UniversalPrinter<int[3]>::PrintToString(n)); |
| 998 } |
| 999 |
| 998 TEST(UniversalTersePrintTest, WorksForNonReference) { | 1000 TEST(UniversalTersePrintTest, WorksForNonReference) { |
| 999 ::std::stringstream ss; | 1001 ::std::stringstream ss; |
| 1000 UniversalTersePrint(123, &ss); | 1002 UniversalTersePrint(123, &ss); |
| 1001 EXPECT_EQ("123", ss.str()); | 1003 EXPECT_EQ("123", ss.str()); |
| 1002 } | 1004 } |
| 1003 | 1005 |
| 1004 TEST(UniversalTersePrintTest, WorksForReference) { | 1006 TEST(UniversalTersePrintTest, WorksForReference) { |
| 1005 const int& n = 123; | 1007 const int& n = 123; |
| 1006 ::std::stringstream ss; | 1008 ::std::stringstream ss; |
| 1007 UniversalTersePrint(n, &ss); | 1009 UniversalTersePrint(n, &ss); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1018 ::std::stringstream ss2; | 1020 ::std::stringstream ss2; |
| 1019 UniversalTersePrint(s2, &ss2); | 1021 UniversalTersePrint(s2, &ss2); |
| 1020 EXPECT_EQ("\"abc\"", ss2.str()); | 1022 EXPECT_EQ("\"abc\"", ss2.str()); |
| 1021 | 1023 |
| 1022 const char* s3 = NULL; | 1024 const char* s3 = NULL; |
| 1023 ::std::stringstream ss3; | 1025 ::std::stringstream ss3; |
| 1024 UniversalTersePrint(s3, &ss3); | 1026 UniversalTersePrint(s3, &ss3); |
| 1025 EXPECT_EQ("NULL", ss3.str()); | 1027 EXPECT_EQ("NULL", ss3.str()); |
| 1026 } | 1028 } |
| 1027 | 1029 |
| 1030 TEST(UniversalPrintTest, WorksForNonReference) { |
| 1031 ::std::stringstream ss; |
| 1032 UniversalPrint(123, &ss); |
| 1033 EXPECT_EQ("123", ss.str()); |
| 1034 } |
| 1035 |
| 1036 TEST(UniversalPrintTest, WorksForReference) { |
| 1037 const int& n = 123; |
| 1038 ::std::stringstream ss; |
| 1039 UniversalPrint(n, &ss); |
| 1040 EXPECT_EQ("123", ss.str()); |
| 1041 } |
| 1042 |
| 1043 TEST(UniversalPrintTest, WorksForCString) { |
| 1044 const char* s1 = "abc"; |
| 1045 ::std::stringstream ss1; |
| 1046 UniversalPrint(s1, &ss1); |
| 1047 EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str())); |
| 1048 |
| 1049 char* s2 = const_cast<char*>(s1); |
| 1050 ::std::stringstream ss2; |
| 1051 UniversalPrint(s2, &ss2); |
| 1052 EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str())); |
| 1053 |
| 1054 const char* s3 = NULL; |
| 1055 ::std::stringstream ss3; |
| 1056 UniversalPrint(s3, &ss3); |
| 1057 EXPECT_EQ("NULL", ss3.str()); |
| 1058 } |
| 1059 |
| 1060 |
| 1028 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) { | 1061 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) { |
| 1029 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple()), | 1062 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple()), |
| 1030 ElementsAre()); | 1063 ElementsAre()); |
| 1031 } | 1064 } |
| 1032 | 1065 |
| 1033 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) { | 1066 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) { |
| 1034 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple(1)), | 1067 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple(1)), |
| 1035 ElementsAre("1")); | 1068 ElementsAre("1")); |
| 1036 } | 1069 } |
| 1037 | 1070 |
| 1038 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) { | 1071 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) { |
| 1039 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a')), | 1072 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a')), |
| 1040 ElementsAre("1", "'a' (97)")); | 1073 ElementsAre("1", "'a' (97)")); |
| 1041 } | 1074 } |
| 1042 | 1075 |
| 1043 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) { | 1076 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) { |
| 1044 const int n = 1; | 1077 const int n = 1; |
| 1045 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings( | 1078 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings( |
| 1046 tuple<const int&, const char*>(n, "a")), | 1079 tuple<const int&, const char*>(n, "a")), |
| 1047 ElementsAre("1", "\"a\"")); | 1080 ElementsAre("1", "\"a\"")); |
| 1048 } | 1081 } |
| 1049 | 1082 |
| 1050 } // namespace gmock_printers_test | 1083 } // namespace gmock_printers_test |
| 1051 } // namespace testing | 1084 } // namespace testing |
| OLD | NEW |