Skip to content

Commit

Permalink
test: add extension tests & mock handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lvlcn-t committed Feb 24, 2024
1 parent f93ce44 commit 50f0496
Show file tree
Hide file tree
Showing 2 changed files with 277 additions and 0 deletions.
236 changes: 236 additions & 0 deletions internal/logger/extensions_test.go
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...)
})
}
}
41 changes: 41 additions & 0 deletions internal/logger/test/handler.go
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
}

0 comments on commit 50f0496

Please sign in to comment.