Skip to content

Commit a99a2a6

Browse files
committed
tests: Add tests for diff hunk navigation
1 parent bed1854 commit a99a2a6

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

tests/providers/diff/test_inline.lua

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,129 @@ T["InlineDiff"]["integration - multiple diffs on same buffer"] = function()
429429
diff2:teardown()
430430
end
431431

432+
T["InlineDiff"]["navigation - tracks hunk positions"] = function()
433+
local bufnr = vim.api.nvim_get_current_buf()
434+
local original_contents = {
435+
"line 1",
436+
"old line 2",
437+
"line 3",
438+
"old line 4",
439+
"line 5",
440+
}
441+
local modified_contents = {
442+
"line 1",
443+
"new line 2",
444+
"line 3",
445+
"new line 4",
446+
"line 5",
447+
}
448+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, modified_contents)
449+
450+
local diff = InlineDiff.new({
451+
bufnr = bufnr,
452+
contents = original_contents,
453+
id = "nav_test",
454+
})
455+
456+
h.eq(diff.hunk_count, 2)
457+
h.eq(type(diff.hunk_start_lines), "table")
458+
h.eq(#diff.hunk_start_lines, 2)
459+
460+
diff:teardown()
461+
end
462+
463+
T["InlineDiff"]["navigation - jump_to_next_hunk works"] = function()
464+
local bufnr = vim.api.nvim_get_current_buf()
465+
local original_contents = {
466+
"line 1",
467+
"old line 2",
468+
"line 3",
469+
"line 4",
470+
"old line 5",
471+
"line 6",
472+
}
473+
local modified_contents = {
474+
"line 1",
475+
"new line 2",
476+
"line 3",
477+
"line 4",
478+
"new line 5",
479+
"line 6",
480+
}
481+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, modified_contents)
482+
483+
local diff = InlineDiff.new({
484+
bufnr = bufnr,
485+
contents = original_contents,
486+
id = "nav_next_test",
487+
})
488+
489+
h.eq(diff.hunk_count, 2)
490+
491+
-- Start at line 1, should jump to first hunk
492+
vim.api.nvim_win_set_cursor(0, { 1, 0 })
493+
diff:jump_to_next_hunk()
494+
local cursor = vim.api.nvim_win_get_cursor(0)
495+
h.eq(cursor[1], diff.hunk_start_lines[1])
496+
497+
-- Jump again, should go to second hunk
498+
diff:jump_to_next_hunk()
499+
cursor = vim.api.nvim_win_get_cursor(0)
500+
h.eq(cursor[1], diff.hunk_start_lines[2])
501+
502+
-- Jump again, should wrap to first hunk
503+
diff:jump_to_next_hunk()
504+
cursor = vim.api.nvim_win_get_cursor(0)
505+
h.eq(cursor[1], diff.hunk_start_lines[1])
506+
507+
diff:teardown()
508+
end
509+
510+
T["InlineDiff"]["navigation - jump_to_prev_hunk works"] = function()
511+
local bufnr = vim.api.nvim_get_current_buf()
512+
local original_contents = {
513+
"line 1",
514+
"old line 2",
515+
"line 3",
516+
"old line 4",
517+
"line 5",
518+
}
519+
local modified_contents = {
520+
"line 1",
521+
"new line 2",
522+
"line 3",
523+
"new line 4",
524+
"line 5",
525+
}
526+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, modified_contents)
527+
528+
local diff = InlineDiff.new({
529+
bufnr = bufnr,
530+
contents = original_contents,
531+
id = "nav_prev_test",
532+
})
533+
534+
h.eq(diff.hunk_count, 2)
535+
536+
-- Start at last line, should jump to last hunk
537+
vim.api.nvim_win_set_cursor(0, { 5, 0 })
538+
diff:jump_to_prev_hunk()
539+
local cursor = vim.api.nvim_win_get_cursor(0)
540+
h.eq(cursor[1], diff.hunk_start_lines[2])
541+
542+
-- Jump again, should go to first hunk
543+
diff:jump_to_prev_hunk()
544+
cursor = vim.api.nvim_win_get_cursor(0)
545+
h.eq(cursor[1], diff.hunk_start_lines[1])
546+
547+
-- Jump again, should wrap to last hunk
548+
diff:jump_to_prev_hunk()
549+
cursor = vim.api.nvim_win_get_cursor(0)
550+
h.eq(cursor[1], diff.hunk_start_lines[2])
551+
552+
diff:teardown()
553+
end
554+
432555
T["InlineDiff"]["integration - edge case with empty file"] = function()
433556
local bufnr = vim.api.nvim_get_current_buf()
434557
local original_contents = { "" } -- Neovim empty buffers have one empty line

0 commit comments

Comments
 (0)