|
| 1 | +// Copyright 2026 The Abseil Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "absl/strings/internal/stringify_stream.h" |
| 16 | + |
| 17 | +#include <cstddef> |
| 18 | +#include <ostream> |
| 19 | +#include <sstream> |
| 20 | + |
| 21 | +#include "gtest/gtest.h" |
| 22 | +#include "absl/base/config.h" |
| 23 | +#include "absl/strings/str_format.h" |
| 24 | +#include "absl/strings/string_view.h" |
| 25 | + |
| 26 | +namespace absl { |
| 27 | +ABSL_NAMESPACE_BEGIN |
| 28 | +namespace strings_internal { |
| 29 | +namespace { |
| 30 | + |
| 31 | +// Exercises the Append(size_t, char) overload |
| 32 | +struct AppendNCharsTest { |
| 33 | + size_t count; |
| 34 | + char ch; |
| 35 | + |
| 36 | + template <typename Sink> |
| 37 | + friend void AbslStringify(Sink& sink, const AppendNCharsTest& t) { |
| 38 | + sink.Append(t.count, t.ch); |
| 39 | + } |
| 40 | +}; |
| 41 | +TEST(StringifyStreamTest, AppendNChars) { |
| 42 | + std::ostringstream os; |
| 43 | + StringifyStream(os) << AppendNCharsTest{5, 'a'}; |
| 44 | + EXPECT_EQ(os.str(), "aaaaa"); |
| 45 | +} |
| 46 | + |
| 47 | +// Exercises the Append(absl::string_view) overload |
| 48 | +struct AppendStringViewTest { |
| 49 | + absl::string_view v; |
| 50 | + |
| 51 | + template <typename Sink> |
| 52 | + friend void AbslStringify(Sink& sink, const AppendStringViewTest& t) { |
| 53 | + sink.Append(t.v); |
| 54 | + } |
| 55 | +}; |
| 56 | +TEST(StringifyStreamTest, AppendStringView) { |
| 57 | + std::ostringstream os; |
| 58 | + StringifyStream(os) << AppendStringViewTest{"abc"}; |
| 59 | + EXPECT_EQ(os.str(), "abc"); |
| 60 | +} |
| 61 | + |
| 62 | +// Exercises AbslFormatFlush(OStringStreamSink*, absl::string_view v) |
| 63 | +struct AbslFormatFlushTest { |
| 64 | + absl::string_view a, b, c; |
| 65 | + |
| 66 | + template <typename Sink> |
| 67 | + friend void AbslStringify(Sink& sink, const AbslFormatFlushTest& t) { |
| 68 | + absl::Format(&sink, "%s, %s, %s", t.a, t.b, t.c); |
| 69 | + } |
| 70 | +}; |
| 71 | +TEST(StringifyStreamTest, AbslFormatFlush) { |
| 72 | + std::ostringstream os; |
| 73 | + StringifyStream(os) << AbslFormatFlushTest{"a", "b", "c"}; |
| 74 | + EXPECT_EQ(os.str(), "a, b, c"); |
| 75 | +} |
| 76 | + |
| 77 | +// If overloads of both AbslStringify and operator<< are defined for the type, |
| 78 | +// the operator<< overload should take precedence. |
| 79 | +struct PreferStreamInsertionOverAbslStringifyTest { |
| 80 | + friend std::ostream& operator<<( // NOLINT(clang-diagnostic-unused-function) |
| 81 | + std::ostream& os, const PreferStreamInsertionOverAbslStringifyTest&) { |
| 82 | + return os << "good"; |
| 83 | + } |
| 84 | + |
| 85 | + template <typename Sink> |
| 86 | + friend void AbslStringify // NOLINT(clang-diagnostic-unused-function) |
| 87 | + (Sink& sink, const PreferStreamInsertionOverAbslStringifyTest&) { |
| 88 | + sink.Append("bad"); |
| 89 | + } |
| 90 | +}; |
| 91 | +TEST(StringifyStreamTest, PreferStreamInsertionOverAbslStringify) { |
| 92 | + std::ostringstream os; |
| 93 | + StringifyStream(os) << PreferStreamInsertionOverAbslStringifyTest{}; |
| 94 | + EXPECT_EQ(os.str(), "good"); |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace |
| 98 | +} // namespace strings_internal |
| 99 | +ABSL_NAMESPACE_END |
| 100 | +} // namespace absl |
0 commit comments