test(parser/diff): new cases
All checks were successful
submodules sync / sync (push) Successful in 1m9s
build / build (push) Successful in 2m34s
build / trigger-build-image (push) Successful in 9s

This commit is contained in:
张泊明518370910136 2025-06-21 13:01:12 -04:00
parent f04a471969
commit b1d03573da
GPG Key ID: D47306D7062CDA9D
2 changed files with 197 additions and 1 deletions

View File

@ -160,7 +160,7 @@ func uniqueElements(a []string) ([]string, []int) {
func patienceDiff(a, b []string, equal func(a, b string) bool) []DiffLine { func patienceDiff(a, b []string, equal func(a, b string) bool) []DiffLine {
switch { switch {
case len(a) == 0 && len(b) == 0: case len(a) == 0 && len(b) == 0:
return nil return []DiffLine{}
case len(a) == 0: case len(a) == 0:
return toDiffLines(b, Insert) return toDiffLines(b, Insert)
case len(b) == 0: case len(b) == 0:

View File

@ -0,0 +1,196 @@
package diff
import (
"reflect"
"testing"
)
func TestStringsEqual(t *testing.T) {
testCases := []struct {
str1 string
str2 string
compareSpace bool
expected bool
}{
{
str1: "hello",
str2: "hello",
compareSpace: true,
expected: true,
},
{
str1: "hello",
str2: "hello",
compareSpace: false,
expected: true,
},
{
str1: "hello",
str2: "world",
compareSpace: true,
expected: false,
},
{
str1: "hello",
str2: "world",
compareSpace: false,
expected: false,
},
{
str1: "hello ",
str2: "hello",
compareSpace: true,
expected: false,
},
{
str1: "hello ",
str2: "hello",
compareSpace: false,
expected: true,
},
{
str1: "hello\t",
str2: "hello",
compareSpace: true,
expected: false,
},
{
str1: "hello\t",
str2: "hello",
compareSpace: false,
expected: true,
},
{
str1: "hello world",
str2: "hello world",
compareSpace: true,
expected: false,
},
{
str1: "hello world",
str2: "hello world",
compareSpace: false,
expected: true,
},
{
str1: "hello\tworld",
str2: "hello world",
compareSpace: false,
expected: true,
},
{
str1: "hello\tworld",
str2: "hello world",
compareSpace: true,
expected: false,
},
{
str1: "",
str2: "",
compareSpace: true,
expected: true,
},
{
str1: "",
str2: "",
compareSpace: false,
expected: true,
},
{
str1: " ",
str2: "",
compareSpace: true,
expected: false,
},
{
str1: " ",
str2: "",
compareSpace: false,
expected: true,
},
{
str1: "hello\n",
str2: "hello",
compareSpace: false,
expected: true,
},
{
str1: "hello\n",
str2: "hello",
compareSpace: true,
expected: false,
},
}
for _, tc := range testCases {
actual := stringsEqual(tc.str1, tc.str2, tc.compareSpace)
if actual != tc.expected {
t.Errorf("stringsEqual(%q, %q, %v) = %v, expected %v", tc.str1, tc.str2, tc.compareSpace, actual, tc.expected)
}
}
}
func TestPatienceDiff(t *testing.T) {
equal := func(a, b string) bool {
return a == b
}
testCases := []struct {
a []string
b []string
expected []DiffLine
}{
{
a: []string{},
b: []string{},
expected: []DiffLine{},
},
{
a: []string{"a", "b", "c"},
b: []string{"a", "b", "c"},
expected: []DiffLine{{Text: "a", Type: Equal}, {Text: "b", Type: Equal}, {Text: "c", Type: Equal}},
},
{
a: []string{"a", "b", "c"},
b: []string{"a", "b", "d"},
expected: []DiffLine{{Text: "a", Type: Equal}, {Text: "b", Type: Equal}, {Text: "c", Type: Delete}, {Text: "d", Type: Insert}},
},
{
a: []string{"a", "b", "c"},
b: []string{"a", "d", "c"},
expected: []DiffLine{{Text: "a", Type: Equal}, {Text: "b", Type: Delete}, {Text: "d", Type: Insert}, {Text: "c", Type: Equal}},
},
{
a: []string{"a", "b", "c"},
b: []string{"d", "e", "f"},
expected: []DiffLine{{Text: "a", Type: Delete}, {Text: "b", Type: Delete}, {Text: "c", Type: Delete}, {Text: "d", Type: Insert}, {Text: "e", Type: Insert}, {Text: "f", Type: Insert}},
},
{
a: []string{"a", "b", "c"},
b: []string{"a", "b", "c", "d"},
expected: []DiffLine{{Text: "a", Type: Equal}, {Text: "b", Type: Equal}, {Text: "c", Type: Equal}, {Text: "d", Type: Insert}},
},
{
a: []string{"a", "b", "c", "d"},
b: []string{"a", "b", "c"},
expected: []DiffLine{{Text: "a", Type: Equal}, {Text: "b", Type: Equal}, {Text: "c", Type: Equal}, {Text: "d", Type: Delete}},
},
{
a: []string{"a", "b", "a", "c"},
b: []string{"a", "b", "b", "c"},
expected: []DiffLine{{Text: "a", Type: Equal}, {Text: "b", Type: Equal}, {Text: "a", Type: Delete}, {Text: "b", Type: Insert}, {Text: "c", Type: Equal}},
},
{
a: []string{"a", "b", "c", "b", "a"},
b: []string{"b", "c", "b", "a", "d"},
expected: []DiffLine{{Text: "a", Type: Delete}, {Text: "b", Type: Equal}, {Text: "c", Type: Equal}, {Text: "b", Type: Equal}, {Text: "a", Type: Equal}, {Text: "d", Type: Insert}},
},
}
for _, tc := range testCases {
actual := patienceDiff(tc.a, tc.b, equal)
if !reflect.DeepEqual(actual, tc.expected) {
t.Errorf("patienceDiff(%q, %q) = %v, expected %v", tc.a, tc.b, actual, tc.expected)
}
}
}