From 82812190424db9cad3bf8409abdf78ac8102c8f5 Mon Sep 17 00:00:00 2001 From: satou <109228324+satou-aaaaa@users.noreply.github.com> Date: Tue, 10 Jan 2023 20:37:31 +0900 Subject: [PATCH] change to use assertSoftly in all tests (#116) * 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 <51846075+satokuuuuuun@users.noreply.github.com> --- .../client/BufferedLineReaderTest.kt | 55 +++++---- .../kotlincsv/client/CsvReaderTest.kt | 107 +++++++++++------- .../kotlincsv/client/CsvWriterTest.kt | 15 ++- .../kotlincsv/client/StringReaderTest.kt | 34 +++--- .../kotlincsv/dsl/CsvReaderDslTest.kt | 17 +-- .../kotlincsv/dsl/CsvWriterDslTest.kt | 17 +-- .../kotlincsv/parser/CsvParserTest.kt | 17 +-- 7 files changed, 157 insertions(+), 105 deletions(-) diff --git a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/BufferedLineReaderTest.kt b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/BufferedLineReaderTest.kt index 08efe27..018425b 100644 --- a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/BufferedLineReaderTest.kt +++ b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/BufferedLineReaderTest.kt @@ -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 @@ -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 + } } }) diff --git a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvReaderTest.kt b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvReaderTest.kt index abb007a..d13f646 100644 --- a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvReaderTest.kt +++ b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvReaderTest.kt @@ -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 @@ -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 + } } } @@ -66,25 +69,26 @@ class CsvReaderTest : WordSpec({ val ex1 = shouldThrow { reader.readAll("a,\"\"failed") } - ex1.rowNum shouldBe 1 - ex1.colIndex shouldBe 4 - ex1.char shouldBe 'f' - val ex2 = shouldThrow { reader.readAll("a,b\nc,\"\"failed") } - - ex2.rowNum shouldBe 2 - ex2.colIndex shouldBe 4 - ex2.char shouldBe 'f' - val ex3 = shouldThrow { 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' + } } } @@ -170,9 +174,12 @@ class CsvReaderTest : WordSpec({ val ex = shouldThrow { 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")) @@ -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")) @@ -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")) @@ -203,8 +214,10 @@ 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 { @@ -212,9 +225,12 @@ class CsvReaderTest : WordSpec({ 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 { @@ -222,18 +238,20 @@ class CsvReaderTest : WordSpec({ excessFieldsRowBehaviour = ExcessFieldsRowBehaviour.IGNORE }.readAll(readTestDataFile("varying-column-lengths.csv")) } - ex1.fieldNum shouldBe 2 - ex1.fieldNumOnFailedRow shouldBe 1 - ex1.csvRowNum shouldBe 2 - val ex2 = shouldThrow { 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")) @@ -241,17 +259,18 @@ class CsvReaderTest : WordSpec({ 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( @@ -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 + } } } diff --git a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvWriterTest.kt b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvWriterTest.kt index 049e0d0..1a50b49 100644 --- a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvWriterTest.kt +++ b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvWriterTest.kt @@ -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 @@ -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 + } } } diff --git a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/StringReaderTest.kt b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/StringReaderTest.kt index 16210b6..93f55da 100644 --- a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/StringReaderTest.kt +++ b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/StringReaderTest.kt @@ -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 @@ -37,25 +38,26 @@ class StringReaderTest : WordSpec({ val ex1 = shouldThrow { readAll("a,\"\"failed") } - ex1.rowNum shouldBe 1 - ex1.colIndex shouldBe 4 - ex1.char shouldBe 'f' - val ex2 = shouldThrow { readAll("a,b\nc,\"\"failed") } - - ex2.rowNum shouldBe 2 - ex2.colIndex shouldBe 4 - ex2.char shouldBe 'f' - val ex3 = shouldThrow { 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' + } } } @@ -90,9 +92,11 @@ class StringReaderTest : WordSpec({ val ex = shouldThrow { 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 + } } } diff --git a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/dsl/CsvReaderDslTest.kt b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/dsl/CsvReaderDslTest.kt index ed684fc..c99ae7e 100644 --- a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/dsl/CsvReaderDslTest.kt +++ b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/dsl/CsvReaderDslTest.kt @@ -3,6 +3,7 @@ package com.github.doyaaaaaken.kotlincsv.dsl import com.github.doyaaaaaken.kotlincsv.client.CsvReader import com.github.doyaaaaaken.kotlincsv.dsl.context.ExcessFieldsRowBehaviour import com.github.doyaaaaaken.kotlincsv.dsl.context.InsufficientFieldsRowBehaviour +import io.kotest.assertions.assertSoftly import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe import io.kotest.matchers.types.shouldBeTypeOf @@ -26,12 +27,14 @@ class CsvReaderDslTest : StringSpec({ insufficientFieldsRowBehaviour = InsufficientFieldsRowBehaviour.IGNORE excessFieldsRowBehaviour = ExcessFieldsRowBehaviour.IGNORE } - reader.charset shouldBe Charsets.ISO_8859_1.name() - reader.quoteChar shouldBe '\'' - reader.delimiter shouldBe '\t' - reader.skipEmptyLine shouldBe true - reader.skipMissMatchedRow shouldBe true - reader.insufficientFieldsRowBehaviour shouldBe InsufficientFieldsRowBehaviour.IGNORE - reader.excessFieldsRowBehaviour shouldBe ExcessFieldsRowBehaviour.IGNORE + assertSoftly { + reader.charset shouldBe Charsets.ISO_8859_1.name() + reader.quoteChar shouldBe '\'' + reader.delimiter shouldBe '\t' + reader.skipEmptyLine shouldBe true + reader.skipMissMatchedRow shouldBe true + reader.insufficientFieldsRowBehaviour shouldBe InsufficientFieldsRowBehaviour.IGNORE + reader.excessFieldsRowBehaviour shouldBe ExcessFieldsRowBehaviour.IGNORE + } } }) diff --git a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/dsl/CsvWriterDslTest.kt b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/dsl/CsvWriterDslTest.kt index a76fe86..f998ba3 100644 --- a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/dsl/CsvWriterDslTest.kt +++ b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/dsl/CsvWriterDslTest.kt @@ -2,6 +2,7 @@ package com.github.doyaaaaaken.kotlincsv.dsl import com.github.doyaaaaaken.kotlincsv.client.CsvWriter import com.github.doyaaaaaken.kotlincsv.dsl.context.WriteQuoteMode +import io.kotest.assertions.assertSoftly import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe import io.kotest.matchers.types.shouldBeTypeOf @@ -26,12 +27,14 @@ class CsvWriterDslTest : StringSpec({ mode = WriteQuoteMode.ALL } } - writer.charset shouldBe Charsets.ISO_8859_1.name() - writer.delimiter shouldBe '\t' - writer.nullCode shouldBe "NULL" - writer.lineTerminator shouldBe "\n" - writer.outputLastLineTerminator shouldBe false - writer.quote.char shouldBe '\'' - 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.outputLastLineTerminator shouldBe false + writer.quote.char shouldBe '\'' + writer.quote.mode = WriteQuoteMode.ALL + } } }) diff --git a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/parser/CsvParserTest.kt b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/parser/CsvParserTest.kt index c23a7c6..4ea8f0e 100644 --- a/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/parser/CsvParserTest.kt +++ b/src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/parser/CsvParserTest.kt @@ -1,6 +1,7 @@ package com.github.doyaaaaaken.kotlincsv.parser import com.github.doyaaaaaken.kotlincsv.util.CSVParseFormatException +import io.kotest.assertions.assertSoftly import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.WordSpec import io.kotest.matchers.shouldBe @@ -59,17 +60,19 @@ class CsvParserTest : WordSpec({ val ex1 = shouldThrow { parser.parseRow("a,\"\"failed") } - ex1.rowNum shouldBe 1 - ex1.colIndex shouldBe 4 - ex1.char shouldBe 'f' - val ex2 = shouldThrow { parser.parseRow("a,\"\"failed", 2) } - ex2.rowNum shouldBe 2 - ex2.colIndex shouldBe 4 - ex2.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' + } } } })