Skip to content

Commit

Permalink
test(util): add test cases (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 authored Oct 20, 2023
1 parent e055155 commit cb19d43
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lua/gitlinker/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ local Configs = {
),
}

--- @param opts table<any, any>
--- @param opts Options?
local function setup(opts)
Configs = vim.tbl_deep_extend("force", vim.deepcopy(Configs), opts or {})
if type(Configs.level) == "string" then
Expand Down
20 changes: 10 additions & 10 deletions lua/gitlinker/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ end

--- @return {lstart:integer,lend:integer}
local function line_range()
local mode = vim.fn.mode()
local pos1 = nil
local pos2 = nil
if is_visual_mode(mode) then
local m = vim.fn.mode()
local l1 = nil
local l2 = nil
if is_visual_mode(m) then
vim.cmd([[execute "normal! \<ESC>"]])
pos1 = vim.fn.getpos("'<")[2]
pos2 = vim.fn.getpos("'>")[2]
l1 = vim.fn.getpos("'<")[2]
l2 = vim.fn.getpos("'>")[2]
else
pos1 = vim.fn.getcurpos()[2]
pos2 = pos1
l1 = vim.fn.getcurpos()[2]
l2 = l1
end
local lstart = math.min(pos1, pos2)
local lend = math.max(pos1, pos2)
local lstart = math.min(l1, l2)
local lend = math.max(l1, l2)
return { lstart = lstart, lend = lend }
end

Expand Down
50 changes: 50 additions & 0 deletions test/util_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
local cwd = vim.fn.getcwd()

describe("util", function()
local assert_eq = assert.is_equal
local assert_true = assert.is_true
local assert_false = assert.is_false

before_each(function()
vim.api.nvim_command("cd " .. cwd)
vim.opt.swapfile = false
end)

local logger = require("gitlinker.logger")
logger.setup()
local util = require("gitlinker.util")
describe("[path_normalize]", function()
it("normalize", function()
local lines = {
"~/github/linrongbin16/gitlinker.nvim/README.md",
"~/github/linrongbin16/gitlinker.nvim/lua/gitlinker.lua",
}
for i, line in ipairs(lines) do
local actual = util.path_normalize(line)
local expect = vim.fn.expand(line)
print(
string.format(
"path normalize[%d]:%s == %s\n",
i,
actual,
expect
)
)
assert_eq(actual, expect)
end
end)
it("relative", function()
local lines = {
"README.md",
"lua/gitlinker.lua",
"lua/gitlinker/util.lua",
}
for i, line in ipairs(lines) do
vim.cmd(string.format([[ edit %s ]], line))
local actual = util.path_relative()
print(string.format("path relative:%s\n", actual))
assert_eq(actual, line)
end
end)
end)
end)

0 comments on commit cb19d43

Please sign in to comment.