forked from sirupsen/logrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
level_test.go
62 lines (56 loc) · 1.24 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
package logrus_test
import (
"bytes"
"encoding/json"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
func TestLevelJsonEncoding(t *testing.T) {
type X struct {
Level logrus.Level
}
var x X
x.Level = logrus.WarnLevel
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
require.NoError(t, enc.Encode(x))
dec := json.NewDecoder(&buf)
var y X
require.NoError(t, dec.Decode(&y))
}
func TestLevelUnmarshalText(t *testing.T) {
var u logrus.Level
for _, level := range logrus.AllLevels {
t.Run(level.String(), func(t *testing.T) {
require.NoError(t, u.UnmarshalText([]byte(level.String())))
require.Equal(t, level, u)
})
}
t.Run("invalid", func(t *testing.T) {
require.Error(t, u.UnmarshalText([]byte("invalid")))
})
}
func TestLevelMarshalText(t *testing.T) {
levelStrings := []string{
"panic",
"fatal",
"error",
"warning",
"info",
"debug",
"trace",
}
for idx, val := range logrus.AllLevels {
level := val
t.Run(level.String(), func(t *testing.T) {
var cmp logrus.Level
b, err := level.MarshalText()
require.NoError(t, err)
require.Equal(t, levelStrings[idx], string(b))
err = cmp.UnmarshalText(b)
require.NoError(t, err)
require.Equal(t, level, cmp)
})
}
}