Skip to content

Commit cb19d43

Browse files
authored
test(util): add test cases (#79)
1 parent e055155 commit cb19d43

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

lua/gitlinker/logger.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ local Configs = {
4242
),
4343
}
4444

45-
--- @param opts table<any, any>
45+
--- @param opts Options?
4646
local function setup(opts)
4747
Configs = vim.tbl_deep_extend("force", vim.deepcopy(Configs), opts or {})
4848
if type(Configs.level) == "string" then

lua/gitlinker/util.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ end
5656

5757
--- @return {lstart:integer,lend:integer}
5858
local function line_range()
59-
local mode = vim.fn.mode()
60-
local pos1 = nil
61-
local pos2 = nil
62-
if is_visual_mode(mode) then
59+
local m = vim.fn.mode()
60+
local l1 = nil
61+
local l2 = nil
62+
if is_visual_mode(m) then
6363
vim.cmd([[execute "normal! \<ESC>"]])
64-
pos1 = vim.fn.getpos("'<")[2]
65-
pos2 = vim.fn.getpos("'>")[2]
64+
l1 = vim.fn.getpos("'<")[2]
65+
l2 = vim.fn.getpos("'>")[2]
6666
else
67-
pos1 = vim.fn.getcurpos()[2]
68-
pos2 = pos1
67+
l1 = vim.fn.getcurpos()[2]
68+
l2 = l1
6969
end
70-
local lstart = math.min(pos1, pos2)
71-
local lend = math.max(pos1, pos2)
70+
local lstart = math.min(l1, l2)
71+
local lend = math.max(l1, l2)
7272
return { lstart = lstart, lend = lend }
7373
end
7474

test/util_spec.lua

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
local cwd = vim.fn.getcwd()
2+
3+
describe("util", function()
4+
local assert_eq = assert.is_equal
5+
local assert_true = assert.is_true
6+
local assert_false = assert.is_false
7+
8+
before_each(function()
9+
vim.api.nvim_command("cd " .. cwd)
10+
vim.opt.swapfile = false
11+
end)
12+
13+
local logger = require("gitlinker.logger")
14+
logger.setup()
15+
local util = require("gitlinker.util")
16+
describe("[path_normalize]", function()
17+
it("normalize", function()
18+
local lines = {
19+
"~/github/linrongbin16/gitlinker.nvim/README.md",
20+
"~/github/linrongbin16/gitlinker.nvim/lua/gitlinker.lua",
21+
}
22+
for i, line in ipairs(lines) do
23+
local actual = util.path_normalize(line)
24+
local expect = vim.fn.expand(line)
25+
print(
26+
string.format(
27+
"path normalize[%d]:%s == %s\n",
28+
i,
29+
actual,
30+
expect
31+
)
32+
)
33+
assert_eq(actual, expect)
34+
end
35+
end)
36+
it("relative", function()
37+
local lines = {
38+
"README.md",
39+
"lua/gitlinker.lua",
40+
"lua/gitlinker/util.lua",
41+
}
42+
for i, line in ipairs(lines) do
43+
vim.cmd(string.format([[ edit %s ]], line))
44+
local actual = util.path_relative()
45+
print(string.format("path relative:%s\n", actual))
46+
assert_eq(actual, line)
47+
end
48+
end)
49+
end)
50+
end)

0 commit comments

Comments
 (0)