This repository has been archived by the owner on Jan 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
levels_test.go
111 lines (87 loc) · 2.3 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
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
package logur
import (
"testing"
)
// nolint: gochecknoglobals
var levelMap = map[string]Level{
"trace": Trace,
"debug": Debug,
"info": Info,
"warn": Warn,
"error": Error,
}
func TestLevel_String(t *testing.T) {
for levelName, level := range levelMap {
levelName, level := levelName, level
t.Run(levelName, func(t *testing.T) {
if levelString := level.String(); levelString != levelName {
t.Errorf("level %q does not match the expected %q", levelString, levelName)
}
})
}
}
func TestLevel_String_Unknown(t *testing.T) {
level := Level(999)
if levelString := level.String(); levelString != "unknown" {
t.Errorf("level %q does not match the expected \"unknown\"", levelString)
}
}
func TestParseAndUnmarshalLevel(t *testing.T) {
tests := map[string]Level{
"trace": Trace,
"debug": Debug,
"info": Info,
"warn": Warn,
"warning": Warn,
"error": Error,
}
for levelName, level := range tests {
levelName, level := levelName, level
t.Run("parse:"+levelName, func(t *testing.T) {
parsedLevel, ok := ParseLevel(levelName)
if !ok {
t.Fatalf("parsing level failed: %q", levelName)
}
if parsedLevel != level {
t.Errorf("parsed level %q does not match the expected %q", parsedLevel, level)
}
})
t.Run("unmarshal:"+levelName, func(t *testing.T) {
var l Level
err := l.UnmarshalText([]byte(levelName))
if err != nil {
t.Fatal("unmarshaling level failed:", err.Error())
}
if l != level {
t.Errorf("unmarshaled level %q does not match the expected %q", l, level)
}
})
}
}
func TestParseLevel_Unknown(t *testing.T) {
_, ok := ParseLevel("unknown")
if ok {
t.Error("parsing unknown levels should fail")
}
}
func TestLevelFunc(t *testing.T) {
for levelName, level := range levelMap {
levelName, level := levelName, level
t.Run(levelName, func(t *testing.T) {
logger := NewTestLogger()
logFunc := LevelFunc(logger, level)
const msg = "message"
logFunc(msg)
if logger.Count() < 1 {
t.Fatal("logger did not record any events")
}
event := logger.LastEvent()
if event.Level != level {
t.Errorf("expected level %q instead of %q", level.String(), event.Level.String())
}
if got, want := event.Line, msg; got != want {
t.Errorf("expected message %q instead of %q", want, got)
}
})
}
}