197 lines
5.9 KiB
Lua
197 lines
5.9 KiB
Lua
local T
|
|
local expect = MiniTest.expect
|
|
local child = MiniTest.new_child_neovim()
|
|
|
|
local source = debug.getinfo(1, 'S').source
|
|
local root = vim.fn.fnamemodify(source:sub(2), ':p:h:h')
|
|
|
|
local function write_config(lines)
|
|
child.lua_func(function(config_lines)
|
|
local path = vim.env.HERDR_SPLITS_CONFIG
|
|
vim.fn.mkdir(vim.fn.fnamemodify(path, ':h'), 'p')
|
|
vim.fn.writefile(config_lines, path)
|
|
end, lines)
|
|
end
|
|
|
|
T = MiniTest.new_set({
|
|
hooks = {
|
|
pre_case = function()
|
|
child.restart({ '--noplugin', '-u', 'NONE', '-i', 'NONE' })
|
|
child.cmd('set runtimepath^=' .. vim.fn.fnameescape(root))
|
|
child.lua([[
|
|
local test_dir = vim.fn.tempname()
|
|
vim.fn.mkdir(test_dir, 'p')
|
|
vim.g.herdr_splits_test_dir = test_dir
|
|
vim.env.HOME = test_dir .. '/home'
|
|
vim.env.XDG_CONFIG_HOME = test_dir .. '/xdg'
|
|
vim.env.HERDR_SPLITS_CONFIG = test_dir .. '/nested/herdr-splits.conf'
|
|
vim.env.HERDR_BIN_PATH = test_dir .. '/missing-herdr'
|
|
vim.env.HERDR_PLUGIN_CONFIG_DIR = nil
|
|
]])
|
|
end,
|
|
post_case = function()
|
|
local test_dir = child.g.herdr_splits_test_dir
|
|
child.stop()
|
|
vim.fn.delete(test_dir, 'rf')
|
|
end,
|
|
},
|
|
})
|
|
|
|
T['to_herdr translates key notation'] = function()
|
|
local translate = function(value)
|
|
return child.lua_func(function(key)
|
|
return require('herdr-splits.conf').to_herdr(key)
|
|
end, value)
|
|
end
|
|
|
|
expect.equality(translate('<C-M-Left>'), 'ctrl+alt+left')
|
|
expect.equality(translate('<A-Return>'), 'alt+enter')
|
|
expect.equality(translate('<S-Space>'), 'shift+space')
|
|
expect.equality(translate('H'), 'h')
|
|
expect.equality(translate('<Escape>'), 'esc')
|
|
expect.equality(translate(''), vim.NIL)
|
|
expect.equality(translate(false), vim.NIL)
|
|
expect.equality(translate('<C>'), vim.NIL)
|
|
end
|
|
|
|
T['read_managed handles a missing file'] = function()
|
|
local result = child.lua_func(function()
|
|
local values, adopted = require('herdr-splits.conf').read_managed()
|
|
return { adopted = adopted, count = vim.tbl_count(values) }
|
|
end)
|
|
|
|
expect.equality(result, { adopted = false, count = 0 })
|
|
end
|
|
|
|
T['read_managed adopts managed legacy values with last value winning'] = function()
|
|
write_config({
|
|
'# legacy config',
|
|
' nav_key_left = ctrl+old',
|
|
'nav_key_left=ctrl+new # last value wins',
|
|
'resize_key_up = alt+up',
|
|
'unzoom_on_nav=false',
|
|
'nav_at_edge=stop',
|
|
'unknown_key=ignored',
|
|
})
|
|
|
|
local result = child.lua_func(function()
|
|
local values, adopted = require('herdr-splits.conf').read_managed()
|
|
return {
|
|
adopted = adopted,
|
|
nav_left = values.nav_keys.left,
|
|
resize_up = values.resize_keys.up,
|
|
unzoom_on_nav = values.unzoom_on_nav,
|
|
nav_at_edge = values.nav_at_edge,
|
|
unknown = values.unknown_key,
|
|
}
|
|
end)
|
|
|
|
expect.equality(result, {
|
|
adopted = true,
|
|
nav_left = 'ctrl+new',
|
|
resize_up = 'alt+up',
|
|
unzoom_on_nav = false,
|
|
nav_at_edge = 'stop',
|
|
})
|
|
end
|
|
|
|
T['write creates generated content without leftover temporary files'] = function()
|
|
local result = child.lua_func(function()
|
|
local conf = require('herdr-splits.conf')
|
|
local ok, err = conf.write({
|
|
nav_keys = { left = 'ctrl+h', down = 'ctrl+j', up = 'ctrl+k', right = 'ctrl+l' },
|
|
resize_keys = { left = 'alt+h', down = 'alt+j', up = 'alt+k', right = 'alt+l' },
|
|
unzoom_on_nav = false,
|
|
nav_at_edge = 'stop',
|
|
})
|
|
local values, adopted = conf.read_managed()
|
|
return {
|
|
ok = ok,
|
|
err = err,
|
|
adopted = adopted,
|
|
adopted_count = vim.tbl_count(values),
|
|
}
|
|
end)
|
|
|
|
expect.equality(result, { ok = true, adopted = false, adopted_count = 0 })
|
|
|
|
local content = child.lua_func(function()
|
|
local file = assert(io.open(vim.env.HERDR_SPLITS_CONFIG, 'r'))
|
|
local bytes = file:read('*a')
|
|
file:close()
|
|
return bytes
|
|
end)
|
|
expect.equality(content, table.concat({
|
|
'# herdr-splits-generated-v1',
|
|
'# herdr-splits plugin config — GENERATED by herdr-splits.nvim setup().',
|
|
'# Regenerated from defaults + setup() opts on every setup(); do not edit by hand.',
|
|
'# A headerless/legacy conf is adopted once on migration (see the startup',
|
|
'# notice), then overwritten thereafter — remove an opt in setup() to revert.',
|
|
'nav_key_left=ctrl+h',
|
|
'nav_key_down=ctrl+j',
|
|
'nav_key_up=ctrl+k',
|
|
'nav_key_right=ctrl+l',
|
|
'resize_key_left=alt+h',
|
|
'resize_key_down=alt+j',
|
|
'resize_key_up=alt+k',
|
|
'resize_key_right=alt+l',
|
|
'unzoom_on_nav=false',
|
|
'nav_at_edge=stop',
|
|
'',
|
|
}, '\n'))
|
|
expect.equality(child.lua_get([[vim.fn.glob(vim.env.HERDR_SPLITS_CONFIG .. '.tmp.*', false, true)]]), {})
|
|
end
|
|
|
|
T['config setup applies precedence and resets omitted managed options'] = function()
|
|
write_config({
|
|
'nav_key_left=ctrl+legacy',
|
|
'resize_key_left=alt+legacy',
|
|
'unzoom_on_nav=false',
|
|
'nav_at_edge=stop',
|
|
})
|
|
|
|
local result = child.lua_func(function()
|
|
vim.notify = function() end
|
|
local config = require('herdr-splits.config')
|
|
|
|
config.setup({
|
|
nav_keys = { left = '<C-x>' },
|
|
unzoom_on_nav = true,
|
|
})
|
|
local first = {
|
|
nav_left = config.nav_keys.left,
|
|
resize_left = config.resize_keys.left,
|
|
unzoom_on_nav = config.unzoom_on_nav,
|
|
nav_at_edge = config.nav_at_edge,
|
|
}
|
|
|
|
config.setup({ nav_keys = { right = '<C-r>' } })
|
|
local second = {
|
|
nav_left = config.nav_keys.left,
|
|
nav_right = config.nav_keys.right,
|
|
resize_left = config.resize_keys.left,
|
|
unzoom_on_nav = config.unzoom_on_nav,
|
|
nav_at_edge = config.nav_at_edge,
|
|
}
|
|
return { first = first, second = second }
|
|
end)
|
|
|
|
expect.equality(result, {
|
|
first = {
|
|
nav_left = 'ctrl+x',
|
|
resize_left = 'alt+legacy',
|
|
unzoom_on_nav = true,
|
|
nav_at_edge = 'stop',
|
|
},
|
|
second = {
|
|
nav_left = 'ctrl+h',
|
|
nav_right = 'ctrl+r',
|
|
resize_left = 'alt+h',
|
|
unzoom_on_nav = true,
|
|
nav_at_edge = 'wrap',
|
|
},
|
|
})
|
|
end
|
|
|
|
return T
|