-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger_test.go
135 lines (116 loc) · 2.71 KB
/
logger_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package logo
import (
"bytes"
"testing"
)
// TestNew checks if the logger is created correctly
func TestNew(t *testing.T) {
// Create buffers
rec1 := NewReceiver(&bytes.Buffer{}, "")
rec2 := NewReceiver(&bytes.Buffer{}, "")
rec3 := NewReceiver(&bytes.Buffer{}, "")
// Create the logger
l := NewLogger(rec1, rec2, rec3)
if len(l.Receivers) != 3 {
t.Error("Logger not created correctly")
}
if !l.Active {
t.Error("Logger wasn't set to active")
}
}
// TestNewSimple checks if a simple logger is created correctly
func TestNewSimple(t *testing.T) {
// Create the logger
l := NewSimpleLogger(&bytes.Buffer{}, WARN, "", true)
r := l.Receivers[0]
// Test stuff
if r.Level != WARN {
t.Error("Level wasn't assigned properly")
}
if !r.Color {
t.Error("Color wasn't assigned properly")
}
if !l.Active {
t.Error("Logger wasn't set to active")
}
}
// TestOnlyIfActive checks if it's only logged if logger is active
func TestOnlyIfActive(t *testing.T) {
buf := &bytes.Buffer{}
rec := NewReceiver(buf, "")
rec.Level = DEBUG
l := NewLogger(rec)
l.Active = false
l.Debug("This shouldn't be logged")
if buf.String() != "" {
t.Error("Buffer should be empty")
}
}
// TestSetLevel checks if the levels are set on all receivers
func TestSetLevel(t *testing.T) {
buf := &bytes.Buffer{}
rec1 := NewReceiver(buf, "")
rec1.Level = DEBUG
rec2 := NewReceiver(buf, "")
rec2.Level = ERROR
l := NewLogger(rec1, rec2)
l.SetLevel(INFO)
if rec1.Level != INFO || rec2.Level != INFO {
t.Error("Level is not set correctly")
}
}
// TestLogAll test if to all receivers is written
// TODO test Fatal level (os.Exit()-problem)
func TestLogLevels(t *testing.T) {
buf := &bytes.Buffer{}
rec := NewReceiver(buf, "")
rec.Level = DEBUG
l := NewLogger(rec)
// Test Debug level
l.Debug("Test")
if buf.String() == "" {
t.Errorf("Buffer shouldn't be empty")
}
buf.Reset()
// Test Info level
l.Info("Test")
if buf.String() == "" {
t.Errorf("Buffer shouldn't be empty")
}
buf.Reset()
// Test Warn level
l.Warn("Test")
if buf.String() == "" {
t.Errorf("Buffer shouldn't be empty")
}
buf.Reset()
// Test Error level
l.Error("Test")
if buf.String() == "" {
t.Errorf("Buffer shouldn't be empty")
}
buf.Reset()
// Test Debug level with format
l.Debugf("Test")
if buf.String() == "" {
t.Errorf("Buffer shouldn't be empty")
}
buf.Reset()
// Test Info level with format
l.Infof("Test")
if buf.String() == "" {
t.Errorf("Buffer shouldn't be empty")
}
buf.Reset()
// Test Warn level with format
l.Warnf("Test")
if buf.String() == "" {
t.Errorf("Buffer shouldn't be empty")
}
buf.Reset()
// Test Error level with format
l.Errorf("Test")
if buf.String() == "" {
t.Errorf("Buffer shouldn't be empty")
}
}