Skip to content

Commit

Permalink
Match regular expressions to string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
rollbear committed Jul 20, 2023
1 parent e9ee31c commit 63bae2b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 11 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Support matching regular expressions to std::string view

* Added adapter for QA Systems Cantata. Thank you Andreas Schätti

v44 2023-04-10
Expand Down
43 changes: 32 additions & 11 deletions include/trompeloeil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2555,28 +2555,50 @@ template <typename T>
class string_helper // a vastly simplified string_view type of class
{
public:
template <
typename S,
typename = decltype(std::declval<const char*&>() = std::declval<S>().data()),
typename = decltype(std::declval<S>().length())
>
string_helper(
std::string const& s)
const S& s)
noexcept
: str(s.c_str())
: begin_(s.data())
, end_(begin_ + s.length())
{}

constexpr
string_helper(
char const* s)
noexcept
: str(s)
{}
: begin_(s)
, end_(s ? begin_ + strlen(s) : nullptr)
{
}

char const*
c_str()
constexpr
explicit
operator bool() const
{
return begin_;
}
constexpr
char const *
begin()
const
{
return begin_;
}
constexpr
char const *
end()
const
noexcept
{
return str;
return end_;
}
private:
char const* str;
char const* begin_;
char const* end_;
};

regex_check(
Expand All @@ -2593,8 +2615,7 @@ template <typename T>
T const&)
const
{
return str.c_str()
&& std::regex_search(str.c_str(), re, match_type);
return str && std::regex_search(str.begin(), str.end(), re, match_type);
}

private:
Expand Down
3 changes: 3 additions & 0 deletions test/compiling_tests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ class mock_str
MAKE_MOCK1(str, void(std::string));
MAKE_MOCK1(overload, void(char const*));
MAKE_MOCK1(overload, void(std::string const&));
#if defined(__cpp_lib_string_view)
MAKE_MOCK1(string_view, void(std::string_view));
#endif
};

class C_ptr
Expand Down
44 changes: 44 additions & 0 deletions test/compiling_tests_11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2918,6 +2918,50 @@ TEST_CASE_METHOD(

// tests of parameter matching using typed matcher re

#if TROMPELOEIL_TEST_REGEX_FAILURES && defined(__cpp_lib_string_view)
TEST_CASE_METHOD(
Fixture,
"C++11: call to string_view function matching regex is not reported",
"[C++11][C++14][matching][matchers][re]")
{
{
mock_str obj;
REQUIRE_CALL_V(obj, string_view(trompeloeil::re("mid")));
char str[] = "pre mid post";
obj.string_view(str);
}
REQUIRE(reports.empty());
}
#endif
#if TROMPELOEIL_TEST_REGEX_FAILURES && defined(__cpp_lib_string_view)
TEST_CASE_METHOD(
Fixture,
"C++11: call to string_view function with non-matching string to regex is reported",
"[C++11][C++14][matching][matchers][re]")
{
mock_str obj;
REQUIRE_CALL_V(obj, string_view(trompeloeil::re("mid")));
try
{
char str[] = "abcde";
obj.string_view(str);
FAIL("did not throw");
}
catch (reported)
{
REQUIRE(reports.size() == 1U);
auto& msg = reports.front().msg;
INFO("msg=" << msg);
auto re = R":(No match for call of string_view with signature void\(std::string_view\) with.
param _1 == abcde
Tried obj.string_view\(trompeloeil::re\("mid"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.*
Expected _1 matching regular expression /mid/):";
REQUIRE(is_match(msg, re));
}
}
#endif

#if TROMPELOEIL_TEST_REGEX_FAILURES

TEST_CASE_METHOD(
Expand Down

0 comments on commit 63bae2b

Please sign in to comment.