-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: named capture groups #206
Comments
If we decide to extend Cucumber expressions, which ever syntax we pick, we should take some care to ensure that we don't collide with existing usage. For this example, people may already use I can also image that Behat users would like to continue to use their turnip expressions. How would be facilitate this? The one thing we can probably do without breaking things is make the |
This part's fine, they'd just import a different attribute (~= annotation in Java) I think we can restrict it to attributes rather than the older comment-style annotations I showed above: use Behat\Given as OldGiven;
use Cucumber\Given;
...
#[Given('I eat {string}')]
#[OldGiven('I eat :fruit')] |
Agreed, it doesn't look as if the grammar restricts it at all so it may be tricky to find a syntax |
Thinking about it, the grammar doesn't restrict what chars are allowed but I bet if we fuzz the existing parser implementations a little we can find some chars that aren't currently allowed :) My pref would be I'd quite like something along the lines of:
Even if we break backwards compatibility we could do that in an Expressions major (depending on the impact that has across different package manager ecosystems) |
I would prefer to keep all the syntax inside the
Now I really don't see a way to make this graceful, but if we release this with a feature toggle, we can also smooth out other migration problems i.e. I'd like to use this in Cucumber JVM but not enable this until the next major of Cucumber-JVM. Otherwise all future patches get stuck behind this breaking change. |
Nice :) FYI The way we do it in behat is we match on name first and then on position, but there are some awkward edge cases
(we also populate some arguments from a DI container) We don't currently implement type-based matching though and I'm wondering if that'd be a nice feature to add, given Cucumber Expressions can capture value object types explicitly |
@mpkorstanje a feature toggle is a GREAT idea to avoid the BC issue entirely |
If it's feature-toggled perhaps I'll pilot this in the PHP implementation? |
Ah fair enough. I'll stick to positional until someone asks for it. Anyway, what about |
I think that's |
Sure, but how would it or the equivalent turnip or regex with named groups map? How would a regex with named and unnamed groups match? |
I just want to give this a big 👍 Seems like it could open up some interesting possibilities. |
I would really like to have this feature too. Having named arguments prevents many mistakes when just ordering is used, especially early on when the expressions change often. As for having multiple definitions for the same name - I think that this should be an error. Regex from my experience also fails to compile when there are multiple capture groups with the same name. And I also think that we cucumber could disallow mixing named and positional arguments. One expression would have to commit to one or the other. I personally don't see a scenario where that would cause issues. Otherwise I am also in favor of the As an aside, what would have to happen now for this to move forward? Is there some formal RFC process or does this just need implementation? |
Following a discussion about implementing cucumber expressions into the pytest-bdd implementation, we rely on having named parameters/args. For us, a format of {label:type} matches what existing parsers (cfparse/parse) in use by behave and pytest-bdd do, so would make the transition much smoother. I am happy to implement this in Python as at the moment it would be a blocker without this feature after a discussion on Discord. I don't want to just whack a solution in though if there is a due process and without advice from those who developed the expressions and have knowledge of the best solution. I am also happy to draw up a draft PR to form the basis of a discussion if this helps. The decisions that look like we need to decide that I can elicit so far:
|
@jsa34 Instead of changing the design of |
My response to these points from a python and personal position:
|
This is helpful to know - thank you! I don't think there is any harm in revisiting; things have moved on, I would argue, since the original discussion, as there is now an established cucumber expressions implementation and there is a need/desire for this feature judging by the pytest-bdd discussions and other members for others uses in this thread. We also shouldn't be limited because a historical decision was made - it's healthy to understand why it was rejected to re-evaluate, so any link or reference to why this was would be extremely helpful. Regardless, I think it's important to be able to reconsider all options. I agree there could be multiple parsers, but the aim I always felt was for Cucumber Expressions to negate the need in most cases to dance between parsers that weren't necessarily specifically designed for gherkin language. I also am conscious of consensus amongst languages as this is language-agnostic (i.e. the format would be the same for all languages), but also conscious that there is a strong existing python precedence for type against arg, especially in strings, and may make it frustrating and extremely counterintuitive if it broke that pattern. We also have no current named args, so whatever choice it will change the design - that's a natural evolution, otherwise this feature will never get implemented and we might as well close anything that involves changing the design to add features |
@jsa34 I think this tension between consensus and innovation is something we need to be aware of, and not let it hold us back. One source of friction from aiming for consistent language implementations is capacity - if you drive this forward for python, it applies pressure to implement the same behaviour in other language implementations. I would encourage you to experiment with this in python so that we can learn from a real implementation. If there are technical barriers, e.g. with releasing a new python version of cucumber-expressions with more features that the other implementations, let's talk about that. I'm sure that's not insurmountable. |
@jsa34 is your goal to label arguments to facilitate the use of Cucumber expressions in Python or to make Cucumber expressions compatible with the existing parsers and expressions? I don't think the latter is feasible or realistic.
I think for Cucumber expressions the only thing that matters is the output format. Formerly a list of arguments, now a list of possibly labeled arguments. How this output is used to invoke a step definition and with what checks should be left to the Cucumber implementation.
These would have the anonymous type and in strongly typed languages take their type from the methods parameter. If I recall this correctly this too is part of the interface, so the Python Cucumber implementation can always pick a string. |
@mpkorstanje the question is more - should there be a standardised format for expressing label and type on cucumber expressions across all languages? For example, would we permit Python to use the format: These are just random examples, however I noted the original suggested format was akin to the Python example, and this feels like the natural format based on Python conventions and is the format currently in use for existing parsers python users have to use for Behave and Pytest-BDD, whereas it as suggested later to use the "Java" example. (I am not proposing Java wants to use this format - it is merely an illustrative example) |
Okay. I think the difference in what you are proposing is mostly stylistic then. And after looking at some languages in a very non-rigorous manner when a So I would be okay with:
The character set to be used in labels and types would have to be restricted and may not include the And the output of matching would be: ---
expression: "{}"
text: '0.22'
expected_args:
- value: '0.22'
label: null
---
expression: "{}"
text: '0.22'
type_hint:
- int
expected_args:
- value: 0.22
label: null
---
expression: "{int}"
text: '0.22'
expected_args:
- value: 0.22
label: null
---
expression: "{x:}"
text: '0.22'
expected_args:
- value: '0.22'
label: 'x'
---
expression: "{x:int}"
text: '0.22'
expected_args:
- value: 0.22
label: 'x' How the args are matched with a step definition would be up to the Cucumber implementation. Questions:
|
@mpkorstanje that's a much clearer summary than I gave - thank you! My opinions on the questions:
|
I've been looking at how cucumber-expressions could be adopted in Behat.
One feature of our existing two pattern syntaxes (regex and turnip) is that they can name the arguments.
e.g.
This allows Behat to match arguments based on name as well as position, letting users transpose the argument order if necessary.
That can be useful if multiple patterns are attached to one step, something allowed in Behat:
The new feature would be for Cucumber Expressions to capture argument names:
{fruit:string}
)Then it would be up to the Cucumber implementation to either use the names for matching to the step definition, or to ignore them and use the order directly as currently happens.
The text was updated successfully, but these errors were encountered: