robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
5.2 KiB143 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local eq = t.eq
6local exec_lua = n.exec_lua
7local clear = n.clear
8
9before_each(clear)
10
11describe('vim.keycode()', function()
12  local function parse(key, simplify)
13    local parsed = exec_lua([[return select(2,vim.keycode(...,true))]], key)
14    return vim.tbl_map(function(tbl)
15      if simplify == 1 then
16        return { tbl.mod, tbl.key }
17      elseif simplify == 2 then
18        return { tbl.mod, tbl.key, tbl.keys }
19      end
20      return tbl
21    end, parsed)
22  end
23
24  it('gets structured keychord info', function()
25    eq({ { mod = { 'M' }, key = 'f', keys = '<M-f>' } }, parse('<A-f>'))
26    eq({ { { 'M' }, 'Home' } }, parse('<A-Home>', 1))
27    eq({ { { 'M' }, 'f' }, { {}, 'b' }, { {}, 'c' } }, parse('<A-f>b<char-99>', 1))
28
29    eq({ { { 'M' }, '>' } }, parse('<A->>', 1))
30    eq({ { { 'M' }, 't_>>' } }, parse('<A-t_>>>', 1))
31
32    -- Multiple modifiers, normalized to canonical order (mod_mask_table): M, T, C, S, …
33    eq({ { mod = { 'C', 'S' }, key = 'Home', keys = '<C-S-Home>' } }, parse('<C-S-Home>'))
34    eq({ { { 'M', 'C' }, 'x', '<M-C-X>' } }, parse('<M-C-x>', 2))
35    eq({ { { 'M', 'C', 'S' }, 'F1', '<M-C-S-F1>' } }, parse('<C-A-S-F1>', 2))
36  end)
37
38  it('normalizes', function()
39    -- stylua: ignore start
40    eq({ { {},      '\\',   '<Bslash>'   } }, parse('\\', 2))
41    eq({ { { 'M' }, '\\',   '<M-Bslash>' } }, parse('<A-\\>', 2))
42    eq({ { { 'C' }, '\\',   '<C-\\>'     } }, parse('<C-\\>', 2))
43    eq({ { {},      '|',    '<Bar>'      } }, parse('|', 2))
44    eq({ { { 'M' }, '|',    '<M-Bar>'    } }, parse('<A-|>', 2))
45    eq({ { { 'C' }, '|',    '<C-Bar>'    } }, parse('<C-|>', 2))
46    eq({ { {},      '\127', '\127'       } }, parse('\127', 2))
47    eq({ { { 'M' }, '\127', '<M-^?>'     } }, parse('<A-\127>', 2))
48    eq({ { { 'C' }, '\127', '<C-^?>'     } }, parse('<C-\127>', 2))
49
50    eq({ { {}, '<',    '<lt>'    } }, parse('<', 2))
51    eq({ { {}, '\t',   '<Tab>'   } }, parse('\t', 2))
52    eq({ { {}, '\r',   '<CR>'    } }, parse('\r', 2))
53    eq({ { {}, '\n',   '<NL>'    } }, parse('\n', 2))
54    eq({ { {}, '\27',  '<Esc>'   } }, parse('\27', 2))
55    eq({ { {}, ' ',    '<Space>' } }, parse(' ', 2))
56
57    eq({ { { 'S' }, 'a', 'A'     } }, parse('A', 2))
58    eq({ { { 'C' }, 'a', '<C-A>' } }, parse('<C-a>', 2))
59    -- stylua: ignore end
60
61    eq({ { mod = {}, key = '<', keys = '<lt>', key_alt = 'lt' } }, parse('<'))
62  end)
63
64  it('utf8', function()
65    eq({ { {}, 'ö' }, { { 'M' }, 'ó' }, { {}, 'ú' }, { {}, 'ü' } }, parse('ö<A-ó>úü', 1))
66    eq({ { {}, 'a' }, { {}, '\226\129\129' }, { {}, 'b' } }, parse('a\226\129\129b', 1))
67    eq({ { {}, 'a' }, { {}, '\242\129\129\129' }, { {}, 'b' } }, parse('a\242\129\129\129b', 1))
68  end)
69
70  -- Canonical notation: concatenating each chord's `keys` must equal keytrans(vim.keycode(…)).
71  it('keys concatenate to keytrans()', function()
72    for _, s in ipairs({
73      '|',
74      '\\',
75      '<',
76      '<A-|>',
77      '<C-\\>',
78      'A',
79      '<C-a>',
80      '<C-S-Home>ab',
81      ' \t',
82      'ö<A-ó>',
83    }) do
84      local concat, keytrans = exec_lua(function(k)
85        local enc, chords = vim.keycode(k, true)
86        local parts = vim.tbl_map(function(c)
87          return c.keys
88        end, chords)
89        return table.concat(parts), vim.fn.keytrans(enc)
90      end, s)
91      eq(keytrans, concat)
92    end
93  end)
94end)
95
96describe('nvim_replace_termcodes', function()
97  it('escapes K_SPECIAL as K_SPECIAL KS_SPECIAL KE_FILLER', function()
98    eq('\128\254X', n.api.nvim_replace_termcodes('\128', true, true, true))
99  end)
100
101  it('leaves non-K_SPECIAL string unchanged', function()
102    eq('abc', n.api.nvim_replace_termcodes('abc', true, true, true))
103  end)
104
105  it('converts <expressions>', function()
106    eq('\\', n.api.nvim_replace_termcodes('<Leader>', true, true, true))
107  end)
108
109  it('converts <LeftMouse> to K_SPECIAL KS_EXTRA KE_LEFTMOUSE', function()
110    -- K_SPECIAL KS_EXTRA KE_LEFTMOUSE
111    -- 0x80      0xfd     0x2c
112    -- 128       253      44
113    eq('\128\253\44', n.api.nvim_replace_termcodes('<LeftMouse>', true, true, true))
114  end)
115
116  it('converts keycodes', function()
117    eq('\nx\27x\rx<x', n.api.nvim_replace_termcodes('<NL>x<Esc>x<CR>x<lt>x', true, true, true))
118  end)
119
120  it('does not convert keycodes if special=false', function()
121    eq(
122      '<NL>x<Esc>x<CR>x<lt>x',
123      n.api.nvim_replace_termcodes('<NL>x<Esc>x<CR>x<lt>x', true, true, false)
124    )
125  end)
126
127  it('does not crash when transforming an empty string', function()
128    -- Actually does not test anything, because current code will use NULL for
129    -- an empty string.
130    --
131    -- Problem here is that if String argument has .data in allocated memory
132    -- then `return str` in vim_replace_termcodes body will make Neovim free
133    -- `str.data` twice: once when freeing arguments, then when freeing return
134    -- value.
135    eq('', n.api.nvim_replace_termcodes('', true, true, true))
136  end)
137
138  -- Not exactly the case, as nvim_replace_termcodes() escapes K_SPECIAL in Unicode
139  it('translates the result of keytrans() on string with 0x80 byte back', function()
140    local s = 'ff\128\253\097tt'
141    eq(s, n.api.nvim_replace_termcodes(n.fn.keytrans(s), true, true, true))
142  end)
143end)