From 2276949691e675739578e3b4a766bdef4d31facb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Wed, 11 Dec 2024 15:05:34 +0100 Subject: [PATCH] contrib/log/slog: test with slogtest (#3018) --- contrib/log/slog/slog_test.go | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/contrib/log/slog/slog_test.go b/contrib/log/slog/slog_test.go index 0d25702cb2..7546009b0f 100644 --- a/contrib/log/slog/slog_test.go +++ b/contrib/log/slog/slog_test.go @@ -14,6 +14,7 @@ import ( "strconv" "strings" "testing" + "testing/slogtest" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -84,13 +85,33 @@ func TestNewJSONHandler(t *testing.T) { } func TestWrapHandler(t *testing.T) { - testLogger( - t, - func(w io.Writer) *slog.Logger { - return slog.New(WrapHandler(slog.NewJSONHandler(w, nil))) - }, - nil, - ) + t.Run("testLogger", func(t *testing.T) { + testLogger( + t, + func(w io.Writer) *slog.Logger { + return slog.New(WrapHandler(slog.NewJSONHandler(w, nil))) + }, + nil, + ) + }) + + t.Run("slogtest", func(t *testing.T) { + var buf bytes.Buffer + h := WrapHandler(slog.NewJSONHandler(&buf, nil)) + results := func() []map[string]any { + var ms []map[string]any + for _, line := range bytes.Split(buf.Bytes(), []byte{'\n'}) { + if len(line) == 0 { + continue + } + var m map[string]any + require.NoError(t, json.Unmarshal(line, &m)) + ms = append(ms, m) + } + return ms + } + require.NoError(t, slogtest.TestHandler(h, results)) + }) } func TestHandlerWithAttrs(t *testing.T) {