ttutisani/Xunit.Gherkin.Quick

Enable cucumber expressions

JornWildt opened this issue ยท 14 comments

Cucumber expression https://github.com/cucumber/cucumber-expressions#readme allows you to write predefined "regex" words such as {word} or {float}.

Unfortunately, Xunit.Gherkin.Quick only understands .NET regex.

To make thinks more complicated, auto completers such as https://github.com/alexkrechik/VSCucumberAutoComplete understands cucumber expressions but not .NET regex.

Please enable the use of cucumber expressions as it would make tools such as VSCucumberAutoComplete much more useful.

Definitely an interesting feature of cucumber. On that page, I see they show examples in various programming languages but not C#. I wonder if there is a .net nuget package from cucumber that can parse those expressions and match them against the methods. Otherwise, implementing this will be necessary from scratch.

On that page, I see ...

Asuming you refer to VSCucumberAutoComplete then, yes, C# is not supported out of the box. But VSCucumberAutoComplete scans source code files (any text files) for regex patterns that largely hits the C# attributes in Xunit.Gherkin.Quick.

I cannot (so far) get VSCucumberAutoComplete to work with the .NET regex patterns, but otherwise it works okay and will recognize Given/When/etc. as long as you do not put regex patterns into them.

If Xunit.Gherkin.Quick supported the cucumber expressions then I can get VSCucumberAutoComplete to insert snippets with placeholdes for the expressions - and I assume Xunit.Gherkin.Quick could rather easily convert {float} etc. to corresponding regex patterns before parsing the input strings.

On the other hand, if you know of another VS Code extension for autocompletion with steps from Xunit.Gherkin.Quick C# attributes, then I would be happy to use that instead :-)

Thanks for all that info, very helpful either way! I was asking if there is a cucumber package that helps parse those cucumber expressions. For example, for parsing Gherkin language, I used Gherkin nuget package from the framework, which simplified my task, instead of implementing the parsing from scratch. Same goes for Regex parsing - .NET does that out of the box. So I wonder if there is a cucumber expression parser out there that could be used instead of implementing it from scratch.

I'm not fluent in any of the packages here, but it seems like the C# Gherkin parser I assume you are using (https://github.com/cucumber/common/tree/main/gherkin/dotnet) simply returns the text of the steps (https://github.com/cucumber/common/blob/main/gherkin/dotnet/Gherkin/Ast/Step.cs) - I don't know how you then match that text with any of the C# Given/When/etc. attributes, but I assume you convert the attribute text to regex patterns and then match that to the text from the Gherkin step text?

If so, I would assume you could transform/string-replace {word} into (\w+) in the attribute text right before you do the regex match? Would that work?

Seems like BaseStepDefinitionAttribute is the place:

    internal Match MatchRegex(string text)
    {
        return Regex.Match(text.Trim(), Pattern, RegexOptions.IgnoreCase);
    }

Replace {word} with (\w+) and {int} with (\d+) etc. right before the match - and I assume it might work :-)

Or maybe it is in StepMethodInfo ๐Ÿคทโ€โ™‚๏ธ:

public ScenarioStepPattern GetMatchingPattern(Step gherkinScenarioStep)

  var match = Regex.Match(gherkinStepText, pattern.Pattern);

Yes, the first one you found is a dead code, and it must be removed to avoid such confusion. StepMethodInfo is where the matching happens.

If you feel like it, feel free to submit the PR with tests. ๐Ÿ˜‰

If you feel like it, feel free to submit the PR with tests.

You got it :-)

#122

Thanks, reviewed it. Good job with the first draft!

The PR is merged. Very clean change so I'm satisfied! ๐Ÿ™‚

I hate to ask you too much, but would you be able to update this documentation to explain the cucumber expressions?

https://github.com/ttutisani/Xunit.Gherkin.Quick/blob/master/docs/step-attributes.md

Update it the way you see fit. One way I am thinking would be to add a separate section, so we will have one for regex and one for cucumber approach. But if you want to combine them that is also fine. The document already explains the regex usage, but it does not have the information about the cucumber expressions, of course.

After that, I can release the nuget new version with your changes.

Sure, I'll take a stab at it.

Congrats! Release 4.2.0 is out.

Good job, @JornWildt! this is all your work! ๐Ÿ˜„

(I will be closing this issue)

Nuget is published with the change addressing this issue.

Cool, thank you.