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