...
1 package logrus_test
2
3 import (
4 "bytes"
5 "encoding/json"
6 "testing"
7
8 "github.com/sirupsen/logrus"
9 "github.com/stretchr/testify/require"
10 )
11
12 func TestLevelJsonEncoding(t *testing.T) {
13 type X struct {
14 Level logrus.Level
15 }
16
17 var x X
18 x.Level = logrus.WarnLevel
19 var buf bytes.Buffer
20 enc := json.NewEncoder(&buf)
21 require.NoError(t, enc.Encode(x))
22 dec := json.NewDecoder(&buf)
23 var y X
24 require.NoError(t, dec.Decode(&y))
25 }
26
27 func TestLevelUnmarshalText(t *testing.T) {
28 var u logrus.Level
29 for _, level := range logrus.AllLevels {
30 t.Run(level.String(), func(t *testing.T) {
31 require.NoError(t, u.UnmarshalText([]byte(level.String())))
32 require.Equal(t, level, u)
33 })
34 }
35 t.Run("invalid", func(t *testing.T) {
36 require.Error(t, u.UnmarshalText([]byte("invalid")))
37 })
38 }
39
40 func TestLevelMarshalText(t *testing.T) {
41 levelStrings := []string{
42 "panic",
43 "fatal",
44 "error",
45 "warning",
46 "info",
47 "debug",
48 "trace",
49 }
50 for idx, val := range logrus.AllLevels {
51 level := val
52 t.Run(level.String(), func(t *testing.T) {
53 var cmp logrus.Level
54 b, err := level.MarshalText()
55 require.NoError(t, err)
56 require.Equal(t, levelStrings[idx], string(b))
57 err = cmp.UnmarshalText(b)
58 require.NoError(t, err)
59 require.Equal(t, level, cmp)
60 })
61 }
62 }
63
View as plain text