-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig_test.go
114 lines (91 loc) · 3.01 KB
/
config_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logit
import (
"fmt"
"io"
"log/slog"
"os"
"testing"
"github.com/FishGoddess/logit/handler"
)
type testConfigHandler struct {
slog.TextHandler
w io.Writer
opts slog.HandlerOptions
}
// go test -v -cover -count=1 -test.cpu=1 -run=^TestConfigNewHandlerOptions$
func TestConfigNewHandlerOptions(t *testing.T) {
replaceAttr := func(groups []string, attr slog.Attr) slog.Attr { return attr }
conf := &config{
level: slog.LevelWarn,
withSource: true,
replaceAttr: replaceAttr,
}
opts := conf.newHandlerOptions()
if opts.Level != conf.level {
t.Fatalf("opts.Level %v != conf.level %v", opts.Level, conf.level)
}
if opts.AddSource != conf.withSource {
t.Fatalf("opts.AddSource %v != conf.withSource %v", opts.AddSource, conf.withSource)
}
if fmt.Sprintf("%p", opts.ReplaceAttr) != fmt.Sprintf("%p", conf.replaceAttr) {
t.Fatalf("opts.ReplaceAttr %p != conf.replaceAttr %p", opts.ReplaceAttr, conf.replaceAttr)
}
}
// go test -v -cover -count=1 -test.cpu=1 -run=^TestConfigNewHandler$
func TestConfigNewHandler(t *testing.T) {
handlerName := t.Name()
handler.Register(handlerName, func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
return &testConfigHandler{
w: w,
opts: *opts,
}
})
newWriter := func() (io.Writer, error) { return os.Stderr, nil }
replaceAttr := func(groups []string, attr slog.Attr) slog.Attr { return attr }
conf := &config{
level: slog.LevelWarn,
handler: handlerName,
newWriter: newWriter,
replaceAttr: replaceAttr,
withSource: true,
}
handler, syncer, closer, err := conf.newHandler()
if err != nil {
t.Fatal(err)
}
if syncer == nil {
t.Fatal("syncer is nil")
}
if closer == nil {
t.Fatal("closer is nil")
}
tcHandler, ok := handler.(*testConfigHandler)
if !ok {
t.Fatalf("handler type %T is wrong", handler)
}
if tcHandler.w != os.Stderr {
t.Fatalf("tcHandler.w %p != os.Stderr %p", tcHandler.w, os.Stderr)
}
if tcHandler.opts.Level != conf.level {
t.Fatalf("tcHandler.opts.Level %v != conf.level %v", tcHandler.opts.Level, conf.level)
}
if tcHandler.opts.AddSource != conf.withSource {
t.Fatalf("tcHandler.opts.AddSource %v != conf.withSource %v", tcHandler.opts.AddSource, conf.withSource)
}
if fmt.Sprintf("%p", tcHandler.opts.ReplaceAttr) != fmt.Sprintf("%p", conf.replaceAttr) {
t.Fatalf("tcHandler.opts.ReplaceAttr %p != conf.replaceAttr %p", tcHandler.opts.ReplaceAttr, conf.replaceAttr)
}
}