1
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
2
|
|
|
// Author: Nikita Koryabkin |
3
|
|
|
// Email: [email protected] |
4
|
|
|
// Telegram: https://t.me/Apologiz |
5
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
6
|
|
|
|
7
|
|
|
package alog |
8
|
|
|
|
9
|
|
|
import ( |
10
|
|
|
"fmt" |
11
|
|
|
"io" |
12
|
|
|
"reflect" |
13
|
|
|
"testing" |
14
|
|
|
|
15
|
|
|
"github.com/mylockerteam/alog/strategy/standart" |
16
|
|
|
) |
17
|
|
|
|
18
|
|
|
type argsLogInfo struct { |
19
|
|
|
msg string |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
type testsLogInfo struct { |
23
|
|
|
name string |
24
|
|
|
fields Writer |
25
|
|
|
args argsLogInfo |
26
|
|
|
want Writer |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
func casesLogInfo() []testsLogInfo { |
30
|
|
|
info := configProvider() |
31
|
|
|
wrn := &Config{ |
32
|
|
|
Loggers: Map{ |
33
|
|
|
Wrn: loggerProvider(), |
34
|
|
|
}, |
35
|
|
|
} |
36
|
|
|
return []testsLogInfo{ |
37
|
|
|
{ |
38
|
|
|
fields: &Log{ |
39
|
|
|
config: info, |
40
|
|
|
}, |
41
|
|
|
want: &Log{ |
42
|
|
|
config: info, |
43
|
|
|
}, |
44
|
|
|
}, |
45
|
|
|
{ |
46
|
|
|
fields: &Log{ |
47
|
|
|
config: wrn, |
48
|
|
|
}, |
49
|
|
|
want: &Log{ |
50
|
|
|
config: wrn, |
51
|
|
|
}, |
52
|
|
|
}, |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
func TestLog_Info(t *testing.T) { |
57
|
|
|
tests := casesLogInfo() |
58
|
|
|
for _, tt := range tests { |
59
|
|
|
t.Run(tt.name, func(t *testing.T) { |
60
|
|
|
if got := tt.fields.Info(tt.args.msg); !reflect.DeepEqual(got, tt.want) { |
61
|
|
|
t.Errorf("Log.Info() = %v, want %v", got, tt.want) |
62
|
|
|
} |
63
|
|
|
}) |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
type argsLogInfof struct { |
68
|
|
|
format string |
69
|
|
|
p []interface{} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
type testsLogInfof struct { |
73
|
|
|
name string |
74
|
|
|
fields Writer |
75
|
|
|
args argsLogInfof |
76
|
|
|
want Writer |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
func casesLogInfof() []testsLogInfof { |
80
|
|
|
info := configProvider() |
81
|
|
|
wrn := &Config{ |
82
|
|
|
Loggers: Map{ |
83
|
|
|
Wrn: loggerProvider(), |
84
|
|
|
}, |
85
|
|
|
} |
86
|
|
|
return []testsLogInfof{ |
87
|
|
|
{ |
88
|
|
|
fields: &Log{ |
89
|
|
|
config: info, |
90
|
|
|
}, |
91
|
|
|
want: &Log{ |
92
|
|
|
config: info, |
93
|
|
|
}, |
94
|
|
|
}, |
95
|
|
|
{ |
96
|
|
|
fields: &Log{ |
97
|
|
|
config: wrn, |
98
|
|
|
}, |
99
|
|
|
want: &Log{ |
100
|
|
|
config: wrn, |
101
|
|
|
}, |
102
|
|
|
}, |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
func TestLog_Infof(t *testing.T) { |
107
|
|
|
tests := casesLogInfof() |
108
|
|
|
for _, tt := range tests { |
109
|
|
|
t.Run(tt.name, func(t *testing.T) { |
110
|
|
|
if got := tt.fields.Infof(tt.args.format, tt.args.p...); !reflect.DeepEqual(got, tt.want) { |
111
|
|
|
t.Errorf("Log.Infof() = %v, want %v", got, tt.want) |
112
|
|
|
} |
113
|
|
|
}) |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
type argsLogWarning struct { |
118
|
|
|
msg string |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
type testsLogWarning struct { |
122
|
|
|
name string |
123
|
|
|
fields Writer |
124
|
|
|
args argsLogWarning |
125
|
|
|
want Writer |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
func casesLogWarning() []testsLogWarning { |
129
|
|
|
info := configProvider() |
130
|
|
|
wrn := &Config{ |
131
|
|
|
Loggers: Map{ |
132
|
|
|
Wrn: loggerProvider(), |
133
|
|
|
}, |
134
|
|
|
} |
135
|
|
|
return []testsLogWarning{ |
136
|
|
|
{ |
137
|
|
|
fields: &Log{ |
138
|
|
|
config: info, |
139
|
|
|
}, |
140
|
|
|
want: &Log{ |
141
|
|
|
config: info, |
142
|
|
|
}, |
143
|
|
|
}, |
144
|
|
|
{ |
145
|
|
|
fields: &Log{ |
146
|
|
|
config: wrn, |
147
|
|
|
}, |
148
|
|
|
want: &Log{ |
149
|
|
|
config: wrn, |
150
|
|
|
}, |
151
|
|
|
}, |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
func TestLog_Warning(t *testing.T) { |
156
|
|
|
tests := casesLogWarning() |
157
|
|
|
for _, tt := range tests { |
158
|
|
|
t.Run(tt.name, func(t *testing.T) { |
159
|
|
|
if got := tt.fields.Warning(tt.args.msg); !reflect.DeepEqual(got, tt.want) { |
160
|
|
|
t.Errorf("Log.Warning() = %v, want %v", got, tt.want) |
161
|
|
|
} |
162
|
|
|
}) |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
type argsPrintNotConfiguredMessage struct { |
167
|
|
|
code uint |
168
|
|
|
skip int |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
type testsPrintNotConfiguredMessage struct { |
172
|
|
|
name string |
173
|
|
|
args argsPrintNotConfiguredMessage |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
func casesPrintNotConfiguredMessage() []testsPrintNotConfiguredMessage { |
177
|
|
|
return []testsPrintNotConfiguredMessage{ |
178
|
|
|
{ |
179
|
|
|
args: argsPrintNotConfiguredMessage{ |
180
|
|
|
code: Info, |
181
|
|
|
skip: 2, |
182
|
|
|
}, |
183
|
|
|
}, |
184
|
|
|
{ |
185
|
|
|
args: argsPrintNotConfiguredMessage{ |
186
|
|
|
code: Info, |
187
|
|
|
skip: 1000, |
188
|
|
|
}, |
189
|
|
|
}, |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
func Test_printNotConfiguredMessage(t *testing.T) { |
194
|
|
|
tests := casesPrintNotConfiguredMessage() |
195
|
|
|
for _, tt := range tests { |
196
|
|
|
t.Run(tt.name, func(t *testing.T) { |
197
|
|
|
printNotConfiguredMessage(tt.args.code, tt.args.skip) |
198
|
|
|
}) |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
type argsLogGetLoggerInterfaceByType struct { |
203
|
|
|
loggerType uint |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
type testsLogGetLoggerInterfaceByType struct { |
207
|
|
|
name string |
208
|
|
|
fields Writer |
209
|
|
|
args argsLogGetLoggerInterfaceByType |
210
|
|
|
want io.Writer |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
func casesLogGetLoggerInterfaceByType() []testsLogGetLoggerInterfaceByType { |
214
|
|
|
config := configProvider() |
215
|
|
|
wrn := &Config{ |
216
|
|
|
Loggers: Map{ |
217
|
|
|
Wrn: loggerProvider(), |
218
|
|
|
}, |
219
|
|
|
} |
220
|
|
|
err := &Config{ |
221
|
|
|
Loggers: Map{ |
222
|
|
|
Err: loggerProvider(), |
223
|
|
|
}, |
224
|
|
|
} |
225
|
|
|
return []testsLogGetLoggerInterfaceByType{ |
226
|
|
|
{ |
227
|
|
|
fields: &Log{ |
228
|
|
|
config: config, |
229
|
|
|
}, |
230
|
|
|
args: argsLogGetLoggerInterfaceByType{ |
231
|
|
|
loggerType: Info, |
232
|
|
|
}, |
233
|
|
|
want: config.Loggers[Info], |
234
|
|
|
}, |
235
|
|
|
{ |
236
|
|
|
fields: &Log{ |
237
|
|
|
config: wrn, |
238
|
|
|
}, |
239
|
|
|
args: argsLogGetLoggerInterfaceByType{ |
240
|
|
|
loggerType: Wrn, |
241
|
|
|
}, |
242
|
|
|
want: wrn.Loggers[Wrn], |
243
|
|
|
}, |
244
|
|
|
{ |
245
|
|
|
fields: &Log{ |
246
|
|
|
config: err, |
247
|
|
|
}, |
248
|
|
|
args: argsLogGetLoggerInterfaceByType{ |
249
|
|
|
loggerType: Err, |
250
|
|
|
}, |
251
|
|
|
want: err.Loggers[Err], |
252
|
|
|
}, |
253
|
|
|
{ |
254
|
|
|
fields: &Log{ |
255
|
|
|
config: &Config{}, |
256
|
|
|
}, |
257
|
|
|
args: argsLogGetLoggerInterfaceByType{ |
258
|
|
|
loggerType: 3, |
259
|
|
|
}, |
260
|
|
|
want: &standart.Strategy{}, |
261
|
|
|
}, |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
func TestLog_GetLoggerInterfaceByType(t *testing.T) { |
266
|
|
|
tests := casesLogGetLoggerInterfaceByType() |
267
|
|
|
for _, tt := range tests { |
268
|
|
|
t.Run(tt.name, func(t *testing.T) { |
269
|
|
|
if got := tt.fields.GetLoggerInterfaceByType(tt.args.loggerType); !reflect.DeepEqual(got, tt.want) { |
270
|
|
|
t.Errorf("Log.GetLoggerInterfaceByType() = %v, want %v", got, tt.want) |
271
|
|
|
} |
272
|
|
|
}) |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
type argsLogError struct { |
277
|
|
|
err error |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
type testsLogError struct { |
281
|
|
|
name string |
282
|
|
|
fields Writer |
283
|
|
|
args argsLogError |
284
|
|
|
want Writer |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
func casesLogError() []testsLogError { |
288
|
|
|
info := configProvider() |
289
|
|
|
err := &Config{ |
290
|
|
|
Loggers: Map{ |
291
|
|
|
Err: loggerProvider(), |
292
|
|
|
}, |
293
|
|
|
} |
294
|
|
|
return []testsLogError{ |
295
|
|
|
{ |
296
|
|
|
fields: &Log{ |
297
|
|
|
config: info, |
298
|
|
|
}, |
299
|
|
|
want: &Log{ |
300
|
|
|
config: info, |
301
|
|
|
}, |
302
|
|
|
}, |
303
|
|
|
{ |
304
|
|
|
fields: &Log{ |
305
|
|
|
config: err, |
306
|
|
|
}, |
307
|
|
|
want: &Log{ |
308
|
|
|
config: err, |
309
|
|
|
}, |
310
|
|
|
}, |
311
|
|
|
{ |
312
|
|
|
fields: &Log{ |
313
|
|
|
config: err, |
314
|
|
|
}, |
315
|
|
|
args: argsLogError{ |
316
|
|
|
err: fmt.Errorf("error for test"), |
317
|
|
|
}, |
318
|
|
|
want: &Log{ |
319
|
|
|
config: err, |
320
|
|
|
}, |
321
|
|
|
}, |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
func TestLog_Error(t *testing.T) { |
326
|
|
|
tests := casesLogError() |
327
|
|
|
for _, tt := range tests { |
328
|
|
|
t.Run(tt.name, func(t *testing.T) { |
329
|
|
|
if got := tt.fields.Error(tt.args.err); !reflect.DeepEqual(got, tt.want) { |
330
|
|
|
t.Errorf("Log.Error() = %v, want %v", got, tt.want) |
331
|
|
|
} |
332
|
|
|
}) |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
type argsLogErrorDebug struct { |
337
|
|
|
err error |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
type testsLogErrorDebug struct { |
341
|
|
|
name string |
342
|
|
|
fields Writer |
343
|
|
|
args argsLogErrorDebug |
344
|
|
|
want Writer |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
func casesLogErrorDebug() []testsLogErrorDebug { |
348
|
|
|
info := configProvider() |
349
|
|
|
err := &Config{ |
350
|
|
|
Loggers: Map{ |
351
|
|
|
Err: loggerProvider(), |
352
|
|
|
}, |
353
|
|
|
} |
354
|
|
|
return []testsLogErrorDebug{ |
355
|
|
|
{ |
356
|
|
|
fields: &Log{ |
357
|
|
|
config: info, |
358
|
|
|
}, |
359
|
|
|
want: &Log{ |
360
|
|
|
config: info, |
361
|
|
|
}, |
362
|
|
|
}, |
363
|
|
|
{ |
364
|
|
|
fields: &Log{ |
365
|
|
|
config: err, |
366
|
|
|
}, |
367
|
|
|
want: &Log{ |
368
|
|
|
config: err, |
369
|
|
|
}, |
370
|
|
|
}, |
371
|
|
|
{ |
372
|
|
|
fields: &Log{ |
373
|
|
|
config: err, |
374
|
|
|
}, |
375
|
|
|
args: argsLogErrorDebug{ |
376
|
|
|
err: fmt.Errorf("error for test"), |
377
|
|
|
}, |
378
|
|
|
want: &Log{ |
379
|
|
|
config: err, |
380
|
|
|
}, |
381
|
|
|
}, |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
func TestLog_ErrorDebug(t *testing.T) { |
386
|
|
|
tests := casesLogErrorDebug() |
387
|
|
|
for _, tt := range tests { |
388
|
|
|
t.Run(tt.name, func(t *testing.T) { |
389
|
|
|
if got := tt.fields.ErrorDebug(tt.args.err); !reflect.DeepEqual(got, tt.want) { |
390
|
|
|
t.Errorf("Log.ErrorDebug() = %v, want %v", got, tt.want) |
391
|
|
|
} |
392
|
|
|
}) |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|