Move tests to test directory

This commit is contained in:
Marty Oehme 2021-05-04 20:42:26 +02:00
parent a3b03461ab
commit 892c1c9edb
Signed by: Marty
GPG key ID: B7538B8F50A1C800
8 changed files with 18 additions and 21 deletions

116
lua/test/action_spec.lua Normal file
View file

@ -0,0 +1,116 @@
local action = require 'zettelkasten.action'
before_each(function()
_G.vim = {g = {}, b = {}, loop = {fs_scandir = function() end}}
end)
after_each(function() _G.vim = nil end)
describe("open", function()
it("should open file in editor if it contains a valid link ref", function()
vim.api = {nvim_command = mock(function() end)}
action.open({ref = "1910271456_link-to-my-file.md"})
assert.spy(vim.api.nvim_command).was_called_with(
"edit 1910271456_link-to-my-file.md")
end)
it("should do nothing when no link passed in", function()
vim.fn = {expand = function() end}
assert.is_not_error(action.open)
end)
it("should first use the anchor to open the corresponding zettel",
function()
vim.api = {nvim_command = mock(function() end)}
local ls = stub(require 'zettelkasten.files', "get_zettel_by_anchor")
action.open({
ref = "1910271456_link-to-my-file.md",
anchor = "1910271456"
})
assert.stub(ls).was_called_with("1910271456")
end)
end)
describe("open_selected", function()
before_each(function()
vim.api = {
nvim_command = mock(function() end),
nvim_get_current_line = function(_)
return
"Hello, this is a line and [mylink](1910271456_link-to-my-file.md) whereas another [link](2030101158 another-link-now.md)"
end,
nvim_win_get_cursor = function(_) return {0, 0} end
}
end)
describe("when looking under cursor", function()
it("should open link", function()
vim.g['zettel_link_following'] = 'cursor'
vim.api.nvim_win_get_cursor = function(_) return {0, 30} end
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'
vim.api.nvim_win_get_cursor = function(_) return {0, 25} end
action.open_selected()
assert.spy(vim.api.nvim_command).was_not_called()
vim.api.nvim_win_get_cursor = function(_) return {0, 26} end
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'
vim.api.nvim_win_get_cursor = function(_) return {0, 65} end
action.open_selected()
assert.spy(vim.api.nvim_command).was_not_called()
vim.api.nvim_win_get_cursor = function(_) return {0, 64} end
action.open_selected()
assert.spy(vim.api.nvim_command).was_called_with(
"edit 1910271456_link-to-my-file.md")
end)
end)
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'
vim.api.nvim_win_get_cursor = function(_) return {0, 65} end
action.open_selected()
assert.spy(vim.api.nvim_command).was_called_with(
"edit 2030101158 another-link-now.md")
end)
end)
end)
describe("create_link", function()
it("substitutes the argument text with a link", function()
pending()
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()
end)
end)

109
lua/test/anchor_spec.lua Normal file
View file

