-
Notifications
You must be signed in to change notification settings - Fork 0
/
slog_test.go
59 lines (52 loc) · 1.31 KB
/
slog_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package wglog
import (
"context"
"github.com/stretchr/testify/require"
"log/slog"
"testing"
)
func TestSlog(t *testing.T) {
t.Run("default", func(t *testing.T) {
def := slog.Default()
defer slog.SetDefault(def)
handler := &testSlogHandler{t: t}
slog.SetDefault(slog.New(handler))
l := Slog(nil)
l.Verbosef("")
l.Errorf("")
handler.Check()
})
t.Run("", func(t *testing.T) {
handler := &testSlogHandler{t: t}
l := Slog(slog.New(handler))
l.Verbosef("")
l.Errorf("")
handler.Check()
})
}
type testSlogHandler struct {
t *testing.T
debug, error bool
invalid *slog.Level
}
func (t *testSlogHandler) Check() {
if t.invalid != nil {
require.NotNil(t.t, t.invalid, "level(%s)", t.invalid.String())
}
require.True(t.t, t.debug, "debug was not called")
require.True(t.t, t.error, "error was not called")
}
func (t *testSlogHandler) Handle(_ context.Context, record slog.Record) error {
switch record.Level {
case slog.LevelDebug:
t.debug = true
case slog.LevelError:
t.error = true
default:
t.invalid = &record.Level
}
return nil
}
func (t *testSlogHandler) Enabled(context.Context, slog.Level) bool { return true }
func (t *testSlogHandler) WithAttrs([]slog.Attr) slog.Handler { return t }
func (t *testSlogHandler) WithGroup(string) slog.Handler { return t }