-
Notifications
You must be signed in to change notification settings - Fork 1
/
level_test.go
107 lines (90 loc) · 3.49 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
package logger_test
import (
"reflect"
"strings"
"testing"
"github.com/gildas/go-logger"
"github.com/stretchr/testify/suite"
)
type LevelSuite struct {
suite.Suite
Name string
}
func TestLevelSuite(t *testing.T) {
suite.Run(t, new(LevelSuite))
}
func (suite *LevelSuite) SetupSuite() {
suite.Name = strings.TrimSuffix(reflect.TypeOf(suite).Elem().Name(), "Suite")
}
func (suite *LevelSuite) TestCanParseLevel() {
suite.Assert().Equal(logger.INFO, logger.ParseLevel("INFO"), "Failed to parse level \"INFO\"")
suite.Assert().Equal(logger.INFO, logger.ParseLevel("info"), "Failed to parse level \"INFO\"")
suite.Assert().Equal(logger.UNSET, logger.ParseLevel("UNSET"), "Failed to parse level \"UNSET\"")
suite.Assert().Equal(logger.NEVER, logger.ParseLevel("whatever"), "Failed to parse level \"whatever\" => NEVER")
}
func (suite *LevelSuite) TestCanStringLevel() {
suite.Assert().Equal("INFO", logger.INFO.String())
suite.Assert().Equal("ERROR", logger.ERROR.String())
bigLevel := logger.Level(220)
suite.Assert().Equal("ALWAYS", bigLevel.String())
weirdLevel := logger.Level(47)
suite.Assert().Equal("WARN", weirdLevel.String())
neverLevel := logger.UNSET
suite.Assert().Equal("UNSET", neverLevel.String())
}
func (suite *LevelSuite) TestCanCompareLevels() {
levels := []logger.Level{logger.UNSET, logger.TRACE, logger.DEBUG, logger.INFO, logger.WARN, logger.ERROR, logger.FATAL, logger.ALWAYS}
var filter logger.Level
for index, filter := range levels[1:6] {
for _, level := range levels[:index] {
suite.Assert().Falsef(level.ShouldWrite(filter), "%s should not be writable with %s filter", level, filter)
}
for _, level := range levels[index+1:] {
suite.Assert().Truef(level.ShouldWrite(filter), "%s should be writable with %s filter", level, filter)
}
}
filter = logger.NEVER
for _, level := range levels {
suite.Assert().Falsef(level.ShouldWrite(filter), "%s should not be writable with %s filter", level, filter)
}
filter = logger.ALWAYS
for _, level := range levels[:0] {
suite.Assert().Falsef(level.ShouldWrite(filter), "%s should not be writable with %s filter", level, filter)
}
for _, level := range levels[1:] {
suite.Assert().Truef(level.ShouldWrite(filter), "%s should be writable with %s filter", level, filter)
}
filter = logger.UNSET
for _, level := range levels[:0] {
suite.Assert().Falsef(level.ShouldWrite(filter), "%s should not be writable with %s filter", level, filter)
}
for _, level := range levels[1:] {
suite.Assert().Truef(level.ShouldWrite(filter), "%s should be writable with %s filter", level, filter)
}
}
func (suite *LevelSuite) TestCanGetNextLevel() {
level := logger.DEBUG
suite.Assert().Equal(logger.INFO, level.Next())
level = logger.ALWAYS
suite.Assert().Equal(logger.ALWAYS, level.Next())
}
func (suite *LevelSuite) TestCanGetPreviousLevel() {
level := logger.DEBUG
suite.Assert().Equal(logger.TRACE, level.Previous())
level = logger.NEVER
suite.Assert().Equal(logger.NEVER, level.Previous())
}
func (suite *LevelSuite) TestCanGetLevelFromRecord() {
record := logger.NewRecord().Set("level", logger.INFO)
level := logger.GetLevelFromRecord(record)
suite.Assert().Equal(logger.INFO, level)
}
func (suite *LevelSuite) TestCanGetLevelFromRecordWithoutLevel() {
record := logger.NewRecord().Set("msg", "Hello")
level := logger.GetLevelFromRecord(record)
suite.Assert().Equal(logger.NEVER, level)
}
func (suite *LevelSuite) TestCanGetLevelFromNilRecord() {
level := logger.GetLevelFromRecord(nil)
suite.Assert().Equal(logger.NEVER, level)
}