-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmap_index_key_test.go
More file actions
341 lines (312 loc) · 7.76 KB
/
Copy pathmap_index_key_test.go
File metadata and controls
341 lines (312 loc) · 7.76 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package jsonpointer
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/speakeasy-api/openapi/marshaller/tests"
"github.com/speakeasy-api/openapi/sequencedmap"
)
func TestMapIntegerKeys_Success(t *testing.T) {
t.Parallel()
tests := []struct {
name string
data interface{}
jsonPointer string
expected interface{}
}{
{
name: "integer key in map[string]interface{}",
data: map[string]interface{}{
"200": "success response",
"404": "not found",
"500": "server error",
},
jsonPointer: "/200",
expected: "success response",
},
{
name: "integer key with nested path in map[string]interface{}",
data: map[string]interface{}{
"responses": map[string]interface{}{
"200": map[string]interface{}{
"description": "OK",
"content": map[string]interface{}{
"application/json": map[string]interface{}{
"schema": map[string]interface{}{
"type": "object",
},
},
},
},
},
},
jsonPointer: "/responses/200/description",
expected: "OK",
},
{
name: "integer key mixed with string keys",
data: map[string]interface{}{
"paths": map[string]interface{}{
"/users": map[string]interface{}{
"get": map[string]interface{}{
"responses": map[string]interface{}{
"200": map[string]interface{}{
"description": "List of users",
},
"400": map[string]interface{}{
"description": "Bad request",
},
},
},
},
},
},
jsonPointer: "/paths/~1users/get/responses/200/description",
expected: "List of users",
},
{
name: "integer key in map[int]interface{}",
data: map[int]interface{}{
200: "success",
404: "not found",
500: "server error",
},
jsonPointer: "/200",
expected: "success",
},
{
name: "integer key in nested map[int]interface{}",
data: map[string]interface{}{
"statusCodes": map[int]interface{}{
200: "OK",
404: "Not Found",
500: "Internal Server Error",
},
},
jsonPointer: "/statusCodes/200",
expected: "OK",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result, err := GetTarget(tt.data, JSONPointer(tt.jsonPointer))
require.NoError(t, err, "should find the target successfully")
assert.Equal(t, tt.expected, result, "should return the expected value")
})
}
}
// TestStruct implements KeyNavigable and IndexNavigable interfaces
type TestStruct struct {
data map[string]interface{}
}
// NavigateWithKey implements KeyNavigable interface
func (ts *TestStruct) NavigateWithKey(key string) (interface{}, error) {
val, exists := ts.data[key]
if !exists {
return nil, ErrNotFound
}
return val, nil
}
// NavigateWithIndex implements IndexNavigable interface
func (ts *TestStruct) NavigateWithIndex(index int) (interface{}, error) {
if index < 0 || index >= len(ts.data) {
return nil, ErrNotFound
}
// For test purposes, convert index to string key
val, exists := ts.data[string(rune('0'+index))]
if !exists {
return nil, ErrNotFound
}
return val, nil
}
func TestStructKeyNavigableIndexNavigable_Success(t *testing.T) {
t.Parallel()
testStruct := &TestStruct{
data: map[string]interface{}{
"200": "success response",
"404": "not found",
"0": "first item",
"1": "second item",
},
}
tests := []struct {
name string
data interface{}
jsonPointer string
expected interface{}
}{
{
name: "integer key should try key navigation first",
data: testStruct,
jsonPointer: "/200",
expected: "success response",
},
{
name: "integer key should fallback to index navigation if key fails",
data: testStruct,
jsonPointer: "/0",
expected: "first item",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result, err := GetTarget(tt.data, JSONPointer(tt.jsonPointer))
require.NoError(t, err, "should find the target successfully")
assert.Equal(t, tt.expected, result, "should return the expected value")
})
}
}
func TestEmbeddedMapModel_Success(t *testing.T) {
t.Parallel()
// Test with the existing TestEmbeddedMapHighModel
embeddedMap := sequencedmap.New[string, string]()
embeddedMap.Set("200", "success status")
embeddedMap.Set("404", "not found status")
embeddedMap.Set("data", "some data")
model := &tests.TestEmbeddedMapHighModel{
Map: embeddedMap,
}
tests := []struct {
name string
data interface{}
jsonPointer string
expected interface{}
}{
{
name: "integer key in embedded map",
data: model,
jsonPointer: "/200",
expected: "success status",
},
{
name: "integer key in nested embedded map",
data: model,
jsonPointer: "/404",
expected: "not found status",
},
{
name: "regular string key in embedded map",
data: model,
jsonPointer: "/data",
expected: "some data",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result, err := GetTarget(tt.data, JSONPointer(tt.jsonPointer))
require.NoError(t, err, "should find the target successfully")
assert.Equal(t, tt.expected, result, "should return the expected value")
})
}
}
func TestEdgeCases_Success(t *testing.T) {
t.Parallel()
tests := []struct {
name string
data interface{}
jsonPointer string
expected interface{}
}{
{
name: "zero integer key",
data: map[string]interface{}{
"0": "zero value",
},
jsonPointer: "/0",
expected: "zero value",
},
{
name: "negative integer key as string",
data: map[string]interface{}{
"-1": "negative value",
},
jsonPointer: "/-1",
expected: "negative value",
},
{
name: "large integer key",
data: map[string]interface{}{
"999999": "large number",
},
jsonPointer: "/999999",
expected: "large number",
},
{
name: "integer key that looks like array index but is a map key",
data: map[string]interface{}{
"items": map[string]interface{}{
"0": "map zero",
"1": "map one",
},
},
jsonPointer: "/items/0",
expected: "map zero",
},
{
name: "both array and map with same integer key",
data: map[string]interface{}{
"mixed": map[string]interface{}{
"0": "map zero",
"array": []string{"array zero", "array one"},
},
},
jsonPointer: "/mixed/0",
expected: "map zero", // Should prefer key over index
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result, err := GetTarget(tt.data, JSONPointer(tt.jsonPointer))
require.NoError(t, err, "should find the target successfully")
assert.Equal(t, tt.expected, result, "should return the expected value")
})
}
}
func TestMapIntegerKeys_Error(t *testing.T) {
t.Parallel()
tests := []struct {
name string
data interface{}
jsonPointer string
expectError bool
}{
{
name: "non-existent integer key",
data: map[string]interface{}{
"200": "success",
"404": "not found",
},
jsonPointer: "/500",
expectError: true,
},
{
name: "invalid path with integer key",
data: map[string]interface{}{
"200": "success",
},
jsonPointer: "/200/invalid",
expectError: true,
},
{
name: "integer key on non-navigable type",
data: "string value",
jsonPointer: "/200",
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
_, err := GetTarget(tt.data, JSONPointer(tt.jsonPointer))
if tt.expectError {
require.Error(t, err, "should return an error for invalid path")
} else {
require.NoError(t, err, "should not return an error")
}
})
}
}