local helpers = require("test.unit.helpers")(after_each) local itp = helpers.gen_itp(it) local to_cstr = helpers.to_cstr local get_str = helpers.ffi.string local eq = helpers.eq local NULL = helpers.NULL local buffer = helpers.cimport("./src/nvim/buffer.h") local globals = helpers.cimport("./src/nvim/globals.h") local stl = helpers.cimport("./src/nvim/statusline.h") describe('build_stl_str_hl', function() local buffer_byte_size = 100 local STL_INITIAL_ITEMS = 20 local output_buffer = '' -- This function builds the statusline -- -- @param arg Optional arguments are: -- .pat The statusline format string -- .fillchar The fill character used in the statusline -- .maximum_cell_count The number of cells available in the statusline local function build_stl_str_hl(arg) output_buffer = to_cstr(string.rep(" ", buffer_byte_size)) local pat = arg.pat or '' local fillchar = arg.fillchar or (' '):byte() local maximum_cell_count = arg.maximum_cell_count or buffer_byte_size return stl.build_stl_str_hl(globals.curwin, output_buffer, buffer_byte_size, to_cstr(pat), NULL, 0, fillchar, maximum_cell_count, NULL, NULL, NULL) end -- Use this function to simplify testing the comparison between -- the format string and the resulting statusline. -- -- @param description The description of what the test should be doing -- @param statusline_cell_count The number of cells available in the statusline -- @param input_stl The format string for the statusline -- @param expected_stl The expected result string for the statusline -- -- @param arg Options can be placed in an optional dictionary as the last parameter -- .expected_cell_count The expected number of cells build_stl_str_hl will return -- .expected_byte_length The expected byte length of the string (defaults to byte length of expected_stl) -- .file_name The name of the file to be tested (useful in %f type tests) -- .fillchar The character that will be used to fill any 'extra' space in the stl local function statusline_test(description, statusline_cell_count, input_stl, expected_stl, arg) -- arg is the optional parameter -- so we either fill in option with arg or an empty dictionary local option = arg or {} local fillchar = option.fillchar or (' '):byte() local expected_cell_count = option.expected_cell_count or statusline_cell_count local expected_byte_length = option.expected_byte_length or #expected_stl itp(description, function() if option.file_name then buffer.setfname(globals.curbuf, to_cstr(option.file_name), NULL, 1) else buffer.setfname(globals.curbuf, nil, NULL, 1) end local result_cell_count = build_stl_str_hl{pat=input_stl, maximum_cell_count=statusline_cell_count, fillchar=fillchar} eq(expected_stl, get_str(output_buffer, expected_byte_length)) eq(expected_cell_count, result_cell_count) end) end -- expression testing statusline_test('Should expand expression', 2, '%!expand(20+1)', '21') statusline_test('Should expand broken expression to itself', 11, '%!expand(20+1', 'expand(20+1') -- file name testing statusline_test('should print no file name', 10, '%f', '[No Name]', {expected_cell_count=9}) statusline_test('should print the relative file name', 30, '%f', 'test/unit/buffer_spec.lua', {file_name='test/unit/buffer_spec.lua', expected_cell_count=25}) statusline_test('should print the full file name', 40, '%F', '/test/unit/buffer_spec.lua', {file_name='/test/unit/buffer_spec.lua', expected_cell_count=26}) -- fillchar testing statusline_test('should handle `!` as a fillchar', 10, 'abcde%=', 'abcde!!!!!', {fillchar=('!'):byte()}) statusline_test('should handle `~` as a fillchar', 10, '%=abcde', '~~~~~abcde', {fillchar=('~'):byte()}) statusline_test('should put fillchar `!` in between text', 10, 'abc%=def', 'abc!!!!def', {fillchar=('!'):byte()}) statusline_test('should put fillchar `~` in between text', 10, 'abc%=def', 'abc~~~~def', {fillchar=('~'):byte()}) statusline_test('should put fillchar `━` in between text', 10, 'abc%=def', 'abc━━━━def', {fillchar=0x2501}) statusline_test('should handle zero-fillchar as a space', 10, 'abcde%=', 'abcde ', {fillchar=0}) statusline_test('should print the tail file name', 80, '%t', 'buffer_spec.lua', {file_name='test/unit/buffer_spec.lua', expected_cell_count=15}) -- standard text testing statusline_test('should copy plain text', 80, 'this is a test', 'this is a test', {expected_cell_count=14}) -- line number testing statusline_test('should print the buffer number', 80, '%n', '1', {expected_cell_count=1}) statusline_test('should print the current line number in the buffer', 80, '%l', '0', {expected_cell_count=1}) statusline_test('should print the number of lines in the buffer', 80, '%L', '1', {expected_cell_count=1}) -- truncation testing statusline_test('should truncate when standard text pattern is too long', 10, '0123456789abcde', '<6789abcde') statusline_test('should truncate when using =', 10, 'abcdef%=ghijkl', 'abcdef') statusline_test('should truncate at `<` with one `=`, test 3', 10, 'abc%