mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +00:00
internal/lsp: merge diffing test data sets
also improve the actual verification of the unified diff Change-Id: I9c23c24e1fc8571cce2a7879463659ec7069fe99 Reviewed-on: https://go-review.googlesource.com/c/tools/+/199738 Run-TryBot: Ian Cottrell <iancottrell@google.com> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
parent
7822de7a2d
commit
6ac766747f
@ -4,27 +4,16 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"golang.org/x/tools/internal/lsp/diff"
|
"golang.org/x/tools/internal/lsp/diff"
|
||||||
"golang.org/x/tools/internal/span"
|
"golang.org/x/tools/internal/lsp/diff/difftest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestApplyEdits(t *testing.T) {
|
func TestApplyEdits(t *testing.T) {
|
||||||
var testCases = []struct {
|
for _, tc := range difftest.TestCases {
|
||||||
before string
|
t.Run(tc.Name, func(t *testing.T) {
|
||||||
edits []diff.TextEdit
|
t.Helper()
|
||||||
want string
|
if got := diff.ApplyEdits(tc.In, tc.Edits); got != tc.Out {
|
||||||
}{
|
t.Errorf("ApplyEdits edits got %q, want %q", got, tc.Out)
|
||||||
{"", nil, ""},
|
|
||||||
{"X", []diff.TextEdit{{newSpan(0, 1), "Y"}}, "Y"},
|
|
||||||
{" X ", []diff.TextEdit{{newSpan(1, 2), "Y"}}, " Y "},
|
|
||||||
{" X X ", []diff.TextEdit{{newSpan(1, 2), "Y"}, {newSpan(3, 4), "Z"}}, " Y Z "},
|
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
})
|
||||||
if got := diff.ApplyEdits(tc.before, tc.edits); got != tc.want {
|
|
||||||
t.Errorf("applyEdits(%v, %v): got %v, want %v", tc.before, tc.edits, got, tc.want)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func newSpan(start, end int) span.Span {
|
|
||||||
return span.New("", span.NewPoint(0, 0, start), span.NewPoint(0, 0, end))
|
|
||||||
}
|
|
||||||
|
@ -8,12 +8,7 @@
|
|||||||
package difftest
|
package difftest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"golang.org/x/tools/internal/lsp/diff"
|
"golang.org/x/tools/internal/lsp/diff"
|
||||||
@ -21,88 +16,104 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
fileA = "from"
|
FileA = "from"
|
||||||
fileB = "to"
|
FileB = "to"
|
||||||
unifiedPrefix = "--- " + fileA + "\n+++ " + fileB + "\n"
|
UnifiedPrefix = "--- " + FileA + "\n+++ " + FileB + "\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
var verifyDiff = flag.Bool("verify-diff", false, "Check that the unified diff output matches `diff -u`")
|
var TestCases = []struct {
|
||||||
|
Name, In, Out, Unified string
|
||||||
func DiffTest(t *testing.T, compute diff.ComputeEdits) {
|
Edits []diff.TextEdit
|
||||||
t.Helper()
|
NoDiff bool
|
||||||
for _, test := range []struct {
|
|
||||||
name, in, out, unified string
|
|
||||||
nodiff bool
|
|
||||||
}{{
|
}{{
|
||||||
name: "empty",
|
Name: "empty",
|
||||||
in: "",
|
In: "",
|
||||||
out: "",
|
Out: "",
|
||||||
}, {
|
}, {
|
||||||
name: "no_diff",
|
Name: "no_diff",
|
||||||
in: "gargantuan\n",
|
In: "gargantuan\n",
|
||||||
out: "gargantuan\n",
|
Out: "gargantuan\n",
|
||||||
}, {
|
}, {
|
||||||
name: "replace_all",
|
Name: "replace_all",
|
||||||
in: "gord\n",
|
In: "fruit\n",
|
||||||
out: "gourd\n",
|
Out: "cheese\n",
|
||||||
unified: unifiedPrefix + `
|
Unified: UnifiedPrefix + `
|
||||||
|
@@ -1 +1 @@
|
||||||
|
-fruit
|
||||||
|
+cheese
|
||||||
|
`[1:],
|
||||||
|
Edits: []diff.TextEdit{{Span: newSpan(0, 5), NewText: "cheese"}},
|
||||||
|
}, {
|
||||||
|
Name: "insert_rune",
|
||||||
|
In: "gord\n",
|
||||||
|
Out: "gourd\n",
|
||||||
|
Unified: UnifiedPrefix + `
|
||||||
@@ -1 +1 @@
|
@@ -1 +1 @@
|
||||||
-gord
|
-gord
|
||||||
+gourd
|
+gourd
|
||||||
`[1:],
|
`[1:],
|
||||||
|
Edits: []diff.TextEdit{{Span: newSpan(2, 2), NewText: "u"}},
|
||||||
}, {
|
}, {
|
||||||
name: "insert_rune",
|
Name: "delete_rune",
|
||||||
in: "gord\n",
|
In: "groat\n",
|
||||||
out: "gourd\n",
|
Out: "goat\n",
|
||||||
unified: unifiedPrefix + `
|
Unified: UnifiedPrefix + `
|
||||||
@@ -1 +1 @@
|
|
||||||
-gord
|
|
||||||
+gourd
|
|
||||||
`[1:],
|
|
||||||
}, {
|
|
||||||
name: "delete_rune",
|
|
||||||
in: "groat\n",
|
|
||||||
out: "goat\n",
|
|
||||||
unified: unifiedPrefix + `
|
|
||||||
@@ -1 +1 @@
|
@@ -1 +1 @@
|
||||||
-groat
|
-groat
|
||||||
+goat
|
+goat
|
||||||
`[1:],
|
`[1:],
|
||||||
|
Edits: []diff.TextEdit{{Span: newSpan(1, 2), NewText: ""}},
|
||||||
}, {
|
}, {
|
||||||
name: "replace_rune",
|
Name: "replace_rune",
|
||||||
in: "loud\n",
|
In: "loud\n",
|
||||||
out: "lord\n",
|
Out: "lord\n",
|
||||||
unified: unifiedPrefix + `
|
Unified: UnifiedPrefix + `
|
||||||
@@ -1 +1 @@
|
@@ -1 +1 @@
|
||||||
-loud
|
-loud
|
||||||
+lord
|
+lord
|
||||||
`[1:],
|
`[1:],
|
||||||
|
Edits: []diff.TextEdit{{Span: newSpan(2, 3), NewText: "r"}},
|
||||||
}, {
|
}, {
|
||||||
name: "insert_line",
|
Name: "replace_partials",
|
||||||
in: "one\nthree\n",
|
In: "blanket\n",
|
||||||
out: "one\ntwo\nthree\n",
|
Out: "bunker\n",
|
||||||
unified: unifiedPrefix + `
|
Unified: UnifiedPrefix + `
|
||||||
|
@@ -1 +1 @@
|
||||||
|
-blanket
|
||||||
|
+bunker
|
||||||
|
`[1:],
|
||||||
|
Edits: []diff.TextEdit{
|
||||||
|
{Span: newSpan(1, 3), NewText: "u"},
|
||||||
|
{Span: newSpan(6, 7), NewText: "r"},
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
Name: "insert_line",
|
||||||
|
In: "one\nthree\n",
|
||||||
|
Out: "one\ntwo\nthree\n",
|
||||||
|
Unified: UnifiedPrefix + `
|
||||||
@@ -1,2 +1,3 @@
|
@@ -1,2 +1,3 @@
|
||||||
one
|
one
|
||||||
+two
|
+two
|
||||||
three
|
three
|
||||||
`[1:],
|
`[1:],
|
||||||
|
Edits: []diff.TextEdit{{Span: newSpan(4, 4), NewText: "two\n"}},
|
||||||
}, {
|
}, {
|
||||||
name: "replace_no_newline",
|
Name: "replace_no_newline",
|
||||||
in: "A",
|
In: "A",
|
||||||
out: "B",
|
Out: "B",
|
||||||
unified: unifiedPrefix + `
|
Unified: UnifiedPrefix + `
|
||||||
@@ -1 +1 @@
|
@@ -1 +1 @@
|
||||||
-A
|
-A
|
||||||
\ No newline at end of file
|
\ No newline at end of file
|
||||||
+B
|
+B
|
||||||
\ No newline at end of file
|
\ No newline at end of file
|
||||||
`[1:],
|
`[1:],
|
||||||
|
Edits: []diff.TextEdit{{Span: newSpan(0, 1), NewText: "B"}},
|
||||||
}, {
|
}, {
|
||||||
name: "delete_front",
|
Name: "delete_front",
|
||||||
in: "A\nB\nC\nA\nB\nB\nA\n",
|
In: "A\nB\nC\nA\nB\nB\nA\n",
|
||||||
out: "C\nB\nA\nB\nA\nC\n",
|
Out: "C\nB\nA\nB\nA\nC\n",
|
||||||
unified: unifiedPrefix + `
|
Unified: UnifiedPrefix + `
|
||||||
@@ -1,7 +1,6 @@
|
@@ -1,7 +1,6 @@
|
||||||
-A
|
-A
|
||||||
-B
|
-B
|
||||||
@ -114,25 +125,32 @@ func DiffTest(t *testing.T, compute diff.ComputeEdits) {
|
|||||||
A
|
A
|
||||||
+C
|
+C
|
||||||
`[1:],
|
`[1:],
|
||||||
nodiff: true, // diff algorithm produces different delete/insert pattern
|
Edits: []diff.TextEdit{
|
||||||
|
{Span: newSpan(0, 4), NewText: ""},
|
||||||
|
{Span: newSpan(6, 6), NewText: "B\n"},
|
||||||
|
{Span: newSpan(10, 12), NewText: ""},
|
||||||
|
{Span: newSpan(14, 14), NewText: "C\n"},
|
||||||
|
},
|
||||||
|
NoDiff: true, // diff algorithm produces different delete/insert pattern
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "replace_last_line",
|
Name: "replace_last_line",
|
||||||
in: "A\nB\n",
|
In: "A\nB\n",
|
||||||
out: "A\nC\n\n",
|
Out: "A\nC\n\n",
|
||||||
unified: unifiedPrefix + `
|
Unified: UnifiedPrefix + `
|
||||||
@@ -1,2 +1,3 @@
|
@@ -1,2 +1,3 @@
|
||||||
A
|
A
|
||||||
-B
|
-B
|
||||||
+C
|
+C
|
||||||
+
|
+
|
||||||
`[1:],
|
`[1:],
|
||||||
|
Edits: []diff.TextEdit{{Span: newSpan(2, 3), NewText: "C\n"}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "mulitple_replace",
|
Name: "mulitple_replace",
|
||||||
in: "A\nB\nC\nD\nE\nF\nG\n",
|
In: "A\nB\nC\nD\nE\nF\nG\n",
|
||||||
out: "A\nH\nI\nJ\nE\nF\nK\n",
|
Out: "A\nH\nI\nJ\nE\nF\nK\n",
|
||||||
unified: unifiedPrefix + `
|
Unified: UnifiedPrefix + `
|
||||||
@@ -1,7 +1,7 @@
|
@@ -1,7 +1,7 @@
|
||||||
A
|
A
|
||||||
-B
|
-B
|
||||||
@ -146,71 +164,42 @@ func DiffTest(t *testing.T, compute diff.ComputeEdits) {
|
|||||||
-G
|
-G
|
||||||
+K
|
+K
|
||||||
`[1:],
|
`[1:],
|
||||||
}} {
|
Edits: []diff.TextEdit{
|
||||||
t.Run(test.name, func(t *testing.T) {
|
{Span: newSpan(2, 8), NewText: "H\nI\nJ\n"},
|
||||||
|
{Span: newSpan(12, 14), NewText: "K\n"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// expand all the spans to full versions
|
||||||
|
// we need them all to have their line number and column
|
||||||
|
for _, tc := range TestCases {
|
||||||
|
c := span.NewContentConverter("", []byte(tc.In))
|
||||||
|
for i := range tc.Edits {
|
||||||
|
tc.Edits[i].Span, _ = tc.Edits[i].Span.WithAll(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DiffTest(t *testing.T, compute diff.ComputeEdits) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
edits := compute(span.FileURI("/"+test.name), test.in, test.out)
|
for _, test := range TestCases {
|
||||||
got := diff.ApplyEdits(test.in, edits)
|
t.Run(test.Name, func(t *testing.T) {
|
||||||
unified := fmt.Sprint(diff.ToUnified("from", "to", test.in, edits))
|
t.Helper()
|
||||||
if got != test.out {
|
edits := compute(span.FileURI("/"+test.Name), test.In, test.Out)
|
||||||
t.Errorf("got patched:\n%v\nfrom diff:\n%v\nexpected:\n%v", got, unified, test.out)
|
got := diff.ApplyEdits(test.In, edits)
|
||||||
}
|
unified := fmt.Sprint(diff.ToUnified(FileA, FileB, test.In, edits))
|
||||||
if unified != test.unified {
|
if got != test.Out {
|
||||||
t.Errorf("got diff:\n%v\nexpected:\n%v", unified, test.unified)
|
t.Errorf("got patched:\n%v\nfrom diff:\n%v\nexpected:\n%v", got, unified, test.Out)
|
||||||
}
|
|
||||||
if *verifyDiff && !test.nodiff {
|
|
||||||
diff, err := getDiffOutput(test.in, test.out)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(diff) > 0 {
|
|
||||||
diff = unifiedPrefix + diff
|
|
||||||
}
|
|
||||||
if diff != test.unified {
|
|
||||||
t.Errorf("unified:\n%q\ndiff -u:\n%q", test.unified, diff)
|
|
||||||
}
|
}
|
||||||
|
if unified != test.Unified {
|
||||||
|
t.Errorf("got diff:\n%v\nexpected:\n%v", unified, test.Unified)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDiffOutput(a, b string) (string, error) {
|
func newSpan(start, end int) span.Span {
|
||||||
fileA, err := ioutil.TempFile("", "myers.in")
|
return span.New("", span.NewPoint(0, 0, start), span.NewPoint(0, 0, end))
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer os.Remove(fileA.Name())
|
|
||||||
if _, err := fileA.Write([]byte(a)); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if err := fileA.Close(); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
fileB, err := ioutil.TempFile("", "myers.in")
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer os.Remove(fileB.Name())
|
|
||||||
if _, err := fileB.Write([]byte(b)); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if err := fileB.Close(); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
cmd := exec.Command("diff", "-u", fileA.Name(), fileB.Name())
|
|
||||||
out, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
if _, ok := err.(*exec.ExitError); !ok {
|
|
||||||
return "", fmt.Errorf("failed to run diff -u %v %v: %v\n%v", fileA.Name(), fileB.Name(), err, string(out))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
diff := string(out)
|
|
||||||
if len(diff) <= 0 {
|
|
||||||
return diff, nil
|
|
||||||
}
|
|
||||||
bits := strings.SplitN(diff, "\n", 3)
|
|
||||||
if len(bits) != 3 {
|
|
||||||
return "", fmt.Errorf("diff output did not have file prefix:\n%s", diff)
|
|
||||||
}
|
|
||||||
return bits[2], nil
|
|
||||||
}
|
}
|
||||||
|
83
internal/lsp/diff/difftest/difftest_test.go
Normal file
83
internal/lsp/diff/difftest/difftest_test.go
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
// Copyright 2019 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package difftest supplies a set of tests that will operate on any
|
||||||
|
// implementation of a diff algorithm as exposed by
|
||||||
|
// "golang.org/x/tools/internal/lsp/diff"
|
||||||
|
package difftest_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/tools/internal/lsp/diff/difftest"
|
||||||
|
"golang.org/x/tools/internal/testenv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func VerifyUnifiedTest(t *testing.T) {
|
||||||
|
testenv.NeedsTool(t, "diff")
|
||||||
|
for _, test := range difftest.TestCases {
|
||||||
|
t.Run(test.Name, func(t *testing.T) {
|
||||||
|
t.Helper()
|
||||||
|
if test.NoDiff {
|
||||||
|
t.Skip("diff tool produces expected different results")
|
||||||
|
}
|
||||||
|
diff, err := getDiffOutput(test.In, test.Out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(diff) > 0 {
|
||||||
|
diff = difftest.UnifiedPrefix + diff
|
||||||
|
}
|
||||||
|
if diff != test.Unified {
|
||||||
|
t.Errorf("unified:\n%q\ndiff -u:\n%q", test.Unified, diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getDiffOutput(a, b string) (string, error) {
|
||||||
|
fileA, err := ioutil.TempFile("", "myers.in")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer os.Remove(fileA.Name())
|
||||||
|
if _, err := fileA.Write([]byte(a)); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if err := fileA.Close(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
fileB, err := ioutil.TempFile("", "myers.in")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer os.Remove(fileB.Name())
|
||||||
|
if _, err := fileB.Write([]byte(b)); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if err := fileB.Close(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
cmd := exec.Command("diff", "-u", fileA.Name(), fileB.Name())
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
if _, ok := err.(*exec.ExitError); !ok {
|
||||||
|
return "", fmt.Errorf("failed to run diff -u %v %v: %v\n%v", fileA.Name(), fileB.Name(), err, string(out))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
diff := string(out)
|
||||||
|
if len(diff) <= 0 {
|
||||||
|
return diff, nil
|
||||||
|
}
|
||||||
|
bits := strings.SplitN(diff, "\n", 3)
|
||||||
|
if len(bits) != 3 {
|
||||||
|
return "", fmt.Errorf("diff output did not have file prefix:\n%s", diff)
|
||||||
|
}
|
||||||
|
return bits[2], nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user