Move extmark assertion to testing configuration

This commit is contained in:
HiPhish 2024-01-30 23:06:09 +01:00
parent 5941c8dbbd
commit 6c7d918621
5 changed files with 59 additions and 91 deletions

View file

@ -1,9 +1,7 @@
local say = require 'say'
local rpcrequest = vim.rpcrequest
local test_utils = require 'testing.utils'
local exec_lua = 'nvim_exec_lua'
local filter = vim.fn.filter
---Markdown document with Lua code inside a code block
local markdown_with_injected_lua = [[This is some Markdown
@ -31,21 +29,6 @@ describe('Buffer Manipulation', function()
return rpcrequest(nvim, method, ...)
end
---Asserts that there are Rainbow Delimiters extmarks at the given position
---@param arguments integer[] Row and column, both zero-based
local function has_extmarks_at(_state, arguments, lang)
local row, column = arguments[1], arguments[2]
local nsid = request(exec_lua, 'return require("rainbow-delimiters.lib").nsids[...]', {lang or 'lua'})
local extmarks = request(exec_lua, 'return vim.inspect_pos(...).extmarks', {0, row, column})
filter(extmarks, function(_, v) return v.ns_id == nsid end)
return #extmarks > 0
end
say:set('assertion.extmarks_at.positive', 'Expected extmarks at (%s, %s)')
say:set('assertion.extmarks_at.negative', 'Expected no extmarks at (%s, %s)')
assert:register('assertion', 'extmarks_at', has_extmarks_at, 'assertion.extmarks_at.positive', 'assertion.extmarks_at.negative')
before_each(function()
nvim = test_utils.start_embedded()
request(exec_lua, 'TSEnsure(...)', {'lua', 'vim', 'markdown'})
@ -69,7 +52,7 @@ describe('Buffer Manipulation', function()
request('nvim_exec_lua', 'TSEnsure(...)', {'lua', 'markdown'})
request('nvim_buf_set_lines', 0, 0, -2, true, vim.fn.split(markdown_with_injected_lua, '\n'))
request('nvim_buf_set_option', 0, 'filetype', 'markdown')
assert.has_extmarks_at(3, 5, 'lua')
assert.nvim(nvim).has_extmarks_at(3, 5, 'lua')
-- Move Lua line out of code block
request('nvim_cmd', {cmd = 'move', range = {4}, args = {5}}, {})
@ -81,14 +64,14 @@ describe('Buffer Manipulation', function()
-- `on_tree_changed` callback on the parser and the extmarks will
-- remain uncleared
if true then return end
assert.Not.has_extmarks_at(4, 5, 'lua')
assert.nvim(nvim).Not.has_extmarks_at(4, 5, 'lua')
end)
it('Adds extmarks when moving line into injected langauge', function()
request('nvim_exec_lua', 'TSEnsure(...)', {'lua', 'markdown'})
request('nvim_buf_set_lines', 0, 0, -2, true, vim.fn.split(markdown_without_injected_lua, '\n'))
request('nvim_buf_set_option', 0, 'filetype', 'markdown')
assert.Not.has_extmarks_at(4, 5, 'lua')
assert.nvim(nvim).Not.has_extmarks_at(4, 5, 'lua')
-- Move Lua line out of code block
request('nvim_cmd', {cmd = 'move', range = {5}, args = {3}}, {})
@ -100,6 +83,6 @@ describe('Buffer Manipulation', function()
-- `on_tree_changed` callback on the parser and the extmarks will
-- remain uncleared
if true then return end
assert.has_extmarks_at(3, 5, 'lua')
assert.nvim.has_extmarks_at(3, 5, 'lua')
end)
end)

View file

@ -1,4 +1,3 @@
local say = require 'say'
local rpcrequest = vim.rpcrequest
local test_utils = require 'testing.utils'
@ -8,8 +7,6 @@ local call_function = 'nvim_call_function'
local buf_set_lines = 'nvim_buf_set_lines'
local buf_set_option = 'nvim_buf_set_option'
local filter = vim.fn.filter
describe('The global strategy', function()
local nvim
@ -17,20 +14,6 @@ describe('The global strategy', function()
return rpcrequest(nvim, method, ...)
end
---Asserts that there are Rainbow Delimiters extmarks at the given position
---@param arguments integer[] Row and column, both zero-based
local function has_extmarks_at(_state, arguments, lang)
local row, column = arguments[1], arguments[2]
local nsid = request(exec_lua, 'return require("rainbow-delimiters.lib").nsids[...]', {lang or 'lua'})
local extmarks = request(exec_lua, 'return vim.inspect_pos(...).extmarks', {0, row, column})
filter(extmarks, function(_, v) return v.ns_id == nsid end)
return #extmarks > 0
end
say:set('assertion.extmarks_at.positive', 'Expected extmarks at (%s, %s)')
say:set('assertion.extmarks_at.negative', 'Expected no extmarks at (%s, %s)')
assert:register('assertion', 'extmarks_at', has_extmarks_at, 'assertion.extmarks_at.positive', 'assertion.extmarks_at.negative')
before_each(function()
nvim = test_utils.start_embedded()
request(exec_lua, 'TSEnsure(...)', {'lua', 'vim'})
@ -56,10 +39,10 @@ describe('The global strategy', function()
it('Does not reactivate when making changes', function()
request(buf_set_lines, 0, 0, -1, true, {'print({{{{{}}}}})', '-- vim:ft=lua'})
request(buf_set_option, 0, 'filetype', 'lua')
assert.has_extmarks_at(0, 5)
assert.nvim(nvim).has_extmarks_at(0, 5, 'lua')
request(call_function, 'rainbow_delimiters#disable', {0})
assert.Not.has_extmarks_at(0, 5)
assert.nvim(nvim).Not.has_extmarks_at(0, 5, 'lua')
-- Add a new pair of curly braces
-- (jump to first column, find the first closing brace, insert new pair)
@ -67,7 +50,7 @@ describe('The global strategy', function()
request(feedkeys, keys, 'n', false)
assert.is.same({'print({{{{{{}}}}}})'}, request('nvim_buf_get_lines', 0, 0, 1, true))
assert.Not.has_extmarks_at(0, 5)
assert.nvim(nvim).Not.has_extmarks_at(0, 5, 'lua')
assert.is.equal(0, request(exec_lua, 'return the_strategy.attachments[1]', {}))
end)
@ -83,8 +66,8 @@ describe('The global strategy', function()
request(buf_set_option, 0, 'filetype', 'lua')
-- The Lua code is highlighted, the Vim code not
assert.has_extmarks_at(0, 6, 'lua')
assert.Not.has_extmarks_at(2, 13, 'vim')
assert.nvim(nvim).has_extmarks_at(0, 6, 'lua')
assert.nvim(nvim).Not.has_extmarks_at(2, 13, 'vim')
end)
it('Ignores non-whitelisted injected languages', function()
@ -99,8 +82,8 @@ describe('The global strategy', function()
request(buf_set_option, 0, 'filetype', 'lua')
-- The Lua code is highlighted, the Vim code not
assert.has_extmarks_at(0, 6, 'lua')
assert.Not.has_extmarks_at(2, 13, 'vim')
assert.nvim(nvim).has_extmarks_at(0, 6, 'lua')
assert.nvim(nvim).Not.has_extmarks_at(2, 13, 'vim')
end)
it('Applies highlighting to nested code', function()
@ -121,7 +104,7 @@ return foo]]
local keys = vim.api.nvim_replace_termcodes("ob = print('b'),<esc>", true, false, true)
vim.fn.rpcrequest(nvim,'nvim_feedkeys', keys, '', false)
assert.has_extmarks_at(2, 11, 'lua')
assert.has_extmarks_at(3, 11, 'lua')
assert.nvim(nvim).has_extmarks_at(2, 11, 'lua')
assert.nvim(nvim).has_extmarks_at(3, 11, 'lua')
end)
end)

View file

@ -1,4 +1,3 @@
local say = require 'say'
local rpcrequest = vim.rpcrequest
local test_utils = require 'testing.utils'
@ -8,8 +7,6 @@ local call_function = 'nvim_call_function'
local buf_set_lines = 'nvim_buf_set_lines'
local buf_set_option = 'nvim_buf_set_option'
local filter = vim.fn.filter
describe('The local strategy', function()
local nvim
@ -17,20 +14,6 @@ describe('The local strategy', function()
return rpcrequest(nvim, method, ...)
end
---Asserts that there are Rainbow Delimiters extmarks at the given position
---@param arguments integer[] Row and column, both zero-based
local function has_extmarks_at(_state, arguments, lang)
local row, column = arguments[1], arguments[2]
local nsid = request(exec_lua, 'return require("rainbow-delimiters.lib").nsids[...]', {lang or 'lua'})
local extmarks = request(exec_lua, 'return vim.inspect_pos(...).extmarks', {0, row, column})
filter(extmarks, function(_, v) return v.ns_id == nsid end)
return #extmarks > 0
end
say:set('assertion.extmarks_at.positive', 'Expected extmarks at (%s, %s)')
say:set('assertion.extmarks_at.negative', 'Expected no extmarks at (%s, %s)')
assert:register('assertion', 'extmarks_at', has_extmarks_at, 'assertion.extmarks_at.positive', 'assertion.extmarks_at.negative')
before_each(function()
nvim = test_utils.start_embedded()
request(exec_lua, 'TSEnsure(...)', {'lua', 'vim'})
@ -56,10 +39,10 @@ describe('The local strategy', function()
request(buf_set_lines, 0, 0, -1, true, {'print({{{{{}}}}})', '-- vim:ft=lua'})
request('nvim_win_set_cursor', 0, {1, 5})
request(buf_set_option, 0, 'filetype', 'lua')
assert.has_extmarks_at(0, 5)
assert.nvim(nvim).has_extmarks_at(0, 5, 'lua')
request(call_function, 'rainbow_delimiters#disable', {0})
assert.Not.has_extmarks_at(0, 5)
assert.nvim(nvim).Not.has_extmarks_at(0, 5, 'lua')
-- Add a new pair of curly braces
-- (jump to first column, find the first closing brace, insert new pair)
@ -67,7 +50,7 @@ describe('The local strategy', function()
request(feedkeys, keys, 'n', false)
assert.is.same({'print({{{{{{}}}}}})'}, request('nvim_buf_get_lines', 0, 0, 1, true))
assert.Not.has_extmarks_at(0, 5)
assert.nvim(nvim).Not.has_extmarks_at(0, 5, 'lua')
assert.is.equal(0, request(exec_lua, 'return the_strategy.attachments[1]', {}))
end)
end)

View file

@ -1,9 +1,6 @@
local say = require 'say'
local rpcrequest = vim.rpcrequest
local test_utils = require 'testing.utils'
local filter = vim.fn.filter
local call_function = 'nvim_call_function'
local exec_lua = 'nvim_exec_lua'
local buf_set_lines = 'nvim_buf_set_lines'
@ -16,20 +13,6 @@ describe('We can use functions to turn rainbow delimiters off and on again.', fu
return rpcrequest(nvim, method, ...)
end
---Asserts that there are Rainbow Delimiters extmarks at the given position
---@param arguments integer[] Row and column, both zero-based
local function has_extmarks_at(_state, arguments)
local row, column = arguments[1], arguments[2]
local nsid = request(exec_lua, 'return require("rainbow-delimiters.lib").nsids.lua', {})
local extmarks = request(exec_lua, 'return vim.inspect_pos(...).extmarks', {0, row, column})
filter(extmarks, function(_, v) return v.ns_id == nsid end)
return #extmarks > 0
end
say:set('assertion.extmarks_at.positive', 'Expected extmarks at (%s, %s)')
say:set('assertion.extmarks_at.negative', 'Expected no extmarks at (%s, %s)')
assert:register('assertion', 'extmarks_at', has_extmarks_at, 'assertion.extmarks_at.positive', 'assertion.extmarks_at.negative')
before_each(function()
nvim = test_utils.start_embedded()
request(exec_lua, 'the_strategy = require("rainbow-delimiters.strategy.global")', {})
@ -43,26 +26,26 @@ describe('We can use functions to turn rainbow delimiters off and on again.', fu
end)
it('Does highlighting initially', function()
assert.has_extmarks_at(0, 5)
assert.nvim(nvim).has_extmarks_at(0, 5, 'lua')
end)
it('Disables rainbow delimiters', function()
request(call_function, 'rainbow_delimiters#disable', {0})
assert.Not.has_extmarks_at(0, 5)
assert.nvim(nvim).Not.has_extmarks_at(0, 5, 'lua')
end)
it('Remains disabled when disabling twice', function()
request(call_function, 'rainbow_delimiters#disable', {0})
request(call_function, 'rainbow_delimiters#disable', {0})
assert.Not.has_extmarks_at(0, 5)
assert.nvim(nvim).Not.has_extmarks_at(0, 5, 'lua')
end)
it('Turns rainbow delimiters back on', function()
request(call_function, 'rainbow_delimiters#disable', {0})
request(call_function, 'rainbow_delimiters#enable', {0})
assert.has_extmarks_at(0, 5)
assert.nvim(nvim).has_extmarks_at(0, 5, 'lua')
end)
it('Remains enabled when enabling twice', function()
@ -70,7 +53,7 @@ describe('We can use functions to turn rainbow delimiters off and on again.', fu
request(call_function, 'rainbow_delimiters#enable', {0})
request(call_function, 'rainbow_delimiters#enable', {0})
assert.has_extmarks_at(0, 5)
assert.nvim(nvim).has_extmarks_at(0, 5, 'lua')
end)
it('Can be disabled after being enabled', function()
@ -78,7 +61,7 @@ describe('We can use functions to turn rainbow delimiters off and on again.', fu
request(call_function, 'rainbow_delimiters#enable', {0})
request(call_function, 'rainbow_delimiters#disable', {0})
assert.Not.has_extmarks_at(0, 5)
assert.nvim(nvim).Not.has_extmarks_at(0, 5, 'lua')
end)
it('Can be enabled after being disabled twice', function()
@ -86,6 +69,6 @@ describe('We can use functions to turn rainbow delimiters off and on again.', fu
request(call_function, 'rainbow_delimiters#disable', {0})
request(call_function, 'rainbow_delimiters#enable', {0})
assert.has_extmarks_at(0, 5)
assert.nvim(nvim).has_extmarks_at(0, 5, 'lua')
end)
end)

View file

@ -0,0 +1,36 @@
-- Custom configuration for Busted
local say = require 'say'
local assert = require 'luassert'
local filter = vim.fn.filter
local rpcrequest = vim.rpcrequest
local NVIM_STATE_KEY = {}
local function nvim(state, args, level)
assert(args.n > 0, "No Neovim channel provided to the modifier")
assert(rawget(state, NVIM_STATE_KEY) == nil, "Neovim already set")
rawset(state, NVIM_STATE_KEY, args[1])
return state
end
---Asserts that there are Rainbow Delimiters extmarks at the given position
---@param arguments integer[] Row and column, both zero-based
local function has_extmarks_at(_state, arguments, lang)
local nvim = rawget(_state, NVIM_STATE_KEY)
assert(nvim ~= nil, 'No Neovim channel set, use the nvim modifier to set the channel')
local row, column = arguments[1], arguments[2]
local nsid = rpcrequest(nvim, 'nvim_exec_lua', 'return require("rainbow-delimiters.lib").nsids[...]', {lang})
local extmarks = rpcrequest(nvim, 'nvim_exec_lua', 'return vim.inspect_pos(...).extmarks', {0, row, column})
filter(extmarks, function(_, v) return v.ns_id == nsid end)
return #extmarks > 0
end
say:set('assertion.extmarks_at.positive', 'Expected extmarks at (%s, %s)')
say:set('assertion.extmarks_at.negative', 'Expected no extmarks at (%s, %s)')
assert:register(
'assertion', 'has_extmarks_at', has_extmarks_at,
'assertion.has_extmarks_at.positive', 'assertion.has_extmarks_at.negative')
assert:register('modifier', 'nvim', nvim)