Skip to content

Commit

Permalink
Allow ParameterType transform to be null/nil and leave args as string
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Mar 8, 2017
1 parent 83e8a8e commit 30c4fbc
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ N/A
(by [aslakhellesoy])
* `ParameterType` can be constructed with `null`/`nil` arguments for
* `type` / `constructorFunction`: Makes it simpler to use in languages without static types
* `transform`: Leave arguments unchanged, return as string
(by [aslakhellesoy])

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
public class SimpleParameterType<T> extends AbstractParameterType<T> {
private final Function<String, T> transformer;

public SimpleParameterType(String name, String regexp) {
this(name, null, regexp, null);
}

public SimpleParameterType(String name, List<String> regexps) {
this(name, null, regexps, null);
}

public SimpleParameterType(String name, Class<T> type, String regexp, Function<String, T> transformer) {
super(name, type, regexp);
this.transformer = transformer;
Expand All @@ -17,7 +25,7 @@ public SimpleParameterType(String name, Class<T> type, List<String> regexps, Fun

@Override
public T transform(String value) {
return transformer.apply(value);
return transformer != null ? transformer.apply(value) : (T) value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public Color apply(String name) {
}

@Test
public void transforms_CucumberExpression_arguments_with_expression_type() {
public void matches_CucumberExpression_parameters_with_custom_parameter_type() {
Expression expression = new CucumberExpression("I have a {color} ball", Collections.<Type>emptyList(), parameterTypeRegistry);
Object transformedArgumentValue = expression.match("I have a red ball").get(0).getTransformedValue();
assertEquals(new Color("red"), transformedArgumentValue);
}

@Test
public void transforms_CucumberExpression_arguments_with_expression_type_using_optional_group() {
public void matches_CucumberExpression_parameters_with_custom_parameter_type_using_optional_group() {
parameterTypeRegistry = new ParameterTypeRegistry(Locale.ENGLISH);
parameterTypeRegistry.defineParameterType(new SimpleParameterType<>(
"color",
Expand All @@ -80,21 +80,19 @@ public Color apply(String name) {
}

@Test
public void transforms_CucumberExpression_arguments_with_explicit_type() {
Expression expression = new CucumberExpression("I have a {color} ball", Collections.<Type>singletonList(Color.class), parameterTypeRegistry);
Object transformedArgumentValue = expression.match("I have a red ball").get(0).getTransformedValue();
assertEquals(new Color("red"), transformedArgumentValue);
}

@Test
public void transforms_CucumberExpression_arguments_using_argument_name_as_type() {
public void matches_CucumberExpression_parameters_with_custom_parameter_without_type_and_transform() {
parameterTypeRegistry = new ParameterTypeRegistry(Locale.ENGLISH);
parameterTypeRegistry.defineParameterType(new SimpleParameterType<>(
"color",
"red|blue|yellow"
));
Expression expression = new CucumberExpression("I have a {color} ball", Collections.<Type>emptyList(), parameterTypeRegistry);
Object transformedArgumentValue = expression.match("I have a red ball").get(0).getTransformedValue();
assertEquals(new Color("red"), transformedArgumentValue);
assertEquals("red", transformedArgumentValue);
}

@Test
public void transforms_CucumberExpression_arguments_with_explicit_type_using_constructor_directly() {
public void matches_CucumberExpression_parameters_with_explicit_type() {
Expression expression = new CucumberExpression("I have a {color} ball", Collections.<Type>singletonList(Color.class), new ParameterTypeRegistry(Locale.ENGLISH));
Color transformedArgumentValue = (Color) expression.match("I have a red ball").get(0).getTransformedValue();
assertEquals("red", transformedArgumentValue.name);
Expand Down Expand Up @@ -208,21 +206,21 @@ public String apply(String s) {
///// RegularExpression

@Test
public void transforms_RegularExpression_arguments_with_explicit_type() {
public void matches_RegularExpression_arguments_with_explicit_type() {
Expression expression = new RegularExpression(compile("I have a (red|blue|yellow) ball"), Collections.<Type>singletonList(Color.class), parameterTypeRegistry);
Object transformedArgumentValue = expression.match("I have a red ball").get(0).getTransformedValue();
assertEquals(new Color("red"), transformedArgumentValue);
}

@Test
public void transforms_RegularExpression_arguments_without_explicit_type() {
public void matches_RegularExpression_arguments_without_explicit_type() {
Expression expression = new RegularExpression(compile("I have a (red|blue|yellow) ball"), Collections.<Type>emptyList(), parameterTypeRegistry);
Object transformedArgumentValue = expression.match("I have a red ball").get(0).getTransformedValue();
assertEquals(new Color("red"), transformedArgumentValue);
}

@Test
public void transforms_RegularExpression_arguments_with_explicit_type_using_constructor_directly() {
public void matches_RegularExpression_arguments_with_explicit_type_using_constructor_directly() {
Expression expression = new RegularExpression(compile("I have a (red|blue|yellow) ball"), Collections.<Type>singletonList(Color.class), new ParameterTypeRegistry(Locale.ENGLISH));
Color transformedArgumentValue = (Color) expression.match("I have a red ball").get(0).getTransformedValue();
assertEquals("red", transformedArgumentValue.name);
Expand Down
3 changes: 1 addition & 2 deletions javascript/src/parameter_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class ParameterType {
this._name = name
this._constructorFunction = constructorFunction
this._regexps = stringArray(regexps)
// TODO: Check for null transform, don't use Identity elsewhere
this._transform = transform
}

Expand All @@ -20,7 +19,7 @@ class ParameterType {
}

transform(string) {
return this._transform(string)
return this._transform ? this._transform(string) : string
}
}

Expand Down
21 changes: 14 additions & 7 deletions javascript/test/custom_parameter_type_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ describe('Custom parameter type', () => {
})

describe(CucumberExpression.name, () => {
it("matches typed parameters", () => {
it("matches parameters with custom parameter type", () => {
const expression = new CucumberExpression("I have a {color} ball", [], parameterTypeRegistry)
const transformedValue = expression.match("I have a red ball")[0].transformedValue
assert.equal(transformedValue.name, "red")
})

it("matches typed parameters with optional group", () => {
it("matches parameters with custom parameter type using optional capture group", () => {
parameterTypeRegistry = new ParameterTypeRegistry()
parameterTypeRegistry.defineParameterType(new ParameterType(
'color',
Expand All @@ -51,14 +51,21 @@ describe('Custom parameter type', () => {
assert.equal(transformedValue.name, "dark red")
})

it("matches untyped parameters with explicit type", () => {
const expression = new CucumberExpression("I have a {color} ball", [Color], parameterTypeRegistry)
it("matches parameters with custom parameter type without constructor function and transform", () => {
parameterTypeRegistry = new ParameterTypeRegistry()
parameterTypeRegistry.defineParameterType(new ParameterType(
'color',
null,
/red|blue|yellow/,
null
))
const expression = new CucumberExpression("I have a {color} ball", [], parameterTypeRegistry)
const transformedValue = expression.match("I have a red ball")[0].transformedValue
assert.equal(transformedValue.name, "red")
assert.equal(transformedValue, "red")
})

it("matches untyped parameters with same name as type", () => {
const expression = new CucumberExpression("I have a {color} ball", [], parameterTypeRegistry)
it("matches parameters with explicit type", () => {
const expression = new CucumberExpression("I have a {color} ball", [Color], parameterTypeRegistry)
const transformedValue = expression.match("I have a red ball")[0].transformedValue
assert.equal(transformedValue.name, "red")
})
Expand Down
10 changes: 5 additions & 5 deletions ruby/lib/cucumber/cucumber_expressions/parameter_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ class ParameterType
# @param regexps [Array] list of regexps for capture groups. A single regexp can also be used.
# @param transformer lambda that transforms a String to (possibly) another type
#
def initialize(name, type, regexps, transformer)
def initialize(name, type, regexp, transformer)
@name, @type, @transformer = name, type, transformer
@regexps = string_array(regexps)
@regexps = string_array(regexp)
end

def transform(value)
@transformer.call(value)
@transformer ? @transformer.call(value) : value
end

private

def string_array(regexps)
array = regexps.is_a?(Array) ? regexps : [regexps]
def string_array(regexp)
array = regexp.is_a?(Array) ? regexp : [regexp]
array.map { |r| r.is_a?(String) ? r : r.source }
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def ==(other)
end

describe CucumberExpression do
it "matches typed parameters" do
it "matches parameters with custom parameter type" do
expression = CucumberExpression.new("I have a {color} ball", [], @parameter_type_registry)
transformed_argument_value = expression.match("I have a red ball")[0].transformed_value
expect( transformed_argument_value ).to eq(Color.new('red'))
end

it "matches typed parameters with optional group" do
it "matches parameters with custom parameter type using optional capture group" do
parameter_registry = ParameterTypeRegistry.new
parameter_registry.define_parameter_type(ParameterType.new(
'color',
Expand All @@ -52,14 +52,21 @@ def ==(other)
expect( transformed_argument_value ).to eq(Color.new('dark red'))
end

it "matches untyped parameters with explicit type" do
expression = CucumberExpression.new("I have a {color} ball", [Color], @parameter_type_registry)
it "matches parameters with custom parameter type without constructor function and transform" do
parameter_registry = ParameterTypeRegistry.new
parameter_registry.define_parameter_type(ParameterType.new(
'color',
nil,
/red|blue|yellow/,
nil
))
expression = CucumberExpression.new("I have a {color} ball", [], parameter_registry)
transformed_argument_value = expression.match("I have a red ball")[0].transformed_value
expect( transformed_argument_value ).to eq(Color.new('red'))
expect( transformed_argument_value ).to eq('red')
end

it "matches untyped parameters with same name as type" do
expression = CucumberExpression.new("I have a {color} ball", [], @parameter_type_registry)
it "matches parameters with explicit type" do
expression = CucumberExpression.new("I have a {color} ball", [Color], @parameter_type_registry)
transformed_argument_value = expression.match("I have a red ball")[0].transformed_value
expect( transformed_argument_value ).to eq(Color.new('red'))
end
Expand Down

0 comments on commit 30c4fbc

Please sign in to comment.