| 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 neq = assert.are_not.same |
| 8 |
local assert_component = helpers.assert_component |
| 9 |
local build_component_opts = helpers.build_component_opts |
| 10 |
local stub = require('luassert.stub') |
| 11 |
|
| 12 |
describe('Component:', function() |
| 13 |
it('can select separators', function() |
| 14 |
local opts = build_component_opts() |
| 15 |
local comp = require('lualine.components.special.function_component')(opts) |
| 16 |
-- correct for lualine_c |
| 17 |
eq('', comp.options.separator) |
| 18 |
local opts2 = build_component_opts { self = { section = 'y' } } |
| 19 |
local comp2 = require('lualine.components.special.function_component')(opts2) |
| 20 |
-- correct for lualine_u |
| 21 |
eq('', comp2.options.separator) |
| 22 |
end) |
| 23 |
|
| 24 |
it('can provide unique identifier', function() |
| 25 |
local opts1 = build_component_opts() |
| 26 |
local comp1 = require('lualine.components.special.function_component')(opts1) |
| 27 |
local opts2 = build_component_opts() |
| 28 |
local comp2 = require('lualine.components.special.function_component')(opts2) |
| 29 |
neq(comp1.component_no, comp2.component_no) |
| 30 |
end) |
| 31 |
|
| 32 |
it('create option highlights', function() |
| 33 |
local color = { fg = '#224532', bg = '#892345' } |
| 34 |
local opts1 = build_component_opts { color = color } |
| 35 |
local hl = require('lualine.highlight') |
| 36 |
stub(hl, 'create_component_highlight_group') |
| 37 |
hl.create_component_highlight_group.returns('MyCompHl') |
| 38 |
local comp1 = require('lualine.components.special.function_component')(opts1) |
| 39 |
eq('MyCompHl', comp1.options.color_highlight) |
| 40 |
-- color highlight wan't in options when create_comp_hl was |
| 41 |
-- called so remove it before assert |
| 42 |
comp1.options.color_highlight = nil |
| 43 |
assert |
| 44 |
.stub(hl.create_component_highlight_group) |
| 45 |
.was_called_with(color, comp1.options.component_name, comp1.options, false) |
| 46 |
hl.create_component_highlight_group:revert() |
| 47 |
color = 'MyHl' |
| 48 |
local opts2 = build_component_opts { color = color } |
| 49 |
stub(hl, 'create_component_highlight_group') |
| 50 |
hl.create_component_highlight_group.returns('MyCompLinkedHl') |
| 51 |
local comp2 = require('lualine.components.special.function_component')(opts2) |
| 52 |
eq('MyCompLinkedHl', comp2.options.color_highlight) |
| 53 |
-- color highlight wan't in options when create_comp_hl was |
| 54 |
-- called so remove it before assert |
| 55 |
comp2.options.color_highlight = nil |
| 56 |
assert |
| 57 |
.stub(hl.create_component_highlight_group) |
| 58 |
.was_called_with(color, comp2.options.component_name, comp2.options, false) |
| 59 |
hl.create_component_highlight_group:revert() |
| 60 |
end) |
| 61 |
|
| 62 |
it('can draw', function() |
| 63 |
local opts = build_component_opts { |
| 64 |
component_separators = { left = '', right = '' }, |
| 65 |
padding = 0, |
| 66 |
} |
| 67 |
assert_component(nil, opts, 'test') |
| 68 |
end) |
| 69 |
|
| 70 |
it('can apply separators', function() |
| 71 |
local opts = build_component_opts { padding = 0 } |
| 72 |
assert_component(nil, opts, 'test') |
| 73 |
end) |
| 74 |
|
| 75 |
it('can apply default highlight', function() |
| 76 |
local opts = build_component_opts { padding = 0, hl = '%#My_highlight#' } |
| 77 |
assert_component(nil, opts, '%#My_highlight#test') |
| 78 |
opts = build_component_opts { |
| 79 |
function() |
| 80 |
return '%#Custom_hl#test' |
| 81 |
end, |
| 82 |
padding = 0, |
| 83 |
hl = '%#My_highlight#', |
| 84 |
} |
| 85 |
assert_component(nil, opts, '%#Custom_hl#test%#My_highlight#') |
| 86 |
opts = build_component_opts { |
| 87 |
function() |
| 88 |
return 'in middle%#Custom_hl#test' |
| 89 |
end, |
| 90 |
padding = 0, |
| 91 |
hl = '%#My_highlight#', |
| 92 |
} |
| 93 |
assert_component(nil, opts, '%#My_highlight#in middle%#Custom_hl#test%#My_highlight#') |
| 94 |
end) |
| 95 |
|
| 96 |
describe('Global options:', function() |
| 97 |
it('left_padding', function() |
| 98 |
local opts = build_component_opts { |
| 99 |
component_separators = { left = '', right = '' }, |
| 100 |
padding = { left = 5 }, |
| 101 |
} |
| 102 |
assert_component(nil, opts, ' test') |
| 103 |
end) |
| 104 |
|
| 105 |
it('right_padding', function() |
| 106 |
local opts = build_component_opts { |
| 107 |
component_separators = { left = '', right = '' }, |
| 108 |
padding = { right = 5 }, |
| 109 |
} |
| 110 |
assert_component(nil, opts, 'test ') |
| 111 |
end) |
| 112 |
|
| 113 |
it('padding', function() |
| 114 |
local opts = build_component_opts { |
| 115 |
component_separators = { left = '', right = '' }, |
| 116 |
padding = 5, |
| 117 |
} |
| 118 |
assert_component(nil, opts, ' test ') |
| 119 |
end) |
| 120 |
|
| 121 |
it('icon', function() |
| 122 |
local opts = build_component_opts { |
| 123 |
component_separators = { left = '', right = '' }, |
| 124 |
padding = 0, |
| 125 |
icon = '0', |
| 126 |
} |
| 127 |
assert_component(nil, opts, '0 test') |
| 128 |
end) |
| 129 |
|
| 130 |
it('icons_enabled', function() |
| 131 |
local opts = build_component_opts { |
| 132 |
component_separators = { left = '', right = '' }, |
| 133 |
padding = 0, |
| 134 |
icons_enabled = true, |
| 135 |
icon = '0', |
| 136 |
} |
| 137 |
assert_component(nil, opts, '0 test') |
| 138 |
local opts2 = build_component_opts { |
| 139 |
component_separators = { left = '', right = '' }, |
| 140 |
padding = 0, |
| 141 |
icons_enabled = false, |
| 142 |
icon = '0', |
| 143 |
} |
| 144 |
assert_component(nil, opts2, 'test') |
| 145 |
end) |
| 146 |
|
| 147 |
it('separator', function() |
| 148 |
local opts = build_component_opts { |
| 149 |
component_separators = { left = '', right = '' }, |
| 150 |
padding = 0, |
| 151 |
separator = '|', |
| 152 |
} |
| 153 |
assert_component(nil, opts, 'test|') |
| 154 |
end) |
| 155 |
|
| 156 |
it('fmt', function() |
| 157 |
local opts = build_component_opts { |
| 158 |
component_separators = { left = '', right = '' }, |
| 159 |
padding = 0, |
| 160 |
fmt = function(data) |
| 161 |
return data:sub(1, 1):upper() .. data:sub(2, #data) |
| 162 |
end, |
| 163 |
} |
| 164 |
assert_component(nil, opts, 'Test') |
| 165 |
end) |
| 166 |
|
| 167 |
it('cond', function() |
| 168 |
local opts = build_component_opts { |
| 169 |
component_separators = { left = '', right = '' }, |
| 170 |
padding = 0, |
| 171 |
cond = function() |
| 172 |
return true |
| 173 |
end, |
| 174 |
} |
| 175 |
assert_component(nil, opts, 'test') |
| 176 |
local opts2 = build_component_opts { |
| 177 |
component_separators = { left = '', right = '' }, |
| 178 |
padding = 0, |
| 179 |
cond = function() |
| 180 |
return false |
| 181 |
end, |
| 182 |
} |
| 183 |
assert_component(nil, opts2, '') |
| 184 |
end) |
| 185 |
|
| 186 |
it('color', function() |
| 187 |
local opts = build_component_opts { |
| 188 |
component_separators = { left = '', right = '' }, |
| 189 |
padding = 0, |
| 190 |
color = 'MyHl', |
| 191 |
} |
| 192 |
local comp = require('lualine.components.special.function_component')(opts) |
| 193 |
local custom_link_hl_name = 'lualine_c_' .. comp.options.component_name |
| 194 |
eq('%#' .. custom_link_hl_name .. '#test', comp:draw(opts.hl)) |
| 195 |
local opts2 = build_component_opts { |
| 196 |
component_separators = { left = '', right = '' }, |
| 197 |
padding = 0, |
| 198 |
color = { bg = '#230055', fg = '#223344' }, |
| 199 |
} |
| 200 |
local hl = require('lualine.highlight') |
| 201 |
stub(hl, 'component_format_highlight') |
| 202 |
hl.component_format_highlight.returns('%#MyCompHl#') |
| 203 |
local comp2 = require('lualine.components.special.function_component')(opts2) |
| 204 |
assert_component(nil, opts2, '%#MyCompHl#test') |
| 205 |
assert.stub(hl.component_format_highlight).was_called_with(comp2.options.color_highlight) |
| 206 |
hl.component_format_highlight:revert() |
| 207 |
end) |
| 208 |
|
| 209 |
it('draw_empty', function() |
| 210 |
local opts = build_component_opts { |
| 211 |
component_separators = { left = '', right = '' }, |
| 212 |
padding = 0, |
| 213 |
separator = '>', |
| 214 |
fmt = function() |
| 215 |
return '' |
| 216 |
end, |
| 217 |
draw_empty = true, |
| 218 |
} |
| 219 |
assert_component(nil, opts, '>') |
| 220 |
end) |
| 221 |
end) |
| 222 |
end) |
| 223 |
|
| 224 |
describe('Encoding component', function() |
| 225 |
it('works', function() |
| 226 |
local opts = build_component_opts { |
| 227 |
component_separators = { left = '', right = '' }, |
| 228 |
padding = 0, |
| 229 |
} |
| 230 |
local tmp_path = 'tmp.txt' |
| 231 |
local tmp_fp = io.open(tmp_path, 'w') |
| 232 |
tmp_fp:write('test file') |
| 233 |
tmp_fp:close() |
| 234 |
vim.cmd('e ' .. tmp_path) |
| 235 |
assert_component('encoding', opts, 'utf-8') |
| 236 |
vim.cmd('bd!') |
| 237 |
os.remove(tmp_path) |
| 238 |
end) |
| 239 |
it('works with BOM and default config', function() |
| 240 |
local opts = build_component_opts { |
| 241 |
component_separators = { left = '', right = '' }, |
| 242 |
padding = 0, |
| 243 |
} |
| 244 |
local tmp_path = 'tmp.txt' |
| 245 |
local tmp_fp = io.open(tmp_path, 'w') |
| 246 |
tmp_fp:write('test file') |
| 247 |
tmp_fp:close() |
| 248 |
vim.cmd('e ' .. tmp_path) |
| 249 |
vim.cmd('set bomb') |
| 250 |
assert_component('encoding', opts, 'utf-8') |
| 251 |
vim.cmd('bd!') |
| 252 |
os.remove(tmp_path) |
| 253 |
end) |
| 254 |
it('works with BOM and option enabled', function() |
| 255 |
local opts = build_component_opts { |
| 256 |
component_separators = { left = '', right = '' }, |
| 257 |
padding = 0, |
| 258 |
show_bomb = true, |
| 259 |
} |
| 260 |
local tmp_path = 'tmp.txt' |
| 261 |
local tmp_fp = io.open(tmp_path, 'w') |
| 262 |
tmp_fp:write('test file') |
| 263 |
tmp_fp:close() |
| 264 |
vim.cmd('e ' .. tmp_path) |
| 265 |
vim.cmd('set bomb') |
| 266 |
assert_component('encoding', opts, 'utf-8 [BOM]') |
| 267 |
vim.cmd('bd!') |
| 268 |
os.remove(tmp_path) |
| 269 |
end) |
| 270 |
end) |
| 271 |
|
| 272 |
describe('Fileformat component', function() |
| 273 |
it('works with icons', function() |
| 274 |
local opts = build_component_opts { |
| 275 |
component_separators = { left = '', right = '' }, |
| 276 |
padding = 0, |
| 277 |
} |
| 278 |
local fmt = vim.bo.fileformat |
| 279 |
vim.bo.fileformat = 'unix' |
| 280 |
assert_component('fileformat', opts, '') |
| 281 |
vim.bo.fileformat = 'dos' |
| 282 |
assert_component('fileformat', opts, '') |
| 283 |
vim.bo.fileformat = 'mac' |
| 284 |
assert_component('fileformat', opts, '') |
| 285 |
vim.bo.fileformat = fmt |
| 286 |
end) |
| 287 |
it('works without icons', function() |
| 288 |
local opts = build_component_opts { |
| 289 |
component_separators = { left = '', right = '' }, |
| 290 |
padding = 0, |
| 291 |
icons_enabled = false, |
| 292 |
} |
| 293 |
assert_component('fileformat', opts, vim.bo.fileformat) |
| 294 |
end) |
| 295 |
end) |
| 296 |
|
| 297 |
describe('Filetype component', function() |
| 298 |
local filetype |
| 299 |
|
| 300 |
before_each(function() |
| 301 |
filetype = vim.bo.filetype |
| 302 |
vim.bo.filetype = 'lua' |
| 303 |
end) |
| 304 |
|
| 305 |
after_each(function() |
| 306 |
vim.bo.filetype = filetype |
| 307 |
end) |
| 308 |
|
| 309 |
it('does not add icon when library unavailable', function() |
| 310 |
local old_require = _G.require |
| 311 |
function _G.require(...) |
| 312 |
if select(1, ...) == 'nvim-web-devicons' then |
| 313 |
error('Test case not suppose to have web-dev-icon 👀') |
| 314 |
end |
| 315 |
return old_require(...) |
| 316 |
end |
| 317 |
local opts = build_component_opts { |
| 318 |
component_separators = { left = '', right = '' }, |
| 319 |
padding = 0, |
| 320 |
} |
| 321 |
assert_component('filetype', opts, 'lua') |
| 322 |
_G.require = old_require |
| 323 |
end) |
| 324 |
|
| 325 |
it('colors nvim-web-devicons icons', function() |
| 326 |
vim.g.actual_curwin = tostring(vim.api.nvim_get_current_win()) |
| 327 |
stub(vim.fn, 'expand') |
| 328 |
vim.fn.expand.on_call_with('%:t').returns('test.lua') |
| 329 |
|
| 330 |
local hl = require('lualine.highlight') |
| 331 |
stub(hl, 'create_component_highlight_group') |
| 332 |
hl.create_component_highlight_group.returns { name = 'MyCompHl', no_mode = false, section = 'a' } |
| 333 |
stub(hl, 'format_highlight') |
| 334 |
hl.format_highlight.returns('%#lualine_c_normal#') |
| 335 |
|
| 336 |
local utils = require('lualine.utils.utils') |
| 337 |
stub(utils, 'extract_highlight_colors') |
| 338 |
utils.extract_highlight_colors.returns('#000') |
| 339 |
|
| 340 |
local devicons = require('nvim-web-devicons') |
| 341 |
stub(devicons, 'get_icon') |
| 342 |
devicons.get_icon.on_call_with('test.lua').returns('*', 'test_highlight_group') |
| 343 |
|
| 344 |
local opts = build_component_opts { |
| 345 |
component_separators = { left = '', right = '' }, |
| 346 |
hl = '%#lualine_c_normal#', |
| 347 |
padding = 0, |
| 348 |
colored = true, |
| 349 |
icon_only = false, |
| 350 |
} |
| 351 |
assert_component('filetype', opts, '%#MyCompHl_normal#* %#lualine_c_normal#lua%#lualine_c_normal#') |
| 352 |
assert.stub(devicons.get_icon).was_called_with('test.lua') |
| 353 |
assert.stub(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg') |
| 354 |
assert |
| 355 |
.stub(hl.create_component_highlight_group) |
| 356 |
.was_called_with({ fg = '#000' }, 'filetype_test_highlight_group', opts, false) |
| 357 |
assert.stub(vim.fn.expand).was_called_with('%:t') |
| 358 |
|
| 359 |
devicons.get_icon:revert() |
| 360 |
utils.extract_highlight_colors:revert() |
| 361 |
hl.create_component_highlight_group:revert() |
| 362 |
hl.format_highlight:revert() |
| 363 |
vim.fn.expand:revert() |
| 364 |
vim.g.actual_curwin = nil |
| 365 |
end) |
| 366 |
|
| 367 |
it("doesn't color when colored is false", function() |
| 368 |
stub(vim.fn, 'expand') |
| 369 |
vim.fn.expand.on_call_with('%:t').returns('test.lua') |
| 370 |
|
| 371 |
local hl = require('lualine.highlight') |
| 372 |
stub(hl, 'create_component_highlight_group') |
| 373 |
|
| 374 |
local utils = require('lualine.utils.utils') |
| 375 |
stub(utils, 'extract_highlight_colors') |
| 376 |
|
| 377 |
local devicons = require('nvim-web-devicons') |
| 378 |
stub(devicons, 'get_icon') |
| 379 |
devicons.get_icon.on_call_with('test.lua').returns('*', 'test_highlight_group') |
| 380 |
|
| 381 |
local opts = build_component_opts { |
| 382 |
component_separators = { left = '', right = '' }, |
| 383 |
padding = 0, |
| 384 |
colored = false, |
| 385 |
} |
| 386 |
assert_component('filetype', opts, '* lua') |
| 387 |
assert.stub(devicons.get_icon).was_called_with('test.lua') |
| 388 |
assert.stub(utils.extract_highlight_colors).was_not_called() |
| 389 |
assert.stub(hl.create_component_highlight_group).was_not_called() |
| 390 |
assert.stub(vim.fn.expand).was_called_with('%:t') |
| 391 |
|
| 392 |
devicons.get_icon:revert() |
| 393 |
utils.extract_highlight_colors:revert() |
| 394 |
hl.create_component_highlight_group:revert() |
| 395 |
vim.fn.expand:revert() |
| 396 |
end) |
| 397 |
|
| 398 |
it('displays only icon when icon_only is true', function() |
| 399 |
stub(vim.fn, 'expand') |
| 400 |
vim.fn.expand.on_call_with('%:t').returns('test.lua') |
| 401 |
|
| 402 |
local hl = require('lualine.highlight') |
| 403 |
stub(hl, 'create_component_highlight_group') |
| 404 |
|
| 405 |
local utils = require('lualine.utils.utils') |
| 406 |
stub(utils, 'extract_highlight_colors') |
| 407 |
|
| 408 |
local devicons = require('nvim-web-devicons') |
| 409 |
stub(devicons, 'get_icon') |
| 410 |
devicons.get_icon.on_call_with('test.lua').returns('*', 'test_highlight_group') |
| 411 |
|
| 412 |
local opts = build_component_opts { |
| 413 |
component_separators = { left = '', right = '' }, |
| 414 |
padding = 0, |
| 415 |
colored = false, |
| 416 |
icon_only = true, |
| 417 |
} |
| 418 |
assert_component('filetype', opts, '* ') |
| 419 |
assert.stub(devicons.get_icon).was_called_with('test.lua') |
| 420 |
assert.stub(utils.extract_highlight_colors).was_not_called() |
| 421 |
assert.stub(hl.create_component_highlight_group).was_not_called() |
| 422 |
assert.stub(vim.fn.expand).was_called_with('%:t') |
| 423 |
|
| 424 |
devicons.get_icon:revert() |
| 425 |
utils.extract_highlight_colors:revert() |
| 426 |
hl.create_component_highlight_group:revert() |
| 427 |
vim.fn.expand:revert() |
| 428 |
end) |
| 429 |
|
| 430 |
it('displays right aligned icon when icon.align is "right"', function() |
| 431 |
stub(vim.fn, 'expand') |
| 432 |
vim.fn.expand.on_call_with('%:t').returns('test.lua') |
| 433 |
|
| 434 |
local hl = require('lualine.highlight') |
| 435 |
stub(hl, 'create_component_highlight_group') |
| 436 |
|
| 437 |
local utils = require('lualine.utils.utils') |
| 438 |
stub(utils, 'extract_highlight_colors') |
| 439 |
|
| 440 |
local devicons = require('nvim-web-devicons') |
| 441 |
stub(devicons, 'get_icon') |
| 442 |
devicons.get_icon.on_call_with('test.lua').returns('*', 'test_highlight_group') |
| 443 |
|
| 444 |
local opts = build_component_opts { |
| 445 |
component_separators = { left = '', right = '' }, |
| 446 |
padding = 0, |
| 447 |
colored = false, |
| 448 |
icon_only = false, |
| 449 |
icon = { align = 'right' }, |
| 450 |
} |
| 451 |
assert_component('filetype', opts, 'lua * ') |
| 452 |
assert.stub(devicons.get_icon).was_called_with('test.lua') |
| 453 |
assert.stub(utils.extract_highlight_colors).was_not_called() |
| 454 |
assert.stub(hl.create_component_highlight_group).was_not_called() |
| 455 |
assert.stub(vim.fn.expand).was_called_with('%:t') |
| 456 |
|
| 457 |
devicons.get_icon:revert() |
| 458 |
utils.extract_highlight_colors:revert() |
| 459 |
hl.create_component_highlight_group:revert() |
| 460 |
vim.fn.expand:revert() |
| 461 |
end) |
| 462 |
|
| 463 |
it('uses filetype lookup when file has no extension', function() |
| 464 |
stub(vim.fn, 'expand') |
| 465 |
vim.fn.expand.on_call_with('%:t').returns('test') |
| 466 |
|
| 467 |
local hl = require('lualine.highlight') |
| 468 |
stub(hl, 'create_component_highlight_group') |
| 469 |
|
| 470 |
local utils = require('lualine.utils.utils') |
| 471 |
stub(utils, 'extract_highlight_colors') |
| 472 |
|
| 473 |
local devicons = require('nvim-web-devicons') |
| 474 |
stub(devicons, 'get_icon') |
| 475 |
devicons.get_icon.on_call_with('test').returns(nil) |
| 476 |
stub(devicons, 'get_icon_by_filetype') |
| 477 |
devicons.get_icon_by_filetype.on_call_with('lua').returns('*', 'test_highlight_group') |
| 478 |
|
| 479 |
local opts = build_component_opts { |
| 480 |
component_separators = { left = '', right = '' }, |
| 481 |
padding = 0, |
| 482 |
colored = false, |
| 483 |
icon_only = false, |
| 484 |
} |
| 485 |
assert_component('filetype', opts, '* lua') |
| 486 |
assert.stub(devicons.get_icon).was_called_with('test') |
| 487 |
assert.stub(devicons.get_icon_by_filetype).was_called_with('lua') |
| 488 |
assert.stub(utils.extract_highlight_colors).was_not_called() |
| 489 |
assert.stub(hl.create_component_highlight_group).was_not_called() |
| 490 |
assert.stub(vim.fn.expand).was_called_with('%:t') |
| 491 |
|
| 492 |
devicons.get_icon_by_filetype:revert() |
| 493 |
devicons.get_icon:revert() |
| 494 |
utils.extract_highlight_colors:revert() |
| 495 |
hl.create_component_highlight_group:revert() |
| 496 |
vim.fn.expand:revert() |
| 497 |
end) |
| 498 |
end) |
| 499 |
|
| 500 |
describe('Hostname component', function() |
| 501 |
it('works', function() |
| 502 |
stub(vim.loop, 'os_gethostname') |
| 503 |
vim.loop.os_gethostname.returns('localhost') |
| 504 |
local opts = build_component_opts { |
| 505 |
component_separators = { left = '', right = '' }, |
| 506 |
padding = 0, |
| 507 |
} |
| 508 |
assert_component('hostname', opts, 'localhost') |
| 509 |
vim.loop.os_gethostname:revert() |
| 510 |
end) |
| 511 |
end) |
| 512 |
|
| 513 |
describe('Location component', function() |
| 514 |
it('works', function() |
| 515 |
local opts = build_component_opts { |
| 516 |
component_separators = { left = '', right = '' }, |
| 517 |
padding = 0, |
| 518 |
} |
| 519 |
assert_component('location', opts, ' 1:1 ') |
| 520 |
vim.cmd('normal! 9o') |
| 521 |
assert_component('location', opts, ' 10:1 ') |
| 522 |
vim.api.nvim_win_set_cursor(0, { 5, 0 }) |
| 523 |
assert_component('location', opts, ' 5:1 ') |
| 524 |
-- test column number |
| 525 |
vim.cmd('normal! oTest') |
| 526 |
assert_component('location', opts, ' 6:4 ') |
| 527 |
-- test column number in line containing cyrillic symbols |
| 528 |
vim.cmd('normal! oТест') |
| 529 |
assert_component('location', opts, ' 7:4 ') |
| 530 |
vim.cmd('bdelete!') |
| 531 |
end) |
| 532 |
end) |
| 533 |
|
| 534 |
describe('Progress component', function() |
| 535 |
it('works', function() |
| 536 |
local opts = build_component_opts { |
| 537 |
component_separators = { left = '', right = '' }, |
| 538 |
padding = 0, |
| 539 |
} |
| 540 |
assert_component('progress', opts, 'Top') |
| 541 |
vim.cmd('normal! 9o') |
| 542 |
assert_component('progress', opts, 'Bot') |
| 543 |
vim.api.nvim_win_set_cursor(0, { 5, 0 }) |
| 544 |
assert_component('progress', opts, '50%%') |
| 545 |
vim.cmd('bdelete!') |
| 546 |
end) |
| 547 |
end) |
| 548 |
|
| 549 |
describe('Mode component', function() |
| 550 |
it('works', function() |
| 551 |
stub(vim.api, 'nvim_get_mode') |
| 552 |
vim.api.nvim_get_mode.returns { mode = 'n', blocking = false } |
| 553 |
local opts = build_component_opts { |
| 554 |
component_separators = { left = '', right = '' }, |
| 555 |
padding = 0, |
| 556 |
} |
| 557 |
assert_component('mode', opts, 'NORMAL') |
| 558 |
vim.api.nvim_get_mode:revert() |
| 559 |
end) |
| 560 |
end) |
| 561 |
|
| 562 |
describe('FileSize component', function() |
| 563 |
it('works', function() |
| 564 |
local opts = build_component_opts { |
| 565 |
component_separators = { left = '', right = '' }, |
| 566 |
padding = 0, |
| 567 |
} |
| 568 |
local fname = 'test-file.txt' |
| 569 |
local f = io.open(fname, 'w') |
| 570 |
f:write(string.rep('........................................\n', 200)) |
| 571 |
f:close() |
| 572 |
vim.cmd(':edit ' .. fname) |
| 573 |
assert_component('filesize', opts, '8.0k') |
| 574 |
vim.cmd(':bdelete!') |
| 575 |
os.remove(fname) |
| 576 |
end) |
| 577 |
end) |
| 578 |
|
| 579 |
describe('Filename component', function() |
| 580 |
it('works', function() |
| 581 |
local opts = build_component_opts { |
| 582 |
component_separators = { left = '', right = '' }, |
| 583 |
padding = 0, |
| 584 |
file_status = false, |
| 585 |
path = 0, |
| 586 |
} |
| 587 |
vim.cmd(':e test-file.txt') |
| 588 |
assert_component('filename', opts, 'test-file.txt') |
| 589 |
vim.cmd(':bdelete!') |
| 590 |
end) |
| 591 |
|
| 592 |
it('can show file_status', function() |
| 593 |
local opts = build_component_opts { |
| 594 |
component_separators = { left = '', right = '' }, |
| 595 |
padding = 0, |
| 596 |
file_status = true, |
| 597 |
path = 0, |
| 598 |
} |
| 599 |
vim.cmd(':e test-file.txt') |
| 600 |
vim.bo.modified = false |
| 601 |
assert_component('filename', opts, 'test-file.txt') |
| 602 |
vim.bo.modified = true |
| 603 |
assert_component('filename', opts, 'test-file.txt [+]') |
| 604 |
vim.bo.ro = true |
| 605 |
assert_component('filename', opts, 'test-file.txt [+][-]') |
| 606 |
vim.bo.modified = false |
| 607 |
assert_component('filename', opts, 'test-file.txt [-]') |
| 608 |
vim.cmd(':bdelete!') |
| 609 |
end) |
| 610 |
|
| 611 |
it('can show new_file_status', function() |
| 612 |
local opts = build_component_opts { |
| 613 |
component_separators = { left = '', right = '' }, |
| 614 |
padding = 0, |
| 615 |
newfile_status = true, |
| 616 |
path = 0, |
| 617 |
} |
| 618 |
vim.cmd(':e new-file.txt') |
| 619 |
assert_component('filename', opts, 'new-file.txt [New]') |
| 620 |
vim.bo.modified = true |
| 621 |
assert_component('filename', opts, 'new-file.txt [+][New]') |
| 622 |
vim.cmd(':bdelete!') |
| 623 |
end) |
| 624 |
|
| 625 |
it('can show relative path', function() |
| 626 |
local opts = build_component_opts { |
| 627 |
component_separators = { left = '', right = '' }, |
| 628 |
padding = 0, |
| 629 |
file_status = false, |
| 630 |
path = 1, |
| 631 |
} |
| 632 |
vim.cmd(':e test-file.txt') |
| 633 |
assert_component('filename', opts, vim.fn.expand('%:~:.')) |
| 634 |
vim.cmd(':bdelete!') |
| 635 |
end) |
| 636 |
|
| 637 |
it('can show full path', function() |
| 638 |
local opts = build_component_opts { |
| 639 |
component_separators = { left = '', right = '' }, |
| 640 |
padding = 0, |
| 641 |
file_status = false, |
| 642 |
path = 2, |
| 643 |
shorting_target = 0, |
| 644 |
} |
| 645 |
vim.cmd(':e test-file.txt') |
| 646 |
assert_component('filename', opts, vim.fn.expand('%:p')) |
| 647 |
vim.cmd(':bdelete!') |
| 648 |
end) |
| 649 |
|
| 650 |
it('shortens path', function() |
| 651 |
stub(vim.fn, 'expand') |
| 652 |
vim.fn.expand.on_call_with('%:p').returns('/home/foobar/test/test.lua') |
| 653 |
stub(vim.fn, 'winwidth') |
| 654 |
vim.fn.winwidth.on_call_with(0).returns(100) |
| 655 |
|
| 656 |
local opts = build_component_opts { |
| 657 |
component_separators = { left = '', right = '' }, |
| 658 |
padding = 0, |
| 659 |
file_status = false, |
| 660 |
path = 2, |
| 661 |
shorting_target = 90, |
| 662 |
} |
| 663 |
vim.cmd(':e test-file.txt') |
| 664 |
assert_component('filename', opts, '/h/f/t/test.lua') |
| 665 |
|
| 666 |
vim.cmd(':bdelete!') |
| 667 |
vim.fn.winwidth:revert() |
| 668 |
vim.fn.expand:revert() |
| 669 |
end) |
| 670 |
|
| 671 |
it('shortens path with tilde', function() |
| 672 |
stub(vim.fn, 'expand') |
| 673 |
vim.fn.expand.on_call_with('%:p:~').returns('~/test/test.lua') |
| 674 |
stub(vim.fn, 'winwidth') |
| 675 |
vim.fn.winwidth.on_call_with(0).returns(100) |
| 676 |
|
| 677 |
local opts = build_component_opts { |
| 678 |
component_separators = { left = '', right = '' }, |
| 679 |
padding = 0, |
| 680 |
file_status = false, |
| 681 |
path = 3, |
| 682 |
shorting_target = 90, |
| 683 |
} |
| 684 |
vim.cmd(':e test-file.txt') |
| 685 |
assert_component('filename', opts, '~/t/test.lua') |
| 686 |
|
| 687 |
vim.cmd(':bdelete!') |
| 688 |
vim.fn.winwidth:revert() |
| 689 |
vim.fn.expand:revert() |
| 690 |
end) |
| 691 |
|
| 692 |
it('shortens path with hidden directory', function() |
| 693 |
stub(vim.fn, 'expand') |
| 694 |
vim.fn.expand.on_call_with('%:p').returns('/home/foobar/.test/test.lua') |
| 695 |
stub(vim.fn, 'winwidth') |
| 696 |
vim.fn.winwidth.on_call_with(0).returns(100) |
| 697 |
|
| 698 |
local opts = build_component_opts { |
| 699 |
component_separators = { left = '', right = '' }, |
| 700 |
padding = 0, |
| 701 |
file_status = false, |
| 702 |
path = 2, |
| 703 |
shorting_target = 90, |
| 704 |
} |
| 705 |
vim.cmd(':e test-file.txt') |
| 706 |
assert_component('filename', opts, '/h/f/.t/test.lua') |
| 707 |
|
| 708 |
vim.cmd(':bdelete!') |
| 709 |
vim.fn.winwidth:revert() |
| 710 |
vim.fn.expand:revert() |
| 711 |
end) |
| 712 |
|
| 713 |
it('shortens path with %', function() |
| 714 |
stub(vim.fn, 'expand') |
| 715 |
vim.fn.expand.on_call_with('%:p').returns('%dir1/%dir2/%actual_%file') |
| 716 |
stub(vim.fn, 'winwidth') |
| 717 |
vim.fn.winwidth.on_call_with(0).returns(100) |
| 718 |
|
| 719 |
local opts = build_component_opts { |
| 720 |
component_separators = { left = '', right = '' }, |
| 721 |
padding = 0, |
| 722 |
file_status = false, |
| 723 |
path = 2, |
| 724 |
shorting_target = 90, |
| 725 |
} |
| 726 |
vim.cmd(':e test-file.txt') |
| 727 |
assert_component('filename', opts, '%%/%%/%%actual_%%file') |
| 728 |
|
| 729 |
vim.cmd(':bdelete!') |
| 730 |
vim.fn.winwidth:revert() |
| 731 |
vim.fn.expand:revert() |
| 732 |
end) |
| 733 |
end) |
| 734 |
|
| 735 |
describe('vim option & variable component', function() |
| 736 |
local opts = build_component_opts { |
| 737 |
component_separators = { left = '', right = '' }, |
| 738 |
padding = 0, |
| 739 |
} |
| 740 |
|
| 741 |
local function assert_vim_var_component(name, options, result) |
| 742 |
options[1] = name |
| 743 |
assert_component('special.vim_var_component', options, result) |
| 744 |
opts[1] = nil |
| 745 |
end |
| 746 |
it('works with variable', function() |
| 747 |
assert_vim_var_component('g:gvar', opts, '') |
| 748 |
vim.g.gvar = 'var1' |
| 749 |
assert_vim_var_component('g:gvar', opts, 'var1') |
| 750 |
vim.g.gvar = 'var2' |
| 751 |
assert_vim_var_component('g:gvar', opts, 'var2') |
| 752 |
vim.b.gvar = 'bvar1' |
| 753 |
assert_vim_var_component('b:gvar', opts, 'bvar1') |
| 754 |
vim.w.gvar = 'wvar1' |
| 755 |
assert_vim_var_component('w:gvar', opts, 'wvar1') |
| 756 |
end) |
| 757 |
it('can index dictionaries', function() |
| 758 |
vim.g.gvar = { a = { b = 'var-value' } } |
| 759 |
assert_vim_var_component('g:gvar.a.b', opts, 'var-value') |
| 760 |
end) |
| 761 |
it('works with options', function() |
| 762 |
local old_number = vim.wo.number |
| 763 |
vim.wo.number = false |
| 764 |
assert_vim_var_component('wo:number', opts, 'false') |
| 765 |
vim.wo.number = old_number |
| 766 |
local old_tw = vim.go.tw |
| 767 |
vim.go.tw = 80 |
| 768 |
assert_vim_var_component('go:tw', opts, '80') |
| 769 |
vim.go.tw = old_tw |
| 770 |
end) |
| 771 |
end) |
| 772 |
|
| 773 |
describe('Vim option & variable component', function() |
| 774 |
local opts = build_component_opts { |
| 775 |
component_separators = { left = '', right = '' }, |
| 776 |
padding = 0, |
| 777 |
} |
| 778 |
|
| 779 |
local function assert_vim_var_component(name, options, result) |
| 780 |
options[1] = name |
| 781 |
assert_component('special.eval_func_component', options, result) |
| 782 |
opts[1] = nil |
| 783 |
end |
| 784 |
|
| 785 |
it('works with vim function', function() |
| 786 |
vim.cmd([[ |
| 787 |
func! TestFunction() abort |
| 788 |
return "TestVimFunction" |
| 789 |
endf |
| 790 |
]]) |
| 791 |
assert_vim_var_component('TestFunction', opts, 'TestVimFunction') |
| 792 |
vim.cmd('delfunction TestFunction') |
| 793 |
end) |
| 794 |
|
| 795 |
it('works with lua expression', function() |
| 796 |
_G.TestFunction = function() |
| 797 |
return 'TestLuaFunction' |
| 798 |
end |
| 799 |
assert_vim_var_component('TestFunction()', opts, 'TestLuaFunction') |
| 800 |
_G.TestFunction = nil |
| 801 |
end) |
| 802 |
end) |
| 803 |
|
| 804 |
describe('Branch component', function() |
| 805 |
-- these tests are broken in wsl will look at them later |
| 806 |
if vim.fn.has('wsl') == 1 then |
| 807 |
return |
| 808 |
end |
| 809 |
local tmpdir |
| 810 |
local file |
| 811 |
local git = function(...) |
| 812 |
return vim.fn.system( |
| 813 |
"git -c user.name='asdf' -c user.email='[email protected]' -C " .. tmpdir .. ' ' .. string.format(...) |
| 814 |
) |
| 815 |
end |
| 816 |
local assert_comp_ins = helpers.assert_component_instance |
| 817 |
|
| 818 |
before_each(function() |
| 819 |
tmpdir = os.tmpname() |
| 820 |
os.remove(tmpdir) |
| 821 |
file = tmpdir .. '/test.txt' |
| 822 |
vim.fn.mkdir(tmpdir, 'p') |
| 823 |
git('init -b test_branch') |
| 824 |
vim.cmd([[aug lualine |
| 825 |
au! |
| 826 |
aug END |
| 827 |
]]) |
| 828 |
end) |
| 829 |
|
| 830 |
after_each(function() |
| 831 |
os.remove(tmpdir) |
| 832 |
end) |
| 833 |
|
| 834 |
it('works with regular branches', function() |
| 835 |
local opts = build_component_opts { |
| 836 |
component_separators = { left = '', right = '' }, |
| 837 |
padding = 0, |
| 838 |
} |
| 839 |
local branch_comp = helpers.init_component('branch', opts) |
| 840 |
vim.cmd('e ' .. file) |
| 841 |
assert_comp_ins(branch_comp, ' test_branch') |
| 842 |
git('checkout -b test_branch2') |
| 843 |
vim.cmd('e k') |
| 844 |
vim.cmd('bd') |
| 845 |
vim.cmd('e ' .. file) |
| 846 |
opts.icons_enabled = false |
| 847 |
assert_comp_ins(branch_comp, 'test_branch2') |
| 848 |
end) |
| 849 |
|
| 850 |
it('works in detached head mode', function() |
| 851 |
local opts = build_component_opts { |
| 852 |
component_separators = { left = '', right = '' }, |
| 853 |
icons_enabled = false, |
| 854 |
padding = 0, |
| 855 |
} |
| 856 |
git('checkout -b test_branch2') |
| 857 |
git('commit --allow-empty -m "test commit1"') |
| 858 |
git('commit --allow-empty -m "test commit2"') |
| 859 |
git('commit --allow-empty -m "test commit3"') |
| 860 |
git('checkout HEAD~1') |
| 861 |
vim.cmd('e ' .. file) |
| 862 |
local rev = git('rev-parse --short=6 HEAD'):sub(1, 6) |
| 863 |
assert_component('branch', opts, rev) |
| 864 |
end) |
| 865 |
end) |
| 866 |
|
| 867 |
describe('lsp_status component', function() |
| 868 |
local assert_comp_ins = helpers.assert_component_instance |
| 869 |
local tmpdir |
| 870 |
local file |
| 871 |
local opts = build_component_opts { |
| 872 |
component_separators = { left = '', right = '' }, |
| 873 |
padding = 0, |
| 874 |
ignore_lsp = { 'null-ls' }, |
| 875 |
} |
| 876 |
local lsp_status_comp |
| 877 |
|
| 878 |
before_each(function() |
| 879 |
require('lualine').setup {} |
| 880 |
tmpdir = os.tmpname() |
| 881 |
file = os.tmpname() .. '/test.lua' |
| 882 |
vim.cmd([[ |
| 883 |
augroup lualine |
| 884 |
autocmd! |
| 885 |
augroup END |
| 886 |
]]) |
| 887 |
lsp_status_comp = helpers.init_component('lsp_status', opts) |
| 888 |
end) |
| 889 |
|
| 890 |
after_each(function() |
| 891 |
os.remove(tmpdir) |
| 892 |
end) |
| 893 |
|
| 894 |
it('is empty when no LSP', function() |
| 895 |
vim.cmd('edit ' .. file) |
| 896 |
stub(vim.lsp, 'get_clients') |
| 897 |
vim.lsp.get_clients.on_call_with({ bufnr = vim.api.nvim_get_current_buf() }).returns {} |
| 898 |
|
| 899 |
assert_comp_ins(lsp_status_comp, '') |
| 900 |
end) |
| 901 |
|
| 902 |
it('is empty when LSP is ignored', function() |
| 903 |
vim.cmd('edit ' .. file) |
| 904 |
stub(vim.lsp, 'get_clients') |
| 905 |
vim.lsp.get_clients.on_call_with({ bufnr = vim.api.nvim_get_current_buf() }).returns { |
| 906 |
{ id = 2, name = 'null-ls' }, |
| 907 |
} |
| 908 |
|
| 909 |
assert_comp_ins(lsp_status_comp, '') |
| 910 |
end) |
| 911 |
|
| 912 |
it('shows LSP name and icon when attached', function() |
| 913 |
vim.cmd('edit ' .. file) |
| 914 |
stub(vim.lsp, 'get_clients') |
| 915 |
vim.lsp.get_clients.on_call_with({ bufnr = vim.api.nvim_get_current_buf() }).returns { { id = 2, name = 'lua_ls' } } |
| 916 |
|
| 917 |
vim.cmd('edit ' .. file) |
| 918 |
|
| 919 |
assert_comp_ins(lsp_status_comp, ' lua_ls') |
| 920 |
end) |
| 921 |
|
| 922 |
it('shows LSP name and icon in older nvim version', function() |
| 923 |
vim.cmd('edit ' .. file) |
| 924 |
vim.lsp.get_clients = nil |
| 925 |
stub(vim.lsp, 'get_active_clients') |
| 926 |
---@diagnostic disable-next-line: deprecated |
| 927 |
vim.lsp.get_active_clients |
| 928 |
.on_call_with({ bufnr = vim.api.nvim_get_current_buf() }) |
| 929 |
.returns { { id = 2, name = 'lua_ls' } } |
| 930 |
|
| 931 |
assert_comp_ins(lsp_status_comp, ' lua_ls') |
| 932 |
end) |
| 933 |
|
| 934 |
it('shows LSP progress when supported', function() |
| 935 |
vim.cmd('edit ' .. file) |
| 936 |
stub(vim.lsp, 'get_clients') |
| 937 |
vim.lsp.get_clients.on_call_with({ bufnr = vim.api.nvim_get_current_buf() }).returns { { id = 2, name = 'lua_ls' } } |
| 938 |
if vim.uv and vim.uv.hrtime then |
| 939 |
stub(vim.uv, 'hrtime') |
| 940 |
vim.uv.hrtime.on_call_with().returns(12 * 1e6 * 80) |
| 941 |
elseif vim.loop and vim.loop.hrtime then |
| 942 |
stub(vim.loop, 'hrtime') |
| 943 |
vim.loop.hrtime.on_call_with().returns(12 * 1e6 * 80) |
| 944 |
end |
| 945 |
|
| 946 |
local ok = pcall(vim.api.nvim_exec_autocmds, 'LspProgress', { |
| 947 |
data = { client_id = 2, params = { value = { kind = 'begin' } } }, |
| 948 |
}) |
| 949 |
|
| 950 |
-- Skip assertion if LSP progress updates are not supported by the current `nvim` version. |
| 951 |
if ok then |
| 952 |
-- Use spinner symbol 12 (time) % 10 (symbol table size) = 2. |
| 953 |
assert_comp_ins(lsp_status_comp, ' lua_ls ⠹') |
| 954 |
end |
| 955 |
end) |
| 956 |
|
| 957 |
it('shows LSP done when supported', function() |
| 958 |
vim.cmd('edit ' .. file) |
| 959 |
stub(vim.lsp, 'get_clients') |
| 960 |
vim.lsp.get_clients.on_call_with({ bufnr = vim.api.nvim_get_current_buf() }).returns { { id = 2, name = 'lua_ls' } } |
| 961 |
|
| 962 |
local ok = pcall(vim.api.nvim_exec_autocmds, 'LspProgress', { |
| 963 |
data = { client_id = 2, params = { value = { kind = 'begin' } } }, |
| 964 |
}) and pcall(vim.api.nvim_exec_autocmds, 'LspProgress', { |
| 965 |
data = { client_id = 2, params = { value = { kind = 'end' } } }, |
| 966 |
}) |
| 967 |
|
| 968 |
-- Skip assertion if LSP progress updates are not supported by the current `nvim` version. |
| 969 |
if ok then |
| 970 |
assert_comp_ins(lsp_status_comp, ' lua_ls ✓') |
| 971 |
end |
| 972 |
end) |
| 973 |
|
| 974 |
it('shows LSP done when supported and no begin', function() |
| 975 |
vim.cmd('edit ' .. file) |
| 976 |
stub(vim.lsp, 'get_clients') |
| 977 |
vim.lsp.get_clients.on_call_with({ bufnr = vim.api.nvim_get_current_buf() }).returns { { id = 2, name = 'lua_ls' } } |
| 978 |
|
| 979 |
local ok = pcall(vim.api.nvim_exec_autocmds, 'LspProgress', { |
| 980 |
data = { client_id = 2, params = { value = { kind = 'end' } } }, |
| 981 |
}) |
| 982 |
|
| 983 |
-- Skip assertion if LSP progress updates are not supported by the current `nvim` version. |
| 984 |
if ok then |
| 985 |
assert_comp_ins(lsp_status_comp, ' lua_ls ✓') |
| 986 |
end |
| 987 |
end) |
| 988 |
|
| 989 |
it('does not add unnecessary space separator when LSP progress symbol is empty', function() |
| 990 |
local opts_no_done = opts |
| 991 |
opts_no_done.symbols = { done = '' } |
| 992 |
local lsp_status_comp_no_done = helpers.init_component('lsp_status', opts) |
| 993 |
|
| 994 |
vim.cmd('edit ' .. file) |
| 995 |
stub(vim.lsp, 'get_clients') |
| 996 |
vim.lsp.get_clients.on_call_with({ bufnr = vim.api.nvim_get_current_buf() }).returns { { id = 2, name = 'lua_ls' } } |
| 997 |
|
| 998 |
local ok = pcall(vim.api.nvim_exec_autocmds, 'LspProgress', { |
| 999 |
data = { client_id = 2, params = { value = { kind = 'begin' } } }, |
| 1000 |
}) and pcall(vim.api.nvim_exec_autocmds, 'LspProgress', { |
| 1001 |
data = { client_id = 2, params = { value = { kind = 'end' } } }, |
| 1002 |
}) |
| 1003 |
|
| 1004 |
-- Skip assertion if LSP progress updates are not supported by the current `nvim` version. |
| 1005 |
if ok then |
| 1006 |
assert_comp_ins(lsp_status_comp_no_done, ' lua_ls') |
| 1007 |
end |
| 1008 |
end) |
| 1009 |
end) |