-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathutils_test.go
More file actions
573 lines (514 loc) · 11.5 KB
/
utils_test.go
File metadata and controls
573 lines (514 loc) · 11.5 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package testutils
import (
"iter"
"testing"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yaml.in/yaml/v4"
)
func TestCreateStringYamlNode_Success(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value string
line int
column int
wantValue *yaml.Node
}{
{
name: "creates string node",
value: "test",
line: 1,
column: 2,
wantValue: &yaml.Node{
Value: "test",
Kind: yaml.ScalarNode,
Tag: "!!str",
Line: 1,
Column: 2,
},
},
{
name: "creates empty string node",
value: "",
line: 3,
column: 4,
wantValue: &yaml.Node{
Value: "",
Kind: yaml.ScalarNode,
Tag: "!!str",
Line: 3,
Column: 4,
},
},
{
name: "creates string with special characters",
value: "hello\nworld",
line: 5,
column: 6,
wantValue: &yaml.Node{
Value: "hello\nworld",
Kind: yaml.ScalarNode,
Tag: "!!str",
Line: 5,
Column: 6,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := CreateStringYamlNode(tt.value, tt.line, tt.column)
assert.Equal(t, tt.wantValue, result, "should create correct string YAML node")
})
}
}
func TestCreateIntYamlNode_Success(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value int
line int
column int
wantValue *yaml.Node
}{
{
name: "creates positive int node",
value: 42,
line: 1,
column: 2,
wantValue: &yaml.Node{
Value: "42",
Kind: yaml.ScalarNode,
Tag: "!!int",
Line: 1,
Column: 2,
},
},
{
name: "creates zero int node",
value: 0,
line: 3,
column: 4,
wantValue: &yaml.Node{
Value: "0",
Kind: yaml.ScalarNode,
Tag: "!!int",
Line: 3,
Column: 4,
},
},
{
name: "creates negative int node",
value: -100,
line: 5,
column: 6,
wantValue: &yaml.Node{
Value: "-100",
Kind: yaml.ScalarNode,
Tag: "!!int",
Line: 5,
Column: 6,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := CreateIntYamlNode(tt.value, tt.line, tt.column)
assert.Equal(t, tt.wantValue, result, "should create correct int YAML node")
})
}
}
func TestCreateBoolYamlNode_Success(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value bool
line int
column int
wantValue *yaml.Node
}{
{
name: "creates true bool node",
value: true,
line: 1,
column: 2,
wantValue: &yaml.Node{
Value: "true",
Kind: yaml.ScalarNode,
Tag: "!!bool",
Line: 1,
Column: 2,
},
},
{
name: "creates false bool node",
value: false,
line: 3,
column: 4,
wantValue: &yaml.Node{
Value: "false",
Kind: yaml.ScalarNode,
Tag: "!!bool",
Line: 3,
Column: 4,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := CreateBoolYamlNode(tt.value, tt.line, tt.column)
assert.Equal(t, tt.wantValue, result, "should create correct bool YAML node")
})
}
}
func TestCreateMapYamlNode_Success(t *testing.T) {
t.Parallel()
tests := []struct {
name string
contents []*yaml.Node
line int
column int
wantValue *yaml.Node
}{
{
name: "creates map node with contents",
contents: []*yaml.Node{
CreateStringYamlNode("key1", 1, 1),
CreateStringYamlNode("value1", 1, 6),
CreateStringYamlNode("key2", 2, 1),
CreateIntYamlNode(42, 2, 6),
},
line: 1,
column: 1,
wantValue: &yaml.Node{
Content: []*yaml.Node{
CreateStringYamlNode("key1", 1, 1),
CreateStringYamlNode("value1", 1, 6),
CreateStringYamlNode("key2", 2, 1),
CreateIntYamlNode(42, 2, 6),
},
Kind: yaml.MappingNode,
Tag: "!!map",
Line: 1,
Column: 1,
},
},
{
name: "creates empty map node",
contents: []*yaml.Node{},
line: 5,
column: 10,
wantValue: &yaml.Node{
Content: []*yaml.Node{},
Kind: yaml.MappingNode,
Tag: "!!map",
Line: 5,
Column: 10,
},
},
{
name: "creates nil map node",
contents: nil,
line: 3,
column: 4,
wantValue: &yaml.Node{
Content: nil,
Kind: yaml.MappingNode,
Tag: "!!map",
Line: 3,
Column: 4,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := CreateMapYamlNode(tt.contents, tt.line, tt.column)
assert.Equal(t, tt.wantValue, result, "should create correct map YAML node")
})
}
}
func TestIsInterfaceNil(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input any
wantValue bool
}{
{
name: "returns true for nil interface",
input: nil,
wantValue: true,
},
{
name: "returns true for nil pointer",
input: (*string)(nil),
wantValue: true,
},
{
name: "returns true for nil slice",
input: ([]string)(nil),
wantValue: true,
},
{
name: "returns true for nil map",
input: (map[string]string)(nil),
wantValue: true,
},
{
name: "returns true for nil channel",
input: (chan int)(nil),
wantValue: true,
},
{
name: "returns true for nil function",
input: (func())(nil),
wantValue: true,
},
{
name: "returns false for non-nil string",
input: "test",
wantValue: false,
},
{
name: "returns false for non-nil pointer",
input: new(string),
wantValue: false,
},
{
name: "returns false for non-nil slice",
input: []string{"test"},
wantValue: false,
},
{
name: "returns false for non-nil map",
input: map[string]string{"key": "value"},
wantValue: false,
},
{
name: "returns false for zero int",
input: 0,
wantValue: false,
},
{
name: "returns false for empty string",
input: "",
wantValue: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := isInterfaceNil(tt.input)
assert.Equal(t, tt.wantValue, result, "should return correct nil check result")
})
}
}
// mockSequencedMap is a simple implementation for testing
type mockSequencedMap struct {
data map[any]any
keys []any
}
func newMockSequencedMap(pairs ...any) *mockSequencedMap {
if len(pairs)%2 != 0 {
panic("pairs must have even length")
}
m := &mockSequencedMap{
data: make(map[any]any),
keys: make([]any, 0, len(pairs)/2),
}
for i := 0; i < len(pairs); i += 2 {
key := pairs[i]
value := pairs[i+1]
m.data[key] = value
m.keys = append(m.keys, key)
}
return m
}
func (m *mockSequencedMap) Len() int {
if m == nil {
return 0
}
return len(m.keys)
}
func (m *mockSequencedMap) AllUntyped() iter.Seq2[any, any] {
return func(yield func(any, any) bool) {
if m == nil {
return
}
for _, k := range m.keys {
if !yield(k, m.data[k]) {
return
}
}
}
}
func (m *mockSequencedMap) GetUntyped(key any) (any, bool) {
if m == nil {
return nil, false
}
v, ok := m.data[key]
return v, ok
}
func TestAssertEqualSequencedMap_Success(t *testing.T) {
t.Parallel()
tests := []struct {
name string
expected SequencedMap
actual SequencedMap
}{
{
name: "both nil interfaces",
expected: nil,
actual: nil,
},
{
name: "both nil underlying values",
expected: (*mockSequencedMap)(nil),
actual: (*mockSequencedMap)(nil),
},
{
name: "equal simple maps",
expected: newMockSequencedMap("key1", "value1", "key2", "value2"),
actual: newMockSequencedMap("key1", "value1", "key2", "value2"),
},
{
name: "equal empty maps",
expected: newMockSequencedMap(),
actual: newMockSequencedMap(),
},
{
name: "equal maps with different types",
expected: newMockSequencedMap("string", "value", 42, 100, true, false),
actual: newMockSequencedMap("string", "value", 42, 100, true, false),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// Should not panic or fail
AssertEqualSequencedMap(t, tt.expected, tt.actual)
})
}
}
func TestAssertEqualSequencedMap_Failure(t *testing.T) {
t.Parallel()
tests := []struct {
name string
expected SequencedMap
actual SequencedMap
}{
{
name: "expected nil, actual not nil",
expected: nil,
actual: newMockSequencedMap("key", "value"),
},
{
name: "expected not nil, actual nil",
expected: newMockSequencedMap("key", "value"),
actual: nil,
},
{
name: "different lengths",
expected: newMockSequencedMap("key1", "value1"),
actual: newMockSequencedMap("key1", "value1", "key2", "value2"),
},
{
name: "different values",
expected: newMockSequencedMap("key", "value1"),
actual: newMockSequencedMap("key", "value2"),
},
{
name: "missing key in actual",
expected: newMockSequencedMap("key1", "value1", "key2", "value2"),
actual: newMockSequencedMap("key1", "value1"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// Create a mock testing.T to capture failures
mockT := &testing.T{}
// This should cause an assertion failure but not panic
// We use require to ensure failures are detected
defer func() {
if r := recover(); r != nil {
t.Errorf("AssertEqualSequencedMap should not panic: %v", r)
}
}()
AssertEqualSequencedMap(mockT, tt.expected, tt.actual)
})
}
}
func TestAssertEqualSequencedMap_NilChecks(t *testing.T) {
t.Parallel()
t.Run("handles nil expected with nil underlying", func(t *testing.T) {
t.Parallel()
var expected *mockSequencedMap
var actual *mockSequencedMap
// Should not panic
AssertEqualSequencedMap(t, expected, actual)
})
t.Run("handles mixed nil types", func(t *testing.T) {
t.Parallel()
mockT := &testing.T{}
var nilPtr *mockSequencedMap
// Should detect difference between true nil and nil pointer
AssertEqualSequencedMap(mockT, nil, nilPtr)
})
}
func TestQueryV4_Success(t *testing.T) {
t.Parallel()
tests := []struct {
name string
yml string
query string
expected string
}{
{
name: "scalar value lookup",
yml: "name: alice\nage: 30\n",
query: "$.name",
expected: "alice",
},
{
name: "nested value lookup",
yml: "user:\n name: bob\n",
query: "$.user.name",
expected: "bob",
},
{
name: "array element lookup",
yml: "items:\n - first\n - second\n",
query: "$.items[1]",
expected: "second",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var root yaml.Node
err := yaml.Unmarshal([]byte(tt.yml), &root)
require.NoError(t, err, "unmarshal should succeed")
path, err := jsonpath.NewPath(tt.query)
require.NoError(t, err, "jsonpath should be valid")
result := QueryV4(path, &root)
require.Len(t, result, 1, "should return exactly one match")
assert.Equal(t, tt.expected, result[0].Value, "should return correct value")
})
}
}
func TestQueryV4_NoMatch(t *testing.T) {
t.Parallel()
var root yaml.Node
err := yaml.Unmarshal([]byte("name: alice\n"), &root)
require.NoError(t, err, "unmarshal should succeed")
path, err := jsonpath.NewPath("$.missing")
require.NoError(t, err, "jsonpath should be valid")
result := QueryV4(path, &root)
assert.Empty(t, result, "should return no matches for missing path")
}