1
0
mirror of https://github.com/golang/go synced 2024-11-19 01:04:40 -07:00
go/internal/lsp/diff/diff_test.go
Ian Cottrell a81264a823 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>
2019-04-07 02:04:52 +00:00

61 lines
1.5 KiB
Go

// 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
import (
"reflect"
"strings"
"testing"
)
func TestDiff(t *testing.T) {
for _, test := range []struct {
a, b string
lines []*Op
operations []*Op
}{
{
a: "A\nB\nC\nA\nB\nB\nA\n",
b: "C\nB\nA\nB\nA\nC\n",
operations: []*Op{
&Op{Kind: Delete, I1: 0, I2: 1, J1: 0},
&Op{Kind: Delete, I1: 1, I2: 2, J1: 0},
&Op{Kind: Insert, Content: []string{"B\n"}, I1: 3, I2: 3, J1: 1},
&Op{Kind: Delete, I1: 5, I2: 6, J1: 4},
&Op{Kind: Insert, Content: []string{"C\n"}, I1: 7, I2: 7, J1: 5},
},
},
{
a: "A\nB\n",
b: "A\nC\n\n",
operations: []*Op{
&Op{Kind: Delete, I1: 1, I2: 2, J1: 1},
&Op{Kind: Insert, Content: []string{"C\n"}, I1: 2, I2: 2, J1: 1},
&Op{Kind: Insert, Content: []string{"\n"}, I1: 2, I2: 2, J1: 2},
},
},
} {
a := strings.SplitAfter(test.a, "\n")
b := strings.SplitAfter(test.b, "\n")
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 {
want := test.operations[i]
if !reflect.DeepEqual(want, got) {
t.Errorf("expected %v, got %v", want, got)
}
}
applied := ApplyEdits(a, test.operations)
for i, want := range applied {
got := b[i]
if got != want {
t.Errorf("expected %v got %v", want, got)
}
}
}
}