robin/laufey

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

zeertzjqfix(ctx): missing options copy when loading hidden buffer #4114930b4c1b40d

master
11.4 KiB342 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
5local eq = t.eq
6local clear = n.clear
7local fn = n.fn
8local api = n.api
9local command = n.command
10local rmdir = n.rmdir
11local pcall_err = t.pcall_err
12local mkdir = t.mkdir
13
14local fname = 'Xtest-functional-eval-buf_functions'
15local fname2 = fname .. '.2'
16local dirname = fname .. '.d'
17
18before_each(clear)
19
20for _, func in ipairs({
21  'bufname(%s)',
22  'bufnr(%s)',
23  'bufwinnr(%s)',
24  'getbufline(%s, 1)',
25  'getbufvar(%s, "changedtick")',
26  'setbufvar(%s, "f", 0)',
27}) do
28  local funcname = func:match('%w+')
29  describe(funcname .. '() function', function()
30    it('errors out when receives v:true/v:false/v:null', function()
31      -- Not compatible with Vim: in Vim it always results in buffer not found
32      -- without any error messages.
33      for _, var in ipairs({ 'v:true', 'v:false' }) do
34        eq(
35          'Vim(call):E5299: Expected a Number or a String, Boolean found',
36          pcall_err(command, 'call ' .. func:format(var))
37        )
38      end
39      eq(
40        'Vim(call):E5300: Expected a Number or a String',
41        pcall_err(command, 'call ' .. func:format('v:null'))
42      )
43    end)
44    it('errors out when receives invalid argument', function()
45      eq(
46        'Vim(call):E745: Expected a Number or a String, List found',
47        pcall_err(command, 'call ' .. func:format('[]'))
48      )
49      eq(
50        'Vim(call):E728: Expected a Number or a String, Dictionary found',
51        pcall_err(command, 'call ' .. func:format('{}'))
52      )
53      eq(
54        'Vim(call):E805: Expected a Number or a String, Float found',
55        pcall_err(command, 'call ' .. func:format('0.0'))
56      )
57      eq(
58        'Vim(call):E703: Expected a Number or a String, Funcref found',
59        pcall_err(command, 'call ' .. func:format('function("tr")'))
60      )
61    end)
62  end)
63end
64
65describe('bufname() function', function()
66  it('returns empty string when buffer was not found', function()
67    command('file ' .. fname)
68    eq('', fn.bufname(2))
69    eq('', fn.bufname('non-existent-buffer'))
70    eq('', fn.bufname('#'))
71    command('edit ' .. fname2)
72    eq(2, fn.bufnr('%'))
73    eq('', fn.bufname('X'))
74  end)
75  before_each(function()
76    mkdir(dirname)
77  end)
78  after_each(function()
79    rmdir(dirname)
80  end)
81  it('returns expected buffer name', function()
82    eq('', fn.bufname('%')) -- Buffer has no name yet
83    command('file ' .. fname)
84    local wd = assert(t.fix_slashes(assert(vim.uv.cwd())))
85    local sep = '/'
86    local curdirname = fn.fnamemodify(wd, ':t')
87    for _, arg in ipairs({ '%', 1, 'X', wd }) do
88      eq(fname, fn.bufname(arg))
89      api.nvim_set_current_dir('..')
90      eq(curdirname .. sep .. fname, fn.bufname(arg))
91      api.nvim_set_current_dir(curdirname)
92      api.nvim_set_current_dir(dirname)
93      eq(wd .. sep .. fname, fn.bufname(arg))
94      api.nvim_set_current_dir('..')
95      eq(fname, fn.bufname(arg))
96      command('enew')
97    end
98    eq('', fn.bufname('%'))
99    eq('', fn.bufname('$'))
100    eq(2, fn.bufnr('%'))
101  end)
102end)
103
104describe('bufnr() function', function()
105  it('returns -1 when buffer was not found', function()
106    command('file ' .. fname)
107    eq(-1, fn.bufnr(2))
108    eq(-1, fn.bufnr('non-existent-buffer'))
109    eq(-1, fn.bufnr('#'))
110    command('edit ' .. fname2)
111    eq(2, fn.bufnr('%'))
112    eq(-1, fn.bufnr('X'))
113  end)
114  it('returns expected buffer number', function()
115    eq(1, fn.bufnr('%'))
116    command('file ' .. fname)
117    local wd = vim.uv.cwd()
118    local curdirname = fn.fnamemodify(wd, ':t')
119    eq(1, fn.bufnr(fname))
120    eq(1, fn.bufnr(wd))
121    eq(1, fn.bufnr(curdirname))
122    eq(1, fn.bufnr('X'))
123  end)
124  it('returns number of last buffer with "$"', function()
125    eq(1, fn.bufnr('$'))
126    command('new')
127    eq(2, fn.bufnr('$'))
128    command('new')
129    eq(3, fn.bufnr('$'))
130    command('only')
131    eq(3, fn.bufnr('$'))
132    eq(3, fn.bufnr('%'))
133    command('buffer 1')
134    eq(3, fn.bufnr('$'))
135    eq(1, fn.bufnr('%'))
136    command('bwipeout 2')
137    eq(3, fn.bufnr('$'))
138    eq(1, fn.bufnr('%'))
139    command('bwipeout 3')
140    eq(1, fn.bufnr('$'))
141    eq(1, fn.bufnr('%'))
142    command('new')
143    eq(4, fn.bufnr('$'))
144  end)
145end)
146
147describe('bufwinnr() function', function()
148  it('returns -1 when buffer was not found', function()
149    command('file ' .. fname)
150    eq(-1, fn.bufwinnr(2))
151    eq(-1, fn.bufwinnr('non-existent-buffer'))
152    eq(-1, fn.bufwinnr('#'))
153    command('split ' .. fname2) -- It would be OK if there was one window
154    eq(2, fn.bufnr('%'))
155    eq(-1, fn.bufwinnr('X'))
156  end)
157  before_each(function()
158    mkdir(dirname)
159  end)
160  after_each(function()
161    rmdir(dirname)
162  end)
163  it('returns expected window number', function()
164    eq(1, fn.bufwinnr('%'))
165    command('file ' .. fname)
166    command('vsplit')
167    command('split ' .. fname2)
168    eq(2, fn.bufwinnr(fname))
169    eq(1, fn.bufwinnr(fname2))
170    eq(-1, fn.bufwinnr(fname:sub(1, #fname - 1)))
171    api.nvim_set_current_dir(dirname)
172    eq(2, fn.bufwinnr(fname))
173    eq(1, fn.bufwinnr(fname2))
174    eq(-1, fn.bufwinnr(fname:sub(1, #fname - 1)))
175    eq(1, fn.bufwinnr('%'))
176    eq(2, fn.bufwinnr(1))
177    eq(1, fn.bufwinnr(2))
178    eq(-1, fn.bufwinnr(3))
179    eq(1, fn.bufwinnr('$'))
180  end)
181end)
182
183describe('getbufline() function', function()
184  it('returns empty list when buffer was not found', function()
185    command('file ' .. fname)
186    eq({}, fn.getbufline(2, 1))
187    eq({}, fn.getbufline('non-existent-buffer', 1))
188    eq({}, fn.getbufline('#', 1))
189    command('edit ' .. fname2)
190    eq(2, fn.bufnr('%'))
191    eq({}, fn.getbufline('X', 1))
192  end)
193  it('returns empty list when range is invalid', function()
194    eq({}, fn.getbufline(1, 0))
195    eq({}, fn.getbufline(-1, 1, '$'))
196    eq({}, fn.getbufline(-1, '$', '$'))
197    api.nvim_buf_set_lines(0, 0, 1, false, { 'foo', 'bar', 'baz' })
198    eq({}, fn.getbufline(1, 2, 1))
199    eq({}, fn.getbufline(1, -10, -20))
200    eq({}, fn.getbufline(1, -2, -1))
201    eq({}, fn.getbufline(1, -1, 9999))
202    eq({}, fn.getbufline(-1, 1, '$'))
203    eq({}, fn.getbufline(-1, '$', '$'))
204  end)
205  it('returns expected lines', function()
206    api.nvim_set_option_value('hidden', true, {})
207    command('file ' .. fname)
208    api.nvim_buf_set_lines(0, 0, 1, false, { 'foo\0', '\0bar', 'baz' })
209    command('edit ' .. fname2)
210    api.nvim_buf_set_lines(0, 0, 1, false, { 'abc\0', '\0def', 'ghi' })
211    eq({ 'foo\n', '\nbar', 'baz' }, fn.getbufline(1, 1, 9999))
212    eq({ 'abc\n', '\ndef', 'ghi' }, fn.getbufline(2, 1, 9999))
213    eq({ 'foo\n', '\nbar', 'baz' }, fn.getbufline(1, 1, '$'))
214    eq({ 'baz' }, fn.getbufline(1, '$', '$'))
215    eq({ 'baz' }, fn.getbufline(1, '$', 9999))
216  end)
217end)
218
219describe('getbufvar() function', function()
220  it('returns empty list when buffer was not found', function()
221    command('file ' .. fname)
222    eq('', fn.getbufvar(2, '&autoindent'))
223    eq('', fn.getbufvar('non-existent-buffer', '&autoindent'))
224    eq('', fn.getbufvar('#', '&autoindent'))
225    command('edit ' .. fname2)
226    eq(2, fn.bufnr('%'))
227    eq('', fn.getbufvar('X', '&autoindent'))
228  end)
229  it('returns empty list when variable/option/etc was not found', function()
230    command('file ' .. fname)
231    eq('', fn.getbufvar(1, '&autondent'))
232    eq('', fn.getbufvar(1, 'changedtic'))
233  end)
234  it('returns expected option value', function()
235    eq(0, fn.getbufvar(1, '&autoindent'))
236    eq(0, fn.getbufvar(1, '&l:autoindent'))
237    eq(0, fn.getbufvar(1, '&g:autoindent'))
238    -- Also works with global-only options
239    eq(1, fn.getbufvar(1, '&hidden'))
240    eq(1, fn.getbufvar(1, '&l:hidden'))
241    eq(1, fn.getbufvar(1, '&g:hidden'))
242    -- Also works with window-local options
243    eq(0, fn.getbufvar(1, '&number'))
244    eq(0, fn.getbufvar(1, '&l:number'))
245    eq(0, fn.getbufvar(1, '&g:number'))
246    command('new')
247    -- But with window-local options it probably does not what you expect
248    command('setl number')
249    -- (note that current window’s buffer is 2, but getbufvar() receives 1)
250    eq(2, api.nvim_win_get_buf(0))
251    eq(1, fn.getbufvar(1, '&number'))
252    eq(1, fn.getbufvar(1, '&l:number'))
253    -- You can get global value though, if you find this useful.
254    eq(0, fn.getbufvar(1, '&g:number'))
255  end)
256  it('returns expected variable value', function()
257    eq(2, fn.getbufvar(1, 'changedtick'))
258    api.nvim_buf_set_lines(0, 0, 1, false, { 'abc\0', '\0def', 'ghi' })
259    eq(3, fn.getbufvar(1, 'changedtick'))
260    api.nvim_buf_set_var(0, 'test', true)
261    eq(true, fn.getbufvar(1, 'test'))
262    eq({ test = true, changedtick = 3 }, fn.getbufvar(1, ''))
263    command('new')
264    eq(3, fn.getbufvar(1, 'changedtick'))
265    eq(true, fn.getbufvar(1, 'test'))
266    eq({ test = true, changedtick = 3 }, fn.getbufvar(1, ''))
267  end)
268end)
269
270describe('setbufvar() function', function()
271  it('throws the error or ignores the input when buffer was not found', function()
272    command('file ' .. fname)
273    command('call setbufvar(2, "&autoindent", 0)')
274    eq(
275      'Vim(call):E94: No matching buffer for non-existent-buffer',
276      pcall_err(command, 'call setbufvar("non-existent-buffer", "&autoindent", 0)')
277    )
278    command('call setbufvar("#", "&autoindent", 0)')
279    command('edit ' .. fname2)
280    eq(2, fn.bufnr('%'))
281    eq(
282      'Vim(call):E93: More than one match for X',
283      pcall_err(command, 'call setbufvar("X", "&autoindent", 0)')
284    )
285  end)
286  it('may set options, including window-local and global values', function()
287    local buf1 = api.nvim_get_current_buf()
288    eq(false, api.nvim_get_option_value('number', {}))
289    command('split')
290    command('new')
291    eq(2, api.nvim_buf_get_number(api.nvim_win_get_buf(0)))
292    fn.setbufvar(1, '&number', true)
293    local windows = api.nvim_tabpage_list_wins(0)
294    eq(false, api.nvim_get_option_value('number', { win = windows[1] }))
295    eq(true, api.nvim_get_option_value('number', { win = windows[2] }))
296    eq(false, api.nvim_get_option_value('number', { win = windows[3] }))
297    eq(false, api.nvim_get_option_value('number', { win = api.nvim_get_current_win() }))
298
299    eq(true, api.nvim_get_option_value('hidden', {}))
300    fn.setbufvar(1, '&hidden', 0)
301    eq(false, api.nvim_get_option_value('hidden', {}))
302
303    eq(false, api.nvim_get_option_value('autoindent', { buf = buf1 }))
304    fn.setbufvar(1, '&autoindent', true)
305    eq(true, api.nvim_get_option_value('autoindent', { buf = buf1 }))
306    eq('Vim(call):E355: Unknown option: xxx', pcall_err(command, 'call setbufvar(1, "&xxx", 0)'))
307  end)
308  it('may set variables', function()
309    local buf1 = api.nvim_get_current_buf()
310    command('split')
311    command('new')
312    eq(2, api.nvim_buf_get_number(0))
313    fn.setbufvar(1, 'number', true)
314    eq(true, api.nvim_buf_get_var(buf1, 'number'))
315    eq('Vim(call):E461: Illegal variable name: b:', pcall_err(command, 'call setbufvar(1, "", 0)'))
316    eq(true, api.nvim_buf_get_var(buf1, 'number'))
317    eq(
318      'Vim:E46: Cannot change read-only variable "b:changedtick"',
319      pcall_err(fn.setbufvar, 1, 'changedtick', true)
320    )
321    eq(2, fn.getbufvar(1, 'changedtick'))
322  end)
323  it('throws error when setting a string option to a boolean value vim-patch:9.0.0090', function()
324    eq('Vim:E928: String required', pcall_err(fn.setbufvar, '', '&errorformat', true))
325  end)
326end)
327
328describe('bufload() function', function()
329  before_each(function()
330    t.write_file(fname, 'foo\n')
331  end)
332  after_each(function()
333    os.remove(fname)
334  end)
335  it('does not set hidden buffer as modified #41148', function()
336    local buf = fn.bufadd(fname)
337    fn.bufload(buf)
338    api.nvim_command('edit ' .. fname)
339    eq(true, api.nvim_buf_is_loaded(buf))
340    eq(false, api.nvim_get_option_value('modified', { buf = buf }))
341  end)
342end)