deprecated_spec.lua (7346B)
1 -- Island of misfit toys. 2 --- @diagnostic disable: deprecated 3 4 local t = require('test.testutil') 5 local n = require('test.functional.testnvim')() 6 local Screen = require('test.functional.ui.screen') 7 local clear, eval, eq, ok = n.clear, n.eval, t.eq, t.ok 8 local api, command, fn = n.api, n.command, n.fn 9 local pcall_err, assert_alive = t.pcall_err, n.assert_alive 10 11 describe('deprecated', function() 12 before_each(n.clear) 13 14 describe('nvim_notify', function() 15 it('can notify a info message', function() 16 n.api.nvim_notify('hello world', 2, {}) 17 end) 18 19 it('can be overridden', function() 20 n.command('lua vim.notify = function(...) return 42 end') 21 t.eq(42, n.api.nvim_exec_lua("return vim.notify('Hello world')", {})) 22 n.api.nvim_notify('hello world', 4, {}) 23 end) 24 end) 25 26 describe('nvim_*get_option functions', function() 27 it('does not leak memory', function() 28 -- String opts caused memory leaks in these functions in Github#32361 29 n.exec_lua([[ 30 vim.api.nvim_get_option('rtp') 31 vim.api.nvim_win_get_option(vim.api.nvim_get_current_win(), 'foldmethod') 32 vim.api.nvim_buf_get_option(0, 'fileformat') 33 ]]) 34 end) 35 end) 36 37 describe('API: highlight', function() 38 clear() 39 Screen.new() -- initialize Screen.colors 40 41 local expected_rgb = { 42 background = Screen.colors.Yellow, 43 foreground = Screen.colors.Red, 44 special = Screen.colors.Blue, 45 bold = true, 46 } 47 local expected_cterm = { 48 background = 10, 49 underline = true, 50 } 51 local expected_rgb2 = { 52 background = Screen.colors.Yellow, 53 foreground = Screen.colors.Red, 54 special = Screen.colors.Blue, 55 bold = true, 56 italic = true, 57 reverse = true, 58 underline = true, 59 strikethrough = true, 60 altfont = true, 61 nocombine = true, 62 } 63 local expected_undercurl = { 64 background = Screen.colors.Yellow, 65 foreground = Screen.colors.Red, 66 special = Screen.colors.Blue, 67 undercurl = true, 68 } 69 70 before_each(function() 71 clear() 72 command( 73 'hi NewHighlight cterm=underline ctermbg=green guifg=red guibg=yellow guisp=blue gui=bold' 74 ) 75 end) 76 77 it('nvim_get_hl_by_id', function() 78 local hl_id = eval("hlID('NewHighlight')") 79 eq(expected_cterm, api.nvim_get_hl_by_id(hl_id, false)) 80 81 hl_id = eval("hlID('NewHighlight')") 82 -- Test valid id. 83 eq(expected_rgb, api.nvim_get_hl_by_id(hl_id, true)) 84 85 -- Test invalid id. 86 eq('Invalid highlight id: 30000', pcall_err(api.nvim_get_hl_by_id, 30000, false)) 87 88 -- Test all highlight properties. 89 command('hi NewHighlight gui=underline,bold,italic,reverse,strikethrough,altfont,nocombine') 90 eq(expected_rgb2, api.nvim_get_hl_by_id(hl_id, true)) 91 92 -- Test undercurl 93 command('hi NewHighlight gui=undercurl') 94 eq(expected_undercurl, api.nvim_get_hl_by_id(hl_id, true)) 95 96 -- Test nil argument. 97 eq( 98 'Wrong type for argument 1 when calling nvim_get_hl_by_id, expecting Integer', 99 pcall_err(api.nvim_get_hl_by_id, { nil }, false) 100 ) 101 102 -- Test 0 argument. 103 eq('Invalid highlight id: 0', pcall_err(api.nvim_get_hl_by_id, 0, false)) 104 105 -- Test -1 argument. 106 eq('Invalid highlight id: -1', pcall_err(api.nvim_get_hl_by_id, -1, false)) 107 108 -- Test highlight group without ctermbg value. 109 command('hi Normal ctermfg=red ctermbg=yellow') 110 command('hi NewConstant ctermfg=green guifg=white guibg=blue') 111 hl_id = eval("hlID('NewConstant')") 112 eq({ foreground = 10 }, api.nvim_get_hl_by_id(hl_id, false)) 113 114 -- Test highlight group without ctermfg value. 115 command('hi clear NewConstant') 116 command('hi NewConstant ctermbg=Magenta guifg=white guibg=blue') 117 eq({ background = 13 }, api.nvim_get_hl_by_id(hl_id, false)) 118 119 -- Test highlight group with ctermfg and ctermbg values. 120 command('hi clear NewConstant') 121 command('hi NewConstant ctermfg=green ctermbg=Magenta guifg=white guibg=blue') 122 eq({ foreground = 10, background = 13 }, api.nvim_get_hl_by_id(hl_id, false)) 123 end) 124 125 it('nvim_get_hl_by_name', function() 126 local expected_normal = { background = Screen.colors.Yellow, foreground = Screen.colors.Red } 127 128 -- Test `Normal` default values. 129 eq({}, api.nvim_get_hl_by_name('Normal', true)) 130 131 eq(expected_cterm, api.nvim_get_hl_by_name('NewHighlight', false)) 132 eq(expected_rgb, api.nvim_get_hl_by_name('NewHighlight', true)) 133 134 -- Test `Normal` modified values. 135 command('hi Normal guifg=red guibg=yellow') 136 eq(expected_normal, api.nvim_get_hl_by_name('Normal', true)) 137 138 -- Test invalid name. 139 eq( 140 "Invalid highlight name: 'unknown_highlight'", 141 pcall_err(api.nvim_get_hl_by_name, 'unknown_highlight', false) 142 ) 143 144 -- Test nil argument. 145 eq( 146 'Wrong type for argument 1 when calling nvim_get_hl_by_name, expecting String', 147 pcall_err(api.nvim_get_hl_by_name, { nil }, false) 148 ) 149 150 -- Test empty string argument. 151 eq('Invalid highlight name', pcall_err(api.nvim_get_hl_by_name, '', false)) 152 153 -- Test "standout" attribute. #8054 154 eq({ underline = true }, api.nvim_get_hl_by_name('cursorline', false)) 155 command( 156 'hi CursorLine cterm=standout,underline term=standout,underline gui=standout,underline' 157 ) 158 command('set cursorline') 159 eq({ underline = true, standout = true }, api.nvim_get_hl_by_name('cursorline', false)) 160 161 -- Test cterm & Normal values. #18024 (tail) & #18980 162 -- Ensure Normal, and groups that match Normal return their fg & bg cterm values 163 api.nvim_set_hl(0, 'Normal', { ctermfg = 17, ctermbg = 213 }) 164 api.nvim_set_hl(0, 'NotNormal', { ctermfg = 17, ctermbg = 213, nocombine = true }) 165 -- Note colors are "cterm" values, not rgb-as-ints 166 eq({ foreground = 17, background = 213 }, api.nvim_get_hl_by_name('Normal', false)) 167 eq( 168 { foreground = 17, background = 213, nocombine = true }, 169 api.nvim_get_hl_by_name('NotNormal', false) 170 ) 171 end) 172 173 it('nvim_get_hl_id_by_name', function() 174 -- precondition: use a hl group that does not yet exist 175 eq( 176 "Invalid highlight name: 'Shrubbery'", 177 pcall_err(api.nvim_get_hl_by_name, 'Shrubbery', true) 178 ) 179 eq(0, fn.hlID('Shrubbery')) 180 181 local hl_id = api.nvim_get_hl_id_by_name('Shrubbery') 182 ok(hl_id > 0) 183 eq(hl_id, fn.hlID('Shrubbery')) 184 185 command('hi Shrubbery guifg=#888888 guibg=#888888') 186 eq( 187 { foreground = tonumber('0x888888'), background = tonumber('0x888888') }, 188 api.nvim_get_hl_by_id(hl_id, true) 189 ) 190 eq( 191 { foreground = tonumber('0x888888'), background = tonumber('0x888888') }, 192 api.nvim_get_hl_by_name('Shrubbery', true) 193 ) 194 end) 195 196 it("nvim_buf_add_highlight to other buffer doesn't crash if undo is disabled #12873", function() 197 command('vsplit file') 198 local err, _ = pcall(api.nvim_set_option_value, 'undofile', false, { buf = 1 }) 199 eq(true, err) 200 err, _ = pcall(api.nvim_set_option_value, 'undolevels', -1, { buf = 1 }) 201 eq(true, err) 202 err, _ = pcall(api.nvim_buf_add_highlight, 1, -1, 'Question', 0, 0, -1) 203 eq(true, err) 204 assert_alive() 205 end) 206 end) 207 end)