You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CsvOpsSpec.scala 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package de.codingchallenge.csv
  2. import org.scalatest.{MustMatchers, WordSpec}
  3. import CsvOps._
  4. import scala.util.{Success, Try}
  5. class CsvOpsSpec extends WordSpec with MustMatchers{
  6. case class CsvTestData(s: String, d: Double)
  7. implicit val csvReads: CsvColumnReads[CsvTestData] = (columns: Seq[String]) => {
  8. Try{(columns.head, columns(1).toDouble)} match{
  9. case Success((s: String, d: Double)) => Some(CsvTestData(s,d))
  10. case _ => None
  11. }
  12. }
  13. implicit val csvWrites: CsvColumnWrites[CsvTestData] = (csvTestData: CsvTestData) =>
  14. csvTestData.productIterator.map(_.toString).toSeq
  15. "The CsvOps reading operations" should {
  16. "parse a csv line with the default delimiter as expected" in {
  17. val csvLine = "myStringVal|3.0"
  18. csvLine.csvToOptOf[CsvTestData] mustBe Some(CsvTestData("myStringVal", 3d))
  19. }
  20. "parse a csv line with expected delimiter" in {
  21. val csvLine = "myStringVal,3.0"
  22. csvLine.csvToOptOf[CsvTestData](",") mustBe Some(CsvTestData("myStringVal", 3d))
  23. }
  24. }
  25. "The CsvOps writing operations" should {
  26. "write a csv line with default delimiter" in {
  27. val expectedLine = "myStringVal|3.0"
  28. val testData = CsvTestData("myStringVal", 3)
  29. testData.toCsvLine mustBe expectedLine
  30. }
  31. "write a csv line with expected delimiter" in {
  32. val expectedCsvLine = "myStringVal,3.0"
  33. val testData = CsvTestData("myStringVal", 3)
  34. testData.toCsvLine(",") mustBe expectedCsvLine
  35. }
  36. }
  37. }