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

Function TestJSONCustomReflectedEncoder

zapcore/json_encoder_test.go:229–282  ·  zapcore/json_encoder_test.go::TestJSONCustomReflectedEncoder
(t *testing.T)

Source from the content-addressed store, hash-verified

227}
228
229func TestJSONCustomReflectedEncoder(t *testing.T) {
230 tests := []struct {
231 name string
232 field zapcore.Field
233 expected string
234 }{
235 {
236 name: "encode custom map object",
237 field: zapcore.Field{
238 Key: "data",
239 Type: zapcore.ReflectType,
240 Interface: map[string]interface{}{
241 "foo": "hello",
242 "bar": 1111,
243 },
244 },
245 expected: `{"data":{}}`,
246 },
247 {
248 name: "encode nil object",
249 field: zapcore.Field{
250 Key: "data",
251 Type: zapcore.ReflectType,
252 },
253 expected: `{"data":null}`,
254 },
255 }
256
257 for _, tt := range tests {
258 tt := tt
259 t.Run(tt.name, func(t *testing.T) {
260 t.Parallel()
261
262 enc := zapcore.NewJSONEncoder(zapcore.EncoderConfig{
263 NewReflectedEncoder: func(writer io.Writer) zapcore.ReflectedEncoder {
264 return &emptyReflectedEncoder{
265 writer: writer,
266 }
267 },
268 })
269
270 buf, err := enc.EncodeEntry(zapcore.Entry{
271 Level: zapcore.DebugLevel,
272 Time: time.Now(),
273 LoggerName: "logger",
274 Message: "things happened",
275 }, []zapcore.Field{tt.field})
276 if assert.NoError(t, err, "Unexpected JSON encoding error.") {
277 assert.JSONEq(t, tt.expected, buf.String(), "Incorrect encoded JSON entry.")
278 }
279 buf.Free()
280 })
281 }
282}

Callers

nothing calls this directly

Calls 5

NewJSONEncoderFunction · 0.92
EncodeEntryMethod · 0.65
NowMethod · 0.65
StringMethod · 0.45
FreeMethod · 0.45

Tested by

no test coverage detected