Skip to content

Commit

Permalink
API renames, based on feedback in cucumber/cucumber-js#764
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Mar 8, 2017
1 parent a319df2 commit d364c4b
Show file tree
Hide file tree
Showing 70 changed files with 932 additions and 945 deletions.
5 changes: 5 additions & 0 deletions cucumber-expressions/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ N/A
(by [aslakhellesoy])

### Changed
* Renamed API:
* `Parameter -> ParameterType`
* `ParameterRegistry -> ParameterTypeRegistry`
* `addParameter -> defineParameterType`

* Stricter conflict checks when defining parameters
([#121](https://github.com/cucumber/cucumber/pull/121)
by [aslakhellesoy])
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.cucumber.cucumberexpressions;

import java.lang.reflect.Type;
import java.util.List;

import static java.util.Collections.singletonList;

public abstract class AbstractParameterType<T> implements ParameterType<T> {
private final String typeName;
private final Type type;
private final List<String> regexps;

public AbstractParameterType(String name, Type type, List<String> regexps) {
this.regexps = regexps;
this.typeName = name;
this.type = type;
}

public AbstractParameterType(String typeName, Type type, String regexp) {
this(typeName, type, singletonList(regexp));
}

@Override
public String getName() {
return typeName;
}

@Override
public Type getType() {
return type;
}

@Override
public List<String> getRegexps() {
return regexps;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
public class Argument {
private final int offset;
private final String value;
private final Parameter parameter;
private final ParameterType parameterType;

public Argument(Integer offset, String value, Parameter parameter) {
public Argument(Integer offset, String value, ParameterType parameterType) {
this.offset = offset;
this.value = value;
this.parameter = parameter;
this.parameterType = parameterType;
}

public int getOffset() {
Expand All @@ -20,6 +20,6 @@ public String getValue() {
}

public Object getTransformedValue() {
return parameter.transform(value);
return parameterType.transform(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
import java.util.regex.Pattern;

class ArgumentBuilder {
static List<Argument> buildArguments(Pattern pattern, String text, List<Parameter<?>> parameters) {
static List<Argument> buildArguments(Pattern pattern, String text, List<ParameterType<?>> parameterTypes) {
Matcher matcher = pattern.matcher(text);
if (matcher.lookingAt()) {
if (matcher.groupCount() != parameters.size()) {
throw new RuntimeException(String.format("Expression has %s arguments, but there were %s parameters", matcher.groupCount(), parameters.size()));
if (matcher.groupCount() != parameterTypes.size()) {
throw new RuntimeException(String.format("Expression has %s arguments, but there were %s parameterTypes", matcher.groupCount(), parameterTypes.size()));
}
List<Argument> arguments = new ArrayList<>(matcher.groupCount());
for (int i = 0; i < matcher.groupCount(); i++) {
int startIndex = matcher.start(i + 1);
String value = matcher.group(i + 1);
Parameter parameter = parameters.get(i);
ParameterType parameterType = parameterTypes.get(i);

arguments.add(new Argument(startIndex, value, parameter));
arguments.add(new Argument(startIndex, value, parameterType));
}
return arguments;
} else {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.cucumber.cucumberexpressions;

import java.lang.reflect.Type;
import java.util.List;

public class ClassParameterType<T> implements ParameterType<T> {
private final ParameterType<T> delegate;

public ClassParameterType(Class<T> type) {
if (type.isEnum()) {
delegate = (ParameterType<T>) new EnumParameterType<>((Class<? extends Enum>) type);
} else {
delegate = new ConstructorParameterType<>(type);
}
}

@Override
public String getName() {
return delegate.getName();
}

@Override
public Type getType() {
return delegate.getType();
}

@Override
public List<String> getRegexps() {
return delegate.getRegexps();
}

@Override
public T transform(String value) {
return delegate.transform(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import java.util.Collections;
import java.util.List;

public class ConstructorParameter<T> extends AbstractParameter<T> {
public class ConstructorParameterType<T> extends AbstractParameterType<T> {
private static final List<String> ANYTHING_GOES = Collections.singletonList(".+");
private final Constructor<T> constructor;

public ConstructorParameter(Class<T> clazz) {
public ConstructorParameterType(Class<T> clazz) {
super(null, clazz, ANYTHING_GOES);
try {
this.constructor = clazz.getConstructor(String.class);
Expand All @@ -21,7 +21,7 @@ public ConstructorParameter(Class<T> clazz) {
@Override
public T transform(String value) {
if (value == null) {
return (T)null;
return null;
}
try {
return constructor.newInstance(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ public class CucumberExpression implements Expression {
private static final Pattern OPTIONAL_PATTERN = Pattern.compile("\\(([^)]+)\\)");
private static final Pattern ALTERNATIVE_WORD_REGEXP = Pattern.compile("([\\p{IsAlphabetic}]+)((/[\\p{IsAlphabetic}]+)+)");


private final Pattern pattern;
private final List<Parameter<?>> parameters = new ArrayList<>();
private final List<ParameterType<?>> parameterTypes = new ArrayList<>();
private final String expression;

public CucumberExpression(String expression, List<? extends Type> types, ParameterRegistry parameterRegistry) {
public CucumberExpression(String expression, List<? extends Type> types, ParameterTypeRegistry parameterTypeRegistry) {
this.expression = expression;
expression = ESCAPE_PATTERN.matcher(expression).replaceAll("\\\\$1");
expression = OPTIONAL_PATTERN.matcher(expression).replaceAll("(?:$1)?");
Expand All @@ -36,47 +35,47 @@ public CucumberExpression(String expression, List<? extends Type> types, Paramet
int typeIndex = 0;
while (matcher.find()) {
String parameterName = matcher.group(1);
String typeName = matcher.group(3);
if (typeName != null) {
System.err.println(String.format("Cucumber expression parameter syntax {%s:%s} is deprecated. Please use {%s} instead.", parameterName, typeName, typeName));
String parameterTypeName = matcher.group(3);
if (parameterTypeName != null) {
System.err.println(String.format("Cucumber expression parameter type syntax {%s:%s} is deprecated. Please use {%s} instead.", parameterName, parameterTypeName, parameterTypeName));
}

Type type = types.size() <= typeIndex ? null : types.get(typeIndex++);

Parameter<?> parameter = null;
ParameterType<?> parameterType = null;
if (type != null) {
parameter = parameterRegistry.lookupByType(type);
parameterType = parameterTypeRegistry.lookupByType(type);
}
if (parameter == null && typeName != null) {
parameter = parameterRegistry.lookupByTypeName(typeName);
if (parameterType == null && parameterTypeName != null) {
parameterType = parameterTypeRegistry.lookupByTypeName(parameterTypeName);
}
if (parameter == null) {
parameter = parameterRegistry.lookupByTypeName(parameterName);
if (parameterType == null) {
parameterType = parameterTypeRegistry.lookupByTypeName(parameterName);
}
if (parameter == null && type != null && type instanceof Class) {
parameter = new ClassParameter<>((Class) type);
if (parameterType == null && type != null && type instanceof Class) {
parameterType = new ClassParameterType<>((Class) type);
}
if (parameter == null) {
parameter = new ConstructorParameter<>(String.class);
if (parameterType == null) {
parameterType = new ConstructorParameterType<>(String.class);
}
parameters.add(parameter);
parameterTypes.add(parameterType);

matcher.appendReplacement(regexp, Matcher.quoteReplacement(getCaptureGroupRegexp(parameter.getCaptureGroupRegexps())));
matcher.appendReplacement(regexp, Matcher.quoteReplacement(getCaptureGroupRegexp(parameterType.getRegexps())));
}
matcher.appendTail(regexp);
regexp.append("$");

pattern = Pattern.compile(regexp.toString());
}

private String getCaptureGroupRegexp(List<String> captureGroupRegexps) {
private String getCaptureGroupRegexp(List<String> regexps) {
StringBuilder sb = new StringBuilder("(");

if (captureGroupRegexps.size() == 1) {
sb.append(captureGroupRegexps.get(0));
if (regexps.size() == 1) {
sb.append(regexps.get(0));
} else {
boolean bar = false;
for (String captureGroupRegexp : captureGroupRegexps) {
for (String captureGroupRegexp : regexps) {
if (bar) sb.append("|");
sb.append("(?:").append(captureGroupRegexp).append(")");
bar = true;
Expand All @@ -89,7 +88,7 @@ private String getCaptureGroupRegexp(List<String> captureGroupRegexps) {

@Override
public List<Argument> match(String text) {
return ArgumentBuilder.buildArguments(pattern, text, parameters);
return ArgumentBuilder.buildArguments(pattern, text, parameterTypes);
}

@Override
Expand Down
Loading

0 comments on commit d364c4b

Please sign in to comment.