StringManip.tests.cpp 2.8 KB

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