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

Cucumber expressions 600 match integers as floats #605

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cucumber-expressions/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Removed

### Fixed

* Match integer strings as `{float}`.
([#600](https://github.com/cucumber/cucumber/issues/600)
[#605](https://github.com/cucumber/cucumber/pull/605)
[aslakhellesoy]
[vincent-psarga])

* Allow `?!` as a non-capturing regex
([#481](https://github.com/cucumber/cucumber/issues/576),
[#593](https://github.com/cucumber/cucumber/pull/593)
Expand Down Expand Up @@ -593,4 +600,5 @@ N/A
[savkk]: https://github.com/savkk
[spicalous]: https://github.com/spicalous
[tooky]: https://github.com/tooky
[tommywo]: https://github.com/tommywo
[tommywo]: https://github.com/tommywo
[vincent-psarga]: https://github.com/vincent-psarga
21 changes: 10 additions & 11 deletions cucumber-expressions/go/cucumber_expression_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestCucumberExpressionGeneratory(t *testing.T) {
false,
)
require.NoError(t, err)
parameterTypeRegistry.DefineParameterType(currencyParameterType)
require.NoError(t, parameterTypeRegistry.DefineParameterType(currencyParameterType))

assertExpressionWithParameterTypeRegistry(
t,
Expand All @@ -116,7 +116,7 @@ func TestCucumberExpressionGeneratory(t *testing.T) {
false,
)
require.NoError(t, err)
parameterTypeRegistry.DefineParameterType(parameterType1)
require.NoError(t, parameterTypeRegistry.DefineParameterType(parameterType1))
parameterType2, err := NewParameterType(
"type2",
[]*regexp.Regexp{regexp.MustCompile("bc")},
Expand All @@ -126,7 +126,7 @@ func TestCucumberExpressionGeneratory(t *testing.T) {
false,
)
require.NoError(t, err)
parameterTypeRegistry.DefineParameterType(parameterType2)
require.NoError(t, parameterTypeRegistry.DefineParameterType(parameterType2))

assertExpressionWithParameterTypeRegistry(
t,
Expand All @@ -150,7 +150,7 @@ func TestCucumberExpressionGeneratory(t *testing.T) {
false,
)
require.NoError(t, err)
parameterTypeRegistry.DefineParameterType(parameterType1)
require.NoError(t, parameterTypeRegistry.DefineParameterType(parameterType1))
parameterType2, err := NewParameterType(
"type2",
[]*regexp.Regexp{regexp.MustCompile("x")},
Expand All @@ -160,7 +160,7 @@ func TestCucumberExpressionGeneratory(t *testing.T) {
false,
)
require.NoError(t, err)
parameterTypeRegistry.DefineParameterType(parameterType2)
require.NoError(t, parameterTypeRegistry.DefineParameterType(parameterType2))
generator := NewCucumberExpressionGenerator(parameterTypeRegistry)
generatedExpressions := generator.GenerateExpressions("I have x and x and another x")
sources := make([]string, len(generatedExpressions))
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestCucumberExpressionGeneratory(t *testing.T) {
false,
)
require.NoError(t, err)
parameterTypeRegistry.DefineParameterType(optionalFlightParameterType)
require.NoError(t, parameterTypeRegistry.DefineParameterType(optionalFlightParameterType))
optionalHotelParameterType, err := NewParameterType(
"optional-hotel",
[]*regexp.Regexp{regexp.MustCompile("(1st hotel)?")},
Expand All @@ -211,7 +211,7 @@ func TestCucumberExpressionGeneratory(t *testing.T) {
false,
)
require.NoError(t, err)
parameterTypeRegistry.DefineParameterType(optionalHotelParameterType)
require.NoError(t, parameterTypeRegistry.DefineParameterType(optionalHotelParameterType))
generator := NewCucumberExpressionGenerator(parameterTypeRegistry)
generatedExpression := generator.GenerateExpressions("I reach Stage4: 1st flight-1st hotel")[0]
// While you would expect this to be `I reach Stage{int}: {optional-flight}-{optional-hotel}`
Expand All @@ -231,7 +231,7 @@ func TestCucumberExpressionGeneratory(t *testing.T) {
false,
)
require.NoError(t, err)
parameterTypeRegistry.DefineParameterType(myType)
require.NoError(t, parameterTypeRegistry.DefineParameterType(myType))
}

generator := NewCucumberExpressionGenerator(parameterTypeRegistry)
Expand All @@ -251,7 +251,7 @@ func TestCucumberExpressionGeneratory(t *testing.T) {
false,
)
require.NoError(t, err)
parameterTypeRegistry.DefineParameterType(zeroOrMore)
require.NoError(t, parameterTypeRegistry.DefineParameterType(zeroOrMore))
exactlyOne, err := NewParameterType(
"exactly-one",
[]*regexp.Regexp{regexp.MustCompile("[a-z]")},
Expand All @@ -261,7 +261,7 @@ func TestCucumberExpressionGeneratory(t *testing.T) {
false,
)
require.NoError(t, err)
parameterTypeRegistry.DefineParameterType(exactlyOne)
require.NoError(t, parameterTypeRegistry.DefineParameterType(exactlyOne))
generator := NewCucumberExpressionGenerator(parameterTypeRegistry)

generatedExpressions := generator.GenerateExpressions("a simple step")
Expand All @@ -279,7 +279,6 @@ func assertExpression(t *testing.T, expectedExpression string, expectedArgumentN
func assertExpressionWithParameterTypeRegistry(t *testing.T, parameterTypeRegistry *ParameterTypeRegistry, expectedExpression string, expectedArgumentNames []string, text string) {
generator := NewCucumberExpressionGenerator(parameterTypeRegistry)
generatedExpressions := generator.GenerateExpressions(text)
require.Len(t, generatedExpressions, 1)
generatedExpression := generatedExpressions[0]
require.Equal(t, generatedExpression.ParameterNames(), expectedArgumentNames)
require.Equal(t, generatedExpression.Source(), expectedExpression)
Expand Down
2 changes: 1 addition & 1 deletion cucumber-expressions/go/cucumber_expression_regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestCucumberExpressionRegExpTranslation(t *testing.T) {
assertRegexp(
t,
"I have {float} cukes at {int} o'clock",
`^I have (-?\d*\.\d+) cukes at ((?:-?\d+)|(?:\d+)) o'clock$`,
`^I have (-?\d*(?:[.,]\d+)?) cukes at ((?:-?\d+)|(?:\d+)) o'clock$`,
)
})

Expand Down
13 changes: 13 additions & 0 deletions cucumber-expressions/go/cucumber_expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ func TestCucumberExpression(t *testing.T) {
)
})

t.Run("matches float with zero", func(t *testing.T) {
require.Equal(
t,
MatchCucumberExpression(t, "{float}", "0"),
[]interface{}{0.0},
)
require.Equal(
t,
MatchCucumberExpression(t, "{float}", "-1"),
[]interface{}{-1.0},
)
})

t.Run("matches anonymous", func(t *testing.T) {
require.Equal(
t,
Expand Down
2 changes: 1 addition & 1 deletion cucumber-expressions/go/parameter_type_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var INTEGER_REGEXPS = []*regexp.Regexp{
regexp.MustCompile(`\d+`),
}
var FLOAT_REGEXPS = []*regexp.Regexp{
regexp.MustCompile(`-?\d*\.\d+`),
regexp.MustCompile(`-?\d*(?:[.,]\d+)?`),
}
var WORD_REGEXPS = []*regexp.Regexp{
regexp.MustCompile(`[^\s]+`),
Expand Down
4 changes: 2 additions & 2 deletions cucumber-expressions/go/regular_expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func TestRegularExpression(t *testing.T) {
})

t.Run("transforms float without integer part", func(t *testing.T) {
require.Equal(t, Match(t, `(-?\d*\.\d+)`, ".22")[0], 0.22)
require.Equal(t, Match(t, `(-?\d*(?:[.,]\d+)?)`, ".22")[0], 0.22)
})

t.Run("transforms float with sign", func(t *testing.T) {
require.Equal(t, Match(t, `(-?\d*\.\d+)`, "-1.22")[0], -1.22)
require.Equal(t, Match(t, `(-?\d*(?:[.,]\d+)?)`, "-1.22")[0], -1.22)
})

t.Run("returns nil when there is no match", func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ public int compareTo(ParameterType<?> o) {
return name.compareTo(otherName);
}

public int weight() {
if(this.type.equals(Integer.class) || this.type.equals(Integer.TYPE)) {
return 1000;
}
return 0;
}

private static final class TransformerAdaptor<T> implements CaptureGroupTransformer<T> {

private final Transformer<T> transformer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ public int compareTo(ParameterTypeMatcher o) {
if (posComparison != 0) return posComparison;
int lengthComparison = Integer.compare(o.group().length(), group().length());
if (lengthComparison != 0) return lengthComparison;
int weightComparison = Integer.compare(o.parameterType.weight(), parameterType.weight());
if (weightComparison != 0) return weightComparison;
return 0;
}

public ParameterType<?> getParameterType() {
return parameterType;
}

public String toString() {
return parameterType.getType().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ParameterTypeRegistry {
// Pattern.compile(...).pattern() is not necessary, but it helps us take advantage of the IntelliJ's regexp validation,
// which detects unneeded escapes.
private static final List<String> INTEGER_REGEXPS = asList(Pattern.compile("-?\\d+").pattern(), Pattern.compile("\\d+").pattern());
private static final List<String> FLOAT_REGEXPS = singletonList(Pattern.compile("-?\\d*[.,]\\d+").pattern());
private static final List<String> FLOAT_REGEXPS = singletonList(Pattern.compile("-?\\d*(?:[.,]\\d+)?").pattern());
private static final List<String> WORD_REGEXPS = singletonList(Pattern.compile("[^\\s]+").pattern());
private static final List<String> STRING_REGEXPS = singletonList(Pattern.compile("\"([^\"\\\\]*(\\\\.[^\"\\\\]*)*)\"|'([^'\\\\]*(\\\\.[^'\\\\]*)*)'").pattern());
private static final String ANONYMOUS_REGEX = Pattern.compile(".*").pattern();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void translates_alternation_with_non_alpha() {
public void translates_parameters() {
assertPattern(
"I have {float} cukes at {int} o'clock",
"^I have (-?\\d*[.,]\\d+) cukes at ((?:-?\\d+)|(?:\\d+)) o'clock$"
"^I have (-?\\d*(?:[.,]\\d+)?) cukes at ((?:-?\\d+)|(?:\\d+)) o'clock$"
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ public void matches_double_with_comma_for_locale_using_comma() {
assertEquals(singletonList(1.22), values);
}

@Test
public void matches_float_with_zero() {
List<?> values = match("{float}", "0", Locale.ENGLISH);
assertEquals(0.0f, values.get(0));
}

private List<?> match(String expr, String text, Type... typeHints) {
return match(expr, text, Locale.ENGLISH, typeHints);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import ParameterType from './ParameterType';
export default class ParameterTypeRegistry {
static readonly INTEGER_REGEXPS: RegExp[];
static readonly FLOAT_REGEXP: RegExp;
static readonly WORD_REGEXP: RegExp;
static readonly STRING_REGEXP: RegExp;
static readonly ANONYMOUS_REGEXP: RegExp;
private readonly parameterTypeByName;
private readonly parameterTypesByRegexp;
constructor();
Expand Down
20 changes: 10 additions & 10 deletions cucumber-expressions/javascript/dist/src/ParameterTypeRegistry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading