forked from samber/oops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkv_test.go
More file actions
270 lines (218 loc) · 8.08 KB
/
kv_test.go
File metadata and controls
270 lines (218 loc) · 8.08 KB
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
package oops
import (
"reflect"
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
const anErrorStr = "assert.AnError general error for testing"
func TestDereferencePointers(t *testing.T) {
is := assert.New(t)
ptr := func(v string) *string { return &v }
err := With("hello", "world").Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"hello": "world"}, err.Context())
err = With("hello", ptr("world")).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"hello": "world"}, err.Context())
err = With("hello", nil).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"hello": nil}, err.Context())
err = With("hello", (*int)(nil)).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"hello": nil}, err.Context())
err = With("hello", (***int)(nil)).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"hello": nil}, err.Context())
var i **int
err = With("hello", (***int)(&i)).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"hello": nil}, err.Context())
}
func TestDereferencePointerEdgeCases(t *testing.T) {
is := assert.New(t)
// Test deeply nested pointers (should not panic)
deepValue := "deep"
ptr1 := &deepValue
ptr2 := &ptr1
ptr3 := &ptr2
ptr4 := &ptr3
ptr5 := &ptr4
err := With("deep", ptr5).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"deep": "deep"}, err.Context())
// Test nil pointers at different levels
var nilPtr *string
err = With("nil1", nilPtr).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"nil1": nil}, err.Context())
var nilPtr2 **string
err = With("nil2", nilPtr2).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"nil2": nil}, err.Context())
// Test mixed nil and non-nil pointers
value := "test"
valuePtr := &value
mixedPtr := &valuePtr
err = With("mixed", mixedPtr).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"mixed": "test"}, err.Context())
// Test with different types
intValue := 42
intPtr := &intValue
err = With("int", intPtr).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"int": 42}, err.Context())
// Test with struct pointers
type testStruct struct {
Field string
}
structValue := testStruct{Field: "test"}
structPtr := &structValue
err = With("struct", structPtr).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"struct": testStruct{Field: "test"}}, err.Context())
}
func TestDereferencePointerSafety(t *testing.T) {
is := assert.New(t)
// Test with invalid reflect values (should not panic)
var invalidPtr unsafe.Pointer
err := With("invalid", invalidPtr).Errorf(anErrorStr).(OopsError) //nolint:govet
// Should handle gracefully without panic
is.NotNil(err)
// Test with function pointers
testFunc := func() string { return "test" }
err = With("func", &testFunc).Errorf(anErrorStr).(OopsError) //nolint:govet
// Should handle function pointers gracefully
is.NotNil(err)
// Test with channel pointers
ch := make(chan int)
err = With("channel", &ch).Errorf(anErrorStr).(OopsError) //nolint:govet
// Should handle channel pointers gracefully
is.NotNil(err)
// Test with slice pointers
slice := []int{1, 2, 3}
err = With("slice", &slice).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"slice": []int{1, 2, 3}}, err.Context())
// Test with map pointers
m := map[string]int{"a": 1, "b": 2}
err = With("map", &m).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"map": map[string]int{"a": 1, "b": 2}}, err.Context())
}
func TestDereferencePointerComplexTypes(t *testing.T) {
is := assert.New(t)
// Test with interface pointers
var iface interface{} = "interface_value"
ifacePtr := &iface
err := With("interface", ifacePtr).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"interface": "interface_value"}, err.Context())
// Test with array pointers
arr := [3]int{1, 2, 3}
arrPtr := &arr
err = With("array", arrPtr).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"array": [3]int{1, 2, 3}}, err.Context())
// Test with nested struct pointers
type nestedStruct struct {
Inner struct {
Value string
}
}
nested := nestedStruct{Inner: struct{ Value string }{Value: "nested"}}
nestedPtr := &nested
err = With("nested", nestedPtr).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"nested": nested}, err.Context())
}
func TestDereferencePointerWithDisabled(t *testing.T) {
is := assert.New(t)
// Save original setting
original := DereferencePointers
defer func() { DereferencePointers = original }()
// Test with dereferencing disabled
DereferencePointers = false
value := "test"
ptr := &value
err := With("test", ptr).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"test": ptr}, err.Context())
// Re-enable and test
DereferencePointers = true
err = With("test", ptr).Errorf(anErrorStr).(OopsError) //nolint:govet
is.EqualValues(map[string]any{"test": "test"}, err.Context())
}
func TestDereferencePointerMultipleValues(t *testing.T) {
is := assert.New(t)
// Test multiple pointer values in the same context
strValue := "string"
intValue := 42
boolValue := true
err := With(
"str", &strValue,
"int", &intValue,
"bool", &boolValue,
"nil", (*string)(nil),
).Errorf(anErrorStr).(OopsError) //nolint:govet
expected := map[string]any{
"str": "string",
"int": 42,
"bool": true,
"nil": nil,
}
is.EqualValues(expected, err.Context())
}
func TestDereferencePointerPanicPrevention(t *testing.T) {
is := assert.New(t)
// Test that the function doesn't panic with extremely deep nesting
// This would have caused a stack overflow in the original implementation
deepValue := "deep"
ptr1 := &deepValue
ptr2 := &ptr1
ptr3 := &ptr2
ptr4 := &ptr3
ptr5 := &ptr4
ptr6 := &ptr5
ptr7 := &ptr6
ptr8 := &ptr7
ptr9 := &ptr8
ptr10 := &ptr9
ptr11 := &ptr10 // This exceeds the 10-level limit
// This should not panic and should return the original value
err := With("very_deep", ptr11).Errorf(anErrorStr).(OopsError) //nolint:govet
is.NotNil(err)
// The context should contain the pointer as-is since it exceeds the depth limit
context := err.Context()
is.Contains(context, "very_deep")
// Test with invalid reflect values that could cause panics
var invalidValue interface{} = func() {} // Function type
err = With("invalid", &invalidValue).Errorf(anErrorStr).(OopsError) //nolint:govet
is.NotNil(err)
// Test with unexported fields that might cause panics when accessing
type unexportedStruct struct {
unexported string
}
unexported := unexportedStruct{unexported: "test"}
err = With("unexported", &unexported).Errorf(anErrorStr).(OopsError) //nolint:govet
is.NotNil(err)
// Test with nil interface
var nilInterface interface{} = nil
err = With("nil_interface", &nilInterface).Errorf(anErrorStr).(OopsError) //nolint:govet
is.NotNil(err)
}
func TestDereferencePointerRecursiveEdgeCases(t *testing.T) {
is := assert.New(t)
// Test with nil pointer
var nilPtr *string
result := dereferencePointerRecursive(reflect.ValueOf(nilPtr), 0)
is.Equal(nil, result)
// Test with max depth exceeded
value := "test"
ptr := &value
result2 := dereferencePointerRecursive(reflect.ValueOf(ptr), 100)
is.Equal(ptr, result2) // Should return the pointer itself
// Test with invalid reflect value
var invalid reflect.Value
result3 := dereferencePointerRecursive(invalid, 0)
is.Equal(nil, result3)
}
func TestLazyValueEvaluationEdgeCases(t *testing.T) {
is := assert.New(t)
// Test with nil value
result := lazyValueEvaluation(nil)
is.Equal(nil, result)
// Test with non-function value
result2 := lazyValueEvaluation("string")
is.Equal("string", result2)
// Test with function that returns error
fn := func() (string, error) {
return "test", assert.AnError
}
result3 := lazyValueEvaluation(fn)
// Should return function as-is when it returns error
is.Equal(reflect.Func, reflect.TypeOf(result3).Kind())
}