1 #include "internal/catch_string_manip.h" 2 3 #include "catch.hpp" 4 5 static const char * const no_whitespace = "There is no extra whitespace here"; 6 static const char * const leading_whitespace = " \r \t\n There is no extra whitespace here"; 7 static const char * const trailing_whitespace = "There is no extra whitespace here \t \n \r "; 8 static const char * const whitespace_at_both_ends = " \r\n \t There is no extra whitespace here \t\t\t \n"; 9 10 TEST_CASE("Trim strings", "[string-manip]") { 11 using Catch::trim; using Catch::StringRef; 12 static_assert(std::is_same<std::string, decltype(trim(std::string{}))>::value, "Trimming std::string should return std::string"); 13 static_assert(std::is_same<StringRef, decltype(trim(StringRef{}))>::value, "Trimming StringRef should return StringRef"); 14 15 REQUIRE(trim(std::string(no_whitespace)) == no_whitespace); 16 REQUIRE(trim(std::string(leading_whitespace)) == no_whitespace); 17 REQUIRE(trim(std::string(trailing_whitespace)) == no_whitespace); 18 REQUIRE(trim(std::string(whitespace_at_both_ends)) == no_whitespace); 19 20 REQUIRE(trim(StringRef(no_whitespace)) == StringRef(no_whitespace)); 21 REQUIRE(trim(StringRef(leading_whitespace)) == StringRef(no_whitespace)); 22 REQUIRE(trim(StringRef(trailing_whitespace)) == StringRef(no_whitespace)); 23 REQUIRE(trim(StringRef(whitespace_at_both_ends)) == StringRef(no_whitespace)); 24 } 25 26 TEST_CASE("replaceInPlace", "[string-manip]") { 27 std::string letters = "abcdefcg"; 28 SECTION("replace single char") { 29 CHECK(Catch::replaceInPlace(letters, "b", "z")); 30 CHECK(letters == "azcdefcg"); 31 } 32 SECTION("replace two chars") { 33 CHECK(Catch::replaceInPlace(letters, "c", "z")); 34 CHECK(letters == "abzdefzg"); 35 } 36 SECTION("replace first char") { 37 CHECK(Catch::replaceInPlace(letters, "a", "z")); 38 CHECK(letters == "zbcdefcg"); 39 } 40 SECTION("replace last char") { 41 CHECK(Catch::replaceInPlace(letters, "g", "z")); 42 CHECK(letters == "abcdefcz"); 43 } 44 SECTION("replace all chars") { 45 CHECK(Catch::replaceInPlace(letters, letters, "replaced")); 46 CHECK(letters == "replaced"); 47 } 48 SECTION("replace no chars") { 49 CHECK_FALSE(Catch::replaceInPlace(letters, "x", "z")); 50 CHECK(letters == letters); 51 } 52 SECTION("escape '") { 53 std::string s = "didn't"; 54 CHECK(Catch::replaceInPlace(s, "'", "|'")); 55 CHECK(s == "didn|'t"); 56 } 57 } 58 59 TEST_CASE("splitString", "[string-manip]") { 60 using namespace Catch::Matchers; 61 using Catch::splitStringRef; 62 using Catch::StringRef; 63 64 CHECK_THAT(splitStringRef("", ','), Equals(std::vector<StringRef>())); 65 CHECK_THAT(splitStringRef("abc", ','), Equals(std::vector<StringRef>{"abc"})); 66 CHECK_THAT(splitStringRef("abc,def", ','), Equals(std::vector<StringRef>{"abc", "def"})); 67 } 68