test: refactor exec_lua in xdiff_spec

This commit is contained in:
Lewis Russell 2024-09-29 11:20:18 +01:00
parent c65646c247
commit 0fb5299e53
3 changed files with 58 additions and 47 deletions

View File

@ -715,7 +715,7 @@ vim.diff({a}, {b}, {opts}) *vim.diff()*
• {b} (`string`) Second string to compare
• {opts} (`table?`) Optional parameters:
• {on_hunk}?
(`fun(start_a: integer, count_a: integer, start_b: integer, count_b: integer): integer`)
(`fun(start_a: integer, count_a: integer, start_b: integer, count_b: integer): integer?`)
Invoked for each hunk in the diff. Return a negative number
to cancel the callback for any remaining hunks. Arguments:
• `start_a` (`integer`): Start line of hunk in {a}.

View File

@ -11,7 +11,7 @@
--- - `count_a` (`integer`): Hunk size in {a}.
--- - `start_b` (`integer`): Start line of hunk in {b}.
--- - `count_b` (`integer`): Hunk size in {b}.
--- @field on_hunk? fun(start_a: integer, count_a: integer, start_b: integer, count_b: integer): integer
--- @field on_hunk? fun(start_a: integer, count_a: integer, start_b: integer, count_b: integer): integer?
---
--- Form of the returned diff:
--- - `unified`: String in unified format.

View File

@ -12,15 +12,11 @@ describe('xdiff bindings', function()
end)
describe('can diff text', function()
before_each(function()
exec_lua [[
a1 = 'Hello\n'
b1 = 'Helli\n'
local a1 = 'Hello\n'
local b1 = 'Helli\n'
a2 = 'Hello\nbye\nfoo\n'
b2 = 'Helli\nbye\nbar\nbaz\n'
]]
end)
local a2 = 'Hello\nbye\nfoo\n'
local b2 = 'Helli\nbye\nbar\nbaz\n'
it('with no callback', function()
eq(
@ -30,7 +26,9 @@ describe('xdiff bindings', function()
'+Helli',
'',
}, '\n'),
exec_lua('return vim.diff(a1, b1)')
exec_lua(function()
return vim.diff(a1, b1)
end)
)
eq(
@ -44,63 +42,81 @@ describe('xdiff bindings', function()
'+baz',
'',
}, '\n'),
exec_lua('return vim.diff(a2, b2)')
exec_lua(function()
return vim.diff(a2, b2)
end)
)
end)
it('with callback', function()
exec_lua([[on_hunk = function(sa, ca, sb, cb)
exp[#exp+1] = {sa, ca, sb, cb}
end]])
eq(
{ { 1, 1, 1, 1 } },
exec_lua [[
exp = {}
assert(vim.diff(a1, b1, {on_hunk = on_hunk}) == nil)
exec_lua(function()
local exp = {} --- @type table[]
assert(vim.diff(a1, b1, {
on_hunk = function(...)
exp[#exp + 1] = { ... }
end,
}) == nil)
return exp
]]
end)
)
eq(
{ { 1, 1, 1, 1 }, { 3, 1, 3, 2 } },
exec_lua [[
exp = {}
assert(vim.diff(a2, b2, {on_hunk = on_hunk}) == nil)
exec_lua(function()
local exp = {} --- @type table[]
assert(vim.diff(a2, b2, {
on_hunk = function(...)
exp[#exp + 1] = { ... }
end,
}) == nil)
return exp
]]
end)
)
-- gives higher precedence to on_hunk over result_type
eq(
{ { 1, 1, 1, 1 }, { 3, 1, 3, 2 } },
exec_lua [[
exp = {}
assert(vim.diff(a2, b2, {on_hunk = on_hunk, result_type='indices'}) == nil)
exec_lua(function()
local exp = {} --- @type table[]
assert(vim.diff(a2, b2, {
on_hunk = function(...)
exp[#exp + 1] = { ... }
end,
result_type = 'indices',
}) == nil)
return exp
]]
end)
)
end)
it('with error callback', function()
exec_lua [[
on_hunk = function(sa, ca, sb, cb)
error('ERROR1')
end
]]
eq(
[[error running function on_hunk: [string "<nvim>"]:0: ERROR1]],
pcall_err(exec_lua, [[vim.diff(a1, b1, {on_hunk = on_hunk})]])
[[.../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)
)
end)
it('with hunk_lines', function()
eq({ { 1, 1, 1, 1 } }, exec_lua([[return vim.diff(a1, b1, {result_type = 'indices'})]]))
eq(
{ { 1, 1, 1, 1 } },
exec_lua(function()
return vim.diff(a1, b1, { result_type = 'indices' })
end)
)
eq(
{ { 1, 1, 1, 1 }, { 3, 1, 3, 2 } },
exec_lua([[return vim.diff(a2, b2, {result_type = 'indices'})]])
exec_lua(function()
return vim.diff(a2, b2, { result_type = 'indices' })
end)
)
end)
@ -143,16 +159,11 @@ describe('xdiff bindings', function()
'+}',
'',
}, '\n'),
exec_lua(
[[
local args = {...}
return vim.diff(args[1], args[2], {
algorithm = 'patience'
exec_lua(function()
return vim.diff(a, b, {
algorithm = 'patience',
})
]],
a,
b
)
end)
)
end)
end)