@@ -9,9 +9,10 @@ import org.bson.types.Decimal128
99import org .scalactic .source .Position
1010import org .scalatest .funsuite .AnyFunSuite
1111import org .scalatestplus .scalacheck .ScalaCheckPropertyChecks
12-
1312import java .nio .ByteBuffer
1413
14+ import com .avsystem .commons .serialization .GenCodec .ReadFailure
15+
1516class BinaryBsonGenCodecRoundtripTest extends GenCodecRoundtripTest {
1617 type Raw = Array [Byte ]
1718
@@ -75,6 +76,18 @@ class BsonValueGenCodecRoundtripTest extends GenCodecRoundtripTest {
7576 val result = input.readSimple().readLong()
7677 assert(result == 42L )
7778 }
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+ }
7891}
7992
8093class BsonInputOutputTest extends AnyFunSuite with ScalaCheckPropertyChecks {
@@ -234,6 +247,26 @@ class BsonInputOutputTest extends AnyFunSuite with ScalaCheckPropertyChecks {
234247 }
235248 }
236249
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+
237270 test(" BsonBinaryReader type metadata" ) {
238271 forAll(SomethingPlain .gen) { sth =>
239272 val document = somethingToBson(sth)
0 commit comments