@@ -87,7 +87,7 @@ This can be achieved in different ways:
8787- transform cells content to any type
8888
8989Note that DataTables in Gherkin can not represent ` null ` or the empty string unambiguously.
90- Cucumber will interpret empty cells as ` null ` .
90+ Cucumber will interpret empty cells as ` None ` or ` null ` .
9191But you can use a replacement to represent empty strings.
9292See below.
9393
@@ -99,8 +99,8 @@ For instance, the following transformer can be defined:
9999``` scala
100100case class Author (name : String , surname : String , famousBook : String )
101101
102- DataTableType { entry : Map [String , String ] =>
103- Author (entry(" name" ), entry(" surname" ), entry(" famousBook" ))
102+ DataTableType { entry : Map [String , Option [ String ]] => // Or Map[String, String]
103+ Author (entry(" name" ).getOrElse( " NoValue " ) , entry(" surname" ).getOrElse( " NoValue " ) , entry(" famousBook" ).getOrElse( " NoValue " ))
104104}
105105```
106106
@@ -113,13 +113,15 @@ Given the following authors
113113```
114114
115115``` scala
116- Given (" the following authors" ) { (authors : java.util.List [Author ]) =>
117- // Do something
118- }
116+ import io .cucumber .scala .Implicits ._
119117
120- // Or using DataTable
121118Given (" the following authors" ) { (table : DataTable ) =>
122- val authors = table.asList[Author ](classOf [Author ])
119+ val authors = table.asScalaRawList[Author ]
120+ }
121+
122+ // Or using Java type
123+ Given (" the following authors" ) { (authors : java.util.List [Author ]) =>
124+ // Do something
123125}
124126```
125127
@@ -129,8 +131,8 @@ For instance, the following transformer can be defined:
129131``` scala
130132case class Author (name : String , surname : String , famousBook : String )
131133
132- DataTableType { row : Seq [String ] =>
133- Author (row(0 ), row(1 ), row(2 ))
134+ DataTableType { row : Seq [Option [ String ]] => // Or Seq[String]
135+ Author (row(0 ).getOrElse( " NoValue " ) , row(1 ).getOrElse( " NoValue " ) , row(2 ).getOrElse( " NoValue " ))
134136}
135137```
136138
@@ -142,20 +144,24 @@ Given the following authors
142144```
143145
144146``` scala
145- Given (" the following authors" ) { (authors : java.util.List [Author ]) =>
146- // Do something
147- }
147+ import io .cucumber .scala .Implicits ._
148148
149- // Or using DataTable
150149Given (" the following authors" ) { (table : DataTable ) =>
151- val authors = table.asList[Author ](classOf [Author ])
150+ val authors = table.asScalaRawList[Author ]
151+ }
152+
153+ // Or using Java types
154+ Given (" the following authors" ) { (authors : java.util.List [Author ]) =>
155+ // Do something
152156}
153157```
154158
155159### DataTable
156160
157161For instance, the following transformer can be defined:
158162``` scala
163+ import io .cucumber .scala .Implicits ._
164+
159165case class Author (name : String , surname : String , famousBook : String )
160166case class GroupOfAuthor (authors : Seq [Author ])
161167
@@ -191,8 +197,8 @@ For instance, the following transformer can be defined:
191197``` scala
192198case class RichCell (content : String )
193199
194- DataTableType { cell : String =>
195- RichCell (cell)
200+ DataTableType { cell : Option [ String ] => // Or String
201+ RichCell (cell.getOrElse( " NoValue " ) )
196202}
197203```
198204
@@ -204,13 +210,15 @@ Given the following authors
204210```
205211
206212``` scala
207- Given (" the following authors" ) { (authors : java.util.List [java.util.List [RichCell ]]) =>
208- // Do something
209- }
213+ import io .cucumber .scala .Implicits ._
210214
211- // Or using DataTable
212215Given (" the following authors" ) { (table : DataTable ) =>
213- val authors = table.asLists[RichCell ](classOf [RichCell ]))
216+ val authors = table.asScalaRawLists[RichCell ]
217+ }
218+
219+ // Or using Java types
220+ Given (" the following authors" ) { (authors : java.util.List [java.util.List [RichCell ]]) =>
221+ // Do something
214222}
215223```
216224
@@ -223,19 +231,21 @@ Given the following authors
223231```
224232
225233``` scala
226- Given (" the following authors" ) { (authors : java.util.List [java.util.Map [String , RichCell ]]) =>
227- // Do something
228- }
234+ import io .cucumber .scala .Implicits ._
229235
230- // Or using DataTable
231236Given (" the following authors" ) { (table : DataTable ) =>
232- val authors = table.asMaps[String , RichCell ](classOf [String ], classOf [RichCell ])
237+ val authors = table.asScalaRawMaps[String , RichCell ]
238+ }
239+
240+ // Or with Java Types
241+ Given (" the following authors" ) { (authors : java.util.List [java.util.Map [String , RichCell ]]) =>
242+ // Do something
233243}
234244```
235245
236246### Empty values
237247
238- By default empty values in DataTable are treated as ` null ` by Cucumber.
248+ By default empty values in DataTable are treated as ` None ` or ` null ` by Cucumber.
239249If you need to have empty values, you can define a replacement like ` [empty] ` that will be automatically replaced to empty when parsing DataTable.
240250
241251To do so, you can add a parameter to a ` DataTableType ` definition.
@@ -244,7 +254,7 @@ For instance, with the following definition:
244254``` scala
245255case class Author (name : String , surname : String , famousBook : String )
246256
247- DataTableType (" [empty]" ) { (entry : Map [String , String ]) =>
257+ DataTableType (" [empty]" ) { (entry : Map [String , String ]) => // Or Map[String, Option[String]]
248258 Author (entry(" name" ), entry(" surname" ), entry(" famousBook" ))
249259}
250260```
@@ -296,13 +306,15 @@ DefaultDataTableEntryTransformer("[empty]") { (fromValue: Map[String, String], t
296306
297307Will be used to convert with such step definitions:
298308``` scala
299- Given (" A step with a datatable" ) { (rows : java.util.List [SomeType ]) =>
300- // Do something
301- }
309+ import io .cucumber .scala .Implicits ._
302310
303- // Or DataTable
304311Given (" A step with a datatable" ) { (dataTable : DataTable ) =>
305- val table = dataTable.asList[SomeType ](classOf [SomeType ])
312+ val table = dataTable.asScalaRawList[SomeType ]
313+ }
314+
315+ // Or with Java types
316+ Given (" A step with a datatable" ) { (rows : java.util.List [SomeType ]) =>
317+ // Do something
306318}
307319```
308320
@@ -319,12 +331,14 @@ DefaultDataTableCellTransformer("[empty]") { (fromValue: String, toValueType: ja
319331
320332Will be used to convert with such step definitions:
321333``` scala
322- Given (" A step with a datatable" ) { (rows : java.util.List [java.util.List [SomeType ]]) =>
323- // Do something
324- }
334+ import io .cucumber .scala .Implicits ._
325335
326- // Or DataTable
327336Given (" A step with a datatable" ) { (dataTable : DataTable ) =>
328- val table = dataTable.asLists[SomeType ](classOf [SomeType ])
337+ val table = dataTable.asScalaRawLists[SomeType ]
338+ }
339+
340+ // Or with Java Types
341+ Given (" A step with a datatable" ) { (rows : java.util.List [java.util.List [SomeType ]]) =>
342+ // Do something
329343}
330344```
0 commit comments