@ -0,0 +1,109 @@
local A = require 'zettelkasten.anchor'
Test_date = {year = 2019, month = 10, day = 29, hour = 16, min = 45}
before_each(function() _G.vim = {g = {}, b = {}} end)
after_each(function() _G.vim = nil end)
describe("create", function()
it("should return zettel anchor from time passed in",
function() assert.same("1910291645", A.create(Test_date)) end)
it(
"should return zettel anchor from current moment if no argument passed in",
function() assert.same(os.date('%y%m%d%H%M'), A.create()) end)
it("should return nil if argument passed in is invalid",
function() assert.is_nil(A.create("My grandmother is lovely.")) end)
it(
"should lower timestamps until the first non-duplicated one if valid anchor gathering function passed",
function()
Anchor_fct = function()
return {
["2010261208"] = "/path/to/my/anchor.md",
["1910291645"] = "/path/to/my/other_anchor.md"
}
end
assert.same("1910291644", A.create(Test_date, Anchor_fct))
end)
it(
"should ignore duplicate timestamps if no anchor gathering function passed",
function() assert.same("1910291645", A.create(Test_date)) end)
end)
describe("prepend", function()
it("should append text to anchor", function()
assert.same("1910291645_isappended",
A.prepend("1910291645", "isappended"))
end)
it("should not add a separator if no text appended",
function() assert.same("1910291645", A.prepend("1910291645", "")) end)
it("should return solely the anchor if no text is passed in",
function() assert.same("1910291645", A.prepend("1910291645", nil)) end)
it("should return solely the anchor if empty text is passed in",
function() assert.same("1910291645", A.prepend("1910291645", "")) end)
it("should add contents of g:zettel_anchor_separator variable to text",
function()
vim.g.zettel_anchor_separator = "SEP"
assert.same("1910291645SEParated", A.prepend("1910291645", "arated"))
end)
it("should add contents of b:zettel_anchor_separator variable to text",
function()
vim.b.zettel_anchor_separator = "---"
assert.same("1910291645---arated", A.prepend("1910291645", "arated"))
end)
end)
describe("extract", function()
it("should get the default anchor from a string of text", function()
assert.same("2010261208", A.extract(
"/home/office/docs/2010261208 we are the champions.md"))
end)
it("should return nil when default anchor not contained", function()
assert.same(nil, A.extract(
"/home/office/docs/10261208 we are the champions.md"))
end)
it("should use the anchor set in options", function()
vim.g.zettel_anchor_regex = '[%u][%l][%d][%d][%d][%d]'
assert.same("Fa1984", A.extract(
"/home/office/docs/Fa1984_we are the champions.md"))
end)
it("should use the anchor regex argument if one is passed", function()
assert.same("bO133T",
A.extract(
"/home/office/docs/bO133T-we are the champions.md",
"[%l][%u][%d][%d][%d][%u]"))
end)
end)
describe("list", function()
it("should return a set of anchors", function()
local anchor_fct = function()
return {
["2010261208"] = "/path/to/my/anchor.md",
["2001011212"] = "/path/to/my/other_anchor.md"
}
end
assert.same({
["2010261208"] = "/path/to/my/anchor.md",
["2001011212"] = '/path/to/my/other_anchor.md'
}, A.list(anchor_fct))
end)
end)
describe("check_anchor_exists", function()
before_each(function()
Anchor_fct = function()
return {
["2010261208"] = "/path/to/my/anchor.md",
["2001011212"] = "/path/to/my/other_anchor.md"
}
end
end)
it("returns true if anchor in existing set",
function() assert.is_true(A.is_duplicate('2001011212', Anchor_fct)) end)
it("returns false if anchor not in existing set",
function() assert.is_false(A.is_duplicate('2001011210', Anchor_fct)) end)
end)

203
lua/test/files_spec.lua Normal file
View file

