github.com/jd-ly/tools@v0.5.7/internal/lsp/diff/diff_test.go (about) 1 package diff_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/jd-ly/tools/internal/lsp/diff" 8 "github.com/jd-ly/tools/internal/lsp/diff/difftest" 9 "github.com/jd-ly/tools/internal/span" 10 ) 11 12 func TestApplyEdits(t *testing.T) { 13 for _, tc := range difftest.TestCases { 14 t.Run(tc.Name, func(t *testing.T) { 15 t.Helper() 16 if got := diff.ApplyEdits(tc.In, tc.Edits); got != tc.Out { 17 t.Errorf("ApplyEdits edits got %q, want %q", got, tc.Out) 18 } 19 if tc.LineEdits != nil { 20 if got := diff.ApplyEdits(tc.In, tc.LineEdits); got != tc.Out { 21 t.Errorf("ApplyEdits lineEdits got %q, want %q", got, tc.Out) 22 } 23 } 24 }) 25 } 26 } 27 28 func TestLineEdits(t *testing.T) { 29 for _, tc := range difftest.TestCases { 30 t.Run(tc.Name, func(t *testing.T) { 31 t.Helper() 32 // if line edits not specified, it is the same as edits 33 edits := tc.LineEdits 34 if edits == nil { 35 edits = tc.Edits 36 } 37 if got := diff.LineEdits(tc.In, tc.Edits); diffEdits(got, edits) { 38 t.Errorf("LineEdits got %q, want %q", got, edits) 39 } 40 }) 41 } 42 } 43 44 func TestUnified(t *testing.T) { 45 for _, tc := range difftest.TestCases { 46 t.Run(tc.Name, func(t *testing.T) { 47 t.Helper() 48 unified := fmt.Sprint(diff.ToUnified(difftest.FileA, difftest.FileB, tc.In, tc.Edits)) 49 if unified != tc.Unified { 50 t.Errorf("edits got diff:\n%v\nexpected:\n%v", unified, tc.Unified) 51 } 52 if tc.LineEdits != nil { 53 unified := fmt.Sprint(diff.ToUnified(difftest.FileA, difftest.FileB, tc.In, tc.LineEdits)) 54 if unified != tc.Unified { 55 t.Errorf("lineEdits got diff:\n%v\nexpected:\n%v", unified, tc.Unified) 56 } 57 } 58 }) 59 } 60 } 61 62 func diffEdits(got, want []diff.TextEdit) bool { 63 if len(got) != len(want) { 64 return true 65 } 66 for i, w := range want { 67 g := got[i] 68 if span.Compare(w.Span, g.Span) != 0 { 69 return true 70 } 71 if w.NewText != g.NewText { 72 return true 73 } 74 } 75 return false 76 }