Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class CsvFileWriter internal constructor(
willWritePreTerminator()
rows.forEachIndexed { index, list ->
writeNext(list)
if (index < rows.size - 1) {
if (index < rows.size - 1 && list.isNotEmpty()) {
writeTerminator()
}
}
Expand All @@ -67,8 +67,9 @@ class CsvFileWriter internal constructor(

val itr = rows.iterator()
while (itr.hasNext()) {
writeNext(itr.next())
if (itr.hasNext()) writeTerminator()
val row = itr.next()
writeNext(row)
if (itr.hasNext() && row.isNotEmpty()) writeTerminator()
}

willWriteEndTerminator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ class CsvFileWriterTest : WordSpec({
val actual = readTestFile()
actual shouldBe expected
}
"write no line terminator when row is empty for rows from list" {
val rows = listOf(listOf("a", "b", "c"), listOf(), listOf("d", "e", "f"))
val expected = "a,b,c\r\nd,e,f\r\n"
csvWriter().open(testFileName) {
writeRows(rows)
}
val actual = readTestFile()
actual shouldBe expected
}
"write no line terminator when row is empty for rows from sequence" {
val rows = listOf(listOf("a", "b", "c"), listOf(), listOf("d", "e", "f")).asSequence()
val expected = "a,b,c\r\nd,e,f\r\n"
csvWriter().open(testFileName) {
writeRows(rows)
}
val actual = readTestFile()
actual shouldBe expected
}
}
"close method" should {
"throw Exception when stream is already closed" {
Expand Down Expand Up @@ -197,4 +215,4 @@ class CsvFileWriterTest : WordSpec({
}
}

})
})