Skip to content

Commit

Permalink
feat: matches::str::eq for case-insensitively comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
DNKpp committed Oct 4, 2024
1 parent 7b25fd5 commit 419c659
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
35 changes: 35 additions & 0 deletions include/mimic++/Matcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,17 @@ namespace mimicpp::matches
*/
}

namespace mimicpp
{
/**
* \brief Tag type, used in string matchers.
* \ingroup EXPECTATION_MATCHERS_STRING
*/
struct case_insensitive_t
{
} constexpr case_insensitive{};
}

namespace mimicpp::matches::str
{
/**
Expand Down Expand Up @@ -458,6 +469,30 @@ namespace mimicpp::matches::str
};
}

/**
* \brief Tests, whether the target string compares case-insensitively equal to the expected string.
* \tparam String The string type.
* \param pattern The pattern object.
*/
template <typename String>
[[nodiscard]]
constexpr auto eq(String&& pattern, [[maybe_unused]] const case_insensitive_t)
{
using pattern_t = std::invoke_result_t<decltype(to_lower), String>;
return PredicateMatcher{
[]<lower_convertible T>(const pattern_t& target, T&& exp)
requires std::equality_comparable_with<pattern_t, std::invoke_result_t<decltype(to_lower), T>>
{
return target == mimicpp::to_lower(std::forward<T>(exp));
},
"is case-insensitively equal to {}",
"is case-insensitively not equal to {}",
std::tuple{
mimicpp::to_lower(std::forward<String>(pattern))
}
};
}

/**
* \}
*/
Expand Down
69 changes: 69 additions & 0 deletions test/unit-tests/StringMatchers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
#include "mimic++/Matcher.hpp"

#include <catch2/catch_template_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <catch2/generators/catch_generators_range.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>

#include <array>

namespace matches = mimicpp::matches;

TEST_CASE(
Expand Down Expand Up @@ -154,3 +158,68 @@ TEMPLATE_TEST_CASE(
std::u32string{U"Hello, World!"},
std::u32string{U"Hello, WOrld!"});
}

TEST_CASE(
"matches::str::eq supports case-insensitive comparison for char-strings.",
"[matcher][matcher::str]"
)
{
static constexpr std::array matches = std::to_array<const char*>(
{
"Hello, World!",
"hello, World!",
"Hello, world!",
"HelLo, world!"
});

static constexpr std::array mismatches = std::to_array<const char*>(
{
" hello, world!",
"hello, world! ",
"hello,world!"
});

const auto matcher = matches::str::eq(
"Hello, World!",
mimicpp::case_insensitive);
REQUIRE_THAT(
matcher.describe(),
Catch::Matchers::Equals("is case-insensitively equal to \"hello, world!\""));

SECTION("When target is equal, they match.")
{
const std::string target = GENERATE(from_range(matches));

REQUIRE(matcher.matches(target));
}

SECTION("When target is not equal, they do not match.")
{
const std::string target = GENERATE(from_range(mismatches));

REQUIRE(!matcher.matches(target));
}

SECTION("Matcher can be inverted.")
{
const auto invertedMatcher = !matches::str::eq("Hello, World!");

REQUIRE_THAT(
invertedMatcher.describe(),
Catch::Matchers::Equals("is case-insensitively not equal to \"hello, world!\""));

SECTION("When target is equal, they do not match.")
{
const std::string target = GENERATE(from_range(matches));

REQUIRE(!invertedMatcher.matches(target));
}

SECTION("When target is not equal, they do match.")
{
const std::string target = GENERATE(from_range(mismatches));

REQUIRE(invertedMatcher.matches(target));
}
}
}

0 comments on commit 419c659

Please sign in to comment.