1
0
mirror of https://github.com/golang/go synced 2024-11-05 17:06:13 -07:00
go/internal/lsp/diff/diff_test.go
Ian Cottrell 7025dca8be internal/lsp: expand edits to whole lines in ToUnified
We don't want to support sub line edits or non line context in the unified diff
printing.

Change-Id: I4267b11b50f12d817dac0fbbae7e1db44ca3dad0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/199739
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-10-10 20:14:46 +00:00

77 lines
1.8 KiB
Go

package diff_test
import (
"fmt"
"testing"
"golang.org/x/tools/internal/lsp/diff"
"golang.org/x/tools/internal/lsp/diff/difftest"
"golang.org/x/tools/internal/span"
)
func TestApplyEdits(t *testing.T) {
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)
}
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)
}
})
}
}
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
}