| 1 |
-- Copyright (c) 2020-2021 shadmansaleh |
| 2 |
-- MIT license, see LICENSE for more details. |
| 3 |
|
| 4 |
local helpers = require('tests.helpers') |
| 5 |
|
| 6 |
local eq = assert.are.same |
| 7 |
local build_component_opts = helpers.build_component_opts |
| 8 |
local stub = require('luassert.stub') |
| 9 |
|
| 10 |
describe('Utils', function() |
| 11 |
local utils = require('lualine.utils.utils') |
| 12 |
|
| 13 |
it('can retrieve highlight groups', function() |
| 14 |
local hl2 = { fg = '#aabbcc', bg = '#889977', sp = '#997788', reverse = true, undercurl = true } |
| 15 |
-- handles non existing hl groups |
| 16 |
eq(utils.extract_highlight_colors('hl2'), nil) |
| 17 |
-- create highlight |
| 18 |
vim.cmd(string.format('hi hl2 guifg=%s guibg=%s guisp=%s gui=reverse,undercurl', hl2.fg, hl2.bg, hl2.sp)) |
| 19 |
-- Can retrieve entire highlight table |
| 20 |
eq(utils.extract_highlight_colors('hl2'), hl2) |
| 21 |
-- Can retrieve specific parts of highlight |
| 22 |
eq(utils.extract_highlight_colors('hl2', 'fg'), hl2.fg) |
| 23 |
-- clear hl2 |
| 24 |
vim.cmd('hi clear hl2') |
| 25 |
end) |
| 26 |
|
| 27 |
it('can extract individual highlight color', function() |
| 28 |
local fg_clr = '#aabbcc' |
| 29 |
local bg_clr = '#889977' |
| 30 |
local sp_clr = '#997788' |
| 31 |
local def_clr = '#ff0000' |
| 32 |
local hl_std = { fg = fg_clr, bg = bg_clr } |
| 33 |
local hl_rvs = { fg = fg_clr, bg = bg_clr, reverse = true } |
| 34 |
local hl_ul = { sp = sp_clr, undercurl = true } |
| 35 |
local hl_ul_rvs = { fg = fg_clr, bg = bg_clr, sp = sp_clr, reverse = true, undercurl = true } |
| 36 |
-- create highlights |
| 37 |
vim.cmd(string.format('hi hl_std guifg=%s guibg=%s', hl_std.fg, hl_std.bg)) |
| 38 |
vim.cmd(string.format('hi hl_rvs guifg=%s guibg=%s gui=reverse', hl_rvs.fg, hl_rvs.bg)) |
| 39 |
vim.cmd(string.format('hi hl_ul guisp=%s gui=undercurl', hl_ul.sp)) |
| 40 |
vim.cmd( |
| 41 |
string.format( |
| 42 |
'hi hl_ul_rvs guifg=%s guibg=%s guisp=%s gui=reverse,undercurl', |
| 43 |
hl_ul_rvs.fg, |
| 44 |
hl_ul_rvs.bg, |
| 45 |
hl_ul_rvs.sp |
| 46 |
) |
| 47 |
) |
| 48 |
-- Can extract color from primary highlight group |
| 49 |
eq(utils.extract_color_from_hllist('fg', { 'hl_std', 'hl_ul' }, def_clr), fg_clr) |
| 50 |
-- Can extract color from fallback highlight group |
| 51 |
eq(utils.extract_color_from_hllist('fg', { 'hl_noexist', 'hl_std' }, def_clr), fg_clr) |
| 52 |
-- Can fall back to default color on nonexistent color |
| 53 |
eq(utils.extract_color_from_hllist('fg', { 'hl_ul' }, def_clr), def_clr) |
| 54 |
-- Can fall back to default color on nonexistent highlight group |
| 55 |
eq(utils.extract_color_from_hllist('fg', { 'hl_noexist' }, def_clr), def_clr) |
| 56 |
-- Can extract fallback color |
| 57 |
eq(utils.extract_color_from_hllist({ 'fg', 'sp' }, { 'hl_ul' }, def_clr), sp_clr) |
| 58 |
-- Can extract reverse color |
| 59 |
eq(utils.extract_color_from_hllist('fg', { 'hl_rvs' }, def_clr), bg_clr) |
| 60 |
-- Can extract fallback reverse color |
| 61 |
eq(utils.extract_color_from_hllist({ 'sp', 'fg' }, { 'hl_rvs' }, def_clr), bg_clr) |
| 62 |
-- clear highlights |
| 63 |
vim.cmd('hi clear hl_std') |
| 64 |
vim.cmd('hi clear hl_rvs') |
| 65 |
vim.cmd('hi clear hl_ul') |
| 66 |
vim.cmd('hi clear hl_ul_rvs') |
| 67 |
end) |
| 68 |
|
| 69 |
it('can shrink list with holes', function() |
| 70 |
local list_with_holes = { |
| 71 |
'2', |
| 72 |
'4', |
| 73 |
'6', |
| 74 |
nil, |
| 75 |
'43', |
| 76 |
nil, |
| 77 |
'2', |
| 78 |
'', |
| 79 |
'a', |
| 80 |
'', |
| 81 |
'b', |
| 82 |
' ', |
| 83 |
} |
| 84 |
local list_without_holes = { '2', '4', '6', '43', '2', 'a', 'b', ' ' } |
| 85 |
eq(utils.list_shrink(list_with_holes), list_without_holes) |
| 86 |
end) |
| 87 |
end) |
| 88 |
|
| 89 |
describe('Cterm generator', function() |
| 90 |
local cterm = require('lualine.utils.color_utils') |
| 91 |
|
| 92 |
it('can convert rgb to cterm', function() |
| 93 |
local colors = { ['#112233'] = 235, ['#7928ae'] = 97, ['#017bdc'] = 68 } |
| 94 |
for rgb, ct in pairs(colors) do |
| 95 |
eq(cterm.rgb2cterm(rgb), tostring(ct)) |
| 96 |
end |
| 97 |
end) |
| 98 |
end) |
| 99 |
|
| 100 |
describe('Section generator', function() |
| 101 |
local hl = require('lualine.highlight') |
| 102 |
stub(hl, 'format_highlight') |
| 103 |
hl.format_highlight.returns('%#lualine_c_normal#') |
| 104 |
|
| 105 |
local sec = require('lualine.utils.section') |
| 106 |
it('can draw', function() |
| 107 |
local opts = build_component_opts { section_separators = { left = '', right = '' } } |
| 108 |
local section = { |
| 109 |
require('lualine.components.special.function_component')(opts), |
| 110 |
require('lualine.components.special.function_component')(opts), |
| 111 |
} |
| 112 |
eq('%#lualine_c_normal# test %#lualine_c_normal# test ', sec.draw_section(section, 'c', true)) |
| 113 |
|
| 114 |
hl.format_highlight:revert() |
| 115 |
end) |
| 116 |
|
| 117 |
it('can remove separators from component with custom colors', function() |
| 118 |
stub(hl, 'format_highlight') |
| 119 |
stub(hl, 'get_lualine_hl') |
| 120 |
hl.format_highlight.returns('%#lualine_MySection_normal#') |
| 121 |
hl.get_lualine_hl.returns { fg = '#000000', bg = '#ffffff' } |
| 122 |
|
| 123 |
vim.g.actual_curwin = tostring(vim.api.nvim_get_current_win()) |
| 124 |
local opts = build_component_opts { section_separators = { left = '', right = '' } } |
| 125 |
local opts_colored = build_component_opts { color = 'MyColor' } |
| 126 |
local opts_colored2 = build_component_opts { |
| 127 |
color = { bg = '#223344' }, |
| 128 |
section_separators = { left = '', right = '' }, |
| 129 |
} |
| 130 |
local opts_colored3 = build_component_opts { |
| 131 |
color = { fg = '#223344' }, |
| 132 |
section_separators = { left = '', right = '' }, |
| 133 |
} |
| 134 |
require('lualine.highlight').create_highlight_groups(require('lualine.themes.gruvbox')) |
| 135 |
local section = { |
| 136 |
require('lualine.components.special.function_component')(opts), |
| 137 |
require('lualine.components.special.function_component')(opts_colored), |
| 138 |
require('lualine.components.special.function_component')(opts), |
| 139 |
} |
| 140 |
local highlight_name2 = 'lualine_c_' .. section[2].options.component_name |
| 141 |
-- Removes separator on string color |
| 142 |
eq( |
| 143 |
'%#lualine_MySection_normal# test %#' .. highlight_name2 .. '#' .. ' test %#lualine_MySection_normal# test ', |
| 144 |
sec.draw_section(section, 'MySection') |
| 145 |
) |
| 146 |
section[2] = require('lualine.components.special.function_component')(opts_colored2) |
| 147 |
local highlight_name = '%#lualine_c_' .. section[2].options.component_name .. '_normal#' |
| 148 |
-- Removes separator on color with bg |
| 149 |
eq( |
| 150 |
'%#lualine_MySection_normal# test ' .. highlight_name .. ' test %#lualine_MySection_normal# test ', |
| 151 |
sec.draw_section(section, 'MySection') |
| 152 |
) |
| 153 |
section[2] = require('lualine.components.special.function_component')(opts_colored3) |
| 154 |
highlight_name2 = '%#lualine_c_' .. section[2].options.component_name .. '_normal#' |
| 155 |
-- Doesn't remove separator on color without bg |
| 156 |
eq( |
| 157 |
'%#lualine_MySection_normal# test ' |
| 158 |
.. highlight_name2 |
| 159 |
.. ' test %#lualine_MySection_normal#%#lualine_MySection_normal# test ', |
| 160 |
sec.draw_section(section, 'MySection') |
| 161 |
) |
| 162 |
vim.g.actual_curwin = nil |
| 163 |
|
| 164 |
hl.format_highlight:revert() |
| 165 |
hl.get_lualine_hl:revert() |
| 166 |
end) |
| 167 |
end) |