Skip to content

Commit

Permalink
Minor cleanups in slogr_test (readability)
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin committed Dec 8, 2023
1 parent c589653 commit cdb930d
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions slogr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func TestWithCallDepth(t *testing.T) {
var buffer bytes.Buffer
logger := logr.FromSlogHandler(slog.NewTextHandler(&buffer, &debugWithCaller))

logHelper := func(logger logr.Logger) {
logger.WithCallDepth(1).Info("hello")
}

logHelper(logger)
_, file, line, _ := runtime.Caller(0)
expectedSource := fmt.Sprintf("%s:%d", path.Base(file), line-1)
Expand All @@ -102,10 +106,6 @@ func TestWithCallDepth(t *testing.T) {
}
}

func logHelper(logger logr.Logger) {
logger.WithCallDepth(1).Info("hello")
}

func TestJSONHandler(t *testing.T) {
testSlog(t, func(buffer *bytes.Buffer) logr.Logger {
handler := slog.NewJSONHandler(buffer, nil)
Expand Down Expand Up @@ -141,10 +141,11 @@ func (s testSlogSink) WithGroup(name string) logr.SlogSink {
}

func TestFuncrHandler(t *testing.T) {
testSlog(t, func(buffer *bytes.Buffer) logr.Logger {
logger := funcr.NewJSON(func(obj string) {
fn := func(buffer *bytes.Buffer) logr.Logger {
printfn := func(obj string) {
fmt.Fprintln(buffer, obj)
}, funcr.Options{
}
opts := funcr.Options{
LogTimestamp: true,
Verbosity: 10,
RenderBuiltinsHook: func(kvList []any) []any {
Expand All @@ -161,17 +162,19 @@ func TestFuncrHandler(t *testing.T) {
}
return mappedKVList
},
})
return logger
},
}
return funcr.NewJSON(printfn, opts)
}
exceptions := []string{
"a Handler should ignore a zero Record.Time", // Time is generated by sink.
"a Handler should handle Group attributes", // funcr doesn't.
"a Handler should inline the Attrs of a group with an empty key", // funcr doesn't know about groups.
"a Handler should not output groups for an empty Record", // Relies on WithGroup. Text may change, see https://go.dev/cl/516155
"a Handler should handle the WithGroup method", // logHandler does by prefixing keys, which is not what the test expects.
"a Handler should handle multiple WithGroup and WithAttr calls", // Same.
"a Handler should call Resolve on attribute values in groups", // funcr doesn't do that and slogHandler can't do it for it.
)
}
testSlog(t, fn, exceptions...)
}

func testSlog(t *testing.T, createLogger func(buffer *bytes.Buffer) logr.Logger, exceptions ...string) {
Expand Down

0 comments on commit cdb930d

Please sign in to comment.