Regenerate nvim config
This commit is contained in:
@ -0,0 +1,99 @@
|
||||
-- Copyright (c) 2020-2021 shadmansaleh
|
||||
-- MIT license, see LICENSE for more details.
|
||||
|
||||
assert = require('luassert')
|
||||
local eq = assert.are.same
|
||||
|
||||
local M = {}
|
||||
|
||||
M.meths = setmetatable({}, {
|
||||
__index = function(_, key)
|
||||
return vim.api['nvim_' .. key]
|
||||
end,
|
||||
})
|
||||
|
||||
function M.init_component(component, opts)
|
||||
if component == nil then
|
||||
component = 'special.function_component'
|
||||
else
|
||||
opts.component_name = component
|
||||
end
|
||||
local comp = require('lualine.components.' .. component)
|
||||
if type(comp) == 'table' then
|
||||
comp = comp(opts)
|
||||
elseif type(comp) == 'function' then
|
||||
opts[1] = comp
|
||||
comp = require('lualine.components.special.function_component')(opts)
|
||||
end
|
||||
return comp
|
||||
end
|
||||
|
||||
-- Checks output of a component
|
||||
M.assert_component = function(component, opts, result, is_active)
|
||||
local comp = M.init_component(component, opts)
|
||||
-- for testing global options
|
||||
eq(result, comp:draw(opts.hl, is_active or true))
|
||||
end
|
||||
|
||||
function M.assert_component_instance(comp, result)
|
||||
eq(result, comp:draw(comp.options.hl))
|
||||
end
|
||||
-- sets defaults for component options
|
||||
M.build_component_opts = function(opts)
|
||||
if not opts then
|
||||
opts = {}
|
||||
end
|
||||
if opts[1] == nil then
|
||||
opts[1] = function()
|
||||
return 'test'
|
||||
end
|
||||
end
|
||||
if not opts.self then
|
||||
opts.self = { section = 'c' }
|
||||
end
|
||||
if not opts.theme then
|
||||
opts.theme = 'gruvbox'
|
||||
end
|
||||
if not opts.hl then
|
||||
opts.hl = ''
|
||||
end
|
||||
if opts.icons_enabled == nil then
|
||||
opts.icons_enabled = true
|
||||
end
|
||||
if not opts.component_separators then
|
||||
opts.component_separators = { left = '', right = '' }
|
||||
end
|
||||
if not opts.section_separators then
|
||||
opts.section_separators = { left = '', right = '' }
|
||||
end
|
||||
return opts
|
||||
end
|
||||
|
||||
M.P = function(t)
|
||||
print(vim.inspect(t))
|
||||
end
|
||||
|
||||
function M.dedent(str, leave_indent)
|
||||
-- find minimum common indent across lines
|
||||
local indent = nil
|
||||
for line in str:gmatch('[^\n]+') do
|
||||
local line_indent = line:match('^%s+') or ''
|
||||
if indent == nil or #line_indent < #indent then
|
||||
indent = line_indent
|
||||
end
|
||||
end
|
||||
if indent == nil or #indent == 0 then
|
||||
-- no minimum common indent
|
||||
return str
|
||||
end
|
||||
local left_indent = (' '):rep(leave_indent or 0)
|
||||
-- create a pattern for the indent
|
||||
indent = indent:gsub('%s', '[ \t]')
|
||||
-- strip it from the first line
|
||||
str = str:gsub('^' .. indent, left_indent)
|
||||
-- strip it from the remaining lines
|
||||
str = str:gsub('[\n]' .. indent, '\n' .. left_indent)
|
||||
return str
|
||||
end
|
||||
|
||||
return M
|
||||
@ -0,0 +1,14 @@
|
||||
-- Copyright (c) 2020-2021 shadmansaleh
|
||||
-- MIT license, see LICENSE for more details.
|
||||
|
||||
if os.getenv('TEST_COV') then
|
||||
require('luacov')
|
||||
end
|
||||
-- load lualine and plenary
|
||||
vim.cmd([[
|
||||
set noswapfile
|
||||
set rtp+=.
|
||||
set rtp+=../plenary.nvim
|
||||
set rtp+=../nvim-web-devicons/
|
||||
runtime plugin/plenary.vim
|
||||
]])
|
||||
@ -0,0 +1,843 @@
|
||||
-- Copyright (c) 2020-2021 shadmansaleh
|
||||
-- MIT license, see LICENSE for more details.
|
||||
|
||||
local helpers = require('tests.helpers')
|
||||
|
||||
local eq = assert.are.same
|
||||
local neq = assert.are_not.same
|
||||
local assert_component = helpers.assert_component
|
||||
local build_component_opts = helpers.build_component_opts
|
||||
local stub = require('luassert.stub')
|
||||
|
||||
describe('Component:', function()
|
||||
it('can select separators', function()
|
||||
local opts = build_component_opts()
|
||||
local comp = require('lualine.components.special.function_component')(opts)
|
||||
-- correct for lualine_c
|
||||
eq('', comp.options.separator)
|
||||
local opts2 = build_component_opts { self = { section = 'y' } }
|
||||
local comp2 = require('lualine.components.special.function_component')(opts2)
|
||||
-- correct for lualine_u
|
||||
eq('', comp2.options.separator)
|
||||
end)
|
||||
|
||||
it('can provide unique identifier', function()
|
||||
local opts1 = build_component_opts()
|
||||
local comp1 = require('lualine.components.special.function_component')(opts1)
|
||||
local opts2 = build_component_opts()
|
||||
local comp2 = require('lualine.components.special.function_component')(opts2)
|
||||
neq(comp1.component_no, comp2.component_no)
|
||||
end)
|
||||
|
||||
it('create option highlights', function()
|
||||
local color = { fg = '#224532', bg = '#892345' }
|
||||
local opts1 = build_component_opts { color = color }
|
||||
local hl = require('lualine.highlight')
|
||||
stub(hl, 'create_component_highlight_group')
|
||||
hl.create_component_highlight_group.returns('MyCompHl')
|
||||
local comp1 = require('lualine.components.special.function_component')(opts1)
|
||||
eq('MyCompHl', comp1.options.color_highlight)
|
||||
-- color highlight wan't in options when create_comp_hl was
|
||||
-- called so remove it before assert
|
||||
comp1.options.color_highlight = nil
|
||||
assert.stub(hl.create_component_highlight_group).was_called_with(
|
||||
color,
|
||||
comp1.options.component_name,
|
||||
comp1.options,
|
||||
false
|
||||
)
|
||||
hl.create_component_highlight_group:revert()
|
||||
color = 'MyHl'
|
||||
local opts2 = build_component_opts { color = color }
|
||||
stub(hl, 'create_component_highlight_group')
|
||||
hl.create_component_highlight_group.returns('MyCompLinkedHl')
|
||||
local comp2 = require('lualine.components.special.function_component')(opts2)
|
||||
eq('MyCompLinkedHl', comp2.options.color_highlight)
|
||||
-- color highlight wan't in options when create_comp_hl was
|
||||
-- called so remove it before assert
|
||||
comp2.options.color_highlight = nil
|
||||
assert.stub(hl.create_component_highlight_group).was_called_with(
|
||||
color,
|
||||
comp2.options.component_name,
|
||||
comp2.options,
|
||||
false
|
||||
)
|
||||
hl.create_component_highlight_group:revert()
|
||||
end)
|
||||
|
||||
it('can draw', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
}
|
||||
assert_component(nil, opts, 'test')
|
||||
end)
|
||||
|
||||
it('can apply separators', function()
|
||||
local opts = build_component_opts { padding = 0 }
|
||||
assert_component(nil, opts, 'test')
|
||||
end)
|
||||
|
||||
it('can apply default highlight', function()
|
||||
local opts = build_component_opts { padding = 0, hl = '%#My_highlight#' }
|
||||
assert_component(nil, opts, '%#My_highlight#test')
|
||||
opts = build_component_opts {
|
||||
function()
|
||||
return '%#Custom_hl#test'
|
||||
end,
|
||||
padding = 0,
|
||||
hl = '%#My_highlight#',
|
||||
}
|
||||
assert_component(nil, opts, '%#Custom_hl#test%#My_highlight#')
|
||||
opts = build_component_opts {
|
||||
function()
|
||||
return 'in middle%#Custom_hl#test'
|
||||
end,
|
||||
padding = 0,
|
||||
hl = '%#My_highlight#',
|
||||
}
|
||||
assert_component(nil, opts, '%#My_highlight#in middle%#Custom_hl#test%#My_highlight#')
|
||||
end)
|
||||
|
||||
describe('Global options:', function()
|
||||
it('left_padding', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = { left = 5 },
|
||||
}
|
||||
assert_component(nil, opts, ' test')
|
||||
end)
|
||||
|
||||
it('right_padding', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = { right = 5 },
|
||||
}
|
||||
assert_component(nil, opts, 'test ')
|
||||
end)
|
||||
|
||||
it('padding', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 5,
|
||||
}
|
||||
assert_component(nil, opts, ' test ')
|
||||
end)
|
||||
|
||||
it('icon', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
icon = '0',
|
||||
}
|
||||
assert_component(nil, opts, '0 test')
|
||||
end)
|
||||
|
||||
it('icons_enabled', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
icons_enabled = true,
|
||||
icon = '0',
|
||||
}
|
||||
assert_component(nil, opts, '0 test')
|
||||
local opts2 = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
icons_enabled = false,
|
||||
icon = '0',
|
||||
}
|
||||
assert_component(nil, opts2, 'test')
|
||||
end)
|
||||
|
||||
it('separator', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
separator = '|',
|
||||
}
|
||||
assert_component(nil, opts, 'test|')
|
||||
end)
|
||||
|
||||
it('fmt', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
fmt = function(data)
|
||||
return data:sub(1, 1):upper() .. data:sub(2, #data)
|
||||
end,
|
||||
}
|
||||
assert_component(nil, opts, 'Test')
|
||||
end)
|
||||
|
||||
it('cond', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
cond = function()
|
||||
return true
|
||||
end,
|
||||
}
|
||||
assert_component(nil, opts, 'test')
|
||||
local opts2 = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
cond = function()
|
||||
return false
|
||||
end,
|
||||
}
|
||||
assert_component(nil, opts2, '')
|
||||
end)
|
||||
|
||||
it('color', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
color = 'MyHl',
|
||||
}
|
||||
local comp = require('lualine.components.special.function_component')(opts)
|
||||
local custom_link_hl_name = 'lualine_c_' .. comp.options.component_name
|
||||
eq('%#' .. custom_link_hl_name .. '#test', comp:draw(opts.hl))
|
||||
local opts2 = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
color = { bg = '#230055', fg = '#223344' },
|
||||
}
|
||||
local hl = require('lualine.highlight')
|
||||
stub(hl, 'component_format_highlight')
|
||||
hl.component_format_highlight.returns('%#MyCompHl#')
|
||||
local comp2 = require('lualine.components.special.function_component')(opts2)
|
||||
assert_component(nil, opts2, '%#MyCompHl#test')
|
||||
assert.stub(hl.component_format_highlight).was_called_with(comp2.options.color_highlight)
|
||||
hl.component_format_highlight:revert()
|
||||
end)
|
||||
|
||||
it('draw_empty', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
separator = '>',
|
||||
fmt = function()
|
||||
return ''
|
||||
end,
|
||||
draw_empty = true,
|
||||
}
|
||||
assert_component(nil, opts, '>')
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Encoding component', function()
|
||||
it('works', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
}
|
||||
local tmp_path = 'tmp.txt'
|
||||
local tmp_fp = io.open(tmp_path, 'w')
|
||||
tmp_fp:write('test file')
|
||||
tmp_fp:close()
|
||||
vim.cmd('e ' .. tmp_path)
|
||||
assert_component('encoding', opts, 'utf-8')
|
||||
vim.cmd('bd!')
|
||||
os.remove(tmp_path)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Fileformat component', function()
|
||||
it('works with icons', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
}
|
||||
local fmt = vim.bo.fileformat
|
||||
vim.bo.fileformat = 'unix'
|
||||
assert_component('fileformat', opts, '')
|
||||
vim.bo.fileformat = 'dos'
|
||||
assert_component('fileformat', opts, '')
|
||||
vim.bo.fileformat = 'mac'
|
||||
assert_component('fileformat', opts, '')
|
||||
vim.bo.fileformat = fmt
|
||||
end)
|
||||
it('works without icons', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
icons_enabled = false,
|
||||
}
|
||||
assert_component('fileformat', opts, vim.bo.fileformat)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Filetype component', function()
|
||||
local filetype
|
||||
|
||||
before_each(function()
|
||||
filetype = vim.bo.filetype
|
||||
vim.bo.filetype = 'lua'
|
||||
end)
|
||||
|
||||
after_each(function()
|
||||
vim.bo.filetype = filetype
|
||||
end)
|
||||
|
||||
it('does not add icon when library unavailable', function()
|
||||
local old_require = _G.require
|
||||
function _G.require(...)
|
||||
if select(1, ...) == 'nvim-web-devicons' then
|
||||
error('Test case not suppose to have web-dev-icon 👀')
|
||||
end
|
||||
return old_require(...)
|
||||
end
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
}
|
||||
assert_component('filetype', opts, 'lua')
|
||||
_G.require = old_require
|
||||
end)
|
||||
|
||||
it('colors nvim-web-devicons icons', function()
|
||||
vim.g.actual_curwin = tostring(vim.api.nvim_get_current_win())
|
||||
stub(vim.fn, 'expand')
|
||||
vim.fn.expand.on_call_with('%:t').returns('test.lua')
|
||||
|
||||
local hl = require('lualine.highlight')
|
||||
stub(hl, 'create_component_highlight_group')
|
||||
hl.create_component_highlight_group.returns { name = 'MyCompHl', no_mode = false, section = 'a' }
|
||||
stub(hl, 'format_highlight')
|
||||
hl.format_highlight.returns('%#lualine_c_normal#')
|
||||
|
||||
local utils = require('lualine.utils.utils')
|
||||
stub(utils, 'extract_highlight_colors')
|
||||
utils.extract_highlight_colors.returns('#000')
|
||||
|
||||
local devicons = require('nvim-web-devicons')
|
||||
stub(devicons, 'get_icon')
|
||||
devicons.get_icon.on_call_with('test.lua').returns('*', 'test_highlight_group')
|
||||
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
hl = '%#lualine_c_normal#',
|
||||
padding = 0,
|
||||
colored = true,
|
||||
icon_only = false,
|
||||
}
|
||||
assert_component('filetype', opts, '%#MyCompHl_normal#* %#lualine_c_normal#lua%#lualine_c_normal#')
|
||||
assert.stub(devicons.get_icon).was_called_with('test.lua')
|
||||
assert.stub(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg')
|
||||
assert.stub(hl.create_component_highlight_group).was_called_with(
|
||||
{ fg = '#000' },
|
||||
'filetype_test_highlight_group',
|
||||
opts,
|
||||
false
|
||||
)
|
||||
assert.stub(vim.fn.expand).was_called_with('%:t')
|
||||
|
||||
devicons.get_icon:revert()
|
||||
utils.extract_highlight_colors:revert()
|
||||
hl.create_component_highlight_group:revert()
|
||||
hl.format_highlight:revert()
|
||||
vim.fn.expand:revert()
|
||||
vim.g.actual_curwin = nil
|
||||
end)
|
||||
|
||||
it("doesn't color when colored is false", function()
|
||||
stub(vim.fn, 'expand')
|
||||
vim.fn.expand.on_call_with('%:t').returns('test.lua')
|
||||
|
||||
local hl = require('lualine.highlight')
|
||||
stub(hl, 'create_component_highlight_group')
|
||||
|
||||
local utils = require('lualine.utils.utils')
|
||||
stub(utils, 'extract_highlight_colors')
|
||||
|
||||
local devicons = require('nvim-web-devicons')
|
||||
stub(devicons, 'get_icon')
|
||||
devicons.get_icon.on_call_with('test.lua').returns('*', 'test_highlight_group')
|
||||
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
colored = false,
|
||||
}
|
||||
assert_component('filetype', opts, '* lua')
|
||||
assert.stub(devicons.get_icon).was_called_with('test.lua')
|
||||
assert.stub(utils.extract_highlight_colors).was_not_called()
|
||||
assert.stub(hl.create_component_highlight_group).was_not_called()
|
||||
assert.stub(vim.fn.expand).was_called_with('%:t')
|
||||
|
||||
devicons.get_icon:revert()
|
||||
utils.extract_highlight_colors:revert()
|
||||
hl.create_component_highlight_group:revert()
|
||||
vim.fn.expand:revert()
|
||||
end)
|
||||
|
||||
it('displays only icon when icon_only is true', function()
|
||||
stub(vim.fn, 'expand')
|
||||
vim.fn.expand.on_call_with('%:t').returns('test.lua')
|
||||
|
||||
local hl = require('lualine.highlight')
|
||||
stub(hl, 'create_component_highlight_group')
|
||||
|
||||
local utils = require('lualine.utils.utils')
|
||||
stub(utils, 'extract_highlight_colors')
|
||||
|
||||
local devicons = require('nvim-web-devicons')
|
||||
stub(devicons, 'get_icon')
|
||||
devicons.get_icon.on_call_with('test.lua').returns('*', 'test_highlight_group')
|
||||
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
colored = false,
|
||||
icon_only = true,
|
||||
}
|
||||
assert_component('filetype', opts, '* ')
|
||||
assert.stub(devicons.get_icon).was_called_with('test.lua')
|
||||
assert.stub(utils.extract_highlight_colors).was_not_called()
|
||||
assert.stub(hl.create_component_highlight_group).was_not_called()
|
||||
assert.stub(vim.fn.expand).was_called_with('%:t')
|
||||
|
||||
devicons.get_icon:revert()
|
||||
utils.extract_highlight_colors:revert()
|
||||
hl.create_component_highlight_group:revert()
|
||||
vim.fn.expand:revert()
|
||||
end)
|
||||
|
||||
it('displays right aligned icon when icon.align is "right"', function()
|
||||
stub(vim.fn, 'expand')
|
||||
vim.fn.expand.on_call_with('%:t').returns('test.lua')
|
||||
|
||||
local hl = require('lualine.highlight')
|
||||
stub(hl, 'create_component_highlight_group')
|
||||
|
||||
local utils = require('lualine.utils.utils')
|
||||
stub(utils, 'extract_highlight_colors')
|
||||
|
||||
local devicons = require('nvim-web-devicons')
|
||||
stub(devicons, 'get_icon')
|
||||
devicons.get_icon.on_call_with('test.lua').returns('*', 'test_highlight_group')
|
||||
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
colored = false,
|
||||
icon_only = false,
|
||||
icon = { align = 'right' }
|
||||
}
|
||||
assert_component('filetype', opts, 'lua * ')
|
||||
assert.stub(devicons.get_icon).was_called_with('test.lua')
|
||||
assert.stub(utils.extract_highlight_colors).was_not_called()
|
||||
assert.stub(hl.create_component_highlight_group).was_not_called()
|
||||
assert.stub(vim.fn.expand).was_called_with('%:t')
|
||||
|
||||
devicons.get_icon:revert()
|
||||
utils.extract_highlight_colors:revert()
|
||||
hl.create_component_highlight_group:revert()
|
||||
vim.fn.expand:revert()
|
||||
end)
|
||||
|
||||
it('uses filetype lookup when file has no extension', function()
|
||||
stub(vim.fn, 'expand')
|
||||
vim.fn.expand.on_call_with('%:t').returns('test')
|
||||
|
||||
local hl = require('lualine.highlight')
|
||||
stub(hl, 'create_component_highlight_group')
|
||||
|
||||
local utils = require('lualine.utils.utils')
|
||||
stub(utils, 'extract_highlight_colors')
|
||||
|
||||
local devicons = require('nvim-web-devicons')
|
||||
stub(devicons, 'get_icon')
|
||||
devicons.get_icon.on_call_with('test').returns(nil)
|
||||
stub(devicons, 'get_icon_by_filetype')
|
||||
devicons.get_icon_by_filetype.on_call_with('lua').returns('*', 'test_highlight_group')
|
||||
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
colored = false,
|
||||
icon_only = false,
|
||||
}
|
||||
assert_component('filetype', opts, '* lua')
|
||||
assert.stub(devicons.get_icon).was_called_with('test')
|
||||
assert.stub(devicons.get_icon_by_filetype).was_called_with('lua')
|
||||
assert.stub(utils.extract_highlight_colors).was_not_called()
|
||||
assert.stub(hl.create_component_highlight_group).was_not_called()
|
||||
assert.stub(vim.fn.expand).was_called_with('%:t')
|
||||
|
||||
devicons.get_icon_by_filetype:revert()
|
||||
devicons.get_icon:revert()
|
||||
utils.extract_highlight_colors:revert()
|
||||
hl.create_component_highlight_group:revert()
|
||||
vim.fn.expand:revert()
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Hostname component', function()
|
||||
it('works', function()
|
||||
stub(vim.loop, 'os_gethostname')
|
||||
vim.loop.os_gethostname.returns('localhost')
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
}
|
||||
assert_component('hostname', opts, 'localhost')
|
||||
vim.loop.os_gethostname:revert()
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Location component', function()
|
||||
it('works', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
}
|
||||
assert_component('location', opts, ' 1:1 ')
|
||||
vim.cmd('normal! 9o')
|
||||
assert_component('location', opts, ' 10:1 ')
|
||||
vim.api.nvim_win_set_cursor(0, {5, 0})
|
||||
assert_component('location', opts, ' 5:1 ')
|
||||
-- test column number
|
||||
vim.cmd('normal! oTest')
|
||||
assert_component('location', opts, ' 6:4 ')
|
||||
-- test column number in line containing cyrillic symbols
|
||||
vim.cmd('normal! oТест')
|
||||
assert_component('location', opts, ' 7:4 ')
|
||||
vim.cmd('bdelete!')
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Progress component', function()
|
||||
it('works', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
}
|
||||
assert_component('progress', opts, 'Top')
|
||||
vim.cmd('normal! 9o')
|
||||
assert_component('progress', opts, 'Bot')
|
||||
vim.api.nvim_win_set_cursor(0, {5, 0})
|
||||
assert_component('progress', opts, '50%%')
|
||||
vim.cmd('bdelete!')
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Mode component', function()
|
||||
it('works', function()
|
||||
stub(vim.api, 'nvim_get_mode')
|
||||
vim.api.nvim_get_mode.returns { mode = 'n', blocking = false }
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
}
|
||||
assert_component('mode', opts, 'NORMAL')
|
||||
vim.api.nvim_get_mode:revert()
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('FileSize component', function()
|
||||
it('works', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
}
|
||||
local fname = 'test-file.txt'
|
||||
local f = io.open(fname, 'w')
|
||||
f:write(string.rep('........................................\n', 200))
|
||||
f:close()
|
||||
vim.cmd(':edit ' .. fname)
|
||||
assert_component('filesize', opts, '8.0k')
|
||||
vim.cmd(':bdelete!')
|
||||
os.remove(fname)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Filename component', function()
|
||||
it('works', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
file_status = false,
|
||||
path = 0,
|
||||
}
|
||||
vim.cmd(':e test-file.txt')
|
||||
assert_component('filename', opts, 'test-file.txt')
|
||||
vim.cmd(':bdelete!')
|
||||
end)
|
||||
|
||||
it('can show file_status', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
file_status = true,
|
||||
path = 0,
|
||||
}
|
||||
vim.cmd(':e test-file.txt')
|
||||
vim.bo.modified = false
|
||||
assert_component('filename', opts, 'test-file.txt')
|
||||
vim.bo.modified = true
|
||||
assert_component('filename', opts, 'test-file.txt [+]')
|
||||
vim.bo.ro = true
|
||||
assert_component('filename', opts, 'test-file.txt [+][-]')
|
||||
vim.bo.modified = false
|
||||
assert_component('filename', opts, 'test-file.txt [-]')
|
||||
vim.cmd(':bdelete!')
|
||||
end)
|
||||
|
||||
it('can show new_file_status', function ()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
newfile_status = true,
|
||||
path = 0,
|
||||
}
|
||||
vim.cmd(':e new-file.txt')
|
||||
assert_component('filename', opts, 'new-file.txt [New]')
|
||||
vim.bo.modified = true
|
||||
assert_component('filename', opts, 'new-file.txt [+][New]')
|
||||
vim.cmd(':bdelete!')
|
||||
end)
|
||||
|
||||
it('can show relative path', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
file_status = false,
|
||||
path = 1,
|
||||
}
|
||||
vim.cmd(':e test-file.txt')
|
||||
assert_component('filename', opts, vim.fn.expand('%:~:.'))
|
||||
vim.cmd(':bdelete!')
|
||||
end)
|
||||
|
||||
it('can show full path', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
file_status = false,
|
||||
path = 2,
|
||||
shorting_target = 0,
|
||||
}
|
||||
vim.cmd(':e test-file.txt')
|
||||
assert_component('filename', opts, vim.fn.expand('%:p'))
|
||||
vim.cmd(':bdelete!')
|
||||
end)
|
||||
|
||||
it('shortens path', function()
|
||||
stub(vim.fn, 'expand')
|
||||
vim.fn.expand.on_call_with('%:p').returns('/home/foobar/test/test.lua')
|
||||
stub(vim.fn, 'winwidth')
|
||||
vim.fn.winwidth.on_call_with(0).returns(100)
|
||||
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
file_status = false,
|
||||
path = 2,
|
||||
shorting_target = 90,
|
||||
}
|
||||
vim.cmd(':e test-file.txt')
|
||||
assert_component('filename', opts, '/h/f/t/test.lua')
|
||||
|
||||
vim.cmd(':bdelete!')
|
||||
vim.fn.winwidth:revert()
|
||||
vim.fn.expand:revert()
|
||||
end)
|
||||
|
||||
it('shortens path with tilde', function()
|
||||
stub(vim.fn, 'expand')
|
||||
vim.fn.expand.on_call_with('%:p:~').returns('~/test/test.lua')
|
||||
stub(vim.fn, 'winwidth')
|
||||
vim.fn.winwidth.on_call_with(0).returns(100)
|
||||
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
file_status = false,
|
||||
path = 3,
|
||||
shorting_target = 90,
|
||||
}
|
||||
vim.cmd(':e test-file.txt')
|
||||
assert_component('filename', opts, '~/t/test.lua')
|
||||
|
||||
vim.cmd(':bdelete!')
|
||||
vim.fn.winwidth:revert()
|
||||
vim.fn.expand:revert()
|
||||
end)
|
||||
|
||||
it('shortens path with hidden directory', function()
|
||||
stub(vim.fn, 'expand')
|
||||
vim.fn.expand.on_call_with('%:p').returns('/home/foobar/.test/test.lua')
|
||||
stub(vim.fn, 'winwidth')
|
||||
vim.fn.winwidth.on_call_with(0).returns(100)
|
||||
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
file_status = false,
|
||||
path = 2,
|
||||
shorting_target = 90,
|
||||
}
|
||||
vim.cmd(':e test-file.txt')
|
||||
assert_component('filename', opts, '/h/f/.t/test.lua')
|
||||
|
||||
vim.cmd(':bdelete!')
|
||||
vim.fn.winwidth:revert()
|
||||
vim.fn.expand:revert()
|
||||
end)
|
||||
|
||||
it('shortens path with %', function()
|
||||
stub(vim.fn, 'expand')
|
||||
vim.fn.expand.on_call_with('%:p').returns('%dir1/%dir2/%actual_%file')
|
||||
stub(vim.fn, 'winwidth')
|
||||
vim.fn.winwidth.on_call_with(0).returns(100)
|
||||
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
file_status = false,
|
||||
path = 2,
|
||||
shorting_target = 90,
|
||||
}
|
||||
vim.cmd(':e test-file.txt')
|
||||
assert_component('filename', opts, '%%/%%/%%actual_%%file')
|
||||
|
||||
vim.cmd(':bdelete!')
|
||||
vim.fn.winwidth:revert()
|
||||
vim.fn.expand:revert()
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('vim option & variable component', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
}
|
||||
|
||||
local function assert_vim_var_component(name, options, result)
|
||||
options[1] = name
|
||||
assert_component('special.vim_var_component', options, result)
|
||||
opts[1] = nil
|
||||
end
|
||||
it('works with variable', function()
|
||||
assert_vim_var_component('g:gvar', opts, '')
|
||||
vim.g.gvar = 'var1'
|
||||
assert_vim_var_component('g:gvar', opts, 'var1')
|
||||
vim.g.gvar = 'var2'
|
||||
assert_vim_var_component('g:gvar', opts, 'var2')
|
||||
vim.b.gvar = 'bvar1'
|
||||
assert_vim_var_component('b:gvar', opts, 'bvar1')
|
||||
vim.w.gvar = 'wvar1'
|
||||
assert_vim_var_component('w:gvar', opts, 'wvar1')
|
||||
end)
|
||||
it('can index dictionaries', function()
|
||||
vim.g.gvar = { a = { b = 'var-value' } }
|
||||
assert_vim_var_component('g:gvar.a.b', opts, 'var-value')
|
||||
end)
|
||||
it('works with options', function()
|
||||
local old_number = vim.wo.number
|
||||
vim.wo.number = false
|
||||
assert_vim_var_component('wo:number', opts, 'false')
|
||||
vim.wo.number = old_number
|
||||
local old_tw = vim.go.tw
|
||||
vim.go.tw = 80
|
||||
assert_vim_var_component('go:tw', opts, '80')
|
||||
vim.go.tw = old_tw
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Vim option & variable component', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
}
|
||||
|
||||
local function assert_vim_var_component(name, options, result)
|
||||
options[1] = name
|
||||
assert_component('special.eval_func_component', options, result)
|
||||
opts[1] = nil
|
||||
end
|
||||
|
||||
it('works with vim function', function()
|
||||
vim.cmd([[
|
||||
func! TestFunction() abort
|
||||
return "TestVimFunction"
|
||||
endf
|
||||
]])
|
||||
assert_vim_var_component('TestFunction', opts, 'TestVimFunction')
|
||||
vim.cmd('delfunction TestFunction')
|
||||
end)
|
||||
|
||||
it('works with lua expression', function()
|
||||
_G.TestFunction = function()
|
||||
return 'TestLuaFunction'
|
||||
end
|
||||
assert_vim_var_component('TestFunction()', opts, 'TestLuaFunction')
|
||||
_G.TestFunction = nil
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Branch component', function()
|
||||
-- these tests are broken in wsl will look at them later
|
||||
if vim.fn.has('wsl') == 1 then
|
||||
return
|
||||
end
|
||||
local tmpdir
|
||||
local file
|
||||
local git = function(...)
|
||||
return vim.fn.system(
|
||||
"git -c user.name='asdf' -c user.email='asdf@jlk.org' -C " .. tmpdir .. ' ' .. string.format(...)
|
||||
)
|
||||
end
|
||||
local assert_comp_ins = helpers.assert_component_instance
|
||||
|
||||
before_each(function()
|
||||
tmpdir = os.tmpname()
|
||||
os.remove(tmpdir)
|
||||
file = tmpdir .. '/test.txt'
|
||||
vim.fn.mkdir(tmpdir, 'p')
|
||||
git('init -b test_branch')
|
||||
vim.cmd([[aug lualine
|
||||
au!
|
||||
aug END
|
||||
]])
|
||||
end)
|
||||
|
||||
after_each(function()
|
||||
os.remove(tmpdir)
|
||||
end)
|
||||
|
||||
it('works with regular branches', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
padding = 0,
|
||||
}
|
||||
local branch_comp = helpers.init_component('branch', opts)
|
||||
vim.cmd('e ' .. file)
|
||||
assert_comp_ins(branch_comp, ' test_branch')
|
||||
git('checkout -b test_branch2')
|
||||
vim.cmd('e k')
|
||||
vim.cmd('bd')
|
||||
vim.cmd('e ' .. file)
|
||||
opts.icons_enabled = false
|
||||
assert_comp_ins(branch_comp, 'test_branch2')
|
||||
end)
|
||||
|
||||
it('works in detached head mode', function()
|
||||
local opts = build_component_opts {
|
||||
component_separators = { left = '', right = '' },
|
||||
icons_enabled = false,
|
||||
padding = 0,
|
||||
}
|
||||
git('checkout -b test_branch2')
|
||||
git('commit --allow-empty -m "test commit1"')
|
||||
git('commit --allow-empty -m "test commit2"')
|
||||
git('commit --allow-empty -m "test commit3"')
|
||||
git('checkout HEAD~1')
|
||||
vim.cmd('e ' .. file)
|
||||
local rev = git('rev-parse --short=6 HEAD'):sub(1, 6)
|
||||
assert_component('branch', opts, rev)
|
||||
end)
|
||||
end)
|
||||
@ -0,0 +1,185 @@
|
||||
-- Copyright (c) 2020-2021 shadmansaleh
|
||||
-- MIT license, see LICENSE for more details.
|
||||
|
||||
local eq = assert.are.same
|
||||
|
||||
describe('config parsing', function()
|
||||
local config_module = require('lualine.config')
|
||||
|
||||
describe('options', function()
|
||||
describe('icons_enabled', function()
|
||||
it('default', function()
|
||||
local config = config_module.apply_configuration {}
|
||||
eq(config.options.icons_enabled, true)
|
||||
end)
|
||||
it('custom', function()
|
||||
local config = { options = { icons_enabled = false } }
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.options.icons_enabled, false)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('theme', function()
|
||||
it('default', function()
|
||||
local config = config_module.apply_configuration {}
|
||||
eq(config.options.theme, 'auto')
|
||||
end)
|
||||
it('custom', function()
|
||||
local config = { options = { theme = 'nord' } }
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.options.theme, 'nord')
|
||||
config = { options = { theme = {} } }
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.options.theme, {})
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('separators', function()
|
||||
it('default', function()
|
||||
local config = config_module.apply_configuration {}
|
||||
eq(config.options.component_separators, { left = '', right = '' })
|
||||
eq(config.options.section_separators, { left = '', right = '' })
|
||||
end)
|
||||
it('double separators', function()
|
||||
local config = {
|
||||
options = {
|
||||
component_separators = { left = 'a', right = 'b' },
|
||||
section_separators = { left = 'c', right = 'd' },
|
||||
},
|
||||
}
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.options.component_separators, { left = 'a', right = 'b' })
|
||||
eq(config.options.section_separators, { left = 'c', right = 'd' })
|
||||
end)
|
||||
|
||||
describe('single separator', function()
|
||||
it('string', function()
|
||||
local config = {
|
||||
options = { component_separators = 'a', section_separators = 'b' },
|
||||
}
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.options.component_separators, { left = 'a', right = 'a' })
|
||||
eq(config.options.section_separators, { left = 'b', right = 'b' })
|
||||
end)
|
||||
it('table', function()
|
||||
local config = {
|
||||
options = {
|
||||
component_separators = { left = 'a', right = 'b' },
|
||||
section_separators = { left = 'b', right = 'a' },
|
||||
},
|
||||
}
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.options.component_separators, { left = 'a', right = 'b' })
|
||||
eq(config.options.section_separators, { left = 'b', right = 'a' })
|
||||
end)
|
||||
end)
|
||||
it('no separators', function()
|
||||
local config = {
|
||||
options = { component_separators = {}, section_separators = {} },
|
||||
}
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.options.component_separators, {})
|
||||
eq(config.options.section_separators, {})
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('disabled filetypes', function()
|
||||
it('default', function()
|
||||
local config = config_module.apply_configuration {}
|
||||
eq(config.options.disabled_filetypes, {statusline={}, winbar={}})
|
||||
end)
|
||||
it('custom', function()
|
||||
local config = { options = { disabled_filetypes = { 'lua' } } }
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.options.disabled_filetypes, {statusline={'lua'}, winbar={'lua'}})
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('non default global option', function()
|
||||
it('default', function()
|
||||
local config = { options = {} }
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.options.non_default_global_option, nil)
|
||||
end)
|
||||
it('custom', function()
|
||||
local config = { options = { non_default_global_option = 1 } }
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.options.non_default_global_option, 1)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('sections', function()
|
||||
it('default', function()
|
||||
local config = {}
|
||||
config = config_module.apply_configuration(config)
|
||||
local lualine_default_sections = {
|
||||
lualine_a = { 'mode' },
|
||||
lualine_b = { 'branch', 'diff', 'diagnostics' },
|
||||
lualine_c = { 'filename' },
|
||||
lualine_x = { 'encoding', 'fileformat', 'filetype' },
|
||||
lualine_y = { 'progress' },
|
||||
lualine_z = { 'location' },
|
||||
}
|
||||
eq(config.sections, lualine_default_sections)
|
||||
end)
|
||||
it('custom', function()
|
||||
local custom_sections = {
|
||||
lualine_a = { { 'mode', lower = true } },
|
||||
lualine_b = { 'branch', { 'branch', lower = true } },
|
||||
lualine_c = nil,
|
||||
lualine_x = {},
|
||||
}
|
||||
local expected_sections = {
|
||||
lualine_a = { { 'mode', lower = true } },
|
||||
lualine_b = { 'branch', { 'branch', lower = true } },
|
||||
lualine_c = { 'filename' },
|
||||
lualine_x = {},
|
||||
lualine_y = { 'progress' },
|
||||
lualine_z = { 'location' },
|
||||
}
|
||||
local config = { sections = custom_sections }
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.sections, expected_sections)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('inactive_sections', function() end)
|
||||
|
||||
describe('tabline', function()
|
||||
it('default', function()
|
||||
local config = {}
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.tabline, {})
|
||||
end)
|
||||
it('custom', function()
|
||||
local custom_sections = {
|
||||
lualine_a = { { 'mode', lower = true } },
|
||||
lualine_b = { 'branch', { 'branch', lower = true } },
|
||||
lualine_c = nil,
|
||||
lualine_x = {},
|
||||
}
|
||||
local expected_sections = {
|
||||
lualine_a = { { 'mode', lower = true } },
|
||||
lualine_b = { 'branch', { 'branch', lower = true } },
|
||||
lualine_x = {},
|
||||
}
|
||||
local config = { tabline = custom_sections }
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.tabline, expected_sections)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('extensions', function()
|
||||
it('default', function()
|
||||
local config = { options = {} }
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.extensions, {})
|
||||
end)
|
||||
it('custom', function()
|
||||
local config = { extensions = { 'fugitive' } }
|
||||
config = config_module.apply_configuration(config)
|
||||
eq(config.extensions, { 'fugitive' })
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,160 @@
|
||||
-- Copyright (c) 2020-2021 shadmansaleh
|
||||
-- MIT license, see LICENSE for more details.
|
||||
|
||||
local helpers = require('tests.helpers')
|
||||
|
||||
local eq = assert.are.same
|
||||
local build_component_opts = helpers.build_component_opts
|
||||
local stub = require('luassert.stub')
|
||||
|
||||
describe('Utils', function()
|
||||
local utils = require('lualine.utils.utils')
|
||||
|
||||
it('can retrieve highlight groups', function()
|
||||
local hl2 = { fg = '#aabbcc', bg = '#889977', sp = '#997788', reverse = true, undercurl = true }
|
||||
-- handles non existing hl groups
|
||||
eq(utils.extract_highlight_colors('hl2'), nil)
|
||||
-- create highlight
|
||||
vim.cmd(string.format('hi hl2 guifg=%s guibg=%s guisp=%s gui=reverse,undercurl', hl2.fg, hl2.bg, hl2.sp))
|
||||
-- Can retrieve entire highlight table
|
||||
eq(utils.extract_highlight_colors('hl2'), hl2)
|
||||
-- Can retrieve specific parts of highlight
|
||||
eq(utils.extract_highlight_colors('hl2', 'fg'), hl2.fg)
|
||||
-- clear hl2
|
||||
vim.cmd('hi clear hl2')
|
||||
end)
|
||||
|
||||
it('can extract individual highlight color', function()
|
||||
local fg_clr = '#aabbcc'
|
||||
local bg_clr = '#889977'
|
||||
local sp_clr = '#997788'
|
||||
local def_clr = '#ff0000'
|
||||
local hl_std = { fg = fg_clr, bg = bg_clr }
|
||||
local hl_rvs = { fg = fg_clr, bg = bg_clr, reverse = true }
|
||||
local hl_ul = { sp = sp_clr, undercurl = true }
|
||||
local hl_ul_rvs = { fg = fg_clr, bg = bg_clr, sp = sp_clr, reverse = true, undercurl = true }
|
||||
-- create highlights
|
||||
vim.cmd(string.format('hi hl_std guifg=%s guibg=%s', hl_std.fg, hl_std.bg))
|
||||
vim.cmd(string.format('hi hl_rvs guifg=%s guibg=%s gui=reverse', hl_rvs.fg, hl_rvs.bg))
|
||||
vim.cmd(string.format('hi hl_ul guisp=%s gui=undercurl', hl_ul.sp))
|
||||
vim.cmd(string.format('hi hl_ul_rvs guifg=%s guibg=%s guisp=%s gui=reverse,undercurl', hl_ul_rvs.fg, hl_ul_rvs.bg, hl_ul_rvs.sp))
|
||||
-- Can extract color from primary highlight group
|
||||
eq(utils.extract_color_from_hllist('fg', {'hl_std','hl_ul'}, def_clr), fg_clr)
|
||||
-- Can extract color from fallback highlight group
|
||||
eq(utils.extract_color_from_hllist('fg', {'hl_noexist','hl_std'}, def_clr), fg_clr)
|
||||
-- Can fall back to default color on nonexistent color
|
||||
eq(utils.extract_color_from_hllist('fg', {'hl_ul'}, def_clr), def_clr)
|
||||
-- Can fall back to default color on nonexistent highlight group
|
||||
eq(utils.extract_color_from_hllist('fg', {'hl_noexist'}, def_clr), def_clr)
|
||||
-- Can extract fallback color
|
||||
eq(utils.extract_color_from_hllist({'fg','sp'}, {'hl_ul'}, def_clr), sp_clr)
|
||||
-- Can extract reverse color
|
||||
eq(utils.extract_color_from_hllist('fg', {'hl_rvs'}, def_clr), bg_clr)
|
||||
-- Can extract fallback reverse color
|
||||
eq(utils.extract_color_from_hllist({'sp','fg'}, {'hl_rvs'}, def_clr), bg_clr)
|
||||
-- clear highlights
|
||||
vim.cmd('hi clear hl_std')
|
||||
vim.cmd('hi clear hl_rvs')
|
||||
vim.cmd('hi clear hl_ul')
|
||||
vim.cmd('hi clear hl_ul_rvs')
|
||||
end)
|
||||
|
||||
it('can shrink list with holes', function()
|
||||
local list_with_holes = {
|
||||
'2',
|
||||
'4',
|
||||
'6',
|
||||
nil,
|
||||
'43',
|
||||
nil,
|
||||
'2',
|
||||
'',
|
||||
'a',
|
||||
'',
|
||||
'b',
|
||||
' ',
|
||||
}
|
||||
local list_without_holes = { '2', '4', '6', '43', '2', 'a', 'b', ' ' }
|
||||
eq(utils.list_shrink(list_with_holes), list_without_holes)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Cterm generator', function()
|
||||
local cterm = require('lualine.utils.color_utils')
|
||||
|
||||
it('can convert rgb to cterm', function()
|
||||
local colors = { ['#112233'] = 235, ['#7928ae'] = 97, ['#017bdc'] = 68 }
|
||||
for rgb, ct in pairs(colors) do
|
||||
eq(cterm.rgb2cterm(rgb), tostring(ct))
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Section generator', function()
|
||||
local hl = require('lualine.highlight')
|
||||
stub(hl, 'format_highlight')
|
||||
hl.format_highlight.returns('%#lualine_c_normal#')
|
||||
|
||||
local sec = require('lualine.utils.section')
|
||||
it('can draw', function()
|
||||
local opts = build_component_opts { section_separators = { left = '', right = '' } }
|
||||
local section = {
|
||||
require('lualine.components.special.function_component')(opts),
|
||||
require('lualine.components.special.function_component')(opts),
|
||||
}
|
||||
eq('%#lualine_c_normal# test %#lualine_c_normal# test ', sec.draw_section(section, 'c', true))
|
||||
|
||||
hl.format_highlight:revert()
|
||||
end)
|
||||
|
||||
it('can remove separators from component with custom colors', function()
|
||||
stub(hl, 'format_highlight')
|
||||
stub(hl, 'get_lualine_hl')
|
||||
hl.format_highlight.returns('%#lualine_MySection_normal#')
|
||||
hl.get_lualine_hl.returns { fg = '#000000', bg = '#ffffff' }
|
||||
|
||||
vim.g.actual_curwin = tostring(vim.api.nvim_get_current_win())
|
||||
local opts = build_component_opts { section_separators = { left = '', right = '' } }
|
||||
local opts_colored = build_component_opts { color = 'MyColor' }
|
||||
local opts_colored2 = build_component_opts {
|
||||
color = { bg = '#223344' },
|
||||
section_separators = { left = '', right = '' },
|
||||
}
|
||||
local opts_colored3 = build_component_opts {
|
||||
color = { fg = '#223344' },
|
||||
section_separators = { left = '', right = '' },
|
||||
}
|
||||
require('lualine.highlight').create_highlight_groups(require('lualine.themes.gruvbox'))
|
||||
local section = {
|
||||
require('lualine.components.special.function_component')(opts),
|
||||
require('lualine.components.special.function_component')(opts_colored),
|
||||
require('lualine.components.special.function_component')(opts),
|
||||
}
|
||||
local highlight_name2 = 'lualine_c_' .. section[2].options.component_name
|
||||
-- Removes separator on string color
|
||||
eq(
|
||||
'%#lualine_MySection_normal# test %#' .. highlight_name2 .. '#' .. ' test %#lualine_MySection_normal# test ',
|
||||
sec.draw_section(section, 'MySection')
|
||||
)
|
||||
section[2] = require('lualine.components.special.function_component')(opts_colored2)
|
||||
local highlight_name = '%#lualine_c_' .. section[2].options.component_name .. '_normal#'
|
||||
-- Removes separator on color with bg
|
||||
eq(
|
||||
'%#lualine_MySection_normal# test ' .. highlight_name .. ' test %#lualine_MySection_normal# test ',
|
||||
sec.draw_section(section, 'MySection')
|
||||
)
|
||||
section[2] = require('lualine.components.special.function_component')(opts_colored3)
|
||||
highlight_name2 = '%#lualine_c_' .. section[2].options.component_name .. '_normal#'
|
||||
-- Doesn't remove separator on color without bg
|
||||
eq(
|
||||
'%#lualine_MySection_normal# test '
|
||||
.. highlight_name2
|
||||
.. ' test %#lualine_MySection_normal#%#lualine_MySection_normal# test ',
|
||||
sec.draw_section(section, 'MySection')
|
||||
)
|
||||
vim.g.actual_curwin = nil
|
||||
|
||||
hl.format_highlight:revert()
|
||||
hl.get_lualine_hl:revert()
|
||||
end)
|
||||
end)
|
||||
@ -0,0 +1,229 @@
|
||||
-- Copyright (c) 2020-2021 shadmansaleh
|
||||
-- MIT license, see LICENSE for more details.
|
||||
|
||||
--- ## Testing module for lualines statusline
|
||||
---
|
||||
--- ###Uses:
|
||||
---
|
||||
--- Create a new instance with status width 120 & for active statusline
|
||||
--- like following.
|
||||
---
|
||||
--- ``lua
|
||||
--- local statusline = require('tests.statusline').new(120, 'active')
|
||||
--- ```
|
||||
---
|
||||
--- To create a new instance with status width 80 & for inactive statusline use following.
|
||||
---
|
||||
--- ``lua
|
||||
--- local statusline = require('tests.statusline').new(120, 'inactive')
|
||||
--- ```
|
||||
---
|
||||
--- Now setup the state you want to test.
|
||||
--- To test you'll call `expect` method on statusline for example.
|
||||
---
|
||||
--- To create a new instance with status width 80 & tabline
|
||||
---
|
||||
--- ``lua
|
||||
--- local statusline = require('tests.statusline').new(120, 'tabline')
|
||||
--- ```
|
||||
---
|
||||
--- Now setup the state you want to test.
|
||||
--- To test you'll call `expect` method on statusline for example.
|
||||
---
|
||||
--- ``lua
|
||||
--- statusline:expect([===[
|
||||
--- highlights = {
|
||||
--- 1: lualine_c_inactive = { bg = "#3c3836", fg = "#a89984" }
|
||||
--- }
|
||||
--- |{1: [No Name] }
|
||||
--- {1: }
|
||||
--- {1: 0:1 }|
|
||||
---
|
||||
---]===])
|
||||
--- ```
|
||||
---
|
||||
--- For more flexibility you can match a pattern in expect block.
|
||||
--- ``lua
|
||||
--- statusline:expect([===[
|
||||
--- highlights = {
|
||||
--- 1: lualine_a_tabs_inactive = { bg = "#3c3836", bold = true, fg = "#a89984" }
|
||||
--- 2: lualine_transitional_lualine_a_tabs_inactive_to_lualine_a_tabs_active = { bg = "#a89984", fg = "#3c3836" }
|
||||
--- 3: lualine_a_tabs_active = { bg = "#a89984", bold = true, fg = "#282828" }
|
||||
--- 4: lualine_transitional_lualine_a_tabs_active_to_lualine_c_normal = { bg = "#3c3836", fg = "#a89984" }
|
||||
--- 5: lualine_c_normal = { bg = "#3c3836", fg = "#a89984" }
|
||||
--- }
|
||||
--- {MATCH:|{1: %d+ }}
|
||||
--- {MATCH:{1: %d+ }}
|
||||
--- {2:}
|
||||
--- {MATCH:{3: %d+ }}
|
||||
--- {4:}
|
||||
--- {MATCH:{5:%s+}|}
|
||||
---
|
||||
---]===])
|
||||
--- ```
|
||||
---
|
||||
--- An easy way to create an expect block is to call `snapshot` method
|
||||
--- on statusline where you'll call expect and run the test. It will print
|
||||
--- an expect block based on the state of statusline. You can copy it and
|
||||
--- replace the snapshot call with the expect call.
|
||||
---
|
||||
--- ``lua
|
||||
--- statusline:snapshot()
|
||||
--- ```
|
||||
|
||||
local helpers = require('tests.helpers')
|
||||
local stub = require('luassert.stub')
|
||||
|
||||
local M = {}
|
||||
|
||||
local function eval_stl(stl_expr, width, eval_type)
|
||||
local stl_buf, hl_list, stl_eval_res
|
||||
stl_eval_res = vim.api.nvim_eval_statusline(
|
||||
stl_expr,
|
||||
{ maxwidth = width, highlights = true, fillchar = ' ', use_tabline = (eval_type == 'tabline') }
|
||||
)
|
||||
stl_buf, hl_list = stl_eval_res.str, stl_eval_res.highlights
|
||||
|
||||
local hl_map = {}
|
||||
|
||||
local buf = { 'highlights = {' }
|
||||
local hl_id = 1
|
||||
for _, hl in ipairs(hl_list) do
|
||||
local hl_name = hl.group
|
||||
if not hl_map[hl_name] then
|
||||
hl_map[hl_name] = require('lualine.utils.utils').extract_highlight_colors(hl_name) or {}
|
||||
table.insert(
|
||||
buf,
|
||||
string.format(' %4d: %s = %s', hl_id, hl_name, vim.inspect(hl_map[hl_name], { newline = ' ', indent = '' }))
|
||||
)
|
||||
hl_map[hl_name].id = hl_id
|
||||
hl_id = hl_id + 1
|
||||
end
|
||||
end
|
||||
table.insert(buf, '}')
|
||||
|
||||
local stl = {}
|
||||
for i = 1, #hl_list do
|
||||
local start, finish = hl_list[i].start, hl_list[i + 1] and hl_list[i + 1].start or #stl_buf
|
||||
if start ~= finish then
|
||||
table.insert(
|
||||
stl,
|
||||
string.format('{%d:%s}', hl_map[hl_list[i].group].id, vim.fn.strpart(stl_buf, start, finish - start))
|
||||
)
|
||||
end
|
||||
end
|
||||
table.insert(buf, '|' .. table.concat(stl, '\n') .. '|')
|
||||
table.insert(buf, '')
|
||||
return table.concat(buf, '\n')
|
||||
end
|
||||
|
||||
function M:expect_expr(expect, expr)
|
||||
if expr == nil then
|
||||
-- test if both are nil when running expect against nil
|
||||
assert.are.same(expect, nil)
|
||||
return
|
||||
end
|
||||
expect = helpers.dedent(expect)
|
||||
local actual = eval_stl(expr, self.width, self.type)
|
||||
local matched = true
|
||||
local errmsg = {}
|
||||
if expect ~= actual then
|
||||
expect = vim.split(expect, '\n')
|
||||
actual = vim.split(actual, '\n')
|
||||
if expect[#expect] == '' then
|
||||
expect[#expect] = nil
|
||||
end
|
||||
if actual[#actual] == '' then
|
||||
actual[#actual] = nil
|
||||
end
|
||||
for i = 1, math.max(#expect, #actual) do
|
||||
if expect[i] and actual[i] then
|
||||
local match_pat = expect[i]:match('{MATCH:(.*)}')
|
||||
if expect[i] == actual[i] or (match_pat and actual[i]:match(match_pat)) then
|
||||
expect[i] = string.rep(' ', 2) .. expect[i]
|
||||
actual[i] = string.rep(' ', 2) .. actual[i]
|
||||
goto loop_end
|
||||
end
|
||||
end
|
||||
matched = false
|
||||
if expect[i] then
|
||||
expect[i] = '*' .. string.rep(' ', 1) .. expect[i]
|
||||
end
|
||||
if actual[i] then
|
||||
actual[i] = '*' .. string.rep(' ', 1) .. actual[i]
|
||||
end
|
||||
::loop_end::
|
||||
end
|
||||
end
|
||||
if not matched then
|
||||
table.insert(errmsg, 'Unexpected statusline')
|
||||
table.insert(errmsg, 'Expected:')
|
||||
table.insert(errmsg, table.concat(expect, '\n') .. '\n')
|
||||
table.insert(errmsg, 'Actual:')
|
||||
table.insert(errmsg, table.concat(actual, '\n'))
|
||||
end
|
||||
assert(matched, table.concat(errmsg, '\n'))
|
||||
end
|
||||
|
||||
function M:snapshot_expr(expr)
|
||||
local type_map = {
|
||||
active = 'statusline',
|
||||
inactive = 'inactive_statusline',
|
||||
tabline = 'tabline',
|
||||
}
|
||||
print((type_map[self.type] or 'statusline') .. ':expect([===[')
|
||||
print(eval_stl(expr, self.width, self.type) .. ']===])')
|
||||
end
|
||||
|
||||
function M:snapshot()
|
||||
local utils = require('lualine.utils.utils')
|
||||
stub(utils, 'is_focused')
|
||||
utils.is_focused.returns(self.type ~= 'inactive')
|
||||
local expr
|
||||
if self.type == 'inactive' then
|
||||
expr = require('lualine').statusline(false)
|
||||
elseif self.type == 'tabline' then
|
||||
expr = require('lualine').tabline()
|
||||
else
|
||||
expr = require('lualine').statusline(true)
|
||||
end
|
||||
self:snapshot_expr(expr)
|
||||
utils.is_focused:revert()
|
||||
end
|
||||
|
||||
function M:expect(result)
|
||||
local utils = require('lualine.utils.utils')
|
||||
stub(utils, 'is_focused')
|
||||
utils.is_focused.returns(self.type ~= 'inactive')
|
||||
local expr
|
||||
if self.type == 'inactive' then
|
||||
expr = require('lualine').statusline(false)
|
||||
elseif self.type == 'tabline' then
|
||||
expr = require('lualine').tabline()
|
||||
else
|
||||
expr = require('lualine').statusline(true)
|
||||
end
|
||||
self:expect_expr(result, expr)
|
||||
utils.is_focused:revert()
|
||||
end
|
||||
|
||||
function M.new(_, width, eval_type)
|
||||
if type(_) ~= 'table' then
|
||||
eval_type = width
|
||||
width = _
|
||||
end
|
||||
local self = {}
|
||||
self.width = width or 120
|
||||
self.type = eval_type
|
||||
if self.type == nil then
|
||||
self.type = 'active'
|
||||
end
|
||||
return setmetatable(self, {
|
||||
__index = M,
|
||||
__call = function(_, ...)
|
||||
M.new(...)
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
return M.new()
|
||||
Reference in New Issue
Block a user