-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_test.go
213 lines (157 loc) · 3.96 KB
/
write_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package timber
import (
"bytes"
"errors"
"fmt"
"regexp"
"strings"
"testing"
)
func TestShouldHaveMessage(t *testing.T) {
var buf bytes.Buffer
err := New(&buf, LevelAll, "", FlagNone)
if err != nil {
t.Fatal(err)
}
Write(LevelError, "test message")
if len(buf.Bytes()) == 0 {
t.Errorf("should have message, got: empty")
}
}
func TestShouldNotHaveMessage1(t *testing.T) {
var buf bytes.Buffer
err := New(&buf, LevelCritical, "", FlagNone)
if err != nil {
t.Fatal(err)
}
Write(LevelError, "test message")
if len(buf.Bytes()) > 1 {
t.Errorf("should not have message, got: %s", buf.String())
}
}
func TestShouldNotHaveMessage2(t *testing.T) {
var buf bytes.Buffer
err := New(&buf, LevelAll, "", FlagNone)
if err != nil {
t.Fatal(err)
}
Write(LevelSilent, "test message")
if len(buf.Bytes()) > 1 {
t.Errorf("should not have message, got: %s", buf.String())
}
}
func TestShouldBeColorful(t *testing.T) {
var buf bytes.Buffer
err := New(&buf, LevelAll, "[]", FlagColorful)
if err != nil {
t.Fatal(err)
}
Write(LevelError, "test message")
exemplar := []byte(fmt.Sprintf("%s[] %stest message%s\n", colorTime, colorError, colorReset))
if !bytes.Equal(buf.Bytes(), exemplar) {
t.Errorf("should be red, got: %s", buf.String())
}
}
func TestShouldNotBeColorful(t *testing.T) {
var buf bytes.Buffer
err := New(&buf, LevelAll, "", FlagNone)
if err != nil {
t.Fatal(err)
}
Write(LevelError, "test message")
if buf.String() != "test message\n" {
t.Errorf("should not have color, got: %s", buf.String())
}
}
func TestShouldHaveFilename(t *testing.T) {
var buf bytes.Buffer
err := New(&buf, LevelAll, "", FlagFileName)
if err != nil {
t.Fatal(err)
}
Write(LevelError, "test message")
if !strings.Contains(buf.String(), "write_test.go") {
t.Errorf("should have filename, got: %s", buf.String())
}
}
func TestShouldHaveTitle(t *testing.T) {
var buf bytes.Buffer
err := New(&buf, LevelAll, "", FlagTitle)
if err != nil {
t.Fatal(err)
}
Write(LevelError, "test message")
if !strings.Contains(buf.String(), "ERROR") {
t.Errorf("should have title, got: %s", buf.String())
}
}
func TestShouldNotHaveTitle(t *testing.T) {
var buf bytes.Buffer
err := New(&buf, LevelAll, "", FlagNone)
if err != nil {
t.Fatal(err)
}
Write(LevelError, "test message")
if strings.Contains(buf.String(), "ERROR") {
t.Errorf("should not have title, got: %s", buf.String())
}
}
func TestShouldHaveTimestamp(t *testing.T) {
var buf bytes.Buffer
err := New(&buf, LevelAll, "[15:04:05]", FlagNone)
if err != nil {
t.Fatal(err)
}
Write(LevelError, "test message")
re := regexp.MustCompile("[0-9]{2}:[0-9]{2}:[0-9]{2}")
if len(re.Find(buf.Bytes())) == 0 {
t.Errorf("should have timestamp, got: %s", buf.String())
}
}
func TestShouldNotHaveTimestamp(t *testing.T) {
var buf bytes.Buffer
err := New(&buf, LevelAll, "", FlagNone)
if err != nil {
t.Fatal(err)
}
Write(LevelError, "test message")
if buf.String() != "test message\n" {
t.Errorf("should not have timestamp, got: %s", buf.String())
}
}
func TestShouldReportNothingToLog(t *testing.T) {
var buf bytes.Buffer
err := New(&buf, LevelAll, "", FlagNone)
if err != nil {
t.Fatal(err)
}
Write(LevelError, "")
if buf.String() != "nothing to log\n" {
t.Errorf("should report nothing to log, got: %s", buf.String())
}
}
func TestShouldWarnWithoutWriters(t *testing.T) {
var buf bytes.Buffer
writers = []options{}
defaultWriter = &buf
Write(LevelError, "test message")
if !strings.Contains(buf.String(), "timber.Write Error:") {
t.Errorf("should report error, got: %s", buf.String())
}
}
type errorWriter struct{}
func (w errorWriter) Write(p []byte) (n int, err error) {
return 0, errors.New("some error occurred")
}
func TestShouldPanicWriteError(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("did not panic")
}
}()
err := New(errorWriter{}, 8, "", FlagNone)
if err != nil {
t.Fatal(err)
}
Write(LevelError, "test message")
}