@ -0,0 +1,203 @@
local ls = require 'zettelkasten.files'
-- these tests, I suppose, only work on unix due to the file structure
local function simple_api_mock(files)
return {
g = {},
b = {},
loop = {
fs_scandir = function()
if #files == 0 then
return false
else
return true
end
end,
fs_scandir_next = function() return table.remove(files) end
}
}
end
describe("get_anchors_and_paths", function()
before_each(function() get_api_mock = simple_api_mock end)
after_each(function() _G.vim = nil end)
it("should return anchor-keyed table pointing to filename of zettel",
function()
local file_list = {}
file_list["someDir/1910291645 this-is-a-testfile.md"] =
"1910291645 this-is-a-testfile.md"
_G.vim = get_api_mock(file_list)
local expected = {
["1910291645"] = "someDir/1910291645 this-is-a-testfile.md"
}
assert.same(expected, ls.get_anchors_and_paths(file_list))
end)
it("should ignore any malformed files", function()
local file_list = {
["someDir/2010261208 this-should-be-picked-up.md"] = "2010261208 this-should-be-picked-up.md",
["someDir/1910291645 this-is-a-testfile.md"] = "1910291645 this-is-a-testfile.md",
["someDir/this-is-not-a-testfile.md"] = "this-is-not-a-testfile.md",
["1910271456 this-is-wrong-extension.txt"] = "1910271456 this-is-wrong-extension.txt",
["1812 this-is-ignored.md"] = "1812 this-is-ignored.md"
}
_G.vim = get_api_mock(file_list)
local expected = {
["1910291645"] = "someDir/1910291645 this-is-a-testfile.md",
["2010261208"] = "someDir/2010261208 this-should-be-picked-up.md"
}
assert.same(expected, ls.get_anchors_and_paths(file_list))
end)
it("should adhere to the zettel extension defined in options", function()
local file_list = {
["mydirectory/1910291645 myfile.wiki"] = "1910291645 myfile.wiki",
["mydirectory/2345678901 another.wiki"] = "2345678901 another.wiki"
}
_G.vim = get_api_mock(file_list)
vim.g['zettel_extension'] = '.wiki'
local expected = {
["1910291645"] = "mydirectory/1910291645 myfile.wiki",
["2345678901"] = "mydirectory/2345678901 another.wiki"
}
assert.same(expected, ls.get_anchors_and_paths(file_list, false, vim.g))
end)
end)
describe("get_all_files", function()
it("should recurse into directories if recursive argument passed in ",
function()
local files = {
{"1910271456 testfile.md", "file"},
{"more-notes-here", "directory"},
{"2010261208 another-testfile.md", "file"}
}
local vim_api_mock = {
g = {},
b = {},
loop = mock({
fs_scandir = function()
if #files == 0 then
return false
else
return true
end
end,
fs_scandir_next = function()
if #files == 0 then return nil end
local fname, ftype = unpack(table.remove(files))
return fname, ftype
end
})
}
_G.vim = vim_api_mock
ls.get_all_files("path/to/startingdir", true)
assert.spy(vim_api_mock.loop.fs_scandir).was_called(2)
assert.spy(vim_api_mock.loop.fs_scandir).was_called_with(
"path/to/startingdir/more-notes-here")
end)
it("should add all files found in subdirectories when recursing", function()
local outer_files = {
"subdir", "1234567890 myfile.md", "2345678901 another.md"
}
local inner_files = {
"2222222222 should-be-present.md",
"3333333333 should-also-be-present.md"
}
local files = outer_files
-- assert.is_true("not implemented")
local vim_api_mock = {
g = {},
b = {},
loop = {
fs_scandir = function()
if #files == 0 then return false end
return true
end,
fs_scandir_next = function()
if #files == 0 then return nil end
local fname, ftype = table.remove(files), 'file'
if fname == "subdir" then
files = inner_files
ftype = 'directory'
end
return fname, ftype
end
}
}
_G.vim = vim_api_mock
local expected = {
["mydirectory/1234567890 myfile.md"] = "1234567890 myfile.md",
["mydirectory/2345678901 another.md"] = "2345678901 another.md",
["mydirectory/subdir/2222222222 should-be-present.md"] = "2222222222 should-be-present.md",
["mydirectory/subdir/3333333333 should-also-be-present.md"] = "3333333333 should-also-be-present.md"
}
assert.same(expected, ls.get_all_files('mydirectory', true))
end)
end)
describe("get_zettel_by_anchor", function()
it("should return the correct zettel by id", function()
local file_list = {
["aDir/1910291645 myfile.md"] = "1910291645 myfile.md",
["dir/2345678901 another.md"] = "2345678901 another.md"
}
_G.vim = simple_api_mock(file_list)
assert.same("aDir/1910291645 myfile.md",
ls.get_zettel_by_anchor("1910291645", file_list))
end)
it("should return nil and not break on no all list passed in", function()
stub(ls, "get_anchors_and_paths")
assert.is_not_error(function()
ls.get_zettel_by_anchor("myanchor")
end)
end)
it("should default to the zettel root dir if no list passed in", function()
local fc = stub(ls, "get_all_files")
local expected = require'zettelkasten.options'.zettel().rootdir
ls.get_zettel_by_anchor(expected)
assert.stub(fc).was_called_with(expected, true)
end)
end)
describe("get_zettel_by_ref", function()
it("should return a full file path for file path linked", function()
local file_list = {
["link/to/my/file.md"] = "file.md",
["link/to/my/target-file.md"] = "target-file.md"
}
assert.same("link/to/my/target-file.md", ls.get_zettel_by_ref(
"link/to/my/target-file.md", file_list))
end)
it("should return path to matching base name if only that is linked",
function()
local file_list = {
["link/to/my/file.md"] = "file.md",
["link/to/my/target-file.md"] = "target-file.md"
}
assert.same("link/to/my/target-file.md",
ls.get_zettel_by_ref("target-file.md", file_list))
end)
it("should not return anything if no match exists", function()
local file_list = {
["link/to/my/file.md"] = "file.md",
["link/to/my/no-target-file.md"] = "no-target-file.md"
}
assert.same(nil, ls.get_zettel_by_ref("target-file.md", file_list))
end)
end)

