@@ -603,3 +603,134 @@ comments before "// k"
603
603
})
604
604
}
605
605
}
606
+
607
+ func TestCleanup (t * testing.T ) {
608
+ for _ , test := range []struct {
609
+ desc string
610
+ want string
611
+ input []Expr
612
+ }{
613
+ {
614
+ desc : "simple_lines" ,
615
+ want : `line: module a
616
+ line: require b v1.0.0
617
+ ` ,
618
+ input : []Expr {
619
+ & Line {
620
+ Token : []string {"module" , "a" },
621
+ },
622
+ & Line {
623
+ Token : []string {"require" , "b" , "v1.0.0" },
624
+ },
625
+ & Line {
626
+ Token : nil ,
627
+ },
628
+ },
629
+ }, {
630
+ desc : "line_block" ,
631
+ want : `line: module a
632
+ block: require
633
+ blockline: b v1.0.0
634
+ blockline: c v1.0.0
635
+ ` ,
636
+ input : []Expr {
637
+ & Line {
638
+ Token : []string {"module" , "a" },
639
+ },
640
+ & LineBlock {
641
+ Token : []string {"require" },
642
+ Line : []* Line {
643
+ {
644
+ Token : []string {"b" , "v1.0.0" },
645
+ InBlock : true ,
646
+ },
647
+ {
648
+ Token : nil ,
649
+ InBlock : true ,
650
+ },
651
+ {
652
+ Token : []string {"c" , "v1.0.0" },
653
+ InBlock : true ,
654
+ },
655
+ },
656
+ },
657
+ },
658
+ }, {
659
+ desc : "collapse" ,
660
+ want : `line: module a
661
+ line: require b v1.0.0
662
+ ` ,
663
+ input : []Expr {
664
+ & Line {
665
+ Token : []string {"module" , "a" },
666
+ },
667
+ & LineBlock {
668
+ Token : []string {"require" },
669
+ Line : []* Line {
670
+ {
671
+ Token : []string {"b" , "v1.0.0" },
672
+ InBlock : true ,
673
+ },
674
+ {
675
+ Token : nil ,
676
+ InBlock : true ,
677
+ },
678
+ },
679
+ },
680
+ },
681
+ },
682
+ } {
683
+ t .Run (test .desc , func (t * testing.T ) {
684
+ syntax := & FileSyntax {
685
+ Stmt : test .input ,
686
+ }
687
+ syntax .Cleanup ()
688
+
689
+ buf := & bytes.Buffer {}
690
+ for _ , stmt := range syntax .Stmt {
691
+ switch stmt := stmt .(type ) {
692
+ case * Line :
693
+ fmt .Fprintf (buf , "line: %v\n " , strings .Join (stmt .Token , " " ))
694
+ case * LineBlock :
695
+ fmt .Fprintf (buf , "block: %v\n " , strings .Join (stmt .Token , " " ))
696
+ for _ , line := range stmt .Line {
697
+ fmt .Fprintf (buf , "blockline: %v\n " , strings .Join (line .Token , " " ))
698
+ }
699
+ }
700
+ }
701
+
702
+ got := strings .TrimSpace (buf .String ())
703
+ want := strings .TrimSpace (test .want )
704
+ if got != want {
705
+ t .Errorf ("got:\n %s\n want:\n %s" , got , want )
706
+ }
707
+ })
708
+ }
709
+ }
710
+
711
+ // Issue 45130: File.Cleanup breaks references so future edits do nothing
712
+ func TestCleanupMaintainsRefs (t * testing.T ) {
713
+ lineB := & Line {
714
+ Token : []string {"b" , "v1.0.0" },
715
+ InBlock : true ,
716
+ }
717
+ syntax := & FileSyntax {
718
+ Stmt : []Expr {
719
+ & LineBlock {
720
+ Token : []string {"require" },
721
+ Line : []* Line {
722
+ lineB ,
723
+ {
724
+ Token : nil ,
725
+ InBlock : true ,
726
+ },
727
+ },
728
+ },
729
+ },
730
+ }
731
+ syntax .Cleanup ()
732
+
733
+ if syntax .Stmt [0 ] != lineB {
734
+ t .Errorf ("got:\n %v\n want:\n %v" , syntax .Stmt [0 ], lineB )
735
+ }
736
+ }
0 commit comments