/
std_test.go
254 lines (234 loc) · 5.88 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package logger
import (
"bytes"
"context"
"fmt"
"log"
"os"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func Test_stdErrLogger(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
t.Cleanup(func() { log.SetOutput(os.Stderr) })
type args struct {
v []any
v2 any
}
tests := []struct {
name string
args args
attributes map[string]any
wantDebug string
wantDebugf string
wantInfo string
wantInfof string
wantWarn string
wantWarnf string
wantError string
wantErrorf string
}{
{
name: "Test 1",
args: args{
v: []any{"Message"},
v2: "Message",
},
attributes: map[string]any{"test_key_1": "test_value_1", "test_key_2": "test_value_2"},
wantDebug: "DEBUG: Message",
wantDebugf: "DEBUG: Formatted Message",
wantInfo: "INFO : Message",
wantInfof: "INFO : Formatted Message",
wantWarn: "WARN : Message",
wantWarnf: "WARN : Formatted Message",
wantError: "ERROR: Message",
wantErrorf: "ERROR: Formatted Message",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
l := &stdErrLogger{attributes: tt.attributes}
format := "Formatted %s"
verifyLog := func(log, methodName, expectedPrefix string) {
if !strings.HasPrefix(log, expectedPrefix) {
t.Errorf("stdErrLogger.%s() = %q, missing prefix %q", methodName, log, expectedPrefix)
}
for k, v := range tt.attributes {
attrStr := fmt.Sprintf("%s=%v", k, v)
if !strings.Contains(log, attrStr) {
t.Errorf("stdErrLogger.%s() missing attribute %s", methodName, attrStr)
}
}
if !strings.HasSuffix(log, "\n") {
t.Errorf("stdErrLogger.%s() = %q, missing suffix \\n", methodName, log)
}
}
l.Debug(ctx, tt.args.v2)
verifyLog(buf.String()[20:], "Debug", tt.wantDebug)
buf.Reset()
l.Debugf(ctx, format, tt.args.v...)
verifyLog(buf.String()[20:], "Debugf", tt.wantDebugf)
buf.Reset()
l.Info(ctx, tt.args.v2)
verifyLog(buf.String()[20:], "Info", tt.wantInfo)
buf.Reset()
l.Infof(ctx, format, tt.args.v...)
verifyLog(buf.String()[20:], "Infof", tt.wantInfof)
buf.Reset()
l.Warn(ctx, tt.args.v2)
verifyLog(buf.String()[20:], "Warn", tt.wantWarn)
buf.Reset()
l.Warnf(ctx, format, tt.args.v...)
verifyLog(buf.String()[20:], "Warnf", tt.wantWarnf)
buf.Reset()
l.Error(ctx, tt.args.v2)
verifyLog(buf.String()[20:], "Error", tt.wantError)
buf.Reset()
l.Errorf(ctx, format, tt.args.v...)
verifyLog(buf.String()[20:], "Errorf", tt.wantErrorf)
buf.Reset()
})
}
}
func Test_stdErrLogger_WithAttributes(t *testing.T) {
t.Parallel()
tests := []struct {
name string
attributes map[string]any
want *stdAttributer
}{
{
name: "with attributes success",
attributes: map[string]any{
"test_key_1": "test_value_1",
"test_key_2": "test_value_2",
},
want: &stdAttributer{
attributes: map[string]any{
"test_key_1": "test_value_1",
"test_key_2": "test_value_2",
},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
l := &stdErrLogger{
attributes: tt.attributes,
}
got := l.WithAttributes()
if diff := cmp.Diff(got, tt.want, cmp.AllowUnexported(stdAttributer{}), cmpopts.IgnoreFields(stdAttributer{}, "logger")); diff != "" {
t.Errorf("stdErrLogger.WithAttributes() mismatch (-want +got):\n%s", diff)
}
if a, ok := got.(*stdAttributer); !ok {
t.Errorf("stdErrLogger.WithAttributes() type %T, want %T", got, &stdAttributer{})
} else if a.logger != l {
t.Errorf("stdErrLogger.WithAttributes().logger != stdErrLogger")
}
})
}
}
func Test_stdAttributer_AddAttribute(t *testing.T) {
t.Parallel()
type args struct {
key string
value any
}
tests := []struct {
name string
args args
attributes map[string]any
want map[string]any
}{
{
name: "success adding attribute",
args: args{
key: "test_key_0",
value: "test_value_0",
},
attributes: map[string]any{
"test_key_1": 1,
"test_key_2": "test_value_2",
},
want: map[string]any{
"test_key_1": 1,
"test_key_2": "test_value_2",
"test_key_0": "test_value_0",
},
},
{
name: "success overwriting attribute",
args: args{
key: "test_key_1",
value: 512,
},
attributes: map[string]any{
"test_key_1": 1,
"test_key_2": "test_value_2",
},
want: map[string]any{
"test_key_1": 512,
"test_key_2": "test_value_2",
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
a := &stdAttributer{
attributes: tt.attributes,
logger: &stdErrLogger{},
}
a.AddAttribute(tt.args.key, tt.args.value)
if diff := cmp.Diff(a.attributes, tt.want); diff != "" {
t.Errorf("stdAttributer.AddAttribute() mismatch (-want +got):\n%s", diff)
}
})
}
}
func Test_stdAttributer_Logger(t *testing.T) {
t.Parallel()
type fields struct {
logger *stdErrLogger
attributes map[string]any
}
tests := []struct {
name string
fields
want *stdErrLogger
}{
{
name: "success getting logger",
fields: fields{
logger: &stdErrLogger{
attributes: map[string]any{"test_key_1": "test_value_1", "test_key_2": "test_value_2"},
},
attributes: map[string]any{"test_key_3": "test_value_3", "test_key_4": "test_value_4"},
},
want: &stdErrLogger{
attributes: map[string]any{"test_key_3": "test_value_3", "test_key_4": "test_value_4"},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
a := &stdAttributer{
logger: tt.fields.logger,
attributes: tt.fields.attributes,
}
got := a.Logger()
if diff := cmp.Diff(got, tt.want, cmp.AllowUnexported(stdErrLogger{})); diff != "" {
t.Errorf("stdAttributer.Logger() mismatch (-want +got):\n%s", diff)
}
})
}
}