2019-09-27 18:01:26 -06:00
|
|
|
package diff_test
|
2019-09-06 15:07:41 -06:00
|
|
|
|
|
|
|
import (
|
2019-10-03 12:56:48 -06:00
|
|
|
"fmt"
|
2019-09-06 15:07:41 -06:00
|
|
|
"testing"
|
|
|
|
|
2019-09-27 18:01:26 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/diff"
|
2019-10-07 17:55:23 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/diff/difftest"
|
2019-10-03 12:56:48 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-09-06 15:07:41 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestApplyEdits(t *testing.T) {
|
2019-10-07 17:55:23 -06:00
|
|
|
for _, tc := range difftest.TestCases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
if got := diff.ApplyEdits(tc.In, tc.Edits); got != tc.Out {
|
|
|
|
t.Errorf("ApplyEdits edits got %q, want %q", got, tc.Out)
|
|
|
|
}
|
2019-10-03 12:56:48 -06:00
|
|
|
if tc.LineEdits != nil {
|
|
|
|
if got := diff.ApplyEdits(tc.In, tc.LineEdits); got != tc.Out {
|
|
|
|
t.Errorf("ApplyEdits lineEdits got %q, want %q", got, tc.Out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLineEdits(t *testing.T) {
|
|
|
|
for _, tc := range difftest.TestCases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
// if line edits not specified, it is the same as edits
|
|
|
|
edits := tc.LineEdits
|
|
|
|
if edits == nil {
|
|
|
|
edits = tc.Edits
|
|
|
|
}
|
|
|
|
if got := diff.LineEdits(tc.In, tc.Edits); diffEdits(got, edits) {
|
|
|
|
t.Errorf("LineEdits got %q, want %q", got, edits)
|
|
|
|
}
|
2019-10-07 17:55:23 -06:00
|
|
|
})
|
2019-09-06 15:07:41 -06:00
|
|
|
}
|
|
|
|
}
|
2019-10-03 12:56:48 -06:00
|
|
|
|
|
|
|
func TestUnified(t *testing.T) {
|
|
|
|
for _, tc := range difftest.TestCases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
unified := fmt.Sprint(diff.ToUnified(difftest.FileA, difftest.FileB, tc.In, tc.Edits))
|
|
|
|
if unified != tc.Unified {
|
|
|
|
t.Errorf("edits got diff:\n%v\nexpected:\n%v", unified, tc.Unified)
|
|
|
|
}
|
|
|
|
if tc.LineEdits != nil {
|
|
|
|
unified := fmt.Sprint(diff.ToUnified(difftest.FileA, difftest.FileB, tc.In, tc.LineEdits))
|
|
|
|
if unified != tc.Unified {
|
|
|
|
t.Errorf("lineEdits got diff:\n%v\nexpected:\n%v", unified, tc.Unified)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func diffEdits(got, want []diff.TextEdit) bool {
|
|
|
|
if len(got) != len(want) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for i, w := range want {
|
|
|
|
g := got[i]
|
|
|
|
if span.Compare(w.Span, g.Span) != 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if w.NewText != g.NewText {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|