lualine.nvim/lua/tests/spec/utils_spec.lua
Shadman 37a314b9e3
feat: add support for dynamic color with functions in color options (#566)
* feat: allow functions in color options.

* update_evilline

* docs: document color functions

* remove unnecesery stuff

* add dynamic color supoort for themes

* chore: autogen (vimdocs+formating)

* fix dynamic colors not working as color fallback

* fix transitional separators not updating for dynamic colors dynamic colors

* fix failing tests

* apply format

* Allow cases where theme doesn't even define nornal color for some mode

* allow color function to return nil

* some enhancements

* more enhancements

* code cleanup

* if we don't have even normal in theme we should just nvim highlight it with it's ususal stl colors

* not sure how it get here . It should be in different pr

* keep only c of lualine_c in component section name

* use sh to run docgen

* fix filetype component not respecting color option properly

* fix section x,y,z not falling back to correct colors

* auto format

* actually fix xyz not falling back to correct mode

* fix comp sep not correctly removed properly on function hl

* pass only section in color fn

* more enhancements

* update docs

* update create_comp_hl call locations

* enhancements+fixes

* fix broken hls in tabline

* Fix function color options not inheriting right colors

* some enhancements

* fix tests

* tweek docs

Co-authored-by: shadmansaleh <shadmansaleh@users.noreply.github.com>
2022-03-02 19:37:08 +06:00

126 lines
4.5 KiB
Lua

-- 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 retrive highlight groups', function()
local hl2 = { fg = '#aabbcc', bg = '#889977', reverse = 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 gui=reverse', hl2.fg, hl2.bg))
-- Can retrive entire highlight table
eq(utils.extract_highlight_colors('hl2'), hl2)
-- Can retrive specific parts of highlight
eq(utils.extract_highlight_colors('hl2', 'fg'), hl2.fg)
-- clear hl2
vim.cmd('hi clear hl2')
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 genarator', 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 genarator', 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)