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