diff --git a/ChangeLog b/ChangeLog index 3ad4cd48..a45c8907 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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 diff --git a/include/trompeloeil.hpp b/include/trompeloeil.hpp index c99c6519..d418ec09 100644 --- a/include/trompeloeil.hpp +++ b/include/trompeloeil.hpp @@ -2555,28 +2555,50 @@ template class string_helper // a vastly simplified string_view type of class { public: + template < + typename S, + typename = decltype(std::declval() = std::declval().data()), + typename = decltype(std::declval().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( @@ -2593,8 +2615,7 @@ template 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: diff --git a/test/compiling_tests.hpp b/test/compiling_tests.hpp index fcc4509f..9bbcdfd9 100644 --- a/test/compiling_tests.hpp +++ b/test/compiling_tests.hpp @@ -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 diff --git a/test/compiling_tests_11.cpp b/test/compiling_tests_11.cpp index ee95859d..57b5f3b0 100644 --- a/test/compiling_tests_11.cpp +++ b/test/compiling_tests_11.cpp @@ -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(