17 #include "absl/strings/str_join.h"
23 #include <initializer_list>
29 #include <type_traits>
32 #include "gtest/gtest.h"
33 #include "absl/base/macros.h"
34 #include "absl/memory/memory.h"
35 #include "absl/strings/str_cat.h"
36 #include "absl/strings/str_split.h"
37 #include "absl/strings/string_view.h"
44 std::vector<std::string>
v = {
"foo",
"bar",
"baz"};
50 std::vector<absl::string_view>
v = {
"foo",
"bar",
"baz"};
56 std::vector<const char*>
v = {
"foo",
"bar",
"baz"};
63 std::vector<char*>
v = {&
a[0], &
b[0], &
c[0]};
69 std::vector<int>
v = {1, 2, 3, -4};
86 std::vector<std::unique_ptr<int>>
v;
87 v.emplace_back(
new int(1));
88 v.emplace_back(
new int(2));
89 v.emplace_back(
new int(3));
95 const int a[] = {1, 2, 3, -4};
101 int x = 1,
y = 2,
z = 3;
102 std::vector<int*>
v = {&
x, &
y, &
z};
108 int x = 1,
y = 2,
z = 3;
109 int *px = &
x, *py = &
y, *pz = &
z;
110 std::vector<int**>
v = {&px, &py, &pz};
117 std::vector<std::string*>
v = {&
a, &
b};
123 std::map<std::string, int>
m = {{
"a", 1}, {
"b", 2}, {
"c", 3}};
140 std::vector<std::string>
v;
147 std::vector<std::string>
v = {
"foo"};
153 std::vector<std::string>
v = {
""};
159 std::vector<std::string>
v = {
"a",
""};
165 std::vector<std::string>
v = {
"",
""};
171 std::vector<bool>
v = {
true,
false,
true};
177 std::vector<std::string>
v{
"One",
"Two",
"Three"};
186 class ImmovableFormatter {
191 ImmovableFormatter() {}
192 ImmovableFormatter(
const ImmovableFormatter&) =
delete;
197 class OverloadedFormatter {
207 const OverloadedFormatter
fmt = {};
222 f(&s,
static_cast<int>(1));
225 f(&s,
static_cast<float>(4));
226 f(&s,
static_cast<double>(5));
227 f(&s,
static_cast<unsigned>(6));
228 f(&s,
static_cast<size_t>(7));
238 std::vector<bool>
v = {
true,
false,
true};
252 struct StreamableType {
255 inline std::ostream&
operator<<(std::ostream& os,
const StreamableType& t) {
256 os <<
"Streamable:" <<
t.contents;
264 f(&s,
static_cast<int>(1));
267 f(&s,
static_cast<float>(4));
268 f(&s,
static_cast<double>(5));
269 f(&s,
static_cast<unsigned>(6));
270 f(&s,
static_cast<size_t>(7));
272 StreamableType streamable = {
"object"};
274 EXPECT_EQ(
"Testing: 1234567 OK Streamable:object", s);
279 struct TestingParenFormatter {
280 template <
typename T>
292 f(&s, std::make_pair(
"a",
"b"));
293 f(&s, std::make_pair(1, 2));
300 TestingParenFormatter());
302 f(&s, std::make_pair(
"a",
"b"));
303 f(&s, std::make_pair(1, 2));
314 int x = 1,
y = 2,
z = 3;
341 int x = 1,
y = 2,
z = 3;
353 auto x = std::unique_ptr<int>(
new int(1));
354 auto y = std::unique_ptr<int>(
new int(2));
355 auto z = std::unique_ptr<int>(
new int(3));
369 std::vector<std::string>
v = {
"a",
"b",
"c"};
391 auto a = {
"a",
"b",
"c"};
396 std::initializer_list<const char*>
a = {
"a",
"b",
"c"};
401 std::initializer_list<std::string>
a = {
"a",
"b",
"c"};
406 std::initializer_list<absl::string_view>
a = {
"a",
"b",
"c"};
412 auto a = {
"a",
"b",
"c"};
413 TestingParenFormatter
f;
425 TestingParenFormatter
f;
443 struct TestFormatter {
461 "0x0000000a-hell-3.",
468 absl::make_unique<std::string>(
y),
469 absl::make_unique<double>(
z)),
484 const char*
data()
const {
return data_; }
508 template <
typename ValueT>
511 using iterator_category = std::forward_iterator_tag;
513 using pointer = void;
515 using difference_type =
int;
518 static TestIterator
begin(
const std::vector<absl::string_view>&
data) {
519 return TestIterator(&
data, 0);
522 static TestIterator
end(
const std::vector<absl::string_view>&
data) {
523 return TestIterator(
nullptr,
data.size());
526 bool operator==(
const TestIterator& other)
const {
527 return pos_ == other.pos_;
529 bool operator!=(
const TestIterator& other)
const {
530 return pos_ != other.pos_;
548 TestIterator
result = *
this;
553 TestIterator& operator--() {
558 TestIterator operator--(
int) {
559 TestIterator
result = *
this;
565 TestIterator(
const std::vector<absl::string_view>*
data,
size_t pos)
568 const std::vector<absl::string_view>*
data_;
572 template <
typename ValueT>
573 class TestIteratorRange {
576 explicit TestIteratorRange(
const std::vector<absl::string_view>&
data)
577 : begin_(TestIterator<ValueT>::
begin(
data)),
580 const TestIterator<ValueT>&
begin()
const {
return begin_; }
581 const TestIterator<ValueT>&
end()
const {
return end_; }
584 TestIterator<ValueT> begin_;
585 TestIterator<ValueT>
end_;
588 TEST(
StrJoin, TestIteratorRequirementsNoFormatter) {
589 const std::vector<absl::string_view>
a = {
"a",
"b",
"c"};
597 TEST(
StrJoin, TestIteratorRequirementsCustomFormatter) {
598 const std::vector<absl::string_view>
a = {
"a",
"b",
"c"};