add unit test (#9)

* add test

* add test with feedkeys not finish

* fix #6 skip single quote with previous character is a word

* update README.md

* Hotfix remove print msg

* add test WIP

* update README

* add more pairs group on breakline

* fix wrong pairs on html

* remove print msg

* fix character in quote

* finish test
This commit is contained in:
windwp 2021-01-17 09:06:38 +07:00 committed by GitHub
parent c1df7f7d74
commit 431a963df5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 195 additions and 0 deletions

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
test:
nvim --headless --noplugin -u tests/minimal.vim -c "PlenaryBustedDirectory tests/ {minimal_init = 'tests/minimal.vim'}"

7
tests/minimal.vim Normal file
View File

@ -0,0 +1,7 @@
set rtp +=.
set rtp +=~/.vim/autoload/plenary.nvim/
runtime! plugin/plenary.vim
lua require("plenary/busted")
lua require("nvim-autopairs")

65
tests/pairs.lua Normal file
View File

@ -0,0 +1,65 @@
local data={
-- {
-- name = "add normal bracket" ,
-- key = [[(]],
-- before = [[aaaa|]],
-- after = [[aaaa(|)]]
-- },
{
name = "add normal quote" ,
key = [["]],
before = [[aa| aa]],
after = [[aa"|" aa]]
},
{
name = "move right end line " ,
key = [["]],
before = [[aaaaaa|"]],
after = [[aaaaaa"|]]
},
-- {
-- name = "move right when inside quote" ,
-- key = [["]],
-- before = [[("abcd|")]],
-- after = [[("abcd"|)]]
-- }
}
local npairs=require('nvim-autopairs')
npairs.setup()
local eq = assert.are.same
function wait(time)
local duration = os.time() + time
while os.time() < duration do end
end
vim.wait(5,function() return true end)
-- TODO change to use feedkeys
for _, value in pairs(data) do
local before = string.gsub(value.before , '%|' , "")
local after = string.gsub(value.after , '%|' , "")
local p_before = string.find(value.before , '%|')
local p_after = string.find(value.after , '|')
local line = 1
local bufnr = vim.fn.bufnr('%')
vim.fn.setline(line , before)
vim.fn.setpos('.' ,{bufnr , line , p_before , 0})
vim.fn.feedkeys("i")
vim.fn.feedkeys(npairs.esc(value.key))
vim.cmd[[redraw]]
local done=false
vim.wait(5,function()
pcall(vim.schedule_wrap( function()
local result = vim.fn.getline(line)
local pos = vim.fn.getpos('.')
eq(p_after , pos[3]+ 1 , "\n\n pos error: " .. value.name .."\n")
eq(after, result , "\n\n text error: " .. value.name .."\n")
done=true
end))
return done
end)
while done==false do end
end

121
tests/pairs_spec.lua Normal file
View File

@ -0,0 +1,121 @@
local helpers = {}
local npairs=require('nvim-autopairs')
npairs.setup()
local eq = assert.are.same
_G.npairs = npairs;
vim.api.nvim_set_keymap('i' , '<CR>','v:lua.npairs.check_break_line_char()', {expr = true , noremap = true})
function helpers.feed(text, feed_opts)
feed_opts = feed_opts or 'n'
local to_feed = vim.api.nvim_replace_termcodes(text, true, false, true)
vim.api.nvim_feedkeys(to_feed, feed_opts, true)
end
function helpers.insert(text)
helpers.feed('i' .. text, 'x')
end
local data = {
{
name = "add normal bracket" ,
key = [[(]],
before = [[aaaa| ]],
after = [[aaaa(|) ]]
},
{
name = "add normal quote" ,
key = [["]],
before = [[aa| aa]],
after = [[aa"|" aa]]
},
{
name = "don't add quote after alphabet char" ,
key = [["]],
before = [[aa|aa]],
after = [[aa"|aa]]
},
{
name = "move right end line " ,
key = [["]],
before = [[aaaa|"]],
after = [[aaaa"|]]
},
{
name = "move right when inside quote" ,
key = [["]],
before = [[("abcd|")]],
after = [[("abcd"|)]]
},
{
name = "move right when inside quote with special slash" ,
key = [["]],
before = [[("abcd\"|")]],
after = [[("abcd\""|)]]
},
{
name = "breakline on {" ,
filetype="javascript",
key = [[<cr>]],
before = [[a{|}]],
after = [[}]]
},
{
name = "breakline on (" ,
filetype="javascript",
key = [[<cr>]],
before = [[a(|)]],
after = [[)]]
},
{
name = "breakline on ]" ,
filetype="javascript",
key = [[<cr>]],
before = [[a[|] ]],
after = "] "
},
{
name = "breakline on < html" ,
filetype="html",
key = [[<cr>]],
before = [[<div>|</div>]],
after = [[</div>]]
}
}
describe('autopairs ', function()
for _, value in pairs(data) do
it("test "..value.name, function()
local before = string.gsub(value.before , '%|' , "")
local after = string.gsub(value.after , '%|' , "")
local p_before = string.find(value.before , '%|')
local p_after = string.find(value.after , '%|')
local line = 1
if value.filetype ~= nil then
vim.bo.filetype = value.filetype
else
vim.bo.filetype = "text"
end
vim.fn.setline(line , before)
vim.fn.setpos('.' ,{0 , line , p_before , 0})
helpers.insert(value.key)
helpers.feed("<esc>")
local result = vim.fn.getline(line)
local pos = vim.fn.getpos('.')
if value.key ~= '<cr>' then
eq(p_after , pos[3]+ 1 , "\n\n pos error: " .. value.name .. "\n")
eq(after, result , "\n\n text error: " .. value.name .. "\n")
else
local line2 = vim.fn.getline(line + 2)
eq(line + 1, pos[2], '\n\n breakline error:' .. value.name .. "\n")
eq(after, line2 , "\n\n text error: " .. value.name .. "\n")
vim.fn.setline(line, '')
vim.fn.setline(line+ 1, '')
vim.fn.setline(line+ 2, '')
end
end)
end
print("end")
end)