From b1d03573dade7e708260869c922282a218b50cd9 Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Sat, 21 Jun 2025 13:01:12 -0400 Subject: [PATCH] test(parser/diff): new cases --- internal/parser/diff/patience.go | 2 +- internal/parser/diff/patience_test.go | 196 ++++++++++++++++++++++++++ 2 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 internal/parser/diff/patience_test.go diff --git a/internal/parser/diff/patience.go b/internal/parser/diff/patience.go index 4d507a1..d302bce 100644 --- a/internal/parser/diff/patience.go +++ b/internal/parser/diff/patience.go @@ -160,7 +160,7 @@ func uniqueElements(a []string) ([]string, []int) { func patienceDiff(a, b []string, equal func(a, b string) bool) []DiffLine { switch { case len(a) == 0 && len(b) == 0: - return nil + return []DiffLine{} case len(a) == 0: return toDiffLines(b, Insert) case len(b) == 0: diff --git a/internal/parser/diff/patience_test.go b/internal/parser/diff/patience_test.go new file mode 100644 index 0000000..7149d74 --- /dev/null +++ b/internal/parser/diff/patience_test.go @@ -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) + } + } +}