This commit is contained in:
@@ -1,70 +0,0 @@
|
||||
--- @diagnostic disable: undefined-field, need-check-nil
|
||||
local Pos = require 'u.pos'
|
||||
local withbuf = loadfile './spec/withbuf.lua'()
|
||||
|
||||
describe('Pos', function()
|
||||
it('get a char from a given position', function()
|
||||
withbuf({ 'asdf', 'bleh', 'a', '', 'goo' }, function()
|
||||
assert.are.same('a', Pos.new(nil, 1, 1):char())
|
||||
assert.are.same('d', Pos.new(nil, 1, 3):char())
|
||||
assert.are.same('f', Pos.new(nil, 1, 4):char())
|
||||
assert.are.same('a', Pos.new(nil, 3, 1):char())
|
||||
assert.are.same('', Pos.new(nil, 4, 1):char())
|
||||
assert.are.same('o', Pos.new(nil, 5, 3):char())
|
||||
end)
|
||||
end)
|
||||
|
||||
it('comparison operators', function()
|
||||
local a = Pos.new(0, 0, 0, 0)
|
||||
local b = Pos.new(0, 1, 0, 0)
|
||||
assert.are.same(a == a, true)
|
||||
assert.are.same(a < b, true)
|
||||
end)
|
||||
|
||||
it('get the next position', function()
|
||||
withbuf({ 'asdf', 'bleh', 'a', '', 'goo' }, function()
|
||||
-- line 1: a => s
|
||||
assert.are.same(Pos.new(nil, 1, 2), Pos.new(nil, 1, 1):next())
|
||||
-- line 1: d => f
|
||||
assert.are.same(Pos.new(nil, 1, 4), Pos.new(nil, 1, 3):next())
|
||||
-- line 1 => 2
|
||||
assert.are.same(Pos.new(nil, 2, 1), Pos.new(nil, 1, 4):next())
|
||||
-- line 3 => 4
|
||||
assert.are.same(Pos.new(nil, 4, 1), Pos.new(nil, 3, 1):next())
|
||||
-- line 4 => 5
|
||||
assert.are.same(Pos.new(nil, 5, 1), Pos.new(nil, 4, 1):next())
|
||||
-- end returns nil
|
||||
assert.are.same(nil, Pos.new(nil, 5, 3):next())
|
||||
end)
|
||||
end)
|
||||
|
||||
it('get the previous position', function()
|
||||
withbuf({ 'asdf', 'bleh', 'a', '', 'goo' }, function()
|
||||
-- line 1: s => a
|
||||
assert.are.same(Pos.new(nil, 1, 1), Pos.new(nil, 1, 2):next(-1))
|
||||
-- line 1: f => d
|
||||
assert.are.same(Pos.new(nil, 1, 3), Pos.new(nil, 1, 4):next(-1))
|
||||
-- line 2 => 1
|
||||
assert.are.same(Pos.new(nil, 1, 4), Pos.new(nil, 2, 1):next(-1))
|
||||
-- line 4 => 3
|
||||
assert.are.same(Pos.new(nil, 3, 1), Pos.new(nil, 4, 1):next(-1))
|
||||
-- line 5 => 4
|
||||
assert.are.same(Pos.new(nil, 4, 1), Pos.new(nil, 5, 1):next(-1))
|
||||
-- beginning returns nil
|
||||
assert.are.same(nil, Pos.new(nil, 1, 1):next(-1))
|
||||
end)
|
||||
end)
|
||||
|
||||
it('find matching brackets', function()
|
||||
withbuf({ 'asdf ({} def <[{}]>) ;lkj' }, function()
|
||||
-- outer parens are matched:
|
||||
assert.are.same(Pos.new(nil, 1, 20), Pos.new(nil, 1, 6):find_match())
|
||||
-- outer parens are matched (backward):
|
||||
assert.are.same(Pos.new(nil, 1, 6), Pos.new(nil, 1, 20):find_match())
|
||||
-- no potential match returns nil
|
||||
assert.are.same(nil, Pos.new(nil, 1, 1):find_match())
|
||||
-- watchdog expires before an otherwise valid match is found:
|
||||
assert.are.same(nil, Pos.new(nil, 1, 6):find_match(2))
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
@@ -1,7 +1,84 @@
|
||||
require 'luacov'
|
||||
|
||||
--- @diagnostic disable: undefined-field, need-check-nil
|
||||
local Pos = require 'u.pos'
|
||||
local Range = require 'u.range'
|
||||
local withbuf = loadfile './spec/withbuf.lua'()
|
||||
local Pos = require 'u'.Pos
|
||||
local Range = require 'u'.Range
|
||||
local function withbuf(lines, f)
|
||||
vim.go.swapfile = false
|
||||
|
||||
vim.cmd.new()
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, lines)
|
||||
local ok, result = pcall(f)
|
||||
vim.cmd.bdelete { bang = true }
|
||||
if not ok then error(result) end
|
||||
end
|
||||
|
||||
describe('Pos', function()
|
||||
it('get a char from a given position', function()
|
||||
withbuf({ 'asdf', 'bleh', 'a', '', 'goo' }, function()
|
||||
assert.are.same('a', Pos.new(nil, 1, 1):char())
|
||||
assert.are.same('d', Pos.new(nil, 1, 3):char())
|
||||
assert.are.same('f', Pos.new(nil, 1, 4):char())
|
||||
assert.are.same('a', Pos.new(nil, 3, 1):char())
|
||||
assert.are.same('', Pos.new(nil, 4, 1):char())
|
||||
assert.are.same('o', Pos.new(nil, 5, 3):char())
|
||||
end)
|
||||
end)
|
||||
|
||||
it('comparison operators', function()
|
||||
local a = Pos.new(0, 0, 0, 0)
|
||||
local b = Pos.new(0, 1, 0, 0)
|
||||
assert.are.same(a == a, true)
|
||||
assert.are.same(a < b, true)
|
||||
end)
|
||||
|
||||
it('get the next position', function()
|
||||
withbuf({ 'asdf', 'bleh', 'a', '', 'goo' }, function()
|
||||
-- line 1: a => s
|
||||
assert.are.same(Pos.new(nil, 1, 2), Pos.new(nil, 1, 1):next())
|
||||
-- line 1: d => f
|
||||
assert.are.same(Pos.new(nil, 1, 4), Pos.new(nil, 1, 3):next())
|
||||
-- line 1 => 2
|
||||
assert.are.same(Pos.new(nil, 2, 1), Pos.new(nil, 1, 4):next())
|
||||
-- line 3 => 4
|
||||
assert.are.same(Pos.new(nil, 4, 1), Pos.new(nil, 3, 1):next())
|
||||
-- line 4 => 5
|
||||
assert.are.same(Pos.new(nil, 5, 1), Pos.new(nil, 4, 1):next())
|
||||
-- end returns nil
|
||||
assert.are.same(nil, Pos.new(nil, 5, 3):next())
|
||||
end)
|
||||
end)
|
||||
|
||||
it('get the previous position', function()
|
||||
withbuf({ 'asdf', 'bleh', 'a', '', 'goo' }, function()
|
||||
-- line 1: s => a
|
||||
assert.are.same(Pos.new(nil, 1, 1), Pos.new(nil, 1, 2):next(-1))
|
||||
-- line 1: f => d
|
||||
assert.are.same(Pos.new(nil, 1, 3), Pos.new(nil, 1, 4):next(-1))
|
||||
-- line 2 => 1
|
||||
assert.are.same(Pos.new(nil, 1, 4), Pos.new(nil, 2, 1):next(-1))
|
||||
-- line 4 => 3
|
||||
assert.are.same(Pos.new(nil, 3, 1), Pos.new(nil, 4, 1):next(-1))
|
||||
-- line 5 => 4
|
||||
assert.are.same(Pos.new(nil, 4, 1), Pos.new(nil, 5, 1):next(-1))
|
||||
-- beginning returns nil
|
||||
assert.are.same(nil, Pos.new(nil, 1, 1):next(-1))
|
||||
end)
|
||||
end)
|
||||
|
||||
it('find matching brackets', function()
|
||||
withbuf({ 'asdf ({} def <[{}]>) ;lkj' }, function()
|
||||
-- outer parens are matched:
|
||||
assert.are.same(Pos.new(nil, 1, 20), Pos.new(nil, 1, 6):find_match())
|
||||
-- outer parens are matched (backward):
|
||||
assert.are.same(Pos.new(nil, 1, 6), Pos.new(nil, 1, 20):find_match())
|
||||
-- no potential match returns nil
|
||||
assert.are.same(nil, Pos.new(nil, 1, 1):find_match())
|
||||
-- watchdog expires before an otherwise valid match is found:
|
||||
assert.are.same(nil, Pos.new(nil, 1, 6):find_match(2))
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Range', function()
|
||||
it('get text in buffer', function()
|
||||
@@ -239,6 +316,78 @@ describe('Range', function()
|
||||
end)
|
||||
end)
|
||||
|
||||
it('text object: it (inside HTML tag)', function()
|
||||
withbuf({ '<div><span>text</span></div>' }, function()
|
||||
vim.cmd.setfiletype 'html'
|
||||
vim.fn.setpos('.', { 0, 1, 12, 0 }) -- inside 'text'
|
||||
local range = Range.from_motion('it')
|
||||
assert.is_not_nil(range)
|
||||
assert.are.same('text', range:text())
|
||||
end)
|
||||
end)
|
||||
|
||||
it('from_motion with contains_cursor returns nil when cursor outside range', function()
|
||||
withbuf({ 'foo "quoted" bar' }, function()
|
||||
vim.fn.setpos('.', { 0, 1, 2, 0 }) -- cursor at 'foo', not in quotes
|
||||
local range = Range.from_motion('a"', { contains_cursor = true })
|
||||
assert.is_nil(range)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('from_motion with pos option', function()
|
||||
withbuf({ 'the quick brown fox' }, function()
|
||||
vim.fn.setpos('.', { 0, 1, 1, 0 }) -- cursor at start
|
||||
local pos = Pos.new(nil, 1, 5) -- position at 'quick'
|
||||
local range = Range.from_motion('aw', { pos = pos })
|
||||
assert.are.same('quick ', range:text())
|
||||
end)
|
||||
end)
|
||||
|
||||
it('from_motion with simple motion (non-text-object)', function()
|
||||
withbuf({ 'hello world' }, function()
|
||||
vim.fn.setpos('.', { 0, 1, 1, 0 })
|
||||
local range = Range.from_motion('w')
|
||||
assert.is_not_nil(range)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('from_motion with bufnr option', function()
|
||||
local buf1 = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_lines(buf1, 0, -1, false, { 'buffer one', 'more text' })
|
||||
|
||||
local buf2 = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_lines(buf2, 0, -1, false, { 'buffer two' })
|
||||
vim.api.nvim_set_current_buf(buf2)
|
||||
|
||||
vim.fn.setpos('.', { buf1, 1, 1, 0 })
|
||||
local range = Range.from_motion('aw', { bufnr = buf1 })
|
||||
assert.is_not_nil(range)
|
||||
assert.are.same(buf1, range.start.bufnr)
|
||||
|
||||
vim.api.nvim_buf_delete(buf1, { force = true })
|
||||
vim.api.nvim_buf_delete(buf2, { force = true })
|
||||
end)
|
||||
|
||||
it('from_motion restores visual selection when started in visual mode', function()
|
||||
withbuf({ 'the quick brown fox' }, function()
|
||||
-- Enter visual mode first
|
||||
vim.fn.setpos('.', { 0, 1, 1, 0 })
|
||||
vim.cmd.normal 'vll' -- select 'the' (3 chars)
|
||||
|
||||
-- Record initial visual marks
|
||||
local initial_v = vim.fn.getpos('v')
|
||||
|
||||
-- Call from_motion (should save and restore visual selection)
|
||||
local range = Range.from_motion('aw')
|
||||
assert.is_not_nil(range)
|
||||
|
||||
-- Check we're back in visual mode with selection restored
|
||||
-- Note: The exact behavior depends on implementation
|
||||
local mode = vim.fn.mode()
|
||||
assert.is_true(mode == 'v' or mode == 'V' or mode == '\22')
|
||||
end)
|
||||
end)
|
||||
|
||||
it('from_tsquery_caps', function()
|
||||
withbuf({
|
||||
'-- a comment',
|
||||
@@ -900,3 +1049,473 @@ describe('Range', function()
|
||||
vim.api.nvim_buf_delete(right_bufnr, { force = true })
|
||||
end)
|
||||
end)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Additional coverage tests
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
describe('Pos additional coverage', function()
|
||||
it('__tostring', function()
|
||||
withbuf({ 'test' }, function()
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local p = Pos.new(bufnr, 1, 1, 0)
|
||||
local s = tostring(p)
|
||||
assert.is_true(s:find('Pos%(1:1%)') ~= nil)
|
||||
assert.is_true(s:find('bufnr=' .. bufnr) ~= nil)
|
||||
|
||||
local p2 = Pos.new(bufnr, 1, 1, 5)
|
||||
s = tostring(p2)
|
||||
assert.is_true(s:find('Pos%(1:1%)') ~= nil)
|
||||
assert.is_true(s:find('off=5') ~= nil)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('from_eol', function()
|
||||
withbuf({ 'hello world' }, function()
|
||||
local eol = Pos.from_eol(nil, 1)
|
||||
assert.are.same(11, eol.col)
|
||||
assert.are.same('hello world', eol:line())
|
||||
end)
|
||||
end)
|
||||
|
||||
it(':eol', function()
|
||||
withbuf({ 'hello world' }, function()
|
||||
local p = Pos.new(nil, 1, 1)
|
||||
local eol = p:eol()
|
||||
assert.are.same(11, eol.col)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('save_to_cursor', function()
|
||||
withbuf({ 'line one', 'line two' }, function()
|
||||
local p = Pos.new(nil, 2, 5)
|
||||
p:save_to_cursor()
|
||||
assert.are.same({ 2, 4 }, vim.api.nvim_win_get_cursor(0))
|
||||
end)
|
||||
end)
|
||||
|
||||
it('save_to_mark', function()
|
||||
withbuf({ 'line one', 'line two' }, function()
|
||||
local b = vim.api.nvim_get_current_buf()
|
||||
local p = Pos.new(b, 2, 5)
|
||||
p:save_to_mark('a')
|
||||
local mark = vim.api.nvim_buf_get_mark(b, 'a')
|
||||
assert.are.same({ 2, 4 }, mark)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('must_next', function()
|
||||
withbuf({ 'abc' }, function()
|
||||
local p = Pos.new(nil, 1, 1)
|
||||
local next = p:must_next(1)
|
||||
assert.are.same(Pos.new(nil, 1, 2), next)
|
||||
|
||||
assert.has.error(function()
|
||||
Pos.new(nil, 1, 3):must_next(1)
|
||||
end, 'error in Pos:next(): Pos:next() returned nil')
|
||||
end)
|
||||
end)
|
||||
|
||||
it('next_while', function()
|
||||
withbuf({ 'aaabbb' }, function()
|
||||
local p = Pos.new(nil, 1, 1)
|
||||
local result = p:next_while(1, function(pos) return pos:char() == 'a' end)
|
||||
assert.are.same(Pos.new(nil, 1, 3), result)
|
||||
|
||||
result = p:next_while(1, function(pos) return pos:char() == 'a' end, true)
|
||||
assert.are.same(Pos.new(nil, 1, 3), result)
|
||||
|
||||
result = p:next_while(1, function() return false end, true)
|
||||
assert.are.same(nil, result)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('insert_before', function()
|
||||
withbuf({ 'world' }, function()
|
||||
local p = Pos.new(nil, 1, 1)
|
||||
p:insert_before('hello ')
|
||||
assert.are.same({ 'hello world' }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
|
||||
end)
|
||||
end)
|
||||
|
||||
it('is_invalid', function()
|
||||
local invalid = Pos.invalid()
|
||||
assert.is_true(invalid:is_invalid())
|
||||
|
||||
withbuf({ 'test' }, function()
|
||||
local valid = Pos.new(nil, 1, 1)
|
||||
assert.is_false(valid:is_invalid())
|
||||
end)
|
||||
end)
|
||||
|
||||
it('__add with number first', function()
|
||||
withbuf({ 'abc' }, function()
|
||||
local p = Pos.new(nil, 1, 1)
|
||||
local result = 1 + p
|
||||
assert.are.same(Pos.new(nil, 1, 2), result)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('__sub with number first', function()
|
||||
withbuf({ 'abc' }, function()
|
||||
local p = Pos.new(nil, 1, 2)
|
||||
local result = 1 - p
|
||||
assert.are.same(Pos.new(nil, 1, 1), result)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('find_next returns nil at end', function()
|
||||
withbuf({ 'abc' }, function()
|
||||
local p = Pos.new(nil, 1, 3)
|
||||
local result = p:find_next(1, 'z')
|
||||
assert.are.same(nil, result)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('find_match with nested brackets', function()
|
||||
withbuf({ '(abc)' }, function()
|
||||
local p = Pos.new(nil, 1, 1) -- '('
|
||||
local match = p:find_match()
|
||||
assert.are.same(Pos.new(nil, 1, 5), match) -- ')'
|
||||
end)
|
||||
|
||||
withbuf({ '{abc}' }, function()
|
||||
local p = Pos.new(nil, 1, 1) -- '{'
|
||||
local match = p:find_match()
|
||||
assert.are.same(Pos.new(nil, 1, 5), match) -- '}'
|
||||
end)
|
||||
|
||||
withbuf({ '(a{b}c)' }, function()
|
||||
-- Test nested: ( at pos 1, { at pos 3
|
||||
local p = Pos.new(nil, 1, 3) -- '{'
|
||||
local match = p:find_match()
|
||||
assert.are.same(Pos.new(nil, 1, 5), match) -- '}'
|
||||
end)
|
||||
end)
|
||||
|
||||
it('from10', function()
|
||||
withbuf({ 'test' }, function()
|
||||
local p = Pos.from10(nil, 1, 0)
|
||||
assert.are.same(1, p.lnum)
|
||||
assert.are.same(1, p.col)
|
||||
|
||||
p = Pos.from10(nil, 2, 5, 10)
|
||||
assert.are.same(2, p.lnum)
|
||||
assert.are.same(6, p.col)
|
||||
assert.are.same(10, p.off)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('as_real with col beyond line length', function()
|
||||
withbuf({ 'ab' }, function()
|
||||
local p = Pos.new(nil, 1, 100)
|
||||
local real = p:as_real()
|
||||
assert.are.same(2, real.col)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('find_next returns nil when not found', function()
|
||||
withbuf({ 'abc' }, function()
|
||||
local p = Pos.new(nil, 1, 1)
|
||||
local result = p:find_next(1, 'z')
|
||||
assert.is_nil(result)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Range additional coverage', function()
|
||||
it('__tostring with nil stop', function()
|
||||
withbuf({ 'test' }, function()
|
||||
local r = Range.new(Pos.new(nil, 1, 1), nil, 'v')
|
||||
local s = tostring(r)
|
||||
assert.is_true(s:find('stop=nil') ~= nil)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('__tostring with off != 0', function()
|
||||
withbuf({ 'test' }, function()
|
||||
local r = Range.new(Pos.new(nil, 1, 1, 5), Pos.new(nil, 1, 4, 3), 'v')
|
||||
local s = tostring(r)
|
||||
assert.is_true(s:find('off=5') ~= nil)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('__tostring', function()
|
||||
withbuf({ 'test' }, function()
|
||||
local r = Range.new(Pos.new(nil, 1, 1), Pos.new(nil, 1, 4), 'v')
|
||||
local s = tostring(r)
|
||||
assert.are.same('v', r.mode)
|
||||
assert.is_true(s:find('Range{') == 1)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('from_lines with negative stop_line', function()
|
||||
withbuf({ 'a', 'b', 'c', 'd', 'e' }, function()
|
||||
local r = Range.from_lines(nil, 1, -1)
|
||||
assert.are.same(5, r.stop.lnum)
|
||||
|
||||
r = Range.from_lines(nil, 1, -2)
|
||||
assert.are.same(4, r.stop.lnum)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('save_to_pos', function()
|
||||
withbuf({ 'hello world' }, function()
|
||||
local r = Range.new(Pos.new(nil, 1, 1), Pos.new(nil, 1, 5), 'v')
|
||||
r:save_to_pos("'[", "']")
|
||||
local start = Pos.from_pos("'[")
|
||||
local stop = Pos.from_pos("']")
|
||||
assert.are.same(1, start.col)
|
||||
assert.are.same(5, stop.col)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('save_to_marks', function()
|
||||
withbuf({ 'hello world' }, function()
|
||||
local b = vim.api.nvim_get_current_buf()
|
||||
local r = Range.new(Pos.new(b, 1, 1), Pos.new(b, 1, 5), 'v')
|
||||
r:save_to_marks('m', 'n')
|
||||
local m = vim.api.nvim_buf_get_mark(b, 'm')
|
||||
local n = vim.api.nvim_buf_get_mark(b, 'n')
|
||||
assert.are.same({ 1, 0 }, m)
|
||||
assert.are.same({ 1, 4 }, n)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('new swaps start and stop when needed', function()
|
||||
withbuf({ 'test' }, function()
|
||||
local r = Range.new(Pos.new(nil, 1, 4), Pos.new(nil, 1, 1), 'v')
|
||||
assert.are.same(1, r.start.col)
|
||||
assert.are.same(4, r.stop.col)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('from_marks with MAX_COL', function()
|
||||
withbuf({ 'test' }, function()
|
||||
local start = Pos.new(nil, 1, 1)
|
||||
local stop = Pos.new(nil, 1, Pos.MAX_COL)
|
||||
start:save_to_pos("'[")
|
||||
stop:save_to_pos("']")
|
||||
local r = Range.from_marks("'[", "']")
|
||||
assert.are.same('V', r.mode)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('contains returns false for non-Pos/Range', function()
|
||||
withbuf({ 'test' }, function()
|
||||
local r = Range.new(Pos.new(nil, 1, 1), Pos.new(nil, 1, 4), 'v')
|
||||
assert.is_false(r:contains('string'))
|
||||
assert.is_false(r:contains(123))
|
||||
end)
|
||||
end)
|
||||
|
||||
it('sub with invalid stop', function()
|
||||
withbuf({ 'abcdef' }, function()
|
||||
local r = Range.new(Pos.new(nil, 1, 1), Pos.new(nil, 1, 6), 'v')
|
||||
local sub = r:sub(1, 100)
|
||||
assert.is_true(sub:is_empty())
|
||||
end)
|
||||
end)
|
||||
|
||||
it('line with negative index', function()
|
||||
withbuf({ 'a', 'b', 'c' }, function()
|
||||
local r = Range.from_lines(nil, 1, 3)
|
||||
local l = r:line(-1)
|
||||
assert.are.same('c', l:text())
|
||||
|
||||
l = r:line(-2)
|
||||
assert.are.same('b', l:text())
|
||||
end)
|
||||
end)
|
||||
|
||||
it('line returns nil for out of bounds', function()
|
||||
withbuf({ 'a', 'b' }, function()
|
||||
local r = Range.from_lines(nil, 1, 2)
|
||||
assert.is_nil(r:line(10))
|
||||
end)
|
||||
end)
|
||||
|
||||
it('trim_start returns range with start=stop when all whitespace', function()
|
||||
withbuf({ ' ' }, function()
|
||||
local r = Range.new(Pos.new(nil, 1, 1), Pos.new(nil, 1, 3), 'v')
|
||||
local trimmed = r:trim_start()
|
||||
assert.is_not.same(nil, trimmed)
|
||||
assert.are.same(trimmed.start, trimmed.stop)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('trim_stop returns range with start=stop when all whitespace', function()
|
||||
withbuf({ ' ' }, function()
|
||||
local r = Range.new(Pos.new(nil, 1, 1), Pos.new(nil, 1, 3), 'v')
|
||||
local trimmed = r:trim_stop()
|
||||
assert.is_not.same(nil, trimmed)
|
||||
assert.are.same(trimmed.start, trimmed.stop)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('smallest returns nil for empty input', function()
|
||||
assert.are.same(nil, Range.smallest {})
|
||||
assert.are.same(nil, Range.smallest { nil })
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Extmark additional coverage', function()
|
||||
it('delete', function()
|
||||
withbuf({ 'test' }, function()
|
||||
local r = Range.new(Pos.new(nil, 1, 1), Pos.new(nil, 1, 4), 'v')
|
||||
local ext = r:save_to_extmark()
|
||||
ext:delete()
|
||||
-- Should not error
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('utils', function()
|
||||
local u = require 'u'
|
||||
|
||||
it('create_delegated_cmd_args', function()
|
||||
local args = {
|
||||
range = 2,
|
||||
line1 = 1,
|
||||
line2 = 5,
|
||||
count = -1,
|
||||
reg = '',
|
||||
bang = true,
|
||||
fargs = { 'arg1', 'arg2' },
|
||||
smods = { silent = true },
|
||||
}
|
||||
local delegated = u.create_delegated_cmd_args(args)
|
||||
assert.are.same({ 1, 5 }, delegated.range)
|
||||
assert.are.same(nil, delegated.count)
|
||||
assert.are.same(nil, delegated.reg)
|
||||
assert.are.same(true, delegated.bang)
|
||||
assert.are.same({ 'arg1', 'arg2' }, delegated.args)
|
||||
|
||||
-- Test range = 1
|
||||
args = { range = 1, line1 = 3, line2 = 3, count = -1, reg = '', bang = false, fargs = {}, smods = {} }
|
||||
delegated = u.create_delegated_cmd_args(args)
|
||||
assert.are.same({ 3 }, delegated.range)
|
||||
|
||||
-- Test range = 0 with count
|
||||
args = { range = 0, line1 = 1, line2 = 1, count = 5, reg = '"', bang = false, fargs = {}, smods = {} }
|
||||
delegated = u.create_delegated_cmd_args(args)
|
||||
assert.are.same(nil, delegated.range)
|
||||
assert.are.same(5, delegated.count)
|
||||
assert.are.same('"', delegated.reg)
|
||||
end)
|
||||
|
||||
it('ucmd with string command', function()
|
||||
u.ucmd('TestUcmdString', 'echo "test"', {})
|
||||
local cmds = vim.api.nvim_get_commands { builtin = false }
|
||||
assert.is_not.same(nil, cmds.TestUcmdString)
|
||||
vim.api.nvim_del_user_command 'TestUcmdString'
|
||||
end)
|
||||
|
||||
it('ucmd with function command', function()
|
||||
local called = false
|
||||
u.ucmd('TestUcmdFunc', function(args)
|
||||
called = true
|
||||
assert.is_not.same(nil, args)
|
||||
end, { range = true })
|
||||
|
||||
local cmds = vim.api.nvim_get_commands { builtin = false }
|
||||
assert.is_not.same(nil, cmds.TestUcmdFunc)
|
||||
|
||||
vim.cmd.TestUcmdFunc()
|
||||
assert.is_true(called)
|
||||
|
||||
vim.api.nvim_del_user_command 'TestUcmdFunc'
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('repeat_', function()
|
||||
local u = require 'u'
|
||||
|
||||
it('is_repeating returns false by default', function()
|
||||
assert.is_false(u.repeat_.is_repeating())
|
||||
end)
|
||||
|
||||
it('run_repeatable executes the function', function()
|
||||
local called = false
|
||||
u.repeat_.run_repeatable(function()
|
||||
called = true
|
||||
end)
|
||||
assert.is_true(called)
|
||||
end)
|
||||
|
||||
it('setup creates keymaps', function()
|
||||
u.repeat_.setup()
|
||||
local maps = vim.api.nvim_get_keymap 'n'
|
||||
local dot_map = vim.iter(maps):find(function(m) return m.lhs == '.' end)
|
||||
local u_map = vim.iter(maps):find(function(m) return m.lhs == 'u' end)
|
||||
assert.is_not.same(nil, dot_map)
|
||||
assert.is_not.same(nil, u_map)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('define_txtobj', function()
|
||||
local u = require 'u'
|
||||
|
||||
it('defines text object keymaps', function()
|
||||
u.define_txtobj('aX', function()
|
||||
return Range.new(Pos.new(nil, 1, 1), Pos.new(nil, 1, 5), 'v')
|
||||
end)
|
||||
|
||||
local xmaps = vim.api.nvim_get_keymap 'x'
|
||||
local omaps = vim.api.nvim_get_keymap 'o'
|
||||
|
||||
local xmap = vim.iter(xmaps):find(function(m) return m.lhs == 'aX' end)
|
||||
local omap_found = vim.iter(omaps):find(function(m) return m.lhs == 'aX' end)
|
||||
|
||||
assert.is_not.same(nil, xmap)
|
||||
assert.is_not.same(nil, omap_found)
|
||||
end)
|
||||
|
||||
it('defines buffer-local text object keymaps', function()
|
||||
withbuf({ 'test' }, function()
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
u.define_txtobj('aY', function()
|
||||
return Range.new(Pos.new(bufnr, 1, 1), Pos.new(bufnr, 1, 4), 'v')
|
||||
end, { buffer = bufnr })
|
||||
|
||||
local xmaps = vim.api.nvim_buf_get_keymap(bufnr, 'x')
|
||||
local omaps = vim.api.nvim_buf_get_keymap(bufnr, 'o')
|
||||
|
||||
local xmap = vim.iter(xmaps):find(function(m) return m.lhs == 'aY' end)
|
||||
local omap_found = vim.iter(omaps):find(function(m) return m.lhs == 'aY' end)
|
||||
|
||||
assert.is_not.same(nil, xmap)
|
||||
assert.is_not.same(nil, omap_found)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Range highlight', function()
|
||||
it('highlight creates highlight and returns clear function', function()
|
||||
withbuf({ 'hello world' }, function()
|
||||
local r = Range.new(Pos.new(nil, 1, 1), Pos.new(nil, 1, 5), 'v')
|
||||
local hl = r:highlight('Search', { timeout = 100 })
|
||||
assert.is_not.same(nil, hl)
|
||||
assert.is_not.same(nil, hl.ns)
|
||||
assert.is_not.same(nil, hl.clear)
|
||||
hl.clear()
|
||||
end)
|
||||
end)
|
||||
|
||||
it('highlight returns nil for empty range', function()
|
||||
withbuf({ 'test' }, function()
|
||||
local r = Range.new(Pos.new(nil, 1, 1), nil, 'v')
|
||||
local hl = r:highlight('Search')
|
||||
assert.is_nil(hl)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('highlight with priority option', function()
|
||||
withbuf({ 'hello world' }, function()
|
||||
local r = Range.new(Pos.new(nil, 1, 1), Pos.new(nil, 1, 5), 'v')
|
||||
local hl = r:highlight('Search', { priority = 100 })
|
||||
assert.is_not.same(nil, hl)
|
||||
hl.clear()
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
@@ -1,11 +0,0 @@
|
||||
require 'luacov'
|
||||
local function withbuf(lines, f)
|
||||
vim.go.swapfile = false
|
||||
|
||||
vim.cmd.new()
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, lines)
|
||||
local ok, result = pcall(f)
|
||||
vim.cmd.bdelete { bang = true }
|
||||
if not ok then error(result) end
|
||||
end
|
||||
return withbuf
|
||||
Reference in New Issue
Block a user