Skip to content

Commit

Permalink
tests: adding tests for FormatByKey and FormatByKind
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Sep 9, 2024
1 parent 8b1565c commit 7805e61
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 8 deletions.
96 changes: 96 additions & 0 deletions formatter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package slogformatter

import (
"context"
"log/slog"
"sync/atomic"
"testing"
"time"

slogmock "github.com/samber/slog-mock"
"github.com/stretchr/testify/assert"
)

func TestFormatByKind(t *testing.T) {
t.Parallel()
is := assert.New(t)

handler := NewFormatterMiddleware(
FormatByKind(slog.KindDuration, func(v slog.Value) slog.Value {
return slog.StringValue(v.Duration().String())
}),
)

var checked int32

logger := slog.New(
handler(
slogmock.Option{
Handle: func(ctx context.Context, record slog.Record) error {
is.Equal("hello world", record.Message)

attrs := map[string]slog.Value{}
record.Attrs(func(attr slog.Attr) bool {
attrs[attr.Key] = attr.Value
return true
})

is.Len(attrs, 2)
is.Equal(attrs["key"], slog.StringValue("value"))
is.Equal(attrs["duration"], slog.StringValue("1s"))

atomic.AddInt32(&checked, 1)
return nil
},
}.NewMockHandler(),
),
)

logger.Info("hello world",
slog.String("key", "value"),
slog.Duration("duration", 1*time.Second))

is.Equal(int32(1), atomic.LoadInt32(&checked))
}

func TestFormatByKey(t *testing.T) {
t.Parallel()
is := assert.New(t)

handler := NewFormatterMiddleware(
FormatByKey("duration", func(v slog.Value) slog.Value {
return slog.StringValue(v.Duration().String())
}),
)

var checked int32

logger := slog.New(
handler(
slogmock.Option{
Handle: func(ctx context.Context, record slog.Record) error {
is.Equal("hello world", record.Message)

attrs := map[string]slog.Value{}
record.Attrs(func(attr slog.Attr) bool {
attrs[attr.Key] = attr.Value
return true
})

is.Len(attrs, 2)
is.Equal(attrs["key"], slog.StringValue("value"))
is.Equal(attrs["duration"], slog.StringValue("1s"))

atomic.AddInt32(&checked, 1)
return nil
},
}.NewMockHandler(),
),
)

logger.Info("hello world",
slog.String("key", "value"),
slog.Duration("duration", 1*time.Second))

is.Equal(int32(1), atomic.LoadInt32(&checked))
}
15 changes: 11 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ module github.com/samber/slog-formatter

go 1.21

require github.com/samber/slog-multi v1.1.0

require (
github.com/samber/slog-multi v1.1.0
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
github.com/samber/lo v1.44.0
github.com/samber/slog-mock v0.1.0
github.com/stretchr/testify v1.8.0
go.uber.org/goleak v1.2.1
)

require (
github.com/samber/lo v1.38.1
go.uber.org/goleak v1.2.1
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
23 changes: 19 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/samber/lo v1.44.0 h1:5il56KxRE+GHsm1IR+sZ/6J42NODigFiqCWpSc2dybA=
github.com/samber/lo v1.44.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
github.com/samber/slog-mock v0.1.0 h1:I3PApMma8+qoxrfh/MoTmHAWTN8CZyrOKAn1FXus7zU=
github.com/samber/slog-mock v0.1.0/go.mod h1:muKl65H9go1idsAoiDnetK/8pIQY/1X760/UwGD+REI=
github.com/samber/slog-multi v1.1.0 h1:m5wfpXE8Qu2gCiR/JnhFGsLcWDOmTxnso32EMffVAY0=
github.com/samber/slog-multi v1.1.0/go.mod h1:uLAvHpGqbYgX4FSL0p1ZwoLuveIAJvBECtE07XmYvFo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 7805e61

Please sign in to comment.