Skip to content

Commit

Permalink
change to use assertSoftly in all tests (#116)
Browse files Browse the repository at this point in the history
* refactor(CsvReaderTest): change to use assertSoftly

* fix(CsvReaderTest): style

* refactor(BufferedLineReaderTest): change to use assertSoftly

* refactor(CsvWriterTest): change to use assertSoftly

* refactor(StringReaderTest): change to use assertSoftly

* refactor(CsvReaderDslTest): change to use assertSoftly

* refactor(CsvWriterDslTest): change to use assertSoftly

* refactor(CsvParserTest): change to use assertSoftly

Co-authored-by: satokuuuuuun <[email protected]>
  • Loading branch information
satou-aaaaa and satokuuuuuun authored Jan 10, 2023
1 parent f7fa08f commit 8281219
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.doyaaaaaken.kotlincsv.client

import io.kotest.assertions.assertSoftly
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe

Expand All @@ -8,61 +9,75 @@ class BufferedLineReaderTest : StringSpec({
val str = "a,b,c\nd,e,f"
val br = str.byteInputStream().bufferedReader()
val blr = BufferedLineReader(br)
blr.readLineWithTerminator() shouldBe "a,b,c\n"
blr.readLineWithTerminator() shouldBe "d,e,f"
blr.readLineWithTerminator() shouldBe null
assertSoftly {
blr.readLineWithTerminator() shouldBe "a,b,c\n"
blr.readLineWithTerminator() shouldBe "d,e,f"
blr.readLineWithTerminator() shouldBe null
}
}

"regard \\r\\n as line terminator" {
val str = "a,b,c\r\nd,e,f"
val br = str.byteInputStream().bufferedReader()
val blr = BufferedLineReader(br)
blr.readLineWithTerminator() shouldBe "a,b,c\r\n"
blr.readLineWithTerminator() shouldBe "d,e,f"
blr.readLineWithTerminator() shouldBe null
assertSoftly {
blr.readLineWithTerminator() shouldBe "a,b,c\r\n"
blr.readLineWithTerminator() shouldBe "d,e,f"
blr.readLineWithTerminator() shouldBe null
}
}

"regard \\r as line terminator" {
val str = "a,b,c\rd,e,f"
val br = str.byteInputStream().bufferedReader()
val blr = BufferedLineReader(br)
blr.readLineWithTerminator() shouldBe "a,b,c\r"
blr.readLineWithTerminator() shouldBe "d,e,f"
blr.readLineWithTerminator() shouldBe null
assertSoftly {
blr.readLineWithTerminator() shouldBe "a,b,c\r"
blr.readLineWithTerminator() shouldBe "d,e,f"
blr.readLineWithTerminator() shouldBe null
}
}

"regard \\u2028 as line terminator" {
val str = "a,b,c\u2028d,e,f"
val br = str.byteInputStream().bufferedReader()
val blr = BufferedLineReader(br)
blr.readLineWithTerminator() shouldBe "a,b,c\u2028"
blr.readLineWithTerminator() shouldBe "d,e,f"
blr.readLineWithTerminator() shouldBe null
assertSoftly {
blr.readLineWithTerminator() shouldBe "a,b,c\u2028"
blr.readLineWithTerminator() shouldBe "d,e,f"
blr.readLineWithTerminator() shouldBe null
}
}

"regard \\u2029 as line terminator" {
val str = "a,b,c\u2029d,e,f"
val br = str.byteInputStream().bufferedReader()
val blr = BufferedLineReader(br)
blr.readLineWithTerminator() shouldBe "a,b,c\u2029"
blr.readLineWithTerminator() shouldBe "d,e,f"
blr.readLineWithTerminator() shouldBe null
assertSoftly {
blr.readLineWithTerminator() shouldBe "a,b,c\u2029"
blr.readLineWithTerminator() shouldBe "d,e,f"
blr.readLineWithTerminator() shouldBe null
}
}

"regard \\u0085 as line terminator" {
val str = "a,b,c\u0085d,e,f"
val br = str.byteInputStream().bufferedReader()
val blr = BufferedLineReader(br)
blr.readLineWithTerminator() shouldBe "a,b,c\u0085"
blr.readLineWithTerminator() shouldBe "d,e,f"
blr.readLineWithTerminator() shouldBe null
assertSoftly {
blr.readLineWithTerminator() shouldBe "a,b,c\u0085"
blr.readLineWithTerminator() shouldBe "d,e,f"
blr.readLineWithTerminator() shouldBe null
}
}

"deal with \\r at the end of file" {
val str = "a,b,c\r"
val br = str.byteInputStream().bufferedReader()
val blr = BufferedLineReader(br)
blr.readLineWithTerminator() shouldBe "a,b,c\r"
blr.readLineWithTerminator() shouldBe null
assertSoftly {
blr.readLineWithTerminator() shouldBe "a,b,c\r"
blr.readLineWithTerminator() shouldBe null
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.github.doyaaaaaken.kotlincsv.util.CSVFieldNumDifferentException
import com.github.doyaaaaaken.kotlincsv.util.CSVParseFormatException
import com.github.doyaaaaaken.kotlincsv.util.Const
import com.github.doyaaaaaken.kotlincsv.util.MalformedCSVException
import io.kotest.assertions.assertSoftly
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.shouldBe
Expand All @@ -30,11 +31,13 @@ class CsvReaderTest : WordSpec({
skipEmptyLine = true
}
val reader = CsvReader(context)
reader.charset shouldBe Charsets.ISO_8859_1.name()
reader.quoteChar shouldBe '\''
reader.delimiter shouldBe '\t'
reader.escapeChar shouldBe '"'
reader.skipEmptyLine shouldBe true
assertSoftly {
reader.charset shouldBe Charsets.ISO_8859_1.name()
reader.quoteChar shouldBe '\''
reader.delimiter shouldBe '\t'
reader.escapeChar shouldBe '"'
reader.skipEmptyLine shouldBe true
}
}
}

Expand Down Expand Up @@ -66,25 +69,26 @@ class CsvReaderTest : WordSpec({
val ex1 = shouldThrow<CSVParseFormatException> {
reader.readAll("a,\"\"failed")
}
ex1.rowNum shouldBe 1
ex1.colIndex shouldBe 4
ex1.char shouldBe 'f'

val ex2 = shouldThrow<CSVParseFormatException> {
reader.readAll("a,b\nc,\"\"failed")
}

ex2.rowNum shouldBe 2
ex2.colIndex shouldBe 4
ex2.char shouldBe 'f'

val ex3 = shouldThrow<CSVParseFormatException> {
reader.readAll("a,\"b\nb\"\nc,\"\"failed")
}

ex3.rowNum shouldBe 3
ex3.colIndex shouldBe 4
ex3.char shouldBe 'f'
assertSoftly {
ex1.rowNum shouldBe 1
ex1.colIndex shouldBe 4
ex1.char shouldBe 'f'

ex2.rowNum shouldBe 2
ex2.colIndex shouldBe 4
ex2.char shouldBe 'f'

ex3.rowNum shouldBe 3
ex3.colIndex shouldBe 4
ex3.char shouldBe 'f'
}
}
}

Expand Down Expand Up @@ -170,9 +174,12 @@ class CsvReaderTest : WordSpec({
val ex = shouldThrow<CSVFieldNumDifferentException> {
csvReader().readAll(readTestDataFile("different-fields-num.csv"))
}
ex.fieldNum shouldBe 3
ex.fieldNumOnFailedRow shouldBe 2
ex.csvRowNum shouldBe 2

assertSoftly {
ex.fieldNum shouldBe 3
ex.fieldNumOnFailedRow shouldBe 2
ex.csvRowNum shouldBe 2
}
}
"Trim row when reading csv with greater num of fields on a subsequent row" {
val expected = listOf(listOf("a", "b"), listOf("c", "d"))
Expand All @@ -181,8 +188,10 @@ class CsvReaderTest : WordSpec({
excessFieldsRowBehaviour = ExcessFieldsRowBehaviour.TRIM
}.readAll(readTestDataFile("different-fields-num2.csv"))

actual shouldBe expected
actual.size shouldBe 2
assertSoftly {
actual shouldBe expected
actual.size shouldBe 2
}
}
"it should be be possible to skip rows with both excess and insufficient fields" {
val expected = listOf(listOf("a", "b"))
Expand All @@ -192,8 +201,10 @@ class CsvReaderTest : WordSpec({
insufficientFieldsRowBehaviour = InsufficientFieldsRowBehaviour.IGNORE
}.readAll(readTestDataFile("varying-column-lengths.csv"))

actual shouldBe expected
actual.size shouldBe 1
assertSoftly {
actual shouldBe expected
actual.size shouldBe 1
}
}
"it should be be possible to trim excess columns and skip insufficient row columns" {
val expected = listOf(listOf("a", "b"), listOf("d","e"))
Expand All @@ -203,55 +214,63 @@ class CsvReaderTest : WordSpec({
insufficientFieldsRowBehaviour = InsufficientFieldsRowBehaviour.IGNORE
}.readAll(readTestDataFile("varying-column-lengths.csv"))

actual shouldBe expected
actual.size shouldBe 2
assertSoftly {
actual shouldBe expected
actual.size shouldBe 2
}
}
"If the excess fields behaviour is ERROR and the insufficient behaviour is IGNORE then an error should be thrown if there are excess columns" {
val ex = shouldThrow<CSVFieldNumDifferentException> {
csvReader {
insufficientFieldsRowBehaviour = InsufficientFieldsRowBehaviour.IGNORE
}.readAll(readTestDataFile("varying-column-lengths.csv"))
}
ex.fieldNum shouldBe 2
ex.fieldNumOnFailedRow shouldBe 3
ex.csvRowNum shouldBe 3

assertSoftly {
ex.fieldNum shouldBe 2
ex.fieldNumOnFailedRow shouldBe 3
ex.csvRowNum shouldBe 3
}
}
"If the excess fields behaviour is IGNORE or TRIM and the insufficient behaviour is ERROR then an error should be thrown if there are columns with insufficient rows" {
val ex1 = shouldThrow<CSVFieldNumDifferentException> {
csvReader {
excessFieldsRowBehaviour = ExcessFieldsRowBehaviour.IGNORE
}.readAll(readTestDataFile("varying-column-lengths.csv"))
}
ex1.fieldNum shouldBe 2
ex1.fieldNumOnFailedRow shouldBe 1
ex1.csvRowNum shouldBe 2

val ex2 = shouldThrow<CSVFieldNumDifferentException> {
csvReader {
excessFieldsRowBehaviour = ExcessFieldsRowBehaviour.TRIM
}.readAll(readTestDataFile("varying-column-lengths.csv"))
}
ex2.fieldNum shouldBe 2
ex2.fieldNumOnFailedRow shouldBe 1
ex2.csvRowNum shouldBe 2
assertSoftly {
ex1.fieldNum shouldBe 2
ex1.fieldNumOnFailedRow shouldBe 1
ex1.csvRowNum shouldBe 2

ex2.fieldNum shouldBe 2
ex2.fieldNumOnFailedRow shouldBe 1
ex2.csvRowNum shouldBe 2
}
}
"should not throw exception when reading csv with different fields num on each row with expected number of columns" {
val expected = listOf(listOf("a", "b", "c"))
val actual = csvReader {
skipMissMatchedRow = true
}.readAll(readTestDataFile("different-fields-num.csv"))

actual shouldBe expected
actual.size shouldBe 1

val expected2 = listOf(listOf("a", "b"))
val actual2 = csvReader {
skipMissMatchedRow = true
}.readAll(readTestDataFile("different-fields-num2.csv"))

actual2 shouldBe expected2
actual2.size shouldBe 1
assertSoftly {
actual shouldBe expected
actual.size shouldBe 1

actual2 shouldBe expected2
actual2.size shouldBe 1
}
}
"should not throw exception when reading csv with header and different fields num on each row" {
val expected = listOf(
Expand All @@ -262,8 +281,10 @@ class CsvReaderTest : WordSpec({
skipMissMatchedRow = true
}.readAllWithHeader(readTestDataFile("with-header-different-size-row.csv"))

actual.size shouldBe 2
expected shouldBe actual
assertSoftly {
actual.size shouldBe 2
expected shouldBe actual
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.doyaaaaaken.kotlincsv.dsl.context.CsvWriterContext
import com.github.doyaaaaaken.kotlincsv.dsl.context.WriteQuoteMode
import com.github.doyaaaaaken.kotlincsv.dsl.csvWriter
import com.github.doyaaaaaken.kotlincsv.util.Const
import io.kotest.assertions.assertSoftly
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.shouldBe
import java.io.File
Expand Down Expand Up @@ -37,12 +38,14 @@ class CsvWriterTest : WordSpec({
}
}
val writer = CsvWriter(context)
writer.charset shouldBe Charsets.ISO_8859_1.name()
writer.delimiter shouldBe '\t'
writer.nullCode shouldBe "NULL"
writer.lineTerminator shouldBe "\n"
writer.quote.char = '\''
writer.quote.mode = WriteQuoteMode.ALL
assertSoftly {
writer.charset shouldBe Charsets.ISO_8859_1.name()
writer.delimiter shouldBe '\t'
writer.nullCode shouldBe "NULL"
writer.lineTerminator shouldBe "\n"
writer.quote.char = '\''
writer.quote.mode = WriteQuoteMode.ALL
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.doyaaaaaken.kotlincsv.util.CSVFieldNumDifferentException
import com.github.doyaaaaaken.kotlincsv.util.CSVParseFormatException
import com.github.doyaaaaaken.kotlincsv.util.MalformedCSVException
import com.github.doyaaaaaken.kotlincsv.util.logger.LoggerNop
import io.kotest.assertions.assertSoftly
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.shouldBe
Expand Down Expand Up @@ -37,25 +38,26 @@ class StringReaderTest : WordSpec({
val ex1 = shouldThrow<CSVParseFormatException> {
readAll("a,\"\"failed")
}
ex1.rowNum shouldBe 1
ex1.colIndex shouldBe 4
ex1.char shouldBe 'f'

val ex2 = shouldThrow<CSVParseFormatException> {
readAll("a,b\nc,\"\"failed")
}

ex2.rowNum shouldBe 2
ex2.colIndex shouldBe 4
ex2.char shouldBe 'f'

val ex3 = shouldThrow<CSVParseFormatException> {
readAll("a,\"b\nb\"\nc,\"\"failed")
}

ex3.rowNum shouldBe 3
ex3.colIndex shouldBe 4
ex3.char shouldBe 'f'
assertSoftly {
ex1.rowNum shouldBe 1
ex1.colIndex shouldBe 4
ex1.char shouldBe 'f'

ex2.rowNum shouldBe 2
ex2.colIndex shouldBe 4
ex2.char shouldBe 'f'

ex3.rowNum shouldBe 3
ex3.colIndex shouldBe 4
ex3.char shouldBe 'f'
}
}
}

Expand Down Expand Up @@ -90,9 +92,11 @@ class StringReaderTest : WordSpec({
val ex = shouldThrow<CSVFieldNumDifferentException> {
readAll(readTestDataFile("different-fields-num.csv"))
}
ex.fieldNum shouldBe 3
ex.fieldNumOnFailedRow shouldBe 2
ex.csvRowNum shouldBe 2
assertSoftly {
ex.fieldNum shouldBe 3
ex.fieldNumOnFailedRow shouldBe 2
ex.csvRowNum shouldBe 2
}
}
}

Expand Down
Loading

0 comments on commit 8281219

Please sign in to comment.