robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
12.1 KiB384 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each, pending = t.describe, t.it, t.before_each, t.pending
6local command, feed = n.command, n.feed
7local dedent = t.dedent
8local clear = n.clear
9local exec_lua = n.exec_lua
10local fn = n.fn
11local nvim_prog = n.nvim_prog
12local matches = t.matches
13local tmpname = t.tmpname
14local eq = t.eq
15local pesc = vim.pesc
16
17-- Collects all names passed to find_path() after attempting ":Man foo".
18local function get_search_history(name)
19  return exec_lua(function()
20    local args = vim.split(name, ' ')
21    local man = require('man')
22    local res = {}
23    --- @diagnostic disable-next-line:duplicate-set-field
24    man._find_path = function(name0, sect)
25      table.insert(res, { sect, name0 })
26      return nil
27    end
28    local err = man.open_page(-1, { tab = 0 }, args)
29    assert(err and err:match('no manual entry'))
30    return res
31  end)
32end
33
34clear()
35if fn.executable('man') == 0 then
36  pending('N/A: missing "man" command', function() end)
37  return
38end
39
40describe(':Man', function()
41  before_each(function()
42    clear()
43  end)
44
45  describe('man.lua: highlight_line()', function()
46    local screen --- @type test.functional.ui.screen
47
48    before_each(function()
49      command('syntax on')
50      command('set filetype=man')
51      command('syntax off') -- Ignore syntax groups
52      screen = Screen.new(52, 5)
53      screen:set_default_attr_ids({
54        b = { bold = true },
55        i = { italic = true },
56        u = { underline = true },
57        bi = { bold = true, italic = true },
58        biu = { bold = true, italic = true, underline = true },
59        c = { foreground = Screen.colors.Blue }, -- control chars
60        eob = { bold = true, foreground = Screen.colors.Blue }, -- empty line '~'s
61      })
62    end)
63
64    it('clears backspaces from text and adds highlights', function()
65      feed(
66        dedent(
67          [[
68        ithis i<C-v><C-h>is<C-v><C-h>s a<C-v><C-h>a test
69        with _<C-v><C-h>o_<C-v><C-h>v_<C-v><C-h>e_<C-v><C-h>r_<C-v><C-h>s_<C-v><C-h>t_<C-v><C-h>r_<C-v><C-h>u_<C-v><C-h>c_<C-v><C-h>k text<ESC>]]
70        )
71      )
72
73      screen:expect {
74        grid = [[
75        this i{c:^H}is{c:^H}s a{c:^H}a test                             |
76        with _{c:^H}o_{c:^H}v_{c:^H}e_{c:^H}r_{c:^H}s_{c:^H}t_{c:^H}r_{c:^H}u_{c:^H}c_{c:^H}k tex^t  |
77        {eob:~                                                   }|*2
78                                                            |
79      ]],
80      }
81
82      exec_lua [[require'man'.init_pager()]]
83
84      screen:expect([[
85      ^this {b:is} {b:a} test                                      |
86      with {i:overstruck} text                                |
87      {eob:~                                                   }|*2
88                                                          |
89      ]])
90    end)
91
92    it('clears escape sequences from text and adds highlights', function()
93      feed(
94        dedent(
95          [[
96        ithis <C-v><ESC>[1mis <C-v><ESC>[3ma <C-v><ESC>[4mtest<C-v><ESC>[0m
97        <C-v><ESC>[4mwith<C-v><ESC>[24m <C-v><ESC>[4mescaped<C-v><ESC>[24m <C-v><ESC>[4mtext<C-v><ESC>[24m<ESC>]]
98        )
99      )
100
101      screen:expect {
102        grid = [=[
103        this {c:^[}[1mis {c:^[}[3ma {c:^[}[4mtest{c:^[}[0m                  |
104        {c:^[}[4mwith{c:^[}[24m {c:^[}[4mescaped{c:^[}[24m {c:^[}[4mtext{c:^[}[24^m  |
105        {eob:~                                                   }|*2
106                                                            |
107      ]=],
108      }
109
110      exec_lua [[require'man'.init_pager()]]
111
112      screen:expect([[
113      ^this {b:is }{bi:a }{biu:test}                                      |
114      {u:with} {u:escaped} {u:text}                                   |
115      {eob:~                                                   }|*2
116                                                          |
117      ]])
118    end)
119
120    it('clears OSC 8 hyperlink markup from text', function()
121      feed(
122        dedent(
123          [[
124        ithis <C-v><ESC>]8;;http://example.com<C-v><ESC>\Link Title<C-v><ESC>]8;;<C-v><ESC>\<ESC>]]
125        )
126      )
127
128      screen:expect {
129        grid = [=[
130        this {c:^[}]8;;http://example.com{c:^[}\Link Title{c:^[}]8;;{c:^[}^\ |
131        {eob:~                                                   }|*3
132                                                            |
133      ]=],
134      }
135
136      exec_lua [[require'man'.init_pager()]]
137
138      screen:expect([[
139      ^this Link Title                                     |
140      {eob:~                                                   }|*3
141                                                          |
142      ]])
143    end)
144
145    it('highlights multibyte text', function()
146      feed(
147        dedent(
148          [[
149        ithis i<C-v><C-h>is<C-v><C-h>s あ<C-v><C-h>あ test
150        with _<C-v><C-h>ö_<C-v><C-h>v_<C-v><C-h>e_<C-v><C-h>r_<C-v><C-h>s_<C-v><C-h>t_<C-v><C-h>r_<C-v><C-h>u_<C-v><C-h>̃_<C-v><C-h>c_<C-v><C-h>k te<C-v><ESC>[3mxt¶<C-v><ESC>[0m<ESC>]]
151        )
152      )
153      exec_lua [[require'man'.init_pager()]]
154
155      screen:expect([[
156      ^this {b:is} {b:あ} test                                     |
157      with {i:överstrũck} te{i:xt¶}                               |
158      {eob:~                                                   }|*2
159                                                          |
160      ]])
161    end)
162
163    it('highlights underscores based on context', function()
164      feed(
165        dedent(
166          [[
167        i_<C-v><C-h>_b<C-v><C-h>be<C-v><C-h>eg<C-v><C-h>gi<C-v><C-h>in<C-v><C-h>ns<C-v><C-h>s
168        m<C-v><C-h>mi<C-v><C-h>id<C-v><C-h>d_<C-v><C-h>_d<C-v><C-h>dl<C-v><C-h>le<C-v><C-h>e
169        _<C-v><C-h>m_<C-v><C-h>i_<C-v><C-h>d_<C-v><C-h>__<C-v><C-h>d_<C-v><C-h>l_<C-v><C-h>e<ESC>]]
170        )
171      )
172      exec_lua [[require'man'.init_pager()]]
173
174      screen:expect([[
175      {b:^_begins}                                             |
176      {b:mid_dle}                                             |
177      {i:mid_dle}                                             |
178      {eob:~                                                   }|
179                                                          |
180      ]])
181    end)
182
183    it('highlights various bullet formats', function()
184      feed(dedent([[
185        i· ·<C-v><C-h>·
186             +<C-v><C-h>o
187        +<C-v><C-h>+<C-v><C-h>o<C-v><C-h>o double<ESC>]]))
188      exec_lua [[require'man'.init_pager()]]
189
190      screen:expect([[
191      ^· {b:·}                                                 |
192           {b:·}                                              |
193      {b:·} double                                            |
194      {eob:~                                                   }|
195                                                          |
196      ]])
197    end)
198
199    it('handles : characters in input', function()
200      feed(dedent([[
201        i<C-v><C-[>[40m    0  <C-v><C-[>[41m    1  <C-v><C-[>[42m    2  <C-v><C-[>[43m    3
202        <C-v><C-[>[44m    4  <C-v><C-[>[45m    5  <C-v><C-[>[46m    6  <C-v><C-[>[47m    7  <C-v><C-[>[100m    8  <C-v><C-[>[101m    9
203        <C-v><C-[>[102m   10  <C-v><C-[>[103m   11  <C-v><C-[>[104m   12  <C-v><C-[>[105m   13  <C-v><C-[>[106m   14  <C-v><C-[>[107m   15
204        <C-v><C-[>[48:5:16m   16  <ESC>]]))
205      exec_lua [[require'man'.init_pager()]]
206
207      screen:expect([[
208       ^    0      1      2      3                          |
209           4      5      6      7      8      9            |
210          10     11     12     13     14     15            |
211          16                                               |
212                                                           |
213      ]])
214    end)
215  end)
216
217  it('q quits in "$MANPAGER mode" (:Man!) #18281', function()
218    -- This will hang if #18281 regresses.
219    local args = {
220      nvim_prog,
221      '--headless',
222      '+autocmd VimLeave * echo "quit works!!"',
223      '+Man!',
224      '+tag ls',
225      '+call nvim_input("q")',
226    }
227    matches('quit works!!', fn.system(args, { 'manpage contents' }))
228  end)
229
230  it('raw manpage into (:Man!) creates a new buffer #30132', function()
231    local args = {
232      nvim_prog,
233      '--headless',
234      '+Man! foo',
235      '+echo bufname()',
236      '+enew',
237      '+Man! foo',
238      '+echo bufname()',
239      '+enew',
240      '+Man! foo',
241      '+echo bufname()',
242      '+q',
243    }
244    local out = fn.system(args, { 'manpage contents' })
245    assert(out and out:match('man://%?new=%d'))
246  end)
247
248  it('reports non-existent man pages for absolute paths', function()
249    local actual_file = tmpname()
250    -- actual_file must be an absolute path to an existent file for us to test against it
251    matches('^/.+', actual_file)
252    local args = { nvim_prog, '--headless', '+:Man ' .. actual_file, '+q' }
253    matches(
254      ('Error in command line:\n' .. 'man.lua: no manual entry for %s'):format(pesc(actual_file)),
255      fn.system(args, { '' })
256    )
257    os.remove(actual_file)
258  end)
259
260  -- if man -w returns multiple results, :Man should select the correct one.
261  it('matches correct manpage section and name', function()
262    -- mock the system function to return the results we want to test.
263    local function _test(results, name, sect)
264      return exec_lua(function()
265        local man = require 'man'
266        return man._match_manpage_path(results, name, sect)
267      end)
268    end
269
270    eq(
271      '/usr/share/man/man1/bash.1',
272      _test({ '/usr/share/man/man1/bash.1' }, '/usr/share/man/man1/bash.1')
273    )
274
275    eq(
276      '/usr/share/man/man3/strlen.3.gz',
277      _test({ '/usr/share/man/man3/strlen.3.gz' }, '/usr/share/man/man3/strlen.3.gz')
278    )
279
280    eq(nil, _test({ '/tmp/not-a-manpage' }, '/tmp/not-a-manpage'))
281    eq(nil, _test({ '/tmp/T10.1' }, '/tmp/T10.1'))
282
283    eq(
284      '/usr/share/man/man3/strcpy.3',
285      _test({
286        '/usr/share/man/man3/strcpy.3',
287        '/usr/share/man/man3/string.3',
288      }, 'strcpy')
289    )
290
291    eq(
292      '/usr/share/man/man3/strcpy.3',
293      _test({
294        '/usr/share/man/man3/string.3',
295        '/usr/share/man/man3/strcpy.3',
296      }, 'strcpy')
297    )
298
299    eq(
300      '/usr/share/man/man3/strcpy.3',
301      _test({
302        '/usr/share/man/man3p/strcpy.3p',
303        '/usr/share/man/man3/strcpy.3',
304        '/usr/share/man/man3/string.3',
305        '/usr/share/man/man7/string_copying.7',
306      }, 'strcpy', '3')
307    )
308
309    eq(
310      '/usr/share/man/man3/strcpy.3.gz',
311      _test({
312        '/usr/share/man/man3p/strcpy.3p.gz',
313        '/usr/share/man/man3/strcpy.3.gz',
314        '/usr/share/man/man3/string.3.gz',
315        '/usr/share/man/man7/string_copying.7.gz',
316      }, 'strcpy', '3')
317    )
318  end)
319
320  it('uses direct manpage lookup if man directories cannot be determined #25919', function()
321    eq(
322      {
323        {
324          name = 'open',
325          filename = 'man://open(2)',
326          cmd = '1',
327        },
328      },
329      exec_lua(function()
330        local man = require('man')
331        vim.env.MANPATH = nil
332        vim.npcall = function()
333          return nil
334        end
335        man._find_path = function(name, sect)
336          if name == 'open' and sect == '2' then
337            return '/usr/share/man/man2/open.2'
338          end
339        end
340        return man.goto_tag('open(2)')
341      end)
342    )
343  end)
344
345  it('tries variants with spaces, underscores #22503', function()
346    eq({
347      { vim.NIL, 'NAME WITH SPACES' },
348      { vim.NIL, 'NAME_WITH_SPACES' },
349    }, get_search_history('NAME WITH SPACES'))
350    eq({
351      { '3', 'some other man' },
352      { '3', 'some_other_man' },
353    }, get_search_history('3 some other man'))
354    eq({
355      { '3x', 'some other man' },
356      { '3x', 'some_other_man' },
357    }, get_search_history('3X some other man'))
358    eq({
359      { '3tcl', 'some other man' },
360      { '3tcl', 'some_other_man' },
361    }, get_search_history('3tcl some other man'))
362    eq({
363      { 'n', 'some other man' },
364      { 'n', 'some_other_man' },
365    }, get_search_history('n some other man'))
366    eq({
367      { vim.NIL, '123some other man' },
368      { vim.NIL, '123some_other_man' },
369    }, get_search_history('123some other man'))
370    eq({
371      { '1', 'other_man' },
372      { '1', 'other_man' },
373    }, get_search_history('other_man(1)'))
374  end)
375
376  it('can complete', function()
377    eq(
378      true,
379      exec_lua(function()
380        return #require('man').man_complete('f', 'Man f') > 0
381      end)
382    )
383  end)
384end)