1
1
# DataTables
2
2
3
- Cucumber Scala support DataTables with Java types.
3
+ Cucumber Scala support DataTables with either:
4
+ - Scala types using a ` DataTable ` as step definition argument and implicit conversions by importing ` import io.cucumber.scala.Implicits._ `
5
+ - Java types in the step definitions arguments
6
+
7
+ ** The benefit of using Scala types** if that you will be handling ` Option ` s instead of potentially ` null ` values in the Java collections.
4
8
5
9
See below the exhaustive list of possible mappings.
6
10
@@ -10,15 +14,25 @@ See below the exhaustive list of possible mappings.
10
14
Given the following table as Map of Map
11
15
| | key1 | key2 | key3 |
12
16
| row1 | val11 | val12 | val13 |
13
- | row2 | val21 | val22 | val23 |
17
+ | row2 | val21 | | val23 |
14
18
| row3 | val31 | val32 | val33 |
15
19
```
16
20
17
21
``` scala
22
+ Given (" the following table as Map of Map" ) { (table : DataTable ) =>
23
+ val scalaTable : Map [String , Map [String , Option [String ]]] = table.asScalaRowColumnMap
24
+ // Map(
25
+ // "row1" -> Map("key1" -> Some("val11"), "key2" -> Some("val12"), "key3" -> Some("val13")),
26
+ // "row2" -> Map("key1" -> Some("val21"), "key2" -> None, "key3" -> Some("val23")),
27
+ // "row3" -> Map("key1" -> Some("val31"), "key2" -> Some("val32"), "key3" -> Some("val33"))
28
+ // )
29
+ }
30
+
31
+ // Or:
18
32
Given (" the following table as Map of Map" ) { (table : JavaMap [String , JavaMap [String , String ]]) =>
19
33
// Map(
20
34
// "row1" -> Map("key1" -> "val11", "key2" -> "val12", "key3" -> "val13"),
21
- // "row2" -> Map("key1" -> "val21", "key2" -> "val22" , "key3" -> "val23"),
35
+ // "row2" -> Map("key1" -> "val21", "key2" -> null , "key3" -> "val23"),
22
36
// "row3" -> Map("key1" -> "val31", "key2" -> "val32", "key3" -> "val33")
23
37
// )
24
38
}
@@ -30,15 +44,25 @@ Given("the following table as Map of Map") { (table: JavaMap[String, JavaMap[Str
30
44
Given the following table as List of Map
31
45
| key1 | key2 | key3 |
32
46
| val11 | val12 | val13 |
33
- | val21 | val22 | val23 |
47
+ | val21 | | val23 |
34
48
| val31 | val32 | val33 |
35
49
```
36
50
37
51
``` scala
52
+ Given (" the following table as List of Map" ) { (table : DataTable ) =>
53
+ val scalaTable : Seq [Map [String , Option [String ]]] = table.asScalaMaps
54
+ // Seq(
55
+ // Map("key1" -> Some("val11"), "key2" -> Some("val12"), "key3" -> Some("val13")),
56
+ // Map("key1" -> Some("val21"), "key2" -> None, "key3" -> Some("val23")),
57
+ // Map("key1" -> Some("val31"), "key2" -> Some("val32"), "key3" -> Some("val33"))
58
+ // )
59
+ }
60
+
61
+ // Or:
38
62
Given (" the following table as List of Map" ) { (table : JavaList [JavaMap [String , String ]]) =>
39
63
// Seq(
40
64
// Map("key1" -> "val11", "key2" -> "val12", "key3" -> "val13"),
41
- // Map("key1" -> "val21", "key2" -> "val22" , "key3" -> "val23"),
65
+ // Map("key1" -> "val21", "key2" -> null , "key3" -> "val23"),
42
66
// Map("key1" -> "val31", "key2" -> "val32", "key3" -> "val33")
43
67
// )
44
68
}
@@ -49,15 +73,25 @@ Given("the following table as List of Map") { (table: JavaList[JavaMap[String, S
49
73
``` gherkin
50
74
Given the following table as Map of List
51
75
| row1 | val11 | val12 | val13 |
52
- | row2 | val21 | val22 | val23 |
76
+ | row2 | val21 | | val23 |
53
77
| row3 | val31 | val32 | val33 |
54
78
```
55
79
56
80
``` scala
81
+ Given (" the following table as Map of List" ) { (table : DataTable ) =>
82
+ val scalaTable : Map [Seq [Option [String ]]] = table.asScalaRowMap
83
+ // Map(
84
+ // "row1" -> Seq(Some("val11"), Some("val12"), Some("val13")),
85
+ // "row2" -> Seq(Some("val21"), None, Some("val23")),
86
+ // "row3" -> Seq(Some("val31"), Some("val32"), Some("val33"))
87
+ // )
88
+ }
89
+
90
+ // Or:
57
91
Given (" the following table as Map of List" ) { (table : JavaMap [String , JavaList [String ]]) =>
58
92
// Map(
59
93
// "row1" -> Seq("val11", "val12", "val13"),
60
- // "row2" -> Seq("val21", "val22" , "val23"),
94
+ // "row2" -> Seq("val21", null , "val23"),
61
95
// "row3" -> Seq("val31", "val32", "val33")
62
96
// )
63
97
}
@@ -69,15 +103,25 @@ Given("the following table as Map of List") { (table: JavaMap[String, JavaList[S
69
103
``` gherkin
70
104
Given the following table as List of List
71
105
| val11 | val12 | val13 |
72
- | val21 | val22 | val23 |
106
+ | val21 | | val23 |
73
107
| val31 | val32 | val33 |
74
108
```
75
109
76
110
``` scala
111
+ Given (" the following table as List of List" ) { (table : DataTable ) =>
112
+ val scalaTable : Seq [Seq [Option [String ]]] = table.asScalaLists
113
+ // Seq(
114
+ // Seq(Some("val11"), Some("val12"), Some("val13")),
115
+ // Seq(Some("val21"), None, Some("val23")),
116
+ // Seq(Some("val31"), Some("val32"), Some("val33"))
117
+ // )
118
+ }
119
+
120
+ // Or:
77
121
Given (" the following table as List of List" ) { (table : JavaList [JavaList [String ]]) =>
78
122
// Seq(
79
123
// Seq("val11", "val12", "val13"),
80
- // Seq("val21", "val22" , "val23"),
124
+ // Seq("val21", null , "val23"),
81
125
// Seq("val31", "val32", "val33")
82
126
// )
83
127
}
@@ -88,15 +132,25 @@ Given("the following table as List of List") { (table: JavaList[JavaList[String]
88
132
``` gherkin
89
133
Given the following table as Map
90
134
| row1 | val11 |
91
- | row2 | val21 |
135
+ | row2 | |
92
136
| row3 | val31 |
93
137
```
94
138
95
139
``` scala
140
+ Given (" the following table as Map" ) { (table : DataTable ) =>
141
+ val scalaTable : Map [String , Option [String ]] = table.asScalaMap[String , String ]
142
+ // Map(
143
+ // "row1" -> Some("val11"),
144
+ // "row2" -> None,
145
+ // "row3" -> Some("val31")
146
+ // )
147
+ }
148
+
149
+ // Or:
96
150
Given (" the following table as Map" ) { (table : JavaMap [String , String ]) =>
97
151
// Map(
98
152
// "row1" -> "val11",
99
- // "row2" -> "val21" ,
153
+ // "row2" -> null ,
100
154
// "row3" -> "val31"
101
155
// )
102
156
}
@@ -107,15 +161,25 @@ Given("the following table as Map") { (table: JavaMap[String, String]) =>
107
161
``` gherkin
108
162
Given the following table as List
109
163
| val11 |
110
- | val21 |
164
+ | |
111
165
| val31 |
112
166
```
113
167
114
168
``` scala
169
+ Given (" the following table as List" ) { (table : DataTable ) =>
170
+ val scalaTable : Seq [Option [String ]] = table.asScalaList
171
+ // Seq(
172
+ // Some("val11"),
173
+ // None,
174
+ // Some("val31")
175
+ // )
176
+ }
177
+
178
+ // Or:
115
179
Given (" the following table as List" ) { (table : JavaList [String ]) =>
116
180
// Seq(
117
181
// "val11",
118
- // "val21" ,
182
+ // null ,
119
183
// "val31"
120
184
// )
121
185
}
0 commit comments