openMSX
join_test.cc
Go to the documentation of this file.
1 #include "catch.hpp"
2 #include "join.hh"
3 
4 #include "string_view.hh"
5 #include "strCat.hh"
6 #include "view.hh"
7 
8 using namespace std;
9 
10 
11 TEST_CASE("join: vector<string_view>, char")
12 {
13  auto check = [](const vector<string_view>& v, const string& expected) {
14  std::string result = join(v, '-');
15  CHECK(result == expected);
16  };
17 
18  check({}, "");
19  check({""}, "");
20  check({"foo"}, "foo");
21  check({"", ""}, "-");
22  check({"foo", ""}, "foo-");
23  check({"", "foo"}, "-foo");
24  check({"foo", "bar"}, "foo-bar");
25  check({"foo", "bar", "qux"}, "foo-bar-qux");
26  check({"", "bar", "qux"}, "-bar-qux");
27  check({"foo", "bar", ""}, "foo-bar-");
28 }
29 
30 TEST_CASE("join: various types")
31 {
32  vector<string> vs = {"foo", "bar", "qux"};
33  vector<int> vi = {1, -89, 673, 0};
34  const char* ac[] = {"blabla", "xyz", "4567"};
35 
36  char sep1 = '-';
37  const char* sep2 = ", ";
38  string sep3 = "<-->";
39  int sep4 = 123;
40 
41  auto check = [](const auto& range, const auto& sep, const string& expected) {
42  std::string result1 = join(range, sep);
43  CHECK(result1 == expected);
44 
45  ostringstream ss;
46  ss << join(range, sep);
47  std::string result2 = ss.str();
48  CHECK(result2 == expected);
49  };
50 
51  check(vs, sep1, "foo-bar-qux");
52  check(vs, sep2, "foo, bar, qux");
53  check(vs, sep3, "foo<-->bar<-->qux");
54  check(vs, sep4, "foo123bar123qux");
55 
56  check(vi, sep1, "1--89-673-0");
57  check(vi, sep2, "1, -89, 673, 0");
58  check(vi, sep3, "1<-->-89<-->673<-->0");
59  check(vi, sep4, "1123-891236731230");
60 
61  check(ac, sep1, "blabla-xyz-4567");
62  check(ac, sep2, "blabla, xyz, 4567");
63  check(ac, sep3, "blabla<-->xyz<-->4567");
64  check(ac, sep4, "blabla123xyz1234567");
65 
66  auto quote = [](auto& s) { return strCat('\'', s, '\''); };
67  check(view::transform(vs, quote), ", ", "'foo', 'bar', 'qux'");
68  check(view::transform(vi, quote), ", ", "'1', '-89', '673', '0'");
69  check(view::transform(ac, quote), ", ", "'blabla', 'xyz', '4567'");
70 }
auto transform(Range &&range, UnaryOp op)
Definition: view.hh:312
STL namespace.
CHECK(m3==m3)
TEST_CASE("join: vector<string_view>, char")
Definition: join_test.cc:11
std::string strCat(Ts &&...ts)
Definition: strCat.hh:577
detail::Joiner< Collection, Separator > join(Collection &&col, Separator &&sep)
Definition: join.hh:60