| 1 |
|
| 2 |
// Copyright Catch2 Authors |
| 3 |
// Distributed under the Boost Software License, Version 1.0. |
| 4 |
// (See accompanying file LICENSE.txt or copy at |
| 5 |
// https://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
// SPDX-License-Identifier: BSL-1.0 |
| 8 |
|
| 9 |
#include <catch2/catch_test_macros.hpp> |
| 10 |
#include <catch2/matchers/catch_matchers_vector.hpp> |
| 11 |
#include <catch2/internal/catch_string_manip.hpp> |
| 12 |
|
| 13 |
static const char * const no_whitespace = "There is no extra whitespace here"; |
| 14 |
static const char * const leading_whitespace = " \r \t\n There is no extra whitespace here"; |
| 15 |
static const char * const trailing_whitespace = "There is no extra whitespace here \t \n \r "; |
| 16 |
static const char * const whitespace_at_both_ends = " \r\n \t There is no extra whitespace here \t\t\t \n"; |
| 17 |
|
| 18 |
TEST_CASE("Trim strings", "[string-manip]") { |
| 19 |
using Catch::trim; using Catch::StringRef; |
| 20 |
static_assert(std::is_same<std::string, decltype(trim(std::string{}))>::value, "Trimming std::string should return std::string"); |
| 21 |
static_assert(std::is_same<StringRef, decltype(trim(StringRef{}))>::value, "Trimming StringRef should return StringRef"); |
| 22 |
|
| 23 |
REQUIRE(trim(std::string(no_whitespace)) == no_whitespace); |
| 24 |
REQUIRE(trim(std::string(leading_whitespace)) == no_whitespace); |
| 25 |
REQUIRE(trim(std::string(trailing_whitespace)) == no_whitespace); |
| 26 |
REQUIRE(trim(std::string(whitespace_at_both_ends)) == no_whitespace); |
| 27 |
|
| 28 |
REQUIRE(trim(StringRef(no_whitespace)) == StringRef(no_whitespace)); |
| 29 |
REQUIRE(trim(StringRef(leading_whitespace)) == StringRef(no_whitespace)); |
| 30 |
REQUIRE(trim(StringRef(trailing_whitespace)) == StringRef(no_whitespace)); |
| 31 |
REQUIRE(trim(StringRef(whitespace_at_both_ends)) == StringRef(no_whitespace)); |
| 32 |
} |
| 33 |
|
| 34 |
TEST_CASE("replaceInPlace", "[string-manip]") { |
| 35 |
std::string letters = "abcdefcg"; |
| 36 |
SECTION("replace single char") { |
| 37 |
CHECK(Catch::replaceInPlace(letters, "b", "z")); |
| 38 |
CHECK(letters == "azcdefcg"); |
| 39 |
} |
| 40 |
SECTION("replace two chars") { |
| 41 |
CHECK(Catch::replaceInPlace(letters, "c", "z")); |
| 42 |
CHECK(letters == "abzdefzg"); |
| 43 |
} |
| 44 |
SECTION("replace first char") { |
| 45 |
CHECK(Catch::replaceInPlace(letters, "a", "z")); |
| 46 |
CHECK(letters == "zbcdefcg"); |
| 47 |
} |
| 48 |
SECTION("replace last char") { |
| 49 |
CHECK(Catch::replaceInPlace(letters, "g", "z")); |
| 50 |
CHECK(letters == "abcdefcz"); |
| 51 |
} |
| 52 |
SECTION("replace all chars") { |
| 53 |
CHECK(Catch::replaceInPlace(letters, letters, "replaced")); |
| 54 |
CHECK(letters == "replaced"); |
| 55 |
} |
| 56 |
SECTION("replace no chars") { |
| 57 |
CHECK_FALSE(Catch::replaceInPlace(letters, "x", "z")); |
| 58 |
CHECK(letters == letters); |
| 59 |
} |
| 60 |
SECTION("no replace in already-replaced string") { |
| 61 |
SECTION("lengthening") { |
| 62 |
CHECK(Catch::replaceInPlace(letters, "c", "cc")); |
| 63 |
CHECK(letters == "abccdefccg"); |
| 64 |
} |
| 65 |
SECTION("shortening") { |
| 66 |
std::string s = "----"; |
| 67 |
CHECK(Catch::replaceInPlace(s, "--", "-")); |
| 68 |
CHECK(s == "--"); |
| 69 |
} |
| 70 |
} |
| 71 |
SECTION("escape '") { |
| 72 |
std::string s = "didn't"; |
| 73 |
CHECK(Catch::replaceInPlace(s, "'", "|'")); |
| 74 |
CHECK(s == "didn|'t"); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
TEST_CASE("splitString", "[string-manip]") { |
| 79 |
using namespace Catch::Matchers; |
| 80 |
using Catch::splitStringRef; |
| 81 |
using Catch::StringRef; |
| 82 |
|
| 83 |
CHECK_THAT(splitStringRef("", ','), Equals(std::vector<StringRef>())); |
| 84 |
CHECK_THAT(splitStringRef("abc", ','), Equals(std::vector<StringRef>{"abc"})); |
| 85 |
CHECK_THAT(splitStringRef("abc,def", ','), Equals(std::vector<StringRef>{"abc", "def"})); |
| 86 |
} |
| 87 |
|
| 88 |
TEST_CASE("startsWith", "[string-manip]") { |
| 89 |
using Catch::startsWith; |
| 90 |
|
| 91 |
CHECK_FALSE(startsWith("", 'c')); |
| 92 |
CHECK(startsWith(std::string("abc"), 'a')); |
| 93 |
CHECK(startsWith("def"_catch_sr, 'd')); |
| 94 |
} |