|
| 1 | +-- Define a table simulating LSP (Language Server Protocol) symbol responses |
| 2 | +local lsp_symbols = { |
| 3 | + pending_request = false, |
| 4 | + symbols = { |
| 5 | + { |
| 6 | + detail = '', |
| 7 | + kind = 19, |
| 8 | + name = 'command', |
| 9 | + range = { |
| 10 | + ['end'] = { |
| 11 | + character = 18, |
| 12 | + line = 0, |
| 13 | + }, |
| 14 | + start = { |
| 15 | + character = 6, |
| 16 | + line = 0, |
| 17 | + }, |
| 18 | + }, |
| 19 | + selectionRange = { |
| 20 | + ['end'] = { |
| 21 | + character = 13, |
| 22 | + line = 0, |
| 23 | + }, |
| 24 | + start = { |
| 25 | + character = 6, |
| 26 | + line = 0, |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + }, |
| 31 | +} |
| 32 | + |
| 33 | +-- Function to get symbols, returns the simulated lsp_symbols table |
| 34 | +function lsp_symbols.get_symbols(_bufnr) |
| 35 | + return lsp_symbols |
| 36 | +end |
| 37 | + |
| 38 | +-- Configuration for the lspsaga plugin |
| 39 | +local lspsaga_opts = { |
| 40 | + ui = { |
| 41 | + winbar_prefix = ' ', |
| 42 | + }, |
| 43 | + symbol_in_winbar = { |
| 44 | + enable = true, |
| 45 | + separator = '|', |
| 46 | + }, |
| 47 | +} |
| 48 | + |
| 49 | +-- Require the lspsaga module and configure it with the defined options |
| 50 | +local lspsaga = require('lspsaga') |
| 51 | +lspsaga.setup(lspsaga_opts) |
| 52 | + |
| 53 | +describe('winbar', function() |
| 54 | + local api = vim.api |
| 55 | + local lspsaga_symbols__get_buf_symbols, lspsaga_head__get_buf_symbols |
| 56 | + local lspsaga_symbol = require('lspsaga.symbol') |
| 57 | + local lspsaga_head = require('lspsaga.symbol.head') |
| 58 | + local lspsaga_winbar = require('lspsaga.symbol.winbar') |
| 59 | + local helper = require('test.helper') |
| 60 | + |
| 61 | + before_each(function() |
| 62 | + -- Create a new buffer and set it as the current buffer |
| 63 | + local buf = api.nvim_create_buf(false, true) |
| 64 | + api.nvim_set_current_buf(buf) |
| 65 | + api.nvim_command('vsplit') |
| 66 | + api.nvim_set_current_win(api.nvim_get_current_win()) |
| 67 | + |
| 68 | + -- Store original functions |
| 69 | + lspsaga_symbols__get_buf_symbols = lspsaga_symbol.get_buf_symbols |
| 70 | + lspsaga_head__get_buf_symbols = lspsaga_symbol.get_buf_symbols |
| 71 | + |
| 72 | + -- Replace real LSP interaction with the mock |
| 73 | + lspsaga_symbol.get_buf_symbols = lsp_symbols.get_symbols |
| 74 | + lspsaga_head.get_buf_symbols = lsp_symbols.get_symbols |
| 75 | + end) |
| 76 | + |
| 77 | + after_each(function() |
| 78 | + -- Restore original functions after each test |
| 79 | + lspsaga_symbol.get_buf_symbols = lspsaga_symbols__get_buf_symbols |
| 80 | + lspsaga_head.get_buf_symbols = lspsaga_head__get_buf_symbols |
| 81 | + end) |
| 82 | + |
| 83 | + it('should correctly extract components from the winbar', function() |
| 84 | + -- Initialize the winbar for the current buffer |
| 85 | + lspsaga_winbar.init_winbar(api.nvim_get_current_buf()) |
| 86 | + |
| 87 | + -- Simulate a situation with many items in the winbar |
| 88 | + -- (Adapt as necessary to create a long list of symbols) |
| 89 | + local cur_win = api.nvim_get_current_win() |
| 90 | + local max_width = math.floor(api.nvim_win_get_width(cur_win) * 0.9) |
| 91 | + api.nvim_set_option_value('winbar', string.rep('Item|', max_width), { |
| 92 | + scope = 'local', |
| 93 | + win = api.nvim_get_current_win(), |
| 94 | + }) |
| 95 | + |
| 96 | + -- Define a winbar value large enough to exceed the window width |
| 97 | + local winbar_value = lspsaga_winbar.get_bar() or '' |
| 98 | + |
| 99 | + -- Extract the components of the winbar |
| 100 | + local saga_prefix = helper.extract_winbar_value(winbar_value, 'Prefix') |
| 101 | + local saga_sep = helper.extract_winbar_value(winbar_value, 'SagaSep') |
| 102 | + local saga_object = helper.extract_winbar_value(winbar_value, 'SagaObject') |
| 103 | + |
| 104 | + -- Verify that components were extracted correctly |
| 105 | + assert(saga_prefix, 'Prefix not found in winbar_value') |
| 106 | + assert(saga_sep, 'Separator not found in winbar_value') |
| 107 | + assert(saga_object, 'Symbol not found in winbar_value') |
| 108 | + |
| 109 | + -- Optionally, check individual presence of prefix, separator, and symbol |
| 110 | + assert(saga_prefix == ' ', 'Prefix does not match expected value') |
| 111 | + assert(saga_sep == '|', 'Separator does not match expected value') |
| 112 | + assert(saga_object == ' command', 'Symbol does not match expected value') |
| 113 | + end) |
| 114 | +end) |
0 commit comments