bugfixes; update README.md
This commit is contained in:
@@ -4,10 +4,27 @@ local withbuf = require '__tt_test_tools'
|
||||
describe('Buffer', function()
|
||||
it('should replace all lines', function()
|
||||
withbuf({}, function()
|
||||
local buf = Buffer.new()
|
||||
local buf = Buffer.from_nr()
|
||||
buf:all():replace 'bleh'
|
||||
local actual_lines = vim.api.nvim_buf_get_lines(buf.buf, 0, -1, false)
|
||||
assert.are.same({ 'bleh' }, actual_lines)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('should replace all but first and last lines', function()
|
||||
withbuf({
|
||||
'one',
|
||||
'two',
|
||||
'three',
|
||||
}, function()
|
||||
local buf = Buffer.from_nr()
|
||||
buf:lines(1, -2):replace 'too'
|
||||
local actual_lines = vim.api.nvim_buf_get_lines(buf.buf, 0, -1, false)
|
||||
assert.are.same({
|
||||
'one',
|
||||
'too',
|
||||
'three',
|
||||
}, actual_lines)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -276,4 +276,183 @@ describe('Range', function()
|
||||
assert.are.same('block', range:line0(1).text())
|
||||
end)
|
||||
end)
|
||||
|
||||
it('from_marks', function()
|
||||
withbuf({ 'line one', 'and line two' }, function()
|
||||
local a = Pos.new(nil, 0, 0)
|
||||
local b = Pos.new(nil, 1, 1)
|
||||
a:save_to_pos "'["
|
||||
b:save_to_pos "']"
|
||||
|
||||
local range = Range.from_marks("'[", "']")
|
||||
assert.are.same(range.start, a)
|
||||
assert.are.same(range.stop, b)
|
||||
assert.are.same(range.mode, 'v')
|
||||
end)
|
||||
end)
|
||||
|
||||
it('from_vtext', function()
|
||||
withbuf({ 'line one', 'and line two' }, function()
|
||||
vim.fn.setpos('.', { 0, 1, 3, 0 }) -- cursor at position (1, 3)
|
||||
vim.cmd.normal 'v' -- enter visual mode
|
||||
vim.cmd.normal 'l' -- select one character to the right
|
||||
local range = Range.from_vtext()
|
||||
assert.are.same(range.start, Pos.new(nil, 0, 2))
|
||||
assert.are.same(range.stop, Pos.new(nil, 0, 3))
|
||||
assert.are.same(range.mode, 'v')
|
||||
end)
|
||||
end)
|
||||
|
||||
it('from_op_func', function()
|
||||
withbuf({ 'line one', 'and line two' }, function()
|
||||
local a = Pos.new(nil, 0, 0)
|
||||
local b = Pos.new(nil, 1, 1)
|
||||
a:save_to_pos "'["
|
||||
b:save_to_pos "']"
|
||||
|
||||
local range = Range.from_op_func 'char'
|
||||
assert.are.same(range.start, a)
|
||||
assert.are.same(range.stop, b)
|
||||
assert.are.same(range.mode, 'v')
|
||||
|
||||
range = Range.from_op_func 'line'
|
||||
assert.are.same(range.start, a)
|
||||
assert.are.same(range.stop, Pos.new(nil, 1, Pos.MAX_COL))
|
||||
assert.are.same(range.mode, 'V')
|
||||
end)
|
||||
end)
|
||||
|
||||
it('from_cmd_args', function()
|
||||
local args = { range = 1 }
|
||||
withbuf({ 'line one', 'and line two' }, function()
|
||||
local a = Pos.new(nil, 0, 0)
|
||||
local b = Pos.new(nil, 1, 1)
|
||||
a:save_to_pos "'<"
|
||||
b:save_to_pos "'>"
|
||||
|
||||
local range = Range.from_cmd_args(args)
|
||||
assert.are.same(range.start, a)
|
||||
assert.are.same(range.stop, b)
|
||||
assert.are.same(range.mode, 'v')
|
||||
end)
|
||||
end)
|
||||
|
||||
it('find_nearest_quotes', function()
|
||||
withbuf({ [[the "quick" brown fox]] }, function()
|
||||
vim.fn.setpos('.', { 0, 1, 5, 0 })
|
||||
local range = Range.find_nearest_quotes()
|
||||
assert.are.same(range.start, Pos.new(nil, 0, 4))
|
||||
assert.are.same(range.stop, Pos.new(nil, 0, 10))
|
||||
end)
|
||||
|
||||
withbuf({ [[the 'quick' brown fox]] }, function()
|
||||
vim.fn.setpos('.', { 0, 1, 5, 0 })
|
||||
local range = Range.find_nearest_quotes()
|
||||
assert.are.same(range.start, Pos.new(nil, 0, 4))
|
||||
assert.are.same(range.stop, Pos.new(nil, 0, 10))
|
||||
end)
|
||||
end)
|
||||
|
||||
it('smallest', function()
|
||||
local r1 = Range.new(Pos.new(nil, 0, 1), Pos.new(nil, 0, 3), 'v')
|
||||
local r2 = Range.new(Pos.new(nil, 0, 2), Pos.new(nil, 0, 4), 'v')
|
||||
local r3 = Range.new(Pos.new(nil, 0, 0), Pos.new(nil, 0, 5), 'v')
|
||||
local smallest = Range.smallest { r1, r2, r3 }
|
||||
assert.are.same(smallest.start, Pos.new(nil, 0, 1))
|
||||
assert.are.same(smallest.stop, Pos.new(nil, 0, 3))
|
||||
end)
|
||||
|
||||
it('clone', function()
|
||||
withbuf({ 'line one', 'and line two' }, function()
|
||||
local original = Range.from_lines(nil, 0, 1)
|
||||
local cloned = original:clone()
|
||||
assert.are.same(original.start, cloned.start)
|
||||
assert.are.same(original.stop, cloned.stop)
|
||||
assert.are.same(original.mode, cloned.mode)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('line_count', function()
|
||||
withbuf({ 'line one', 'and line two', 'line three' }, function()
|
||||
local range = Range.from_lines(nil, 0, 2)
|
||||
assert.are.same(range:line_count(), 3)
|
||||
end)
|
||||
end)
|
||||
|
||||
it('to_linewise()', function()
|
||||
withbuf({ 'line one', 'and line two' }, function()
|
||||
local range = Range.new(Pos.new(nil, 0, 1), Pos.new(nil, 1, 3), 'v')
|
||||
local linewise_range = range:to_linewise()
|
||||
assert.are.same(linewise_range.start.col, 0)
|
||||
assert.are.same(linewise_range.stop.col, Pos.MAX_COL)
|
||||
assert.are.same(linewise_range.mode, 'V')
|
||||
end)
|
||||
end)
|
||||
|
||||
it('is_empty', function()
|
||||
withbuf({ 'line one', 'and line two' }, function()
|
||||
local range = Range.new(Pos.new(nil, 0, 0), Pos.new(nil, 0, 0), 'v')
|
||||
assert.is_true(range:is_empty())
|
||||
|
||||
local range2 = Range.new(Pos.new(nil, 0, 0), Pos.new(nil, 0, 1), 'v')
|
||||
assert.is_false(range2:is_empty())
|
||||
end)
|
||||
end)
|
||||
|
||||
it('trim_start', function()
|
||||
withbuf({ ' line one', 'line two' }, function()
|
||||
local range = Range.new(Pos.new(nil, 0, 0), Pos.new(nil, 0, 9), 'v')
|
||||
local trimmed = range:trim_start()
|
||||
assert.are.same(trimmed.start, Pos.new(nil, 0, 3)) -- should be after the spaces
|
||||
end)
|
||||
end)
|
||||
|
||||
it('trim_stop', function()
|
||||
withbuf({ 'line one ', 'line two' }, function()
|
||||
local range = Range.new(Pos.new(nil, 0, 0), Pos.new(nil, 0, 9), 'v')
|
||||
local trimmed = range:trim_stop()
|
||||
assert.are.same(trimmed.stop, Pos.new(nil, 0, 7)) -- should be before the spaces
|
||||
end)
|
||||
end)
|
||||
|
||||
it('contains', function()
|
||||
withbuf({ 'line one', 'and line two' }, function()
|
||||
local range = Range.new(Pos.new(nil, 0, 1), Pos.new(nil, 0, 3), 'v')
|
||||
local pos = Pos.new(nil, 0, 2)
|
||||
assert.is_true(range:contains(pos))
|
||||
|
||||
pos = Pos.new(nil, 0, 4) -- outside of range
|
||||
assert.is_false(range:contains(pos))
|
||||
end)
|
||||
end)
|
||||
|
||||
it('shrink', function()
|
||||
withbuf({ 'line one', 'and line two' }, function()
|
||||
local range = Range.new(Pos.new(nil, 0, 1), Pos.new(nil, 1, 3), 'v')
|
||||
local shrunk = range:shrink(1)
|
||||
assert.are.same(shrunk.start, Pos.new(nil, 0, 2))
|
||||
assert.are.same(shrunk.stop, Pos.new(nil, 1, 2))
|
||||
end)
|
||||
end)
|
||||
|
||||
it('must_shrink', function()
|
||||
withbuf({ 'line one', 'and line two' }, function()
|
||||
local range = Range.new(Pos.new(nil, 0, 1), Pos.new(nil, 1, 3), 'v')
|
||||
local shrunk = range:must_shrink(1)
|
||||
assert.are.same(shrunk.start, Pos.new(nil, 0, 2))
|
||||
assert.are.same(shrunk.stop, Pos.new(nil, 1, 2))
|
||||
|
||||
assert.has.error(function() range:must_shrink(100) end, 'error in Range:must_shrink: Range:shrink() returned nil')
|
||||
end)
|
||||
end)
|
||||
|
||||
it('set_visual_selection', function()
|
||||
withbuf({ 'line one', 'and line two' }, function()
|
||||
local range = Range.from_lines(nil, 0, 1)
|
||||
range:set_visual_selection()
|
||||
|
||||
assert.are.same(Pos.from_pos 'v', Pos.new(nil, 0, 0))
|
||||
assert.are.same(Pos.from_pos '.', Pos.new(nil, 1, 11))
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user