-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] Extract CoreStepDefinitions from Java and Java8 implementation
Removes the `CucumberExpression` creation from the backend modules and will allow a type registry to be created for each pickle.
- Loading branch information
1 parent
8520993
commit f2eeac1
Showing
40 changed files
with
840 additions
and
718 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
core/src/main/java/io/cucumber/core/backend/ParameterInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.cucumber.core.backend; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
public interface ParameterInfo { | ||
|
||
Type getType(); | ||
|
||
boolean isTransposed(); | ||
|
||
TypeResolver getTypeResolver(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
core/src/main/java/io/cucumber/core/backend/TypeResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.cucumber.core.backend; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Allows lazy resolution of the type of a data table or doc string. | ||
*/ | ||
@API(status = API.Status.STABLE) | ||
public interface TypeResolver { | ||
|
||
/** | ||
* A type to data convert the table or doc string to. May not return null. | ||
* <p> | ||
* When the {@link Object} type is returned no transform will be applied. | ||
* | ||
* @return a type | ||
*/ | ||
Type resolve(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
core/src/main/java/io/cucumber/core/runner/CoreStepDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.cucumber.core.runner; | ||
|
||
import gherkin.pickles.PickleStep; | ||
import io.cucumber.core.backend.ParameterInfo; | ||
import io.cucumber.core.backend.StepDefinition; | ||
import io.cucumber.core.stepexpression.Argument; | ||
import io.cucumber.core.stepexpression.ArgumentMatcher; | ||
import io.cucumber.core.stepexpression.StepExpression; | ||
import io.cucumber.core.stepexpression.StepExpressionFactory; | ||
import io.cucumber.core.stepexpression.TypeRegistry; | ||
import io.cucumber.core.stepexpression.TypeResolver; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
class CoreStepDefinition implements StepDefinition { | ||
|
||
private final StepExpression expression; | ||
private final ArgumentMatcher argumentMatcher; | ||
private final StepDefinition stepDefinition; | ||
private final Type[] types; | ||
|
||
CoreStepDefinition(StepDefinition stepDefinition, TypeRegistry typeRegistry) { | ||
this.stepDefinition = requireNonNull(stepDefinition); | ||
List<ParameterInfo> parameterInfos = stepDefinition.parameterInfos(); | ||
this.expression = createExpression(parameterInfos, stepDefinition.getPattern(), typeRegistry); | ||
this.argumentMatcher = new ArgumentMatcher(this.expression); | ||
this.types = getTypes(parameterInfos); | ||
} | ||
|
||
private StepExpression createExpression(List<ParameterInfo> parameterInfos, String expression, TypeRegistry typeRegistry) { | ||
if (parameterInfos == null || parameterInfos.isEmpty()) { | ||
return new StepExpressionFactory(typeRegistry).createExpression(expression); | ||
} else { | ||
ParameterInfo parameterInfo = parameterInfos.get(parameterInfos.size() - 1); | ||
TypeResolver typeResolver = parameterInfo.getTypeResolver()::resolve; | ||
boolean transposed = parameterInfo.isTransposed(); | ||
return new StepExpressionFactory(typeRegistry).createExpression(expression, typeResolver, transposed); | ||
} | ||
} | ||
|
||
@Override | ||
public void execute(Object[] args) throws Throwable { | ||
stepDefinition.execute(args); | ||
} | ||
|
||
@Override | ||
public boolean isDefinedAt(StackTraceElement stackTraceElement) { | ||
return stepDefinition.isDefinedAt(stackTraceElement); | ||
} | ||
|
||
@Override | ||
public List<ParameterInfo> parameterInfos() { | ||
return stepDefinition.parameterInfos(); | ||
} | ||
|
||
@Override | ||
public String getLocation(boolean detail) { | ||
return stepDefinition.getLocation(detail); | ||
} | ||
|
||
public String getPattern() { | ||
return expression.getSource(); | ||
} | ||
|
||
public StepDefinition getStepDefinition() { | ||
return stepDefinition; | ||
} | ||
|
||
List<Argument> matchedArguments(PickleStep step) { | ||
return argumentMatcher.argumentsFrom(step, types); | ||
} | ||
|
||
private static Type[] getTypes(List<ParameterInfo> parameterInfos) { | ||
if (parameterInfos == null) { | ||
return new Type[0]; | ||
} | ||
|
||
Type[] types = new Type[parameterInfos.size()]; | ||
for (int i = 0; i < types.length; i++) { | ||
types[i] = parameterInfos.get(i).getType(); | ||
} | ||
return types; | ||
} | ||
|
||
} |
5 changes: 2 additions & 3 deletions
5
core/src/main/java/io/cucumber/core/runner/FailedPickleStepInstantiationMatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
core/src/main/java/io/cucumber/core/stepexpression/Argument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.