-
Notifications
You must be signed in to change notification settings - Fork 82
/
facility_test.go
94 lines (79 loc) · 2.71 KB
/
facility_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
package logging
import (
"strings"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/uber-common/bark"
"github.com/uber/ringpop-go/test/mocks"
)
type LogFacilityTestSuite struct {
suite.Suite
mockLogger *mocks.Logger
facility *Facility
}
func (s *LogFacilityTestSuite) SetupTest() {
s.mockLogger = &mocks.Logger{}
s.facility = NewFacility(s.mockLogger)
// Set expected calls
for _, meth := range []string{"Debug", "Info", "Warn", "Error", "Fatal", "Panic"} {
s.mockLogger.On(meth, mock.Anything)
s.mockLogger.On(meth+"f", mock.Anything, mock.Anything)
}
s.mockLogger.On("WithFields", mock.Anything).Return(s.mockLogger)
s.mockLogger.On("WithField", mock.Anything, mock.Anything).Return(s.mockLogger)
}
func (s *LogFacilityTestSuite) TestForwarding() {
levels := []Level{Debug, Info, Warn, Error, Fatal, Panic}
for _, level := range levels {
methName := strings.Title(level.String())
msg := []interface{}{"msg"}
fields := bark.Fields{"a": 1}
s.facility.Log("name", level, fields, msg)
s.mockLogger.AssertCalled(s.T(), methName, msg)
s.mockLogger.AssertCalled(s.T(), "WithFields", fields)
fields = bark.Fields{"b": 2}
s.facility.Logf("name", level, fields, "format", msg)
s.mockLogger.AssertCalled(s.T(), methName+"f", "format", msg)
s.mockLogger.AssertCalled(s.T(), "WithFields", fields)
}
}
func (s *LogFacilityTestSuite) TestSetLogger() {
newLogger := &mocks.Logger{}
s.facility.SetLogger(newLogger)
newLogger.On("Debug", mock.Anything)
msg := []interface{}{"msg"}
s.facility.Log("name", Debug, nil, msg)
newLogger.AssertCalled(s.T(), "Debug", msg)
}
func (s *LogFacilityTestSuite) TestSetLevel() {
s.facility.SetLevel("name", Warn)
s.afterLevelIsSet()
}
func (s *LogFacilityTestSuite) TestSetLevels() {
s.facility.SetLevels(map[string]Level{"name": Warn})
s.afterLevelIsSet()
}
func (s *LogFacilityTestSuite) afterLevelIsSet() {
msg := []interface{}{"msg"}
s.facility.Log("name", Debug, nil, msg)
s.facility.Logf("name", Debug, nil, "format", msg)
s.mockLogger.AssertNotCalled(s.T(), "Debug", msg)
s.mockLogger.AssertNotCalled(s.T(), "Debugf", "format", msg)
}
func (s *LogFacilityTestSuite) TestSetLevelError() {
s.Error(s.facility.SetLevel("name", Panic),
"Setting a severity level above Fatal should fail.")
}
func (s *LogFacilityTestSuite) TestSetLevelsError() {
s.Error(s.facility.SetLevels(map[string]Level{"name": Panic}),
"Setting a severity level above Fatal should fail.")
}
func (s *LogFacilityTestSuite) TestNamedLogger() {
logger := s.facility.Logger("name")
logger.Debug("msg")
s.mockLogger.AssertCalled(s.T(), "Debug", []interface{}{"msg"})
}
func TestLogFacilityTestSuite(t *testing.T) {
suite.Run(t, new(LogFacilityTestSuite))
}