-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlevel_test.go
163 lines (140 loc) · 4 KB
/
level_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
package logf
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLevelChecker(t *testing.T) {
type LevelCheck struct {
level Level
enabled bool
}
cases := []struct {
level Level
goldens []LevelCheck
}{
{
LevelError,
[]LevelCheck{{LevelError, true}, {LevelWarn, false}, {LevelInfo, false}, {LevelDebug, false}},
},
{
LevelWarn,
[]LevelCheck{{LevelError, true}, {LevelWarn, true}, {LevelInfo, false}, {LevelDebug, false}},
},
{
LevelInfo,
[]LevelCheck{{LevelError, true}, {LevelWarn, true}, {LevelInfo, true}, {LevelDebug, false}},
},
{
LevelDebug,
[]LevelCheck{{LevelError, true}, {LevelWarn, true}, {LevelInfo, true}, {LevelDebug, true}},
},
}
for _, cs := range cases {
checker := cs.level.LevelChecker()
for _, golden := range cs.goldens {
assert.Equal(t, golden.enabled, checker(golden.level), "%q checks with %q", cs.level, golden.level)
}
}
}
func TestLevelString(t *testing.T) {
cases := []struct {
level Level
strGolden string
capStrGolden string
}{
{LevelError, "error", "ERROR"},
{LevelWarn, "warn", "WARN"},
{LevelInfo, "info", "INFO"},
{LevelDebug, "debug", "DEBUG"},
{Level(42), "unknown", "UNKNOWN"},
}
for _, cs := range cases {
assert.Equal(t, cs.strGolden, cs.level.String(), "check level %d String", int(cs.level))
assert.Equal(t, cs.capStrGolden, cs.level.UpperCaseString(), "check level %d UpperCaseString", int(cs.level))
}
}
func TestLevelFromString(t *testing.T) {
cases := []struct {
checking []string
golden Level
}{
{[]string{"error", "ERROR"}, LevelError},
{[]string{"warn", "WARN", "warning", "WARNING"}, LevelWarn},
{[]string{"info", "INFO", "information", "INFORMATION"}, LevelInfo},
{[]string{"debug", "DEBUG"}, LevelDebug},
}
for _, cs := range cases {
for _, lvl := range cs.checking {
level, ok := LevelFromString(lvl)
assert.True(t, ok, "validate %q", lvl)
assert.Equal(t, cs.golden, level, "compare golden %q with %q", cs.golden, level)
}
}
_, ok := LevelFromString("unknown")
assert.False(t, ok, "fail is expected")
}
func TestDefaultLevelEncoder(t *testing.T) {
enc := testTypeEncoder{}
DefaultLevelEncoder(LevelError, &enc)
assert.EqualValues(t, "error", enc.result)
}
func TestUpperCaseLevelEncoder(t *testing.T) {
enc := testTypeEncoder{}
UpperCaseLevelEncoder(LevelError, &enc)
assert.EqualValues(t, "ERROR", enc.result)
}
func TestMutableLevelChecker(t *testing.T) {
type LevelCheck struct {
level Level
enabled bool
}
cases := []struct {
checker LevelChecker
goldens []LevelCheck
}{
{
NewMutableLevel(LevelError).LevelChecker(),
[]LevelCheck{{LevelError, true}, {LevelWarn, false}, {LevelInfo, false}, {LevelDebug, false}},
},
{
NewMutableLevel(LevelWarn).LevelChecker(),
[]LevelCheck{{LevelError, true}, {LevelWarn, true}, {LevelInfo, false}, {LevelDebug, false}},
},
{
NewMutableLevel(LevelInfo).LevelChecker(),
[]LevelCheck{{LevelError, true}, {LevelWarn, true}, {LevelInfo, true}, {LevelDebug, false}},
},
{
NewMutableLevel(LevelDebug).LevelChecker(),
[]LevelCheck{{LevelError, true}, {LevelWarn, true}, {LevelInfo, true}, {LevelDebug, true}},
},
}
for i, cs := range cases {
for _, golden := range cs.goldens {
assert.Equal(t, golden.enabled, cs.checker(golden.level), "%d checks with %q", i, golden.level)
}
}
}
func TestMutableLevel(t *testing.T) {
level := NewMutableLevel(LevelError)
assert.Equal(t, LevelError, level.Level())
level.Set(LevelDebug)
assert.Equal(t, LevelDebug, level.Level())
}
func TestLevelUnmarshal(t *testing.T) {
v := struct {
Level Level `json:"level"`
}{}
err := json.Unmarshal([]byte(`{"level": "warn"}`), &v)
assert.NoError(t, err)
assert.Equal(t, LevelWarn, v.Level)
}
func TestLevelUnmarshalInvalid(t *testing.T) {
v := struct {
Level Level `json:"level"`
}{}
err := json.Unmarshal([]byte(`{"level": "some-invalid-value"}`), &v)
assert.EqualError(t, err, `invalid logging level "some-invalid-value"`)
assert.Equal(t, LevelError, v.Level)
}