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
|
|
|
"runtime" |
14
|
|
|
"testing" |
15
|
|
|
"time" |
16
|
|
|
|
17
|
|
|
"github.com/mylockerteam/alog/strategy/file" |
18
|
|
|
"github.com/mylockerteam/alog/strategy/standart" |
19
|
|
|
"github.com/mylockerteam/alog/util" |
20
|
|
|
) |
21
|
|
|
|
22
|
|
|
const testMsg = "Hello, ALog!" |
23
|
|
|
|
24
|
|
|
func loggerProvider() *Logger { |
25
|
|
|
return &Logger{ |
26
|
|
|
Channel: make(chan string, 1), |
27
|
|
|
Strategies: []io.Writer{ |
28
|
|
|
file.Get(fmt.Sprintf("/tmp/%s/", util.RandString(10))), |
29
|
|
|
standart.Get(), |
30
|
|
|
}, |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
func configProvider() *Config { |
35
|
|
|
return &Config{ |
36
|
|
|
Loggers: Map{ |
37
|
|
|
Info: loggerProvider(), |
38
|
|
|
}, |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
type argsLogPrepareLog struct { |
43
|
|
|
time time.Time |
44
|
|
|
msg string |
45
|
|
|
skip int |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
type testsLogPrepareLog struct { |
49
|
|
|
name string |
50
|
|
|
fields Writer |
51
|
|
|
args argsLogPrepareLog |
52
|
|
|
want string |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
func casesLogPrepareLog() []testsLogPrepareLog { |
56
|
|
|
_, fileName, fileLine, _ := runtime.Caller(2) |
57
|
|
|
now := time.Now() |
58
|
|
|
configFirst := configProvider() |
59
|
|
|
configFirst.TimeFormat = time.RFC3339 |
60
|
|
|
configSecond := configProvider() |
61
|
|
|
loggerErr := loggerProvider() |
62
|
|
|
loggerErr.Strategies = append(loggerErr.Strategies, file.Get("")) |
63
|
|
|
configSecond.Loggers = Map{ |
64
|
|
|
Info: loggerProvider(), |
65
|
|
|
Err: loggerErr, |
66
|
|
|
} |
67
|
|
|
return []testsLogPrepareLog{ |
68
|
|
|
{ |
69
|
|
|
fields: &Log{ |
70
|
|
|
config: configFirst, |
71
|
|
|
}, |
72
|
|
|
args: argsLogPrepareLog{ |
73
|
|
|
time: now, |
74
|
|
|
msg: testMsg, |
75
|
|
|
skip: 2, |
76
|
|
|
}, |
77
|
|
|
want: fmt.Sprintf( |
78
|
|
|
messageFormatWithFileLine, |
79
|
|
|
now.Format(time.RFC3339), |
80
|
|
|
fileName, |
81
|
|
|
fileLine, |
82
|
|
|
testMsg, |
83
|
|
|
), |
84
|
|
|
}, |
85
|
|
|
{ |
86
|
|
|
fields: &Log{ |
87
|
|
|
config: configSecond, |
88
|
|
|
}, |
89
|
|
|
args: argsLogPrepareLog{ |
90
|
|
|
time: now, |
91
|
|
|
msg: testMsg, |
92
|
|
|
skip: 2, |
93
|
|
|
}, |
94
|
|
|
want: fmt.Sprintf( |
95
|
|
|
messageFormatWithFileLine, |
96
|
|
|
now.Format(time.RFC3339Nano), |
97
|
|
|
fileName, |
98
|
|
|
fileLine, |
99
|
|
|
testMsg, |
100
|
|
|
), |
101
|
|
|
}, |
102
|
|
|
{ |
103
|
|
|
fields: &Log{ |
104
|
|
|
config: configSecond, |
105
|
|
|
}, |
106
|
|
|
args: argsLogPrepareLog{ |
107
|
|
|
time: now, |
108
|
|
|
msg: testMsg, |
109
|
|
|
skip: 1000, |
110
|
|
|
}, |
111
|
|
|
want: fmt.Sprintf( |
112
|
|
|
messageFormatDefault, |
113
|
|
|
now.Format(time.RFC3339Nano), |
114
|
|
|
testMsg, |
115
|
|
|
), |
116
|
|
|
}, |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
func TestLog_prepareLog(t *testing.T) { |
121
|
|
|
tests := casesLogPrepareLog() |
122
|
|
|
for _, tt := range tests { |
123
|
|
|
t.Run(tt.name, func(t *testing.T) { |
124
|
|
|
if got := tt.fields.(*Log).prepareLog(tt.args.time, tt.args.msg, tt.args.skip); got != tt.want { |
125
|
|
|
t.Errorf("Log.prepareLog() = %v, want %v", got, tt.want) |
126
|
|
|
} |
127
|
|
|
}) |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
type testsCreate struct { |
132
|
|
|
name string |
133
|
|
|
config *Config |
134
|
|
|
want Writer |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
func casesCreate() []testsCreate { |
138
|
|
|
config := configProvider() |
139
|
|
|
return []testsCreate{ |
140
|
|
|
{ |
141
|
|
|
config: config, |
142
|
|
|
want: &Log{ |
143
|
|
|
config: config, |
144
|
|
|
}, |
145
|
|
|
}, |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
func TestCreate(t *testing.T) { |
150
|
|
|
tests := casesCreate() |
151
|
|
|
for _, tt := range tests { |
152
|
|
|
t.Run(tt.name, func(t *testing.T) { |
153
|
|
|
if got := Create(tt.config); !reflect.DeepEqual(got, tt.want) { |
154
|
|
|
t.Errorf("Create() = %v, want %v", got, tt.want) |
155
|
|
|
} |
156
|
|
|
}) |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
func TestDefault(t *testing.T) { |
161
|
|
|
if got := Default(0); reflect.TypeOf(got) != reflect.TypeOf(&Log{}) { |
162
|
|
|
t.Errorf("Create() = %v, want %v", got, &Log{}) |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|