Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tronikelis committed Nov 2, 2024
1 parent 7eead6a commit 4376797
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 13 deletions.
3 changes: 1 addition & 2 deletions lua/conflict-marker/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,11 @@ function Conflict:conflict_range_without_base()
end

local base = utils.target_in_range(from, to, self:two_way_search(CONFLICT_BASE))
local mid = utils.target_in_range(from, to, self:two_way_search(CONFLICT_MID))

if not base then
return from, to
end

local mid = utils.target_in_range(from, to, self:two_way_search(CONFLICT_MID))
if not mid then
return
end
Expand Down
148 changes: 137 additions & 11 deletions test/main_spec.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
describe("main", function()
local nvim

local buf
local lines

local exec_lua = function(fn, ...)
return vim.fn.rpcrequest(nvim, "nvim_exec_lua", fn, { ... })
end

before_each(function()
buf = 0
lines = {
[[<<<<<<< HEAD]],
[[local value = 5 + 7]],
Expand All @@ -16,19 +17,144 @@ describe("main", function()
[[>>>>>>> new_branch]],
}

nvim = vim.fn.jobstart(
{ "nvim", "--embed", "--headless", "-c", 'lua require("conflict-marker").setup()' },
{ rpc = true }
)
vim.fn.rpcrequest(nvim, "nvim_buf_set_lines", buf, 0, -1, true, lines)
nvim = vim.fn.jobstart({ "nvim", "--embed", "--headless" }, { rpc = true })
exec_lua([[require("conflict-marker").setup()]])
end)

after_each(function()
vim.fn.jobstop(nvim)
end)

it("sets lines correctly", function()
local result = vim.fn.rpcrequest(nvim, "nvim_buf_get_lines", buf, 0, -1, true)
assert.is_same(result, lines)
for _, prepare in ipairs({
{
"diff2",
function()
lines = {
[[<<<<<<< HEAD]],
[[local value = 5 + 7]],
[[print(value)]],
[[print(string.format("value is %d", value))]],
[[=======]],
[[local value = 1 - 1]],
[[>>>>>>> new_branch]],
}
end,
},
{
"diff3",
function()
lines = {
[[<<<<<<< HEAD]],
[[local value = 5 + 7]],
[[print(value)]],
[[print(string.format("value is %d", value))]],
[[||||||| 229039e]],
[[local value = 1 + 1]],
[[=======]],
[[local value = 1 - 1]],
[[>>>>>>> new_branch]],
}
end,
},
}) do
describe("with " .. prepare[1], function()
before_each(function()
prepare[2]()
exec_lua(
[[
vim.api.nvim_buf_set_lines(0, 0, -1, true, ({...})[1])
vim.cmd("doautocmd BufReadPost")
]],
lines
)
end)

it("Conflict ours works", function()
local result = exec_lua([[
vim.cmd("Conflict ours")
return vim.api.nvim_buf_get_lines(0, 0, -1, true)
]])

assert.is_same(result, {
[[local value = 5 + 7]],
[[print(value)]],
[[print(string.format("value is %d", value))]],
})
end)

it("Conflict theirs works", function()
local result = exec_lua([[
vim.cmd("Conflict theirs")
return vim.api.nvim_buf_get_lines(0, 0, -1, true)
]])

assert.is_same(result, {
[[local value = 1 - 1]],
})
end)

it("Conflict both works", function()
local result = exec_lua([[
vim.cmd("Conflict both")
return vim.api.nvim_buf_get_lines(0, 0, -1, true)
]])

assert.is_same(result, {
[[local value = 5 + 7]],
[[print(value)]],
[[print(string.format("value is %d", value))]],
[[local value = 1 - 1]],
})
end)

it("Conflict none works", function()
local result = exec_lua([[
vim.cmd("Conflict none")
return vim.api.nvim_buf_get_lines(0, 0, -1, true)
]])

assert.is_same(result, { "" })
end)
end)
end

it("selects conflict under cursor", function()
lines = {
[[<<<<<<< HEAD]],
[[ours]],
[[=======]],
[[theirs]],
[[>>>>>>> new_branch]],
[[<<<<<<< HEAD]],
[[ours2]],
[[=======]],
[[theirs2]],
[[>>>>>>> new_branch]],
}

local result = exec_lua(
[[
local lines = ({...})[1]
vim.api.nvim_buf_set_lines(0, 0, -1, true, lines)
vim.cmd("doautocmd BufReadPost")
vim.api.nvim_win_set_cursor(0, { #lines, 0 })
vim.cmd("Conflict ours")
return vim.api.nvim_buf_get_lines(0, 0, -1, true)
]],
lines
)

assert.is_same(result, {
[[<<<<<<< HEAD]],
[[ours]],
[[=======]],
[[theirs]],
[[>>>>>>> new_branch]],
[[ours2]],
})
end)
end)

0 comments on commit 4376797

Please sign in to comment.