robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
18.0 KiB615 linesraw
1local t = require('test.unit.testutil')
2local describe = t.describe
3local itp = t.gen_itp(t.it)
4
5local to_cstr = t.to_cstr
6local get_str = t.ffi.string
7local eq = t.eq
8local NULL = t.NULL
9
10local buffer = t.cimport('./src/nvim/buffer.h')
11local globals = t.cimport('./src/nvim/globals.h')
12local stl = t.cimport('./src/nvim/statusline.h')
13local grid = t.cimport('./src/nvim/grid.h')
14
15describe('build_stl_str_hl', function()
16  local buffer_byte_size = 120
17  local STL_INITIAL_ITEMS = 20
18  local output_buffer = ''
19
20  -- This function builds the statusline
21  --
22  -- @param arg Optional arguments are:
23  --    .pat The statusline format string
24  --    .fillchar The fill character used in the statusline
25  --    .maximum_cell_count The number of cells available in the statusline
26  local function build_stl_str_hl(arg)
27    output_buffer = to_cstr(string.rep(' ', buffer_byte_size))
28
29    local pat = arg.pat or ''
30    local fillchar = arg.fillchar or ' '
31    local maximum_cell_count = arg.maximum_cell_count or buffer_byte_size
32    if type(fillchar) == type('') then
33      fillchar = grid.schar_from_str(fillchar)
34    end
35
36    return stl.build_stl_str_hl(
37      globals.curwin,
38      t.ffi.new('CharBuf', { output_buffer, buffer_byte_size }),
39      to_cstr(pat),
40      -1,
41      0,
42      fillchar,
43      maximum_cell_count,
44      NULL,
45      NULL,
46      NULL,
47      NULL
48    )
49  end
50
51  -- Use this function to simplify testing the comparison between
52  --  the format string and the resulting statusline.
53  --
54  -- @param description The description of what the test should be doing
55  -- @param statusline_cell_count The number of cells available in the statusline
56  -- @param input_stl The format string for the statusline
57  -- @param expected_stl The expected result string for the statusline
58  --
59  -- @param arg Options can be placed in an optional dict as the last parameter
60  --    .expected_cell_count The expected number of cells build_stl_str_hl will return
61  --    .expected_byte_length The expected byte length of the string (defaults to byte length of expected_stl)
62  --    .file_name The name of the file to be tested (useful in %f type tests)
63  --    .fillchar The character that will be used to fill any 'extra' space in the stl
64  local function statusline_test(description, statusline_cell_count, input_stl, expected_stl, arg)
65    -- arg is the optional parameter
66    -- so we either fill in option with arg or an empty dict
67    local option = arg or {}
68
69    local fillchar = option.fillchar or ' '
70    local expected_cell_count = option.expected_cell_count or statusline_cell_count
71    local expected_byte_length = option.expected_byte_length or #expected_stl
72
73    itp(description, function()
74      if option.file_name then
75        buffer.setfname(globals.curbuf, to_cstr(option.file_name), NULL, 1)
76      else
77        buffer.setfname(globals.curbuf, nil, NULL, 1)
78      end
79
80      local result_cell_count = build_stl_str_hl {
81        pat = input_stl,
82        maximum_cell_count = statusline_cell_count,
83        fillchar = fillchar,
84      }
85
86      local output_str = get_str(output_buffer, expected_byte_length + 1)
87      eq(expected_stl, output_str:sub(1, -2))
88      eq('\0', output_str:sub(-1), 'output string must be NUL-terminated')
89      eq(expected_cell_count, result_cell_count)
90    end)
91  end
92
93  -- expression testing
94  statusline_test('expands expression', 2, '%!expand(20+1)', '21')
95  statusline_test('expands broken expression to itself', 11, '%!expand(20+1', 'expand(20+1')
96
97  -- file name testing
98  statusline_test('prints no file name', 10, '%f', '[No Name]', { expected_cell_count = 9 })
99  statusline_test(
100    'prints the relative file name',
101    30,
102    '%f',
103    'test/unit/buffer_spec.lua',
104    { file_name = 'test/unit/buffer_spec.lua', expected_cell_count = 25 }
105  )
106  statusline_test(
107    'prints the full file name',
108    40,
109    '%F',
110    '/test/unit/buffer_spec.lua',
111    { file_name = '/test/unit/buffer_spec.lua', expected_cell_count = 26 }
112  )
113
114  -- fillchar testing
115  statusline_test('handles `!` as a fillchar', 10, 'abcde%=', 'abcde!!!!!', { fillchar = '!' })
116  statusline_test('handles `~` as a fillchar', 10, '%=abcde', '~~~~~abcde', { fillchar = '~' })
117  statusline_test(
118    'puts fillchar `!` in between text',
119    10,
120    'abc%=def',
121    'abc!!!!def',
122    { fillchar = '!' }
123  )
124  statusline_test(
125    'puts fillchar `~` in between text',
126    10,
127    'abc%=def',
128    'abc~~~~def',
129    { fillchar = '~' }
130  )
131  statusline_test(
132    'puts fillchar `━` in between text',
133    10,
134    'abc%=def',
135    'abc━━━━def',
136    { fillchar = '━' }
137  )
138  statusline_test('handles zero-fillchar as a space', 10, 'abcde%=', 'abcde     ', { fillchar = 0 })
139  statusline_test(
140    'prints the tail file name',
141    80,
142    '%t',
143    'buffer_spec.lua',
144    { file_name = 'test/unit/buffer_spec.lua', expected_cell_count = 15 }
145  )
146
147  -- standard text testing
148  statusline_test(
149    'copies plain text',
150    80,
151    'this is a test',
152    'this is a test',
153    { expected_cell_count = 14 }
154  )
155
156  -- line number testing
157  statusline_test('prints the buffer number', 80, '%n', '1', { expected_cell_count = 1 })
158  statusline_test(
159    'prints the current line number in the buffer',
160    80,
161    '%l',
162    '0',
163    { expected_cell_count = 1 }
164  )
165  statusline_test(
166    'prints the number of lines in the buffer',
167    80,
168    '%L',
169    '1',
170    { expected_cell_count = 1 }
171  )
172
173  -- truncation testing
174  statusline_test(
175    'truncates when standard text pattern is too long',
176    10,
177    '0123456789abcde',
178    '<6789abcde'
179  )
180  statusline_test('truncates when using =', 10, 'abcdef%=ghijkl', 'abcdef<jkl')
181  statusline_test('truncates centered text when using ==', 10, 'abcde%=gone%=fghij', 'abcde<ghij')
182  statusline_test('respects the `<` marker', 10, 'abc%<defghijkl', 'abc<ghijkl')
183  statusline_test('truncates at `<` with one `=`, test 1', 10, 'abc%<def%=ghijklmno', 'abc<jklmno')
184  statusline_test('truncates at `<` with one `=`, test 2', 10, 'abcdef%=ghijkl%<mno', 'abcdefghi>')
185  statusline_test('truncates at `<` with one `=`, test 3', 10, 'abc%<def%=ghij', 'abcdefghij')
186  statusline_test('truncates at `<` with one `=`, test 4', 10, 'abc%<def%=ghijk', 'abc<fghijk')
187  statusline_test('truncates at `<` with many `=`, test 5', 10, 'ab%<cdef%=g%=h%=ijk', 'ab<efghijk')
188  statusline_test('truncates at the first `<`', 10, 'abc%<def%<ghijklm', 'abc<hijklm')
189  statusline_test('ignores trailing %', 3, 'abc%', 'abc')
190
191  -- alignment testing with fillchar
192  local function statusline_test_align(
193    description,
194    statusline_cell_count,
195    input_stl,
196    expected_stl,
197    arg
198  )
199    arg = arg or {}
200    statusline_test(
201      description .. ' without fillchar',
202      statusline_cell_count,
203      input_stl,
204      expected_stl:gsub('%~', ' '),
205      arg
206    )
207    arg.fillchar = '!'
208    statusline_test(
209      description .. ' with fillchar `!`',
210      statusline_cell_count,
211      input_stl,
212      expected_stl:gsub('%~', '!'),
213      arg
214    )
215    arg.fillchar = '━'
216    statusline_test(
217      description .. ' with fillchar `━`',
218      statusline_cell_count,
219      input_stl,
220      expected_stl:gsub('%~', '━'),
221      arg
222    )
223  end
224
225  statusline_test_align('right aligns when using =', 20, 'neo%=vim', 'neo~~~~~~~~~~~~~~vim')
226  statusline_test_align(
227    'centers text when using %=text%=',
228    20,
229    'abc%=neovim%=def',
230    'abc~~~~neovim~~~~def'
231  )
232  statusline_test_align(
233    'handles uneven spacing in the buffer when using %=text%=',
234    20,
235    'abc%=neo_vim%=def',
236    'abc~~~neo_vim~~~~def'
237  )
238  statusline_test_align(
239    'produces approximately equal spaces even with non-equal sides when using =',
240    20,
241    'foobar%=test%=baz',
242    'foobar~~~test~~~~baz'
243  )
244  statusline_test_align(
245    'produces approximately equal spaces even with longer right side when using =',
246    20,
247    'a%=test%=longtext',
248    'a~~~test~~~~longtext'
249  )
250  statusline_test_align(
251    'handles an empty left side when using ==',
252    20,
253    '%=test%=baz',
254    '~~~~~~test~~~~~~~baz'
255  )
256  statusline_test_align(
257    'handles an empty right side when using ==',
258    20,
259    'foobar%=test%=',
260    'foobar~~~~~test~~~~~'
261  )
262  statusline_test_align('handles consecutive empty ==', 20, '%=%=test%=', '~~~~~~~~~~test~~~~~~')
263  statusline_test_align('handles an = alone', 20, '%=', '~~~~~~~~~~~~~~~~~~~~')
264  statusline_test_align(
265    'right aligns text when it is alone with =',
266    20,
267    '%=foo',
268    '~~~~~~~~~~~~~~~~~foo'
269  )
270  statusline_test_align(
271    'left aligns text when it is alone with =',
272    20,
273    'foo%=',
274    'foo~~~~~~~~~~~~~~~~~'
275  )
276
277  statusline_test_align(
278    'approximately centers text when using %=text%=',
279    21,
280    'abc%=neovim%=def',
281    'abc~~~~neovim~~~~~def'
282  )
283  statusline_test_align(
284    'completely fills the buffer when using %=text%=',
285    21,
286    'abc%=neo_vim%=def',
287    'abc~~~~neo_vim~~~~def'
288  )
289  statusline_test_align(
290    'produces equal spacing even with non-equal sides when using =',
291    21,
292    'foobar%=test%=baz',
293    'foobar~~~~test~~~~baz'
294  )
295  statusline_test_align(
296    'produces equal spacing even with longer right side when using =',
297    21,
298    'a%=test%=longtext',
299    'a~~~~test~~~~longtext'
300  )
301  statusline_test_align(
302    'handles an empty left side when using ==',
303    21,
304    '%=test%=baz',
305    '~~~~~~~test~~~~~~~baz'
306  )
307  statusline_test_align(
308    'handles an empty right side when using ==',
309    21,
310    'foobar%=test%=',
311    'foobar~~~~~test~~~~~~'
312  )
313
314  statusline_test_align(
315    'quadrants the text when using 3 %=',
316    40,
317    'abcd%=n%=eovim%=ef',
318    'abcd~~~~~~~~~n~~~~~~~~~eovim~~~~~~~~~~ef'
319  )
320  statusline_test_align(
321    'works well with %t',
322    40,
323    '%t%=right_aligned',
324    'buffer_spec.lua~~~~~~~~~~~~right_aligned',
325    { file_name = 'test/unit/buffer_spec.lua' }
326  )
327  statusline_test_align(
328    'works well with %t and regular text',
329    40,
330    'l%=m_l %t m_r%=r',
331    'l~~~~~~~m_l buffer_spec.lua m_r~~~~~~~~r',
332    { file_name = 'test/unit/buffer_spec.lua' }
333  )
334  statusline_test_align(
335    'works well with %=, %t, %L, and %l',
336    40,
337    '%t %= %L %= %l',
338    'buffer_spec.lua ~~~~~~~~~ 1 ~~~~~~~~~~ 0',
339    { file_name = 'test/unit/buffer_spec.lua' }
340  )
341
342  statusline_test_align(
343    'quadrants the text when using 3 %=',
344    41,
345    'abcd%=n%=eovim%=ef',
346    'abcd~~~~~~~~~n~~~~~~~~~eovim~~~~~~~~~~~ef'
347  )
348  statusline_test_align(
349    'works well with %t',
350    41,
351    '%t%=right_aligned',
352    'buffer_spec.lua~~~~~~~~~~~~~right_aligned',
353    { file_name = 'test/unit/buffer_spec.lua' }
354  )
355  statusline_test_align(
356    'works well with %t and regular text',
357    41,
358    'l%=m_l %t m_r%=r',
359    'l~~~~~~~~m_l buffer_spec.lua m_r~~~~~~~~r',
360    { file_name = 'test/unit/buffer_spec.lua' }
361  )
362  statusline_test_align(
363    'works well with %=, %t, %L, and %l',
364    41,
365    '%t %= %L %= %l',
366    'buffer_spec.lua ~~~~~~~~~~ 1 ~~~~~~~~~~ 0',
367    { file_name = 'test/unit/buffer_spec.lua' }
368  )
369
370  statusline_test_align(
371    'works with 10 %=',
372    50,
373    'aaaa%=b%=c%=d%=e%=fg%=hi%=jk%=lmnop%=qrstuv%=wxyz',
374    'aaaa~~b~~c~~d~~e~~fg~~hi~~jk~~lmnop~~qrstuv~~~wxyz'
375  )
376
377  -- item group testing
378
379  statusline_test_align(
380    'right aligns in right aligned groups',
381    30,
382    '%5(%l%), %5.(%l%), %5.5(%l%), %5.10(%l%)',
383    '~~~~0, ~~~~0, ~~~~0, ~~~~0',
384    { expected_cell_count = 26 }
385  )
386
387  statusline_test_align(
388    'left aligns in left aligned groups',
389    30,
390    '%-5(%l%), %-5.(%l%), %-5.5(%l%), %-5.10(%l%)',
391    '0~~~~, 0~~~~, 0~~~~, 0~~~~',
392    { expected_cell_count = 26 }
393  )
394
395  statusline_test_align(
396    'expands at %= in item groups according to minwid',
397    60,
398    '%10(neo%=vim%), %10.(neo%=vim%), %10.10(neo%=vim%), %10.20(neo%=vim%)',
399    'neo~~~~vim, neo~~~~vim, neo~~~~vim, neo~~~~vim',
400    { expected_cell_count = 46 }
401  )
402
403  statusline_test_align(
404    'expands at %= in item groups with normal items',
405    60,
406    '%10(%L%=%l,%c%), %10(%=%L%=%l,%c%=%), %10(x%=%L%=%l,%c%=y%)',
407    '1~~~~~~0,0, ~~1~~0,0~~, x~1~0,0~~y',
408    { expected_cell_count = 34 }
409  )
410
411  statusline_test_align(
412    'expands at %= in nested item groups and top-level',
413    29,
414    '%21(foo%=%7(x%=y%=z%)%=bar%)%=%5(a%=b%=c%)',
415    'foo~~~~x~~y~~z~~~~bar~~~a~b~c'
416  )
417
418  statusline_test(
419    'ignores unexpanded %= in item groups when expanding at top-level',
420    20,
421    '%4(%L%=neo%)vim',
422    '1neovim',
423    { expected_cell_count = 7 }
424  )
425
426  statusline_test(
427    'ignores truncated %= in item groups when expanding at top-level',
428    20,
429    '%10.10(%<hidden%=%f%)TEST',
430    '<_spec.luaTEST',
431    { file_name = 'test/unit/buffer_spec.lua', expected_cell_count = 14 }
432  )
433
434  statusline_test(
435    'ignores %= in hidden item groups when expanding at top-level',
436    20,
437    '%(hidden%=%h%)TEST',
438    'TEST',
439    { expected_cell_count = 4 }
440  )
441
442  statusline_test(
443    'truncates item groups according to maxwid',
444    30,
445    '%.5(1234567%), %2.5(1234567%), %5.5(1234567%)',
446    '<4567, <4567, <4567',
447    { expected_cell_count = 19 }
448  )
449
450  statusline_test(
451    'truncates at first normal item in item groups',
452    30,
453    '%.15(path: %f%)',
454    'path: <spec.lua',
455    { file_name = 'test/unit/buffer_spec.lua', expected_cell_count = 15 }
456  )
457
458  statusline_test(
459    'truncates at %< in item groups',
460    60,
461    '%.15(%<path: %f%), %.15(path: %<%f%), %.15(path: %f%<%)',
462    '<uffer_spec.lua, path: <spec.lua, path: test/uni>',
463    { file_name = 'test/unit/buffer_spec.lua', expected_cell_count = 49 }
464  )
465
466  statusline_test(
467    'truncates at %< in nested item groups and top-level',
468    40,
469    '%.24(trim both ends: %<%.17(%f%<%)%), path: %<%f',
470    'trim both ends: <buffer>, path: <pec.lua',
471    { file_name = 'test/unit/buffer_spec.lua' }
472  )
473
474  statusline_test(
475    'ignores unused %< in item groups when truncating at top-level',
476    25,
477    '%.6(%L%< test%) file: %<%f',
478    '1 test file: <er_spec.lua',
479    { file_name = 'test/unit/buffer_spec.lua' }
480  )
481
482  statusline_test(
483    'ignores second %< in item groups when truncating at top-level',
484    32,
485    '%.15(file: %<%f%<%), file: %<%f',
486    'file: <spec.lua, file: <spec.lua',
487    { file_name = 'test/unit/buffer_spec.lua' }
488  )
489
490  statusline_test(
491    'ignores %< in hidden item groups when truncating at top-level',
492    15,
493    '%.15(hidden%<%)%f%<',
494    'test/unit/buff>',
495    { file_name = 'test/unit/buffer_spec.lua' }
496  )
497
498  statusline_test(
499    'hides auto-hiding item group with only empty normal items',
500    29,
501    'visible: %(pre-%h%f-post%), hidden: %(pre-%h%w-post%)',
502    'visible: pre-X-post, hidden: ',
503    { file_name = 'X' }
504  )
505
506  statusline_test(
507    'hides auto-hiding item group with expression evaluating to empty string',
508    29,
509    "visible: %(pre-%{'X'}-post%), hidden: %(pre-%{''}-post%)",
510    'visible: pre-X-post, hidden: '
511  )
512
513  statusline_test(
514    'hides auto-hiding item group with re-evaluated expression evaluating to empty string',
515    29,
516    "visible: %(pre-%{%'%1*X%*'%}-post%), hidden: %(pre-%{%''%}-post%)",
517    'visible: pre-X-post, hidden: '
518  )
519
520  statusline_test(
521    'hides auto-hiding item group with re-evaluated expression evaluating to only empty normal items',
522    29,
523    "visible: %(pre-%{%'%h%f'%}-post%), hidden: %(pre-%{%'%h%w'%}-post%)",
524    'visible: pre-X-post, hidden: ',
525    { file_name = 'X' }
526  )
527
528  statusline_test(
529    'hides auto-hiding item group with nested re-evaluated expressions evaluating to empty string',
530    29,
531    "visible: %(pre-%{%'%{%''%1*X%*''%'..nr2char(125)%}-post%), hidden: %(pre-%{%'%{%''''%'..nr2char(125)%}-post%)",
532    'visible: pre-X-post, hidden: '
533  )
534
535  statusline_test(
536    'hides auto-hiding item group with nested re-evaluated expressions evaluating to only empty normal items',
537    29,
538    "visible: %(pre-%{%'%h%{%''%f''%'..nr2char(125)%}-post%), hidden: %(pre-%{%'%h%{%''%w''%'..nr2char(125)%}-post%)",
539    'visible: pre-X-post, hidden: ',
540    { file_name = 'X' }
541  )
542
543  -- stl item testing
544  local tabline = ''
545  for i = 1, 1000 do
546    tabline = tabline .. (i % 2 == 0 and '%#TabLineSel#' or '%#TabLineFill#') .. tostring(i % 2)
547  end
548  statusline_test('handles a large amount of any items', 20, tabline, '<1010101010101010101') -- Should not show any error
549  statusline_test(
550    'handles a larger amount of = than stl initial item',
551    20,
552    ('%='):rep(STL_INITIAL_ITEMS * 5),
553    '                    '
554  ) -- Should not show any error
555  statusline_test(
556    'handles many extra characters',
557    20,
558    'a' .. ('a'):rep(STL_INITIAL_ITEMS * 5),
559    '<aaaaaaaaaaaaaaaaaaa'
560  ) -- Does not show any error
561  statusline_test(
562    'handles many extra characters and flags',
563    20,
564    'a' .. ('%=a'):rep(STL_INITIAL_ITEMS * 2),
565    'a<aaaaaaaaaaaaaaaaaa'
566  ) -- Should not show any error
567
568  -- multi-cell character testing
569  statusline_test(
570    'returns reduced width after truncating top-level from the left at multicell character',
571    5,
572    '12🙂345',
573    '<345',
574    { expected_cell_count = 4 }
575  )
576  statusline_test(
577    'returns reduced width after truncating top-level from the right at multicell character',
578    5,
579    '123🙂45%<',
580    '123>',
581    { expected_cell_count = 4 }
582  )
583  statusline_test_align(
584    'compensates with fillchar to reach minwid after truncating item group at multicell character',
585    100,
586    '%.5(12🙂345%), %4.5(12🙂345%), %5.5(12🙂345%), %-5.5(12🙂345%), %50.5(12🙂345%), %.5(123🙂%L%<%), %4.5(123🙂%L%<%), %5.5(123🙂%L%<%), %-5.5(123🙂%L%<%), %50.5(123🙂%L%<%)',
587    '<345, <345, <345~, <345~, <345~, 123>, 123>, 123>~, 123>~, 123>~',
588    { expected_cell_count = 64 }
589  )
590
591  -- multi-byte testing
592  statusline_test('handles multibyte characters', 10, 'Ĉ%=x', 'Ĉ        x')
593  statusline_test(
594    'handles multibyte characters and different fillchars',
595    10,
596    'Ą%=mid%=end',
597    'Ą@mid@@end',
598    { fillchar = '@' }
599  )
600  for _, fillchar in ipairs({ '-', '±', '━', '𝕚' }) do
601    local minwid = math.floor(2147483647 / #fillchar) + 10000 -- this supposes 4-byte integers
602    itp("doesn't crash due to integer overflow with fillchar " .. fillchar, function()
603      build_stl_str_hl {
604        pat = '%' .. tostring(minwid) .. '(%)',
605        maximum_cell_count = 10,
606        fillchar = fillchar,
607      }
608    end)
609  end
610
611  -- escaping % testing
612  statusline_test('handles escape of %', 4, 'abc%%', 'abc%')
613  statusline_test('case where escaped % does not fit', 3, 'abc%%abcabc', '<bc')
614  statusline_test('escaped % is first', 1, '%%', '%')
615end)