7
7
package bson
8
8
9
9
import (
10
- "bytes"
11
10
"encoding/hex"
12
11
"encoding/json"
13
12
"fmt"
14
13
"io/ioutil"
15
14
"path"
16
- "reflect"
17
15
"strconv"
18
16
"strings"
19
17
"testing"
@@ -23,7 +21,6 @@ import (
23
21
"github.com/google/go-cmp/cmp"
24
22
"github.com/mongodb/mongo-go-driver/bson/bsoncodec"
25
23
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
26
- "github.com/mongodb/mongo-go-driver/x/bsonx"
27
24
"github.com/stretchr/testify/require"
28
25
"github.com/tidwall/pretty"
29
26
)
@@ -62,7 +59,6 @@ type parseErrorTestCase struct {
62
59
63
60
const dataDir = "../data"
64
61
65
- var pc bsonx.PrimitiveCodecs
66
62
var dvd bsoncodec.DefaultValueDecoders
67
63
var dve bsoncodec.DefaultValueEncoders
68
64
@@ -163,39 +159,36 @@ func normalizeRelaxedDouble(t *testing.T, key string, rEJ string) string {
163
159
}
164
160
165
161
// bsonToNative decodes the BSON bytes (b) into a native Document
166
- func bsonToNative (t * testing.T , b []byte , bType , testDesc string ) bsonx. Doc {
167
- doc := reflect . New ( reflect . TypeOf (bsonx. Doc {})). Elem ()
168
- err := pc . DocumentDecodeValue ( dc , bsonrw . NewBSONDocumentReader ( b ), doc )
162
+ func bsonToNative (t * testing.T , b []byte , bType , testDesc string ) D {
163
+ var doc D
164
+ err := Unmarshal ( b , & doc )
169
165
expectNoError (t , err , fmt .Sprintf ("%s: decoding %s BSON" , testDesc , bType ))
170
- return doc . Interface ().(bsonx. Doc )
166
+ return doc
171
167
}
172
168
173
169
// nativeToBSON encodes the native Document (doc) into canonical BSON and compares it to the expected
174
170
// canonical BSON (cB)
175
- func nativeToBSON (t * testing.T , cB []byte , doc bsonx.Doc , testDesc , bType , docSrcDesc string ) {
176
- actualB := new (bytes.Buffer )
177
- vw , err := bsonrw .NewBSONValueWriter (actualB )
178
- expectNoError (t , err , fmt .Sprintf ("%s: creating ValueWriter" , testDesc ))
179
- err = pc .DocumentEncodeValue (ec , vw , reflect .ValueOf (doc ))
171
+ func nativeToBSON (t * testing.T , cB []byte , doc D , testDesc , bType , docSrcDesc string ) {
172
+ actual , err := Marshal (doc )
180
173
expectNoError (t , err , fmt .Sprintf ("%s: encoding %s BSON" , testDesc , bType ))
181
174
182
- if diff := cmp .Diff (cB , actualB . Bytes () ); diff != "" {
175
+ if diff := cmp .Diff (cB , actual ); diff != "" {
183
176
t .Errorf ("%s: 'native_to_bson(%s) = cB' failed (-want, +got):\n -%v\n +%v\n " ,
184
- testDesc , docSrcDesc , cB , actualB . Bytes () )
177
+ testDesc , docSrcDesc , cB , actual )
185
178
t .FailNow ()
186
179
}
187
180
}
188
181
189
182
// jsonToNative decodes the extended JSON string (ej) into a native Document
190
- func jsonToNative (t * testing.T , ej , ejType , testDesc string ) bsonx. Doc {
191
- var doc bsonx. Doc
183
+ func jsonToNative (t * testing.T , ej , ejType , testDesc string ) D {
184
+ var doc D
192
185
err := UnmarshalExtJSON ([]byte (ej ), ejType != "relaxed" , & doc )
193
186
expectNoError (t , err , fmt .Sprintf ("%s: decoding %s extended JSON" , testDesc , ejType ))
194
187
return doc
195
188
}
196
189
197
190
// nativeToJSON encodes the native Document (doc) into an extended JSON string
198
- func nativeToJSON (t * testing.T , ej string , doc bsonx. Doc , testDesc , ejType , ejShortName , docSrcDesc string ) {
191
+ func nativeToJSON (t * testing.T , ej string , doc D , testDesc , ejType , ejShortName , docSrcDesc string ) {
199
192
actualEJ , err := MarshalExtJSON (doc , ejType != "relaxed" , true )
200
193
expectNoError (t , err , fmt .Sprintf ("%s: encoding %s extended JSON" , testDesc , ejType ))
201
194
@@ -300,8 +293,8 @@ func runTest(t *testing.T, file string) {
300
293
b , err := hex .DecodeString (d .Bson )
301
294
expectNoError (t , err , d .Description )
302
295
303
- doc := reflect . New ( reflect . TypeOf (bsonx. Doc {})). Elem ()
304
- err = pc . DocumentDecodeValue ( dc , bsonrw . NewBSONDocumentReader ( b ), doc )
296
+ var doc D
297
+ err = Unmarshal ( b , & doc )
305
298
expectError (t , err , fmt .Sprintf ("%s: expected decode error" , d .Description ))
306
299
}
307
300
@@ -318,7 +311,7 @@ func runTest(t *testing.T, file string) {
318
311
319
312
switch test .BsonType {
320
313
case "0x00" :
321
- var doc bsonx. Doc
314
+ var doc D
322
315
err := UnmarshalExtJSON ([]byte (s ), true , & doc )
323
316
expectError (t , err , fmt .Sprintf ("%s: expected parse error" , p .Description ))
324
317
case "0x13" :
0 commit comments