diff --git a/library/src/main/kotlin/kotlinx/serialization/csv/decode/CsvDecoder.kt b/library/src/main/kotlin/kotlinx/serialization/csv/decode/CsvDecoder.kt index 7edd760..bc31af9 100644 --- a/library/src/main/kotlin/kotlinx/serialization/csv/decode/CsvDecoder.kt +++ b/library/src/main/kotlin/kotlinx/serialization/csv/decode/CsvDecoder.kt @@ -114,15 +114,15 @@ internal abstract class CsvDecoder( protected open fun decodeColumn() = reader.readColumn() - protected fun readHeaders(desc: SerialDescriptor) { + protected fun readHeaders(descriptor: SerialDescriptor) { if (configuration.hasHeaderRecord && headers == null) { - this.headers = readHeaders(desc, "") + this.headers = readHeaders(descriptor, "") readTrailingDelimiter() } } - private fun readHeaders(desc: SerialDescriptor, prefix: String): Headers { + private fun readHeaders(descriptor: SerialDescriptor, prefix: String): Headers { val headers = Headers() var position = 0 while (!reader.isDone && reader.isFirstRecord) { @@ -138,15 +138,15 @@ internal abstract class CsvDecoder( // If there is an exact name match, store the header, otherwise try reading class structure val header = value.substringAfter(prefix) - val headerIndex = desc.getElementIndex(header) + val headerIndex = descriptor.getElementIndex(header) if (headerIndex != CompositeDecoder.UNKNOWN_NAME) { headers[position] = headerIndex reader.unmark() } else { val name = header.substringBefore(configuration.headerSeparator) - val nameIndex = desc.getElementIndex(name) + val nameIndex = descriptor.getElementIndex(name) if (nameIndex != CompositeDecoder.UNKNOWN_NAME) { - val childDesc = desc.getElementDescriptor(nameIndex) + val childDesc = descriptor.getElementDescriptor(nameIndex) if (childDesc.kind is StructureKind.CLASS) { reader.reset() headers[position] = nameIndex diff --git a/library/src/main/kotlin/kotlinx/serialization/csv/decode/SealedCsvDecoder.kt b/library/src/main/kotlin/kotlinx/serialization/csv/decode/SealedCsvDecoder.kt index dfa6b2b..ab9967e 100644 --- a/library/src/main/kotlin/kotlinx/serialization/csv/decode/SealedCsvDecoder.kt +++ b/library/src/main/kotlin/kotlinx/serialization/csv/decode/SealedCsvDecoder.kt @@ -58,8 +58,8 @@ internal class SealedCsvDecoder( return value } - private fun readEmptyColumns(desc: SerialDescriptor) { - for (innerDesc in desc.elementDescriptors.toList()) { + private fun readEmptyColumns(descriptor: SerialDescriptor) { + for (innerDesc in descriptor.elementDescriptors.toList()) { decodeNull() } } diff --git a/library/src/main/kotlin/kotlinx/serialization/csv/encode/CsvEncoder.kt b/library/src/main/kotlin/kotlinx/serialization/csv/encode/CsvEncoder.kt index e11d1f4..ed2f546 100644 --- a/library/src/main/kotlin/kotlinx/serialization/csv/encode/CsvEncoder.kt +++ b/library/src/main/kotlin/kotlinx/serialization/csv/encode/CsvEncoder.kt @@ -62,7 +62,7 @@ internal abstract class CsvEncoder( parent?.endChildStructure(descriptor) } - protected open fun endChildStructure(desc: SerialDescriptor) { + protected open fun endChildStructure(descriptor: SerialDescriptor) { } override fun encodeByte(value: Byte) { @@ -109,24 +109,24 @@ internal abstract class CsvEncoder( encodeColumn(enumDescriptor.getElementName(index)) } - protected fun printHeaderRecord(desc: SerialDescriptor) { + protected fun printHeaderRecord(descriptor: SerialDescriptor) { writer.beginRecord() - printHeader("", desc) + printHeader("", descriptor) writer.endRecord() } - private fun printHeader(prefix: String, desc: SerialDescriptor) { - when (desc.kind) { + private fun printHeader(prefix: String, descriptor: SerialDescriptor) { + when (descriptor.kind) { is StructureKind.LIST, is StructureKind.MAP, is PolymorphicKind.OPEN -> { - error("CSV headers are not supported for variable sized type '${desc.kind}'.") + error("CSV headers are not supported for variable sized type '${descriptor.kind}'.") } } - for (i in 0 until desc.elementsCount) { - val name = prefix + desc.getElementName(i) - val childDesc = desc.getElementDescriptor(i) + for (i in 0 until descriptor.elementsCount) { + val name = prefix + descriptor.getElementName(i) + val childDesc = descriptor.getElementDescriptor(i) when { // TODO Check diff --git a/library/src/main/kotlin/kotlinx/serialization/csv/encode/RecordListCsvEncoder.kt b/library/src/main/kotlin/kotlinx/serialization/csv/encode/RecordListCsvEncoder.kt index d685fbb..52b9f71 100644 --- a/library/src/main/kotlin/kotlinx/serialization/csv/encode/RecordListCsvEncoder.kt +++ b/library/src/main/kotlin/kotlinx/serialization/csv/encode/RecordListCsvEncoder.kt @@ -23,7 +23,7 @@ internal class RecordListCsvEncoder( return super.beginStructure(descriptor) } - override fun endChildStructure(desc: SerialDescriptor) { + override fun endChildStructure(descriptor: SerialDescriptor) { // For complex records: End the record here writer.endRecord() } diff --git a/library/src/main/kotlin/kotlinx/serialization/csv/encode/RootCsvEncoder.kt b/library/src/main/kotlin/kotlinx/serialization/csv/encode/RootCsvEncoder.kt index ee46873..733979c 100644 --- a/library/src/main/kotlin/kotlinx/serialization/csv/encode/RootCsvEncoder.kt +++ b/library/src/main/kotlin/kotlinx/serialization/csv/encode/RootCsvEncoder.kt @@ -43,7 +43,7 @@ internal class RootCsvEncoder( return super.beginStructure(descriptor) } - override fun endChildStructure(desc: SerialDescriptor) { + override fun endChildStructure(descriptor: SerialDescriptor) { writer.endRecord() } diff --git a/library/src/main/kotlin/kotlinx/serialization/csv/encode/SealedCsvEncoder.kt b/library/src/main/kotlin/kotlinx/serialization/csv/encode/SealedCsvEncoder.kt index 2806f59..1341e85 100644 --- a/library/src/main/kotlin/kotlinx/serialization/csv/encode/SealedCsvEncoder.kt +++ b/library/src/main/kotlin/kotlinx/serialization/csv/encode/SealedCsvEncoder.kt @@ -38,16 +38,16 @@ internal class SealedCsvEncoder( } } - override fun endChildStructure(desc: SerialDescriptor) { + override fun endChildStructure(descriptor: SerialDescriptor) { val sealedChildren = sealedDesc.getElementDescriptor(1).elementDescriptors.toList() - val index = sealedChildren.indexOf(desc) + val index = sealedChildren.indexOf(descriptor) for (innerDesc in sealedChildren.subList(index + 1, sealedChildren.size)) { printEmptyColumns(innerDesc) } } - private fun printEmptyColumns(desc: SerialDescriptor) { - for (innerDesc in desc.elementDescriptors.toList()) { + private fun printEmptyColumns(descriptor: SerialDescriptor) { + for (innerDesc in descriptor.elementDescriptors.toList()) { encodeNull() } } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvDelimiterTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvDelimiterTest.kt index b52c39d..6547506 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvDelimiterTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvDelimiterTest.kt @@ -5,7 +5,7 @@ import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.CsvConfiguration import kotlinx.serialization.csv.records.ComplexRecord import kotlinx.serialization.csv.records.Enum -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test /** @@ -15,7 +15,7 @@ import kotlin.test.Test class CsvDelimiterTest { @Test - fun testDefault() = assertStringFormAndRestored( + fun testDefault() = Csv.assertEncodeAndDecode( "-150,-1,42,9223372036854775807,-2.0,24.24,true,testing,,kotlin.Unit,FIRST", ComplexRecord( -150, @@ -30,12 +30,15 @@ class CsvDelimiterTest { Unit, Enum.FIRST ), - ComplexRecord.serializer(), - Csv + ComplexRecord.serializer() ) @Test - fun testComma() = assertStringFormAndRestored( + fun testComma() = Csv( + CsvConfiguration( + delimiter = ',' + ) + ).assertEncodeAndDecode( "-150,-1,42,9223372036854775807,-2.0,24.24,true,testing,,kotlin.Unit,FIRST", ComplexRecord( -150, @@ -50,16 +53,15 @@ class CsvDelimiterTest { Unit, Enum.FIRST ), - ComplexRecord.serializer(), - Csv( - CsvConfiguration( - delimiter = ',' - ) - ) + ComplexRecord.serializer() ) @Test - fun testColon() = assertStringFormAndRestored( + fun testColon() = Csv( + CsvConfiguration( + delimiter = ';' + ) + ).assertEncodeAndDecode( "-150;-1;42;9223372036854775807;-2.0;24.24;true;testing;;kotlin.Unit;FIRST", ComplexRecord( -150, @@ -74,16 +76,15 @@ class CsvDelimiterTest { Unit, Enum.FIRST ), - ComplexRecord.serializer(), - Csv( - CsvConfiguration( - delimiter = ';' - ) - ) + ComplexRecord.serializer() ) @Test - fun testDot() = assertStringFormAndRestored( + fun testDot() = Csv( + CsvConfiguration( + delimiter = '.' + ) + ).assertEncodeAndDecode( "-150.-1.42.9223372036854775807.\"-2.0\".\"24.24\".true.testing..\"kotlin.Unit\".FIRST", ComplexRecord( -150, @@ -98,16 +99,15 @@ class CsvDelimiterTest { Unit, Enum.FIRST ), - ComplexRecord.serializer(), - Csv( - CsvConfiguration( - delimiter = '.' - ) - ) + ComplexRecord.serializer() ) @Test - fun testTab() = assertStringFormAndRestored( + fun testTab() = Csv( + CsvConfiguration( + delimiter = '\t' + ) + ).assertEncodeAndDecode( "-150\t-1\t42\t9223372036854775807\t-2.0\t24.24\ttrue\ttesting\t\tkotlin.Unit\tFIRST", ComplexRecord( -150, @@ -122,11 +122,6 @@ class CsvDelimiterTest { Unit, Enum.FIRST ), - ComplexRecord.serializer(), - Csv( - CsvConfiguration( - delimiter = '\t' - ) - ) + ComplexRecord.serializer() ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvEmptyLinesTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvEmptyLinesTest.kt index a931639..40c3f7d 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvEmptyLinesTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvEmptyLinesTest.kt @@ -7,7 +7,7 @@ import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.CsvConfiguration import kotlinx.serialization.csv.records.IntStringRecord import kotlinx.serialization.csv.records.NullRecord -import kotlinx.serialization.test.assertParse +import kotlinx.serialization.test.assertDecode import kotlin.test.Test /** @@ -17,139 +17,129 @@ import kotlin.test.Test class CsvEmptyLinesTest { @Test - fun testIntList() = assertParse( + fun testIntList() = Csv( + CsvConfiguration( + ignoreEmptyLines = true + ) + ).assertDecode( "\r\n-150\r\n150\r\n\r\n42\r\n", listOf(-150, 150, 42), - ListSerializer(Int.serializer()), - Csv( - CsvConfiguration( - ignoreEmptyLines = true - ) - ) + ListSerializer(Int.serializer()) ) @Test - fun testEmptyIntList() = assertParse( + fun testEmptyIntList() = Csv( + CsvConfiguration( + ignoreEmptyLines = true + ) + ).assertDecode( "\r\n\r\n\r\n\r\n", listOf(), - ListSerializer(Int.serializer()), - Csv( - CsvConfiguration( - ignoreEmptyLines = true - ) - ) + ListSerializer(Int.serializer()) ) @Test - fun testRecordList() = assertParse( + fun testRecordList() = Csv( + CsvConfiguration( + ignoreEmptyLines = true + ) + ).assertDecode( "\r\n\r\n1,testing\r\n\r\n2,bar\r\n\r\n", listOf( IntStringRecord(1, "testing"), IntStringRecord(2, "bar") ), - ListSerializer(IntStringRecord.serializer()), - Csv( - CsvConfiguration( - ignoreEmptyLines = true - ) - ) + ListSerializer(IntStringRecord.serializer()) ) @Test - fun testEmptyRecordList() = assertParse( + fun testEmptyRecordList() = Csv( + CsvConfiguration( + ignoreEmptyLines = true + ) + ).assertDecode( "\r\n\r\n\r\n\r\n", listOf(), ListSerializer(IntStringRecord.serializer()), - Csv( - CsvConfiguration( - ignoreEmptyLines = true - ) - ) ) @Test - fun testRecordListWithHeaders() = assertParse( + fun testRecordListWithHeaders() = Csv( + CsvConfiguration( + ignoreEmptyLines = true, + hasHeaderRecord = true + ) + ).assertDecode( "\r\na,b\r\n\r\n1,testing\r\n\r\n2,bar\r\n\r\n", listOf( IntStringRecord(1, "testing"), IntStringRecord(2, "bar") ), - ListSerializer(IntStringRecord.serializer()), - Csv( - CsvConfiguration( - ignoreEmptyLines = true, - hasHeaderRecord = true - ) - ) + ListSerializer(IntStringRecord.serializer()) ) @Test - fun testEmptyRecordListWithHeaders() = assertParse( + fun testEmptyRecordListWithHeaders() = Csv( + CsvConfiguration( + ignoreEmptyLines = true, + hasHeaderRecord = true + ) + ).assertDecode( "\r\na,b\r\n\r\n\r\n\r\n", listOf(), - ListSerializer(IntStringRecord.serializer()), - Csv( - CsvConfiguration( - ignoreEmptyLines = true, - hasHeaderRecord = true - ) - ) + ListSerializer(IntStringRecord.serializer()) ) @Test - fun testListOfIntList() = assertParse( + fun testListOfIntList() = Csv( + CsvConfiguration( + ignoreEmptyLines = true + ) + ).assertDecode( "\r\n\r\n1,2\r\n\r\n3,4\r\n\r\n5,6,7\r\n", listOf( listOf(1, 2), listOf(3, 4), listOf(5, 6, 7) ), - ListSerializer(ListSerializer(Int.serializer())), - Csv( - CsvConfiguration( - ignoreEmptyLines = true - ) - ) + ListSerializer(ListSerializer(Int.serializer())) ) @Test - fun testEmptyListOfIntList() = assertParse( + fun testEmptyListOfIntList() = Csv( + CsvConfiguration( + ignoreEmptyLines = true + ) + ).assertDecode( "\r\n\r\n\r\n\r\n", listOf(), - ListSerializer(ListSerializer(Int.serializer())), - Csv( - CsvConfiguration( - ignoreEmptyLines = true - ) - ) + ListSerializer(ListSerializer(Int.serializer())) ) @Test - fun testNullRecordList() = assertParse( + fun testNullRecordList() = Csv( + CsvConfiguration( + ignoreEmptyLines = true, + nullString = "null" + ) + ).assertDecode( "\r\n\r\nnull\r\n\r\nnull\r\n\r\n", listOf( NullRecord(null), NullRecord(null) ), - ListSerializer(NullRecord.serializer()), - Csv( - CsvConfiguration( - ignoreEmptyLines = true, - nullString = "null" - ) - ) + ListSerializer(NullRecord.serializer()) ) @Test - fun testNullRecordListWithEmptyNullString() = assertParse( + fun testNullRecordListWithEmptyNullString() = Csv( + CsvConfiguration( + ignoreEmptyLines = true, + nullString = "" + ) + ).assertDecode( "\r\n\r\n\r\n\r\n\r\n\r\n", listOf(), - ListSerializer(NullRecord.serializer()), - Csv( - CsvConfiguration( - ignoreEmptyLines = true, - nullString = "" - ) - ) + ListSerializer(NullRecord.serializer()) ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvEscapeCharTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvEscapeCharTest.kt index 48dff4a..de2bff0 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvEscapeCharTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvEscapeCharTest.kt @@ -5,8 +5,8 @@ import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.CsvConfiguration import kotlinx.serialization.csv.CsvConfiguration.QuoteMode.NONE import kotlinx.serialization.csv.records.StringRecord -import kotlinx.serialization.test.assertParse -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertDecode +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test /** @@ -16,129 +16,119 @@ import kotlin.test.Test class CsvEscapeCharTest { @Test - fun testDefault() = assertStringFormAndRestored( + fun testDefault() = Csv( + CsvConfiguration( + quoteMode = NONE + ) + ).assertEncodeAndDecode( "a\\\"b", StringRecord("a\"b"), - StringRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = NONE - ) - ) + StringRecord.serializer() ) @Test - fun testSlash() = assertStringFormAndRestored( + fun testSlash() = Csv( + CsvConfiguration( + quoteMode = NONE, + escapeChar = '\\' + ) + ).assertEncodeAndDecode( "a\\\"b", StringRecord("a\"b"), - StringRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = NONE, - escapeChar = '\\' - ) - ) + StringRecord.serializer() ) @Test - fun testBang() = assertStringFormAndRestored( + fun testBang() = Csv( + CsvConfiguration( + quoteMode = NONE, + escapeChar = '!' + ) + ).assertEncodeAndDecode( "a!\"b", StringRecord("a\"b"), - StringRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = NONE, - escapeChar = '!' - ) - ) + StringRecord.serializer() ) @Test - fun testEscapedDelimiter() = assertStringFormAndRestored( + fun testEscapedDelimiter() = Csv( + CsvConfiguration( + quoteMode = NONE, + escapeChar = '\\' + ) + ).assertEncodeAndDecode( """test\,ing""", StringRecord("test,ing"), - StringRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = NONE, - escapeChar = '\\' - ) - ) + StringRecord.serializer() ) @Test - fun testEscapedEscapeChar() = assertStringFormAndRestored( + fun testEscapedEscapeChar() = Csv( + CsvConfiguration( + quoteMode = NONE, + escapeChar = '\\' + ) + ).assertEncodeAndDecode( """test\\ing""", StringRecord("""test\ing"""), - StringRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = NONE, - escapeChar = '\\' - ) - ) + StringRecord.serializer() ) @Test - fun testParseEscapedEscapeChar() = assertParse( + fun testParseEscapedEscapeChar() = Csv( + CsvConfiguration( + escapeChar = '\\' + ) + ).assertDecode( """test\\ing""", StringRecord("""test\ing"""), - StringRecord.serializer(), - Csv( - CsvConfiguration( - escapeChar = '\\' - ) - ) + StringRecord.serializer() ) @Test - fun testEscapedNewLine() = assertStringFormAndRestored( + fun testEscapedNewLine() = Csv( + CsvConfiguration( + quoteMode = NONE, + escapeChar = '\\' + ) + ).assertEncodeAndDecode( """test\ning""", StringRecord("test\ning"), - StringRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = NONE, - escapeChar = '\\' - ) - ) + StringRecord.serializer() ) @Test - fun testParseEscapedNewLine() = assertParse( + fun testParseEscapedNewLine() = Csv( + CsvConfiguration( + escapeChar = '\\' + ) + ).assertDecode( """test\ning""", StringRecord("test\ning"), - StringRecord.serializer(), - Csv( - CsvConfiguration( - escapeChar = '\\' - ) - ) + StringRecord.serializer() ) @Test - fun testTab() = assertStringFormAndRestored( + fun testTab() = Csv( + CsvConfiguration( + delimiter = '\t', + quoteMode = NONE, + escapeChar = '\\' + ) + ).assertEncodeAndDecode( """test\ting""", StringRecord("test\ting"), - StringRecord.serializer(), - Csv( - CsvConfiguration( - delimiter = '\t', - quoteMode = NONE, - escapeChar = '\\' - ) - ) + StringRecord.serializer() ) @Test - fun testParseEscapedTab() = assertParse( + fun testParseEscapedTab() = Csv( + CsvConfiguration( + escapeChar = '\\' + ) + ).assertDecode( """test\ting""", StringRecord("test\ting"), - StringRecord.serializer(), - Csv( - CsvConfiguration( - escapeChar = '\\' - ) - ) + StringRecord.serializer() ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvHasHeaderRecordTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvHasHeaderRecordTest.kt index 5d102ef..aa76a68 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvHasHeaderRecordTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvHasHeaderRecordTest.kt @@ -5,130 +5,125 @@ import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.CsvConfiguration import kotlinx.serialization.csv.records.* -import kotlinx.serialization.test.assertParse -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertDecode +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test @OptIn(ExperimentalSerializationApi::class) class CsvHasHeaderRecordTest { @Test - fun testDefault() = assertStringFormAndRestored( + fun testDefault() = Csv.assertEncodeAndDecode( "1", IntRecord(1), - IntRecord.serializer(), - Csv + IntRecord.serializer() ) @Test - fun testWithoutHeaderRecords() = assertStringFormAndRestored( + fun testWithoutHeaderRecords() = Csv( + CsvConfiguration( + hasHeaderRecord = false + ) + ).assertEncodeAndDecode( "1", IntRecord(1), - IntRecord.serializer(), - Csv( - CsvConfiguration( - hasHeaderRecord = false - ) - ) + IntRecord.serializer() ) @Test - fun testWithHeaderRecords() = assertStringFormAndRestored( + fun testWithHeaderRecords() = Csv( + CsvConfiguration( + hasHeaderRecord = true + ) + ).assertEncodeAndDecode( "a\r\n1", IntRecord(1), - IntRecord.serializer(), - Csv( - CsvConfiguration( - hasHeaderRecord = true - ) - ) + IntRecord.serializer() ) @Test - fun testListWithoutHeaderRecords() = assertStringFormAndRestored( + fun testListWithoutHeaderRecords() = Csv( + CsvConfiguration( + hasHeaderRecord = false + ) + ).assertEncodeAndDecode( "1\r\n2\r\n3", listOf( IntRecord(1), IntRecord(2), IntRecord(3) ), - ListSerializer(IntRecord.serializer()), - Csv( - CsvConfiguration( - hasHeaderRecord = false - ) - ) + ListSerializer(IntRecord.serializer()) ) @Test - fun testListWithHeaderRecords() = assertStringFormAndRestored( + fun testListWithHeaderRecords() = Csv( + CsvConfiguration( + hasHeaderRecord = true + ) + ).assertEncodeAndDecode( "a\r\n1\r\n2\r\n3", listOf( IntRecord(1), IntRecord(2), IntRecord(3) ), - ListSerializer(IntRecord.serializer()), - Csv( - CsvConfiguration( - hasHeaderRecord = true - ) - ) + ListSerializer(IntRecord.serializer()) ) @Test - fun testMultipleColumns() = assertStringFormAndRestored( + fun testMultipleColumns() = Csv( + CsvConfiguration( + hasHeaderRecord = true + ) + ).assertEncodeAndDecode( "a,b\r\n1,testing", IntStringRecord(1, "testing"), - IntStringRecord.serializer(), - Csv( - CsvConfiguration( - hasHeaderRecord = true - ) - ) + IntStringRecord.serializer() ) @Test - fun testSerialName() = assertStringFormAndRestored( + fun testSerialName() = Csv( + CsvConfiguration( + hasHeaderRecord = true + ) + ).assertEncodeAndDecode( "first,second\r\n1,2", SerialNameRecord(1, 2), - SerialNameRecord.serializer(), - Csv( - CsvConfiguration( - hasHeaderRecord = true - ) - ) + SerialNameRecord.serializer() ) @Test - fun testMultipleColumnsReordered() = assertParse( + fun testMultipleColumnsReordered() = Csv( + CsvConfiguration( + hasHeaderRecord = true + ) + ).assertDecode( "b,a\r\ntesting,1", IntStringRecord(1, "testing"), - IntStringRecord.serializer(), - Csv( - CsvConfiguration( - hasHeaderRecord = true - ) - ) + IntStringRecord.serializer() ) @Test - fun testListMultipleColumnsReordered() = assertParse( + fun testListMultipleColumnsReordered() = Csv( + CsvConfiguration( + hasHeaderRecord = true + ) + ).assertDecode( "b,a\r\ntesting,1\r\nbar,2", listOf( IntStringRecord(1, "testing"), IntStringRecord(2, "bar") ), - ListSerializer(IntStringRecord.serializer()), - Csv( - CsvConfiguration( - hasHeaderRecord = true - ) - ) + ListSerializer(IntStringRecord.serializer()) ) @Test - fun testNestedRecordWithHeaderReordered() = assertParse( + fun testNestedRecordWithHeaderReordered() = Csv( + CsvConfiguration( + hasHeaderRecord = true + ) + ).assertDecode( "time,data.location.lon,data.location.lat,data.info,data.speed,name\r\n0,1.0,0.0,info,100,Alice", NestedRecord( 0, @@ -140,16 +135,15 @@ class CsvHasHeaderRecordTest { ), 100, "info" ) ), - NestedRecord.serializer(), - Csv( - CsvConfiguration( - hasHeaderRecord = true - ) - ) + NestedRecord.serializer() ) @Test - fun testNestedRecordListWithHeaderReordered() = assertParse( + fun testNestedRecordListWithHeaderReordered() = Csv( + CsvConfiguration( + hasHeaderRecord = true + ) + ).assertDecode( "time,name,data.location.lon,data.location.lat,data.speed,data.info\r\n0,Alice,1.0,0.0,100,info\r\n1,Bob,20.0,10.0,50,info2", listOf( NestedRecord( @@ -173,11 +167,6 @@ class CsvHasHeaderRecordTest { ) ) ), - ListSerializer(NestedRecord.serializer()), - Csv( - CsvConfiguration( - hasHeaderRecord = true - ) - ) + ListSerializer(NestedRecord.serializer()) ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvHasTrailingDelimiterTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvHasTrailingDelimiterTest.kt index 24d613f..ad97fe2 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvHasTrailingDelimiterTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvHasTrailingDelimiterTest.kt @@ -7,154 +7,143 @@ import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.CsvConfiguration import kotlinx.serialization.csv.records.Enum import kotlinx.serialization.csv.records.IntRecord -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test @OptIn(ExperimentalSerializationApi::class) class CsvHasTrailingDelimiterTest { @Test - fun testDefault() = assertStringFormAndRestored( + fun testDefault() = Csv.assertEncodeAndDecode( "1", IntRecord(1), - IntRecord.serializer(), - Csv + IntRecord.serializer() ) @Test - fun testWithoutTrailingDelimiter() = assertStringFormAndRestored( + fun testWithoutTrailingDelimiter() = Csv( + CsvConfiguration( + hasTrailingDelimiter = false + ) + ).assertEncodeAndDecode( "1", IntRecord(1), - IntRecord.serializer(), - Csv( - CsvConfiguration( - hasTrailingDelimiter = false - ) - ) + IntRecord.serializer() ) @Test - fun testWithTrailingDelimiter() = assertStringFormAndRestored( + fun testWithTrailingDelimiter() = Csv( + CsvConfiguration( + hasTrailingDelimiter = true + ) + ).assertEncodeAndDecode( "1,", IntRecord(1), - IntRecord.serializer(), - Csv( - CsvConfiguration( - hasTrailingDelimiter = true - ) - ) + IntRecord.serializer() ) @Test - fun testWithTrailingDelimiterAndHeaderRecord() = assertStringFormAndRestored( + fun testWithTrailingDelimiterAndHeaderRecord() = Csv( + CsvConfiguration( + hasTrailingDelimiter = true, + hasHeaderRecord = true + ) + ).assertEncodeAndDecode( "a,\r\n1,", IntRecord(1), - IntRecord.serializer(), - Csv( - CsvConfiguration( - hasTrailingDelimiter = true, - hasHeaderRecord = true - ) - ) + IntRecord.serializer() ) @Test - fun testListWithoutTrailingDelimiter() = assertStringFormAndRestored( + fun testListWithoutTrailingDelimiter() = Csv( + CsvConfiguration( + hasTrailingDelimiter = false + ) + ).assertEncodeAndDecode( "1\r\n2\r\n3", listOf( IntRecord(1), IntRecord(2), IntRecord(3) ), - ListSerializer(IntRecord.serializer()), - Csv( - CsvConfiguration( - hasTrailingDelimiter = false - ) - ) + ListSerializer(IntRecord.serializer()) ) @Test - fun testListWithTrailingDelimiter() = assertStringFormAndRestored( + fun testListWithTrailingDelimiter() = Csv( + CsvConfiguration( + hasTrailingDelimiter = true + ) + ).assertEncodeAndDecode( "1,\r\n2,\r\n3,", listOf( IntRecord(1), IntRecord(2), IntRecord(3) ), - ListSerializer(IntRecord.serializer()), - Csv( - CsvConfiguration( - hasTrailingDelimiter = true - ) - ) + ListSerializer(IntRecord.serializer()) ) @Test - fun testListWithTrailingDelimiterAndHeaderRecord() = assertStringFormAndRestored( + fun testListWithTrailingDelimiterAndHeaderRecord() = Csv( + CsvConfiguration( + hasTrailingDelimiter = true, + hasHeaderRecord = true + ) + ).assertEncodeAndDecode( "a,\r\n1,\r\n2,\r\n3,", listOf( IntRecord(1), IntRecord(2), IntRecord(3) ), - ListSerializer(IntRecord.serializer()), - Csv( - CsvConfiguration( - hasTrailingDelimiter = true, - hasHeaderRecord = true - ) - ) + ListSerializer(IntRecord.serializer()) ) @Test - fun testPrimitiveWithTrailingDelimiter() = assertStringFormAndRestored( + fun testPrimitiveWithTrailingDelimiter() = Csv( + CsvConfiguration( + hasTrailingDelimiter = true + ) + ).assertEncodeAndDecode( "1,", 1, - Int.serializer(), - Csv( - CsvConfiguration( - hasTrailingDelimiter = true - ) - ) + Int.serializer() ) @Test - fun testPrimitiveListWithTrailingDelimiter() = assertStringFormAndRestored( + fun testPrimitiveListWithTrailingDelimiter() = Csv( + CsvConfiguration( + hasTrailingDelimiter = true + ) + ).assertEncodeAndDecode( "-150,\r\n150,\r\n42,", listOf(-150, 150, 42), - ListSerializer(Int.serializer()), - Csv( - CsvConfiguration( - hasTrailingDelimiter = true - ) - ) + ListSerializer(Int.serializer()) ) @Test - fun testEnumWithTrailingDelimiter() = assertStringFormAndRestored( + fun testEnumWithTrailingDelimiter() = Csv( + CsvConfiguration( + hasTrailingDelimiter = true + ) + ).assertEncodeAndDecode( "FIRST,", Enum.FIRST, - Enum.serializer(), - Csv( - CsvConfiguration( - hasTrailingDelimiter = true - ) - ) + Enum.serializer() ) @Test - fun testEnumListWithTrailingDelimiter() = assertStringFormAndRestored( + fun testEnumListWithTrailingDelimiter() = Csv( + CsvConfiguration( + hasTrailingDelimiter = true + ) + ).assertEncodeAndDecode( "FIRST,\r\nFIRST,", listOf( Enum.FIRST, Enum.FIRST ), - ListSerializer(Enum.serializer()), - Csv( - CsvConfiguration( - hasTrailingDelimiter = true - ) - ) + ListSerializer(Enum.serializer()) ) } \ No newline at end of file diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvIgnoreUnknownKeysTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvIgnoreUnknownKeysTest.kt index 75a92f6..55919cc 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvIgnoreUnknownKeysTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvIgnoreUnknownKeysTest.kt @@ -8,63 +8,64 @@ import kotlinx.serialization.csv.records.Data import kotlinx.serialization.csv.records.IntStringRecord import kotlinx.serialization.csv.records.Location import kotlinx.serialization.csv.records.NestedRecord -import kotlinx.serialization.test.assertParse -import kotlinx.serialization.test.assertParseFails +import kotlinx.serialization.test.assertDecode +import kotlinx.serialization.test.assertDecodeFails import kotlin.test.Test @OptIn(ExperimentalSerializationApi::class) internal class CsvIgnoreUnknownKeysTest { @Test - fun testMultipleColumns() = assertParse( + fun testMultipleColumns() = Csv( + CsvConfiguration( + hasHeaderRecord = true, + ignoreUnknownColumns = true + ) + ).assertDecode( "a,b,IGNORED\r\n1,testing,ignored", IntStringRecord(1, "testing"), - IntStringRecord.serializer(), - Csv( - CsvConfiguration( - hasHeaderRecord = true, - ignoreUnknownColumns = true - ) - ) + IntStringRecord.serializer() ) @Test - fun testMultipleColumns_failure() = assertParseFails( - "a,b,IGNORED\r\n1,testing,ignored", - IntStringRecord.serializer(), - Csv( - CsvConfiguration( - hasHeaderRecord = true - ) + fun testMultipleColumns_failure() = Csv( + CsvConfiguration( + hasHeaderRecord = true ) + ).assertDecodeFails( + "a,b,IGNORED\r\n1,testing,ignored", + IntStringRecord.serializer() ) @Test - fun testMultipleColumnsReordered() = assertParse( + fun testMultipleColumnsReordered() = Csv( + CsvConfiguration( + hasHeaderRecord = true, + ignoreUnknownColumns = true + ) + ).assertDecode( "IGNORED,b,a\r\nignored,testing,1", IntStringRecord(1, "testing"), - IntStringRecord.serializer(), - Csv( - CsvConfiguration( - hasHeaderRecord = true, - ignoreUnknownColumns = true - ) - ) + IntStringRecord.serializer() ) @Test - fun testMultipleColumnsReordered_failure() = assertParseFails( - "IGNORED,b,a\r\nignored,testing,1", - IntStringRecord.serializer(), - Csv( - CsvConfiguration( - hasHeaderRecord = true - ) + fun testMultipleColumnsReordered_failure() = Csv( + CsvConfiguration( + hasHeaderRecord = true ) + ).assertDecodeFails( + "IGNORED,b,a\r\nignored,testing,1", + IntStringRecord.serializer() ) @Test - fun testNestedRecordListWithHeaderReordered() = assertParse( + fun testNestedRecordListWithHeaderReordered() = Csv( + CsvConfiguration( + hasHeaderRecord = true, + ignoreUnknownColumns = true + ) + ).assertDecode( """IGNORED,time,name,data.location.lon,data.location.IGNORED,data.location.lat,data.speed,data.info,IGNORED |IGNORED,0,Alice,1.0,IGNORED,0.0,100,info,IGNORED |IGNORED,1,Bob,20.0,IGNORED,10.0,50,info2,IGNORED @@ -95,12 +96,6 @@ internal class CsvIgnoreUnknownKeysTest { ) ) ), - ListSerializer(NestedRecord.serializer()), - Csv( - CsvConfiguration( - hasHeaderRecord = true, - ignoreUnknownColumns = true - ) - ) + ListSerializer(NestedRecord.serializer()) ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvNullStringTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvNullStringTest.kt index 9893150..c3e6c05 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvNullStringTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvNullStringTest.kt @@ -4,7 +4,7 @@ import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.CsvConfiguration import kotlinx.serialization.csv.records.NullRecord -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test /** @@ -14,46 +14,42 @@ import kotlin.test.Test class CsvNullStringTest { @Test - fun testDefault() = assertStringFormAndRestored( + fun testDefault() = Csv.assertEncodeAndDecode( "", NullRecord(null), - NullRecord.serializer(), - Csv + NullRecord.serializer() ) @Test - fun testEmpty() = assertStringFormAndRestored( + fun testEmpty() = Csv( + CsvConfiguration( + nullString = "" + ) + ).assertEncodeAndDecode( "", NullRecord(null), - NullRecord.serializer(), - Csv( - CsvConfiguration( - nullString = "" - ) - ) + NullRecord.serializer() ) @Test - fun testNull() = assertStringFormAndRestored( + fun testNull() = Csv( + CsvConfiguration( + nullString = "null" + ) + ).assertEncodeAndDecode( "null", NullRecord(null), - NullRecord.serializer(), - Csv( - CsvConfiguration( - nullString = "null" - ) - ) + NullRecord.serializer() ) @Test - fun testNA() = assertStringFormAndRestored( + fun testNA() = Csv( + CsvConfiguration( + nullString = "N/A" + ) + ).assertEncodeAndDecode( "N/A", NullRecord(null), - NullRecord.serializer(), - Csv( - CsvConfiguration( - nullString = "N/A" - ) - ) + NullRecord.serializer() ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvQuoteCharTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvQuoteCharTest.kt index 485c776..fc765f0 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvQuoteCharTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvQuoteCharTest.kt @@ -6,7 +6,7 @@ import kotlinx.serialization.csv.CsvConfiguration import kotlinx.serialization.csv.CsvConfiguration.QuoteMode.ALL import kotlinx.serialization.csv.records.IntRecord import kotlinx.serialization.csv.records.StringRecord -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test /** @@ -16,66 +16,61 @@ import kotlin.test.Test class CsvQuoteCharTest { @Test - fun testDefault() = assertStringFormAndRestored( + fun testDefault() = Csv( + CsvConfiguration( + quoteMode = ALL + ) + ).assertEncodeAndDecode( "\"1\"", IntRecord(1), - IntRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = ALL - ) - ) + IntRecord.serializer() ) @Test - fun testDoubleQuote() = assertStringFormAndRestored( + fun testDoubleQuote() = Csv( + CsvConfiguration( + quoteMode = ALL, + quoteChar = '"' + ) + ).assertEncodeAndDecode( "\"1\"", IntRecord(1), - IntRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = ALL, - quoteChar = '"' - ) - ) + IntRecord.serializer() ) @Test - fun testSingleQuote() = assertStringFormAndRestored( + fun testSingleQuote() = Csv( + CsvConfiguration( + quoteMode = ALL, + quoteChar = '\'' + ) + ).assertEncodeAndDecode( "'1'", IntRecord(1), - IntRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = ALL, - quoteChar = '\'' - ) - ) + IntRecord.serializer() ) @Test - fun testBang() = assertStringFormAndRestored( + fun testBang() = Csv( + CsvConfiguration( + quoteMode = ALL, + quoteChar = '!' + ) + ).assertEncodeAndDecode( "!1!", IntRecord(1), - IntRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = ALL, - quoteChar = '!' - ) - ) + IntRecord.serializer() ) @Test - fun testEscapingOfQuoteChar() = assertStringFormAndRestored( + fun testEscapingOfQuoteChar() = Csv( + CsvConfiguration( + quoteMode = ALL, + quoteChar = '\'' + ) + ).assertEncodeAndDecode( "'a''b'", StringRecord("a'b"), - StringRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = ALL, - quoteChar = '\'' - ) - ) + StringRecord.serializer() ) } \ No newline at end of file diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvQuoteModeTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvQuoteModeTest.kt index 465f478..4d609e2 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvQuoteModeTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvQuoteModeTest.kt @@ -5,7 +5,7 @@ import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.CsvConfiguration import kotlinx.serialization.csv.CsvConfiguration.QuoteMode.* import kotlinx.serialization.csv.records.* -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test @OptIn(ExperimentalSerializationApi::class) @@ -453,99 +453,92 @@ class CsvQuoteModeTest { private fun testString(quoteMode: CsvConfiguration.QuoteMode, value: String, expected: String) = - assertStringFormAndRestored( + Csv( + CsvConfiguration( + quoteMode = quoteMode + ) + ).assertEncodeAndDecode( expected, StringRecord(value), - StringRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = quoteMode - ) - ) + StringRecord.serializer() ) private fun testByte(quoteMode: CsvConfiguration.QuoteMode, value: Byte, expected: String) = - assertStringFormAndRestored( + Csv( + CsvConfiguration( + quoteMode = quoteMode, + delimiter = '1', + recordSeparator = "2" + ) + ).assertEncodeAndDecode( expected, ByteRecord(value), - ByteRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = quoteMode, - delimiter = '1', - recordSeparator = "2" - ) - ) + ByteRecord.serializer() ) private fun testShort(quoteMode: CsvConfiguration.QuoteMode, value: Short, expected: String) = - assertStringFormAndRestored( + Csv( + CsvConfiguration( + quoteMode = quoteMode, + delimiter = '1', + recordSeparator = "2" + ) + ).assertEncodeAndDecode( expected, ShortRecord(value), - ShortRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = quoteMode, - delimiter = '1', - recordSeparator = "2" - ) - ) + ShortRecord.serializer() ) private fun testInt(quoteMode: CsvConfiguration.QuoteMode, value: Int, expected: String) = - assertStringFormAndRestored( + Csv( + CsvConfiguration( + quoteMode = quoteMode, + delimiter = '1', + recordSeparator = "2" + ) + ).assertEncodeAndDecode( expected, IntRecord(value), - IntRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = quoteMode, - delimiter = '1', - recordSeparator = "2" - ) - ) + IntRecord.serializer() ) private fun testLong(quoteMode: CsvConfiguration.QuoteMode, value: Long, expected: String) = - assertStringFormAndRestored( + Csv( + CsvConfiguration( + quoteMode = quoteMode, + delimiter = '1', + recordSeparator = "2" + ) + ).assertEncodeAndDecode( expected, LongRecord(value), - LongRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = quoteMode, - delimiter = '1', - recordSeparator = "2" - ) - ) + LongRecord.serializer() ) private fun testFloat(quoteMode: CsvConfiguration.QuoteMode, value: Float, expected: String) = - assertStringFormAndRestored( + Csv( + CsvConfiguration( + quoteMode = quoteMode, + delimiter = '1', + recordSeparator = "2" + ) + ).assertEncodeAndDecode( expected, FloatRecord(value), - FloatRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = quoteMode, - delimiter = '1', - recordSeparator = "2" - ) - ) + FloatRecord.serializer() ) private fun testDouble(quoteMode: CsvConfiguration.QuoteMode, value: Double, expected: String) = - assertStringFormAndRestored( + Csv( + CsvConfiguration( + quoteMode = quoteMode, + delimiter = '1', + recordSeparator = "2" + ) + ).assertEncodeAndDecode( expected, DoubleRecord(value), - DoubleRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = quoteMode, - delimiter = '1', - recordSeparator = "2" - ) - ) + DoubleRecord.serializer() ) private fun testBoolean( @@ -553,70 +546,65 @@ class CsvQuoteModeTest { value: Boolean, expected: String ) = - assertStringFormAndRestored( + Csv( + CsvConfiguration( + quoteMode = quoteMode, + delimiter = 'f', + recordSeparator = "a" + ) + ).assertEncodeAndDecode( expected, BooleanRecord(value), - BooleanRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = quoteMode, - delimiter = 'f', - recordSeparator = "a" - ) - ) + BooleanRecord.serializer() ) private fun testNull(quoteMode: CsvConfiguration.QuoteMode, expected: String) = - assertStringFormAndRestored( + Csv( + CsvConfiguration( + quoteMode = quoteMode, + nullString = "null" + ) + ).assertEncodeAndDecode( expected, NullRecord(null), - NullRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = quoteMode, - nullString = "null" - ) - ) + NullRecord.serializer() ) private fun testNullRequiringQuotes(quoteMode: CsvConfiguration.QuoteMode, expected: String) = - assertStringFormAndRestored( + Csv( + CsvConfiguration( + quoteMode = quoteMode, + nullString = "null", + delimiter = 'u', + recordSeparator = "l" + ) + ).assertEncodeAndDecode( expected, NullRecord(null), - NullRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = quoteMode, - nullString = "null", - delimiter = 'u', - recordSeparator = "l" - ) - ) + NullRecord.serializer() ) private fun testUnit(quoteMode: CsvConfiguration.QuoteMode, expected: String) = - assertStringFormAndRestored( + Csv( + CsvConfiguration( + quoteMode = quoteMode + ) + ).assertEncodeAndDecode( expected, UnitRecord(Unit), - UnitRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = quoteMode - ) - ) + UnitRecord.serializer() ) private fun testUnitRequiringQuotes(quoteMode: CsvConfiguration.QuoteMode, expected: String) = - assertStringFormAndRestored( + Csv( + CsvConfiguration( + quoteMode = quoteMode, + delimiter = 'U', + recordSeparator = "i" + ) + ).assertEncodeAndDecode( expected, UnitRecord(Unit), - UnitRecord.serializer(), - Csv( - CsvConfiguration( - quoteMode = quoteMode, - delimiter = 'U', - recordSeparator = "i" - ) - ) + UnitRecord.serializer() ) } \ No newline at end of file diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvRecordSeparatorTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvRecordSeparatorTest.kt index 7636231..635755e 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvRecordSeparatorTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvRecordSeparatorTest.kt @@ -5,7 +5,7 @@ import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.CsvConfiguration import kotlinx.serialization.csv.records.IntRecord -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test /** @@ -15,62 +15,58 @@ import kotlin.test.Test class CsvRecordSeparatorTest { @Test - fun testDefault() = assertStringFormAndRestored( + fun testDefault() = Csv.assertEncodeAndDecode( "1\r\n2\r\n3", listOf( IntRecord(1), IntRecord(2), IntRecord(3) ), - ListSerializer(IntRecord.serializer()), - Csv + ListSerializer(IntRecord.serializer()) ) @Test - fun testWindows() = assertStringFormAndRestored( + fun testWindows() = Csv( + CsvConfiguration( + recordSeparator = "\r\n" + ) + ).assertEncodeAndDecode( "1\r\n2\r\n3", listOf( IntRecord(1), IntRecord(2), IntRecord(3) ), - ListSerializer(IntRecord.serializer()), - Csv( - CsvConfiguration( - recordSeparator = "\r\n" - ) - ) + ListSerializer(IntRecord.serializer()) ) @Test - fun testUnix() = assertStringFormAndRestored( + fun testUnix() = Csv( + CsvConfiguration( + recordSeparator = "\n" + ) + ).assertEncodeAndDecode( "1\n2\n3", listOf( IntRecord(1), IntRecord(2), IntRecord(3) ), - ListSerializer(IntRecord.serializer()), - Csv( - CsvConfiguration( - recordSeparator = "\n" - ) - ) + ListSerializer(IntRecord.serializer()) ) @Test - fun testHash() = assertStringFormAndRestored( + fun testHash() = Csv( + CsvConfiguration( + recordSeparator = "#" + ) + ).assertEncodeAndDecode( "1#2#3", listOf( IntRecord(1), IntRecord(2), IntRecord(3) ), - ListSerializer(IntRecord.serializer()), - Csv( - CsvConfiguration( - recordSeparator = "#" - ) - ) + ListSerializer(IntRecord.serializer()) ) } \ No newline at end of file diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvUnitStringTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvUnitStringTest.kt index 25556ac..2ef79de 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvUnitStringTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/config/CsvUnitStringTest.kt @@ -3,7 +3,7 @@ package kotlinx.serialization.csv.config import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.records.UnitRecord -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test /** @@ -13,10 +13,9 @@ import kotlin.test.Test class CsvUnitStringTest { @Test - fun testDefault() = assertStringFormAndRestored( + fun testDefault() = Csv.assertEncodeAndDecode( "kotlin.Unit", UnitRecord(Unit), - UnitRecord.serializer(), - Csv + UnitRecord.serializer() ) } \ No newline at end of file diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/example/ExampleTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/example/ExampleTest.kt index a57f710..6d1fa01 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/example/ExampleTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/example/ExampleTest.kt @@ -11,7 +11,7 @@ import kotlinx.serialization.csv.example.Tire.Axis.REAR import kotlinx.serialization.csv.example.Tire.Side.LEFT import kotlinx.serialization.csv.example.Tire.Side.RIGHT import kotlinx.serialization.modules.SerializersModule -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import java.time.LocalDateTime import java.util.* import kotlin.test.Test @@ -46,7 +46,11 @@ class ExampleTest { ) @Test - fun testLocationRecords() = assertStringFormAndRestored( + fun testLocationRecords() = Csv( + CsvConfiguration.rfc4180.copy( + hasHeaderRecord = true + ) + ).assertEncodeAndDecode( """|id,date,position.latitude,position.longitude,driver.id,driver.foreName,driver.lastName,driver.birthday,vehicle.uuid,vehicle.type,vehicle.brand,vehicleData.speed,vehicleData.consumption,vehicleData.consumption.Combustion.consumptionLiterPer100Km,vehicleData.consumption.Electric.consumptionKWhPer100Km |0,2020-02-01T13:33:00,0.0,0.0,12,Jon,Smith,,f9682dcb-30f7-4e88-915e-60e3b2758da7,CAR,Tesla,,Unknown,, |1,2020-02-01T13:37:00,0.1,0.1,12,Jon,Smith,,f9682dcb-30f7-4e88-915e-60e3b2758da7,CAR,Tesla,27.7778,Electric,,18.1 @@ -72,16 +76,17 @@ class ExampleTest { VehicleData(20.0, Consumption.Combustion(7.9)) ) ), - ListSerializer(LocationRecord.serializer()), - Csv( - CsvConfiguration.rfc4180.copy( - hasHeaderRecord = true - ) - ) + ListSerializer(LocationRecord.serializer()) ) @Test - fun testVehiclePartRecords() = assertStringFormAndRestored( + fun testVehiclePartRecords() = Csv( + CsvConfiguration.rfc4180, + SerializersModule { + polymorphic(Part::class, Tire::class, Tire.serializer()) + polymorphic(Part::class, Oil::class, Oil.serializer()) + } + ).assertEncodeAndDecode( """|101,f9682dcb-30f7-4e88-915e-60e3b2758da7,CAR,Tesla,Tire,FRONT,LEFT,245,35,21,0.25 |102,f9682dcb-30f7-4e88-915e-60e3b2758da7,CAR,Tesla,Tire,FRONT,RIGHT,245,35,21,0.21 |103,f9682dcb-30f7-4e88-915e-60e3b2758da7,CAR,Tesla,Tire,REAR,LEFT,265,35,21,0.35 @@ -103,18 +108,11 @@ class ExampleTest { VehiclePartRecord(204, porsche, Tire(REAR, LEFT, 265, 35, 20), 0.2), VehiclePartRecord(205, porsche, Tire(REAR, RIGHT, 265, 35, 20), 0.2) ), - ListSerializer(VehiclePartRecord.serializer()), - Csv( - CsvConfiguration.rfc4180, - SerializersModule { - polymorphic(Part::class, Tire::class, Tire.serializer()) - polymorphic(Part::class, Oil::class, Oil.serializer()) - } - ) + ListSerializer(VehiclePartRecord.serializer()) ) @Test - fun testVehicleFeaturesRecords() = assertStringFormAndRestored( + fun testVehicleFeaturesRecords() = Csv(CsvConfiguration.rfc4180).assertEncodeAndDecode( """|c038c27b-a3fd-4e35-b6ac-ab06d747e16c,MOTORBIKE,Harley,, |c038c27b-a3fd-4e35-b6ac-ab06d747e16c,MOTORBIKE,Harley,0,0 |f9682dcb-30f7-4e88-915e-60e3b2758da7,CAR,Tesla,5,ELECTRIC,AUTOMATIC,HEATED_SEATS,NAVIGATION_SYSTEM,XENON,2,ELECTRIC,0,XENON,1 @@ -128,12 +126,11 @@ class ExampleTest { mapOf(ELECTRIC to 0, XENON to 1) ) ), - ListSerializer(VehicleFeaturesRecord.serializer()), - Csv(CsvConfiguration.rfc4180) + ListSerializer(VehicleFeaturesRecord.serializer()) ) @Test - fun testExcel() = assertStringFormAndRestored( + fun testExcel() = Csv(CsvConfiguration.excel).assertEncodeAndDecode( """|c038c27b-a3fd-4e35-b6ac-ab06d747e16c,MOTORBIKE,Harley,, |c038c27b-a3fd-4e35-b6ac-ab06d747e16c,MOTORBIKE,Harley,0,0 | @@ -149,7 +146,6 @@ class ExampleTest { mapOf(ELECTRIC to 0, XENON to 1) ) ), - ListSerializer(VehicleFeaturesRecord.serializer().nullable), - Csv(CsvConfiguration.excel) + ListSerializer(VehicleFeaturesRecord.serializer().nullable) ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvCollectionsTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvCollectionsTest.kt index b9b431a..672ce2d 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvCollectionsTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvCollectionsTest.kt @@ -5,7 +5,7 @@ import kotlinx.serialization.Serializable import kotlinx.serialization.builtins.* import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.CsvConfiguration -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test /** @@ -15,59 +15,55 @@ import kotlin.test.Test class CsvCollectionsTest { @Test - fun testListOfIntList() = assertStringFormAndRestored( + fun testListOfIntList() = Csv.assertEncodeAndDecode( "1,2\r\n3,4\r\n5,6,7", listOf( listOf(1, 2), listOf(3, 4), listOf(5, 6, 7) ), - ListSerializer(ListSerializer(Int.serializer())), - Csv + ListSerializer(ListSerializer(Int.serializer())) ) @Test - fun testListOfIntSet() = assertStringFormAndRestored( + fun testListOfIntSet() = Csv.assertEncodeAndDecode( "1,2\r\n3,4\r\n5,6,7", listOf( setOf(1, 2), setOf(3, 4), setOf(5, 6, 7) ), - ListSerializer(SetSerializer(Int.serializer())), - Csv + ListSerializer(SetSerializer(Int.serializer())) ) @Test - fun testNullableListOfNullableIntList() = assertStringFormAndRestored( + fun testNullableListOfNullableIntList() = Csv( + CsvConfiguration( + ignoreEmptyLines = false + ) + ).assertEncodeAndDecode( "1,2\r\n\r\n5,,7", listOf( listOf(1, 2), null, listOf(5, null, 7) ), - ListSerializer(ListSerializer(Int.serializer().nullable).nullable), - Csv( - CsvConfiguration( - ignoreEmptyLines = false - ) - ) + ListSerializer(ListSerializer(Int.serializer().nullable).nullable) ) @Test - fun testMapOfIntLists() = assertStringFormAndRestored( + fun testMapOfIntLists() = Csv.assertEncodeAndDecode( "3,2,1,2,2,3,4,2,5,6,2,7,8,1,9,4,10,11,12,13", mapOf( listOf(1, 2) to listOf(3, 4), listOf(5, 6) to listOf(7, 8), listOf(9) to listOf(10, 11, 12, 13) ), - MapSerializer(ListSerializer(Int.serializer()), ListSerializer(Int.serializer())), - Csv + MapSerializer(ListSerializer(Int.serializer()), ListSerializer(Int.serializer())) ) @Test - fun testMultipleMapOfIntLists() = assertStringFormAndRestored( + fun testMultipleMapOfIntLists() = Csv.assertEncodeAndDecode( """|2,1,2,2,3,4,2,5,6,2,7,8,1,9,4,10,11,12,13 |1,1,2,2,3,3,4,5,6,4,7,8,9,10 """.trimMargin().replace("\n", "\r\n"), @@ -87,12 +83,11 @@ class CsvCollectionsTest { ListSerializer(Int.serializer()), ListSerializer(Int.serializer()) ) - ), - Csv + ) ) @Test - fun testRecordWithMapOfIntLists() = assertStringFormAndRestored( + fun testRecordWithMapOfIntLists() = Csv.assertEncodeAndDecode( "3,2,1,2,2,3,4,2,5,6,2,7,8,1,9,4,10,11,12,13", Record( mapOf( @@ -101,12 +96,11 @@ class CsvCollectionsTest { listOf(9) to listOf(10, 11, 12, 13) ) ), - Record.serializer(), - Csv + Record.serializer() ) @Test - fun testMultipleRecordsWithMapOfIntLists() = assertStringFormAndRestored( + fun testMultipleRecordsWithMapOfIntLists() = Csv.assertEncodeAndDecode( """|3,2,1,2,2,3,4,2,5,6,2,7,8,1,9,4,10,11,12,13 |2,1,1,2,2,3,3,4,5,6,4,7,8,9,10 """.trimMargin().replace("\n", "\r\n"), @@ -125,8 +119,7 @@ class CsvCollectionsTest { ) ) ), - ListSerializer(Record.serializer()), - Csv + ListSerializer(Record.serializer()) ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvNestedRecordTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvNestedRecordTest.kt index fa5e68d..956880a 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvNestedRecordTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvNestedRecordTest.kt @@ -4,14 +4,14 @@ import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.CsvConfiguration -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test @OptIn(ExperimentalSerializationApi::class) class CsvNestedRecordTest { @Test - fun testNestedRecord() = assertStringFormAndRestored( + fun testNestedRecord() = Csv.assertEncodeAndDecode( "0,Alice,0.0,1.0,100,info", NestedRecord( 0, @@ -23,12 +23,11 @@ class CsvNestedRecordTest { ), 100, "info" ) ), - NestedRecord.serializer(), - Csv + NestedRecord.serializer() ) @Test - fun testNestedRecordList() = assertStringFormAndRestored( + fun testNestedRecordList() = Csv.assertEncodeAndDecode( "0,Alice,0.0,1.0,100,info\r\n1,Bob,10.0,20.0,50,info2", listOf( NestedRecord( @@ -52,12 +51,15 @@ class CsvNestedRecordTest { ) ) ), - ListSerializer(NestedRecord.serializer()), - Csv + ListSerializer(NestedRecord.serializer()) ) @Test - fun testNestedRecordWithHeader() = assertStringFormAndRestored( + fun testNestedRecordWithHeader() = Csv( + CsvConfiguration( + hasHeaderRecord = true + ) + ).assertEncodeAndDecode( "time,name,data.location.lat,data.location.lon,data.speed,data.info\r\n0,Alice,0.0,1.0,100,info", NestedRecord( 0, @@ -69,16 +71,15 @@ class CsvNestedRecordTest { ), 100, "info" ) ), - NestedRecord.serializer(), - Csv( - CsvConfiguration( - hasHeaderRecord = true - ) - ) + NestedRecord.serializer() ) @Test - fun testNestedRecordListWithHeader() = assertStringFormAndRestored( + fun testNestedRecordListWithHeader() = Csv( + CsvConfiguration( + hasHeaderRecord = true + ) + ).assertEncodeAndDecode( "time,name,data.location.lat,data.location.lon,data.speed,data.info\r\n0,Alice,0.0,1.0,100,info\r\n1,Bob,10.0,20.0,50,info2", listOf( NestedRecord( @@ -102,11 +103,6 @@ class CsvNestedRecordTest { ) ) ), - ListSerializer(NestedRecord.serializer()), - Csv( - CsvConfiguration( - hasHeaderRecord = true - ) - ) + ListSerializer(NestedRecord.serializer()) ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvNullablePrimitivesTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvNullablePrimitivesTest.kt index 0faefd6..49e486d 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvNullablePrimitivesTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvNullablePrimitivesTest.kt @@ -6,7 +6,7 @@ import kotlinx.serialization.builtins.nullable import kotlinx.serialization.builtins.serializer import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.CsvConfiguration -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test /** @@ -16,102 +16,90 @@ import kotlin.test.Test class CsvNullablePrimitivesTest { @Test - fun testByte() = assertStringFormAndRestored( + fun testByte() = Csv.assertEncodeAndDecode( "", null, - Byte.serializer().nullable, - Csv + Byte.serializer().nullable ) @Test - fun testShort() = assertStringFormAndRestored( + fun testShort() = Csv.assertEncodeAndDecode( "", null, - Short.serializer().nullable, - Csv + Short.serializer().nullable ) @Test - fun testInt() = assertStringFormAndRestored( + fun testInt() = Csv.assertEncodeAndDecode( "", null, - Int.serializer().nullable, - Csv + Int.serializer().nullable ) @Test - fun testLong() = assertStringFormAndRestored( + fun testLong() = Csv.assertEncodeAndDecode( "", null, - Long.serializer().nullable, - Csv + Long.serializer().nullable ) @Test - fun testFloat() = assertStringFormAndRestored( + fun testFloat() = Csv.assertEncodeAndDecode( "", null, - Float.serializer().nullable, - Csv + Float.serializer().nullable ) @Test - fun testDouble() = assertStringFormAndRestored( + fun testDouble() = Csv.assertEncodeAndDecode( "", null, - Double.serializer().nullable, - Csv + Double.serializer().nullable ) @Test - fun testBooleanTrue() = assertStringFormAndRestored( + fun testBooleanTrue() = Csv.assertEncodeAndDecode( "", null, - Boolean.serializer().nullable, - Csv + Boolean.serializer().nullable ) @Test - fun testBooleanFalse() = assertStringFormAndRestored( + fun testBooleanFalse() = Csv.assertEncodeAndDecode( "", null, - Boolean.serializer().nullable, - Csv + Boolean.serializer().nullable ) @Test - fun testChar() = assertStringFormAndRestored( + fun testChar() = Csv.assertEncodeAndDecode( "", null, - Char.serializer().nullable, - Csv + Char.serializer().nullable ) @Test - fun testString() = assertStringFormAndRestored( + fun testString() = Csv.assertEncodeAndDecode( "", null, - String.serializer().nullable, - Csv + String.serializer().nullable ) @Test - fun testUnit() = assertStringFormAndRestored( + fun testUnit() = Csv.assertEncodeAndDecode( "", null, - Unit.serializer().nullable, - Csv + Unit.serializer().nullable ) @Test - fun testIntList() = assertStringFormAndRestored( + fun testIntList() = Csv( + CsvConfiguration( + ignoreEmptyLines = false + ) + ).assertEncodeAndDecode( "-150\r\n\r\n42", listOf(-150, null, 42), - ListSerializer(Int.serializer().nullable), - Csv( - CsvConfiguration( - ignoreEmptyLines = false - ) - ) + ListSerializer(Int.serializer().nullable) ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvNullableSimpleRecordTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvNullableSimpleRecordTest.kt index 78663ca..a271e27 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvNullableSimpleRecordTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvNullableSimpleRecordTest.kt @@ -5,8 +5,8 @@ import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.builtins.nullable import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.CsvConfiguration -import kotlinx.serialization.test.assertParse -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertDecode +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test /** @@ -16,136 +16,121 @@ import kotlin.test.Test class CsvNullableSimpleRecordTest { @Test - fun testInteger() = assertStringFormAndRestored( + fun testInteger() = Csv.assertEncodeAndDecode( "", null, - IntRecord.serializer().nullable, - Csv + IntRecord.serializer().nullable ) @Test - fun testByte() = assertStringFormAndRestored( + fun testByte() = Csv.assertEncodeAndDecode( "", null, - ByteRecord.serializer().nullable, - Csv + ByteRecord.serializer().nullable ) @Test - fun testShort() = assertStringFormAndRestored( + fun testShort() = Csv.assertEncodeAndDecode( "", null, - ShortRecord.serializer().nullable, - Csv + ShortRecord.serializer().nullable ) @Test - fun testLong() = assertStringFormAndRestored( + fun testLong() = Csv.assertEncodeAndDecode( "", null, - LongRecord.serializer().nullable, - Csv + LongRecord.serializer().nullable ) @Test - fun testFloat() = assertStringFormAndRestored( + fun testFloat() = Csv.assertEncodeAndDecode( "", null, - FloatRecord.serializer().nullable, - Csv + FloatRecord.serializer().nullable ) @Test - fun testDouble() = assertStringFormAndRestored( + fun testDouble() = Csv.assertEncodeAndDecode( "", null, - DoubleRecord.serializer().nullable, - Csv + DoubleRecord.serializer().nullable ) @Test - fun testBoolean() = assertStringFormAndRestored( + fun testBoolean() = Csv.assertEncodeAndDecode( "", null, - BooleanRecord.serializer().nullable, - Csv + BooleanRecord.serializer().nullable ) @Test - fun testChar() = assertStringFormAndRestored( + fun testChar() = Csv.assertEncodeAndDecode( "", null, - CharRecord.serializer().nullable, - Csv + CharRecord.serializer().nullable ) @Test - fun testString() = assertStringFormAndRestored( + fun testString() = Csv.assertEncodeAndDecode( "", null, - StringRecord.serializer().nullable, - Csv + StringRecord.serializer().nullable ) @Test - fun testNull() = assertStringFormAndRestored( + fun testNull() = Csv.assertEncodeAndDecode( "", null, - NullRecord.serializer().nullable, - Csv + NullRecord.serializer().nullable ) @Test - fun testUnit() = assertStringFormAndRestored( + fun testUnit() = Csv.assertEncodeAndDecode( "", null, - UnitRecord.serializer().nullable, - Csv + UnitRecord.serializer().nullable ) @Test - fun testEnum() = assertStringFormAndRestored( + fun testEnum() = Csv.assertEncodeAndDecode( "", null, - EnumRecord.serializer().nullable, - Csv + EnumRecord.serializer().nullable ) @Test - fun testComplex() = assertStringFormAndRestored( + fun testComplex() = Csv.assertEncodeAndDecode( "", null, - ComplexRecord.serializer().nullable, - Csv + ComplexRecord.serializer().nullable ) @Test - fun testIntList() = assertStringFormAndRestored( + fun testIntList() = Csv( + CsvConfiguration( + ignoreEmptyLines = false + ) + ).assertEncodeAndDecode( "-150\r\n\r\n150", listOf( IntRecord(-150), null, IntRecord(150) ), - ListSerializer(IntRecord.serializer().nullable), - Csv( - CsvConfiguration( - ignoreEmptyLines = false - ) - ) + ListSerializer(IntRecord.serializer().nullable) ) @Test - fun testIntListWithLastLineEmpty() = assertParse( + fun testIntListWithLastLineEmpty() = Csv( + CsvConfiguration( + ignoreEmptyLines = false + ) + ).assertDecode( "-150\r\n\r\n150\r\n", listOf( IntRecord(-150), null, IntRecord(150), null ), - ListSerializer(IntRecord.serializer().nullable), - Csv( - CsvConfiguration( - ignoreEmptyLines = false - ) - ) + ListSerializer(IntRecord.serializer().nullable) ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvObjectTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvObjectTest.kt index c45dd0c..7a7ccfd 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvObjectTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvObjectTest.kt @@ -5,7 +5,7 @@ import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.builtins.nullable import kotlinx.serialization.csv.Csv import kotlinx.serialization.csv.CsvConfiguration -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import org.junit.jupiter.api.assertThrows import kotlin.test.Test @@ -16,35 +16,32 @@ import kotlin.test.Test class CsvObjectTest { @Test - fun testObject() = assertStringFormAndRestored( + fun testObject() = Csv.assertEncodeAndDecode( "kotlinx.serialization.csv.records.ObjectRecord", ObjectRecord, - ObjectRecord.serializer(), - Csv + ObjectRecord.serializer() ) @Test - fun testNullableObject() = assertStringFormAndRestored( + fun testNullableObject() = Csv.assertEncodeAndDecode( "", null, - ObjectRecord.serializer().nullable, - Csv + ObjectRecord.serializer().nullable ) @Test - fun testObjectList() = assertStringFormAndRestored( + fun testObjectList() = Csv( + CsvConfiguration( + ignoreEmptyLines = false + ) + ).assertEncodeAndDecode( "kotlinx.serialization.csv.records.ObjectRecord\r\n\r\nkotlinx.serialization.csv.records.ObjectRecord", listOf( ObjectRecord, null, ObjectRecord ), - ListSerializer(ObjectRecord.serializer().nullable), - Csv( - CsvConfiguration( - ignoreEmptyLines = false - ) - ) + ListSerializer(ObjectRecord.serializer().nullable) ) @Test diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvPrimitivesTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvPrimitivesTest.kt index 87d854c..5a6cb5f 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvPrimitivesTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvPrimitivesTest.kt @@ -4,7 +4,7 @@ import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.builtins.serializer import kotlinx.serialization.csv.Csv -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test /** @@ -14,98 +14,86 @@ import kotlin.test.Test class CsvPrimitivesTest { @Test - fun testByte() = assertStringFormAndRestored( + fun testByte() = Csv.assertEncodeAndDecode( "-123", -123, - Byte.serializer(), - Csv + Byte.serializer() ) @Test - fun testShort() = assertStringFormAndRestored( + fun testShort() = Csv.assertEncodeAndDecode( "-150", -150, - Short.serializer(), - Csv + Short.serializer() ) @Test - fun testInt() = assertStringFormAndRestored( + fun testInt() = Csv.assertEncodeAndDecode( "-150", -150, - Int.serializer(), - Csv + Int.serializer() ) @Test - fun testLong() = assertStringFormAndRestored( + fun testLong() = Csv.assertEncodeAndDecode( "-150", -150, - Long.serializer(), - Csv + Long.serializer() ) @Test - fun testFloat() = assertStringFormAndRestored( + fun testFloat() = Csv.assertEncodeAndDecode( "-150.0", -150f, - Float.serializer(), - Csv + Float.serializer() ) @Test - fun testDouble() = assertStringFormAndRestored( + fun testDouble() = Csv.assertEncodeAndDecode( "-150.0", -150.0, - Double.serializer(), - Csv + Double.serializer() ) @Test - fun testBooleanTrue() = assertStringFormAndRestored( + fun testBooleanTrue() = Csv.assertEncodeAndDecode( "true", true, - Boolean.serializer(), - Csv + Boolean.serializer() ) @Test - fun testBooleanFalse() = assertStringFormAndRestored( + fun testBooleanFalse() = Csv.assertEncodeAndDecode( "false", false, - Boolean.serializer(), - Csv + Boolean.serializer() ) @Test - fun testChar() = assertStringFormAndRestored( + fun testChar() = Csv.assertEncodeAndDecode( "a", 'a', - Char.serializer(), - Csv + Char.serializer() ) @Test - fun testString() = assertStringFormAndRestored( + fun testString() = Csv.assertEncodeAndDecode( "testing", "testing", - String.serializer(), - Csv + String.serializer() ) @Test - fun testUnit() = assertStringFormAndRestored( + fun testUnit() = Csv.assertEncodeAndDecode( "kotlin.Unit", Unit, - Unit.serializer(), - Csv + Unit.serializer() ) @Test - fun testIntList() = assertStringFormAndRestored( + fun testIntList() = Csv.assertEncodeAndDecode( "-150\r\n150\r\n42", listOf(-150, 150, 42), - ListSerializer(Int.serializer()), - Csv + ListSerializer(Int.serializer()) ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvSealedKindTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvSealedKindTest.kt index ab0fa5f..239bcc2 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvSealedKindTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvSealedKindTest.kt @@ -4,7 +4,7 @@ import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.builtins.nullable import kotlinx.serialization.csv.Csv -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Ignore import kotlin.test.Test @@ -15,48 +15,43 @@ import kotlin.test.Test class CsvSealedKindTest { @Test - fun testSealedObject() = assertStringFormAndRestored( + fun testSealedObject() = Csv.assertEncodeAndDecode( "kotlinx.serialization.csv.records.SealedRecord.Object,,,,", SealedRecord.Object, - SealedRecord.serializer(), - Csv + SealedRecord.serializer() ) @Ignore("For now, serialization library does not provide the required information to write multiple columns") @Test - fun testSealedNullable() = assertStringFormAndRestored( + fun testSealedNullable() = Csv.assertEncodeAndDecode( ",,", null, - SealedRecord.serializer().nullable, - Csv + SealedRecord.serializer().nullable ) @Test - fun testSealedOtherObject() = assertStringFormAndRestored( + fun testSealedOtherObject() = Csv.assertEncodeAndDecode( "kotlinx.serialization.csv.records.SealedRecord.OtherObject,,,,", SealedRecord.OtherObject, - SealedRecord.serializer(), - Csv + SealedRecord.serializer() ) @Test - fun testSealedClass() = assertStringFormAndRestored( + fun testSealedClass() = Csv.assertEncodeAndDecode( "kotlinx.serialization.csv.records.SealedRecord.Class,42,testing,,", SealedRecord.Class("testing"), - SealedRecord.serializer(), - Csv + SealedRecord.serializer() ) @Test - fun testSealedOtherClass() = assertStringFormAndRestored( + fun testSealedOtherClass() = Csv.assertEncodeAndDecode( "kotlinx.serialization.csv.records.SealedRecord.OtherClass,,,41,test-ing", SealedRecord.OtherClass("test-ing"), - SealedRecord.serializer(), - Csv + SealedRecord.serializer() ) @Test - fun testSealedList() = assertStringFormAndRestored( + fun testSealedList() = Csv.assertEncodeAndDecode( """ |kotlinx.serialization.csv.records.SealedRecord.Object,,,, |kotlinx.serialization.csv.records.SealedRecord.OtherObject,,,, @@ -67,7 +62,6 @@ class CsvSealedKindTest { SealedRecord.OtherObject, SealedRecord.Class("testing") ), - ListSerializer(SealedRecord.serializer()), - Csv + ListSerializer(SealedRecord.serializer()) ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvSimpleRecordTest.kt b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvSimpleRecordTest.kt index 156736f..01d1cd9 100644 --- a/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvSimpleRecordTest.kt +++ b/library/src/test/kotlin/kotlinx/serialization/csv/records/CsvSimpleRecordTest.kt @@ -3,8 +3,8 @@ package kotlinx.serialization.csv.records import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.csv.Csv -import kotlinx.serialization.test.assertParse -import kotlinx.serialization.test.assertStringFormAndRestored +import kotlinx.serialization.test.assertDecode +import kotlinx.serialization.test.assertEncodeAndDecode import kotlin.test.Test /** @@ -14,103 +14,91 @@ import kotlin.test.Test class CsvSimpleRecordTest { @Test - fun testInteger() = assertStringFormAndRestored( + fun testInteger() = Csv.assertEncodeAndDecode( "-150", IntRecord(-150), - IntRecord.serializer(), - Csv + IntRecord.serializer() ) @Test - fun testByte() = assertStringFormAndRestored( + fun testByte() = Csv.assertEncodeAndDecode( "-15", ByteRecord(-15), - ByteRecord.serializer(), - Csv + ByteRecord.serializer() ) @Test - fun testShort() = assertStringFormAndRestored( + fun testShort() = Csv.assertEncodeAndDecode( "-150", ShortRecord(-150), - ShortRecord.serializer(), - Csv + ShortRecord.serializer() ) @Test - fun testLong() = assertStringFormAndRestored( + fun testLong() = Csv.assertEncodeAndDecode( "-150", LongRecord(-150), - LongRecord.serializer(), - Csv + LongRecord.serializer() ) @Test - fun testFloat() = assertStringFormAndRestored( + fun testFloat() = Csv.assertEncodeAndDecode( "-150.0", FloatRecord(-150.0f), - FloatRecord.serializer(), - Csv + FloatRecord.serializer() ) @Test - fun testDouble() = assertStringFormAndRestored( + fun testDouble() = Csv.assertEncodeAndDecode( "-150.0", DoubleRecord(-150.0), - DoubleRecord.serializer(), - Csv + DoubleRecord.serializer() ) @Test - fun testBoolean() = assertStringFormAndRestored( + fun testBoolean() = Csv.assertEncodeAndDecode( "true", BooleanRecord(true), - BooleanRecord.serializer(), - Csv + BooleanRecord.serializer() ) @Test - fun testChar() = assertStringFormAndRestored( + fun testChar() = Csv.assertEncodeAndDecode( "t", CharRecord('t'), - CharRecord.serializer(), - Csv + CharRecord.serializer() ) @Test - fun testString() = assertStringFormAndRestored( + fun testString() = Csv.assertEncodeAndDecode( "testing", StringRecord("testing"), - StringRecord.serializer(), - Csv + StringRecord.serializer() ) @Test - fun testNull() = assertStringFormAndRestored( + fun testNull() = Csv.assertEncodeAndDecode( "", NullRecord(null), - NullRecord.serializer(), - Csv + NullRecord.serializer() ) @Test - fun testUnit() = assertStringFormAndRestored( + fun testUnit() = Csv.assertEncodeAndDecode( "kotlin.Unit", UnitRecord(Unit), - UnitRecord.serializer(), - Csv + UnitRecord.serializer() ) @Test - fun testEnum() = assertStringFormAndRestored( + fun testEnum() = Csv.assertEncodeAndDecode( "FIRST", EnumRecord(Enum.FIRST), - EnumRecord.serializer(), - Csv + EnumRecord.serializer() ) @Test - fun testComplex() = assertStringFormAndRestored( + fun testComplex() = Csv.assertEncodeAndDecode( "-150,-1,42,9223372036854775807,-2.0,24.24,true,testing,,kotlin.Unit,FIRST", ComplexRecord( -150, @@ -125,29 +113,26 @@ class CsvSimpleRecordTest { Unit, Enum.FIRST ), - ComplexRecord.serializer(), - Csv + ComplexRecord.serializer() ) @Test - fun testIntList() = assertStringFormAndRestored( + fun testIntList() = Csv.assertEncodeAndDecode( "-150\r\n150", listOf( IntRecord(-150), IntRecord(150) ), - ListSerializer(IntRecord.serializer()), - Csv + ListSerializer(IntRecord.serializer()) ) @Test - fun testIntListWithLastLineEmpty() = assertParse( + fun testIntListWithLastLineEmpty() = Csv.assertDecode( "-150\r\n150\r\n", listOf( IntRecord(-150), IntRecord(150) ), - ListSerializer(IntRecord.serializer()), - Csv + ListSerializer(IntRecord.serializer()) ) } diff --git a/library/src/test/kotlin/kotlinx/serialization/test/TestingFramework.kt b/library/src/test/kotlin/kotlinx/serialization/test/TestingFramework.kt index 74163e9..d2ce3c5 100644 --- a/library/src/test/kotlin/kotlinx/serialization/test/TestingFramework.kt +++ b/library/src/test/kotlin/kotlinx/serialization/test/TestingFramework.kt @@ -12,58 +12,40 @@ import kotlin.test.assertEquals import kotlin.test.assertFailsWith @OptIn(ExperimentalSerializationApi::class) -inline fun assertStringFormAndRestored( +inline fun StringFormat.assertEncodeAndDecode( expected: String, original: T, serializer: KSerializer, - format: StringFormat, printResult: Boolean = false ) { - val string = format.encodeToString(serializer, original) + val string = encodeToString(serializer, original) if (printResult) println("[Serialized form] $string") assertEquals(expected, string) - val restored = format.decodeFromString(serializer, string) + val restored = decodeFromString(serializer, string) if (printResult) println("[Restored form] $restored") assertEquals(original, restored) } @OptIn(ExperimentalSerializationApi::class) -inline fun assertParse( +inline fun StringFormat.assertDecode( input: String, expected: T, serializer: KSerializer, - format: StringFormat, printResult: Boolean = false ) { - val restored = format.decodeFromString(serializer, input) + val restored = decodeFromString(serializer, input) if (printResult) println("[Restored form] $restored") assertEquals(expected, restored) } @OptIn(ExperimentalSerializationApi::class) -inline fun assertParseFails( +inline fun StringFormat.assertDecodeFails( input: String, - serializer: KSerializer, - format: StringFormat + serializer: KSerializer ) { assertFailsWith { - format.decodeFromString(serializer, input) + decodeFromString(serializer, input) } } -@OptIn(ExperimentalSerializationApi::class) -inline fun StringFormat.assertStringFormAndRestored( - expected: String, - original: T, - serializer: KSerializer, - printResult: Boolean = false -) { - val string = this.encodeToString(serializer, original) - if (printResult) println("[Serialized form] $string") - assertEquals(expected, string) - val restored = this.decodeFromString(serializer, string) - if (printResult) println("[Restored form] $restored") - assertEquals(original, restored) -} - infix fun T.shouldBe(expected: T) = assertEquals(expected, this)