15
15
*/
16
16
package org.bson.codecs.kotlinx
17
17
18
+ import java.util.stream.Stream
18
19
import kotlin.test.assertEquals
19
20
import kotlinx.serialization.ExperimentalSerializationApi
20
21
import kotlinx.serialization.MissingFieldException
@@ -23,12 +24,17 @@ import kotlinx.serialization.modules.SerializersModule
23
24
import kotlinx.serialization.modules.plus
24
25
import kotlinx.serialization.modules.polymorphic
25
26
import kotlinx.serialization.modules.subclass
27
+ import org.bson.BsonBoolean
26
28
import org.bson.BsonDocument
27
29
import org.bson.BsonDocumentReader
28
30
import org.bson.BsonDocumentWriter
31
+ import org.bson.BsonDouble
32
+ import org.bson.BsonInt32
33
+ import org.bson.BsonInt64
29
34
import org.bson.BsonInvalidOperationException
30
35
import org.bson.BsonMaxKey
31
36
import org.bson.BsonMinKey
37
+ import org.bson.BsonString
32
38
import org.bson.BsonUndefined
33
39
import org.bson.codecs.DecoderContext
34
40
import org.bson.codecs.EncoderContext
@@ -90,11 +96,12 @@ import org.bson.codecs.kotlinx.samples.SealedInterface
90
96
import org.bson.codecs.kotlinx.samples.ValueClass
91
97
import org.junit.jupiter.api.Test
92
98
import org.junit.jupiter.api.assertThrows
99
+ import org.junit.jupiter.params.ParameterizedTest
100
+ import org.junit.jupiter.params.provider.MethodSource
93
101
94
102
@OptIn(ExperimentalSerializationApi ::class )
95
103
@Suppress(" LargeClass" )
96
104
class KotlinSerializerCodecTest {
97
- private val numberLong = " \$ numberLong"
98
105
private val oid = " \$ oid"
99
106
private val emptyDocument = " {}"
100
107
private val altConfiguration =
@@ -134,15 +141,59 @@ class KotlinSerializerCodecTest {
134
141
135
142
private val allBsonTypesDocument = BsonDocument .parse(allBsonTypesJson)
136
143
137
- @Test
138
- fun testDataClassWithSimpleValues () {
139
- val expected =
140
- """ {"char": "c", "byte": 0, "short": 1, "int": 22, "long": {"$numberLong ": "42"}, "float": 4.0,
141
- | "double": 4.2, "boolean": true, "string": "String"}"""
142
- .trimMargin()
143
- val dataClass = DataClassWithSimpleValues (' c' , 0 , 1 , 22 , 42L , 4.0f , 4.2 , true , " String" )
144
+ companion object {
145
+ @JvmStatic
146
+ fun testTypesCastingDataClassWithSimpleValues (): Stream <BsonDocument > {
147
+ return Stream .of(
148
+ BsonDocument ()
149
+ .append(" char" , BsonString (" c" ))
150
+ .append(" byte" , BsonInt32 (1 ))
151
+ .append(" short" , BsonInt32 (2 ))
152
+ .append(" int" , BsonInt32 (10 ))
153
+ .append(" long" , BsonInt32 (10 ))
154
+ .append(" float" , BsonInt32 (2 ))
155
+ .append(" double" , BsonInt32 (3 ))
156
+ .append(" boolean" , BsonBoolean .TRUE )
157
+ .append(" string" , BsonString (" String" )),
158
+ BsonDocument ()
159
+ .append(" char" , BsonString (" c" ))
160
+ .append(" byte" , BsonDouble (1.0 ))
161
+ .append(" short" , BsonDouble (2.0 ))
162
+ .append(" int" , BsonDouble (9.9999999999999992 ))
163
+ .append(" long" , BsonDouble (9.9999999999999992 ))
164
+ .append(" float" , BsonDouble (2.0 ))
165
+ .append(" double" , BsonDouble (3.0 ))
166
+ .append(" boolean" , BsonBoolean .TRUE )
167
+ .append(" string" , BsonString (" String" )),
168
+ BsonDocument ()
169
+ .append(" char" , BsonString (" c" ))
170
+ .append(" byte" , BsonDouble (1.0 ))
171
+ .append(" short" , BsonDouble (2.0 ))
172
+ .append(" int" , BsonDouble (10.0 ))
173
+ .append(" long" , BsonDouble (10.0 ))
174
+ .append(" float" , BsonDouble (2.0 ))
175
+ .append(" double" , BsonDouble (3.0 ))
176
+ .append(" boolean" , BsonBoolean .TRUE )
177
+ .append(" string" , BsonString (" String" )),
178
+ BsonDocument ()
179
+ .append(" char" , BsonString (" c" ))
180
+ .append(" byte" , BsonInt64 (1 ))
181
+ .append(" short" , BsonInt64 (2 ))
182
+ .append(" int" , BsonInt64 (10 ))
183
+ .append(" long" , BsonInt64 (10 ))
184
+ .append(" float" , BsonInt64 (2 ))
185
+ .append(" double" , BsonInt64 (3 ))
186
+ .append(" boolean" , BsonBoolean .TRUE )
187
+ .append(" string" , BsonString (" String" )))
188
+ }
189
+ }
144
190
145
- assertRoundTrips(expected, dataClass)
191
+ @ParameterizedTest
192
+ @MethodSource(" testTypesCastingDataClassWithSimpleValues" )
193
+ fun testTypesCastingDataClassWithSimpleValues (data : BsonDocument ) {
194
+ val expectedDataClass = DataClassWithSimpleValues (' c' , 1 , 2 , 10 , 10L , 2.0f , 3.0 , true , " String" )
195
+
196
+ assertDecodesTo(data, expectedDataClass)
146
197
}
147
198
148
199
@Test
0 commit comments