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.
11 "golang.org/x/tools/internal/lsp/diff"
12 "golang.org/x/tools/internal/lsp/diff/difftest"
13 "golang.org/x/tools/internal/span"
16 func TestApplyEdits(t *testing.T) {
17 for _, tc := range difftest.TestCases {
18 t.Run(tc.Name, func(t *testing.T) {
20 if got := diff.ApplyEdits(tc.In, tc.Edits); got != tc.Out {
21 t.Errorf("ApplyEdits edits got %q, want %q", got, tc.Out)
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)
32 func TestLineEdits(t *testing.T) {
33 for _, tc := range difftest.TestCases {
34 t.Run(tc.Name, func(t *testing.T) {
36 // if line edits not specified, it is the same as edits
41 if got := diff.LineEdits(tc.In, tc.Edits); diffEdits(got, edits) {
42 t.Errorf("LineEdits got %q, want %q", got, edits)
48 func TestUnified(t *testing.T) {
49 for _, tc := range difftest.TestCases {
50 t.Run(tc.Name, func(t *testing.T) {
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)
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)
66 func diffEdits(got, want []diff.TextEdit) bool {
67 if len(got) != len(want) {
70 for i, w := range want {
72 if span.Compare(w.Span, g.Span) != 0 {
75 if w.NewText != g.NewText {