Skip to content

Commit c1c5633

Browse files
author
Isabella Siu
committed
GODRIVER-409 add test that structs are not zeroed when decoding
Change-Id: I360745392a4379dedbdf536a33334cfecd069db0
1 parent a29da95 commit c1c5633

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

bson/decoder_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
1717
"github.com/mongodb/mongo-go-driver/bson/bsonrw/bsonrwtest"
1818
"github.com/mongodb/mongo-go-driver/bson/bsontype"
19+
"github.com/mongodb/mongo-go-driver/x/bsonx"
1920
)
2021

2122
func TestBasicDecode(t *testing.T) {
@@ -138,6 +139,26 @@ func TestDecoderv2(t *testing.T) {
138139
}
139140
})
140141
})
142+
t.Run("Decode doesn't zero struct", func(t *testing.T) {
143+
type foo struct {
144+
Item string
145+
Qty int
146+
Bonus int
147+
}
148+
var got foo
149+
got.Item = "apple"
150+
got.Bonus = 2
151+
data := docToBytes(bsonx.Doc{{"item", bsonx.String("canvas")}, {"qty", bsonx.Int32(4)}})
152+
vr := bsonrw.NewBSONDocumentReader(data)
153+
dec, err := NewDecoder(DefaultRegistry, vr)
154+
noerr(t, err)
155+
err = dec.Decode(&got)
156+
noerr(t, err)
157+
want := foo{Item: "canvas", Qty: 4, Bonus: 2}
158+
if !reflect.DeepEqual(got, want) {
159+
t.Errorf("Results do not match. got %+v; want %+v", got, want)
160+
}
161+
})
141162
t.Run("Reset", func(t *testing.T) {
142163
vr1, vr2 := bsonrw.NewBSONDocumentReader([]byte{}), bsonrw.NewBSONDocumentReader([]byte{})
143164
dec, err := NewDecoder(DefaultRegistry, vr1)

0 commit comments

Comments
 (0)