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