-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestx.go
182 lines (150 loc) · 3.83 KB
/
testx.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
package testx
import (
"fmt"
"runtime"
"strings"
"testing"
)
type Wrapper func(tb testing.TB) testing.TB
func Wrap(tb testing.TB, wrappers ...Wrapper) testing.TB {
for _, w := range wrappers {
tb = w(tb)
}
return tb
}
type LoggingMethodSwitch struct {
Error bool
Errorf bool
Fatal bool
Fatalf bool
Log bool
Logf bool
}
type LoggingOptions struct {
DisableMethods LoggingMethodSwitch
}
type logger struct {
testing.TB
opts LoggingOptions
format func(method func(...any), args ...any)
formatf func(method func(string, ...any), format string, args ...any)
}
func (l *logger) Log(args ...any) {
if l.opts.DisableMethods.Log {
l.TB.Log(args...)
return
}
l.format(l.TB.Log, args...)
}
func (l *logger) Logf(format string, args ...any) {
if l.opts.DisableMethods.Logf {
l.TB.Logf(format, args...)
return
}
l.formatf(l.TB.Logf, format, args...)
}
func (l *logger) Error(args ...any) {
if l.opts.DisableMethods.Error {
l.TB.Error(args...)
return
}
l.format(l.TB.Error, args...)
}
func (l *logger) Errorf(format string, args ...any) {
if l.opts.DisableMethods.Errorf {
l.TB.Errorf(format, args...)
return
}
l.formatf(l.TB.Errorf, format, args...)
}
func (l *logger) Fatal(args ...any) {
if l.opts.DisableMethods.Fatal {
l.TB.Fatal(args...)
return
}
l.format(l.TB.Fatal, args...)
}
func (l *logger) Fatalf(format string, args ...any) {
if l.opts.DisableMethods.Fatalf {
l.TB.Fatalf(format, args...)
return
}
l.formatf(l.TB.Fatalf, format, args...)
}
type PrefixLoggingOptions struct {
DisableMethods LoggingMethodSwitch
Prefix string
PrefixFunc func() string
}
func WithPrefixLogging(opts PrefixLoggingOptions) Wrapper {
return func(tb testing.TB) testing.TB {
return newPrefixLogger(tb, opts)
}
}
func Prefix(tb testing.TB, prefix string) testing.TB {
return newPrefixLogger(tb, PrefixLoggingOptions{
Prefix: prefix,
})
}
func Prefixf(tb testing.TB, format string, args ...any) testing.TB {
return Prefix(tb, fmt.Sprintf(format, args...))
}
func newPrefixLogger(tb testing.TB, opts PrefixLoggingOptions) *logger {
var prefixFunc func() string
switch {
case opts.Prefix == "" && opts.PrefixFunc == nil:
tb.Fatal("must specify either Prefix or PrefixFunc")
case opts.Prefix != "" && opts.PrefixFunc == nil:
prefixFunc = func() string { return opts.Prefix }
case opts.Prefix == "" && opts.PrefixFunc != nil:
prefixFunc = opts.PrefixFunc
default:
tb.Fatal("cannot specify both Prefix and PrefixFunc")
}
return &logger{
TB: tb,
opts: LoggingOptions{DisableMethods: opts.DisableMethods},
format: func(method func(...any), args ...any) {
method(append([]any{prefixFunc()}, args...)...)
},
formatf: func(method func(string, ...any), format string, args ...any) {
method(prefixFunc() + " " + fmt.Sprintf(format, args...))
},
}
}
type StackTraceLoggingOptions struct {
DisableMethods LoggingMethodSwitch
BufferSize int
SkipLines int
All bool
}
func WithStackTraceLogging(opts StackTraceLoggingOptions) Wrapper {
return func(tb testing.TB) testing.TB {
return newStackTraceLogger(tb, opts)
}
}
func newStackTraceLogger(tb testing.TB, opts StackTraceLoggingOptions) *logger {
stack := func() string {
buf := make([]byte, opts.BufferSize)
runtime.Stack(buf, opts.All)
if skip := opts.SkipLines; skip > 0 {
lines := strings.Split(string(buf), "\n")
if skip >= len(lines) {
skip = len(lines) - 1
}
lines = lines[skip:]
buf = []byte(strings.Join(lines, "\n"))
}
return fmt.Sprintf("\nstack: %s", string(buf))
}
return &logger{
TB: tb,
opts: LoggingOptions{DisableMethods: opts.DisableMethods},
format: func(method func(...any), args ...any) {
method(append(args, stack())...)
},
formatf: func(method func(string, ...any), format string, args ...any) {
method(fmt.Sprintf(format, args...) + stack())
},
}
}