-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
241 lines (222 loc) · 4.9 KB
/
helpers_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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package logg
import (
"bytes"
"runtime"
"strings"
"testing"
"time"
)
func Test_caller(t *testing.T) {
file, line := caller(60, false)
if file != "???" {
t.Errorf("caller path must be undefined. Received: %s", file)
}
if line != 0 {
t.Errorf("caller line must be 0. Received: %d", line)
}
file, line = caller(1, false)
_, f, l, _ := runtime.Caller(0)
if file != f {
t.Errorf("wrong caller path. Expected: %s, received: %s", f, file)
}
if line != l-1 {
t.Errorf("wrong caller line. Expected: %d, received: %d", l-1, line)
}
file, line = caller(1, true)
_, f, l, _ = runtime.Caller(0)
if !strings.Contains(f, file) {
t.Errorf("wrong caller path. Expected: %s, received: %s", f, file)
}
if line != l-1 {
t.Errorf("wrong caller line. Expected: %d, received: %d", l-1, line)
}
}
func Test_appendTimestamp(t *testing.T) {
now := time.Now()
tests := map[string]struct {
t time.Time
flags int
format format
buf []byte
}{
"empty": {
t: time.Time{},
flags: 0,
buf: []byte("0000-00-00 00:00:00"),
},
"default format": {
t: now,
flags: LstdFlags,
buf: []byte(now.Format("2006-01-02 15:04:05")),
},
"date only": {
t: now,
flags: Ldate,
buf: []byte(now.Format("2006-01-02")),
},
"time only": {
t: now,
flags: Ltime,
buf: []byte(now.Format("15:04:05")),
},
"milliseconds only": {
t: now,
flags: Lmicroseconds,
buf: []byte(now.Add(time.Nanosecond * 500).Format(".999999")),
},
"time with milliseconds": {
t: now,
flags: Ltime | Lmicroseconds,
buf: []byte(now.Add(time.Nanosecond * 500).Format("15:04:05.999999")),
},
"date with time with milliseconds": {
t: now,
flags: Ldate | Ltime | Lmicroseconds,
buf: []byte(now.Add(time.Nanosecond * 500).Format("2006-01-02 15:04:05.999999")),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
buf := []byte{}
buf = appendTimestamp(tc.t, tc.format, tc.flags, buf)
if !bytes.Equal(buf, tc.buf) {
t.Errorf("wrong timestamp. Expected: %s, received: %s", string(tc.buf), string(buf))
}
})
}
}
func Benchmark_appendTimestamp(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
now := time.Now()
buf := []byte{}
for n := 0; n < b.N; n++ {
appendTimestamp(now, Pretty, LstdFlags, buf)
}
}
func Test_defineLevel(t *testing.T) {
tests := map[string]struct {
data []byte
result []byte
level level
}{
"empty": {
data: []byte{},
result: []byte{},
level: Empty,
},
"empty with string": {
data: []byte("test"),
result: []byte("test"),
level: Empty,
},
"short": {
data: []byte("INF test"),
result: []byte("test"),
level: Info,
},
"short with brackets": {
data: []byte("[ERR] test"),
result: []byte("test"),
level: Error,
},
"double short": {
data: []byte("WRN ERR test"),
result: []byte("ERR test"),
level: Warning,
},
"long": {
data: []byte("DEBUG test"),
result: []byte("test"),
level: Debug,
},
"long with brackets": {
data: []byte("[PANIC] test"),
result: []byte("test"),
level: Panic,
},
"double long": {
data: []byte("PANIC DEBUG test"),
result: []byte("DEBUG test"),
level: Panic,
},
"debug short": {
data: []byte("DBG test"),
result: []byte("test"),
level: Debug,
},
"debug long": {
data: []byte("DEBUG test"),
result: []byte("test"),
level: Debug,
},
"info short": {
data: []byte("INF test"),
result: []byte("test"),
level: Info,
},
"info long": {
data: []byte("INFO test"),
result: []byte("test"),
level: Info,
},
"error short": {
data: []byte("ERR test"),
result: []byte("test"),
level: Error,
},
"error long": {
data: []byte("ERROR test"),
result: []byte("test"),
level: Error,
},
"warning short": {
data: []byte("WRN test"),
result: []byte("test"),
level: Warning,
},
"warning long": {
data: []byte("WARN test"),
result: []byte("test"),
level: Warning,
},
"panic short": {
data: []byte("PNC test"),
result: []byte("test"),
level: Panic,
},
"panic long": {
data: []byte("PANIC test"),
result: []byte("test"),
level: Panic,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
lvl := defineLevel(&tc.data)
tc.data = removeLevel(tc.data, lvl)
if lvl != tc.level {
t.Errorf("wrong level. Expected: %d, received: %d", tc.level, lvl)
}
if !bytes.Equal(tc.data, tc.result) {
t.Errorf("level not removed from data. Expected: %s, received: %s", string(tc.result), string(tc.data))
}
})
}
}
func Benchmark_defineLevel(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
t := []byte("INF test")
_ = defineLevel(&t)
}
}
func Benchmark_removeLevel(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
t := []byte("INFO test")
t = removeLevel(t, Info)
}
}