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

Function TestEmptyAttr

exp/zapslog/handler_test.go:92–133  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

90}
91
92func TestEmptyAttr(t *testing.T) {
93 t.Parallel()
94
95 fac, observedLogs := observer.New(zapcore.DebugLevel)
96 sl := slog.New(NewHandler(fac))
97
98 t.Run("Handle", func(t *testing.T) {
99 sl.Info(
100 "msg",
101 slog.String("foo", "bar"),
102 slog.Attr{},
103 )
104
105 logs := observedLogs.TakeAll()
106 require.Len(t, logs, 1, "Expected exactly one entry to be logged")
107 assert.Equal(t, map[string]any{
108 "foo": "bar",
109 }, logs[0].ContextMap(), "Unexpected context")
110 })
111
112 t.Run("WithAttrs", func(t *testing.T) {
113 sl.With(slog.String("foo", "bar"), slog.Attr{}).Info("msg")
114
115 logs := observedLogs.TakeAll()
116 require.Len(t, logs, 1, "Expected exactly one entry to be logged")
117 assert.Equal(t, map[string]any{
118 "foo": "bar",
119 }, logs[0].ContextMap(), "Unexpected context")
120 })
121
122 t.Run("Group", func(t *testing.T) {
123 sl.With("k", slog.GroupValue(slog.String("foo", "bar"), slog.Attr{})).Info("msg")
124
125 logs := observedLogs.TakeAll()
126 require.Len(t, logs, 1, "Expected exactly one entry to be logged")
127 assert.Equal(t, map[string]any{
128 "k": map[string]any{
129 "foo": "bar",
130 },
131 }, logs[0].ContextMap(), "Unexpected context")
132 })
133}
134
135func TestWithName(t *testing.T) {
136 fac, observedLogs := observer.New(zapcore.DebugLevel)

Callers

nothing calls this directly

Calls 8

NewFunction · 0.92
NewHandlerFunction · 0.85
TakeAllMethod · 0.80
ContextMapMethod · 0.80
WithMethod · 0.65
InfoMethod · 0.45
StringMethod · 0.45
LenMethod · 0.45

Tested by

no test coverage detected