Skip to content

Commit 60e6380

Browse files
committed
[Java] Update docs around data tables
1 parent 9578337 commit 60e6380

File tree

3 files changed

+114
-17
lines changed

3 files changed

+114
-17
lines changed

java/README.md

+51-4
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,16 @@ public class Steps {
9393

9494
### Data Table Type
9595

96-
Data table types can be declared by annotating a method with `@DataTableType`. Depending on the parameter type this
97-
will be either a:
96+
Data table types can be declared by annotating a method with `@DataTableType`.
97+
Depending on the parameter type this will be either a:
9898
* `String` -> `io.cucumber.datatable.TableCellTranformer`
99-
* `Map<String,String>` -> `io.cucumber.datatable.TableEntry`
100-
* `List<String` -> `io.cucumber.datatable.TableRow`
99+
* `Map<String,String>` -> `io.cucumber.datatable.TableEntryTransformer`
100+
* `List<String` -> `io.cucumber.datatable.TableRowTranformer`
101101
* `DataTable` -> `io.cucumber.datatable.TableTransformer`
102102

103+
For a full list of transformations that can be achieved with data table types
104+
see [cucumber/datatable](https://github.com/cucumber/cucumber/tree/master/datatable)
105+
103106
```java
104107
package com.example.app;
105108

@@ -204,3 +207,47 @@ public class DataTableSteps {
204207
}
205208
}
206209
```
210+
211+
# Transposing Tables
212+
213+
A data table can be transposed by annotating the data table parameter (or the
214+
parameter the data table will be converted into) with `@Transpose`. This means
215+
the keys will be in the first column rather then the first row.
216+
217+
218+
```gherkin
219+
Given the user is
220+
| firstname | Roberto |
221+
| lastname | Lo Giacco |
222+
| nationality | Italian |
223+
```
224+
225+
And a data table type to create a User
226+
227+
```java
228+
package com.example.app;
229+
230+
import io.cucumber.java.DataTableType;
231+
import io.cucumber.java.en.Given;
232+
import io.cucumber.java.Transpose;
233+
234+
import java.util.Map;
235+
import java.util.List;
236+
237+
public class DataTableSteps {
238+
239+
@DataTableType
240+
public User convert(Map<String, String> entry){
241+
return new User(
242+
entry.get("firstname"),
243+
entry.get("lastname")
244+
entry.get("nationality")
245+
);
246+
}
247+
248+
@Given("the user is")
249+
public void the_user_is(@Transpose User user){
250+
// user = [User(firstname="Roberto", lastname="Lo Giacco", nationality="Italian")
251+
}
252+
}
253+
```

java/src/main/java/io/cucumber/java/Transpose.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,16 @@
2323
* And a data table type to create a User
2424
*
2525
* <pre>
26-
* typeRegistry.defineDataTableType(new DataTableType(
27-
* Author.class,
28-
* new TableEntryTransformer&lt;User&gt;() {
29-
* &#064;Override
30-
* public Author transform(Map&lt;String, String&gt; entry) {
31-
* return new User(
32-
* entry.get("firstName"),
33-
* entry.get("lastName"),
34-
* entry.get("nationality"));
35-
* }
36-
* }));
37-
*
26+
* {@code
27+
* @DataTableType
28+
* public User convert(Map<String, String> entry){
29+
* return new User(
30+
* entry.get("firstname"),
31+
* entry.get("lastname")
32+
* entry.get("nationality")
33+
* );
34+
* }
35+
* }
3836
* </pre>
3937
* Then the following Java Step Definition would convert that into an User object:
4038
* <pre>

java8/README.md

+53-1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ Jackson to quickly transform well known string representations to Java objects.
119119
* `DefaultDataTableEntryTransformer`
120120
* `DefaultDataTableCellTransformer`
121121

122+
For a full list of transformations that can be achieved with data table types
123+
see [cucumber/datatable](https://github.com/cucumber/cucumber/tree/master/datatable)
124+
122125
```java
123126
package com.example.app;
124127

@@ -186,4 +189,53 @@ public class StepDefinitions implements En {
186189
});
187190
}
188191
}
189-
```
192+
```
193+
194+
195+
# Transposing Tables
196+
197+
A data table can be transposed by calling `.transpose()`. This means the keys
198+
will be in the first column rather then the first row.
199+
200+
201+
```gherkin
202+
Given the user is
203+
| firstname | Roberto |
204+
| lastname | Lo Giacco |
205+
| nationality | Italian |
206+
```
207+
208+
And a data table type to create a User
209+
210+
```java
211+
package com.example.app;
212+
213+
import io.cucumber.datatable.DataTable;
214+
215+
import io.cucumber.java8.En;
216+
217+
import java.util.List;
218+
import java.util.Map;
219+
import java.util.Optional;
220+
221+
import static org.junit.jupiter.api.Assertions.assertEquals;
222+
import static org.junit.jupiter.api.Assertions.assertNotSame;
223+
import static org.junit.jupiter.api.Assertions.assertSame;
224+
import static org.junit.jupiter.api.Assertions.assertTrue;
225+
226+
public class StepDefinitions implements En {
227+
228+
public StepDefinitions() {
229+
DataTableType((Map<String, String> entry) -> new User(
230+
entry.get("firstname"),
231+
entry.get("lastname")
232+
entry.get("nationality")
233+
));
234+
235+
Given("the user is", (DataTable authorsTable) -> {
236+
User user = authorsTable.transpose().asList(User.class);
237+
// user = User(firstname="Roberto", lastname="Lo Giacco", nationality="Italian")
238+
});
239+
}
240+
}
241+
```

0 commit comments

Comments
 (0)