internal/lsp: change diff tests to take strings not string arrays to make them more realistic

Change-Id: I7916c29c24cf581ddb4909a9beb8b0be3fb94c68
Reviewed-on: https://go-review.googlesource.com/c/tools/+/170881
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Ian Cottrell 2019-04-05 13:49:15 -04:00
parent 719e078ade
commit a81264a823
1 changed files with 19 additions and 16 deletions

View File

@ -6,48 +6,51 @@ package diff
import ( import (
"reflect" "reflect"
"strings"
"testing" "testing"
) )
func TestDiff(t *testing.T) { func TestDiff(t *testing.T) {
for _, tt := range []struct { for _, test := range []struct {
a, b []string a, b string
lines []*Op lines []*Op
operations []*Op operations []*Op
}{ }{
{ {
a: []string{"A", "B", "C", "A", "B", "B", "A"}, a: "A\nB\nC\nA\nB\nB\nA\n",
b: []string{"C", "B", "A", "B", "A", "C"}, b: "C\nB\nA\nB\nA\nC\n",
operations: []*Op{ operations: []*Op{
&Op{Kind: Delete, I1: 0, I2: 1, J1: 0}, &Op{Kind: Delete, I1: 0, I2: 1, J1: 0},
&Op{Kind: Delete, I1: 1, I2: 2, J1: 0}, &Op{Kind: Delete, I1: 1, I2: 2, J1: 0},
&Op{Kind: Insert, Content: []string{"B"}, I1: 3, I2: 3, J1: 1}, &Op{Kind: Insert, Content: []string{"B\n"}, I1: 3, I2: 3, J1: 1},
&Op{Kind: Delete, I1: 5, I2: 6, J1: 4}, &Op{Kind: Delete, I1: 5, I2: 6, J1: 4},
&Op{Kind: Insert, Content: []string{"C"}, I1: 7, I2: 7, J1: 5}, &Op{Kind: Insert, Content: []string{"C\n"}, I1: 7, I2: 7, J1: 5},
}, },
}, },
{ {
a: []string{"A", "B"}, a: "A\nB\n",
b: []string{"A", "C", ""}, b: "A\nC\n\n",
operations: []*Op{ operations: []*Op{
&Op{Kind: Delete, I1: 1, I2: 2, J1: 1}, &Op{Kind: Delete, I1: 1, I2: 2, J1: 1},
&Op{Kind: Insert, Content: []string{"C"}, I1: 2, I2: 2, J1: 1}, &Op{Kind: Insert, Content: []string{"C\n"}, I1: 2, I2: 2, J1: 1},
&Op{Kind: Insert, Content: []string{""}, I1: 2, I2: 2, J1: 2}, &Op{Kind: Insert, Content: []string{"\n"}, I1: 2, I2: 2, J1: 2},
}, },
}, },
} { } {
ops := Operations(tt.a, tt.b) a := strings.SplitAfter(test.a, "\n")
if len(ops) != len(tt.operations) { b := strings.SplitAfter(test.b, "\n")
t.Fatalf("expected %v operations, got %v", len(tt.operations), len(ops)) ops := Operations(a, b)
if len(ops) != len(test.operations) {
t.Fatalf("expected %v operations, got %v", len(test.operations), len(ops))
} }
for i, got := range ops { for i, got := range ops {
want := tt.operations[i] want := test.operations[i]
if !reflect.DeepEqual(want, got) { if !reflect.DeepEqual(want, got) {
t.Errorf("expected %v, got %v", want, got) t.Errorf("expected %v, got %v", want, got)
} }
} }
b := ApplyEdits(tt.a, tt.operations) applied := ApplyEdits(a, test.operations)
for i, want := range tt.b { for i, want := range applied {
got := b[i] got := b[i]
if got != want { if got != want {
t.Errorf("expected %v got %v", want, got) t.Errorf("expected %v got %v", want, got)