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

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: Reworked PrintTo so that it's always implemented, or gtest will print out uninitialized bytes. 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.cc ('K') | « webrtc/base/optional.cc ('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 int value;
26 };
27
28 struct MyPrintableType {
29 int value;
30 };
31
32 struct MyOstreamPrintableType {
33 int value;
34 };
35
36 // Since only one of these two (PrintTo and operator<<) will be picked by gtest
37 // for printing, we need to ensure it's fine that the other one is unused. The
38 // test itself will ensure the correct one is called.
39 void PrintTo(const MyPrintableType& mpt, std::ostream* os)
40 __attribute__((unused));
41
42 void PrintTo(const MyPrintableType& mpt, std::ostream* os) {
43 *os << "The value is " << mpt.value;
44 }
45
46 std::ostream& operator<<(std::ostream& os,
47 const MyPrintableType& mpt) __attribute__((unused));
48
49 std::ostream& operator<<(std::ostream& os,
50 const MyPrintableType& mpt) {
51 os << mpt.value;
52 return os;
53 }
54
55 std::ostream& operator<<(std::ostream& os,
56 const MyOstreamPrintableType& mpt) {
57 os << mpt.value;
58 return os;
59 }
60
24 // Class whose instances logs various method calls (constructor, destructor, 61 // Class whose instances logs various method calls (constructor, destructor,
25 // etc.). Each instance has a unique ID (a simple global sequence number) and 62 // 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 63 // 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, 64 // 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 65 // 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). 66 // constructor) or given as an argument (explicit constructor).
30 class Logger { 67 class Logger {
31 public: 68 public:
32 Logger() : id_(g_next_id++), origin_(id_) { Log("default constructor"); } 69 Logger() : id_(g_next_id++), origin_(id_) { Log("default constructor"); }
33 explicit Logger(int origin) : id_(g_next_id++), origin_(origin) { 70 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(); 770 Logger moved = x.MoveValue();
734 log->push_back("---"); 771 log->push_back("---");
735 } 772 }
736 EXPECT_EQ( 773 EXPECT_EQ(
737 V("0:42. explicit constructor", "1:42. move constructor (from 0:42)", 774 V("0:42. explicit constructor", "1:42. move constructor (from 0:42)",
738 "0:42. destructor", "---", "2:42. move constructor (from 1:42)", "---", 775 "0:42. destructor", "---", "2:42. move constructor (from 1:42)", "---",
739 "2:42. destructor", "1:42. destructor"), 776 "2:42. destructor", "1:42. destructor"),
740 *log); 777 *log);
741 } 778 }
742 779
780 TEST(OptionalTest, TestPrintTo) {
781 constexpr char kEmptyOptionalMessage[] = "<empty optional>";
782 const Optional<MyUnprintableType> empty_unprintable;
783 const Optional<MyPrintableType> empty_printable;
784 const Optional<MyOstreamPrintableType> empty_ostream_printable;
785 EXPECT_EQ(kEmptyOptionalMessage, ::testing::PrintToString(empty_unprintable));
786 EXPECT_EQ(kEmptyOptionalMessage, ::testing::PrintToString(empty_printable));
787 EXPECT_EQ(kEmptyOptionalMessage,
788 ::testing::PrintToString(empty_ostream_printable));
789 EXPECT_NE("1", ::testing::PrintToString(Optional<MyUnprintableType>({1})));
790 EXPECT_NE("1", ::testing::PrintToString(Optional<MyPrintableType>({1})));
791 EXPECT_EQ("The value is 1",
792 ::testing::PrintToString(Optional<MyPrintableType>({1})));
793 EXPECT_EQ("1",
794 ::testing::PrintToString(Optional<MyOstreamPrintableType>({1})));
795 }
796
743 } // namespace rtc 797 } // namespace rtc
OLDNEW
« webrtc/base/optional.cc ('K') | « webrtc/base/optional.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698