nvim-ts-autotag/tests/renametag_spec.lua

167 lines
4.6 KiB
Lua
Raw Normal View History

2021-03-08 06:19:01 -06:00
local ts = require 'nvim-treesitter.configs'
local helpers = {}
ts.setup {
ensure_installed = 'maintained',
2021-03-08 20:26:36 -06:00
highlight = {
use_languagetree = true,
enable = true
},
2021-03-08 06:19:01 -06:00
}
local eq = assert.are.same
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 = {
{
2021-03-09 22:48:00 -06:00
name = "html rename open tag" ,
2021-03-08 20:26:36 -06:00
filepath = './sample/index.html',
filetype = "html",
linenr = 10,
key = [[ciwlala]],
before = [[<di|v> dsadsa </div> ]],
after = [[<lala|> dsadsa </lala> ]]
},
{
2021-03-09 22:48:00 -06:00
name = "html rename open tag with attr" ,
2021-03-08 06:19:01 -06:00
filepath = './sample/index.html',
filetype = "html",
linenr = 10,
2021-03-08 20:26:36 -06:00
key = [[ciwlala]],
before = [[<di|v class="lla"> dsadsa </div> ]],
after = [[<lala| class="lla"> dsadsa </lala|> ]]
2021-03-08 06:19:01 -06:00
},
2021-03-13 06:57:34 -06:00
{
name = "html rename close tag with attr" ,
filepath = './sample/index.html',
filetype = "html",
linenr = 10,
key = [[ciwlala]],
before = [[<div class="lla"> dsadsa </di|v> ]],
after = [[<lala class="lla"> dsadsa </lala|> ]]
},
2021-03-12 20:37:36 -06:00
{
name = "html not rename close tag on char <" ,
filepath = './sample/index.html',
filetype = "html",
linenr = 10,
key = [[i<]],
before = [[<div class="lla"> dsadsa |/button> ]],
after = [[<div| class="lla"> dsadsa |</button> ]]
},
2021-03-08 06:19:01 -06:00
{
2021-03-13 06:57:34 -06:00
name = "html not rename close tag with not valid" ,
2021-03-08 06:19:01 -06:00
filepath = './sample/index.html',
filetype = "html",
2021-03-13 06:57:34 -06:00
linenr = 12,
2021-03-08 20:26:36 -06:00
key = [[ciwlala]],
2021-03-13 06:57:34 -06:00
before = {
[[<di|v class="lla" ]],
[[ dsadsa </div>]]
},
after = [[<lala class="lla" ]]
},
{
name = "html not rename close tag with not valid" ,
filepath = './sample/index.html',
filetype = "html",
linenr = 12,
key = [[ciwlala]],
before = {
[[<div class="lla" </d|iv>]],
},
after = [[<div class="lla" </l|ala>]]
2021-03-08 06:19:01 -06:00
},
{
2021-03-09 22:48:00 -06:00
name = "typescriptreact rename open tag" ,
2021-03-08 06:19:01 -06:00
filepath = './sample/index.tsx',
filetype = "typescriptreact",
linenr = 12,
2021-03-08 20:26:36 -06:00
key = [[ciwlala]],
before = [[<di|v> dsadsa </div> ]],
after = [[<lala|> dsadsa </lala> ]]
2021-03-08 06:19:01 -06:00
},
{
2021-03-09 22:48:00 -06:00
name = "typescriptreact rename open tag with attr" ,
2021-03-08 06:19:01 -06:00
filepath = './sample/index.tsx',
filetype = "typescriptreact",
2021-03-08 20:26:36 -06:00
linenr = 12,
key = [[ciwlala]],
before = [[<di|v class="lla"> dsadsa </div> ]],
after = [[<lala| class="lla"> dsadsa </lala> ]]
2021-03-08 06:19:01 -06:00
},
{
2021-03-09 22:48:00 -06:00
name = "typescriptreact rename close tag with attr" ,
2021-03-08 20:26:36 -06:00
filepath = './sample/index.tsx',
filetype = "html",
2021-03-08 06:19:01 -06:00
linenr = 12,
2021-03-08 20:26:36 -06:00
key = [[ciwlala]],
before = [[<div class="lla"> dsadsa </di|v> ]],
after = [[<lala class="lla"> dsadsa </lala|> ]]
2021-03-08 06:19:01 -06:00
},
}
2021-03-08 20:26:36 -06:00
2021-03-08 06:19:01 -06:00
local run_data = {}
for _, value in pairs(data) do
if value.only == true then
table.insert(run_data, value)
break
end
end
if #run_data == 0 then run_data = data end
local autotag = require('nvim-ts-autotag')
2021-03-08 20:26:36 -06:00
autotag.test = true
2021-03-08 06:19:01 -06:00
local function Test(test_data)
for _, value in pairs(test_data) do
it("test "..value.name, function()
2021-03-13 06:57:34 -06:00
local text_before={}
local pos_before={
linenr = value.linenr,
colnr=0
}
if not vim.tbl_islist(value.before) then
value.before = {value.before}
end
local numlnr = 0
for _, text in pairs(value.before) do
local txt = string.gsub(text, '%|' , "")
table.insert(text_before, txt )
if string.match( text, "%|") then
pos_before.colnr = string.find(text, '%|')
pos_before.linenr = pos_before.linenr + numlnr
end
numlnr = numlnr + 1
end
local after = string.gsub(value.after, '%|' , "")
2021-03-08 06:19:01 -06:00
vim.bo.filetype = value.filetype
if vim.fn.filereadable(vim.fn.expand(value.filepath)) == 1 then
2021-03-08 20:26:36 -06:00
vim.cmd(":bd!")
2021-03-08 06:19:01 -06:00
vim.cmd(":e " .. value.filepath)
2021-03-13 06:57:34 -06:00
local bufnr=vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(bufnr, pos_before.linenr -1, pos_before.linenr +#text_before, false, text_before)
vim.fn.cursor(pos_before.linenr, pos_before.colnr)
2021-03-09 09:56:05 -06:00
-- autotag.renameTag()
2021-03-13 06:57:34 -06:00
helpers.feed(value.key, 'x')
2021-03-08 20:26:36 -06:00
helpers.feed("<esc>",'x')
2021-03-13 06:57:34 -06:00
local result = vim.fn.getline(pos_before.linenr)
2021-03-09 09:56:05 -06:00
eq(after, result , "\n\n ERROR: " .. value.name .. "\n")
2021-03-08 06:19:01 -06:00
else
eq(false, true, "\n\n file not exist " .. value.filepath .. "\n")
end
end)
end
end
2021-03-08 20:26:36 -06:00
describe('[rename tag]', function()
2021-03-08 06:19:01 -06:00
Test(run_data)
end)
2021-03-08 20:26:36 -06:00