-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions_test.go
150 lines (118 loc) · 3.51 KB
/
options_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package glog
import (
"context"
"github.com/stretchr/testify/assert"
"go.uber.org/zap/zapcore"
"testing"
)
type mockCore struct {
enabled bool
entries []zapcore.Entry
fields []Field
}
func (m *mockCore) Enabled(lvl Level) bool {
return m.enabled
}
func (m *mockCore) With(fields []Field) Core {
m.fields = append(m.fields, fields...)
return m
}
func (m *mockCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if m.Enabled(ent.Level) {
return ce.AddCore(ent, m)
}
return nil
}
func (m *mockCore) Write(ent zapcore.Entry, fields []Field) error {
m.entries = append(m.entries, ent)
m.fields = append(m.fields, fields...)
return nil
}
func (m *mockCore) Sync() error {
return nil
}
func (m *mockCore) reset() {
m.enabled = false
m.entries = nil
m.fields = nil
}
func TestWrapCore(t *testing.T) {
logger := &Logger{}
option := WrapCore(func(core Core) Core {
return &mockCore{}
})
option.apply(logger)
assert.NotNil(t, logger.core, "Expected core to be wrapped")
}
func TestWithName(t *testing.T) {
logger := &Logger{}
option := WithName("test_logger")
option.apply(logger)
assert.Equal(t, "test_logger", logger.name, "Expected logger name to be 'test_logger'")
}
func TestWithCaller(t *testing.T) {
logger := &Logger{}
option := WithCaller(true)
option.apply(logger)
assert.True(t, logger.addCaller, "Expected addCaller to be true")
option = WithCaller(false)
option.apply(logger)
assert.False(t, logger.addCaller, "Expected addCaller to be false")
}
func TestAddCaller(t *testing.T) {
logger := &Logger{}
option := AddCaller()
option.apply(logger)
assert.True(t, logger.addCaller, "Expected addCaller to be true from AddCaller")
}
func TestWithStack(t *testing.T) {
logger := &Logger{}
lvl := LevelInfo
option := WithStack(lvl)
option.apply(logger)
assert.Equal(t, lvl, logger.stackLevel, "Expected stackLevel to be set to InfoLevel")
}
func TestAddCallerSkip(t *testing.T) {
logger := &Logger{}
option := AddCallerSkip(2)
option.apply(logger)
assert.Equal(t, 2, logger.callerSkip, "Expected callerSkip to be incremented by 2")
}
func TestWithCallerSkip(t *testing.T) {
logger := &Logger{}
option := WithCallerSkip(3)
option.apply(logger)
assert.Equal(t, 3, logger.callerSkip, "Expected callerSkip to be set to 3")
}
func TestWithFormatEnabled(t *testing.T) {
logger := &Logger{}
option := WithFormatEnabled()
option.apply(logger)
assert.True(t, logger.formatEnabled, "Expected formatEnabled to be true")
}
func TestWithContextHandlers(t *testing.T) {
logger := &Logger{}
handler := func(ctx context.Context, record *Record) {}
option := WithContextHandlers(handler)
option.apply(logger)
assert.NotNil(t, logger.contextHandlers, "Expected contextHandlers to be set")
assert.Equal(t, 1, len(logger.contextHandlers), "Expected one contextHandler")
}
func TestAddContextHandlers(t *testing.T) {
logger := &Logger{}
handler1 := func(ctx context.Context, record *Record) {}
handler2 := func(ctx context.Context, record *Record) {}
option := AddContextHandlers(handler1)
option.apply(logger)
assert.Equal(t, 1, len(logger.contextHandlers), "Expected one contextHandler")
option = AddContextHandlers(handler2)
option.apply(logger)
assert.Equal(t, 2, len(logger.contextHandlers), "Expected two contextHandlers")
}
func TestWithFields(t *testing.T) {
logger := &Logger{core: &mockCore{}}
field := zapcore.Field{Key: "key", String: "value"}
option := WithFields(field)
option.apply(logger)
assert.NotNil(t, logger.core, "Expected core to be set with fields")
}