6
lua/test/init_spec.lua Normal file
View file

@ -0,0 +1,6 @@
ZK = require 'zettelkasten.init'
-- describe("Zettelkasten", function()
-- it("should create an anchor for the current datetime",
-- function() assert.same(os.date('%y%m%d%H%M'), ZK.create_anchor()) end)
-- end)

123
lua/test/link_spec.lua Normal file
View file

@ -0,0 +1,123 @@
local link = require 'zettelkasten.link'
Test_date = {year = 2019, month = 10, day = 29, hour = 16, min = 45}
before_each(function() _G.vim = {g = {}, b = {}} end)
after_each(function() _G.vim = nil end)
describe("urlify", function()
it("should return lowercased link text", function()
assert.same("yesiamshouting", link.urlify("YESIAMSHOUTING"))
end)
it("should return spaces in text replaced with dashes", function()
assert.same("yes-indeed-a-space", link.urlify("yes indeed a space"))
end)
end)
describe("append_extension", function()
it("should append the contents set in global zettel extension option",
function()
vim.g.zettel_extension = ".extension"
assert.same("myfile.extension", link.append_extension("myfile"))
end)
it("should append the contents set in global zettel extension option",
function()
vim.b.zettel_extension = ".bufext"
assert.same("myfile.bufext", link.append_extension("myfile"))
end)
end)
describe("create", function()
before_each(function()
vim.g.zettel_extension = ".md"
vim.g.zettel_anchor_separator = "_"
vim.g.zettel_link_style = "markdown"
end)
after_each(function() vim.g = nil end)
it("should create a working link using set options in vim", function()
assert.same("[My FILE NAME](1910291645_my-file-name.md)",
link.create("1910291645", "My FILE NAME"))
end)
it("should create a working link if style is manually set", function()
assert.same("[[1910291645|My FILE NAME]]",
link.create("1910291645", "My FILE NAME", "wiki"))
end)
it("should not error on empty text", function()
vim.g.zettel_extension = ".wiki"
vim.g.zettel_anchor_separator = "_"
vim.g.zettel_link_style = "wiki"
assert.same("[[1910291645]]", link.create("1910291645"))
end)
describe("wiki link styling", function()
it("should correctly apply transformations to link and text", function()
assert.same("[[1910291645|My AWESOME Link]]",
link.create("1910291645", "My AWESOME Link", "wiki"))
end)
it("should trim whitespace for the text area", function()
assert.same("[[1910291645]]",
link.create("1910291645", " ", "wiki"))
assert.same("[[1910291645|hi]]",
link.create("1910291645", " hi ", "wiki"))
end)
describe("markdown link styling", function()
it("should correctly apply transformations to link and text",
function()
assert.same("[My AWESOME Link](1910291645_my-awesome-link.md)",
link.create("1910291645", "My AWESOME Link",
"markdown"))
end)
it("should trim whitespace for the text area", function()
assert.same("[](1910291645.md)",
link.create("1910291645", " ", "markdown"))
assert.same("[hi](1910291645_hi.md)",
link.create("1910291645", " hi ", "markdown"))
end)
end)
end)
end)
describe("new", function()
before_each(function()
vim.loop = {
fs_scandir = function(_) return false end,
fs_scandir_next = function(_) end
}
end)
it("should create a link out of only text input", function()
local result = link.new("My FILE NAME")
assert.is_not_nil(result:match(
"%[My FILE NAME%]%(.*_my%-file%-name%.md%)"))
end)
it("should be callable without any parameters, using default settings",
function()
vim.g.zettel_link_style = "wiki"
local result = link.new()
assert.is_not_nil(result:match("%[%[[^|]+%]%]"))
end)
end)
describe("extract_all", function()
it("should get all links input string", function()
local input =
"[Some text](2003042042_my-link.md) and another, [with more text](2001261123 another-link.md), and done. "
local expected = {
{
endpos = 34,
ref = "2003042042_my-link.md",
startpos = 1,
text = "Some text",
anchor = "2003042042"
}, {
endpos = 92,
ref = "2001261123 another-link.md",
startpos = 49,
text = "with more text",
anchor = "2001261123"
}
}
assert.same(expected, link.extract_all(input))
end)
end)

