@@ -422,3 +422,190 @@ func TestNewHelper(t *testing.T) {
422
422
})
423
423
}
424
424
}
425
+
426
+ func Test_filterPatchMap (t * testing.T ) {
427
+ tests := []struct {
428
+ name string
429
+ patchMap map [string ]interface {}
430
+ paths []contract.Path
431
+ want map [string ]interface {}
432
+ }{
433
+ {
434
+ name : "Allow values" ,
435
+ patchMap : map [string ]interface {}{
436
+ "foo" : "123" ,
437
+ "bar" : 123 ,
438
+ "baz" : map [string ]interface {}{
439
+ "foo" : "123" ,
440
+ "bar" : 123 ,
441
+ },
442
+ },
443
+ paths : []contract.Path {
444
+ []string {"foo" },
445
+ []string {"baz" , "foo" },
446
+ },
447
+ want : map [string ]interface {}{
448
+ "foo" : "123" ,
449
+ "baz" : map [string ]interface {}{
450
+ "foo" : "123" ,
451
+ },
452
+ },
453
+ },
454
+ {
455
+ name : "Allow maps" ,
456
+ patchMap : map [string ]interface {}{
457
+ "foo" : map [string ]interface {}{
458
+ "foo" : "123" ,
459
+ "bar" : 123 ,
460
+ },
461
+ "bar" : map [string ]interface {}{
462
+ "foo" : "123" ,
463
+ "bar" : 123 ,
464
+ },
465
+ "baz" : map [string ]interface {}{
466
+ "foo" : map [string ]interface {}{
467
+ "foo" : "123" ,
468
+ "bar" : 123 ,
469
+ },
470
+ "bar" : map [string ]interface {}{
471
+ "foo" : "123" ,
472
+ "bar" : 123 ,
473
+ },
474
+ },
475
+ },
476
+ paths : []contract.Path {
477
+ []string {"foo" },
478
+ []string {"baz" , "foo" },
479
+ },
480
+ want : map [string ]interface {}{
481
+ "foo" : map [string ]interface {}{
482
+ "foo" : "123" ,
483
+ "bar" : 123 ,
484
+ },
485
+ "baz" : map [string ]interface {}{
486
+ "foo" : map [string ]interface {}{
487
+ "foo" : "123" ,
488
+ "bar" : 123 ,
489
+ },
490
+ },
491
+ },
492
+ },
493
+ {
494
+ name : "Cleanup empty maps" ,
495
+ patchMap : map [string ]interface {}{
496
+ "foo" : map [string ]interface {}{
497
+ "bar" : "123" ,
498
+ "baz" : map [string ]interface {}{
499
+ "bar" : "123" ,
500
+ },
501
+ },
502
+ },
503
+ paths : []contract.Path {},
504
+ want : map [string ]interface {}{},
505
+ },
506
+ }
507
+ for _ , tt := range tests {
508
+ t .Run (tt .name , func (t * testing.T ) {
509
+ g := NewWithT (t )
510
+
511
+ filterPatchMap (tt .patchMap , tt .paths )
512
+
513
+ g .Expect (tt .patchMap ).To (Equal (tt .want ))
514
+ })
515
+ }
516
+ }
517
+
518
+ func Test_removePath (t * testing.T ) {
519
+ tests := []struct {
520
+ name string
521
+ patchMap map [string ]interface {}
522
+ path contract.Path
523
+ want map [string ]interface {}
524
+ }{
525
+ {
526
+ name : "Remove value" ,
527
+ patchMap : map [string ]interface {}{
528
+ "foo" : "123" ,
529
+ },
530
+ path : contract .Path ([]string {"foo" }),
531
+ want : map [string ]interface {}{},
532
+ },
533
+ {
534
+ name : "Remove map" ,
535
+ patchMap : map [string ]interface {}{
536
+ "foo" : map [string ]interface {}{
537
+ "bar" : "123" ,
538
+ },
539
+ },
540
+ path : contract .Path ([]string {"foo" }),
541
+ want : map [string ]interface {}{},
542
+ },
543
+ {
544
+ name : "Remove nested value" ,
545
+ patchMap : map [string ]interface {}{
546
+ "foo" : map [string ]interface {}{
547
+ "bar" : "123" ,
548
+ "baz" : "123" ,
549
+ },
550
+ },
551
+ path : contract .Path ([]string {"foo" , "bar" }),
552
+ want : map [string ]interface {}{
553
+ "foo" : map [string ]interface {}{
554
+ "baz" : "123" ,
555
+ },
556
+ },
557
+ },
558
+ {
559
+ name : "Remove nested map" ,
560
+ patchMap : map [string ]interface {}{
561
+ "foo" : map [string ]interface {}{
562
+ "bar" : map [string ]interface {}{
563
+ "baz" : "123" ,
564
+ },
565
+ "baz" : "123" ,
566
+ },
567
+ },
568
+ path : contract .Path ([]string {"foo" , "bar" }),
569
+ want : map [string ]interface {}{
570
+ "foo" : map [string ]interface {}{
571
+ "baz" : "123" ,
572
+ },
573
+ },
574
+ },
575
+ {
576
+ name : "Ignore partial match" ,
577
+ patchMap : map [string ]interface {}{
578
+ "foo" : map [string ]interface {}{
579
+ "bar" : "123" ,
580
+ },
581
+ },
582
+ path : contract .Path ([]string {"foo" , "bar" , "baz" }),
583
+ want : map [string ]interface {}{
584
+ "foo" : map [string ]interface {}{
585
+ "bar" : "123" ,
586
+ },
587
+ },
588
+ },
589
+ {
590
+ name : "Cleanup empty maps" ,
591
+ patchMap : map [string ]interface {}{
592
+ "foo" : map [string ]interface {}{
593
+ "baz" : map [string ]interface {}{
594
+ "bar" : "123" ,
595
+ },
596
+ },
597
+ },
598
+ path : contract .Path ([]string {"foo" , "baz" , "bar" }),
599
+ want : map [string ]interface {}{},
600
+ },
601
+ }
602
+ for _ , tt := range tests {
603
+ t .Run (tt .name , func (t * testing.T ) {
604
+ g := NewWithT (t )
605
+
606
+ removePath (tt .patchMap , tt .path )
607
+
608
+ g .Expect (tt .patchMap ).To (Equal (tt .want ))
609
+ })
610
+ }
611
+ }
0 commit comments