-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathrecursive_test.go
More file actions
837 lines (725 loc) · 18.9 KB
/
recursive_test.go
File metadata and controls
837 lines (725 loc) · 18.9 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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
package zog
import (
"testing"
"github.com/Oudwins/zog/pkgs/internals/tutils"
"github.com/Oudwins/zog/zconst"
"github.com/stretchr/testify/assert"
)
type Node struct {
Value int
Self *Node
}
type TreeNode struct {
Value int
Children []*TreeNode
}
type DeepNode struct {
Value int
Next *DeepNode
}
var nodeSchema = EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"self": self(func(original *PointerSchema) *PointerSchema {
return original
}),
}))
})
// ============================================================================
// 1. Basic Recursive Test (existing)
// ============================================================================
func TestRecursive(t *testing.T) {
var node *Node
errs := nodeSchema.Parse(map[string]any{"value": 10, "self": map[string]any{"value": 20}}, &node)
assert.Nil(t, errs)
assert.Equal(t, 10, node.Value)
assert.NotNil(t, node.Self)
assert.Equal(t, 20, node.Self.Value)
}
// ============================================================================
// 2. lazySchema Internal Tests
// ============================================================================
func TestLazySchemaGetType(t *testing.T) {
ptr := Ptr(Struct(Shape{
"value": Int().Required(),
"self": Ptr(Struct(Shape{
"value": Int().Required(),
})),
}))
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"self": self(),
}))
})
// getType should return the underlying schema type (ptr)
assert.Equal(t, ptr.getType(), schema.getType())
}
func TestLazySchemaSetCoercer(t *testing.T) {
type TestStruct struct {
Value string
Self *TestStruct
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": String().Required(),
"self": self(),
}))
})
// Set a custom coercer that converts int to string
customCoercer := func(v any) (any, error) {
if i, ok := v.(int); ok {
return string(rune(i + 48)), nil
}
return v, nil
}
schema.setCoercer(customCoercer)
var result *TestStruct
// Test that coercer is applied (though in practice, coercers work at field level)
errs := schema.Parse(map[string]any{"value": 10, "self": nil}, &result)
assert.Empty(t, errs)
assert.NotNil(t, result)
}
func TestLazySchemaLazyInitialization(t *testing.T) {
callCount := 0
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
callCount++
return Ptr(Struct(Shape{
"value": Int().Required(),
"self": self(),
}))
})
// First call to getType should initialize
_ = schema.getType()
initialCallCount := callCount
// Subsequent calls should not re-initialize
_ = schema.getType()
_ = schema.getType()
_ = schema.getType()
// Builder should only be called once during EXPERIMENTAL_RECURSIVE
// The lazy initialization happens when get() is called, but the builder
// itself is only called once
assert.Equal(t, initialCallCount, callCount)
}
// ============================================================================
// 3. Parse Tests for Multi-Level Deep Recursion
// ============================================================================
func TestRecursiveMultiLevelDeep(t *testing.T) {
type DeepNode struct {
Value int
Next *DeepNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"next": self(),
}))
})
var node *DeepNode
data := map[string]any{
"value": 1,
"next": map[string]any{
"value": 2,
"next": map[string]any{
"value": 3,
"next": map[string]any{
"value": 4,
"next": nil,
},
},
},
}
errs := schema.Parse(data, &node)
assert.Empty(t, errs)
assert.NotNil(t, node)
assert.Equal(t, 1, node.Value)
assert.NotNil(t, node.Next)
assert.Equal(t, 2, node.Next.Value)
assert.NotNil(t, node.Next.Next)
assert.Equal(t, 3, node.Next.Next.Value)
assert.NotNil(t, node.Next.Next.Next)
assert.Equal(t, 4, node.Next.Next.Next.Value)
assert.Nil(t, node.Next.Next.Next.Next)
}
func TestRecursiveFiveLevelsDeep(t *testing.T) {
type DeepNode struct {
Value int
Next *DeepNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"next": self(),
}))
})
var node *DeepNode
data := map[string]any{
"value": 1,
"next": map[string]any{
"value": 2,
"next": map[string]any{
"value": 3,
"next": map[string]any{
"value": 4,
"next": map[string]any{
"value": 5,
"next": nil,
},
},
},
},
}
errs := schema.Parse(data, &node)
assert.Empty(t, errs)
// Verify all 5 levels
current := node
for i := 1; i <= 5; i++ {
assert.NotNil(t, current)
assert.Equal(t, i, current.Value)
if i < 5 {
current = current.Next
} else {
assert.Nil(t, current.Next)
}
}
}
// ============================================================================
// 4. Parse Tests for Recursive Slices
// ============================================================================
func TestRecursiveSliceTree(t *testing.T) {
type TreeNode struct {
Value int
Children []*TreeNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"children": Slice(self()),
}))
})
var tree *TreeNode
data := map[string]any{
"value": 1,
"children": []any{
map[string]any{
"value": 2,
"children": []any{},
},
map[string]any{
"value": 3,
"children": []any{
map[string]any{
"value": 4,
"children": []any{},
},
},
},
},
}
errs := schema.Parse(data, &tree)
assert.Empty(t, errs)
assert.NotNil(t, tree)
assert.Equal(t, 1, tree.Value)
assert.Len(t, tree.Children, 2)
assert.Equal(t, 2, tree.Children[0].Value)
assert.Len(t, tree.Children[0].Children, 0)
assert.Equal(t, 3, tree.Children[1].Value)
assert.Len(t, tree.Children[1].Children, 1)
assert.Equal(t, 4, tree.Children[1].Children[0].Value)
}
func TestRecursiveSliceEmptyChildren(t *testing.T) {
type TreeNode struct {
Value int
Children []*TreeNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"children": Slice(self()),
}))
})
var tree *TreeNode
data := map[string]any{
"value": 1,
"children": []any{},
}
errs := schema.Parse(data, &tree)
assert.Empty(t, errs)
assert.NotNil(t, tree)
assert.Equal(t, 1, tree.Value)
assert.Len(t, tree.Children, 0)
}
// ============================================================================
// 5. Parse Tests for Nil Values and Edge Cases
// ============================================================================
func TestRecursiveNilAtRoot(t *testing.T) {
var node *Node
errs := nodeSchema.Parse(nil, &node)
assert.Empty(t, errs)
assert.Nil(t, node)
}
func TestRecursiveNilAtNestedLevel(t *testing.T) {
var node *Node
errs := nodeSchema.Parse(map[string]any{
"value": 10,
"self": nil,
}, &node)
assert.Empty(t, errs)
assert.NotNil(t, node)
assert.Equal(t, 10, node.Value)
assert.Nil(t, node.Self)
}
func TestRecursiveNilAtDeepLevel(t *testing.T) {
type DeepNode struct {
Value int
Next *DeepNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"next": self(),
}))
})
var node *DeepNode
data := map[string]any{
"value": 1,
"next": map[string]any{
"value": 2,
"next": nil,
},
}
errs := schema.Parse(data, &node)
assert.Empty(t, errs)
assert.NotNil(t, node)
assert.Equal(t, 1, node.Value)
assert.NotNil(t, node.Next)
assert.Equal(t, 2, node.Next.Value)
assert.Nil(t, node.Next.Next)
}
func TestRecursiveZeroValue(t *testing.T) {
var node *Node
errs := nodeSchema.Parse(map[string]any{
"value": 0,
"self": nil,
}, &node)
assert.Empty(t, errs)
assert.NotNil(t, node)
assert.Equal(t, 0, node.Value)
}
func TestRecursiveMissingField(t *testing.T) {
var node *Node
errs := nodeSchema.Parse(map[string]any{
"self": map[string]any{"value": 20},
}, &node)
assert.NotEmpty(t, errs)
tutils.VerifyDefaultIssueMessages(t, errs)
}
// ============================================================================
// 6. Validation Tests
// ============================================================================
func TestRecursiveValidate(t *testing.T) {
type DeepNode struct {
Value int
Next *DeepNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"next": self(),
}))
})
node := &DeepNode{
Value: 1,
Next: &DeepNode{
Value: 2,
Next: nil,
},
}
errs := schema.Validate(&node)
assert.Empty(t, errs)
assert.Equal(t, 1, node.Value)
assert.Equal(t, 2, node.Next.Value)
// Test with nil pointer
var nilNode *DeepNode
errs = schema.Validate(&nilNode)
assert.Empty(t, errs)
}
func TestRecursiveValidateWithNilAtNestedLevel(t *testing.T) {
type DeepNode struct {
Value int
Next *DeepNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"next": self(),
}))
})
node := &DeepNode{
Value: 1,
Next: nil,
}
errs := schema.Validate(&node)
assert.Empty(t, errs)
assert.Nil(t, node.Next)
}
func TestRecursiveValidateTree(t *testing.T) {
type TreeNode struct {
Value int
Children []*TreeNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"children": Slice(self()),
}))
})
tree := &TreeNode{
Value: 1,
Children: []*TreeNode{
{Value: 2, Children: []*TreeNode{}},
{Value: 3, Children: []*TreeNode{
{Value: 4, Children: []*TreeNode{}},
}},
},
}
errs := schema.Validate(&tree)
assert.Empty(t, errs)
assert.Equal(t, 1, tree.Value)
assert.Len(t, tree.Children, 2)
}
// ============================================================================
// 7. RecursiveSchemaUpdater Tests
// ============================================================================
func TestRecursiveSchemaUpdater(t *testing.T) {
type DeepNode struct {
Value int
Next *DeepNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"next": self(func(original *PointerSchema) *PointerSchema {
// Apply NotNil() via updater
return original.NotNil()
}),
}))
})
var node *DeepNode
// Test with nil next - should fail because of NotNil()
data := map[string]any{
"value": 1,
"next": nil,
}
errs := schema.Parse(data, &node)
assert.NotEmpty(t, errs)
tutils.VerifyDefaultIssueMessages(t, errs)
assert.Equal(t, zconst.IssueCodeNotNil, errs[0].Code)
// Test with valid next but nil nested next - should fail because NotNil() is applied recursively
// The updater applies NotNil() to all recursive instances, so any nil in the chain should fail
data = map[string]any{
"value": 1,
"next": map[string]any{
"value": 2,
"next": nil, // This should fail because NotNil() is applied via updater
},
}
errs = schema.Parse(data, &node)
assert.NotEmpty(t, errs) // Should fail because nested next is nil
tutils.VerifyDefaultIssueMessages(t, errs)
// Verify that the error is about the nested next being nil
foundNotNilError := false
for _, err := range errs {
if err.Code == zconst.IssueCodeNotNil {
foundNotNilError = true
break
}
}
assert.True(t, foundNotNilError, "Should have a NotNil error")
}
func TestRecursiveSchemaUpdaterMultiple(t *testing.T) {
type DeepNode struct {
Value int
Next *DeepNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"next": self(func(original *PointerSchema) *PointerSchema {
// Apply updater that returns the original unchanged
return original
}),
}))
})
var node *DeepNode
data := map[string]any{
"value": 1,
"next": map[string]any{
"value": 2,
"next": nil,
},
}
errs := schema.Parse(data, &node)
assert.Empty(t, errs)
assert.NotNil(t, node)
assert.Equal(t, 1, node.Value)
assert.NotNil(t, node.Next)
assert.Equal(t, 2, node.Next.Value)
}
func TestRecursiveSchemaUpdaterNoUpdater(t *testing.T) {
type DeepNode struct {
Value int
Next *DeepNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"next": self(), // No updater passed
}))
})
var node *DeepNode
data := map[string]any{
"value": 1,
"next": nil, // Should be allowed without updater
}
errs := schema.Parse(data, &node)
assert.Empty(t, errs)
assert.NotNil(t, node)
assert.Nil(t, node.Next)
}
// ============================================================================
// 8. Error Path Tests
// ============================================================================
func TestRecursiveErrorPathRoot(t *testing.T) {
var node *Node
errs := nodeSchema.Parse(map[string]any{
"value": "not_an_int", // Invalid type
"self": nil,
}, &node)
assert.NotEmpty(t, errs)
tutils.VerifyDefaultIssueMessages(t, errs)
// Error should be at root level or value field
assert.True(t, len(errs) > 0)
}
func TestRecursiveErrorPathNested(t *testing.T) {
type DeepNode struct {
Value int
Next *DeepNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"next": self(),
}))
})
var node *DeepNode
data := map[string]any{
"value": 1,
"next": map[string]any{
"value": "invalid", // Error at nested level
"next": nil,
},
}
errs := schema.Parse(data, &node)
assert.NotEmpty(t, errs)
tutils.VerifyDefaultIssueMessages(t, errs)
// Check that error path includes "next"
hasNextPath := false
for _, err := range errs {
pathStr := ""
for _, p := range err.Path {
pathStr += p + "."
}
if len(err.Path) > 0 && err.Path[0] == "next" {
hasNextPath = true
break
}
}
assert.True(t, hasNextPath || len(errs) > 0)
}
func TestRecursiveErrorPathDeepNested(t *testing.T) {
type DeepNode struct {
Value int
Next *DeepNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"next": self(),
}))
})
var node *DeepNode
data := map[string]any{
"value": 1,
"next": map[string]any{
"value": 2,
"next": map[string]any{
"value": "invalid", // Error at deep nested level
"next": nil,
},
},
}
errs := schema.Parse(data, &node)
assert.NotEmpty(t, errs)
tutils.VerifyDefaultIssueMessages(t, errs)
}
func TestRecursiveMultipleErrors(t *testing.T) {
type DeepNode struct {
Value int
Next *DeepNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"next": self(),
}))
})
var node *DeepNode
data := map[string]any{
"value": "invalid1",
"next": map[string]any{
"value": "invalid2",
"next": nil,
},
}
errs := schema.Parse(data, &node)
assert.NotEmpty(t, errs)
assert.True(t, len(errs) >= 1) // At least one error
tutils.VerifyDefaultIssueMessages(t, errs)
}
func TestRecursiveErrorPathInTree(t *testing.T) {
type TreeNode struct {
Value int
Children []*TreeNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"children": Slice(self()),
}))
})
var tree *TreeNode
data := map[string]any{
"value": 1,
"children": []any{
map[string]any{
"value": "invalid",
"children": []any{},
},
},
}
errs := schema.Parse(data, &tree)
assert.NotEmpty(t, errs)
tutils.VerifyDefaultIssueMessages(t, errs)
}
// ============================================================================
// 9. Edge Cases
// ============================================================================
func TestRecursiveCircularLikePattern(t *testing.T) {
// Test deeply nested structure that goes back and forth
// This tests that recursive schemas can handle very deep nesting
type DeepNode struct {
Value int
Next *DeepNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"next": self(),
}))
})
var node *DeepNode
// Create a very deep structure
data := map[string]any{
"value": 1,
"next": map[string]any{
"value": 2,
"next": map[string]any{
"value": 3,
"next": map[string]any{
"value": 4,
"next": map[string]any{
"value": 5,
"next": nil,
},
},
},
},
}
errs := schema.Parse(data, &node)
assert.Empty(t, errs)
assert.NotNil(t, node)
// Verify deep nesting works
current := node
expectedValues := []int{1, 2, 3, 4, 5}
for i, expected := range expectedValues {
assert.NotNil(t, current, "Node at level %d should not be nil", i)
assert.Equal(t, expected, current.Value, "Value at level %d should be %d", i, expected)
if i < len(expectedValues)-1 {
current = current.Next
} else {
assert.Nil(t, current.Next, "Last node should have nil Next")
}
}
}
func TestRecursiveMixedSchemaTypes(t *testing.T) {
type MixedNode struct {
Value int
Children []*MixedNode
Next *MixedNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"children": Slice(self()),
"next": self(),
}))
})
var node *MixedNode
data := map[string]any{
"value": 1,
"children": []any{
map[string]any{
"value": 2,
"children": []any{},
"next": nil,
},
},
"next": map[string]any{
"value": 3,
"children": []any{},
"next": nil,
},
}
errs := schema.Parse(data, &node)
assert.Empty(t, errs)
assert.NotNil(t, node)
assert.Equal(t, 1, node.Value)
assert.Len(t, node.Children, 1)
assert.Equal(t, 2, node.Children[0].Value)
assert.NotNil(t, node.Next)
assert.Equal(t, 3, node.Next.Value)
}
func TestRecursiveRequiredField(t *testing.T) {
type DeepNode struct {
Value int
Next *DeepNode
}
schema := EXPERIMENTAL_RECURSIVE(func(self RecursiveSchema[*PointerSchema]) *PointerSchema {
return Ptr(Struct(Shape{
"value": Int().Required(),
"next": self(),
}))
})
var node *DeepNode
// Missing required field
data := map[string]any{
"next": map[string]any{
"value": 2,
"next": nil,
},
}
errs := schema.Parse(data, &node)
assert.NotEmpty(t, errs)
tutils.VerifyDefaultIssueMessages(t, errs)
}