| 1 |
#include "catch.hpp" |
| 2 |
#include "join.hh" |
| 3 |
|
| 4 |
#include "strCat.hh" |
| 5 |
|
| 6 |
#include <ranges> |
| 7 |
#include <string_view> |
| 8 |
|
| 9 |
TEST_CASE("join: vector<string_view>, char") |
| 10 |
{ |
| 11 |
auto check = [](const std::vector<std::string_view>& v, std::string_view expected) { |
| 12 |
std::string result = join(v, '-'); |
| 13 |
CHECK(result == expected); |
| 14 |
}; |
| 15 |
|
| 16 |
check({}, ""); |
| 17 |
check({""}, ""); |
| 18 |
check({"foo"}, "foo"); |
| 19 |
check({"", ""}, "-"); |
| 20 |
check({"foo", ""}, "foo-"); |
| 21 |
check({"", "foo"}, "-foo"); |
| 22 |
check({"foo", "bar"}, "foo-bar"); |
| 23 |
check({"foo", "bar", "qux"}, "foo-bar-qux"); |
| 24 |
check({"", "bar", "qux"}, "-bar-qux"); |
| 25 |
check({"foo", "bar", ""}, "foo-bar-"); |
| 26 |
} |
| 27 |
|
| 28 |
TEST_CASE("join: various types") |
| 29 |
{ |
| 30 |
std::vector<std::string> vs = {"foo", "bar", "qux"}; |
| 31 |
std::vector<int> vi = {1, -89, 673, 0}; |
| 32 |
std::array ac = {"blabla", "xyz", "4567"}; |
| 33 |
|
| 34 |
char sep1 = '-'; |
| 35 |
const char* sep2 = ", "; |
| 36 |
std::string sep3 = "<-->"; |
| 37 |
int sep4 = 123; |
| 38 |
|
| 39 |
auto check = [](const auto& range, const auto& sep, std::string_view expected) { |
| 40 |
std::string result1 = join(range, sep); |
| 41 |
CHECK(result1 == expected); |
| 42 |
}; |
| 43 |
|
| 44 |
check(vs, sep1, "foo-bar-qux"); |
| 45 |
check(vs, sep2, "foo, bar, qux"); |
| 46 |
check(vs, sep3, "foo<-->bar<-->qux"); |
| 47 |
check(vs, sep4, "foo123bar123qux"); |
| 48 |
|
| 49 |
check(vi, sep1, "1--89-673-0"); |
| 50 |
check(vi, sep2, "1, -89, 673, 0"); |
| 51 |
check(vi, sep3, "1<-->-89<-->673<-->0"); |
| 52 |
check(vi, sep4, "1123-891236731230"); |
| 53 |
|
| 54 |
check(ac, sep1, "blabla-xyz-4567"); |
| 55 |
check(ac, sep2, "blabla, xyz, 4567"); |
| 56 |
check(ac, sep3, "blabla<-->xyz<-->4567"); |
| 57 |
check(ac, sep4, "blabla123xyz1234567"); |
| 58 |
|
| 59 |
auto quote = [](auto& s) { return strCat('\'', s, '\''); }; |
| 60 |
check(std::views::transform(vs, quote), ", ", "'foo', 'bar', 'qux'"); |
| 61 |
check(std::views::transform(vi, quote), ", ", "'1', '-89', '673', '0'"); |
| 62 |
check(std::views::transform(ac, quote), ", ", "'blabla', 'xyz', '4567'"); |
| 63 |
} |