File: patch_test.go

package info (click to toggle)
kitty 0.46.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,064 kB
  • sloc: ansic: 91,642; python: 59,657; objc: 6,273; sh: 1,333; xml: 364; makefile: 144; javascript: 78
file content (135 lines) | stat: -rw-r--r-- 4,488 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>

package diff

import (
	"regexp"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)

var region_eq = cmpopts.EquateComparable(Region{})

func TestWordDiffCenter(t *testing.T) {
	re := regexp.MustCompile(`\S+`)
	type tc struct {
		left, right   string
		left_regions  []Region
		right_regions []Region
	}
	tests := []tc{
		{
			// word count equal, single substitution at index 1 → positional pair
			// "quick" vs "slow": no common chars → full words
			left: "the quick brown fox", right: "the slow brown fox",
			left_regions:  []Region{{4, 5}},
			right_regions: []Region{{4, 4}},
		},
		{
			left: "hello world", right: "hello world",
			left_regions:  nil,
			right_regions: nil,
		},
		{
			// word count equal, single substitution at index 1 → positional pair
			// "bar" vs "qux": no common chars → full words
			left: "foo bar baz", right: "foo qux baz",
			left_regions:  []Region{{4, 3}},
			right_regions: []Region{{4, 3}},
		},
		{
			// left has 3 words, right has 4 → word counts differ with unmatched
			// changed words → fall back to changed_center
			// changed_center gives: offset=4 (common "aaa "), suffix="ccc"(4)
			// left_size=3 ("bbb"), right_size=7 ("xxx yyy")
			left: "aaa bbb ccc", right: "aaa xxx yyy ccc",
			left_regions:  []Region{{4, 3}},
			right_regions: []Region{{4, 7}},
		},
		{
			// word on left deleted: unmatched changed word → fall back to changed_center
			// changed_center: prefix="aaa "(4), suffix=" ccc ddd"(8)
			// left_size=3 ("bbb"), right_size=-1 → nil
			left: "aaa bbb ccc ddd", right: "aaa ccc ddd",
			left_regions:  []Region{{4, 3}},
			right_regions: nil,
		},
		{
			// word counts equal, single substitution at index 3 → positional pair
			// "fox" vs "cat": no common chars → full words
			left: "the quick brown fox over the lazy dog", right: "the quick brown cat over the lazy dog",
			left_regions:  []Region{{16, 3}},
			right_regions: []Region{{16, 3}},
		},
		{
			// single word, positional pair with common char prefix "version" (7)
			left:          "version1",
			right:         "version2",
			left_regions:  []Region{{7, 1}},
			right_regions: []Region{{7, 1}},
		},
		{
			// positional pair at index 1, char prefix="prefix"(6), suffix="suffix"(6)
			// word at offset 7 → highlight offset 13, size 2
			left:          "update prefixABsuffix done",
			right:         "update prefixCDsuffix done",
			left_regions:  []Region{{13, 2}},
			right_regions: []Region{{13, 2}},
		},
		{
			// equal word count, multiple positional pairs (all words changed)
			// each pair has no common chars → full-word regions
			left: "aaa bbb ccc", right: "xxx yyy zzz",
			left_regions:  []Region{{0, 3}, {4, 3}, {8, 3}},
			right_regions: []Region{{0, 3}, {4, 3}, {8, 3}},
		},
		{
			// pure insertion on right side → unmatched changed word → fall back to changed_center
			// changed_center finds common prefix "aaa bbb ccc" (11 bytes) and suffix "";
			// left_size=0 (nil), right_size=4 (" ddd" inserted at the end)
			left: "aaa bbb ccc", right: "aaa bbb ccc ddd",
			left_regions:  nil,
			right_regions: []Region{{11, 4}},
		},
	}
	for _, tc := range tests {
		c := word_diff_center(tc.left, tc.right, re)
		if diff := cmp.Diff(tc.left_regions, c.left_regions, region_eq); diff != "" {
			t.Errorf("word_diff_center(%q, %q) left_regions mismatch: %s", tc.left, tc.right, diff)
		}
		if diff := cmp.Diff(tc.right_regions, c.right_regions, region_eq); diff != "" {
			t.Errorf("word_diff_center(%q, %q) right_regions mismatch: %s", tc.left, tc.right, diff)
		}
	}
}

func TestChangedCenter(t *testing.T) {
	type tc struct {
		left, right   string
		left_regions  []Region
		right_regions []Region
	}
	tests := []tc{
		{
			left: "the quick brown fox", right: "the slow brown fox",
			left_regions:  []Region{{4, 5}},
			right_regions: []Region{{4, 4}},
		},
		{
			left: "hello world", right: "hello world",
			left_regions:  nil,
			right_regions: nil,
		},
	}
	for _, tc := range tests {
		c := changed_center(tc.left, tc.right)
		if diff := cmp.Diff(tc.left_regions, c.left_regions, region_eq); diff != "" {
			t.Errorf("changed_center(%q, %q) left_regions mismatch: %s", tc.left, tc.right, diff)
		}
		if diff := cmp.Diff(tc.right_regions, c.right_regions, region_eq); diff != "" {
			t.Errorf("changed_center(%q, %q) right_regions mismatch: %s", tc.left, tc.right, diff)
		}
	}
}