-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_test.go
105 lines (80 loc) · 3.3 KB
/
handlers_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
package epilog
import (
"bytes"
"time"
"github.com/go-st/logger"
. "gopkg.in/check.v1"
)
type HandlersTestSuite struct {
rawFormatter IFormatter
}
var (
_ = Suite(&HandlersTestSuite{})
)
func (s *HandlersTestSuite) SetUpSuite(c *C) {
s.rawFormatter = NewTextFormatter(":message:")
}
func (s *HandlersTestSuite) TestStreamHandlerHandle(c *C) {
buf := &bytes.Buffer{}
handler := NewStreamHandler(logger.LevelDebug, s.rawFormatter, buf)
handler.Handle(NewEntry(logger.LevelDebug, time.Now(), "hello"))
handler.Handle(NewEntry(logger.LevelInfo, time.Now(), "man"))
c.Assert(buf.String(), Equals, "hello\nman\n")
}
func (s *HandlersTestSuite) TestStreamHandlerHandleLowLevel(c *C) {
buf := &bytes.Buffer{}
handler := NewStreamHandler(logger.LevelInfo, s.rawFormatter, buf)
handler.Handle(NewEntry(logger.LevelDebug, time.Now(), "hello"))
handler.Handle(NewEntry(logger.LevelInfo, time.Now(), "man"))
c.Assert(buf.String(), Equals, "man\n")
}
func (s *HandlersTestSuite) TestStreamHandlerCopy(c *C) {
buf := &bytes.Buffer{}
handler := NewStreamHandler(logger.LevelInfo, s.rawFormatter, buf)
c.Assert(handler.Copy(), Equals, handler)
}
func (s *HandlersTestSuite) TestBufferHandlerHandle(c *C) {
buf := &bytes.Buffer{}
streamHandler := NewStreamHandler(logger.LevelDebug, s.rawFormatter, buf)
handler := NewBufferHandler(streamHandler, logger.LevelWarning)
handler.Handle(NewEntry(logger.LevelDebug, time.Now(), "debug"))
handler.Handle(NewEntry(logger.LevelInfo, time.Now(), "info"))
handler.Handle(NewEntry(logger.LevelWarning, time.Now(), "warning"))
c.Assert(buf.String(), Equals, "debug\ninfo\nwarning\n")
}
func (s *HandlersTestSuite) TestBufferHandlerHandleLowLevel(c *C) {
buf := &bytes.Buffer{}
streamHandler := NewStreamHandler(logger.LevelDebug, s.rawFormatter, buf)
handler := NewBufferHandler(streamHandler, logger.LevelWarning)
handler.Handle(NewEntry(logger.LevelDebug, time.Now(), "debug"))
handler.Handle(NewEntry(logger.LevelInfo, time.Now(), "info"))
c.Assert(buf.String(), Equals, "")
}
func (s *HandlersTestSuite) TestBufferHandlerCopy(c *C) {
handler := &handlerForCopy{}
bufferHandler := NewBufferHandler(handler, logger.LevelWarning)
copy := bufferHandler.Copy().(*BufferHandler)
c.Assert(copy, Not(Equals), bufferHandler)
c.Assert(copy.flushLevel, Equals, bufferHandler.flushLevel)
c.Assert(copy.handler.(*handlerForCopy).original, Equals, handler)
}
func (s *HandlersTestSuite) TestMultiHandlerHandle(c *C) {
buf := &bytes.Buffer{}
streamHandler := NewStreamHandler(logger.LevelDebug, s.rawFormatter, buf)
buf2 := &bytes.Buffer{}
streamHandler2 := NewStreamHandler(logger.LevelInfo, s.rawFormatter, buf2)
handler := NewMultiHandler(streamHandler, streamHandler2)
handler.Handle(NewEntry(logger.LevelDebug, time.Now(), "debug"))
handler.Handle(NewEntry(logger.LevelInfo, time.Now(), "info"))
c.Assert(buf.String(), Equals, "debug\ninfo\n")
c.Assert(buf2.String(), Equals, "info\n")
}
func (s *HandlersTestSuite) TestMultiHandlerCopy(c *C) {
handler := &handlerForCopy{}
handler2 := &handlerForCopy{}
multiHandler := NewMultiHandler(handler, handler2)
copy := multiHandler.Copy().(*MultiHandler)
c.Assert(copy, Not(Equals), multiHandler)
c.Assert(copy.handlers[0].(*handlerForCopy).original, Equals, handler)
c.Assert(copy.handlers[1].(*handlerForCopy).original, Equals, handler2)
}