nvim-ts-autotag/tests/closetag_spec.lua

164 lines
4.4 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',
highlight = {enable = true},
}
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 17:41:24 -06:00
name = "html close tag" ,
2021-03-08 06:19:01 -06:00
filepath = './sample/index.html',
filetype = "html",
linenr = 10,
key = [[>]],
2021-03-09 17:41:24 -06:00
before = [[<div|]],
after = [[<div></div>]]
2021-03-08 06:19:01 -06:00
},
{
name = "html not close on input tag" ,
filepath = './sample/index.html',
filetype = "html",
linenr = 10,
key = [[>]],
before = [[<input| ]],
after = [[<input>| ]]
},
2021-03-09 09:56:05 -06:00
{
name = "html not close inside quote" ,
filepath = './sample/index.html',
filetype = "html",
linenr = 10,
key = [[>]],
before = [[<div class="aa|"> </div> ]],
after = [[<div class="aa>|"> </div> ]]
},
2021-03-09 17:41:24 -06:00
{
only=true,
name = "html not close on exist tag" ,
filepath = './sample/index.html',
filetype = "html",
linenr = 10,
key = [[>]],
before = [[<div| </div]],
after = [[<div> </div>]]
},
2021-03-08 06:19:01 -06:00
{
name = "typescriptreact auto close tag" ,
filepath = './sample/index.tsx',
filetype = "typescriptreact",
linenr = 12,
key = [[>]],
2021-03-09 17:41:24 -06:00
before = [[<Img|]],
after = [[<Img>|</Img>]]
2021-03-08 06:19:01 -06:00
},
2021-03-09 09:56:05 -06:00
{
2021-03-09 17:41:24 -06:00
name = "typescriptreact don't close closing tag" ,
2021-03-09 09:56:05 -06:00
filepath = './sample/index.tsx',
filetype = "typescriptreact",
linenr = 12,
key = [[>]],
before = [[<button className="btn " onClick={()}> </button| ]],
after = [[<button className="btn " onClick={()}> </button>| ]]
},
2021-03-09 17:41:24 -06:00
{
name = "typescriptreact not close on expresion" ,
filepath = './sample/index.tsx',
filetype = "typescriptreact",
linenr = 6,
key = [[>]],
before = [[<button className="btn " onClick={(|)}> </button> ]],
after = [[<button className="btn " onClick={(>|)}> </button> ]]
},
2021-03-09 09:56:05 -06:00
{
name = "typescriptreact not close on script" ,
filepath = './sample/index.tsx',
filetype = "typescriptreact",
linenr = 6,
key = [[>]],
before = [[const data:Array<string| ]],
after = [[const data:Array<string> ]]
},
2021-03-09 17:41:24 -06:00
2021-03-08 06:19:01 -06:00
{
name = "typescriptreact not close on script" ,
filepath = './sample/index.tsx',
filetype = "typescriptreact",
linenr = 6,
key = [[>]],
2021-03-09 17:41:24 -06:00
before = [[{(card.data | 0) && <div></div>}]],
after = [[{(card.data >| 0) && <div></div>}]]
},
{
name = "vue auto close tag" ,
filepath = './sample/index.vue',
filetype = "vue",
linenr = 4,
key = [[>]],
before = [[<Img|]],
after = [[<Img>|</Img>]]
},
{
name = "vue not close on script",
filepath = './sample/index.vue',
filetype = "vue",
linenr = 12,
key = [[>]],
2021-03-08 06:19:01 -06:00
before = [[const data:Array<string| ]],
after = [[const data:Array<string> ]]
},
}
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-09 17:41:24 -06:00
autotag.enableRename = false
2021-03-08 06:19:01 -06:00
2021-03-09 17:41:24 -06:00
local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils')
_G.TU=ts_utils
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()
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 =value.linenr
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-09 17:41:24 -06:00
vim.bo.filetype = value.filetype
2021-03-08 06:19:01 -06:00
vim.fn.setline(line , before)
2021-03-09 17:41:24 -06:00
vim.fn.cursor(line, p_before -1)
2021-03-08 20:26:36 -06:00
-- autotag.closeTag()
2021-03-08 06:19:01 -06:00
helpers.insert(value.key)
local result = vim.fn.getline(line)
eq(after, result , "\n\n text error: " .. value.name .. "\n")
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('[close tag]', function()
2021-03-08 06:19:01 -06:00
Test(run_data)
end)