aboutsummaryrefslogtreecommitdiffstats
path: root/logging/test/handler_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'logging/test/handler_test.go')
-rw-r--r--logging/test/handler_test.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/logging/test/handler_test.go b/logging/test/handler_test.go
new file mode 100644
index 0000000..5e9430a
--- /dev/null
+++ b/logging/test/handler_test.go
@@ -0,0 +1,66 @@
+package test_test
+
+import (
+ "fmt"
+ "log/slog"
+ "runtime"
+ "testing"
+ "testing/slogtest"
+
+ "go.sudomsg.com/kit/logging/test"
+)
+
+func TestMockHandlerCompliance(t *testing.T) {
+ var lastMock *test.MockHandler
+
+ slogtest.Run(t,
+ func(t *testing.T) slog.Handler {
+ lastMock = test.NewMockLogHandler(t)
+ return lastMock
+ },
+ func(t *testing.T) map[string]any {
+ t.Helper()
+
+ recs := lastMock.Records()
+ if len(recs) == 0 {
+ t.Fatalf("no records captured")
+ }
+
+ rec := recs[len(recs)-1]
+
+ // Convert slog.Record to map[string]any
+ m := make(map[string]any)
+ if !rec.Time.IsZero() {
+ m[slog.TimeKey] = rec.Time
+ }
+ if rec.PC != 0 {
+ fs := runtime.CallersFrames([]uintptr{rec.PC})
+ f, _ := fs.Next()
+ m[slog.SourceKey] = fmt.Sprintf("%s:%d", f.File, f.Line)
+ }
+ m[slog.LevelKey] = rec.Level
+ m[slog.MessageKey] = rec.Message
+
+ rec.Attrs(func(attr slog.Attr) bool {
+ insertAttr(t, m, attr)
+ return true
+ })
+
+ return m
+ },
+ )
+}
+
+func insertAttr(t testing.TB, m map[string]any, attr slog.Attr) {
+ t.Helper()
+
+ if attr.Value.Kind() == slog.KindGroup {
+ groupMap := make(map[string]any)
+ for _, a := range attr.Value.Group() {
+ insertAttr(t, groupMap, a)
+ }
+ m[attr.Key] = groupMap
+ } else {
+ m[attr.Key] = attr.Value.Any()
+ }
+}