Skip to content

Commit

Permalink
test: improve slog coverage (#1347)
Browse files Browse the repository at this point in the history
Improving test coverage on slog package.

Refs #1333
  • Loading branch information
zekth authored Sep 6, 2023
1 parent e5e8b70 commit 9a36792
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions exp/zapslog/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package zapslog
import (
"log/slog"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -69,6 +70,20 @@ func TestAddStack(t *testing.T) {
)
}

func TestAddStackSkip(t *testing.T) {
fac, logs := observer.New(zapcore.DebugLevel)
sl := slog.New(NewHandler(fac, AddStacktraceAt(slog.LevelDebug), WithCallerSkip(1)))
sl.Info("msg")

require.Len(t, logs.AllUntimed(), 1, "Expected exactly one entry to be logged")
entry := logs.AllUntimed()[0]
assert.Regexp(t,
`src/testing/testing.go:\d+`,
entry.Stack,
"Unexpected stack trace annotation.",
)
}

func TestEmptyAttr(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -99,3 +114,66 @@ func TestEmptyAttr(t *testing.T) {
}, logs[0].ContextMap(), "Unexpected context")
})
}

func TestWithName(t *testing.T) {
t.Parallel()
fac, observedLogs := observer.New(zapcore.DebugLevel)
t.Run("default", func(t *testing.T) {
sl := slog.New(NewHandler(fac))
sl.Info("msg")

logs := observedLogs.TakeAll()
require.Len(t, logs, 1, "Expected exactly one entry to be logged")
entry := logs[0]
assert.Equal(t, "", entry.LoggerName, "Unexpected logger name")
})
t.Run("with name", func(t *testing.T) {
sl := slog.New(NewHandler(fac, WithName("test-name")))
sl.Info("msg")

logs := observedLogs.TakeAll()
require.Len(t, logs, 1, "Expected exactly one entry to be logged")
entry := logs[0]
assert.Equal(t, "test-name", entry.LoggerName, "Unexpected logger name")
})
}

type Token string

func (Token) LogValue() slog.Value {
return slog.StringValue("REDACTED_TOKEN")
}

func TestAttrKinds(t *testing.T) {
fac, logs := observer.New(zapcore.DebugLevel)
sl := slog.New(NewHandler(fac))
testToken := Token("no")
sl.Info(
"msg",
slog.Bool("bool", true),
slog.Duration("duration", time.Hour),
slog.Float64("float64", 42.0),
slog.Int64("int64", -1234),
slog.Time("time", time.Date(2015, 10, 21, 7, 28, 00, 0, time.UTC)),
slog.Uint64("uint64", 2),
slog.Group("group", slog.String("inner", "inner-group")),
"logvaluer", testToken,
"any", "what am i?",
)

require.Len(t, logs.AllUntimed(), 1, "Expected exactly one entry to be logged")
entry := logs.AllUntimed()[0]
assert.Equal(t,
map[string]any{
"bool": true,
"duration": time.Hour,
"float64": float64(42),
"group": map[string]any{"inner": "inner-group"},
"int64": int64(-1234),
"time": time.Date(2015, time.October, 21, 7, 28, 0, 0, time.UTC),
"uint64": uint64(2),
"logvaluer": "REDACTED_TOKEN",
"any": "what am i?",
},
entry.ContextMap())
}

0 comments on commit 9a36792

Please sign in to comment.