2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
|
|
|
|
local clear = n.clear
|
|
|
|
local exec_lua = n.exec_lua
|
2024-04-08 02:03:20 -07:00
|
|
|
local eq = t.eq
|
|
|
|
local pcall_err = t.pcall_err
|
2021-08-22 03:22:04 -07:00
|
|
|
|
|
|
|
describe('xdiff bindings', function()
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('can diff text', function()
|
2024-09-29 03:20:18 -07:00
|
|
|
local a1 = 'Hello\n'
|
|
|
|
local b1 = 'Helli\n'
|
|
|
|
|
|
|
|
local a2 = 'Hello\nbye\nfoo\n'
|
|
|
|
local b2 = 'Helli\nbye\nbar\nbaz\n'
|
2021-08-22 03:22:04 -07:00
|
|
|
|
|
|
|
it('with no callback', function()
|
|
|
|
eq(
|
|
|
|
table.concat({
|
|
|
|
'@@ -1 +1 @@',
|
|
|
|
'-Hello',
|
|
|
|
'+Helli',
|
|
|
|
'',
|
|
|
|
}, '\n'),
|
2024-09-29 03:20:18 -07:00
|
|
|
exec_lua(function()
|
|
|
|
return vim.diff(a1, b1)
|
|
|
|
end)
|
2021-08-22 03:22:04 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
eq(
|
|
|
|
table.concat({
|
|
|
|
'@@ -1 +1 @@',
|
|
|
|
'-Hello',
|
|
|
|
'+Helli',
|
|
|
|
'@@ -3 +3,2 @@',
|
|
|
|
'-foo',
|
|
|
|
'+bar',
|
|
|
|
'+baz',
|
|
|
|
'',
|
|
|
|
}, '\n'),
|
2024-09-29 03:20:18 -07:00
|
|
|
exec_lua(function()
|
|
|
|
return vim.diff(a2, b2)
|
|
|
|
end)
|
2021-08-22 03:22:04 -07:00
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('with callback', function()
|
|
|
|
eq(
|
|
|
|
{ { 1, 1, 1, 1 } },
|
2024-09-29 03:20:18 -07:00
|
|
|
exec_lua(function()
|
|
|
|
local exp = {} --- @type table[]
|
|
|
|
assert(vim.diff(a1, b1, {
|
|
|
|
on_hunk = function(...)
|
|
|
|
exp[#exp + 1] = { ... }
|
|
|
|
end,
|
|
|
|
}) == nil)
|
2021-08-22 03:22:04 -07:00
|
|
|
return exp
|
2024-09-29 03:20:18 -07:00
|
|
|
end)
|
2021-08-22 03:22:04 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
eq(
|
|
|
|
{ { 1, 1, 1, 1 }, { 3, 1, 3, 2 } },
|
2024-09-29 03:20:18 -07:00
|
|
|
exec_lua(function()
|
|
|
|
local exp = {} --- @type table[]
|
|
|
|
assert(vim.diff(a2, b2, {
|
|
|
|
on_hunk = function(...)
|
|
|
|
exp[#exp + 1] = { ... }
|
|
|
|
end,
|
|
|
|
}) == nil)
|
2021-08-22 03:22:04 -07:00
|
|
|
return exp
|
2024-09-29 03:20:18 -07:00
|
|
|
end)
|
2021-08-22 03:22:04 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
-- gives higher precedence to on_hunk over result_type
|
|
|
|
eq(
|
|
|
|
{ { 1, 1, 1, 1 }, { 3, 1, 3, 2 } },
|
2024-09-29 03:20:18 -07:00
|
|
|
exec_lua(function()
|
|
|
|
local exp = {} --- @type table[]
|
|
|
|
assert(vim.diff(a2, b2, {
|
|
|
|
on_hunk = function(...)
|
|
|
|
exp[#exp + 1] = { ... }
|
|
|
|
end,
|
|
|
|
result_type = 'indices',
|
|
|
|
}) == nil)
|
2021-08-22 03:22:04 -07:00
|
|
|
return exp
|
2024-09-29 03:20:18 -07:00
|
|
|
end)
|
2021-08-22 03:22:04 -07:00
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('with error callback', function()
|
2022-11-14 03:01:35 -07:00
|
|
|
eq(
|
2024-09-29 03:20:18 -07:00
|
|
|
[[.../xdiff_spec.lua:0: error running function on_hunk: .../xdiff_spec.lua:0: ERROR1]],
|
|
|
|
pcall_err(exec_lua, function()
|
|
|
|
vim.diff(a1, b1, {
|
|
|
|
on_hunk = function()
|
|
|
|
error('ERROR1')
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end)
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-08-22 03:22:04 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('with hunk_lines', function()
|
2024-09-29 03:20:18 -07:00
|
|
|
eq(
|
|
|
|
{ { 1, 1, 1, 1 } },
|
|
|
|
exec_lua(function()
|
|
|
|
return vim.diff(a1, b1, { result_type = 'indices' })
|
|
|
|
end)
|
|
|
|
)
|
2021-08-22 03:22:04 -07:00
|
|
|
|
|
|
|
eq(
|
|
|
|
{ { 1, 1, 1, 1 }, { 3, 1, 3, 2 } },
|
2024-09-29 03:20:18 -07:00
|
|
|
exec_lua(function()
|
|
|
|
return vim.diff(a2, b2, { result_type = 'indices' })
|
|
|
|
end)
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-08-22 03:22:04 -07:00
|
|
|
end)
|
|
|
|
|
2022-02-05 10:49:48 -07:00
|
|
|
it('can run different algorithms', function()
|
|
|
|
local a = table.concat({
|
|
|
|
'.foo1 {',
|
|
|
|
' margin: 0;',
|
|
|
|
'}',
|
|
|
|
'',
|
|
|
|
'.bar {',
|
|
|
|
' margin: 0;',
|
|
|
|
'}',
|
|
|
|
'',
|
|
|
|
}, '\n')
|
|
|
|
|
|
|
|
local b = table.concat({
|
|
|
|
'.bar {',
|
|
|
|
' margin: 0;',
|
|
|
|
'}',
|
|
|
|
'',
|
|
|
|
'.foo1 {',
|
|
|
|
' margin: 0;',
|
|
|
|
' color: green;',
|
|
|
|
'}',
|
|
|
|
'',
|
|
|
|
}, '\n')
|
|
|
|
|
|
|
|
eq(
|
|
|
|
table.concat({
|
|
|
|
'@@ -1,4 +0,0 @@',
|
|
|
|
'-.foo1 {',
|
|
|
|
'- margin: 0;',
|
|
|
|
'-}',
|
|
|
|
'-',
|
|
|
|
'@@ -7,0 +4,5 @@',
|
|
|
|
'+',
|
|
|
|
'+.foo1 {',
|
|
|
|
'+ margin: 0;',
|
|
|
|
'+ color: green;',
|
|
|
|
'+}',
|
|
|
|
'',
|
|
|
|
}, '\n'),
|
2024-09-29 03:20:18 -07:00
|
|
|
exec_lua(function()
|
|
|
|
return vim.diff(a, b, {
|
|
|
|
algorithm = 'patience',
|
2022-02-05 10:49:48 -07:00
|
|
|
})
|
2024-09-29 03:20:18 -07:00
|
|
|
end)
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2022-02-05 10:49:48 -07:00
|
|
|
end)
|
2021-08-22 03:22:04 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can handle bad args', function()
|
|
|
|
eq([[Expected at least 2 arguments]], pcall_err(exec_lua, [[vim.diff('a')]]))
|
|
|
|
|
2022-11-14 03:01:35 -07:00
|
|
|
eq([[bad argument #1 to 'diff' (expected string)]], pcall_err(exec_lua, [[vim.diff(1, 2)]]))
|
2021-08-22 03:22:04 -07:00
|
|
|
|
2022-11-14 03:01:35 -07:00
|
|
|
eq(
|
|
|
|
[[bad argument #3 to 'diff' (expected table)]],
|
2021-08-22 03:22:04 -07:00
|
|
|
pcall_err(exec_lua, [[vim.diff('a', 'b', true)]])
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-08-22 03:22:04 -07:00
|
|
|
|
2024-02-11 11:13:38 -07:00
|
|
|
eq([[invalid key: bad_key]], pcall_err(exec_lua, [[vim.diff('a', 'b', { bad_key = true })]]))
|
2021-08-22 03:22:04 -07:00
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
2022-11-14 03:01:35 -07:00
|
|
|
[[on_hunk is not a function]],
|
2021-08-22 03:22:04 -07:00
|
|
|
pcall_err(exec_lua, [[vim.diff('a', 'b', { on_hunk = true })]])
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-08-22 03:22:04 -07:00
|
|
|
end)
|
2024-09-26 08:10:11 -07:00
|
|
|
|
|
|
|
it('can handle strings with embedded NUL characters (GitHub #30305)', function()
|
|
|
|
eq(
|
|
|
|
{ { 0, 0, 1, 1 }, { 1, 0, 3, 2 } },
|
|
|
|
exec_lua(function()
|
|
|
|
return vim.diff('\n', '\0\n\n\nb', { linematch = true, result_type = 'indices' })
|
|
|
|
end)
|
|
|
|
)
|
|
|
|
end)
|
2021-08-22 03:22:04 -07:00
|
|
|
end)
|