robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
5.5 KiB183 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 clear = n.clear
6local eq = t.eq
7local exec_lua = n.exec_lua
8
9describe('|api-fast| functions', function()
10  before_each(clear)
11
12  it('callable in a fast event', function()
13    local out = exec_lua(function()
14      local result
15      local timer = assert(vim.uv.new_timer())
16      timer:start(0, 0, function()
17        timer:close()
18        local ok
19        ok, result = pcall(function()
20          return {
21            byteidx = vim.fn.byteidx('héllo', 2),
22            char2nr = vim.fn.char2nr('A'),
23            charidx = vim.fn.charidx('héllo', 3),
24            in_fast = vim.in_fast_event(),
25            keycode = vim.keycode('<C-a>'),
26            keytrans = vim.fn.keytrans(vim.keycode('<C-Home>')),
27            nr2char = vim.fn.nr2char(65),
28            nvim_create_autocmd = vim.api.nvim_create_autocmd('User', {
29              pattern = 'Fast',
30              callback = function() end,
31            }) > 0,
32            nvim_replace_termcodes = vim.api.nvim_replace_termcodes('<Esc>', true, true, true),
33            str2list = vim.fn.str2list('AB'),
34            strcharlen = vim.fn.strcharlen('abc'),
35            strcharpart = vim.fn.strcharpart('héllo', 1, 2),
36            strchars = vim.fn.strchars('abc'),
37            strdisplaywidth = vim.fn.strdisplaywidth('ab'),
38            strgetchar = vim.fn.strgetchar('abc', 1),
39            strlen = vim.fn.strlen('abc'),
40            strpart = vim.fn.strpart('abcdef', 1, 3),
41            strtrans = vim.fn.strtrans('a\001b'),
42            tr = vim.fn.tr('abc', 'ab', 'AB'),
43            trim = vim.fn.trim('  x  '),
44            utf16idx = vim.fn.utf16idx('abc', 2),
45          }
46        end)
47      end)
48      vim.wait(2000, function()
49        return result ~= nil
50      end)
51      return result
52    end)
53    eq({
54      byteidx = 3,
55      char2nr = 65,
56      charidx = 2,
57      in_fast = true,
58      keycode = '\1', -- <C-a>
59      keytrans = '<C-Home>',
60      nr2char = 'A',
61      nvim_create_autocmd = true,
62      nvim_replace_termcodes = '\27', -- <Esc>
63      str2list = { 65, 66 },
64      strcharlen = 3,
65      strcharpart = 'él',
66      strchars = 3,
67      strdisplaywidth = 2,
68      strgetchar = 98,
69      strlen = 3,
70      strpart = 'bcd',
71      strtrans = 'a^Ab',
72      tr = 'ABc',
73      trim = 'x',
74      utf16idx = 2,
75    }, out)
76  end)
77
78  it('do not trigger os_breakcheck()', function()
79    local res = exec_lua(function()
80      local polls = 0
81      -- os_breakcheck() polls the event loop (loop_poll_events), which fires a uv_check handle.
82      local chk = assert(vim.uv.new_check())
83      chk:start(function()
84        polls = polls + 1
85      end)
86      local function polled(fn)
87        polls = 0
88        fn()
89        return polls ~= 0
90      end
91
92      -- Big (multibyte) input => per-char breakcheck would exceed BREAKCHECK_SKIP (1000, or 100_000
93      -- for `veryfast_breakcheck`). Small input could pass (false negative).
94      local big = ('héllo wörld '):rep(100000) -- 1.2M chars.
95
96      local aupat = ('a'):rep(80000) -- Must stay under the "E339: Pattern too long" limit.
97      vim.api.nvim_create_autocmd('User', { pattern = aupat, callback = function() end })
98
99      local cases = {
100        byteidx = function()
101          vim.fn.byteidx(big, 500000)
102        end,
103        char2nr = function()
104          vim.fn.char2nr('A')
105        end,
106        charidx = function()
107          vim.fn.charidx(big, 500000)
108        end,
109        keycode = function()
110          vim.keycode('<C-a>')
111        end,
112        keytrans = function()
113          vim.fn.keytrans(vim.keycode('<C-Home>'))
114        end,
115        nr2char = function()
116          vim.fn.nr2char(65)
117        end,
118        nvim_create_autocmd = function()
119          vim.api.nvim_exec_autocmds('User', { pattern = aupat })
120        end,
121        nvim_replace_termcodes = function()
122          vim.api.nvim_replace_termcodes('<Esc>', true, true, true)
123        end,
124        str2list = function()
125          vim.fn.str2list(big)
126        end,
127        strcharlen = function()
128          vim.fn.strcharlen(big)
129        end,
130        strcharpart = function()
131          vim.fn.strcharpart(big, 1, 2)
132        end,
133        strchars = function()
134          vim.fn.strchars(big)
135        end,
136        strdisplaywidth = function()
137          vim.fn.strdisplaywidth(big)
138        end,
139        strgetchar = function()
140          vim.fn.strgetchar(big, 500000)
141        end,
142        strlen = function()
143          vim.fn.strlen(big)
144        end,
145        strpart = function()
146          vim.fn.strpart(big, 1, 3)
147        end,
148        strtrans = function()
149          vim.fn.strtrans(big)
150        end,
151        tr = function()
152          vim.fn.tr(big, 'ho', 'HO')
153        end,
154        trim = function()
155          vim.fn.trim(big)
156        end,
157        utf16idx = function()
158          vim.fn.utf16idx(big, 500000)
159        end,
160      }
161
162      local bad = {}
163      for name, fn in pairs(cases) do
164        if polled(fn) then
165          bad[#bad + 1] = name
166        end
167      end
168      table.sort(bad)
169
170      -- Positive control: a big Vimscript loop calls line_breakcheck() every iteration.
171      local control = polled(function()
172        vim.api.nvim_exec2('for i in range(1500000)|endfor', {})
173      end)
174
175      chk:stop()
176      chk:close()
177      return { bad = bad, control = control }
178    end)
179
180    eq(true, res.control) -- Confirm our detector actually works: breakcheck was seen.
181    eq({}, res.bad) -- No api-fast function polled the event-loop.
182  end)
183end)