83
lua/test/options_spec.lua Normal file
View file

@ -0,0 +1,83 @@
local opt = require 'zettelkasten.options'
before_each(function() _G.vim = {g = {}, b = {}} end)
after_each(function() _G.vim = nil end)
describe("zettel options", function()
it("should return the global zettel extension if set in vim", function()
_G.vim.g.zettel_extension = ".myextension"
assert.same(".myextension", opt.zettel().extension)
end)
it("should return the buffer zettel extension if set in vim", function()
_G.vim.b.zettel_extension = ".mybufextension"
assert.same(".mybufextension", opt.zettel().extension)
end)
it("should return the default zettel extension if not set in vim",
function() assert.same(".md", opt.zettel().extension) end)
it("should return the zettel root dir if set in vim", function()
_G.vim.g.zettel_rootdir = "~/my/own/dir"
assert.same("~/my/own/dir", opt.zettel().rootdir)
end)
it("should return the default root directory if not set in vim",
function() assert.same("~/documents/notes", opt.zettel().rootdir) end)
end)
describe("link options", function()
it("should return the global link style if set in vim", function()
_G.vim.g.zettel_link_style = "wiki"
assert.same("wiki", opt.link().style)
end)
it("should return the buffer link style if set in vim", function()
_G.vim.b.zettel_link_style = "wiki"
assert.same("wiki", opt.link().style)
end)
it("should return the default link style if not set in vim",
function() assert.same("markdown", opt.link().style) end)
it("should error on entries other than markdown/wiki", function()
_G.vim.g.zettel_link_style = "idontbelong"
assert.is_error(function() opt.link() end)
end)
it("should return the global link following if set in vim", function()
_G.vim.g.zettel_link_following = "line"
assert.same("line", opt.link().following)
end)
it("should return the buffer link following if set in vim", function()
_G.vim.b.zettel_link_following = "line"
assert.same("line", opt.link().following)
end)
it("should return the default link following if not set in vim",
function() assert.same("cursor", opt.link().following) end)
it("should error on entries other than markdown/wiki", function()
_G.vim.g.zettel_link_following = "idontbelong"
assert.is_error(function() opt.link() end)
end)
end)
describe("anchor options", function()
it("should return the global anchor separator if set in vim", function()
_G.vim.g.zettel_anchor_separator = "SEPARATE"
assert.same("SEPARATE", opt.anchor().separator)
end)
it("should return the buffer anchor separator if set in vim", function()
_G.vim.b.zettel_anchor_separator = "--"
assert.same("--", opt.anchor().separator)
end)
it("should return the default anchor separator if not set in vim",
function() assert.same("_", opt.anchor().separator) end)
it("should return the anchor regex if set in vim", function()
vim.g.zettel_anchor_regex =
"[%d][%d][%d][%d][%d][%d][%d][%d][%d][%d][%d]?[%d]?"
-- allowing both 10- and 12-digit time-based zettel ids
assert.same('[%d][%d][%d][%d][%d][%d][%d][%d][%d][%d][%d]?[%d]?',
opt.anchor().regex)
end)
it("should return the default anchor regex if not set in vim", function()
assert.same('[%d][%d][%d][%d][%d][%d][%d][%d][%d][%d]',
opt.anchor().regex)
end)
end)

108
lua/test/text_spec.lua Normal file
View file

@ -0,0 +1,108 @@
local t = require 'zettelkasten.text'
before_each(function() _G.vim = {g = {}, b = {}} end)
after_each(function() _G.vim = nil end)
describe("get_current_selection", function()
before_each(function()
vim.fn = {
getpos = function(mark)
if mark == "'<" then
return {0, 1, 15}
elseif mark == "'>" then
return {0, 1, 23}
end
end
}
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)
describe("get_current_word", function()
it("returns the complete word the cursor is over", function()
vim.api = {
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)