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

Side by Side Diff: webrtc/base/optional_unittest.cc

Issue 2704483002: Add a PrintTo function for rtc::Optional to aid with testing. (Closed)
Patch Set: Created 3 years, 10 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
« webrtc/base/optional.h ('K') | « webrtc/base/optional.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 MyUnprintableType() = default;
26 MyUnprintableType(int value) : value(value) {}
27
28 int value;
kwiberg-webrtc 2017/02/16 14:07:37 You can make these classes several lines shorter b
ossu 2017/02/16 14:23:48 That is true. I could also just use brace initiali
29 };
30
31 struct MyPrintableType {
32 MyPrintableType() = default;
33 MyPrintableType(int value) : value(value) {}
34
35 int value;
36
37 friend std::ostream& operator<<(std::ostream& os,
38 const MyPrintableType& mpt) {
39 os << mpt.value;
40 return os;
41 }
42 };
43
24 // Class whose instances logs various method calls (constructor, destructor, 44 // Class whose instances logs various method calls (constructor, destructor,
25 // etc.). Each instance has a unique ID (a simple global sequence number) and 45 // 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 46 // 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, 47 // 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 48 // 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). 49 // constructor) or given as an argument (explicit constructor).
30 class Logger { 50 class Logger {
31 public: 51 public:
32 Logger() : id_(g_next_id++), origin_(id_) { Log("default constructor"); } 52 Logger() : id_(g_next_id++), origin_(id_) { Log("default constructor"); }
33 explicit Logger(int origin) : id_(g_next_id++), origin_(origin) { 53 explicit Logger(int origin) : id_(g_next_id++), origin_(origin) {
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 Logger moved = x.MoveValue(); 753 Logger moved = x.MoveValue();
734 log->push_back("---"); 754 log->push_back("---");
735 } 755 }
736 EXPECT_EQ( 756 EXPECT_EQ(
737 V("0:42. explicit constructor", "1:42. move constructor (from 0:42)", 757 V("0:42. explicit constructor", "1:42. move constructor (from 0:42)",
738 "0:42. destructor", "---", "2:42. move constructor (from 1:42)", "---", 758 "0:42. destructor", "---", "2:42. move constructor (from 1:42)", "---",
739 "2:42. destructor", "1:42. destructor"), 759 "2:42. destructor", "1:42. destructor"),
740 *log); 760 *log);
741 } 761 }
742 762
763 TEST(OptionalTest, TestPrintTo) {
764 constexpr char kEmptyOptionalMessage[] = "<empty optional>";
765 const Optional<MyUnprintableType> empty_unprintable;
766 const Optional<MyPrintableType> empty_printable;
767 static_assert(std::is_same<void, decltype(PrintTo(empty_printable,
ossu 2017/02/16 13:20:05 Not really the greatest place to do a static_asser
kwiberg-webrtc 2017/02/16 14:07:37 Hmm. When would the return value not be void?
ossu 2017/02/16 14:23:48 I put this here only to ensure that my cast in dec
kwiberg-webrtc 2017/02/16 14:33:33 Oh, OK, that explains why you were talking about w
768 static_cast<std::ostream*>(
769 nullptr)))>::value != 0,
kwiberg-webrtc 2017/02/16 14:07:37 std::declval<std::ostream*>() ?
ossu 2017/02/16 14:23:48 Ah, right! The last time I did this, I had a C++11
770 "PrintTo should return void");
771 EXPECT_NE(kEmptyOptionalMessage, ::testing::PrintToString(empty_unprintable));
772 EXPECT_EQ(kEmptyOptionalMessage, ::testing::PrintToString(empty_printable));
773 EXPECT_NE("1", ::testing::PrintToString(Optional<MyUnprintableType>(1)));
774 EXPECT_EQ("1", ::testing::PrintToString(Optional<MyPrintableType>(1)));
775 }
776
743 } // namespace rtc 777 } // namespace rtc
OLDNEW
« webrtc/base/optional.h ('K') | « webrtc/base/optional.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698