xref: /aosp_15_r20/external/cronet/base/strings/to_string_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/strings/to_string.h"
6 
7 #include <ios>
8 #include <ostream>
9 #include <string>
10 
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace base {
14 namespace {
15 
16 class NotStringifiable {};
17 class HasToString {
18  public:
ToString() const19   std::string ToString() const { return "yay!"; }
20 };
21 
22 // .ToString() support on structs.
23 static_assert(!internal::SupportsToString<NotStringifiable>,
24               "value without ToString() shouldn't be marked SupportsToString");
25 static_assert(!internal::SupportsToString<NotStringifiable&>,
26               "& without ToString() shouldn't be marked SupportsToString");
27 static_assert(!internal::SupportsToString<const NotStringifiable&>,
28               "const& without ToString() shouldn't be marked SupportsToString");
29 static_assert(!internal::SupportsToString<NotStringifiable&&>,
30               "&& without ToString() shouldn't be marked SupportsToString");
31 static_assert(internal::SupportsToString<HasToString>,
32               "value with ToString() should be marked SupportsToString");
33 static_assert(internal::SupportsToString<HasToString&>,
34               "& with ToString() should be marked SupportsToString");
35 static_assert(internal::SupportsToString<const HasToString&>,
36               "const& with ToString() should be marked SupportsToString");
37 static_assert(internal::SupportsToString<HasToString&&>,
38               "&& with ToString() should be marked SupportsToString");
39 
TEST(ToStringTest,Streamable)40 TEST(ToStringTest, Streamable) {
41   // Types with built-in <<.
42   EXPECT_EQ(ToString("foo"), "foo");
43   EXPECT_EQ(ToString(123), "123");
44 }
45 
46 enum class StreamableTestEnum { kGreeting, kLocation };
47 
operator <<(std::ostream & os,const StreamableTestEnum & value)48 std::ostream& operator<<(std::ostream& os, const StreamableTestEnum& value) {
49   switch (value) {
50     case StreamableTestEnum::kGreeting:
51       return os << "hello";
52     case StreamableTestEnum::kLocation:
53       return os << "world";
54   }
55 }
56 
TEST(ToStringTest,UserDefinedStreamable)57 TEST(ToStringTest, UserDefinedStreamable) {
58   // Type with user-defined <<.
59   EXPECT_EQ(ToString(StreamableTestEnum::kGreeting), "hello");
60   EXPECT_EQ(ToString(StreamableTestEnum::kGreeting, " ",
61                      StreamableTestEnum::kLocation),
62             "hello world");
63 }
64 
TEST(ToStringTest,UserDefinedToString)65 TEST(ToStringTest, UserDefinedToString) {
66   // Type with user-defined ToString().
67   EXPECT_EQ(ToString(HasToString()), "yay!");
68 }
69 
70 class UnusualToString {
71  public:
ToString() const72   HasToString ToString() const { return HasToString(); }
73 };
74 
TEST(ToStringTest,ToStringReturnsNonStdString)75 TEST(ToStringTest, ToStringReturnsNonStdString) {
76   // Types with a ToString() that does not directly return a std::string should
77   // still work.
78   EXPECT_EQ(ToString(UnusualToString()), "yay!");
79 }
80 
81 enum class NonStreamableTestEnum { kGreeting = 0, kLocation };
82 
TEST(ToStringTest,ScopedEnum)83 TEST(ToStringTest, ScopedEnum) {
84   // Scoped enums without a defined << should print as their underlying type.
85   EXPECT_EQ(ToString(NonStreamableTestEnum::kLocation), "1");
86 }
87 
TEST(ToStringTest,IoManip)88 TEST(ToStringTest, IoManip) {
89   // I/O manipulators should have their expected effect, not be printed as
90   // function pointers.
91   EXPECT_EQ(ToString("42 in hex is ", std::hex, 42), "42 in hex is 2a");
92 }
93 
TEST(ToStringTest,Tuple)94 TEST(ToStringTest, Tuple) {
95   // Tuples should correctly format the contained types.
96   EXPECT_EQ(ToString(std::make_tuple(StreamableTestEnum::kGreeting,
97                                      HasToString(), "a string")),
98             "<hello, yay!, a string>");
99 }
100 
Func()101 void Func() {}
102 
TEST(ToStringTest,FunctionPointer)103 TEST(ToStringTest, FunctionPointer) {
104   // We don't care about the actual address, but a function pointer should not
105   // be implicitly converted to bool.
106   EXPECT_NE(ToString(&Func), ToString(true));
107 
108   // Functions should be treated like function pointers.
109   EXPECT_EQ(ToString(Func), ToString(&Func));
110 }
111 
112 class OverloadsAddressOp {
113  public:
operator &()114   OverloadsAddressOp* operator&() { return nullptr; }
operator &() const115   const OverloadsAddressOp* operator&() const { return nullptr; }
116 };
117 
TEST(ToStringTest,NonStringifiable)118 TEST(ToStringTest, NonStringifiable) {
119   // Non-stringifiable types should be printed using a fallback.
120   EXPECT_NE(ToString(NotStringifiable()).find("-byte object at 0x"),
121             std::string::npos);
122 
123   // Non-stringifiable types which overload operator& should print their real
124   // address.
125   EXPECT_NE(ToString(OverloadsAddressOp()),
126             ToString(static_cast<OverloadsAddressOp*>(nullptr)));
127 }
128 
129 }  // namespace
130 }  // namespace base
131