-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler_test.go
103 lines (95 loc) · 2.4 KB
/
handler_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
// Copyright 2013 Marc Weistroff. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package log
import (
"testing"
)
func TestIsHandling(t *testing.T) {
h := &Handler{Level: EMERGENCY}
if h.S(DEBUG) {
t.Error("handler should not handle DEBUG priority")
}
if h.S(INFO) {
t.Error("handler should not handle INFO priority")
}
if h.S(NOTICE) {
t.Error("handler should not handle EMERGENCY priority")
}
if h.S(WARNING) {
t.Error("handler should not handle WARNING priority")
}
if h.S(ERROR) {
t.Error("handler should not handle ERROR priority")
}
if h.S(CRITICAL) {
t.Error("handler should not handle CRITICAL priority")
}
if h.S(ALERT) {
t.Error("handler should not handle ALERT priority")
}
if !h.S(EMERGENCY) {
t.Error("handler should handle EMERGENCY priority")
}
h = &Handler{Level: DEBUG}
if !h.S(DEBUG) {
t.Error("handler should handle DEBUG priority")
}
if !h.S(INFO) {
t.Error("handler should handle INFO priority")
}
if !h.S(NOTICE) {
t.Error("handler should handle NOTICE priority")
}
if !h.S(WARNING) {
t.Error("handler should handle WARNING priority")
}
if !h.S(ERROR) {
t.Error("handler should handle ERROR priority")
}
if !h.S(CRITICAL) {
t.Error("handler should handle CRITICAL priority")
}
if !h.S(ALERT) {
t.Error("handler should handle ALERTY priority")
}
if !h.S(EMERGENCY) {
t.Error("handler should handle EMERGENCY priority")
}
}
func TestPushAndPopProcessor(t *testing.T) {
h := &Handler{}
p1 := NewProcessor(func(r *Record) {})
p2 := NewProcessor(func(r *Record) {})
h.PushProcessor(p1)
h.PushProcessor(p2)
if len(h.Processors) != 2 {
t.Error("number of processor should be equal to 2")
}
if h.Processors[0] != p2 {
t.Error("processor mismatch")
}
if h.Processors[1] != p1 {
t.Error("processor mismatch")
}
h.PopProcessor()
if len(h.Processors) != 1 {
t.Error("number of processor should be equal to 1")
}
if h.Processors[0] != p1 {
t.Error("processor mistmatch")
}
}
func TestPrepare(t *testing.T) {
h := &Handler{Level: DEBUG}
p1 := NewProcessor(func(r *Record) { r.Message = "p1" }) // Will be called last
p2 := NewProcessor(func(r *Record) { r.Message = "p2" })
h.PushProcessor(p1)
h.PushProcessor(p2)
h.SetFormatter(&LineFormatter{LineFormat: "%message%"})
r := &Record{Message: "original"}
h.Prepare(r)
if r.Formatted != "p1" {
t.Error(r.Formatted)
}
}