| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 // the value if it is a protocol buffer, or print the raw bytes in the | 50 // the value if it is a protocol buffer, or print the raw bytes in the |
| 51 // value otherwise. | 51 // value otherwise. |
| 52 // | 52 // |
| 53 // To aid debugging: when T is a reference type, the address of the | 53 // To aid debugging: when T is a reference type, the address of the |
| 54 // value is also printed; when T is a (const) char pointer, both the | 54 // value is also printed; when T is a (const) char pointer, both the |
| 55 // pointer value and the NUL-terminated string it points to are | 55 // pointer value and the NUL-terminated string it points to are |
| 56 // printed. | 56 // printed. |
| 57 // | 57 // |
| 58 // We also provide some convenient wrappers: | 58 // We also provide some convenient wrappers: |
| 59 // | 59 // |
| 60 // // Prints a value as the given type to a string. | 60 // // Prints a value to a string. For a (const or not) char |
| 61 // string ::testing::internal::UniversalPrinter<T>::PrintToString(value); | 61 // // pointer, the NUL-terminated string (but not the pointer) is |
| 62 // // printed. |
| 63 // std::string ::testing::PrintToString(const T& value); |
| 62 // | 64 // |
| 63 // // Prints a value tersely: for a reference type, the referenced | 65 // // Prints a value tersely: for a reference type, the referenced |
| 64 // // value (but not the address) is printed; for a (const) char | 66 // // value (but not the address) is printed; for a (const or not) char |
| 65 // // pointer, the NUL-terminated string (but not the pointer) is | 67 // // pointer, the NUL-terminated string (but not the pointer) is |
| 66 // // printed. | 68 // // printed. |
| 67 // void ::testing::internal::UniversalTersePrint(const T& value, ostream*); | 69 // void ::testing::internal::UniversalTersePrint(const T& value, ostream*); |
| 68 // | 70 // |
| 69 // // Prints value using the type inferred by the compiler. The difference | 71 // // Prints value using the type inferred by the compiler. The difference |
| 70 // // from UniversalTersePrint() is that this function prints both the | 72 // // from UniversalTersePrint() is that this function prints both the |
| 71 // // pointer and the NUL-terminated string for a (const) char pointer. | 73 // // pointer and the NUL-terminated string for a (const or not) char pointer. |
| 72 // void ::testing::internal::UniversalPrint(const T& value, ostream*); | 74 // void ::testing::internal::UniversalPrint(const T& value, ostream*); |
| 73 // | 75 // |
| 74 // // Prints the fields of a tuple tersely to a string vector, one | 76 // // Prints the fields of a tuple tersely to a string vector, one |
| 75 // // element for each field. | 77 // // element for each field. |
| 76 // std::vector<string> UniversalTersePrintTupleFieldsToStrings( | 78 // std::vector<string> UniversalTersePrintTupleFieldsToStrings( |
| 77 // const Tuple& value); | 79 // const Tuple& value); |
| 78 // | 80 // |
| 79 // Known limitation: | 81 // Known limitation: |
| 80 // | 82 // |
| 81 // The print primitives print the elements of an STL-style container | 83 // The print primitives print the elements of an STL-style container |
| (...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 // the value. | 540 // the value. |
| 539 // | 541 // |
| 540 // Thanks to Koenig look-up, if T is a class and has its own | 542 // Thanks to Koenig look-up, if T is a class and has its own |
| 541 // PrintTo() function defined in its namespace, that function will | 543 // PrintTo() function defined in its namespace, that function will |
| 542 // be visible here. Since it is more specific than the generic ones | 544 // be visible here. Since it is more specific than the generic ones |
| 543 // in ::testing::internal, it will be picked by the compiler in the | 545 // in ::testing::internal, it will be picked by the compiler in the |
| 544 // following statement - exactly what we want. | 546 // following statement - exactly what we want. |
| 545 PrintTo(value, os); | 547 PrintTo(value, os); |
| 546 } | 548 } |
| 547 | 549 |
| 548 // A convenient wrapper for Print() that returns the print-out as a | |
| 549 // string. | |
| 550 static string PrintToString(const T& value) { | |
| 551 ::std::stringstream ss; | |
| 552 Print(value, &ss); | |
| 553 return ss.str(); | |
| 554 } | |
| 555 | |
| 556 #ifdef _MSC_VER | 550 #ifdef _MSC_VER |
| 557 #pragma warning(pop) // Restores the warning state. | 551 #pragma warning(pop) // Restores the warning state. |
| 558 #endif // _MSC_VER | 552 #endif // _MSC_VER |
| 559 }; | 553 }; |
| 560 | 554 |
| 561 // UniversalPrintArray(begin, len, os) prints an array of 'len' | 555 // UniversalPrintArray(begin, len, os) prints an array of 'len' |
| 562 // elements, starting at address 'begin'. | 556 // elements, starting at address 'begin'. |
| 563 template <typename T> | 557 template <typename T> |
| 564 void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) { | 558 void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) { |
| 565 if (len == 0) { | 559 if (len == 0) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 578 PrintRawArrayTo(begin, kChunkSize, os); | 572 PrintRawArrayTo(begin, kChunkSize, os); |
| 579 *os << ", ..., "; | 573 *os << ", ..., "; |
| 580 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os); | 574 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os); |
| 581 } | 575 } |
| 582 *os << " }"; | 576 *os << " }"; |
| 583 } | 577 } |
| 584 } | 578 } |
| 585 // This overload prints a (const) char array compactly. | 579 // This overload prints a (const) char array compactly. |
| 586 void UniversalPrintArray(const char* begin, size_t len, ::std::ostream* os); | 580 void UniversalPrintArray(const char* begin, size_t len, ::std::ostream* os); |
| 587 | 581 |
| 588 // Prints an array of 'len' elements, starting at address 'begin', to a string. | |
| 589 template <typename T> | |
| 590 string UniversalPrintArrayToString(const T* begin, size_t len) { | |
| 591 ::std::stringstream ss; | |
| 592 UniversalPrintArray(begin, len, &ss); | |
| 593 return ss.str(); | |
| 594 } | |
| 595 | |
| 596 // Implements printing an array type T[N]. | 582 // Implements printing an array type T[N]. |
| 597 template <typename T, size_t N> | 583 template <typename T, size_t N> |
| 598 class UniversalPrinter<T[N]> { | 584 class UniversalPrinter<T[N]> { |
| 599 public: | 585 public: |
| 600 // Prints the given array, omitting some elements when there are too | 586 // Prints the given array, omitting some elements when there are too |
| 601 // many. | 587 // many. |
| 602 static void Print(const T (&a)[N], ::std::ostream* os) { | 588 static void Print(const T (&a)[N], ::std::ostream* os) { |
| 603 UniversalPrintArray(a, N, os); | 589 UniversalPrintArray(a, N, os); |
| 604 } | 590 } |
| 605 | |
| 606 // A convenient wrapper for Print() that returns the print-out as a | |
| 607 // string. | |
| 608 static string PrintToString(const T (&a)[N]) { | |
| 609 return UniversalPrintArrayToString(a, N); | |
| 610 } | |
| 611 }; | 591 }; |
| 612 | 592 |
| 613 // Implements printing a reference type T&. | 593 // Implements printing a reference type T&. |
| 614 template <typename T> | 594 template <typename T> |
| 615 class UniversalPrinter<T&> { | 595 class UniversalPrinter<T&> { |
| 616 public: | 596 public: |
| 617 // MSVC warns about adding const to a function type, so we want to | 597 // MSVC warns about adding const to a function type, so we want to |
| 618 // disable the warning. | 598 // disable the warning. |
| 619 #ifdef _MSC_VER | 599 #ifdef _MSC_VER |
| 620 #pragma warning(push) // Saves the current warning state. | 600 #pragma warning(push) // Saves the current warning state. |
| 621 #pragma warning(disable:4180) // Temporarily disables warning 4180. | 601 #pragma warning(disable:4180) // Temporarily disables warning 4180. |
| 622 #endif // _MSC_VER | 602 #endif // _MSC_VER |
| 623 | 603 |
| 624 static void Print(const T& value, ::std::ostream* os) { | 604 static void Print(const T& value, ::std::ostream* os) { |
| 625 // Prints the address of the value. We use reinterpret_cast here | 605 // Prints the address of the value. We use reinterpret_cast here |
| 626 // as static_cast doesn't compile when T is a function type. | 606 // as static_cast doesn't compile when T is a function type. |
| 627 *os << "@" << reinterpret_cast<const void*>(&value) << " "; | 607 *os << "@" << reinterpret_cast<const void*>(&value) << " "; |
| 628 | 608 |
| 629 // Then prints the value itself. | 609 // Then prints the value itself. |
| 630 UniversalPrinter<T>::Print(value, os); | 610 UniversalPrinter<T>::Print(value, os); |
| 631 } | 611 } |
| 632 | 612 |
| 633 // A convenient wrapper for Print() that returns the print-out as a | |
| 634 // string. | |
| 635 static string PrintToString(const T& value) { | |
| 636 ::std::stringstream ss; | |
| 637 Print(value, &ss); | |
| 638 return ss.str(); | |
| 639 } | |
| 640 | |
| 641 #ifdef _MSC_VER | 613 #ifdef _MSC_VER |
| 642 #pragma warning(pop) // Restores the warning state. | 614 #pragma warning(pop) // Restores the warning state. |
| 643 #endif // _MSC_VER | 615 #endif // _MSC_VER |
| 644 }; | 616 }; |
| 645 | 617 |
| 646 // Prints a value tersely: for a reference type, the referenced value | 618 // Prints a value tersely: for a reference type, the referenced value |
| 647 // (but not the address) is printed; for a (const) char pointer, the | 619 // (but not the address) is printed; for a (const) char pointer, the |
| 648 // NUL-terminated string (but not the pointer) is printed. | 620 // NUL-terminated string (but not the pointer) is printed. |
| 649 template <typename T> | 621 template <typename T> |
| 650 void UniversalTersePrint(const T& value, ::std::ostream* os) { | 622 void UniversalTersePrint(const T& value, ::std::ostream* os) { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 // UniversalTersePrint() for how we define "tersely". | 705 // UniversalTersePrint() for how we define "tersely". |
| 734 template <typename Tuple> | 706 template <typename Tuple> |
| 735 Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) { | 707 Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) { |
| 736 Strings result; | 708 Strings result; |
| 737 TuplePrefixPrinter< ::std::tr1::tuple_size<Tuple>::value>:: | 709 TuplePrefixPrinter< ::std::tr1::tuple_size<Tuple>::value>:: |
| 738 TersePrintPrefixToStrings(value, &result); | 710 TersePrintPrefixToStrings(value, &result); |
| 739 return result; | 711 return result; |
| 740 } | 712 } |
| 741 | 713 |
| 742 } // namespace internal | 714 } // namespace internal |
| 715 |
| 716 template <typename T> |
| 717 ::std::string PrintToString(const T& value) { |
| 718 ::std::stringstream ss; |
| 719 internal::UniversalTersePrint(value, &ss); |
| 720 return ss.str(); |
| 721 } |
| 722 |
| 743 } // namespace testing | 723 } // namespace testing |
| 744 | 724 |
| 745 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_PRINTERS_H_ | 725 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_PRINTERS_H_ |
| OLD | NEW |