-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_test.go
210 lines (188 loc) · 5.52 KB
/
message_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package logg
import (
"bytes"
"fmt"
"strings"
"testing"
"time"
)
func TestLogg_newMessage(t *testing.T) {
tests := map[string]struct {
level level
calldepth int
flags int
format format
color bool
}{
"empty": {},
"info level": {
level: Info,
},
"flags": {
flags: LstdFlags,
},
"calldepth": {
calldepth: 4,
},
"format": {
format: Json,
},
"color": {
color: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
m := newMessage(tc.level, tc.calldepth, tc.flags, tc.format, tc.color)
if m == nil {
t.Error("message object is nil")
t.FailNow()
}
if len(m.buf) != 0 {
t.Error("message buffer must be empty")
}
if m.level != tc.level {
t.Errorf("message lvl must be equal to %d, received: %d", tc.level, m.level)
}
if m.calldepth != tc.calldepth {
t.Errorf("message calldepth must be equal to %d, received: %d", tc.calldepth, m.calldepth)
}
if m.flags != tc.flags {
t.Errorf("message flags must be equal to %d, received: %d", tc.flags, m.flags)
}
if m.format != tc.format {
t.Errorf("message format must be equal to %d, received: %d", tc.format, m.format)
}
if m.color != tc.color {
t.Errorf("message color must be equal to %v, received: %v", tc.color, m.color)
}
})
}
}
func TestLogg_message_put(t *testing.T) {
m := &message{
buf: make([]byte, 1<<16+1),
}
m.put()
mFromPool := messagePool.Get().(*message)
if len(mFromPool.buf) >= 1<<16 {
t.Error("message has a buf size maxSize+1, cannot be saved in sync.Pool")
}
}
func TestLogg_message_build(t *testing.T) {
tests := map[string]struct {
data []byte
level level
calldepth int
flags int
color bool
pretty []byte
json []byte
}{
"empty": {
level: Empty,
},
"string": {
data: []byte("test"),
level: Empty,
pretty: []byte("test"),
json: []byte(`{"message": "test"}`),
},
"time": {
data: []byte("test"),
level: Empty,
flags: LstdFlags,
pretty: []byte(fmt.Sprintf("%s test", time.Now().Format("2006-01-02 15:04:05"))),
json: []byte(fmt.Sprintf(`{"time": "%s", "message": "test"}`, time.Now().Format(time.RFC3339))),
},
"caller": {
data: []byte("test"),
level: Empty,
flags: Lshortfile,
calldepth: 3,
pretty: []byte("$1:$2 test"),
json: []byte(`{"file": "$1", "line": "$2", "message": "test"}`),
},
"level": {
data: []byte("test"),
level: Info,
pretty: []byte("INF test"),
json: []byte(`{"level": "INF", "message": "test"}`),
},
"color": {
data: []byte("test"),
level: Error,
color: true,
pretty: []byte(fmt.Sprintf("%s%s[1mERR%s%s test", generate(Red), escape, escapeClose, escapeClose)),
json: []byte(`{"level": "ERR", "message": "test"}`),
},
"time + level + message": {
data: []byte("test"),
level: Warning,
flags: LstdFlags,
pretty: []byte(fmt.Sprintf("%s WRN test", time.Now().Format("2006-01-02 15:04:05"))),
json: []byte(fmt.Sprintf(`{"time": "%s", "level": "WRN", "message": "test"}`, time.Now().Format(time.RFC3339))),
},
"time + caller + level + message": {
data: []byte("test"),
level: Warning,
flags: LstdFlags | Lshortfile,
calldepth: 3,
pretty: []byte(fmt.Sprintf("%s $1:$2 WRN test", time.Now().Format("2006-01-02 15:04:05"))),
json: []byte(fmt.Sprintf(`{"time": "%s", "file": "$1", "line": "$2", "level": "WRN", "message": "test"}`, time.Now().Format(time.RFC3339))),
},
"color + time + level + message": {
data: []byte("test"),
level: Warning,
color: true,
flags: LstdFlags,
pretty: []byte(fmt.Sprintf("%s %s%s[1mWRN%s%s test",
fmt.Sprintf("%s%v%s", timeColor, time.Now().Format("2006-01-02 15:04:05"), escapeClose),
generate(HiGreen),
escape,
escapeClose,
escapeClose,
)),
json: []byte(fmt.Sprintf(`{"time": "%s", "level": "WRN", "message": "test"}`, time.Now().Format(time.RFC3339))),
},
"color + caller + time + level + message": {
data: []byte("test"),
level: Warning,
color: true,
flags: LstdFlags | Lshortfile,
calldepth: 3,
pretty: []byte(fmt.Sprintf("%s $1:$2 %s%s[1mWRN%s%s test",
fmt.Sprintf("%s%v%s", timeColor, time.Now().Format("2006-01-02 15:04:05"), escapeClose),
generate(HiGreen),
escape,
escapeClose,
escapeClose,
)),
json: []byte(fmt.Sprintf(`{"time": "%s", "file": "$1", "line": "$2", "level": "WRN", "message": "test"}`, time.Now().Format(time.RFC3339))),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
m := newMessage(tc.level, tc.calldepth, tc.flags, Pretty, tc.color)
m.build(tc.data)
if tc.flags&(Lshortfile|Llongfile) != 0 {
file, line := caller(1, tc.flags&Lshortfile != 0)
tc.pretty = []byte(strings.Replace(string(tc.pretty), "$1", file, 1))
tc.pretty = []byte(strings.Replace(string(tc.pretty), "$2", fmt.Sprint(line-3), 1))
}
if !bytes.Equal(m.buf, tc.pretty) {
t.Errorf("wrong pretty output. Expected: %s, received: %s", string(tc.pretty), string(m.buf))
}
m = newMessage(tc.level, tc.calldepth, tc.flags, Json, tc.color)
m.build(tc.data)
if tc.flags&(Lshortfile|Llongfile) != 0 {
file, line := caller(1, tc.flags&Lshortfile != 0)
tc.json = []byte(strings.Replace(string(tc.json), "$1", file, 1))
tc.json = []byte(strings.Replace(string(tc.json), "$2", fmt.Sprint(line-3), 1))
}
if !bytes.Equal(m.buf, tc.json) {
t.Errorf("wrong json output. Expected: %s, received: %s", string(tc.json), string(m.buf))
}
})
}
}