2021-04-30 11:26:40 +00:00
|
|
|
local action = require 'zettelkasten.action'
|
2020-10-31 13:17:26 +00:00
|
|
|
|
2020-11-06 16:28:05 +00:00
|
|
|
before_each(function()
|
2022-01-21 16:13:44 +00:00
|
|
|
_G.vim = {
|
|
|
|
g = {},
|
|
|
|
b = {},
|
|
|
|
fn = {fnameescape = function(input) return input end},
|
|
|
|
loop = {fs_scandir = function() end}
|
|
|
|
}
|
2020-11-06 16:28:05 +00:00
|
|
|
end)
|
2020-10-31 13:17:26 +00:00
|
|
|
after_each(function() _G.vim = nil end)
|
|
|
|
|
|
|
|
describe("open", function()
|
2020-11-04 20:29:23 +00:00
|
|
|
it("should open file in editor if it contains a valid link ref", function()
|
2020-10-31 13:17:26 +00:00
|
|
|
vim.api = {nvim_command = mock(function() end)}
|
|
|
|
|
2020-11-04 20:29:23 +00:00
|
|
|
action.open({ref = "1910271456_link-to-my-file.md"})
|
2020-10-31 13:17:26 +00:00
|
|
|
assert.spy(vim.api.nvim_command).was_called_with(
|
|
|
|
"edit 1910271456_link-to-my-file.md")
|
|
|
|
end)
|
2020-11-02 20:51:14 +00:00
|
|
|
it("should do nothing when no link passed in", function()
|
2020-10-31 13:17:26 +00:00
|
|
|
vim.fn = {expand = function() end}
|
|
|
|
assert.is_not_error(action.open)
|
|
|
|
end)
|
2021-04-30 14:28:22 +00:00
|
|
|
it("should first use the anchor to open the corresponding zettel",
|
|
|
|
function()
|
2020-11-06 16:28:05 +00:00
|
|
|
vim.api = {nvim_command = mock(function() end)}
|
2021-04-30 13:52:54 +00:00
|
|
|
local ls = stub(require 'zettelkasten.files', "get_zettel_by_anchor")
|
2020-11-06 16:28:05 +00:00
|
|
|
|
|
|
|
action.open({
|
|
|
|
ref = "1910271456_link-to-my-file.md",
|
|
|
|
anchor = "1910271456"
|
|
|
|
})
|
|
|
|
assert.stub(ls).was_called_with("1910271456")
|
|
|
|
end)
|
2020-10-31 13:17:26 +00:00
|
|
|
end)
|
2020-11-02 20:51:14 +00:00
|
|
|
|
2020-10-31 13:17:26 +00:00
|
|
|
describe("open_selected", function()
|
2020-11-02 20:51:14 +00:00
|
|
|
before_each(function()
|
|
|
|
vim.api = {
|
|
|
|
nvim_command = mock(function() end),
|
2021-05-04 18:42:26 +00:00
|
|
|
nvim_get_current_line = function(_)
|
2020-11-02 20:51:14 +00:00
|
|
|
return
|
|
|
|
"Hello, this is a line and [mylink](1910271456_link-to-my-file.md) whereas another [link](2030101158 another-link-now.md)"
|
|
|
|
end,
|
2021-05-04 18:42:26 +00:00
|
|
|
nvim_win_get_cursor = function(_) return {0, 0} end
|
2020-11-02 20:51:14 +00:00
|
|
|
}
|
|
|
|
end)
|
2020-11-04 20:29:23 +00:00
|
|
|
describe("when looking under cursor", function()
|
|
|
|
it("should open link", function()
|
|
|
|
vim.g['zettel_link_following'] = 'cursor'
|
2021-05-04 18:42:26 +00:00
|
|
|
vim.api.nvim_win_get_cursor = function(_) return {0, 30} end
|
2020-11-04 20:29:23 +00:00
|
|
|
action.open_selected()
|
|
|
|
assert.spy(vim.api.nvim_command).was_called_with(
|
|
|
|
"edit 1910271456_link-to-my-file.md")
|
|
|
|
end)
|
|
|
|
it("should detect correct position for link start", function()
|
|
|
|
vim.g['zettel_link_following'] = 'cursor'
|
2020-11-02 20:51:14 +00:00
|
|
|
|
2021-05-04 18:42:26 +00:00
|
|
|
vim.api.nvim_win_get_cursor = function(_) return {0, 25} end
|
2020-11-04 20:29:23 +00:00
|
|
|
action.open_selected()
|
|
|
|
assert.spy(vim.api.nvim_command).was_not_called()
|
2020-11-02 20:51:14 +00:00
|
|
|
|
2021-05-04 18:42:26 +00:00
|
|
|
vim.api.nvim_win_get_cursor = function(_) return {0, 26} end
|
2020-11-04 20:29:23 +00:00
|
|
|
action.open_selected()
|
|
|
|
assert.spy(vim.api.nvim_command).was_called_with(
|
|
|
|
"edit 1910271456_link-to-my-file.md")
|
|
|
|
end)
|
|
|
|
it("should detect correct position for link end", function()
|
|
|
|
vim.g['zettel_link_following'] = 'cursor'
|
|
|
|
|
2021-05-04 18:42:26 +00:00
|
|
|
vim.api.nvim_win_get_cursor = function(_) return {0, 65} end
|
2020-11-04 20:29:23 +00:00
|
|
|
action.open_selected()
|
|
|
|
assert.spy(vim.api.nvim_command).was_not_called()
|
|
|
|
|
2021-05-04 18:42:26 +00:00
|
|
|
vim.api.nvim_win_get_cursor = function(_) return {0, 64} end
|
2020-11-04 20:29:23 +00:00
|
|
|
action.open_selected()
|
|
|
|
assert.spy(vim.api.nvim_command).was_called_with(
|
|
|
|
"edit 1910271456_link-to-my-file.md")
|
|
|
|
end)
|
2020-11-02 20:51:14 +00:00
|
|
|
end)
|
2020-11-04 20:29:23 +00:00
|
|
|
describe("when looking until end of line", function()
|
|
|
|
it("should use the style passed to it, above the one set in options",
|
|
|
|
function()
|
|
|
|
vim.g['zettel_link_following'] = 'cursor'
|
|
|
|
|
|
|
|
vim.api.nvim_get_current_line = mock(vim.api.nvim_get_current_line)
|
|
|
|
action.open_selected("line")
|
|
|
|
|
|
|
|
assert.spy(vim.api.nvim_get_current_line).was_called()
|
|
|
|
end)
|
|
|
|
it("should open next link on line if option set", function()
|
|
|
|
vim.g['zettel_link_following'] = 'line'
|
|
|
|
action.open_selected()
|
|
|
|
assert.spy(vim.api.nvim_command).was_called_with(
|
|
|
|
"edit 1910271456_link-to-my-file.md")
|
|
|
|
end)
|
|
|
|
it("should ignore links before cursor position", function()
|
|
|
|
vim.g['zettel_link_following'] = 'line'
|
2021-05-04 18:42:26 +00:00
|
|
|
vim.api.nvim_win_get_cursor = function(_) return {0, 65} end
|
2020-11-04 20:29:23 +00:00
|
|
|
action.open_selected()
|
|
|
|
assert.spy(vim.api.nvim_command).was_called_with(
|
|
|
|
"edit 2030101158 another-link-now.md")
|
|
|
|
end)
|
2020-11-02 20:51:14 +00:00
|
|
|
end)
|
2020-10-31 13:17:26 +00:00
|
|
|
end)
|
2021-05-03 15:31:28 +00:00
|
|
|
|
|
|
|
describe("create_link", function()
|
|
|
|
it("substitutes the argument text with a link", function()
|
2021-05-04 12:50:56 +00:00
|
|
|
pending()
|
2021-05-04 18:42:26 +00:00
|
|
|
vim.fn = {getpos = function() return {1, 2, 3} end}
|
|
|
|
vim.api = {
|
|
|
|
nvim_buf_get_lines = function() return {"hi", 1, 2} end,
|
|
|
|
nvim_win_get_cursor = function() return {1, 1, 2} end,
|
|
|
|
nvim_get_current_line = function()
|
|
|
|
return "hi i am a line"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
action.make_link()
|
2021-05-03 15:31:28 +00:00
|
|
|
end)
|
|
|
|
end)
|