2021-09-20 11:46:38 +00:00
|
|
|
-- Copyright (c) 2020-2021 shadmansaleh
|
|
|
|
-- MIT license, see LICENSE for more details.
|
|
|
|
|
2022-01-02 11:38:39 +00:00
|
|
|
local helpers = require('tests.helpers')
|
2021-05-05 18:04:16 +00:00
|
|
|
|
2021-05-09 21:11:18 +00:00
|
|
|
local eq = assert.are.same
|
|
|
|
local neq = assert.are_not.same
|
2021-05-05 18:04:16 +00:00
|
|
|
local assert_component = helpers.assert_component
|
|
|
|
local build_component_opts = helpers.build_component_opts
|
2022-01-02 11:38:39 +00:00
|
|
|
local stub = require('luassert.stub')
|
2021-05-05 18:04:16 +00:00
|
|
|
|
|
|
|
describe('Component:', function()
|
|
|
|
it('can select separators', function()
|
|
|
|
local opts = build_component_opts()
|
2022-01-02 11:38:39 +00:00
|
|
|
local comp = require('lualine.components.special.function_component')(opts)
|
2021-05-05 18:04:16 +00:00
|
|
|
-- correct for lualine_c
|
|
|
|
eq('', comp.options.separator)
|
2022-03-02 13:37:08 +00:00
|
|
|
local opts2 = build_component_opts { self = { section = 'y' } }
|
2022-01-02 11:38:39 +00:00
|
|
|
local comp2 = require('lualine.components.special.function_component')(opts2)
|
2021-05-05 18:04:16 +00:00
|
|
|
-- correct for lualine_u
|
|
|
|
eq('', comp2.options.separator)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('can provide unique identifier', function()
|
|
|
|
local opts1 = build_component_opts()
|
2022-01-02 11:38:39 +00:00
|
|
|
local comp1 = require('lualine.components.special.function_component')(opts1)
|
2021-05-05 18:04:16 +00:00
|
|
|
local opts2 = build_component_opts()
|
2022-01-02 11:38:39 +00:00
|
|
|
local comp2 = require('lualine.components.special.function_component')(opts2)
|
2021-05-05 18:04:16 +00:00
|
|
|
neq(comp1.component_no, comp2.component_no)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('create option highlights', function()
|
2021-09-03 18:28:20 +00:00
|
|
|
local color = { fg = '#224532', bg = '#892345' }
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts1 = build_component_opts { color = color }
|
2022-01-02 11:38:39 +00:00
|
|
|
local hl = require('lualine.highlight')
|
2021-05-05 18:04:16 +00:00
|
|
|
stub(hl, 'create_component_highlight_group')
|
2022-01-02 11:38:39 +00:00
|
|
|
hl.create_component_highlight_group.returns('MyCompHl')
|
|
|
|
local comp1 = require('lualine.components.special.function_component')(opts1)
|
2021-05-05 18:04:16 +00:00
|
|
|
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
|
2022-03-02 13:37:08 +00:00
|
|
|
assert.stub(hl.create_component_highlight_group).was_called_with(
|
|
|
|
color,
|
|
|
|
comp1.options.component_name,
|
|
|
|
comp1.options,
|
|
|
|
false
|
|
|
|
)
|
2021-05-05 18:04:16 +00:00
|
|
|
hl.create_component_highlight_group:revert()
|
2021-08-09 07:53:42 +00:00
|
|
|
color = 'MyHl'
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts2 = build_component_opts { color = color }
|
2021-08-09 07:53:42 +00:00
|
|
|
stub(hl, 'create_component_highlight_group')
|
2022-01-02 11:38:39 +00:00
|
|
|
hl.create_component_highlight_group.returns('MyCompLinkedHl')
|
|
|
|
local comp2 = require('lualine.components.special.function_component')(opts2)
|
2021-08-09 07:53:42 +00:00
|
|
|
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
|
2022-03-02 13:37:08 +00:00
|
|
|
assert.stub(hl.create_component_highlight_group).was_called_with(
|
|
|
|
color,
|
|
|
|
comp2.options.component_name,
|
|
|
|
comp2.options,
|
|
|
|
false
|
|
|
|
)
|
2021-08-09 07:53:42 +00:00
|
|
|
hl.create_component_highlight_group:revert()
|
2021-05-05 18:04:16 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can draw', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-09-03 18:28:20 +00:00
|
|
|
padding = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component(nil, opts, 'test')
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('can apply separators', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts { padding = 0 }
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component(nil, opts, 'test')
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('can apply default highlight', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts { padding = 0, hl = '%#My_highlight#' }
|
2021-08-26 11:47:48 +00:00
|
|
|
assert_component(nil, opts, '%#My_highlight#test')
|
2022-02-01 08:04:03 +00:00
|
|
|
opts = build_component_opts {
|
2021-09-03 18:28:20 +00:00
|
|
|
function()
|
|
|
|
return '%#Custom_hl#test'
|
|
|
|
end,
|
|
|
|
padding = 0,
|
|
|
|
hl = '%#My_highlight#',
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-08-26 11:47:48 +00:00
|
|
|
assert_component(nil, opts, '%#Custom_hl#test%#My_highlight#')
|
2022-02-01 08:04:03 +00:00
|
|
|
opts = build_component_opts {
|
2021-09-03 18:28:20 +00:00
|
|
|
function()
|
|
|
|
return 'in middle%#Custom_hl#test'
|
|
|
|
end,
|
|
|
|
padding = 0,
|
|
|
|
hl = '%#My_highlight#',
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-08-26 11:47:48 +00:00
|
|
|
assert_component(nil, opts, '%#My_highlight#in middle%#Custom_hl#test%#My_highlight#')
|
2021-05-05 18:04:16 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('Global options:', function()
|
|
|
|
it('left_padding', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
|
|
|
padding = { left = 5 },
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component(nil, opts, ' test')
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('right_padding', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
|
|
|
padding = { right = 5 },
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component(nil, opts, 'test ')
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('padding', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-09-03 18:28:20 +00:00
|
|
|
padding = 5,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component(nil, opts, ' test ')
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('icon', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-05-05 18:04:16 +00:00
|
|
|
padding = 0,
|
2021-09-03 18:28:20 +00:00
|
|
|
icon = '0',
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component(nil, opts, '0 test')
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('icons_enabled', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-05-05 18:04:16 +00:00
|
|
|
padding = 0,
|
|
|
|
icons_enabled = true,
|
2021-09-03 18:28:20 +00:00
|
|
|
icon = '0',
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component(nil, opts, '0 test')
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts2 = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-09-03 18:28:20 +00:00
|
|
|
padding = 0,
|
|
|
|
icons_enabled = false,
|
|
|
|
icon = '0',
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component(nil, opts2, 'test')
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('separator', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-05-05 18:04:16 +00:00
|
|
|
padding = 0,
|
2021-09-03 18:28:20 +00:00
|
|
|
separator = '|',
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component(nil, opts, 'test|')
|
|
|
|
end)
|
|
|
|
|
2021-09-14 15:14:23 +00:00
|
|
|
it('fmt', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-05-05 18:04:16 +00:00
|
|
|
padding = 0,
|
2021-09-14 15:14:23 +00:00
|
|
|
fmt = function(data)
|
2021-05-05 18:04:16 +00:00
|
|
|
return data:sub(1, 1):upper() .. data:sub(2, #data)
|
2021-09-03 18:28:20 +00:00
|
|
|
end,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component(nil, opts, 'Test')
|
|
|
|
end)
|
|
|
|
|
2021-09-14 15:14:23 +00:00
|
|
|
it('cond', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-05-05 18:04:16 +00:00
|
|
|
padding = 0,
|
2021-09-14 15:14:23 +00:00
|
|
|
cond = function()
|
2021-09-03 18:28:20 +00:00
|
|
|
return true
|
|
|
|
end,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component(nil, opts, 'test')
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts2 = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-09-03 18:28:20 +00:00
|
|
|
padding = 0,
|
2021-09-14 15:14:23 +00:00
|
|
|
cond = function()
|
2021-09-03 18:28:20 +00:00
|
|
|
return false
|
|
|
|
end,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component(nil, opts2, '')
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('color', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-05-05 18:04:16 +00:00
|
|
|
padding = 0,
|
2021-09-03 18:28:20 +00:00
|
|
|
color = 'MyHl',
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2022-01-02 11:38:39 +00:00
|
|
|
local comp = require('lualine.components.special.function_component')(opts)
|
2022-03-02 13:37:08 +00:00
|
|
|
local custom_link_hl_name = 'lualine_c_' .. comp.options.component_name
|
2021-09-03 18:28:20 +00:00
|
|
|
eq('%#' .. custom_link_hl_name .. '#test', comp:draw(opts.hl))
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts2 = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-09-03 18:28:20 +00:00
|
|
|
padding = 0,
|
|
|
|
color = { bg = '#230055', fg = '#223344' },
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2022-01-02 11:38:39 +00:00
|
|
|
local hl = require('lualine.highlight')
|
2021-05-05 18:04:16 +00:00
|
|
|
stub(hl, 'component_format_highlight')
|
2022-01-02 11:38:39 +00:00
|
|
|
hl.component_format_highlight.returns('%#MyCompHl#')
|
|
|
|
local comp2 = require('lualine.components.special.function_component')(opts2)
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component(nil, opts2, '%#MyCompHl#test')
|
2021-09-03 18:28:20 +00:00
|
|
|
assert.stub(hl.component_format_highlight).was_called_with(comp2.options.color_highlight)
|
2021-05-05 18:04:16 +00:00
|
|
|
hl.component_format_highlight:revert()
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('Encoding component', function()
|
|
|
|
it('works', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-09-03 18:28:20 +00:00
|
|
|
padding = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-10-21 13:16:12 +00:00
|
|
|
local tmp_path = 'tmp.txt'
|
|
|
|
local tmp_fp = io.open(tmp_path, 'w')
|
2022-01-02 11:38:39 +00:00
|
|
|
tmp_fp:write('test file')
|
2021-10-21 13:16:12 +00:00
|
|
|
tmp_fp:close()
|
|
|
|
vim.cmd('e ' .. tmp_path)
|
|
|
|
assert_component('encoding', opts, 'utf-8')
|
2022-01-02 11:38:39 +00:00
|
|
|
vim.cmd('bd!')
|
2021-10-21 13:16:12 +00:00
|
|
|
os.remove(tmp_path)
|
2021-05-05 18:04:16 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('Fileformat component', function()
|
|
|
|
it('works with icons', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-09-03 18:28:20 +00:00
|
|
|
padding = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
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()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-05-05 18:04:16 +00:00
|
|
|
padding = 0,
|
2021-09-03 18:28:20 +00:00
|
|
|
icons_enabled = false,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component('fileformat', opts, vim.bo.fileformat)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2021-05-11 12:55:18 +00:00
|
|
|
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()
|
2021-11-13 14:01:01 +00:00
|
|
|
local old_require = _G.require
|
|
|
|
function _G.require(...)
|
|
|
|
if select(1, ...) == 'nvim-web-devicons' then
|
2022-01-02 11:38:39 +00:00
|
|
|
error('Test case not suppose to have web-dev-icon 👀')
|
2021-11-13 14:01:01 +00:00
|
|
|
end
|
|
|
|
return old_require(...)
|
|
|
|
end
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-09-03 18:28:20 +00:00
|
|
|
padding = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-11 12:55:18 +00:00
|
|
|
assert_component('filetype', opts, 'lua')
|
2021-11-13 14:01:01 +00:00
|
|
|
_G.require = old_require
|
2021-05-11 12:55:18 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('colors nvim-web-devicons icons', function()
|
|
|
|
package.loaded['nvim-web-devicons'] = {
|
|
|
|
get_icon = function()
|
|
|
|
return '*', 'test_highlight_group'
|
2021-09-03 18:28:20 +00:00
|
|
|
end,
|
2021-05-11 12:55:18 +00:00
|
|
|
}
|
2022-03-02 13:37:08 +00:00
|
|
|
vim.g.actual_curwin = tostring(vim.api.nvim_get_current_win())
|
2022-01-02 11:38:39 +00:00
|
|
|
local hl = require('lualine.highlight')
|
|
|
|
local utils = require('lualine.utils.utils')
|
2021-05-11 12:55:18 +00:00
|
|
|
stub(hl, 'create_component_highlight_group')
|
2022-03-02 13:37:08 +00:00
|
|
|
stub(hl, 'format_highlight')
|
2021-05-11 12:55:18 +00:00
|
|
|
stub(utils, 'extract_highlight_colors')
|
2022-03-02 13:37:08 +00:00
|
|
|
hl.create_component_highlight_group.returns { name = 'MyCompHl', no_mode = false, section = 'a' }
|
|
|
|
hl.format_highlight.returns('%#lualine_c_normal#')
|
2022-01-02 11:38:39 +00:00
|
|
|
utils.extract_highlight_colors.returns('#000')
|
2021-05-11 12:55:18 +00:00
|
|
|
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2022-03-02 13:37:08 +00:00
|
|
|
hl = '%#lualine_c_normal#',
|
2021-05-12 09:17:51 +00:00
|
|
|
padding = 0,
|
2021-09-03 18:28:20 +00:00
|
|
|
colored = true,
|
2021-09-14 15:14:23 +00:00
|
|
|
icon_only = false,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2022-03-02 13:37:08 +00:00
|
|
|
assert_component('filetype', opts, '%#MyCompHl_normal#*%#lualine_c_normal# lua%#lualine_c_normal#')
|
2021-05-11 12:55:18 +00:00
|
|
|
assert.stub(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg')
|
2022-03-02 13:37:08 +00:00
|
|
|
assert.stub(hl.create_component_highlight_group).was_called_with(
|
|
|
|
{ fg = '#000' },
|
2022-04-14 18:30:47 +00:00
|
|
|
'filetype_test_highlight_group',
|
2022-03-02 13:37:08 +00:00
|
|
|
opts,
|
|
|
|
false
|
|
|
|
)
|
2021-05-11 12:55:18 +00:00
|
|
|
hl.create_component_highlight_group:revert()
|
2022-03-02 13:37:08 +00:00
|
|
|
hl.format_highlight:revert()
|
2021-05-11 12:55:18 +00:00
|
|
|
utils.extract_highlight_colors:revert()
|
2021-05-12 09:17:51 +00:00
|
|
|
package.loaded['nvim-web-devicons'] = nil
|
2022-03-02 13:37:08 +00:00
|
|
|
vim.g.actual_curwin = nil
|
2021-05-12 09:17:51 +00:00
|
|
|
end)
|
|
|
|
|
2021-09-03 18:28:20 +00:00
|
|
|
it("Doesn't color when colored is false", function()
|
2021-05-12 09:17:51 +00:00
|
|
|
package.loaded['nvim-web-devicons'] = {
|
|
|
|
get_icon = function()
|
|
|
|
return '*', 'test_highlight_group'
|
2021-09-03 18:28:20 +00:00
|
|
|
end,
|
2021-05-12 09:17:51 +00:00
|
|
|
}
|
2022-01-02 11:38:39 +00:00
|
|
|
local hl = require('lualine.highlight')
|
|
|
|
local utils = require('lualine.utils.utils')
|
2021-05-12 09:17:51 +00:00
|
|
|
stub(hl, 'create_component_highlight_group')
|
|
|
|
stub(utils, 'extract_highlight_colors')
|
2022-01-02 11:38:39 +00:00
|
|
|
hl.create_component_highlight_group.returns('MyCompHl')
|
|
|
|
utils.extract_highlight_colors.returns('#000')
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-05-12 09:17:51 +00:00
|
|
|
padding = 0,
|
|
|
|
colored = false,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-12 09:17:51 +00:00
|
|
|
assert_component('filetype', opts, '* lua')
|
|
|
|
hl.create_component_highlight_group:revert()
|
|
|
|
utils.extract_highlight_colors:revert()
|
|
|
|
package.loaded['nvim-web-devicons'] = nil
|
|
|
|
end)
|
|
|
|
|
2021-09-14 15:14:23 +00:00
|
|
|
it('displays only icon when icon_only is true', function()
|
2021-05-12 09:17:51 +00:00
|
|
|
package.loaded['nvim-web-devicons'] = {
|
|
|
|
get_icon = function()
|
|
|
|
return '*', 'test_highlight_group'
|
2021-09-03 18:28:20 +00:00
|
|
|
end,
|
2021-05-12 09:17:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-05-12 09:17:51 +00:00
|
|
|
padding = 0,
|
|
|
|
colored = false,
|
2021-09-14 15:14:23 +00:00
|
|
|
icon_only = true,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-12 09:17:51 +00:00
|
|
|
assert_component('filetype', opts, '*')
|
|
|
|
package.loaded['nvim-web-devicons'] = nil
|
2021-05-11 12:55:18 +00:00
|
|
|
end)
|
2022-04-30 10:10:47 +00:00
|
|
|
|
|
|
|
it('displays right aligned icon when icon.align is "right"', function()
|
|
|
|
package.loaded['nvim-web-devicons'] = {
|
|
|
|
get_icon = function()
|
|
|
|
return '*', 'test_highlight_group'
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
local opts = build_component_opts {
|
|
|
|
component_separators = { left = '', right = '' },
|
|
|
|
padding = 0,
|
|
|
|
colored = false,
|
|
|
|
icon_only = false,
|
|
|
|
icon = { align = 'right' }
|
|
|
|
}
|
|
|
|
assert_component('filetype', opts, 'lua *')
|
|
|
|
package.loaded['nvim-web-devicons'] = nil
|
|
|
|
end)
|
2021-05-11 12:55:18 +00:00
|
|
|
end)
|
|
|
|
|
2021-05-05 18:04:16 +00:00
|
|
|
describe('Hostname component', function()
|
|
|
|
it('works', function()
|
|
|
|
stub(vim.loop, 'os_gethostname')
|
2022-01-02 11:38:39 +00:00
|
|
|
vim.loop.os_gethostname.returns('localhost')
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-09-03 18:28:20 +00:00
|
|
|
padding = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component('hostname', opts, 'localhost')
|
|
|
|
vim.loop.os_gethostname:revert()
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('Location component', function()
|
|
|
|
it('works', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-09-03 18:28:20 +00:00
|
|
|
padding = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-10-22 04:19:29 +00:00
|
|
|
assert_component('location', opts, '%3l:%-2v')
|
2021-05-05 18:04:16 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('Progress component', function()
|
|
|
|
it('works', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-09-03 18:28:20 +00:00
|
|
|
padding = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-10-22 04:19:29 +00:00
|
|
|
assert_component('progress', opts, '%3p%%')
|
2021-05-05 18:04:16 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('Mode component', function()
|
|
|
|
it('works', function()
|
|
|
|
stub(vim.api, 'nvim_get_mode')
|
2022-02-01 08:04:03 +00:00
|
|
|
vim.api.nvim_get_mode.returns { mode = 'n', blocking = false }
|
|
|
|
local opts = build_component_opts {
|
2021-09-14 15:14:23 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
2021-09-03 18:28:20 +00:00
|
|
|
padding = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-05-05 18:04:16 +00:00
|
|
|
assert_component('mode', opts, 'NORMAL')
|
|
|
|
vim.api.nvim_get_mode:revert()
|
|
|
|
end)
|
|
|
|
end)
|
2021-11-22 13:40:18 +00:00
|
|
|
|
|
|
|
describe('FileSize component', function()
|
|
|
|
it('works', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-11-22 13:40:18 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
|
|
|
padding = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-11-22 13:40:18 +00:00
|
|
|
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')
|
2022-01-02 11:38:39 +00:00
|
|
|
vim.cmd(':bdelete!')
|
2021-11-22 13:40:18 +00:00
|
|
|
os.remove(fname)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('Filename component', function()
|
|
|
|
local function shorten_path(path, target)
|
|
|
|
target = target and target or 40
|
|
|
|
local sep = package.config:sub(1, 1)
|
|
|
|
local winwidth = vim.fn.winwidth(0)
|
|
|
|
local segments = select(2, string.gsub(path, sep, ''))
|
|
|
|
for _ = 0, segments do
|
|
|
|
if winwidth <= 84 or #path > winwidth - target then
|
|
|
|
path = path:gsub(string.format('([^%s])[^%s]+%%%s', sep, sep, sep), '%1' .. sep, 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
|
|
|
|
it('works', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-11-22 13:40:18 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
|
|
|
padding = 0,
|
|
|
|
file_status = false,
|
|
|
|
path = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2022-01-02 11:38:39 +00:00
|
|
|
vim.cmd(':e test-file.txt')
|
2021-11-22 13:40:18 +00:00
|
|
|
assert_component('filename', opts, 'test-file.txt')
|
2022-01-02 11:38:39 +00:00
|
|
|
vim.cmd(':bdelete!')
|
2021-11-22 13:40:18 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can show file_status', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-11-22 13:40:18 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
|
|
|
padding = 0,
|
|
|
|
file_status = true,
|
|
|
|
path = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2022-01-02 11:38:39 +00:00
|
|
|
vim.cmd(':e test-file.txt')
|
2021-11-22 13:40:18 +00:00
|
|
|
vim.bo.modified = false
|
|
|
|
assert_component('filename', opts, 'test-file.txt')
|
|
|
|
vim.bo.modified = true
|
|
|
|
assert_component('filename', opts, 'test-file.txt[+]')
|
|
|
|
vim.bo.modified = false
|
|
|
|
vim.bo.ro = true
|
|
|
|
assert_component('filename', opts, 'test-file.txt[-]')
|
2022-01-02 11:38:39 +00:00
|
|
|
vim.cmd(':bdelete!')
|
2021-11-22 13:40:18 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can show relative path', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-11-22 13:40:18 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
|
|
|
padding = 0,
|
|
|
|
file_status = false,
|
|
|
|
path = 1,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2022-01-02 11:38:39 +00:00
|
|
|
vim.cmd(':e test-file.txt')
|
|
|
|
assert_component('filename', opts, shorten_path(vim.fn.expand('%:~:.')))
|
|
|
|
vim.cmd(':bdelete!')
|
2021-11-22 13:40:18 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can show full path', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-11-22 13:40:18 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
|
|
|
padding = 0,
|
|
|
|
file_status = false,
|
|
|
|
path = 2,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2022-01-02 11:38:39 +00:00
|
|
|
vim.cmd(':e test-file.txt')
|
|
|
|
assert_component('filename', opts, shorten_path(vim.fn.expand('%:p')))
|
|
|
|
vim.cmd(':bdelete!')
|
2021-11-22 13:40:18 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('vim option & variable component', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-11-22 13:40:18 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
|
|
|
padding = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-11-22 13:40:18 +00:00
|
|
|
|
|
|
|
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()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-11-22 13:40:18 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
|
|
|
padding = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-11-22 13:40:18 +00:00
|
|
|
|
|
|
|
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()
|
2022-01-02 11:38:39 +00:00
|
|
|
vim.cmd([[
|
2021-11-22 13:40:18 +00:00
|
|
|
func! TestFunction() abort
|
|
|
|
return "TestVimFunction"
|
|
|
|
endf
|
2022-01-02 11:38:39 +00:00
|
|
|
]])
|
2021-11-22 13:40:18 +00:00
|
|
|
assert_vim_var_component('TestFunction', opts, 'TestVimFunction')
|
2022-01-02 11:38:39 +00:00
|
|
|
vim.cmd('delfunction TestFunction')
|
2021-11-22 13:40:18 +00:00
|
|
|
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()
|
2022-07-23 07:01:52 +00:00
|
|
|
-- these tests are broken in wsl will look at them later
|
|
|
|
if vim.fn.has('wsl') == 1 then
|
|
|
|
return
|
|
|
|
end
|
2021-11-22 13:40:18 +00:00
|
|
|
local tmpdir
|
|
|
|
local file
|
|
|
|
local git = function(...)
|
2022-03-05 13:41:08 +00:00
|
|
|
return vim.fn.system(
|
|
|
|
"git -c user.name='asdf' -c user.email='asdf@jlk.org' -C " .. tmpdir .. ' ' .. string.format(...)
|
|
|
|
)
|
2021-11-22 13:40:18 +00:00
|
|
|
end
|
|
|
|
local assert_comp_ins = helpers.assert_component_instence
|
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
tmpdir = os.tmpname()
|
|
|
|
os.remove(tmpdir)
|
|
|
|
file = tmpdir .. '/test.txt'
|
|
|
|
vim.fn.mkdir(tmpdir, 'p')
|
2022-01-02 11:38:39 +00:00
|
|
|
git('init -b test_branch')
|
|
|
|
vim.cmd([[aug lualine
|
2021-11-22 13:40:18 +00:00
|
|
|
au!
|
|
|
|
aug END
|
2022-01-02 11:38:39 +00:00
|
|
|
]])
|
2021-11-22 13:40:18 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
|
|
|
os.remove(tmpdir)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('works with regular branches', function()
|
2022-02-01 08:04:03 +00:00
|
|
|
local opts = build_component_opts {
|
2021-11-22 13:40:18 +00:00
|
|
|
component_separators = { left = '', right = '' },
|
|
|
|
padding = 0,
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-11-22 13:40:18 +00:00
|
|
|
local branch_comp = helpers.init_component('branch', opts)
|
|
|
|
vim.cmd('e ' .. file)
|
|
|
|
assert_comp_ins(branch_comp, ' test_branch')
|
2022-01-02 11:38:39 +00:00
|
|
|
git('checkout -b test_branch2')
|
|
|
|
vim.cmd('e k')
|
|
|
|
vim.cmd('bd')
|
2021-11-22 13:40:18 +00:00
|
|
|
vim.cmd('e ' .. file)
|
|
|
|
opts.icons_enabled = false
|
|
|
|
assert_comp_ins(branch_comp, 'test_branch2')
|
|
|
|
end)
|
|
|
|
|
2022-03-05 13:40:36 +00:00
|
|
|
it('works in detached head mode', function()
|
|
|
|
local opts = build_component_opts {
|
|
|
|
component_separators = { left = '', right = '' },
|
|
|
|
icons_enabled = false,
|
|
|
|
padding = 0,
|
|
|
|
}
|
2022-03-05 13:41:08 +00:00
|
|
|
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"')
|
2022-03-05 13:40:36 +00:00
|
|
|
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)
|
2021-11-22 13:40:18 +00:00
|
|
|
end)
|