Skip to content

Commit

Permalink
fix: inplace matchers shall never treat two raw-pointers as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
DNKpp committed Jan 27, 2025
1 parent 5c48abb commit a248d7e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions include/mimic++/ExpectationBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,12 @@ namespace mimicpp::detail
return std::forward<Arg>(arg);
}

// if the param is a character-pointer, there is no evidence, whether it denotes a null-terminated string or just an
// actual pointer to a value.
// But, the Mock user shall know it, thus if `Arg` is not a character-pointer, we enable this matcher.
template <string Param, string Arg>
requires(!std::is_pointer_v<std::remove_reference_t<Param>>)
|| (!std::is_pointer_v<std::remove_reference_t<Arg>>)
[[nodiscard]]
constexpr auto make_arg_matcher(Arg&& arg, [[maybe_unused]] const priority_tag<1>)
{
Expand Down
18 changes: 16 additions & 2 deletions test/unit-tests/Mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,20 @@ TEST_CASE(
Mock<void(const char*)> mock{};
SCOPED_EXP mock.expect_call("Hello, World!");
mock("Hello, World!");

SECTION("If both, param and argument, are character pointers, they are not treated as strings.")
{
constexpr std::array<char, 2> value1{42, 0};
constexpr std::array<char, 2> value2{42, 0};

// If the second expectation is matched, then we know, that the argument has been treated as a string.
// Therefore, the first expectation must be chosen, which compares the actual pointers.
SCOPED_EXP mock.expect_call(matches::eq(value2.data()));
SCOPED_EXP mock.expect_call(value1.data())
and expect::at_least(0u);

mock(value2.data());
}
}

SECTION("With explicit matchers.")
Expand All @@ -926,7 +940,7 @@ TEST_CASE(
Mock<void()> mock{
MockSettings{.name = "MyMock"}};
const ScopedExpectation expectation = mock.expect_call()
and expect::never();
and expect::never();

REQUIRE_THAT(
expectation.mock_name(),
Expand All @@ -939,7 +953,7 @@ TEST_CASE(
{
Mock<void()> mock{};
const ScopedExpectation expectation = mock.expect_call()
and expect::never();
and expect::never();

REQUIRE_THAT(
expectation.mock_name(),
Expand Down

0 comments on commit a248d7e

Please sign in to comment.