MCPcopy
hub / github.com/uber-go/zap / TestObjectsAndObjectValues_marshalError

Function TestObjectsAndObjectValues_marshalError

array_test.go:202–258  ·  array_test.go::TestObjectsAndObjectValues_marshalError
(t *testing.T)

Source from the content-addressed store, hash-verified

200}
201
202func TestObjectsAndObjectValues_marshalError(t *testing.T) {
203 t.Parallel()
204
205 tests := []struct {
206 desc string
207 give Field
208 want []any
209 wantErr string
210 }{
211 {
212 desc: "Objects",
213 give: Objects("", []*fakeObject{
214 {value: "foo"},
215 {value: "bar", err: errors.New("great sadness")},
216 {value: "baz"}, // does not get marshaled
217 }),
218 want: []any{
219 map[string]any{"value": "foo"},
220 map[string]any{"value": "bar"},
221 },
222 wantErr: "great sadness",
223 },
224 {
225 desc: "ObjectValues",
226 give: ObjectValues("", []fakeObject{
227 {value: "foo"},
228 {value: "bar", err: errors.New("stuff failed")},
229 {value: "baz"}, // does not get marshaled
230 }),
231 want: []any{
232 map[string]any{"value": "foo"},
233 map[string]any{"value": "bar"},
234 },
235 wantErr: "stuff failed",
236 },
237 }
238
239 for _, tt := range tests {
240 tt := tt
241 t.Run(tt.desc, func(t *testing.T) {
242 t.Parallel()
243
244 tt.give.Key = "k"
245
246 enc := zapcore.NewMapObjectEncoder()
247 tt.give.AddTo(enc)
248
249 require.Contains(t, enc.Fields, "k")
250 assert.Equal(t, tt.want, enc.Fields["k"])
251
252 // AddTo puts the error in a "%vError" field based on the name of the
253 // original field.
254 require.Contains(t, enc.Fields, "kError")
255 assert.Equal(t, tt.wantErr, enc.Fields["kError"])
256 })
257 }
258}
259

Callers

nothing calls this directly

Calls 4

NewMapObjectEncoderFunction · 0.92
ObjectsFunction · 0.85
ObjectValuesFunction · 0.85
AddToMethod · 0.80

Tested by

no test coverage detected