Skip to content

Commit

Permalink
Clean up slog testing and restore coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin committed Dec 6, 2023
1 parent a875290 commit 0141228
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 334 deletions.
23 changes: 0 additions & 23 deletions logr_noslog_test.go

This file was deleted.

218 changes: 0 additions & 218 deletions logr_slog_test.go

This file was deleted.

79 changes: 0 additions & 79 deletions logr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,85 +24,6 @@ import (
"testing"
)

// testLogSink is a Logger just for testing that calls optional hooks on each method.
type testLogSink struct {
fnInit func(ri RuntimeInfo)
fnEnabled func(lvl int) bool
fnInfo func(lvl int, msg string, kv ...any)
fnError func(err error, msg string, kv ...any)
fnWithValues func(kv ...any)
fnWithName func(name string)

withValues []any

// testSlogSink contains some additional fields if (and only if) slog is supported by Go.
// See logr_slog_test.go.
//nolint:unused // Only unused with Go < 1.21.
testSlogSink
}

var _ LogSink = &testLogSink{}

func (l *testLogSink) Init(ri RuntimeInfo) {
if l.fnInit != nil {
l.fnInit(ri)
}
}

func (l *testLogSink) Enabled(lvl int) bool {
if l.fnEnabled != nil {
return l.fnEnabled(lvl)
}
return false
}

func (l *testLogSink) Info(lvl int, msg string, kv ...any) {
if l.fnInfo != nil {
l.fnInfo(lvl, msg, kv...)
}
}

func (l *testLogSink) Error(err error, msg string, kv ...any) {
if l.fnError != nil {
l.fnError(err, msg, kv...)
}
}

func (l *testLogSink) WithValues(kv ...any) LogSink {
if l.fnWithValues != nil {
l.fnWithValues(kv...)
}
out := *l
n := len(out.withValues)
out.withValues = append(out.withValues[:n:n], kv...)
return &out
}

func (l *testLogSink) WithName(name string) LogSink {
if l.fnWithName != nil {
l.fnWithName(name)
}
out := *l
return &out
}

type testCallDepthLogSink struct {
testLogSink
callDepth int
fnWithCallDepth func(depth int)
}

var _ CallDepthLogSink = &testCallDepthLogSink{}

func (l *testCallDepthLogSink) WithCallDepth(depth int) LogSink {
if l.fnWithCallDepth != nil {
l.fnWithCallDepth(depth)
}
out := *l
out.callDepth += depth
return &out
}

func TestNew(t *testing.T) {
calledInit := 0

Expand Down
Loading

0 comments on commit 0141228

Please sign in to comment.