11# DataTables
22
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.
48
59See below the exhaustive list of possible mappings.
610
@@ -10,15 +14,25 @@ See below the exhaustive list of possible mappings.
1014Given the following table as Map of Map
1115| | key1 | key2 | key3 |
1216| row1 | val11 | val12 | val13 |
13- | row2 | val21 | val22 | val23 |
17+ | row2 | val21 | | val23 |
1418| row3 | val31 | val32 | val33 |
1519```
1620
1721``` 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:
1832Given (" the following table as Map of Map" ) { (table : JavaMap [String , JavaMap [String , String ]]) =>
1933 // Map(
2034 // "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"),
2236 // "row3" -> Map("key1" -> "val31", "key2" -> "val32", "key3" -> "val33")
2337 // )
2438}
@@ -30,15 +44,25 @@ Given("the following table as Map of Map") { (table: JavaMap[String, JavaMap[Str
3044Given the following table as List of Map
3145| key1 | key2 | key3 |
3246| val11 | val12 | val13 |
33- | val21 | val22 | val23 |
47+ | val21 | | val23 |
3448| val31 | val32 | val33 |
3549```
3650
3751``` 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:
3862Given (" the following table as List of Map" ) { (table : JavaList [JavaMap [String , String ]]) =>
3963 // Seq(
4064 // Map("key1" -> "val11", "key2" -> "val12", "key3" -> "val13"),
41- // Map("key1" -> "val21", "key2" -> "val22" , "key3" -> "val23"),
65+ // Map("key1" -> "val21", "key2" -> null , "key3" -> "val23"),
4266 // Map("key1" -> "val31", "key2" -> "val32", "key3" -> "val33")
4367 // )
4468}
@@ -49,15 +73,25 @@ Given("the following table as List of Map") { (table: JavaList[JavaMap[String, S
4973``` gherkin
5074Given the following table as Map of List
5175| row1 | val11 | val12 | val13 |
52- | row2 | val21 | val22 | val23 |
76+ | row2 | val21 | | val23 |
5377| row3 | val31 | val32 | val33 |
5478```
5579
5680``` 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:
5791Given (" the following table as Map of List" ) { (table : JavaMap [String , JavaList [String ]]) =>
5892 // Map(
5993 // "row1" -> Seq("val11", "val12", "val13"),
60- // "row2" -> Seq("val21", "val22" , "val23"),
94+ // "row2" -> Seq("val21", null , "val23"),
6195 // "row3" -> Seq("val31", "val32", "val33")
6296 // )
6397}
@@ -69,15 +103,25 @@ Given("the following table as Map of List") { (table: JavaMap[String, JavaList[S
69103``` gherkin
70104Given the following table as List of List
71105| val11 | val12 | val13 |
72- | val21 | val22 | val23 |
106+ | val21 | | val23 |
73107| val31 | val32 | val33 |
74108```
75109
76110``` 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:
77121Given (" the following table as List of List" ) { (table : JavaList [JavaList [String ]]) =>
78122 // Seq(
79123 // Seq("val11", "val12", "val13"),
80- // Seq("val21", "val22" , "val23"),
124+ // Seq("val21", null , "val23"),
81125 // Seq("val31", "val32", "val33")
82126 // )
83127}
@@ -88,15 +132,25 @@ Given("the following table as List of List") { (table: JavaList[JavaList[String]
88132``` gherkin
89133Given the following table as Map
90134| row1 | val11 |
91- | row2 | val21 |
135+ | row2 | |
92136| row3 | val31 |
93137```
94138
95139``` 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:
96150Given (" the following table as Map" ) { (table : JavaMap [String , String ]) =>
97151 // Map(
98152 // "row1" -> "val11",
99- // "row2" -> "val21" ,
153+ // "row2" -> null ,
100154 // "row3" -> "val31"
101155 // )
102156}
@@ -107,15 +161,25 @@ Given("the following table as Map") { (table: JavaMap[String, String]) =>
107161``` gherkin
108162Given the following table as List
109163| val11 |
110- | val21 |
164+ | |
111165| val31 |
112166```
113167
114168``` 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:
115179Given (" the following table as List" ) { (table : JavaList [String ]) =>
116180 // Seq(
117181 // "val11",
118- // "val21" ,
182+ // null ,
119183 // "val31"
120184 // )
121185}
0 commit comments