-
Notifications
You must be signed in to change notification settings - Fork 12
/
std_test.go
125 lines (118 loc) · 3.22 KB
/
std_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
package xlog
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGlobalLogger(t *testing.T) {
o := newTestOutput()
oldStd := std
defer func() { std = oldStd }()
SetLogger(New(Config{Output: o}))
Debug("test")
last := o.get()
assert.Equal(t, "test", last["message"])
assert.Equal(t, "debug", last["level"])
o.reset()
Debugf("test")
last = o.get()
assert.Equal(t, "test", last["message"])
assert.Equal(t, "debug", last["level"])
o.reset()
Info("test")
last = o.get()
assert.Equal(t, "test", last["message"])
assert.Equal(t, "info", last["level"])
o.reset()
Infof("test")
last = o.get()
assert.Equal(t, "test", last["message"])
assert.Equal(t, "info", last["level"])
o.reset()
Warn("test")
last = o.get()
assert.Equal(t, "test", last["message"])
assert.Equal(t, "warn", last["level"])
o.reset()
Warnf("test")
last = o.get()
assert.Equal(t, "test", last["message"])
assert.Equal(t, "warn", last["level"])
o.reset()
Error("test")
last = o.get()
assert.Equal(t, "test", last["message"])
assert.Equal(t, "error", last["level"])
o.reset()
Errorf("test")
last = o.get()
assert.Equal(t, "test", last["message"])
assert.Equal(t, "error", last["level"])
o.reset()
oldExit := exit1
exit1 = func() {}
defer func() { exit1 = oldExit }()
Fatal("test")
last = o.get()
assert.Equal(t, "test", last["message"])
assert.Equal(t, "fatal", last["level"])
o.reset()
Fatalf("test")
last = o.get()
assert.Equal(t, "test", last["message"])
assert.Equal(t, "fatal", last["level"])
o.reset()
}
func TestStdError(t *testing.T) {
o := newTestOutput()
oldStd := std
defer func() { std = oldStd }()
SetLogger(New(Config{Output: o}))
Error("test", F{"foo": "bar"})
last := <-o.w
assert.Contains(t, last["file"], "std_test.go:")
delete(last, "file")
assert.Equal(t, map[string]interface{}{"time": fakeNow, "level": "error", "message": "test", "foo": "bar"}, last)
}
func TestStdErrorf(t *testing.T) {
o := newTestOutput()
oldStd := std
defer func() { std = oldStd }()
SetLogger(New(Config{Output: o}))
Errorf("test %d%v", 1, F{"foo": "bar"})
last := <-o.w
assert.Contains(t, last["file"], "std_test.go:")
delete(last, "file")
assert.Equal(t, map[string]interface{}{"time": fakeNow, "level": "error", "message": "test 1", "foo": "bar"}, last)
}
func TestStdFatal(t *testing.T) {
e := exit1
exited := 0
exit1 = func() { exited++ }
defer func() { exit1 = e }()
o := newTestOutput()
oldStd := std
defer func() { std = oldStd }()
SetLogger(New(Config{Output: o}))
Fatal("test", F{"foo": "bar"})
last := <-o.w
assert.Contains(t, last["file"], "std_test.go:")
delete(last, "file")
assert.Equal(t, map[string]interface{}{"time": fakeNow, "level": "fatal", "message": "test", "foo": "bar"}, last)
assert.Equal(t, 1, exited)
}
func TestStdFatalf(t *testing.T) {
e := exit1
exited := 0
exit1 = func() { exited++ }
defer func() { exit1 = e }()
o := newTestOutput()
oldStd := std
defer func() { std = oldStd }()
SetLogger(New(Config{Output: o}))
Fatalf("test %d%v", 1, F{"foo": "bar"})
last := <-o.w
assert.Contains(t, last["file"], "std_test.go:")
delete(last, "file")
assert.Equal(t, map[string]interface{}{"time": fakeNow, "level": "fatal", "message": "test 1", "foo": "bar"}, last)
assert.Equal(t, 1, exited)
}