From 7805e61e58ff00485c2cd9538aecc6ce9b372742 Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Mon, 9 Sep 2024 19:33:18 +0200 Subject: [PATCH] tests: adding tests for FormatByKey and FormatByKind --- formatter_test.go | 96 +++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 15 ++++++-- go.sum | 23 ++++++++++-- 3 files changed, 126 insertions(+), 8 deletions(-) create mode 100644 formatter_test.go diff --git a/formatter_test.go b/formatter_test.go new file mode 100644 index 0000000..7fa668c --- /dev/null +++ b/formatter_test.go @@ -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)) +} diff --git a/go.mod b/go.mod index 76776a6..897fdb3 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 7599c92..178491f 100644 --- a/go.sum +++ b/go.sum @@ -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=