Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change to use assertSoftly in all tests #116

Merged
merged 8 commits into from
Jan 10, 2023
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