-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_test.go
122 lines (110 loc) · 2.26 KB
/
helper_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
package golog
import (
"bytes"
"io"
"testing"
)
func testHelperKey(helperFunc func(logger Logger, key string) *Helper, t *testing.T) {
t.Helper()
tests := []struct {
name string
call func(helper *Helper)
want string
}{
{
name: "debug",
call: func(helper *Helper) {
helper.Debug(1, "2", 3)
},
want: `DEBUG, "log": "123"`,
},
{
name: "debugf",
call: func(helper *Helper) {
helper.Debugf("%d %d %d", 1, 2, 3)
},
want: `DEBUG, "log": "1 2 3"`,
},
{
name: "info",
call: func(helper *Helper) {
helper.Info(1, "2", 3)
},
want: `INFO, "log": "123"`,
},
{
name: "infof",
call: func(helper *Helper) {
helper.Infof("%d %d %d", 1, 2, 3)
},
want: `INFO, "log": "1 2 3"`,
},
{
name: "warn",
call: func(helper *Helper) {
helper.Warn(1, "2", 3)
},
want: `WARN, "log": "123"`,
},
{
name: "warnf",
call: func(helper *Helper) {
helper.Warnf("%d %d %d", 1, 2, 3)
},
want: `WARN, "log": "1 2 3"`,
},
{
name: "error",
call: func(helper *Helper) {
helper.Error(1, "2", 3)
},
want: `ERROR, "log": "123"`,
},
{
name: "errorf",
call: func(helper *Helper) {
helper.Errorf("%d %d %d", 1, 2, 3)
},
want: `ERROR, "log": "1 2 3"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
logger := NewStdLogger(&buf)
helper := helperFunc(logger, "log")
tt.call(helper)
want := tt.want + "\n"
if got := buf.String(); got != want {
t.Errorf("buf.String() = %q want %q", got, want)
}
})
}
}
// Test that NewHelper properly record logs.
func TestNewHelper(t *testing.T) {
t.Parallel()
testHelperKey(func(logger Logger, key string) *Helper {
return NewHelper(logger, MessageKey(key))
}, t)
}
// Test that WithKey properly record logs.
func TestHelperWithKey(t *testing.T) {
t.Parallel()
testHelperKey(func(logger Logger, key string) *Helper {
helper := NewHelper(logger)
return helper.WithKey(key)
}, t)
}
func BenchmarkHelperPrint(b *testing.B) {
log := NewHelper(NewStdLogger(io.Discard))
for i := 0; i < b.N; i++ {
log.Debug("test")
}
}
func BenchmarkHelperPrintf(b *testing.B) {
log := NewHelper(NewStdLogger(io.Discard))
for i := 0; i < b.N; i++ {
log.Debugf("%s", "test")
}
}