forked from gookit/slog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_test.go
88 lines (73 loc) · 2.22 KB
/
common_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
package slog_test
import (
"fmt"
"testing"
"github.com/gookit/goutil/dump"
"github.com/gookit/slog"
"github.com/stretchr/testify/assert"
)
var (
testData1 = slog.M{"key0": "val0", "age": 23}
testData2 = slog.M{"key0": "val0", "age": 23, "sub": slog.M{
"subKey0": 345,
}}
)
func TestDefine_basic(t *testing.T) {
assert.NotEmpty(t, slog.NoTimeFields)
assert.NotEmpty(t, slog.FieldKeyDate)
assert.NotEmpty(t, slog.FieldKeyTime)
assert.NotEmpty(t, slog.FieldKeyCaller)
assert.NotEmpty(t, slog.FieldKeyError)
}
func TestM_String(t *testing.T) {
m := slog.M{
"k0": 12,
"k1": "abc",
"k2": true,
"k3": 23.45,
"k4": []int{12, 23},
"k5": []string{"ab", "bc"},
"k6": map[string]interface{}{
"k6-1": 23,
"k6-2": "def",
},
}
fmt.Println(m)
dump.P(m.String(), m)
assert.NotEmpty(t, m.String())
}
func TestLevel_Name(t *testing.T) {
assert.Equal(t, "INFO", slog.InfoLevel.Name())
assert.Equal(t, "INFO", slog.InfoLevel.String())
assert.Equal(t, "info", slog.InfoLevel.LowerName())
assert.Equal(t, "unknown", slog.Level(330).LowerName())
}
func TestLevelByName(t *testing.T) {
assert.Equal(t, slog.InfoLevel, slog.LevelByName("info"))
assert.Equal(t, slog.InfoLevel, slog.LevelByName("invalid"))
}
func TestLevelName(t *testing.T) {
for level, wantName := range slog.LevelNames {
realName := slog.LevelName(level)
assert.Equal(t, wantName, realName)
}
assert.Equal(t, "UNKNOWN", slog.LevelName(20))
}
func TestLevel_ShouldHandling(t *testing.T) {
assert.True(t, slog.InfoLevel.ShouldHandling(slog.ErrorLevel))
assert.False(t, slog.InfoLevel.ShouldHandling(slog.TraceLevel))
assert.True(t, slog.DebugLevel.ShouldHandling(slog.InfoLevel))
assert.False(t, slog.DebugLevel.ShouldHandling(slog.TraceLevel))
}
func TestLevels_Contains(t *testing.T) {
assert.True(t, slog.DangerLevels.Contains(slog.ErrorLevel))
assert.False(t, slog.DangerLevels.Contains(slog.InfoLevel))
assert.True(t, slog.NormalLevels.Contains(slog.InfoLevel))
assert.False(t, slog.NormalLevels.Contains(slog.PanicLevel))
}
func TestNewLvFormatter(t *testing.T) {
lf := slog.NewLvFormatter(slog.InfoLevel)
assert.True(t, lf.IsHandling(slog.ErrorLevel))
assert.True(t, lf.IsHandling(slog.InfoLevel))
assert.False(t, lf.IsHandling(slog.DebugLevel))
}