-
Notifications
You must be signed in to change notification settings - Fork 0
/
levels_test.go
60 lines (50 loc) · 1.23 KB
/
levels_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
package log
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseLevel(t *testing.T) {
cases := []struct {
String string
Level Level
Num int
}{
{"debug", DebugLevel, 0},
{"info", InfoLevel, 1},
{"warn", WarnLevel, 2},
{"warning", WarnLevel, 3},
{"error", ErrorLevel, 4},
{"fatal", FatalLevel, 5},
}
for _, c := range cases {
t.Run(c.String, func(t *testing.T) {
l, err := ParseLevel(c.String)
assert.NoError(t, err, "parse")
assert.Equal(t, c.Level, l)
})
}
t.Run("invalid", func(t *testing.T) {
l, err := ParseLevel("something")
assert.Equal(t, ErrInvalidLevel, err)
assert.Equal(t, InvalidLevel, l)
})
}
func TestLevel_MarshalJSON(t *testing.T) {
e := Entry{
Level: InfoLevel,
Message: "hello",
Fields: Fields{},
}
expect := `{"fields":{},"level":"info","timestamp":"0001-01-01T00:00:00Z","message":"hello"}`
b, err := json.Marshal(e)
assert.NoError(t, err)
assert.Equal(t, expect, string(b))
}
func TestLevel_UnmarshalJSON(t *testing.T) {
s := `{"fields":{},"level":"info","timestamp":"0001-01-01T00:00:00Z","message":"hello"}`
e := new(Entry)
err := json.Unmarshal([]byte(s), e)
assert.NoError(t, err)
assert.Equal(t, InfoLevel, e.Level)
}