|
| 1 | +package io.cucumber.scala |
| 2 | + |
| 3 | +import io.cucumber.datatable.DataTable |
| 4 | +import io.cucumber.scala.ScalaDslDataTableTypeTest.{Author, Cell, GroupOfAuthor} |
| 5 | +import org.junit.Assert.assertEquals |
| 6 | +import org.junit.Test |
| 7 | + |
| 8 | +import scala.collection.JavaConverters._ |
| 9 | + |
| 10 | +object ScalaDslDataTableTypeTest { |
| 11 | + |
| 12 | + private case class GroupOfAuthor(authors: Seq[Author]) |
| 13 | + |
| 14 | + private case class Author(name: String, surname: String, famousBook: String) |
| 15 | + |
| 16 | + private case class Cell(cell: String) |
| 17 | + |
| 18 | + val DATATABLE: Seq[Seq[String]] = Seq( |
| 19 | + Seq("name", "surname", "famousBook"), |
| 20 | + Seq("Alan", "Alou", "The Lion King"), |
| 21 | + Seq("Robert", "Bob", "Le Petit Prince") |
| 22 | + ) |
| 23 | + |
| 24 | + val DATATABLE_WITH_EMPTY: Seq[Seq[String]] = Seq( |
| 25 | + Seq("name", "surname", "famousBook"), |
| 26 | + Seq("Alan", "Alou", "The Lion King"), |
| 27 | + Seq("Robert", "[empty]", "Le Petit Prince") |
| 28 | + ) |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +class ScalaDslDataTableTypeTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + def testDataTableEntryType(): Unit = { |
| 36 | + |
| 37 | + class Glue extends ScalaDsl with EN { |
| 38 | + DataTableType { entry: Map[String, String] => |
| 39 | + Author(entry("name"), entry("surname"), entry("famousBook")) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + val glue = new Glue() |
| 44 | + |
| 45 | + val expected = Seq( |
| 46 | + Author("Alan", "Alou", "The Lion King"), |
| 47 | + Author("Robert", "Bob", "Le Petit Prince") |
| 48 | + ).asJava |
| 49 | + assertClassDataTableType(glue.registry.dataTableTypes.head, Seq(), ScalaDslDataTableTypeTest.DATATABLE, expected) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + def testDataTableEntryTypeWithReplacement(): Unit = { |
| 54 | + |
| 55 | + class Glue extends ScalaDsl with EN { |
| 56 | + DataTableType("[empty]") { (entry: Map[String, String]) => |
| 57 | + Author(entry("name"), entry("surname"), entry("famousBook")) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + val glue = new Glue() |
| 62 | + |
| 63 | + val expected = Seq( |
| 64 | + Author("Alan", "Alou", "The Lion King"), |
| 65 | + Author("Robert", "", "Le Petit Prince") |
| 66 | + ).asJava |
| 67 | + assertClassDataTableType(glue.registry.dataTableTypes.head, Seq("[empty]"), ScalaDslDataTableTypeTest.DATATABLE_WITH_EMPTY, expected) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + def testDataTableRowType(): Unit = { |
| 72 | + |
| 73 | + class Glue extends ScalaDsl with EN { |
| 74 | + DataTableType { row: Seq[String] => |
| 75 | + Author(row(0), row(1), row(2)) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + val glue = new Glue() |
| 80 | + |
| 81 | + val expected = Seq( |
| 82 | + Author("Alan", "Alou", "The Lion King"), |
| 83 | + Author("Robert", "Bob", "Le Petit Prince") |
| 84 | + ).asJava |
| 85 | + assertClassDataTableType(glue.registry.dataTableTypes.head, Seq(), ScalaDslDataTableTypeTest.DATATABLE.drop(1), expected) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + def testDataTableRowTypeWithReplacement(): Unit = { |
| 90 | + |
| 91 | + class Glue extends ScalaDsl with EN { |
| 92 | + DataTableType("[empty]") { (row: Seq[String]) => |
| 93 | + Author(row(0), row(1), row(2)) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + val glue = new Glue() |
| 98 | + |
| 99 | + val expected = Seq( |
| 100 | + Author("Alan", "Alou", "The Lion King"), |
| 101 | + Author("Robert", "", "Le Petit Prince") |
| 102 | + ).asJava |
| 103 | + assertClassDataTableType(glue.registry.dataTableTypes.head, Seq("[empty]"), ScalaDslDataTableTypeTest.DATATABLE_WITH_EMPTY.drop(1), expected) |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + def testDataTableCellType(): Unit = { |
| 108 | + |
| 109 | + class Glue extends ScalaDsl with EN { |
| 110 | + DataTableType { cell: String => |
| 111 | + Cell(cell) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + val glue = new Glue() |
| 116 | + |
| 117 | + val expected = Seq( |
| 118 | + Seq(Cell("Alan"), Cell("Alou"), Cell("The Lion King")).asJava, |
| 119 | + Seq(Cell("Robert"), Cell("Bob"), Cell("Le Petit Prince")).asJava |
| 120 | + ).asJava |
| 121 | + assertClassDataTableType(glue.registry.dataTableTypes.head, Seq(), ScalaDslDataTableTypeTest.DATATABLE.drop(1), expected) |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + def testDataTableCellTypeWithReplacement(): Unit = { |
| 126 | + |
| 127 | + class Glue extends ScalaDsl with EN { |
| 128 | + DataTableType("[empty]") { (cell: String) => |
| 129 | + Cell(cell) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + val glue = new Glue() |
| 134 | + |
| 135 | + val expected = Seq( |
| 136 | + Seq(Cell("Alan"), Cell("Alou"), Cell("The Lion King")).asJava, |
| 137 | + Seq(Cell("Robert"), Cell(""), Cell("Le Petit Prince")).asJava |
| 138 | + ).asJava |
| 139 | + assertClassDataTableType(glue.registry.dataTableTypes.head, Seq("[empty]"), ScalaDslDataTableTypeTest.DATATABLE_WITH_EMPTY.drop(1), expected) |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + def testClassDataTableTableType(): Unit = { |
| 144 | + |
| 145 | + class Glue extends ScalaDsl with EN { |
| 146 | + DataTableType { table: DataTable => |
| 147 | + val authors = table.asMaps().asScala |
| 148 | + .map(_.asScala) |
| 149 | + .map(entry => Author(entry("name"), entry("surname"), entry("famousBook"))) |
| 150 | + .toSeq |
| 151 | + GroupOfAuthor(authors) |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + val glue = new Glue() |
| 156 | + |
| 157 | + val expected = GroupOfAuthor(Seq( |
| 158 | + Author("Alan", "Alou", "The Lion King"), |
| 159 | + Author("Robert", "Bob", "Le Petit Prince") |
| 160 | + )) |
| 161 | + assertClassDataTableType(glue.registry.dataTableTypes.head, Seq(), ScalaDslDataTableTypeTest.DATATABLE, expected) |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + def testClassDataTableTableTypeWithReplacement(): Unit = { |
| 166 | + |
| 167 | + class Glue extends ScalaDsl with EN { |
| 168 | + DataTableType("[empty]") { (table: DataTable) => |
| 169 | + val authors = table.asMaps().asScala |
| 170 | + .map(_.asScala) |
| 171 | + .map(entry => Author(entry("name"), entry("surname"), entry("famousBook"))) |
| 172 | + .toSeq |
| 173 | + GroupOfAuthor(authors) |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + val glue = new Glue() |
| 178 | + |
| 179 | + val expected = GroupOfAuthor(Seq( |
| 180 | + Author("Alan", "Alou", "The Lion King"), |
| 181 | + Author("Robert", "", "Le Petit Prince") |
| 182 | + )) |
| 183 | + assertClassDataTableType(glue.registry.dataTableTypes.head, Seq("[empty]"), ScalaDslDataTableTypeTest.DATATABLE_WITH_EMPTY, expected) |
| 184 | + } |
| 185 | + |
| 186 | + // -------------------- Test on object -------------------- |
| 187 | + // Note: for now there is no difference between the two in ScalaDsl but better safe than sorry |
| 188 | + |
| 189 | + @Test |
| 190 | + def testObjectDataTableEntryType(): Unit = { |
| 191 | + |
| 192 | + object Glue extends ScalaDsl with EN { |
| 193 | + DataTableType { entry: Map[String, String] => |
| 194 | + Author(entry("name"), entry("surname"), entry("famousBook")) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + val expected = Seq( |
| 199 | + Author("Alan", "Alou", "The Lion King"), |
| 200 | + Author("Robert", "Bob", "Le Petit Prince") |
| 201 | + ).asJava |
| 202 | + assertObjectDataTableType(Glue.registry.dataTableTypes.head, Seq(), ScalaDslDataTableTypeTest.DATATABLE, expected) |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + def testObjectDataTableEntryTypeWithReplacement(): Unit = { |
| 207 | + |
| 208 | + object Glue extends ScalaDsl with EN { |
| 209 | + DataTableType("[empty]") { (entry: Map[String, String]) => |
| 210 | + Author(entry("name"), entry("surname"), entry("famousBook")) |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + val expected = Seq( |
| 215 | + Author("Alan", "Alou", "The Lion King"), |
| 216 | + Author("Robert", "", "Le Petit Prince") |
| 217 | + ).asJava |
| 218 | + assertObjectDataTableType(Glue.registry.dataTableTypes.head, Seq("[empty]"), ScalaDslDataTableTypeTest.DATATABLE_WITH_EMPTY, expected) |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + def testObjectDataTableRowType(): Unit = { |
| 223 | + |
| 224 | + object Glue extends ScalaDsl with EN { |
| 225 | + DataTableType { row: Seq[String] => |
| 226 | + Author(row(0), row(1), row(2)) |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + val expected = Seq( |
| 231 | + Author("Alan", "Alou", "The Lion King"), |
| 232 | + Author("Robert", "Bob", "Le Petit Prince") |
| 233 | + ).asJava |
| 234 | + assertObjectDataTableType(Glue.registry.dataTableTypes.head, Seq(), ScalaDslDataTableTypeTest.DATATABLE.drop(1), expected) |
| 235 | + } |
| 236 | + |
| 237 | + @Test |
| 238 | + def testObjectDataTableRowTypeWithReplacement(): Unit = { |
| 239 | + |
| 240 | + object Glue extends ScalaDsl with EN { |
| 241 | + DataTableType("[empty]") { (row: Seq[String]) => |
| 242 | + Author(row(0), row(1), row(2)) |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + val expected = Seq( |
| 247 | + Author("Alan", "Alou", "The Lion King"), |
| 248 | + Author("Robert", "", "Le Petit Prince") |
| 249 | + ).asJava |
| 250 | + assertObjectDataTableType(Glue.registry.dataTableTypes.head, Seq("[empty]"), ScalaDslDataTableTypeTest.DATATABLE_WITH_EMPTY.drop(1), expected) |
| 251 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + def testObjectDataTableCellType(): Unit = { |
| 255 | + |
| 256 | + object Glue extends ScalaDsl with EN { |
| 257 | + DataTableType { cell: String => |
| 258 | + Cell(cell) |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + val expected = Seq( |
| 263 | + Seq(Cell("Alan"), Cell("Alou"), Cell("The Lion King")).asJava, |
| 264 | + Seq(Cell("Robert"), Cell("Bob"), Cell("Le Petit Prince")).asJava |
| 265 | + ).asJava |
| 266 | + assertObjectDataTableType(Glue.registry.dataTableTypes.head, Seq(), ScalaDslDataTableTypeTest.DATATABLE.drop(1), expected) |
| 267 | + } |
| 268 | + |
| 269 | + @Test |
| 270 | + def testObjectDataTableCellTypeWithReplacement(): Unit = { |
| 271 | + |
| 272 | + object Glue extends ScalaDsl with EN { |
| 273 | + DataTableType("[empty]") { (cell: String) => |
| 274 | + Cell(cell) |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + val expected = Seq( |
| 279 | + Seq(Cell("Alan"), Cell("Alou"), Cell("The Lion King")).asJava, |
| 280 | + Seq(Cell("Robert"), Cell(""), Cell("Le Petit Prince")).asJava |
| 281 | + ).asJava |
| 282 | + assertObjectDataTableType(Glue.registry.dataTableTypes.head, Seq("[empty]"), ScalaDslDataTableTypeTest.DATATABLE_WITH_EMPTY.drop(1), expected) |
| 283 | + } |
| 284 | + |
| 285 | + @Test |
| 286 | + def testObjectDataTableTableType(): Unit = { |
| 287 | + |
| 288 | + object Glue extends ScalaDsl with EN { |
| 289 | + DataTableType { table: DataTable => |
| 290 | + val authors = table.asMaps().asScala |
| 291 | + .map(_.asScala) |
| 292 | + .map(entry => Author(entry("name"), entry("surname"), entry("famousBook"))) |
| 293 | + .toSeq |
| 294 | + GroupOfAuthor(authors) |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + val expected = GroupOfAuthor(Seq( |
| 299 | + Author("Alan", "Alou", "The Lion King"), |
| 300 | + Author("Robert", "Bob", "Le Petit Prince") |
| 301 | + )) |
| 302 | + assertObjectDataTableType(Glue.registry.dataTableTypes.head, Seq(), ScalaDslDataTableTypeTest.DATATABLE, expected) |
| 303 | + } |
| 304 | + |
| 305 | + @Test |
| 306 | + def testObjectDataTableTableTypeWithReplacement(): Unit = { |
| 307 | + |
| 308 | + object Glue extends ScalaDsl with EN { |
| 309 | + DataTableType("[empty]") { (table: DataTable) => |
| 310 | + val authors = table.asMaps().asScala |
| 311 | + .map(_.asScala) |
| 312 | + .map(entry => Author(entry("name"), entry("surname"), entry("famousBook"))) |
| 313 | + .toSeq |
| 314 | + GroupOfAuthor(authors) |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + val expected = GroupOfAuthor(Seq( |
| 319 | + Author("Alan", "Alou", "The Lion King"), |
| 320 | + Author("Robert", "", "Le Petit Prince") |
| 321 | + )) |
| 322 | + assertObjectDataTableType(Glue.registry.dataTableTypes.head, Seq("[empty]"), ScalaDslDataTableTypeTest.DATATABLE_WITH_EMPTY, expected) |
| 323 | + } |
| 324 | + |
| 325 | + private def assertClassDataTableType(details: ScalaDataTableTypeDetails[_], emptyPatterns: Seq[String], dataTable: Seq[Seq[String]], expectedObj: Any): Unit = { |
| 326 | + assertDataTableType(ScalaDataTableTypeDefinition(details, true), emptyPatterns, dataTable, expectedObj) |
| 327 | + } |
| 328 | + |
| 329 | + private def assertObjectDataTableType(details: ScalaDataTableTypeDetails[_], emptyPatterns: Seq[String], dataTable: Seq[Seq[String]], expectedObj: Any): Unit = { |
| 330 | + assertDataTableType(ScalaDataTableTypeDefinition(details, false), emptyPatterns, dataTable, expectedObj) |
| 331 | + } |
| 332 | + |
| 333 | + private def assertDataTableType(dataTableType: ScalaDataTableTypeDefinition, emptyPatterns: Seq[String], dataTable: Seq[Seq[String]], expectedObj: Any): Unit = { |
| 334 | + assertEquals(emptyPatterns, dataTableType.emptyPatterns) |
| 335 | + |
| 336 | + val obj = dataTableType.dataTableType.transform(dataTable.map(_.asJava).asJava) |
| 337 | + assertEquals(expectedObj, obj) |
| 338 | + } |
| 339 | + |
| 340 | +} |
0 commit comments