Add tests to text module

This commit is contained in:
Marty Oehme 2021-05-04 16:38:39 +02:00
parent a4aa3f825b
commit 5345b6c345
Signed by: Marty
GPG key ID: B7538B8F50A1C800
4 changed files with 107 additions and 31 deletions

View file

@ -6,7 +6,6 @@ start neovim with `nvim --cmd "set rtp+=$(pwd)" .` to automatically load the fi
## up next ## up next
* text.lua testing
* action.lua testing? * action.lua testing?
## TODO: needed functionality ## TODO: needed functionality

View file

@ -47,8 +47,9 @@ function A.make_link(visual)
else else
selection, start_col = t.get_current_word() selection, start_col = t.get_current_word()
end end
vim.api.nvim_set_current_line(t.replace_text(selection, l.new(selection), vim.api.nvim_set_current_line(t.replace_text_in_current_line(selection,
start_col)) l.new(selection),
start_col))
end end
return {open = A.open, open_selected = A.open_selected, make_link = A.make_link} return {open = A.open, open_selected = A.open_selected, make_link = A.make_link}

View file

@ -7,7 +7,7 @@ function T.get_current_selection()
local line, start_col, end_col = vim.fn.getpos("'<")[2], local line, start_col, end_col = vim.fn.getpos("'<")[2],
vim.fn.getpos("'<")[3], vim.fn.getpos("'<")[3],
vim.fn.getpos("'>")[3] vim.fn.getpos("'>")[3]
local selection = vim.fn.getline(line, line)[1]:sub(start_col, end_col) local selection = T.get_line(line):sub(start_col, end_col)
return selection, start_col return selection, start_col
end end
@ -20,7 +20,7 @@ function T.get_current_word(big)
if not big then pattern = [[\S]] end if not big then pattern = [[\S]] end
local cur_col = vim.api.nvim_win_get_cursor(0)[2] local cur_col = vim.api.nvim_win_get_cursor(0)[2]
local line = vim.api.nvim_get_current_line() local line = T.get_line()
local word_before_cur = vim.fn.matchstrpos(line:sub(1, cur_col + 1), local word_before_cur = vim.fn.matchstrpos(line:sub(1, cur_col + 1),
pattern .. "*$") pattern .. "*$")
@ -70,8 +70,8 @@ end
-- which can prevent falsely substituting the wrong text fragment if an -- which can prevent falsely substituting the wrong text fragment if an
-- identical one exists earlier on the line. (E.g. I want to replace the -- identical one exists earlier on the line. (E.g. I want to replace the
-- second 'test' in 'test test 1 2 3'). -- second 'test' in 'test test 1 2 3').
function T.replace_text(text, new_text, start_col) function T.replace_text_in_current_line(text, new_text, start_col)
local line_full = vim.api.nvim_get_current_line() local line_full = T.get_line()
local line_edited local line_edited
if start_col then if start_col then
line_edited = line_full:sub(1, start_col - 1) .. line_edited = line_full:sub(1, start_col - 1) ..
@ -85,12 +85,12 @@ end
--- Return editor line contents. --- Return editor line contents.
-- Returns the content of the line number passed in or the currently active -- Returns the content of the line number passed in or the currently active
-- line if no number passed in. Lines are, as per neovim function, -- line if no number passed in. Lines are, different to the neovim function,
-- *zero-indexed* compared to what you see in e.g. the editor sidebar. -- *one-indexed*.
--- @param linenr number --- @param linenr number
--- @return string --- @return string
function T.get_line(linenr) function T.get_line(linenr)
if linenr then return vim.api.nvim_buf_get_lines(0, linenr, linenr + 1) end if linenr then return vim.api.nvim_buf_get_lines(0, linenr - 1, linenr, false)[1] end
return vim.api.nvim_get_current_line() return vim.api.nvim_get_current_line()
end end

View file

@ -3,30 +3,106 @@ local t = require 'zettelkasten.text'
before_each(function() _G.vim = {g = {}, b = {}} end) before_each(function() _G.vim = {g = {}, b = {}} end)
after_each(function() _G.vim = nil end) after_each(function() _G.vim = nil end)
describe("get_line", function() describe("get_current_selection", function()
it("returns current line contents if no line nr passed", function() before_each(function()
vim.api = { vim.fn = {
nvim_get_current_line = function() getpos = function(mark)
return "hello my old friend" if mark == "'<" then
end return {0, 1, 15}
} elseif mark == "'>" then
assert.same("hello my old friend", t.get_line()) return {0, 1, 23}
end)
it("returns zero-indexed line contents", function()
vim.api = {
nvim_buf_get_lines = function(...)
local args = table.pack(...)
if args[1] == 0 and args[2] + 1 == args[3] then
return "hello my new enemy"
end end
return "wrong arguments"
end end
} }
assert.same("hello my new enemy", t.get_line(1)) vim.api = {
nvim_buf_get_lines = function()
return {"unfortunately we did it not"}
end
}
end)
it("returns the selected area",
function() assert.same("we did it", t.get_current_selection()) end)
it("returns the starting selection column", function()
local _, result = t.get_current_selection()
assert.same(15, result)
end) end)
end) end)
-- describe("Zettelkasten", function() describe("get_current_word", function()
-- it("should create an anchor for the current datetime", it("returns the complete word the cursor is over", function()
-- function() assert.same(os.date('%y%m%d%H%M'), ZK.create_anchor()) end) vim.api = {
-- end) nvim_get_current_line = function()
return "we found aWord here"
end,
nvim_win_get_cursor = function() return {0, 12, 0} end
}
vim.fn = {
matchstrpos = function(txt, _)
if #txt == 13 then return {"aWor", 1} end
end,
matchstr = function(_, _) return "rd" end
}
assert.same("aWord", t.get_current_word())
end)
end)
describe("replace_text", function()
before_each(function()
vim.api = {
nvim_get_current_line = function()
return "we-are? pretty pretty"
end
}
end)
it("returns the current editor line with input text correctly replaced",
function()
assert.same("you-are? pretty pretty",
t.replace_text_in_current_line("we", "you"))
end)
it("only replaces exactly one instance of whatever it matches", function()
assert.same("we-are? awesome pretty",
t.replace_text_in_current_line("pretty", "awesome"))
end)
it("avoids replacing the first line match if the second should be replaced",
function()
assert.same("we-are? pretty awesome",
t.replace_text_in_current_line("pretty", "awesome", 15))
end)
it("correctly replaces dashes, or other lua special matching characters",
function()
assert.same("we-are! amazingly? pretty pretty",
t.replace_text_in_current_line("we-are", "we-are! amazingly"))
end)
end)
describe("get_line", function()
before_each(function()
vim.api = {
nvim_get_current_line = function()
return "hello my old friend"
end,
nvim_buf_get_lines = function(...)
local args = table.pack(...)
if args[1] == 0 and args[2] + 1 == args[3] then
return {"hello my new enemy"}
end
return {"wrong arguments"}
end
}
end)
it("returns current line contents if no line nr passed",
function() assert.same("hello my old friend", t.get_line()) end)
it("returns arbitrary line contents when numbered",
function() assert.same("hello my new enemy", t.get_line(1)) end)
it("takes a 1-indexed line but calls the nvim internal 0-index", function()
local gl = mock(vim.api)
t.get_line(1)
assert.spy(gl.nvim_buf_get_lines).was_called_with(0, 0, 1, false)
mock.revert(t)
end)
end)