test: add buffer deleteline test and buffer lines test, fix bugs with that
This commit is contained in:
parent
89e2daf2cd
commit
8c43260cbb
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,3 +6,4 @@ tts/node_modules/
|
||||
tts/package-lock.json
|
||||
Trans.json
|
||||
ultimate.db
|
||||
lua/.luarc.json
|
||||
|
@ -1,10 +0,0 @@
|
||||
{
|
||||
"workspace.library": [
|
||||
"/home/zoran/.local/share/nvim/lazy/neodev.nvim/types/nightly",
|
||||
"/home/zoran/.local/share/bob/nightly/nvim-linux64/share/nvim/runtime/lua",
|
||||
"/home/zoran/project/Neovim/Trans.nvim/lua",
|
||||
"${3rd}/luv/library",
|
||||
"/home/zoran/.config/LLS-Addons/busted/library",
|
||||
"${3rd}/luassert/library"
|
||||
]
|
||||
}
|
@ -17,7 +17,7 @@ end
|
||||
function buffer:deleteline(_start, _end)
|
||||
---@diagnostic disable-next-line: cast-local-type
|
||||
_start = _start and _start - 1 or self:line_count() - 1
|
||||
_end = _end and _end - 1 or _start + 1
|
||||
_end = _end or _start + 1 -- because of end exclusive
|
||||
api.nvim_buf_set_lines(self.bufnr, _start, _end, false, {})
|
||||
end
|
||||
|
||||
@ -70,7 +70,7 @@ end
|
||||
---@return string[]
|
||||
function buffer:lines(i, j)
|
||||
i = i and i - 1 or 0
|
||||
j = j and j - 1 or -1
|
||||
j = j or -1 -- because of end exclusive
|
||||
return api.nvim_buf_get_lines(self.bufnr, i, j, false)
|
||||
end
|
||||
|
||||
@ -90,7 +90,12 @@ end
|
||||
---Get buffer line count
|
||||
---@return integer
|
||||
function buffer:line_count()
|
||||
return api.nvim_buf_line_count(self.bufnr)
|
||||
local line_count = api.nvim_buf_line_count(self.bufnr)
|
||||
if line_count == 1 and self[1] == '' then
|
||||
return 0
|
||||
end
|
||||
|
||||
return line_count
|
||||
end
|
||||
|
||||
---Set line content
|
||||
@ -98,7 +103,6 @@ end
|
||||
---@param one_index number? line number should be set[one index] or let it be nil to append
|
||||
function buffer:setline(nodes, one_index)
|
||||
local append_line_index = self:line_count() + 1
|
||||
if append_line_index == 2 and self[1] == '' then append_line_index = 1 end
|
||||
one_index = one_index or append_line_index
|
||||
if one_index > append_line_index then
|
||||
for i = append_line_index, one_index - 1 do
|
||||
@ -147,7 +151,6 @@ buffer.__index = function(self, key)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
buffer.__newindex = function(self, key, nodes)
|
||||
if type(key) == 'number' then
|
||||
self:setline(nodes, key)
|
||||
|
@ -1,72 +0,0 @@
|
||||
local Trans = require 'Trans'
|
||||
local node = Trans.util.node
|
||||
local i, t, pr, f = node.item, node.text, node.prompt, node.format
|
||||
|
||||
---@param func fun(buffer: TransBuffer)
|
||||
---@return fun()
|
||||
local function with_buffer(func)
|
||||
return function()
|
||||
local buffer = Trans.buffer.new()
|
||||
func(buffer)
|
||||
buffer:destroy()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe('buffer:setline()', function()
|
||||
it('and buffer[i] can accept a string as first arg', with_buffer(function(buffer)
|
||||
buffer:setline 'hello world'
|
||||
buffer[2] = 'hello world'
|
||||
assert.are.equal(buffer[1], 'hello world')
|
||||
assert.are.equal(buffer[2], 'hello world')
|
||||
end))
|
||||
|
||||
it('and buffer[i] can accept a node as first arg', with_buffer(function(buffer)
|
||||
buffer:setline(i { 'hello world' })
|
||||
buffer[2] = i { 'hello world' }
|
||||
assert.are.equal(buffer[1], 'hello world')
|
||||
assert.are.equal(buffer[2], 'hello world')
|
||||
end))
|
||||
|
||||
it('and buffer[i] can accept a node list as first arg', with_buffer(function(buffer)
|
||||
buffer:setline {
|
||||
i { 'hello ' },
|
||||
i { 'world' },
|
||||
}
|
||||
|
||||
buffer[2] = {
|
||||
i { 'hello ' },
|
||||
i { 'world' },
|
||||
}
|
||||
|
||||
assert.are.equal(buffer[1], 'hello world')
|
||||
assert.are.equal(buffer[2], 'hello world')
|
||||
end))
|
||||
|
||||
it('and buffer[i] accept linenr more than line_count will fill with empty line', with_buffer(function(buffer)
|
||||
buffer:setline('hello world', 3)
|
||||
buffer[4] = 'hello world'
|
||||
assert.are.equal(buffer[1], '')
|
||||
assert.are.equal(buffer[2], '')
|
||||
assert.are.equal(buffer[3], 'hello world')
|
||||
assert.are.equal(buffer[4], 'hello world')
|
||||
end))
|
||||
|
||||
|
||||
it('can accept one index linenr as second arg', with_buffer(function(buffer)
|
||||
buffer:setline({
|
||||
i { 'hello ' },
|
||||
i { 'world' },
|
||||
}, 1)
|
||||
assert.are.equal(buffer[1], 'hello world')
|
||||
end))
|
||||
|
||||
it('when no second arg, it will append line', with_buffer(function(buffer)
|
||||
buffer[1] = 'hello'
|
||||
buffer:setline 'world'
|
||||
|
||||
assert.are.equal(buffer[2], 'world')
|
||||
end))
|
||||
end)
|
||||
|
||||
-- TODO :Add node test
|
@ -1,17 +1,4 @@
|
||||
local Trans = require 'Trans'
|
||||
local node = Trans.util.node
|
||||
local i, t, pr, f = node.item, node.text, node.prompt, node.format
|
||||
|
||||
---@param func fun(buffer: TransBuffer)
|
||||
---@return fun()
|
||||
local function with_buffer(func)
|
||||
return function()
|
||||
local buffer = Trans.buffer.new()
|
||||
func(buffer)
|
||||
buffer:destroy()
|
||||
end
|
||||
end
|
||||
|
||||
require 'test.setup'
|
||||
|
||||
describe('buffer:setline()', function()
|
||||
it('can accept one index linenr as second arg', with_buffer(function(buffer)
|
||||
@ -22,7 +9,7 @@ describe('buffer:setline()', function()
|
||||
assert.are.equal(buffer[1], 'hello world')
|
||||
end))
|
||||
|
||||
it('when no second arg, it will append line', with_buffer(function(buffer)
|
||||
it('will append line when no second arg passed', with_buffer(function(buffer)
|
||||
buffer[1] = 'hello'
|
||||
buffer:setline 'world'
|
||||
|
||||
@ -59,7 +46,7 @@ describe('buffer:setline()', function()
|
||||
assert.are.equal(buffer[2], 'hello world')
|
||||
end))
|
||||
|
||||
it(' will fill with empty line if accept linenr more than line_count', with_buffer(function(buffer)
|
||||
it(' will fill with empty line if linenr is more than line_count', with_buffer(function(buffer)
|
||||
buffer:setline('hello world', 3)
|
||||
buffer[4] = 'hello world'
|
||||
assert.are.equal(buffer[1], '')
|
||||
@ -78,3 +65,80 @@ describe('buffer:setline()', function()
|
||||
end)
|
||||
end)
|
||||
-- TODO :Add node test
|
||||
|
||||
describe('buffer:deleteline()', with_buffer(function(buffer)
|
||||
before_each(function()
|
||||
buffer:wipe()
|
||||
end)
|
||||
|
||||
it('will delete the last line if no arg', function()
|
||||
buffer[1] = 'line 1'
|
||||
buffer[2] = 'line 2'
|
||||
buffer:deleteline()
|
||||
assert.are.equal(buffer:line_count(), 1)
|
||||
assert.are.equal(buffer[1], 'line 1')
|
||||
|
||||
buffer:deleteline()
|
||||
assert.are.equal(buffer:line_count(), 0)
|
||||
end)
|
||||
|
||||
it('can accept a one indexed linenr to be deleted', function()
|
||||
buffer[1] = 'line 1'
|
||||
buffer[2] = 'line 2'
|
||||
buffer:deleteline(1)
|
||||
assert.are.equal(buffer[1], 'line 2')
|
||||
end)
|
||||
|
||||
it('can accept a one indexed range to be deleted', function()
|
||||
stub(api, 'nvim_buf_set_lines')
|
||||
buffer[1] = 'line 1'
|
||||
buffer[2] = 'line 2'
|
||||
buffer[3] = 'line 3'
|
||||
buffer:deleteline(1, 2)
|
||||
---@diagnostic disable-next-line: param-type-mismatch
|
||||
assert.stub(api.nvim_buf_set_lines).called_with(buffer.bufnr, 0, 2, false, {})
|
||||
|
||||
api.nvim_buf_set_lines:revert()
|
||||
buffer:deleteline(1, 2)
|
||||
assert.are.equal(buffer[1], 'line 3')
|
||||
end)
|
||||
end))
|
||||
|
||||
|
||||
describe('buffer:lines()', with_buffer(function(buffer)
|
||||
before_each(function()
|
||||
buffer:wipe()
|
||||
end)
|
||||
|
||||
it('will return all lines if no arg', function()
|
||||
buffer[1] = 'line 1'
|
||||
buffer[2] = 'line 2'
|
||||
local lines = buffer:lines()
|
||||
assert.are.equal(lines[1], 'line 1')
|
||||
assert.are.equal(lines[2], 'line 2')
|
||||
end)
|
||||
|
||||
it('will return all lines after linenr accept a one indexed linenr', function()
|
||||
buffer[1] = 'line 1'
|
||||
buffer[2] = 'line 2'
|
||||
buffer[3] = 'line 3'
|
||||
buffer[4] = 'line 4'
|
||||
local lines = buffer:lines(2)
|
||||
assert.are.equal(lines[1], 'line 2')
|
||||
assert.are.equal(lines[2], 'line 3')
|
||||
assert.are.equal(lines[3], 'line 4')
|
||||
end)
|
||||
|
||||
it('can accept a one indexed range', function()
|
||||
buffer[1] = 'line 1'
|
||||
buffer[2] = 'line 2'
|
||||
buffer[3] = 'line 3'
|
||||
local lines = buffer:lines(1, 2)
|
||||
assert.are.equal(lines[1], 'line 1')
|
||||
assert.are.equal(lines[2], 'line 2')
|
||||
|
||||
lines = buffer:lines(2, 3)
|
||||
assert.are.equal(lines[1], 'line 2')
|
||||
assert.are.equal(lines[2], 'line 3')
|
||||
end)
|
||||
end))
|
||||
|
18
lua/test/setup.lua
Normal file
18
lua/test/setup.lua
Normal file
@ -0,0 +1,18 @@
|
||||
_G.Trans = require 'Trans'
|
||||
local node = Trans.util.node
|
||||
_G.i, _G.t, _G.pr, _G.f = node.item, node.text, node.prompt, node.format
|
||||
|
||||
_G.api = vim.api
|
||||
_G.fn = vim.fn
|
||||
_G.mock = require 'luassert.mock'
|
||||
_G.stub = require 'luassert.stub'
|
||||
|
||||
---@param func fun(buffer: TransBuffer)
|
||||
---@return fun()
|
||||
function _G.with_buffer(func)
|
||||
return function()
|
||||
local buffer = Trans.buffer.new()
|
||||
func(buffer)
|
||||
buffer:destroy()
|
||||
end
|
||||
end
|
9
lua/test/util_spec.lua
Normal file
9
lua/test/util_spec.lua
Normal file
@ -0,0 +1,9 @@
|
||||
require 'test.setup'
|
||||
|
||||
local util = Trans.util
|
||||
|
||||
describe('util.display_height', with_buffer(function(buffer)
|
||||
--- TODO :
|
||||
it('can calculate the height of lines when window with wrap option', function()
|
||||
end)
|
||||
end))
|
Loading…
x
Reference in New Issue
Block a user