-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel_test.go
151 lines (133 loc) · 4.21 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
package lager
import (
"bytes"
"flag"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLevelString(t *testing.T) {
testCases := map[Level]string{
Level(-42): "Level(-42)",
}
for l, v := range levelToString {
testCases[l] = v
}
for lvl, stringLevel := range testCases {
assert.Equal(t, stringLevel, lvl.String(), "unexpected lowercase level string.")
assert.Equal(t, strings.ToUpper(stringLevel), lvl.CapitalString(), "unexpected all-caps level string.")
}
}
func TestLevelText(t *testing.T) {
testCases := []struct {
text string
level Level
}{
{"debug", DebugLevel},
{"info", InfoLevel},
{"", InfoLevel}, // make the zero value useful
{"warn", WarnLevel},
{"error", ErrorLevel},
{"none", NoneLevel},
{"fatal", FatalLevel},
}
for _, tt := range testCases {
if tt.text != "" {
lvl := tt.level
marshaled, err := lvl.MarshalText()
assert.NoError(t, err, "unexpected error marshaling level %v to text.", &lvl)
assert.Equal(t, tt.text, string(marshaled), "Marshaling level %v to text yielded unexpected result.", &lvl)
}
var unmarshalled Level
err := unmarshalled.UnmarshalText([]byte(tt.text))
assert.NoError(t, err, `unexpected error unmarshalling text %q to level.`, tt.text)
assert.Equal(t, tt.level, unmarshalled, `Text %q unmarshalled to an unexpected level.`, tt.text)
}
}
func TestCapitalLevelsParse(t *testing.T) {
testCases := []struct {
text string
level Level
}{
{"DEBUG", DebugLevel},
{"INFO", InfoLevel},
{"WARN", WarnLevel},
{"ERROR", ErrorLevel},
{"NONE", NoneLevel},
{"FATAL", FatalLevel},
}
for _, tt := range testCases {
var unmarshalled Level
err := unmarshalled.UnmarshalText([]byte(tt.text))
assert.NoError(t, err, `unexpected error unmarshalling text %q to level.`, tt.text)
assert.Equal(t, tt.level, unmarshalled, `Text %q unmarshalled to an unexpected level.`, tt.text)
}
}
func TestWeirdLevelsParse(t *testing.T) {
testCases := []struct {
text string
level Level
}{
// I guess...
{"Debug", DebugLevel},
{"Info", InfoLevel},
{"Warn", WarnLevel},
{"Error", ErrorLevel},
{"None", NoneLevel},
{"Fatal", FatalLevel},
// What even is...
{"DeBuG", DebugLevel},
{"InFo", InfoLevel},
{"WaRn", WarnLevel},
{"ErRor", ErrorLevel},
{"NoNe", NoneLevel},
{"FaTaL", FatalLevel},
}
for _, tt := range testCases {
var unmarshalled Level
err := unmarshalled.UnmarshalText([]byte(tt.text))
assert.NoError(t, err, `unexpected error unmarshalling text %q to level.`, tt.text)
assert.Equal(t, tt.level, unmarshalled, `Text %q unmarshalled to an unexpected level.`, tt.text)
}
}
func TestLevelNils(t *testing.T) {
var l *Level
// The String() method will not handle nil level properly.
assert.Panics(t, func() {
assert.Equal(t, "Level(nil)", l.String(), "unexpected result stringify nil *Level.")
}, "Level(nil).String() should panic")
assert.Panics(t, func() {
_, _ = l.MarshalText()
}, "expected to panic when marshalling a nil level.")
err := l.UnmarshalText([]byte("debug"))
assert.Equal(t, errUnmarshalNilLevel, err, "expected to error unmarshalling into a nil Level.")
}
func TestLevelUnmarshalUnknownText(t *testing.T) {
var l Level
err := l.UnmarshalText([]byte("foo"))
assert.Error(t, err)
assert.Contains(t, err.Error(), "unrecognized level", "expected unmarshalling arbitrary text to fail.")
}
func TestLevelAsFlagValue(t *testing.T) {
var (
buf bytes.Buffer
lvl Level
)
fs := flag.NewFlagSet("levelTest", flag.ContinueOnError)
fs.SetOutput(&buf)
fs.Var(&lvl, "level", "log level")
for _, expected := range []Level{DebugLevel, InfoLevel, WarnLevel, ErrorLevel, NoneLevel, FatalLevel} {
assert.NoError(t, fs.Parse([]string{"-level", expected.String()}))
assert.Equal(t, expected, lvl, "unexpected level after parsing flag.")
assert.Equal(t, expected, lvl.Get(), "unexpected output using flag.Getter API.")
assert.Empty(t, buf.String(), "unexpected error output parsing level flag.")
buf.Reset()
}
assert.Error(t, fs.Parse([]string{"-level", "nope"}))
assert.Equal(
t,
`invalid value "nope" for flag -level: unrecognized level: "nope"`,
strings.Split(buf.String(), "\n")[0], // second line is help message
"unexpected error output from invalid flag input.",
)
}