@@ -9,9 +9,10 @@ import org.bson.types.Decimal128
9
9
import org .scalactic .source .Position
10
10
import org .scalatest .funsuite .AnyFunSuite
11
11
import org .scalatestplus .scalacheck .ScalaCheckPropertyChecks
12
-
13
12
import java .nio .ByteBuffer
14
13
14
+ import com .avsystem .commons .serialization .GenCodec .ReadFailure
15
+
15
16
class BinaryBsonGenCodecRoundtripTest extends GenCodecRoundtripTest {
16
17
type Raw = Array [Byte ]
17
18
@@ -75,6 +76,18 @@ class BsonValueGenCodecRoundtripTest extends GenCodecRoundtripTest {
75
76
val result = input.readSimple().readLong()
76
77
assert(result == 42L )
77
78
}
79
+
80
+ test(" Int32 to Double decoding" ) {
81
+ val input = createInput(new BsonInt32 (43 ))
82
+ val result = input.readSimple().readDouble()
83
+ assert(result == 43D )
84
+ }
85
+
86
+ test(" Int64 to Double decoding exception" ) {
87
+ val input = createInput(new BsonInt64 (44 ))
88
+
89
+ assertThrows[ReadFailure ](input.readSimple().readDouble())
90
+ }
78
91
}
79
92
80
93
class BsonInputOutputTest extends AnyFunSuite with ScalaCheckPropertyChecks {
@@ -234,6 +247,26 @@ class BsonInputOutputTest extends AnyFunSuite with ScalaCheckPropertyChecks {
234
247
}
235
248
}
236
249
250
+ test(" BsonBinaryReader Int types to Double decoding" ) {
251
+ val document = new BsonDocument ()
252
+ .append(" int" , new BsonInt32 (42 ))
253
+ .append(" long" , new BsonInt64 (42L ))
254
+
255
+ val rawJson = document.toJson(JsonWriterSettings .builder.outputMode(JsonMode .EXTENDED ).build)
256
+ val input = new BsonReaderInput (new BsonBinaryReader (RawBsonDocument .parse(rawJson).getByteBuffer.asNIO()))
257
+
258
+ val objectInput = input.readObject()
259
+
260
+ val intField = objectInput.peekField(" int" )
261
+ assert(intField.nonEmpty)
262
+ assert(intField.get.readDouble() == 42D )
263
+
264
+
265
+ val longField = objectInput.peekField(" long" )
266
+ assert(longField.nonEmpty)
267
+ assertThrows[ReadFailure ](longField.get.readDouble())
268
+ }
269
+
237
270
test(" BsonBinaryReader type metadata" ) {
238
271
forAll(SomethingPlain .gen) { sth =>
239
272
val document = somethingToBson(sth)
0 commit comments