forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_spec.lua
More file actions
111 lines (105 loc) · 2.41 KB
/
Copy pathdiff_spec.lua
File metadata and controls
111 lines (105 loc) · 2.41 KB
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
local diff = require('CopilotChat.utils.diff')
describe('CopilotChat.utils.diff', function()
it('parses unified diff', function()
local diff_text = [[
--- a/foo.txt
+++ b/foo.txt
@@ ... @@
context line
-old line
+new line
]]
local file_path, hunks = diff.parse_unified_diff(diff_text)
assert.equals('b/foo.txt', file_path)
assert.equals('context line', hunks[1].context[1])
assert.equals('old line', hunks[1].minus[1])
assert.equals('new line', hunks[1].plus[1])
end)
it('applies unified diff', function()
local diff_text = [[
--- a/foo.txt
+++ b/foo.txt
@@ ... @@
context
-old
+new
]]
local original = { 'context', 'old', 'other' }
local result, applied = diff.apply_unified_diff(diff_text, original)
assert.is_true(applied)
assert.are.same({ 'context', 'new', 'other' }, result)
end)
it('gets unified diff region', function()
local diff_text = [[
--- a/foo.txt
+++ b/foo.txt
@@ ... @@
context
-old
+new
]]
local original = { 'context', 'old', 'other' }
local first, last = diff.get_unified_diff_region(diff_text, original)
assert.equals(2, first)
assert.equals(2, last)
end)
it('applies unified diff with no context', function()
local diff_text = [[
--- a/foo.txt
+++ b/foo.txt
@@ ... @@
-old
+new
]]
local original = { 'old', 'other' }
local result, applied = diff.apply_unified_diff(diff_text, original)
assert.is_true(applied)
assert.are.same({ 'new', 'other' }, result)
end)
it('applies unified diff with multiline edits', function()
local diff_text = [[
--- a/foo.txt
+++ b/foo.txt
@@ ... @@
context1
context2
-old1
-old2
+new1
+new2
]]
local original = {
'context1',
'context2',
'old1',
'old2',
'context3',
'other',
}
local result, applied = diff.apply_unified_diff(diff_text, original)
assert.is_true(applied)
assert.are.same({
'context1',
'context2',
'new1',
'new2',
'context3',
'other',
}, result)
end)
it('does not apply ambiguous edit', function()
local diff_text = [[
--- a/foo.txt
+++ b/foo.txt
@@ ... @@
context
-old
+new
]]
local original = { 'context', 'old', 'context', 'old' }
local result, applied = diff.apply_unified_diff(diff_text, original)
-- Should not apply because there are two possible matches
assert.is_false(applied)
assert.are.same({ 'context', 'old', 'context', 'old' }, result)
end)
end)