@@ -34,9 +34,9 @@ import (
3434 "errors"
3535 "net/url"
3636 "reflect"
37+ "strings"
3738 "testing"
3839 "time"
39- "strings"
4040
4141 "github.com/globalsign/mgo/bson"
4242 . "gopkg.in/check.v1"
@@ -111,6 +111,10 @@ var sampleItems = []testItemType{
111111 {bson.M {"BSON" : []interface {}{"awesome" , float64 (5.05 ), 1986 }},
112112 "1\x00 \x00 \x00 \x04 BSON\x00 &\x00 \x00 \x00 \x02 0\x00 \x08 \x00 \x00 \x00 " +
113113 "awesome\x00 \x01 1\x00 333333\x14 @\x10 2\x00 \xc2 \x07 \x00 \x00 \x00 \x00 " },
114+ {bson.M {"slice" : []uint8 {1 , 2 }},
115+ "\x13 \x00 \x00 \x00 \x05 slice\x00 \x02 \x00 \x00 \x00 \x00 \x01 \x02 \x00 " },
116+ {bson.M {"slice" : []byte {1 , 2 }},
117+ "\x13 \x00 \x00 \x00 \x05 slice\x00 \x02 \x00 \x00 \x00 \x00 \x01 \x02 \x00 " },
114118}
115119
116120func (s * S ) TestMarshalSampleItems (c * C ) {
@@ -343,6 +347,27 @@ func (s *S) TestOneWayMarshalItems(c *C) {
343347 }
344348}
345349
350+ // --------------------------------------------------------------------------
351+ // Some ops marshaling operations which would encode []uint8 or []byte in array.
352+
353+ var arrayOpsMarshalItems = []testItemType {
354+ {bson.M {"_" : bson.M {"$in" : []uint8 {1 , 2 }}},
355+ "\x03 _\x00 \x1d \x00 \x00 \x00 \x04 $in\x00 \x13 \x00 \x00 \x00 \x10 0\x00 \x01 \x00 \x00 \x00 \x10 1\x00 \x02 \x00 \x00 \x00 \x00 \x00 " },
356+ {bson.M {"_" : bson.M {"$nin" : []uint8 {1 , 2 }}},
357+ "\x03 _\x00 \x1e \x00 \x00 \x00 \x04 $nin\x00 \x13 \x00 \x00 \x00 \x10 0\x00 \x01 \x00 \x00 \x00 \x10 1\x00 \x02 \x00 \x00 \x00 \x00 \x00 " },
358+ {bson.M {"_" : bson.M {"$all" : []uint8 {1 , 2 }}},
359+ "\x03 _\x00 \x1e \x00 \x00 \x00 \x04 $all\x00 \x13 \x00 \x00 \x00 \x10 0\x00 \x01 \x00 \x00 \x00 \x10 1\x00 \x02 \x00 \x00 \x00 \x00 \x00 " },
360+ }
361+
362+ func (s * S ) TestArrayOpsMarshalItems (c * C ) {
363+ for i , item := range arrayOpsMarshalItems {
364+ data , err := bson .Marshal (item .obj )
365+ c .Assert (err , IsNil )
366+ c .Assert (string (data ), Equals , wrapInDoc (item .data ),
367+ Commentf ("Failed on item %d" , i ))
368+ }
369+ }
370+
346371// --------------------------------------------------------------------------
347372// Two-way tests for user-defined structures using the samples
348373// from bsonspec.org.
0 commit comments