aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/go.uber.org/zap/array_test.go
blob: 97738c99ab55bbc142afe09c48fe5d65d433cd52 (plain) (blame)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package zap

import (
	"errors"
	"fmt"
	"testing"
	"time"

	"go.uber.org/zap/zapcore"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func BenchmarkBoolsArrayMarshaler(b *testing.B) {
	// Keep this benchmark here to capture the overhead of the ArrayMarshaler
	// wrapper.
	bs := make([]bool, 50)
	enc := zapcore.NewJSONEncoder(zapcore.EncoderConfig{})
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Bools("array", bs).AddTo(enc.Clone())
	}
}

func BenchmarkBoolsReflect(b *testing.B) {
	bs := make([]bool, 50)
	enc := zapcore.NewJSONEncoder(zapcore.EncoderConfig{})
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Reflect("array", bs).AddTo(enc.Clone())
	}
}

func TestArrayWrappers(t *testing.T) {
	tests := []struct {
		desc     string
		field    Field
		expected []interface{}
	}{
		{"empty bools", Bools("", []bool{}), []interface{}{}},
		{"empty byte strings", ByteStrings("", [][]byte{}), []interface{}{}},
		{"empty complex128s", Complex128s("", []complex128{}), []interface{}{}},
		{"empty complex64s", Complex64s("", []complex64{}), []interface{}{}},
		{"empty durations", Durations("", []time.Duration{}), []interface{}{}},
		{"empty float64s", Float64s("", []float64{}), []interface{}{}},
		{"empty float32s", Float32s("", []float32{}), []interface{}{}},
		{"empty ints", Ints("", []int{}), []interface{}{}},
		{"empty int64s", Int64s("", []int64{}), []interface{}{}},
		{"empty int32s", Int32s("", []int32{}), []interface{}{}},
		{"empty int16s", Int16s("", []int16{}), []interface{}{}},
		{"empty int8s", Int8s("", []int8{}), []interface{}{}},
		{"empty strings", Strings("", []string{}), []interface{}{}},
		{"empty times", Times("", []time.Time{}), []interface{}{}},
		{"empty uints", Uints("", []uint{}), []interface{}{}},
		{"empty uint64s", Uint64s("", []uint64{}), []interface{}{}},
		{"empty uint32s", Uint32s("", []uint32{}), []interface{}{}},
		{"empty uint16s", Uint16s("", []uint16{}), []interface{}{}},
		{"empty uint8s", Uint8s("", []uint8{}), []interface{}{}},
		{"empty uintptrs", Uintptrs("", []uintptr{}), []interface{}{}},
		{"bools", Bools("", []bool{true, false}), []interface{}{true, false}},
		{"byte strings", ByteStrings("", [][]byte{{1, 2}, {3, 4}}), []interface{}{"\x01\x02", "\x03\x04"}},
		{"complex128s", Complex128s("", []complex128{1 + 2i, 3 + 4i}), []interface{}{1 + 2i, 3 + 4i}},
		{"complex64s", Complex64s("", []complex64{1 + 2i, 3 + 4i}), []interface{}{complex64(1 + 2i), complex64(3 + 4i)}},
		{"durations", Durations("", []time.Duration{1, 2}), []interface{}{time.Nanosecond, 2 * time.Nanosecond}},
		{"float64s", Float64s("", []float64{1.2, 3.4}), []interface{}{1.2, 3.4}},
		{"float32s", Float32s("", []float32{1.2, 3.4}), []interface{}{float32(1.2), float32(3.4)}},
		{"ints", Ints("", []int{1, 2}), []interface{}{1, 2}},
		{"int64s", Int64s("", []int64{1, 2}), []interface{}{int64(1), int64(2)}},
		{"int32s", Int32s("", []int32{1, 2}), []interface{}{int32(1), int32(2)}},
		{"int16s", Int16s("", []int16{1, 2}), []interface{}{int16(1), int16(2)}},
		{"int8s", Int8s("", []int8{1, 2}), []interface{}{int8(1), int8(2)}},
		{"strings", Strings("", []string{"foo", "bar"}), []interface{}{"foo", "bar"}},
		{"times", Times("", []time.Time{time.Unix(0, 0), time.Unix(0, 0)}), []interface{}{time.Unix(0, 0), time.Unix(0, 0)}},
		{"uints", Uints("", []uint{1, 2}), []interface{}{uint(1), uint(2)}},
		{"uint64s", Uint64s("", []uint64{1, 2}), []interface{}{uint64(1), uint64(2)}},
		{"uint32s", Uint32s("", []uint32{1, 2}), []interface{}{uint32(1), uint32(2)}},
		{"uint16s", Uint16s("", []uint16{1, 2}), []interface{}{uint16(1), uint16(2)}},
		{"uint8s", Uint8s("", []uint8{1, 2}), []interface{}{uint8(1), uint8(2)}},
		{"uintptrs", Uintptrs("", []uintptr{1, 2}), []interface{}{uintptr(1), uintptr(2)}},
	}

	for _, tt := range tests {
		enc := zapcore.NewMapObjectEncoder()
		tt.field.Key = "k"
		tt.field.AddTo(enc)
		assert.Equal(t, tt.expected, enc.Fields["k"], "%s: unexpected map contents.", tt.desc)
		assert.Equal(t, 1, len(enc.Fields), "%s: found extra keys in map: %v", tt.desc, enc.Fields)
	}
}

