2019-01-17 09:59:05 -07:00
|
|
|
// 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"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiff(t *testing.T) {
|
|
|
|
for _, tt := range []struct {
|
|
|
|
a, b []string
|
|
|
|
lines []*Op
|
|
|
|
operations []*Op
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
a: []string{"A", "B", "C", "A", "B", "B", "A"},
|
|
|
|
b: []string{"C", "B", "A", "B", "A", "C"},
|
|
|
|
operations: []*Op{
|
|
|
|
&Op{Kind: Delete, I1: 0, I2: 1, J1: 0, J2: 0},
|
|
|
|
&Op{Kind: Delete, I1: 1, I2: 2, J1: 0, J2: 0},
|
|
|
|
&Op{Kind: Insert, Content: "B", I1: 3, I2: 3, J1: 1, J2: 2},
|
|
|
|
&Op{Kind: Delete, I1: 5, I2: 6, J1: 4, J2: 4},
|
|
|
|
&Op{Kind: Insert, Content: "C", I1: 7, I2: 7, J1: 5, J2: 6},
|
|
|
|
},
|
|
|
|
},
|
2019-02-12 11:51:16 -07:00
|
|
|
{
|
|
|
|
a: []string{"A", "B"},
|
|
|
|
b: []string{"A", "C", ""},
|
|
|
|
operations: []*Op{
|
|
|
|
&Op{Kind: Delete, I1: 1, I2: 2, J1: 1, J2: 1},
|
|
|
|
&Op{Kind: Insert, Content: "C", I1: 2, I2: 2, J1: 1, J2: 2},
|
|
|
|
&Op{Kind: Insert, Content: "", I1: 2, I2: 2, J1: 2, J2: 3},
|
|
|
|
},
|
|
|
|
},
|
2019-01-17 09:59:05 -07:00
|
|
|
} {
|
2019-02-12 11:51:16 -07:00
|
|
|
ops := Operations(tt.a, tt.b)
|
|
|
|
if len(ops) != len(tt.operations) {
|
|
|
|
t.Fatalf("expected %v operations, got %v", len(tt.operations), len(ops))
|
2019-01-17 09:59:05 -07:00
|
|
|
}
|
2019-02-12 11:51:16 -07:00
|
|
|
for i, got := range ops {
|
2019-01-17 09:59:05 -07:00
|
|
|
want := tt.operations[i]
|
|
|
|
if !reflect.DeepEqual(want, got) {
|
|
|
|
t.Errorf("expected %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 11:51:16 -07:00
|
|
|
b := ApplyEdits(tt.a, tt.operations)
|
2019-01-17 09:59:05 -07:00
|
|
|
for i, want := range tt.b {
|
|
|
|
got := b[i]
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("expected %v got %v", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|