@@ -9,11 +9,13 @@ package bson
9
9
import (
10
10
"bytes"
11
11
"io"
12
+ "reflect"
12
13
"testing"
13
14
"time"
14
15
15
16
"github.com/google/go-cmp/cmp"
16
17
"github.com/mongodb/mongo-go-driver/bson/objectid"
18
+ "github.com/stretchr/testify/assert"
17
19
)
18
20
19
21
func TestEncoder (t * testing.T ) {
@@ -911,6 +913,42 @@ func reflectionEncoderTest(t *testing.T) {
911
913
}
912
914
}
913
915
916
+ type zeroTest struct {
917
+ reportZero bool
918
+ }
919
+
920
+ func (z zeroTest ) IsZero () bool { return z .reportZero }
921
+
922
+ type nonZeroer struct {
923
+ value bool
924
+ }
925
+
926
+ func TestZeoerInterfaceUsedByDecoder (t * testing.T ) {
927
+ enc := & encoder {}
928
+
929
+ // cases that are zero, because they are known types or pointers
930
+ var st * nonZeroer
931
+ assert .True (t , enc .isZero (reflect .ValueOf (st )))
932
+ assert .True (t , enc .isZero (reflect .ValueOf (0 )))
933
+ assert .True (t , enc .isZero (reflect .ValueOf (false )))
934
+
935
+ // cases that shouldn't be zero
936
+ st = & nonZeroer {value : false }
937
+ assert .False (t , enc .isZero (reflect .ValueOf (struct { val bool }{val : true })))
938
+ assert .False (t , enc .isZero (reflect .ValueOf (struct { val bool }{val : false })))
939
+ assert .False (t , enc .isZero (reflect .ValueOf (st )))
940
+ st .value = true
941
+ assert .False (t , enc .isZero (reflect .ValueOf (st )))
942
+
943
+ // a test to see if the interface impacts the outcome
944
+ z := zeroTest {}
945
+ assert .False (t , enc .isZero (reflect .ValueOf (z )))
946
+
947
+ z .reportZero = true
948
+ assert .True (t , enc .isZero (reflect .ValueOf (z )))
949
+
950
+ }
951
+
914
952
func docToBytes (d * Document ) []byte {
915
953
b , err := d .MarshalBSON ()
916
954
if err != nil {
0 commit comments