rollbear / trompeloeil

Header only C++14 mocking framework
Boost Software License 1.0
808 stars 86 forks source link

regex matching `std::string_view` #304

Closed dvirtz closed 1 year ago

dvirtz commented 1 year ago

Currently trompeloeil::re only supports std::string or C-style strings. Trying to use it with a std::string_view parameter results in the error

error: cannot convert 'trompeloeil::predicate_matcher<trompeloeil::lambdas::regex_check, trompeloeil::lambdas::regex_printer, trompeloeil::duck_typed_matcher<trompeloeil::lambdas::regex_check, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >' to 'trompeloeil::param_list_t<void(std::basic_string_view<char>), 0>' {aka 'std::basic_string_view<char>'}
rollbear commented 1 year ago

Thank you for reporting. This is because the standard library regexes only supports those. I'll see what I can do. I believe it can be done without too much trouble.

rollbear commented 1 year ago

Can you try branch string_view_re and report back how it works?

dvirtz commented 1 year ago

Looks good. This is the test I did:

#include <trompeloeil.hpp>
#include <string_view>
#include <iostream>

struct S
{
  MAKE_MOCK1(func, void(std::string_view));
};

int main() {
  S s;
  ALLOW_CALL(s, func(trompeloeil::re("3")));
  s.func("3");
  try {
    s.func("4");
  } catch (const std::exception& ex) {
    std::cerr << "error: " << ex.what() << '\n';
  }
}

output is:

error: 
No match for call of func with signature void(std::string_view) with.
  param  _1 == 4

Tried s.func(trompeloeil::re("3")) at C:\Users\dyitzchaki\github\conan-center-index\recipes\trompeloeil\all\test_package\test_package.cpp:12
  Expected  _1 matching regular expression /3/
rollbear commented 1 year ago

I'm traveling right now, but I'll try to find the time to squeeze in a release tag soon.

rollbear commented 1 year ago

Tagged v45 which includes this fix, so closing now.