-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add extension tests & mock handler
- Loading branch information
Showing
2 changed files
with
277 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
package logger | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"testing" | ||
|
||
"github.com/lvlcn-t/loggerhead/internal/logger/test" | ||
) | ||
|
||
type logFunc func(l Logger, msg string, args ...any) | ||
|
||
func TestLogger_LevelExtensions(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
logFunc logFunc | ||
handler test.MockHandler | ||
}{ | ||
{ | ||
name: "debug level disabled", | ||
logFunc: func(l Logger, msg string, args ...any) { | ||
l.Debugf(msg, args...) | ||
}, | ||
handler: test.MockHandler{ | ||
EnabledFunc: func(ctx context.Context, level slog.Level) bool { | ||
return false | ||
}, | ||
HandleFunc: func(ctx context.Context, r slog.Record) error { | ||
t.Error("Handle should not be called") | ||
return nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "debug level enabled", | ||
logFunc: func(l Logger, msg string, args ...any) { | ||
l.Debugf(msg, args...) | ||
}, | ||
handler: test.MockHandler{ | ||
HandleFunc: func(ctx context.Context, r slog.Record) error { | ||
level := LevelDebug | ||
if r.Level != level { | ||
t.Errorf("Expected level to be [%s], got [%s]", getLevelString(level), r.Level) | ||
} | ||
if r.NumAttrs() != 0 { | ||
t.Errorf("Expected 0 attributes, got %d", r.NumAttrs()) | ||
} | ||
return nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "info level disabled", | ||
logFunc: func(l Logger, msg string, args ...any) { | ||
l.Infof(msg, args...) | ||
}, | ||
handler: test.MockHandler{ | ||
EnabledFunc: func(ctx context.Context, level slog.Level) bool { | ||
return false | ||
}, | ||
HandleFunc: func(ctx context.Context, r slog.Record) error { | ||
t.Error("Handle should not be called") | ||
return nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "info level enabled", | ||
logFunc: func(l Logger, msg string, args ...any) { | ||
l.Infof(msg, args...) | ||
}, | ||
handler: test.MockHandler{ | ||
HandleFunc: func(ctx context.Context, r slog.Record) error { | ||
level := LevelInfo | ||
if r.Level != level { | ||
t.Errorf("Expected level to be [%s], got [%s]", getLevelString(level), r.Level) | ||
} | ||
if r.NumAttrs() != 0 { | ||
t.Errorf("Expected 0 attributes, got %d", r.NumAttrs()) | ||
} | ||
return nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "warn level disabled", | ||
logFunc: func(l Logger, msg string, args ...any) { | ||
l.Warnf(msg, args...) | ||
}, | ||
handler: test.MockHandler{ | ||
EnabledFunc: func(ctx context.Context, level slog.Level) bool { | ||
return false | ||
}, | ||
HandleFunc: func(ctx context.Context, r slog.Record) error { | ||
t.Error("Handle should not be called") | ||
return nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "warn level enabled", | ||
logFunc: func(l Logger, msg string, args ...any) { | ||
l.Warnf(msg, args...) | ||
}, | ||
handler: test.MockHandler{ | ||
HandleFunc: func(ctx context.Context, r slog.Record) error { | ||
level := LevelWarn | ||
if r.Level != level { | ||
t.Errorf("Expected level to be [%s], got [%s]", getLevelString(level), r.Level) | ||
} | ||
if r.NumAttrs() != 0 { | ||
t.Errorf("Expected 0 attributes, got %d", r.NumAttrs()) | ||
} | ||
return nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "error level disabled", | ||
logFunc: func(l Logger, msg string, args ...any) { | ||
l.Errorf(msg, args...) | ||
}, | ||
handler: test.MockHandler{ | ||
EnabledFunc: func(ctx context.Context, level slog.Level) bool { | ||
return false | ||
}, | ||
HandleFunc: func(ctx context.Context, r slog.Record) error { | ||
t.Error("Handle should not be called") | ||
return nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "error level enabled", | ||
logFunc: func(l Logger, msg string, args ...any) { | ||
l.Errorf(msg, args...) | ||
}, | ||
handler: test.MockHandler{ | ||
HandleFunc: func(ctx context.Context, r slog.Record) error { | ||
level := LevelError | ||
if r.Level != level { | ||
t.Errorf("Expected level to be [%s], got [%s]", getLevelString(level), r.Level) | ||
} | ||
if r.NumAttrs() != 0 { | ||
t.Errorf("Expected 0 attributes, got %d", r.NumAttrs()) | ||
} | ||
return nil | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l := NewLogger(tt.handler) | ||
tt.logFunc(l, "test") | ||
}) | ||
} | ||
} | ||
|
||
func TestLogger_PanicLevel(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
attrs []any | ||
logFunc logFunc | ||
handler test.MockHandler | ||
}{ | ||
{ | ||
name: "panic level", | ||
attrs: []any{"key", "value"}, | ||
logFunc: func(l Logger, msg string, args ...any) { | ||
l.Panic(msg, args...) | ||
}, | ||
handler: test.MockHandler{ | ||
HandleFunc: func(ctx context.Context, r slog.Record) error { | ||
level := LevelPanic | ||
if r.Level != level { | ||
t.Errorf("Expected level to be [%s], got [%s]", getLevelString(level), r.Level) | ||
} | ||
if r.NumAttrs() == 0 { | ||
t.Errorf("Expected attributes, got %d", r.NumAttrs()) | ||
} | ||
return nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "panicf level", | ||
logFunc: func(l Logger, msg string, args ...any) { | ||
l.Panicf(msg, args...) | ||
}, | ||
handler: test.MockHandler{ | ||
HandleFunc: func(ctx context.Context, r slog.Record) error { | ||
level := LevelPanic | ||
if r.Level != level { | ||
t.Errorf("Expected level to be [%s], got [%s]", getLevelString(level), r.Level) | ||
} | ||
if r.NumAttrs() != 0 { | ||
t.Errorf("Expected 0 attributes, got %d", r.NumAttrs()) | ||
} | ||
return nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "panic context level", | ||
attrs: []any{"key", "value"}, | ||
logFunc: func(l Logger, msg string, args ...any) { | ||
l.PanicContext(context.Background(), msg, args...) | ||
}, | ||
handler: test.MockHandler{ | ||
HandleFunc: func(ctx context.Context, r slog.Record) error { | ||
level := LevelPanic | ||
if r.Level != level { | ||
t.Errorf("Expected level to be [%s], got [%s]", getLevelString(level), r.Level) | ||
} | ||
if r.NumAttrs() == 0 { | ||
t.Errorf("Expected attributes, got %d", r.NumAttrs()) | ||
} | ||
return nil | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l := NewLogger(tt.handler) | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Error("Expected panic") | ||
} | ||
}() | ||
tt.logFunc(l, "test", tt.attrs...) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
) | ||
|
||
type MockHandler struct { | ||
EnabledFunc func(ctx context.Context, level slog.Level) bool | ||
HandleFunc func(ctx context.Context, r slog.Record) error | ||
WithAttrsFunc func(attrs []slog.Attr) slog.Handler | ||
WithGroupFunc func(name string) slog.Handler | ||
} | ||
|
||
func (m MockHandler) Enabled(ctx context.Context, level slog.Level) bool { | ||
if m.EnabledFunc != nil { | ||
return m.EnabledFunc(ctx, level) | ||
} | ||
return true | ||
} | ||
|
||
func (m MockHandler) Handle(ctx context.Context, r slog.Record) error { | ||
if m.HandleFunc != nil { | ||
return m.HandleFunc(ctx, r) | ||
} | ||
return nil | ||
} | ||
|
||
func (m MockHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
if m.WithAttrsFunc != nil { | ||
return m.WithAttrsFunc(attrs) | ||
} | ||
return m | ||
} | ||
|
||
func (m MockHandler) WithGroup(name string) slog.Handler { | ||
if m.WithGroupFunc != nil { | ||
return m.WithGroupFunc(name) | ||
} | ||
return m | ||
} |