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 1 commit
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
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
Expand Up @@ -10,7 +10,7 @@ const {
} = require('./errors');

const INTEGER_REGEXPS = [/-?\d+/, /\d+/];
const FLOAT_REGEXP = /-?\d*\.\d+/;
const FLOAT_REGEXP = /-?\d*(?:\.\d+)?/;
const WORD_REGEXP = /[^\s]+/;
const STRING_REGEXP = /"([^"\\]*(\\.[^"\\]*)*)"|'([^'\\]*(\\.[^'\\]*)*)'/;
const ANONYMOUS_REGEXP = /.*/;
Expand Down
2 changes: 1 addition & 1 deletion cucumber-expressions/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-prettier": "^3.0.0",
"mocha": "^6.1.2",
"nyc": "^13.1.0",
"nyc": "^14.1.0",
"prettier": "^1.13.2"
},
"files": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
} = require('./errors')

const INTEGER_REGEXPS = [/-?\d+/, /\d+/]
const FLOAT_REGEXP = /-?\d*\.\d+/
const FLOAT_REGEXP = /-?\d*(?:\.\d+)?/
const WORD_REGEXP = /[^\s]+/
const STRING_REGEXP = /"([^"\\]*(\\.[^"\\]*)*)"|'([^'\\]*(\\.[^'\\]*)*)'/
const ANONYMOUS_REGEXP = /.*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('CucumberExpression', () => {
it('translates parameters', () => {
assertRegexp(
"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 @@ -115,6 +115,10 @@ describe('CucumberExpression', () => {
assert.deepEqual(match('{float}', '.22'), [0.22])
})

it('matches float with zero', () => {
assert.deepEqual(match('{float}', '0'), [0])
})

it('matches anonymous', () => {
assert.deepEqual(match('{}', '0.22'), ['0.22'])
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def transform(self_obj, group_values)
def <=>(other)
return -1 if prefer_for_regexp_match? && !other.prefer_for_regexp_match?
return 1 if other.prefer_for_regexp_match? && !prefer_for_regexp_match?
name <=> other.name
return name <=> other.name
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Cucumber
module CucumberExpressions
class ParameterTypeRegistry
INTEGER_REGEXPS = [/-?\d+/, /\d+/]
FLOAT_REGEXP = /-?\d*\.\d+/
FLOAT_REGEXP = /-?\d*(?:[.,]\d+)?/
WORD_REGEXP = /[^\s]+/
STRING_REGEXP = /"([^"\\]*(\\.[^"\\]*)*)"|'([^'\\]*(\\.[^'\\]*)*)'/
ANONYMOUS_REGEXP = /.*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def assert_regexp(expression, regexp)
it "translates parameters" do
assert_regexp(
"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$/
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ module CucumberExpressions
expect(match("{int}", "1.22")).to be_nil
end

it "matches int as float" do
expect(match("{float}", "0")).to eq([0.0])
end

it "matches float" do
expect(match("{float}", "0.22")).to eq([0.22])
expect(match("{float}", ".22")).to eq([0.22])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ module CucumberExpressions
end

it "transforms float without integer part" do
expect( match(/(-?\d*\.\d+)/, ".22") ).to eq([0.22])
expect( match(/(-?\d*(?:[.,]\d+)?)/, ".22") ).to eq([0.22])
end

it "transforms float with sign" do
expect( match(/(-?\d*\.\d+)/, "-1.22") ).to eq([-1.22])
expect( match(/(-?\d*(?:[.,]\d+)?)/, "-1.22") ).to eq([-1.22])
end

it "returns nil when there is no match" do
Expand Down