diff --git a/internal/lsp/diff/diff_test.go b/internal/lsp/diff/diff_test.go new file mode 100644 index 0000000000..be7965b156 --- /dev/null +++ b/internal/lsp/diff/diff_test.go @@ -0,0 +1,15 @@ +// 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 diff_test + +import ( + "testing" + + "golang.org/x/tools/internal/lsp/diff/difftest" +) + +func TestDiff(t *testing.T) { + difftest.DiffTest(t) +} diff --git a/internal/lsp/diff/difftest/difftest.go b/internal/lsp/diff/difftest/difftest.go new file mode 100644 index 0000000000..f7db02d79f --- /dev/null +++ b/internal/lsp/diff/difftest/difftest.go @@ -0,0 +1,51 @@ +// 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 + +import ( + "testing" + + "golang.org/x/tools/internal/lsp/diff" + "golang.org/x/tools/internal/span" +) + +func DiffTest(t *testing.T) { + t.Helper() + for _, test := range []struct{ name, in, out, unified string }{{ + name: "empty", + in: "", + out: "", + }, { + name: "no_diff", + in: "gargantuan", + out: "gargantuan", + }, { + name: "insert_rune", + in: "gord", + out: "gourd", + }, { + name: "delete_rune", + in: "groat", + out: "goat", + }, { + name: "replace_rune", + in: "loud", + out: "lord", + }, { + name: "insert_line", + in: "one\nthree\n", + out: "one\ntwo\nthree\n", + }} { + edits := diff.ComputeEdits(span.FileURI("/"+test.name), test.in, test.out) + got := diff.ApplyEdits(test.in, edits) + if got != test.out { + t.Logf("test %v had diff:%v\n", test.name, diff.ToUnified(test.name+".orig", test.name, test.in, edits)) + t.Errorf("diff %v got:\n%v\nexpected:\n%v", test.name, got, test.out) + } + } +} diff --git a/internal/lsp/diff/myers/diff.go b/internal/lsp/diff/myers/diff.go index ddf04cae6c..8c2daeb2c2 100644 --- a/internal/lsp/diff/myers/diff.go +++ b/internal/lsp/diff/myers/diff.go @@ -67,6 +67,10 @@ func ApplyEdits(a []string, operations []*Op) []string { // Operations returns the list of operations to convert a into b, consolidating // operations for multiple lines and not including equal lines. func Operations(a, b []string) []*Op { + if len(a) == 0 && len(b) == 0 { + return nil + } + trace, offset := shortestEditSequence(a, b) snakes := backtrack(trace, len(a), len(b), offset)