OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <sstream> | 12 #include <sstream> |
13 #include <string> | 13 #include <string> |
14 #include <utility> | 14 #include <utility> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "webrtc/base/gunit.h" | 17 #include "webrtc/base/gunit.h" |
18 #include "webrtc/base/optional.h" | 18 #include "webrtc/base/optional.h" |
19 | 19 |
20 namespace rtc { | 20 namespace rtc { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
| 24 struct MyUnprintableType { |
| 25 int value; |
| 26 }; |
| 27 |
| 28 struct MyPrintableType { |
| 29 int value; |
| 30 }; |
| 31 |
| 32 struct MyOstreamPrintableType { |
| 33 int value; |
| 34 }; |
| 35 |
| 36 void PrintTo(const MyPrintableType& mpt, std::ostream* os) { |
| 37 *os << "The value is " << mpt.value; |
| 38 } |
| 39 |
| 40 std::ostream& operator<<(std::ostream& os, |
| 41 const MyPrintableType& mpt) { |
| 42 os << mpt.value; |
| 43 return os; |
| 44 } |
| 45 |
| 46 std::ostream& operator<<(std::ostream& os, |
| 47 const MyOstreamPrintableType& mpt) { |
| 48 os << mpt.value; |
| 49 return os; |
| 50 } |
| 51 |
24 // Class whose instances logs various method calls (constructor, destructor, | 52 // Class whose instances logs various method calls (constructor, destructor, |
25 // etc.). Each instance has a unique ID (a simple global sequence number) and | 53 // etc.). Each instance has a unique ID (a simple global sequence number) and |
26 // an origin ID. When a copy is made, the new object gets a fresh ID but copies | 54 // an origin ID. When a copy is made, the new object gets a fresh ID but copies |
27 // the origin ID from the original. When a new Logger is created from scratch, | 55 // the origin ID from the original. When a new Logger is created from scratch, |
28 // it gets a fresh ID, and the origin ID is the same as the ID (default | 56 // it gets a fresh ID, and the origin ID is the same as the ID (default |
29 // constructor) or given as an argument (explicit constructor). | 57 // constructor) or given as an argument (explicit constructor). |
30 class Logger { | 58 class Logger { |
31 public: | 59 public: |
32 Logger() : id_(g_next_id++), origin_(id_) { Log("default constructor"); } | 60 Logger() : id_(g_next_id++), origin_(id_) { Log("default constructor"); } |
33 explicit Logger(int origin) : id_(g_next_id++), origin_(origin) { | 61 explicit Logger(int origin) : id_(g_next_id++), origin_(origin) { |
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
733 Logger moved = x.MoveValue(); | 761 Logger moved = x.MoveValue(); |
734 log->push_back("---"); | 762 log->push_back("---"); |
735 } | 763 } |
736 EXPECT_EQ( | 764 EXPECT_EQ( |
737 V("0:42. explicit constructor", "1:42. move constructor (from 0:42)", | 765 V("0:42. explicit constructor", "1:42. move constructor (from 0:42)", |
738 "0:42. destructor", "---", "2:42. move constructor (from 1:42)", "---", | 766 "0:42. destructor", "---", "2:42. move constructor (from 1:42)", "---", |
739 "2:42. destructor", "1:42. destructor"), | 767 "2:42. destructor", "1:42. destructor"), |
740 *log); | 768 *log); |
741 } | 769 } |
742 | 770 |
| 771 TEST(OptionalTest, TestPrintTo) { |
| 772 constexpr char kEmptyOptionalMessage[] = "<empty optional>"; |
| 773 const Optional<MyUnprintableType> empty_unprintable; |
| 774 const Optional<MyPrintableType> empty_printable; |
| 775 const Optional<MyOstreamPrintableType> empty_ostream_printable; |
| 776 EXPECT_EQ(kEmptyOptionalMessage, ::testing::PrintToString(empty_unprintable)); |
| 777 EXPECT_EQ(kEmptyOptionalMessage, ::testing::PrintToString(empty_printable)); |
| 778 EXPECT_EQ(kEmptyOptionalMessage, |
| 779 ::testing::PrintToString(empty_ostream_printable)); |
| 780 EXPECT_NE("1", ::testing::PrintToString(Optional<MyUnprintableType>({1}))); |
| 781 EXPECT_NE("1", ::testing::PrintToString(Optional<MyPrintableType>({1}))); |
| 782 EXPECT_EQ("The value is 1", |
| 783 ::testing::PrintToString(Optional<MyPrintableType>({1}))); |
| 784 EXPECT_EQ("1", |
| 785 ::testing::PrintToString(Optional<MyOstreamPrintableType>({1}))); |
| 786 } |
| 787 |
| 788 void UnusedFunctionWorkaround() { |
| 789 // These are here to ensure we don't get warnings about ostream and PrintTo |
| 790 // for MyPrintableType never getting called. |
| 791 const MyPrintableType dont_warn{17}; |
| 792 const MyOstreamPrintableType dont_warn2{18}; |
| 793 std::stringstream sstr; |
| 794 sstr << dont_warn; |
| 795 PrintTo(dont_warn, &sstr); |
| 796 sstr << dont_warn2; |
| 797 } |
| 798 |
743 } // namespace rtc | 799 } // namespace rtc |
OLD | NEW |