summaryrefslogtreecommitdiffstats
path: root/logging/test/handler_test.go
diff options
context:
space:
mode:
authorMarc Pervaz Boocha <mboocha@sudomsg.com>2025-08-07 22:54:01 +0530
committerMarc Pervaz Boocha <mboocha@sudomsg.com>2025-08-07 22:54:01 +0530
commit8ca9c43cfb814cc30e73fe324795171fe7193371 (patch)
tree3e3aa1f7f63fc4980763a5dc4474f8aeb71dbe36 /logging/test/handler_test.go
parentAdded File Mode to sockets and socket activation (diff)
downloadkit-8ca9c43cfb814cc30e73fe324795171fe7193371.tar
kit-8ca9c43cfb814cc30e73fe324795171fe7193371.tar.gz
kit-8ca9c43cfb814cc30e73fe324795171fe7193371.tar.bz2
kit-8ca9c43cfb814cc30e73fe324795171fe7193371.tar.lz
kit-8ca9c43cfb814cc30e73fe324795171fe7193371.tar.xz
kit-8ca9c43cfb814cc30e73fe324795171fe7193371.tar.zst
kit-8ca9c43cfb814cc30e73fe324795171fe7193371.zip
Fixed Complience of slog test handler
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()
+ }
+}