Skip to content

Commit

Permalink
Add unit test for the incorrect CSV line
Browse files Browse the repository at this point in the history
  • Loading branch information
george-zubrienko committed Dec 10, 2024
1 parent 394b4ba commit a7220eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ object CSVParser:
case _ => Some(line.slice(from = fromIndex, until = currentPosition))

def scanLine(charIndex: Int, quoteSum: Int, prevCharIndex: Int): IndexedSeq[Option[String]] = {
if charIndex >= line.length then
throw new IllegalStateException(s"Failed to split a CSV line $line with delimiter $delimiter into a value sequence")

line(charIndex) match
// recursive case in a quoted line - opening quote - move on
case '"' if charIndex < line.length && quoteSum == 0 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,22 @@ class CdmParserTests extends AnyFlatSpec with Matchers {
("5637144576,\"NFO\",,0,", Seq(Some("5637144576"), Some("NFO"), None, Some("0"), None))
)

private val invalidCsvlines = Table(
("line", "result"),
("\"q\",\",\"1321\"", Seq())
)

it should "handle valid quoted CSV lines correctly" in {
forAll (validCsvLines) { (line, result) =>
CSVParser.parseCsvLine(line) should equal(result)
}
}

it should "handle invalid quoted CSV lines correctly" in {
forAll (invalidCsvlines) { (line, _) =>
intercept[IllegalStateException] {
CSVParser.parseCsvLine(line)
}
}
}
}

0 comments on commit a7220eb

Please sign in to comment.