Skip to content
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

[Java] Add @ParameterType(useRegexpMatchAsStrongTypeHint=true/false) #1914

Merged
merged 2 commits into from
Mar 6, 2020

Conversation

mpkorstanje
Copy link
Contributor

@mpkorstanje mpkorstanje commented Mar 6, 2020

Summary

Parameter types are registered with a regex. When a step definition is written
as a regular expression cucumber-expressions use the regex in the capture
group as a type hint.

When the type hint provided by the regex and the type hint provided by the
method (i.e. the types of its arguments) disagree about the type
cucumber-expressions prefers the hint provided by the regex.

When parameter types are declared with very simple regexes this may cause a
problem. For example:

import io.cucumber.java.ParameterType;
import io.cucumber.java.en.Given;

public class StepDefinitions {

    private static class TypeA {}

    @ParameterType("\\w+")
    public TypeA typeA(String a) {
        return new TypeA();
    }

    @Given("a cucumber expression of {typeA}")
    public void an_object_of_type_a(TypeA a) {
        // works fine!
    }

    @Given("^a regular expression of (\\w+)$")
    public void an_object_of_type_b(String b) {
        // broken: will attempt to use the transformer for TypeA
    }
}

By using @ParameterType(pattern="\\w+", useRegexpMatchAsStrongTypeHint=false)
this behaviour can be changed to instead prefer the type hint from the method.

Motivation and Context

cucumber/common#918 (comment)

Types of changes

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).

Checklist:

  • I've added tests for my code.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

Parameter types are registered with a regex. When a step definition is written
as a regular expression `cucumber-expressions` use the regex in the capture
group as a type hint.

When the type hint provided by the regex and the type hint provided by the
method (i.e. the types of its arguments) disagree about the type
`cucumber-expressions` prefers the hint provided by the regex.

When parameter types are declared with very simple regexes this may cause a
problem. For example:

```
import io.cucumber.java.ParameterType;
import io.cucumber.java.en.Given;

public class Main {

    private static class TypeA {}

    @ParameterType("\\w+")
    public TypeA typeA(String a) {
        return new TypeA();
    }

    @given("a cucumber expression of {typeA}")
    public void an_object_of_type_a(TypeA a) {
        // works fine!
    }

    @given("^a regular expression of (\\w+)$")
    public void an_object_of_type_b(String b) {
        // broken: will attempt to use the transformer for TypeA
    }
}
```

By using `@ParameterType(pattern="\\w+", useRegexpMatchAsStrongTypeHint=false)`
this behaviour can be changed to instead prefer the type hint from the method.
@coveralls
Copy link

coveralls commented Mar 6, 2020

Coverage Status

Coverage increased (+0.002%) to 85.53% when pulling e5fabf8 on use-regexp-match-as-strong-type-hint into c13a345 on master.

@mpkorstanje mpkorstanje merged commit 95e0f82 into master Mar 6, 2020
@mpkorstanje mpkorstanje deleted the use-regexp-match-as-strong-type-hint branch March 6, 2020 13:41
mpkorstanje added a commit that referenced this pull request Apr 30, 2020
Regular expressions providing strong type hints turn out to be rather annoying
when mixing cucumber expressions and regular expressions. Especially when the
patterns involved are short and common.

See: #1914, cucumber/common#918

Closes: #1915
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants