-
Notifications
You must be signed in to change notification settings - Fork 0
/
zap_test.go
151 lines (133 loc) · 3.68 KB
/
zap_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
151
package logger
import (
"bytes"
"fmt"
"testing"
)
// TestNewZap tests the NewZap function.
func TestNewZap(t *testing.T) {
buffer := new(bytes.Buffer)
config := Config{
Level: InfoLevel,
Output: buffer,
ExitFunc: func(int) {},
}
zapLogger := NewZap(config)
if zapLogger.Config.Level != InfoLevel {
t.Errorf("Expected log level %v, got %v", InfoLevel, zapLogger.Config.Level)
}
if zapLogger.Config.ExitFunc == nil {
t.Errorf("Expected ExitFunc to be set, but it is nil")
}
// Use a buffer to test log output
zapLogger.Info("Info message", Fields{"key": "value"})
expected := "Info message"
if !bytes.Contains(buffer.Bytes(), []byte(expected)) {
t.Errorf("Expected %s to contain %s", buffer.String(), expected)
}
}
// TestZap_Debug tests the Debug method.
func TestZap_Debug(t *testing.T) {
buffer := new(bytes.Buffer)
config := Config{
Level: DebugLevel,
Output: buffer,
ExitFunc: func(int) {},
}
zapLogger := NewZap(config)
zapLogger.Debug("Debug message", Fields{"key": "value"})
expected := "Debug message"
if !bytes.Contains(buffer.Bytes(), []byte(expected)) {
t.Errorf("Expected %s to contain %s", buffer.String(), expected)
}
}
// TestZap_Info tests the Info method.
func TestZap_Info(t *testing.T) {
buffer := new(bytes.Buffer)
config := Config{
Level: InfoLevel,
Output: buffer,
ExitFunc: func(int) {},
}
zapLogger := NewZap(config)
zapLogger.Info("Info message", Fields{"key": "value"})
expected := "Info message"
if !bytes.Contains(buffer.Bytes(), []byte(expected)) {
t.Errorf("Expected %s to contain %s", buffer.String(), expected)
}
}
// TestZap_Warn tests the Warn method.
func TestZap_Warn(t *testing.T) {
buffer := new(bytes.Buffer)
config := Config{
Level: WarnLevel,
Output: buffer,
ExitFunc: func(int) {},
}
zapLogger := NewZap(config)
zapLogger.Warn("Warn message", Fields{"key": "value"})
expected := "Warn message"
if !bytes.Contains(buffer.Bytes(), []byte(expected)) {
t.Errorf("Expected %s to contain %s", buffer.String(), expected)
}
}
// TestZap_Error tests the Error method.
func TestZap_Error(t *testing.T) {
buffer := new(bytes.Buffer)
config := Config{
Level: ErrorLevel,
Output: buffer,
ExitFunc: func(int) {},
}
zapLogger := NewZap(config)
zapLogger.Error("Error message", Fields{"key": "value"})
expected := "Error message"
if !bytes.Contains(buffer.Bytes(), []byte(expected)) {
t.Errorf("Expected %s to contain %s", buffer.String(), expected)
}
}
// TestShouldLog tests the shouldLog function.
func TestShouldLog(t *testing.T) {
tests := []struct {
configLevel Level
logLevel Level
expected bool
}{
{DebugLevel, DebugLevel, true},
{DebugLevel, InfoLevel, true},
{DebugLevel, WarnLevel, true},
{DebugLevel, ErrorLevel, true},
{DebugLevel, FatalLevel, true},
{InfoLevel, DebugLevel, false},
{InfoLevel, InfoLevel, true},
{InfoLevel, WarnLevel, true},
{InfoLevel, ErrorLevel, true},
{InfoLevel, FatalLevel, true},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%v-%v", test.configLevel, test.logLevel), func(t *testing.T) {
config := Config{
Level: test.configLevel,
}
zapLogger := NewZap(config)
actual := zapLogger.shouldLog(test.logLevel)
if actual != test.expected {
t.Errorf("shouldLog(%v) = %v, expected %v", test.logLevel, actual, test.expected)
}
})
}
}
// TestMapToZapFields tests the mapToZapFields function.
func TestMapToZapFields(t *testing.T) {
fields := Fields{
"string": "value",
"int": 42,
"int64": int64(64),
"float64": 3.14,
"bool": true,
}
zapFields := mapToZapFields(fields)
if len(zapFields) != len(fields) {
t.Errorf("Expected %d fields, got %d", len(fields), len(zapFields))
}
}