| 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("escape '") { |
| 61 | std::string s = "didn't"; |
| 62 | CHECK(Catch::replaceInPlace(s, "'", "|'")); |
| 63 | CHECK(s == "didn|'t"); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | TEST_CASE("splitString", "[string-manip]") { |
| 68 | using namespace Catch::Matchers; |
| 69 | using Catch::splitStringRef; |
| 70 | using Catch::StringRef; |
| 71 | |
| 72 | CHECK_THAT(splitStringRef("", ','), Equals(std::vector<StringRef>())); |
| 73 | CHECK_THAT(splitStringRef("abc", ','), Equals(std::vector<StringRef>{"abc"})); |
| 74 | CHECK_THAT(splitStringRef("abc,def", ','), Equals(std::vector<StringRef>{"abc", "def"})); |
| 75 | } |
| 76 | |
| 77 | TEST_CASE("startsWith", "[string-manip]") { |
| 78 | using Catch::startsWith; |
| 79 | |
| 80 | CHECK_FALSE(startsWith("", 'c')); |
| 81 | CHECK(startsWith(std::string("abc"), 'a')); |
| 82 | CHECK(startsWith("def"_catch_sr, 'd')); |
| 83 | } |