-
-
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.
[Java] Allow parameter types access to test context (#1677)
# Summary By annotating methods parameter and data table types can be defined as part of the Glue. This enables them to access the test context and makes them eligible for dependency injection. Additionally the type registry is now created for each feature. This means the language of the feature will be used to convert numbers. ## Details ## Parameter and DataTable Type Introduces the `@ParameterType` and `@DataTableType` annotations. This allows parameter and datatable types to be mapped to objects which can only be created by services inside the test context. For example in this scenario ```gherkin Given the awesome catalog When a user places the awestruck eels in his basket Then you will be shocked at what happened next ``` We are now able to look up the "awestruck eels" in the "awesome" catalog. ```java private final Catalog catalog; @ParameterType(".*") public Product product(String name) { return catalog.findProductByName(name); } ``` ## Default Transformer It is now also possible to register default transformers using annotations. Default transformers allow you to specific a transformer that will be used when there is no transform defined. This can be combined with an object mapper like Jackson to quickly transform well known string representations to Java objects. * `@DefaultParameterTransformer` * `@DefaultDataTableEntryTransformer` * `@DefaultDataTableCellTransformer` ```java package com.example.app; import com.fasterxml.jackson.databind.ObjectMapper; import io.cucumber.java.DefaultDataTableCellTransformer; import io.cucumber.java.DefaultDataTableEntryTransformer; import io.cucumber.java.DefaultParameterTransformer; import java.lang.reflect.Type; public class DataTableSteps { private final ObjectMapper objectMapper = new ObjectMapper(); @DefaultParameterTransformer @DefaultDataTableEntryTransformer @DefaultDataTableCellTransformer public Object defaultTransformer(Object fromValue, Type toValueType) { return objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType)); } } ``` ## Localization Some languages uses comma's rather then points to separate decimals. Previously to parse these properly you'd have to use `TypeRegistryConfigurer.locale` to set this globally. When not explicitly provided Cucumber will now take the language from the feature file. This makes the following work without additional configuration: ```gherkin # language: fr Fonctionnalité: Concombres fractionnaires Scénario: dans la ventre Étant donné j'ai 5,5 concombres fractionnaires ``` ```java @Étantdonné("j'ai {bigdecimal} concombres fractionnaires") public void jAiConcombresFractionnaires(BigDecimal arg0) { assertThat(arg0, is(new BigDecimal("5.5"))); } ``` # Motivation & Context Fixes #851. Fixes #1458.
- Loading branch information
Showing
110 changed files
with
3,198 additions
and
864 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
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
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
19 changes: 19 additions & 0 deletions
19
core/src/main/java/io/cucumber/core/backend/DataTableTypeDefinition.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,19 @@ | ||
package io.cucumber.core.backend; | ||
|
||
import io.cucumber.datatable.DataTableType; | ||
import org.apiguardian.api.API; | ||
|
||
@API(status = API.Status.STABLE) | ||
public interface DataTableTypeDefinition { | ||
|
||
DataTableType dataTableType(); | ||
|
||
/** | ||
* The source line where the data table type is defined. | ||
* Example: com/example/app/Cucumber.test():42 | ||
* | ||
* @param detail true if extra detailed location information should be included. | ||
* @return The source line of the step definition. | ||
*/ | ||
String getLocation(boolean detail); | ||
} |
20 changes: 20 additions & 0 deletions
20
core/src/main/java/io/cucumber/core/backend/DefaultDataTableCellTransformerDefinition.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,20 @@ | ||
package io.cucumber.core.backend; | ||
|
||
import io.cucumber.datatable.TableCellByTypeTransformer; | ||
import org.apiguardian.api.API; | ||
|
||
@API(status = API.Status.STABLE) | ||
public interface DefaultDataTableCellTransformerDefinition { | ||
|
||
TableCellByTypeTransformer tableCellByTypeTransformer(); | ||
|
||
/** | ||
* The source line where the default data table cell is defined. | ||
* Example: com/example/app/Cucumber.test():42 | ||
* | ||
* @param detail true if extra detailed location information should be included. | ||
* @return The source line of the step definition. | ||
*/ | ||
String getLocation(boolean detail); | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
core/src/main/java/io/cucumber/core/backend/DefaultDataTableEntryTransformerDefinition.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,19 @@ | ||
package io.cucumber.core.backend; | ||
|
||
import io.cucumber.datatable.TableEntryByTypeTransformer; | ||
import org.apiguardian.api.API; | ||
|
||
@API(status = API.Status.STABLE) | ||
public interface DefaultDataTableEntryTransformerDefinition { | ||
|
||
TableEntryByTypeTransformer tableEntryByTypeTransformer(); | ||
|
||
/** | ||
* The source line where the default table entry transformer is defined. | ||
* Example: com/example/app/Cucumber.test():42 | ||
* | ||
* @param detail true if extra detailed location information should be included. | ||
* @return The source line of the step definition. | ||
*/ | ||
String getLocation(boolean detail); | ||
} |
19 changes: 19 additions & 0 deletions
19
core/src/main/java/io/cucumber/core/backend/DefaultParameterTransformerDefinition.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,19 @@ | ||
package io.cucumber.core.backend; | ||
|
||
import io.cucumber.cucumberexpressions.ParameterByTypeTransformer; | ||
import org.apiguardian.api.API; | ||
|
||
@API(status = API.Status.STABLE) | ||
public interface DefaultParameterTransformerDefinition { | ||
|
||
ParameterByTypeTransformer parameterByTypeTransformer(); | ||
|
||
/** | ||
* The source line where the default parameter transformer is defined. | ||
* Example: com/example/app/Cucumber.test():42 | ||
* | ||
* @param detail true if extra detailed location information should be included. | ||
* @return The source line of the step definition. | ||
*/ | ||
String getLocation(boolean detail); | ||
} |
15 changes: 0 additions & 15 deletions
15
core/src/main/java/io/cucumber/core/backend/DuplicateStepDefinitionException.java
This file was deleted.
Oops, something went wrong.
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
19 changes: 19 additions & 0 deletions
19
core/src/main/java/io/cucumber/core/backend/ParameterTypeDefinition.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,19 @@ | ||
package io.cucumber.core.backend; | ||
|
||
import io.cucumber.cucumberexpressions.ParameterType; | ||
import org.apiguardian.api.API; | ||
|
||
@API(status = API.Status.EXPERIMENTAL) | ||
public interface ParameterTypeDefinition { | ||
|
||
ParameterType<?> parameterType(); | ||
|
||
/** | ||
* The source line where the parameter type is defined. | ||
* Example: com/example/app/Cucumber.test():42 | ||
* | ||
* @param detail true if extra detailed location information should be included. | ||
* @return The source line of the step definition. | ||
*/ | ||
String getLocation(boolean detail); | ||
} |
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
Oops, something went wrong.