StringManip.tests.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_test_macros.hpp>
  7. #include <catch2/matchers/catch_matchers_vector.hpp>
  8. #include <catch2/internal/catch_string_manip.hpp>
  9. static const char * const no_whitespace = "There is no extra whitespace here";
  10. static const char * const leading_whitespace = " \r \t\n There is no extra whitespace here";
  11. static const char * const trailing_whitespace = "There is no extra whitespace here \t \n \r ";
  12. static const char * const whitespace_at_both_ends = " \r\n \t There is no extra whitespace here \t\t\t \n";
  13. TEST_CASE("Trim strings", "[string-manip]") {
  14. using Catch::trim; using Catch::StringRef;
  15. static_assert(std::is_same<std::string, decltype(trim(std::string{}))>::value, "Trimming std::string should return std::string");
  16. static_assert(std::is_same<StringRef, decltype(trim(StringRef{}))>::value, "Trimming StringRef should return StringRef");
  17. REQUIRE(trim(std::string(no_whitespace)) == no_whitespace);
  18. REQUIRE(trim(std::string(leading_whitespace)) == no_whitespace);
  19. REQUIRE(trim(std::string(trailing_whitespace)) == no_whitespace);
  20. REQUIRE(trim(std::string(whitespace_at_both_ends)) == no_whitespace);
  21. REQUIRE(trim(StringRef(no_whitespace)) == StringRef(no_whitespace));
  22. REQUIRE(trim(StringRef(leading_whitespace)) == StringRef(no_whitespace));
  23. REQUIRE(trim(StringRef(trailing_whitespace)) == StringRef(no_whitespace));
  24. REQUIRE(trim(StringRef(whitespace_at_both_ends)) == StringRef(no_whitespace));
  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. TEST_CASE("splitString", "[string-manip]") {
  59. using namespace Catch::Matchers;
  60. using Catch::splitStringRef;
  61. using Catch::StringRef;
  62. CHECK_THAT(splitStringRef("", ','), Equals(std::vector<StringRef>()));
  63. CHECK_THAT(splitStringRef("abc", ','), Equals(std::vector<StringRef>{"abc"}));
  64. CHECK_THAT(splitStringRef("abc,def", ','), Equals(std::vector<StringRef>{"abc", "def"}));
  65. }
  66. TEST_CASE("startsWith", "[string-manip]") {
  67. using Catch::startsWith;
  68. CHECK_FALSE(startsWith("", 'c'));
  69. CHECK(startsWith(std::string("abc"), 'a'));
  70. CHECK(startsWith("def"_catch_sr, 'd'));
  71. }