func TestObjectsAndObjectValues(t *testing.T) {
	t.Parallel()

	tests := []struct {
		desc string
		give Field
		want []any
	}{
		{
			desc: "Objects/nil slice",
			give: Objects[*emptyObject]("", nil),
			want: []any{},
		},
		{
			desc: "ObjectValues/nil slice",
			give: ObjectValues[emptyObject]("", nil),
			want: []any{},
		},
		{
			desc: "ObjectValues/empty slice",
			give: ObjectValues("", []emptyObject{}),
			want: []any{},
		},
		{
			desc: "ObjectValues/single item",
			give: ObjectValues("", []emptyObject{
				{},
			}),
			want: []any{
				map[string]any{},
			},
		},
		{
			desc: "Objects/multiple different objects",
			give: Objects("", []*fakeObject{
				{value: "foo"},
				{value: "bar"},
				{value: "baz"},
			}),
			want: []any{
				map[string]any{"value": "foo"},
				map[string]any{"value": "bar"},
				map[string]any{"value": "baz"},
			},
		},
		{
			desc: "ObjectValues/multiple different objects",
			give: ObjectValues("", []fakeObject{
				{value: "foo"},
				{value: "bar"},
				{value: "baz"},
			}),
			want: []any{
				map[string]any{"value": "foo"},
				map[string]any{"value": "bar"},
				map[string]any{"value": "baz"},
			},
		},
	}

	for _, tt := range tests {
		tt := tt
		t.Run(tt.desc, func(t *testing.T) {
			t.Parallel()

			tt.give.Key = "k"

			enc := zapcore.NewMapObjectEncoder()
			tt.give.AddTo(enc)
			assert.Equal(t, tt.want, enc.Fields["k"])
		})
	}
}

type emptyObject struct{}

func (*emptyObject) MarshalLogObject(zapcore.ObjectEncoder) error {
	return nil
}

type fakeObject struct {
	value string
	err   error // marshaling error, if any
}

func (o *fakeObject) MarshalLogObject(enc zapcore.ObjectEncoder) error {
	enc.AddString("value", o.value)
	return o.err
}

func TestObjectsAndObjectValues_marshalError(t *testing.T) {
	t.Parallel()

	tests := []struct {
		desc    string
		give    Field
		want    []any
		wantErr string
	}{
		{
			desc: "Objects",
			give: Objects("", []*fakeObject{
				{value: "foo"},
				{value: "bar", err: errors.New("great sadness")},
				{value: "baz"}, // does not get marshaled
			}),
			want: []any{
				map[string]any{"value": "foo"},
				map[string]any{"value": "bar"},
			},
			wantErr: "great sadness",
		},
		{
			desc: "ObjectValues",
			give: ObjectValues("", []fakeObject{
				{value: "foo"},
				{value: "bar", err: errors.New("stuff failed")},
				{value: "baz"}, // does not get marshaled
			}),
			want: []any{
				map[string]any{"value": "foo"},
				map[string]any{"value": "bar"},
			},
			wantErr: "stuff failed",
		},
	}

	for _, tt := range tests {
		tt := tt
		t.Run(tt.desc, func(t *testing.T) {
			t.Parallel()

			tt.give.Key = "k"

			enc := zapcore.NewMapObjectEncoder()
			tt.give.AddTo(enc)

			require.Contains(t, enc.Fields, "k")
			assert.Equal(t, tt.want, enc.Fields["k"])

			// AddTo puts the error in a "%vError" field based on the name of the
			// original field.
			require.Contains(t, enc.Fields, "kError")
			assert.Equal(t, tt.wantErr, enc.Fields["kError"])
		})
	}
}

type stringerObject struct {
	value string
}

func (s stringerObject) String() string {
	return s.value
}

func TestStringers(t *testing.T) {
	t.Parallel()

	tests := []struct {
		desc string
		give Field
		want []any
	}{
		{
			desc: "Stringers",
			give: Stringers("", []stringerObject{
				{value: "foo"},
				{value: "bar"},
				{value: "baz"},
			}),
			want: []any{
				"foo",
				"bar",
				"baz",
			},
		},
		{
			desc: "Stringers with []fmt.Stringer",
			give: Stringers("", []fmt.Stringer{
				stringerObject{value: "foo"},
				stringerObject{value: "bar"},
				stringerObject{value: "baz"},
			}),
			want: []any{
				"foo",
				"bar",
				"baz",
			},
		},
	}

	for _, tt := range tests {
		tt := tt
		t.Run(tt.desc, func(t *testing.T) {
			t.Parallel()

			tt.give.Key = "k"

			enc := zapcore.NewMapObjectEncoder()
			tt.give.AddTo(enc)
			assert.Equal(t, tt.want, enc.Fields["k"])
		})
	}
}