1
2
3
4
5
6
7 package bson
8
9 import (
10 "strings"
11 "testing"
12
13 "go.mongodb.org/mongo-driver/bson/bsoncodec"
14 "go.mongodb.org/mongo-driver/bson/bsontype"
15 "go.mongodb.org/mongo-driver/internal/assert"
16 )
17
18 func TestMarshalValue(t *testing.T) {
19 t.Parallel()
20
21 marshalValueTestCases := newMarshalValueTestCasesWithInterfaceCore(t)
22
23 t.Run("MarshalValue", func(t *testing.T) {
24 t.Parallel()
25
26 for _, tc := range marshalValueTestCases {
27 tc := tc
28
29 t.Run(tc.name, func(t *testing.T) {
30 t.Parallel()
31
32 valueType, valueBytes, err := MarshalValue(tc.val)
33 assert.Nil(t, err, "MarshalValue error: %v", err)
34 compareMarshalValueResults(t, tc, valueType, valueBytes)
35 })
36 }
37 })
38 t.Run("MarshalValueAppend", func(t *testing.T) {
39 t.Parallel()
40
41 for _, tc := range marshalValueTestCases {
42 tc := tc
43
44 t.Run(tc.name, func(t *testing.T) {
45 t.Parallel()
46
47 valueType, valueBytes, err := MarshalValueAppend(nil, tc.val)
48 assert.Nil(t, err, "MarshalValueAppend error: %v", err)
49 compareMarshalValueResults(t, tc, valueType, valueBytes)
50 })
51 }
52 })
53 t.Run("MarshalValueWithRegistry", func(t *testing.T) {
54 t.Parallel()
55
56 for _, tc := range marshalValueTestCases {
57 tc := tc
58
59 t.Run(tc.name, func(t *testing.T) {
60 t.Parallel()
61
62 valueType, valueBytes, err := MarshalValueWithRegistry(DefaultRegistry, tc.val)
63 assert.Nil(t, err, "MarshalValueWithRegistry error: %v", err)
64 compareMarshalValueResults(t, tc, valueType, valueBytes)
65 })
66 }
67 })
68 t.Run("MarshalValueWithContext", func(t *testing.T) {
69 t.Parallel()
70
71 ec := bsoncodec.EncodeContext{Registry: DefaultRegistry}
72 for _, tc := range marshalValueTestCases {
73 tc := tc
74
75 t.Run(tc.name, func(t *testing.T) {
76 t.Parallel()
77
78 valueType, valueBytes, err := MarshalValueWithContext(ec, tc.val)
79 assert.Nil(t, err, "MarshalValueWithContext error: %v", err)
80 compareMarshalValueResults(t, tc, valueType, valueBytes)
81 })
82 }
83 })
84 t.Run("MarshalValueAppendWithRegistry", func(t *testing.T) {
85 t.Parallel()
86
87 for _, tc := range marshalValueTestCases {
88 tc := tc
89
90 t.Run(tc.name, func(t *testing.T) {
91 t.Parallel()
92
93 valueType, valueBytes, err := MarshalValueAppendWithRegistry(DefaultRegistry, nil, tc.val)
94 assert.Nil(t, err, "MarshalValueAppendWithRegistry error: %v", err)
95 compareMarshalValueResults(t, tc, valueType, valueBytes)
96 })
97 }
98 })
99 t.Run("MarshalValueAppendWithContext", func(t *testing.T) {
100 t.Parallel()
101
102 ec := bsoncodec.EncodeContext{Registry: DefaultRegistry}
103 for _, tc := range marshalValueTestCases {
104 tc := tc
105
106 t.Run(tc.name, func(t *testing.T) {
107 t.Parallel()
108
109 valueType, valueBytes, err := MarshalValueAppendWithContext(ec, nil, tc.val)
110 assert.Nil(t, err, "MarshalValueWithContext error: %v", err)
111 compareMarshalValueResults(t, tc, valueType, valueBytes)
112 })
113 }
114 })
115 }
116
117 func compareMarshalValueResults(t *testing.T, tc marshalValueTestCase, gotType bsontype.Type, gotBytes []byte) {
118 t.Helper()
119 expectedValue := RawValue{Type: tc.bsontype, Value: tc.bytes}
120 gotValue := RawValue{Type: gotType, Value: gotBytes}
121 assert.Equal(t, expectedValue, gotValue, "value mismatch; expected %s, got %s", expectedValue, gotValue)
122 }
123
124
125 func BenchmarkSliceCodecMarshal(b *testing.B) {
126 testStruct := unmarshalerNonPtrStruct{B: []byte(strings.Repeat("t", 4096))}
127
128 b.ResetTimer()
129 b.RunParallel(func(pb *testing.PB) {
130 for pb.Next() {
131 _, _, err := MarshalValue(testStruct)
132 if err != nil {
133 b.Fatal(err)
134 }
135 }
136 })
137 }
138
View as plain text