1
0
mirror of https://github.com/regclient/regclient.git synced 2025-04-20 09:47:54 +03:00
regclient/internal/diff/diff_test.go
Brandon Mitchell 0ad815ee02
Update tests to use t.Parallel where possible
Signed-off-by: Brandon Mitchell <git@bmitch.net>
2023-10-02 20:09:11 -04:00

148 lines
2.4 KiB
Go

package diff
import "testing"
func TestDiff(t *testing.T) {
t.Parallel()
tests := []struct {
name string
a, b, expect []string
opts []Opt
}{
{
name: "empty",
},
{
name: "deletes",
a: []string{"a", "b", "c"},
expect: []string{
"@@ -1,3 +1,0 @@",
"- a",
"- b",
"- c",
},
},
{
name: "inserts",
b: []string{"a", "b", "c"},
expect: []string{
"@@ -1,0 +1,3 @@",
"+ a",
"+ b",
"+ c",
},
},
{
name: "equal",
a: []string{"a", "b", "c"},
b: []string{"a", "b", "c"},
},
{
name: "myers",
a: []string{"a", "b", "c", "a", "b", "b", "a"},
b: []string{"c", "b", "a", "b", "a", "c"},
expect: []string{
"@@ -1,2 +1,0 @@",
"- a",
"- b",
"@@ -4,0 +2,1 @@",
"+ b",
"@@ -6,1 +5,0 @@",
"- b",
"@@ -8,0 +6,1 @@",
"+ c",
},
},
{
name: "replace",
a: []string{"a", "b", "c"},
b: []string{"d", "e", "f"},
expect: []string{
"@@ -1,3 +1,3 @@",
"- a",
"- b",
"- c",
"+ d",
"+ e",
"+ f",
},
},
{
name: "change one",
a: []string{"a", "b", "c", "d"},
b: []string{"a", "e", "f", "d"},
expect: []string{
"@@ -2,2 +2,2 @@",
"- b",
"- c",
"+ e",
"+ f",
},
},
{
name: "context one",
a: []string{"a", "b", "c", "d", "e"},
b: []string{"a", "b", "f", "d", "e"},
opts: []Opt{WithContext(1, 1)},
expect: []string{
"@@ -2,3 +2,3 @@",
" b",
"- c",
"+ f",
" d",
},
},
{
name: "context three",
a: []string{"a", "b", "c", "d", "e"},
b: []string{"a", "b", "f", "d", "e"},
opts: []Opt{WithContext(3, 3)},
expect: []string{
"@@ -1,5 +1,5 @@",
" a",
" b",
"- c",
"+ f",
" d",
" e",
},
},
{
name: "context full",
a: []string{"a", "b", "c", "d", "e"},
b: []string{"a", "b", "f", "d", "e"},
opts: []Opt{WithFullContext()},
expect: []string{
"@@ -1,5 +1,5 @@",
" a",
" b",
"- c",
"+ f",
" d",
" e",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := Diff(tt.a, tt.b, tt.opts...)
if !strSliceEq(tt.expect, result) {
t.Errorf("mismatch, expected %v, received %v", tt.expect, result)
}
})
}
}
func strSliceEq(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}