-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_test.go
97 lines (89 loc) · 1.83 KB
/
io_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
package say
import (
"reflect"
"testing"
)
func TestCapturePanic(t *testing.T) {
expect(t, func() {
defer CapturePanic()
panic("oops")
}, []string{
"FATAL oops",
})
}
func TestLoggerCapturePanic(t *testing.T) {
expect(t, func() {
log := NewLogger(SkipStackFrames(-1))
defer log.CapturePanic()
panic("oops")
}, []string{
"FATAL oops",
})
}
func TestFlush(t *testing.T) {
received := false
SetListener(func(msg *Message) {
received = true
})
defer SetListener(nil)
Info("test")
Flush()
if !received {
t.Error("Message not received")
}
}
func TestFlushNoListener(t *testing.T) {
Flush()
}
func TestSetListener(t *testing.T) {
content := "hello"
processed := false
SetListener(func(msg *Message) {
if msg.Type != TypeInfo {
t.Errorf("Invalid message type, got %s, want %s",
msg.Type, TypeInfo)
}
if msg.Content != content {
t.Errorf("Invalid message type, got %s, want %s",
msg.Content, content)
}
wantData := Data{{Key: "int", Value: 42}}
if !reflect.DeepEqual(msg.Data, wantData) {
t.Errorf("Invalid message data, got %#v, want %#v",
msg.Data, wantData)
}
processed = true
})
defer SetListener(nil)
Info(content, "int", 42)
Flush()
if !processed {
t.Error("Message not processed")
}
}
func TestPanicSetListener(t *testing.T) {
content := "oops"
processed := false
SetListener(func(msg *Message) {
if msg.Type != TypeFatal {
t.Errorf("Invalid message type, got %s, want %s",
msg.Type, TypeFatal)
}
if msg.Content != content {
t.Errorf("Invalid message type, got %s, want %s",
msg.Content, content)
}
if len(msg.Data) != 0 {
t.Errorf("Invalid message data, got %#v, want empty", msg.Data)
}
processed = true
})
defer func() {
SetListener(nil)
if !processed {
t.Error("Message not processed")
}
}()
defer CapturePanic()
panic(content)
}