Skip to content

Commit

Permalink
added ReplaceAttr test
Browse files Browse the repository at this point in the history
  • Loading branch information
lmittmann committed Oct 29, 2023
1 parent 0c0d0cf commit da92dd8
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"log/slog"
"os"
"slices"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -98,7 +99,7 @@ func TestHandler(t *testing.T) {
F: func(l *slog.Logger) {
l.Info("test", "key", "val")
},
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:99 test key=val`,
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:100 test key=val`,
},
{
Opts: &tint.Options{
Expand Down Expand Up @@ -325,7 +326,7 @@ func TestHandler(t *testing.T) {
F: func(l *slog.Logger) {
l.Info("test")
},
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:326 test`,
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:327 test`,
},
{ // https://github.com/lmittmann/tint/issues/44
F: func(l *slog.Logger) {
Expand Down Expand Up @@ -385,6 +386,50 @@ func replace(new slog.Value, keys ...string) func([]string, slog.Attr) slog.Attr
}
}

func TestReplaceAttr(t *testing.T) {
tests := [][]any{
{},
{"key", "val"},
{"key", "val", slog.Group("group", "key2", "val2")},
{"key", "val", slog.Group("group", "key2", "val2", slog.Group("group2", "key3", "val3"))},
}

type replaceAttrParams struct {
Groups []string
Attr slog.Attr
}

replaceAttrRecorder := func(record *[]replaceAttrParams) func([]string, slog.Attr) slog.Attr {
return func(groups []string, a slog.Attr) slog.Attr {
*record = append(*record, replaceAttrParams{groups, a})
return a
}
}

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
slogRecord := make([]replaceAttrParams, 0)
tintRecord := make([]replaceAttrParams, 0)

slogLogger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{
ReplaceAttr: replaceAttrRecorder(&slogRecord),
}))
tintLogger := slog.New(tint.NewHandler(io.Discard, &tint.Options{
ReplaceAttr: replaceAttrRecorder(&tintRecord),
}))

tintLogger.Log(context.TODO(), slog.LevelInfo, "", test...)
slogLogger.Log(context.TODO(), slog.LevelInfo, "", test...)

if !slices.EqualFunc(slogRecord, tintRecord, func(a, b replaceAttrParams) bool {
return slices.Equal(a.Groups, b.Groups) && a.Attr.Equal(b.Attr)
}) {
t.Fatalf("(-want +got)\n- %v\n+ %v", slogRecord, tintRecord)
}
})
}
}

// See https://github.com/golang/exp/blob/master/slog/benchmarks/benchmarks_test.go#L25
//
// Run e.g.:
Expand Down

0 comments on commit da92dd8

Please sign in to comment.