robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
10.4 KiB311 linesraw
1-- Island of misfit toys.
2--- @diagnostic disable: deprecated
3
4local t = require('test.testutil')
5local n = require('test.functional.testnvim')()
6local Screen = require('test.functional.ui.screen')
7local describe, it, before_each = t.describe, t.it, t.before_each
8local clear, eval, eq, ok = n.clear, n.eval, t.eq, t.ok
9local api, command, fn = n.api, n.command, n.fn
10local pcall_err, assert_alive = t.pcall_err, n.assert_alive
11local insert, exec, feed = n.insert, n.exec, n.feed
12
13describe('deprecated', function()
14  before_each(n.clear)
15
16  describe('nvim_notify', function()
17    it('can notify a info message', function()
18      n.api.nvim_notify('hello world', 2, {})
19    end)
20
21    it('can be overridden', function()
22      n.command('lua vim.notify = function(...) return 42 end')
23      t.eq(42, n.api.nvim_exec_lua("return vim.notify('Hello world')", {}))
24      n.api.nvim_notify('hello world', 4, {})
25    end)
26  end)
27
28  describe('nvim_*get_option functions', function()
29    it('does not leak memory', function()
30      -- String opts caused memory leaks in these functions in Github#32361
31      n.exec_lua([[
32        vim.api.nvim_get_option('rtp')
33        vim.api.nvim_win_get_option(vim.api.nvim_get_current_win(), 'foldmethod')
34        vim.api.nvim_buf_get_option(0, 'fileformat')
35      ]])
36    end)
37  end)
38
39  describe('API: highlight', function()
40    clear()
41    Screen.new() -- initialize Screen.colors
42
43    local expected_rgb = {
44      background = Screen.colors.Yellow,
45      foreground = Screen.colors.Red,
46      special = Screen.colors.Blue,
47      bold = true,
48    }
49    local expected_cterm = {
50      background = 10,
51      underline = true,
52    }
53    local expected_rgb2 = {
54      background = Screen.colors.Yellow,
55      foreground = Screen.colors.Red,
56      special = Screen.colors.Blue,
57      bold = true,
58      italic = true,
59      reverse = true,
60      underline = true,
61      strikethrough = true,
62      altfont = true,
63      nocombine = true,
64    }
65    local expected_undercurl = {
66      background = Screen.colors.Yellow,
67      foreground = Screen.colors.Red,
68      special = Screen.colors.Blue,
69      undercurl = true,
70    }
71
72    before_each(function()
73      clear()
74      command(
75        'hi NewHighlight cterm=underline ctermbg=green guifg=red guibg=yellow guisp=blue gui=bold'
76      )
77    end)
78
79    it('nvim_get_hl_by_id', function()
80      local hl_id = eval("hlID('NewHighlight')")
81      eq(expected_cterm, api.nvim_get_hl_by_id(hl_id, false))
82
83      hl_id = eval("hlID('NewHighlight')")
84      -- Test valid id.
85      eq(expected_rgb, api.nvim_get_hl_by_id(hl_id, true))
86
87      -- Test invalid id.
88      eq('Invalid highlight id: 30000', pcall_err(api.nvim_get_hl_by_id, 30000, false))
89
90      -- Test all highlight properties.
91      command('hi NewHighlight gui=underline,bold,italic,reverse,strikethrough,altfont,nocombine')
92      eq(expected_rgb2, api.nvim_get_hl_by_id(hl_id, true))
93
94      -- Test undercurl
95      command('hi NewHighlight gui=undercurl')
96      eq(expected_undercurl, api.nvim_get_hl_by_id(hl_id, true))
97
98      -- Test nil argument.
99      eq(
100        'Wrong type for argument 1 when calling nvim_get_hl_by_id, expecting Integer',
101        pcall_err(api.nvim_get_hl_by_id, { nil }, false)
102      )
103
104      -- Test 0 argument.
105      eq('Invalid highlight id: 0', pcall_err(api.nvim_get_hl_by_id, 0, false))
106
107      -- Test -1 argument.
108      eq('Invalid highlight id: -1', pcall_err(api.nvim_get_hl_by_id, -1, false))
109
110      -- Test highlight group without ctermbg value.
111      command('hi Normal ctermfg=red ctermbg=yellow')
112      command('hi NewConstant ctermfg=green guifg=white guibg=blue')
113      hl_id = eval("hlID('NewConstant')")
114      eq({ foreground = 10 }, api.nvim_get_hl_by_id(hl_id, false))
115
116      -- Test highlight group without ctermfg value.
117      command('hi clear NewConstant')
118      command('hi NewConstant ctermbg=Magenta guifg=white guibg=blue')
119      eq({ background = 13 }, api.nvim_get_hl_by_id(hl_id, false))
120
121      -- Test highlight group with ctermfg and ctermbg values.
122      command('hi clear NewConstant')
123      command('hi NewConstant ctermfg=green ctermbg=Magenta guifg=white guibg=blue')
124      eq({ foreground = 10, background = 13 }, api.nvim_get_hl_by_id(hl_id, false))
125    end)
126
127    it('nvim_get_hl_by_name', function()
128      local expected_normal = { background = Screen.colors.Yellow, foreground = Screen.colors.Red }
129
130      -- Test `Normal` default values.
131      eq({}, api.nvim_get_hl_by_name('Normal', true))
132
133      eq(expected_cterm, api.nvim_get_hl_by_name('NewHighlight', false))
134      eq(expected_rgb, api.nvim_get_hl_by_name('NewHighlight', true))
135
136      -- Test `Normal` modified values.
137      command('hi Normal guifg=red guibg=yellow')
138      eq(expected_normal, api.nvim_get_hl_by_name('Normal', true))
139
140      -- Test invalid name.
141      eq(
142        "Invalid highlight name: 'unknown_highlight'",
143        pcall_err(api.nvim_get_hl_by_name, 'unknown_highlight', false)
144      )
145
146      -- Test nil argument.
147      eq(
148        'Wrong type for argument 1 when calling nvim_get_hl_by_name, expecting String',
149        pcall_err(api.nvim_get_hl_by_name, { nil }, false)
150      )
151
152      -- Test empty string argument.
153      eq('Invalid highlight name', pcall_err(api.nvim_get_hl_by_name, '', false))
154
155      -- Test "standout" attribute. #8054
156      eq({ underline = true }, api.nvim_get_hl_by_name('cursorline', false))
157      command(
158        'hi CursorLine cterm=standout,underline term=standout,underline gui=standout,underline'
159      )
160      command('set cursorline')
161      eq({ underline = true, standout = true }, api.nvim_get_hl_by_name('cursorline', false))
162
163      -- Test cterm & Normal values. #18024 (tail) & #18980
164      -- Ensure Normal, and groups that match Normal return their fg & bg cterm values
165      api.nvim_set_hl(0, 'Normal', { ctermfg = 17, ctermbg = 213 })
166      api.nvim_set_hl(0, 'NotNormal', { ctermfg = 17, ctermbg = 213, nocombine = true })
167      -- Note colors are "cterm" values, not rgb-as-ints
168      eq({ foreground = 17, background = 213 }, api.nvim_get_hl_by_name('Normal', false))
169      eq(
170        { foreground = 17, background = 213, nocombine = true },
171        api.nvim_get_hl_by_name('NotNormal', false)
172      )
173    end)
174
175    it('nvim_get_hl_id_by_name', function()
176      -- precondition: use a hl group that does not yet exist
177      eq(
178        "Invalid highlight name: 'Shrubbery'",
179        pcall_err(api.nvim_get_hl_by_name, 'Shrubbery', true)
180      )
181      eq(0, fn.hlID('Shrubbery'))
182
183      local hl_id = api.nvim_get_hl_id_by_name('Shrubbery')
184      ok(hl_id > 0)
185      eq(hl_id, fn.hlID('Shrubbery'))
186
187      command('hi Shrubbery guifg=#888888 guibg=#888888')
188      eq(
189        { foreground = tonumber('0x888888'), background = tonumber('0x888888') },
190        api.nvim_get_hl_by_id(hl_id, true)
191      )
192      eq(
193        { foreground = tonumber('0x888888'), background = tonumber('0x888888') },
194        api.nvim_get_hl_by_name('Shrubbery', true)
195      )
196    end)
197
198    it("nvim_buf_add_highlight to other buffer doesn't crash if undo is disabled #12873", function()
199      command('vsplit file')
200      local err, _ = pcall(api.nvim_set_option_value, 'undofile', false, { buf = 1 })
201      eq(true, err)
202      err, _ = pcall(api.nvim_set_option_value, 'undolevels', -1, { buf = 1 })
203      eq(true, err)
204      err, _ = pcall(api.nvim_buf_add_highlight, 1, -1, 'Question', 0, 0, -1)
205      eq(true, err)
206      assert_alive()
207    end)
208  end)
209
210  describe('{get,set}_height', function()
211    it('works', function()
212      command('vsplit')
213      eq(
214        api.nvim_win_get_height(api.nvim_list_wins()[2]),
215        api.nvim_win_get_height(api.nvim_list_wins()[1])
216      )
217      api.nvim_set_current_win(api.nvim_list_wins()[2])
218      command('split')
219      eq(
220        api.nvim_win_get_height(api.nvim_list_wins()[2]),
221        math.floor(api.nvim_win_get_height(api.nvim_list_wins()[1]) / 2)
222      )
223      api.nvim_win_set_height(api.nvim_list_wins()[2], 2)
224      eq(2, api.nvim_win_get_height(api.nvim_list_wins()[2]))
225    end)
226
227    it('failure modes', function()
228      command('split')
229      eq('Invalid window id: 999999', pcall_err(api.nvim_win_set_height, 999999, 10))
230      eq(
231        'Wrong type for argument 2 when calling nvim_win_set_height, expecting Integer',
232        pcall_err(api.nvim_win_set_height, 0, 0.9)
233      )
234    end)
235
236    it('correctly handles height=1', function()
237      command('split')
238      api.nvim_set_current_win(api.nvim_list_wins()[1])
239      api.nvim_win_set_height(api.nvim_list_wins()[2], 1)
240      eq(1, api.nvim_win_get_height(api.nvim_list_wins()[2]))
241    end)
242
243    it('correctly handles height=1 with a winbar', function()
244      command('set winbar=foobar')
245      command('set winminheight=0')
246      command('split')
247      api.nvim_set_current_win(api.nvim_list_wins()[1])
248      api.nvim_win_set_height(api.nvim_list_wins()[2], 1)
249      eq(1, api.nvim_win_get_height(api.nvim_list_wins()[2]))
250    end)
251
252    it('do not cause ml_get errors with foldmethod=expr #19989', function()
253      insert([[
254        aaaaa
255        bbbbb
256        ccccc]])
257      command('set foldmethod=expr')
258      exec([[
259        new
260        let w = nvim_get_current_win()
261        wincmd w
262        call nvim_win_set_height(w, 5)
263      ]])
264      feed('l')
265      eq('', api.nvim_get_vvar('errmsg'))
266    end)
267  end)
268
269  describe('{get,set}_width', function()
270    it('works', function()
271      command('split')
272      eq(
273        api.nvim_win_get_width(api.nvim_list_wins()[2]),
274        api.nvim_win_get_width(api.nvim_list_wins()[1])
275      )
276      api.nvim_set_current_win(api.nvim_list_wins()[2])
277      command('vsplit')
278      eq(
279        api.nvim_win_get_width(api.nvim_list_wins()[2]),
280        math.floor(api.nvim_win_get_width(api.nvim_list_wins()[1]) / 2)
281      )
282      api.nvim_win_set_width(api.nvim_list_wins()[2], 2)
283      eq(2, api.nvim_win_get_width(api.nvim_list_wins()[2]))
284    end)
285
286    it('failure modes', function()
287      command('vsplit')
288      eq('Invalid window id: 999999', pcall_err(api.nvim_win_set_width, 999999, 10))
289      eq(
290        'Wrong type for argument 2 when calling nvim_win_set_width, expecting Integer',
291        pcall_err(api.nvim_win_set_width, 0, 0.9)
292      )
293    end)
294
295    it('do not cause ml_get errors with foldmethod=expr #19989', function()
296      insert([[
297        aaaaa
298        bbbbb
299        ccccc]])
300      command('set foldmethod=expr')
301      exec([[
302        vnew
303        let w = nvim_get_current_win()
304        wincmd w
305        call nvim_win_set_width(w, 5)
306      ]])
307      feed('l')
308      eq('', api.nvim_get_vvar('errmsg'))
309    end)
310  end)
311end)