cucumber / cucumber-cpp

Support for writing Cucumber step definitions in C++
MIT License
306 stars 131 forks source link

Support taking regex captures as arguments to the step definition's function #159

Closed muggenhor closed 7 years ago

muggenhor commented 7 years ago

This feature depends on the availability of C++11 (or more recent). It's main purpose is to support the transformation from code such as this:

GIVEN("some (\\d+) and a \"([^\"]+)\"") {
    REGEX_PARAM(unsigned, number);
    REGEX_PARAM(std::string, string);
}

to use the more familiar syntax present in this code:

GIVEN("some (\\d+) and a \"([^\"]+)\"", (const unsigned number, const std::string& string)) {
}

Currently all captures from the regex that aren't passed as function parameters can still be extracted by means of the REGEX_PARAM macro.

If C++11 is unavailable then this feature is just unavailable and all existing code should continue to work as is.

In the future it may be desirable to check, at compile-time, that the amount of captures in the regex equals the amount of function parameters. I have some prototype code I've been playing with that suggests this is relatively doable. It currently depends on "relaxed constexpr" support (C++14), constexpr lambdas (C++ 17) and "constexpr if". I may be able to rewrite this prototype to rely on just the C++14 features, but that's for the future. Heck, it may even be possible to use recursion and rely only on C++11, but that's bound to produce very difficult to read code.

konserw commented 7 years ago

Looks very good to me 👍 I'm also happy to see modern C++ features in good use.