-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_test.go
More file actions
201 lines (189 loc) · 5.12 KB
/
log_test.go
File metadata and controls
201 lines (189 loc) · 5.12 KB
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
package log
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestDebug(t *testing.T) {
resetTest()
t.Run("must returns debug message in output", func(t *testing.T) {
message := "text message"
Debug(message)
text := strings.ToLower(testOutput.String())
assert.Contains(t, text, fmt.Sprintf("\"message\":\"%v\"", message))
assert.Contains(t, text, fmt.Sprintf("\"level\":%d", LevelDebug))
})
}
func TestInfo(t *testing.T) {
resetTest()
t.Run("must returns info message in output", func(t *testing.T) {
message := "text message"
Info(message)
text := strings.ToLower(testOutput.String())
assert.Contains(t, text, fmt.Sprintf("\"message\":\"%v\"", message))
assert.Contains(t, text, fmt.Sprintf("\"level\":%d", LevelInfo))
})
}
func TestWarning(t *testing.T) {
resetTest()
t.Run("must returns warning message in output", func(t *testing.T) {
message := "text message"
Warning(message)
text := strings.ToLower(testOutput.String())
assert.Contains(t, text, fmt.Sprintf("\"message\":\"%v\"", message))
assert.Contains(t, text, fmt.Sprintf("\"level\":%d", LevelWarning))
})
}
func TestError(t *testing.T) {
resetTest()
t.Run("must returns error message in output", func(t *testing.T) {
message := "text message"
Error(message)
text := strings.ToLower(testOutput.String())
assert.Contains(t, text, fmt.Sprintf("\"message\":\"%v\"", message))
assert.Contains(t, text, fmt.Sprintf("\"level\":%d", LevelError))
})
}
func TestFatal(t *testing.T) {
resetTest()
t.Run("must returns fatal message in output", func(t *testing.T) {
message := "fatal text message"
exit = func(code int) {
assert.Equal(t, code, 1)
}
Fatal(message)
text := strings.ToLower(testOutput.String())
assert.Contains(t, text, fmt.Sprintf("\"message\":\"%v\"", message))
assert.Contains(t, text, fmt.Sprintf("\"level\":%d", LevelFatal))
})
}
func TestSetConstant(t *testing.T) {
resetTest()
data := "\"Data\":{\"const_key\":\"const_value\"}"
SetConstant("const_key", "const_value")
for _, lvl := range testLevels {
t.Run(fmt.Sprintf("must returns constant values in %v", strings.ToLower(lvl.String())), func(t *testing.T) {
switch lvl {
case LevelDebug:
Debug("")
case LevelInfo:
Info("")
case LevelWarning:
Warning("")
case LevelError:
Error("")
case LevelFatal:
Fatal("")
}
assert.Contains(t, testOutput.String(), data)
testOutput.Reset()
})
}
}
func TestSetLevel(t *testing.T) {
resetTest()
for _, lvl := range testLevels {
SetLevel(lvl)
t.Run(fmt.Sprintf("must allowed to logs equal or greater than %v", strings.ToLower(lvl.String())), func(t *testing.T) {
for _, l := range testLevels {
switch l {
case LevelDebug:
Debug("")
case LevelInfo:
Info("")
case LevelWarning:
Warning("")
case LevelError:
Error("")
case LevelFatal:
Fatal("")
}
if lvl <= l {
assert.NotEmpty(t, testOutput.String())
} else {
assert.Empty(t, testOutput.String())
}
testOutput.Reset()
}
})
}
}
func TestSetFormatter(t *testing.T) {
resetTest()
t.Run("must sets json-formatter in formatter var", func(t *testing.T) {
f := new(jsonFormatter)
SetFormatter(f)
assert.Equal(t, formatter, f)
})
t.Run("must sets yaml-formatter in formatter var", func(t *testing.T) {
f := new(yamlFormatter)
SetFormatter(f)
assert.Equal(t, formatter, f)
})
t.Run("must sets text-formatter in formatter var", func(t *testing.T) {
f := new(textFormatter)
SetFormatter(f)
assert.Equal(t, formatter, f)
})
}
func TestSetOutput(t *testing.T) {
resetTest()
t.Run("must sets buffer in output var", func(t *testing.T) {
buf := new(bytes.Buffer)
SetOutput(buf)
assert.Equal(t, output, buf)
})
}
func TestValue(t *testing.T) {
resetTest()
t.Run("must returns entry with value", func(t *testing.T) {
entry := Value("key", "value")
assert.Equal(t, entry.Data["key"], "value")
})
}
func TestWith(t *testing.T) {
resetTest()
t.Run("must returns entry with value", func(t *testing.T) {
entry := With("value1").With("value2").With("value3")
assert.Equal(t, entry.Data[dataValues], []interface{}{"value1", "value2", "value3"})
})
t.Run("must returns entry with inline struct", func(t *testing.T) {
testData := struct {
Name string
}{
Name: "name",
}
entry := With(testData)
assert.Equal(t, entry.Data[""], testData)
})
t.Run("must returns entry with defined struct", func(t *testing.T) {
type testData struct {
Name string
}
td := testData{Name: "name"}
entry := With(td)
assert.Equal(t, entry.Data["testData"], td)
})
t.Run("must returns entry with error", func(t *testing.T) {
err := fmt.Errorf("test message")
entry := With(err)
assert.Equal(t, entry.Data[dataError], err.Error())
})
t.Run("must returns entry with map", func(t *testing.T) {
dict := map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
entry := With(dict)
for key, value := range dict {
assert.Equal(t, entry.Data[key], value)
}
})
t.Run("must returns entry without data affect with nil", func(t *testing.T) {
entry := With(nil)
assert.NotNil(t, entry)
})
}