summaryrefslogtreecommitdiffstatshomepage
path: root/test/functional/api/deprecated_spec.lua
blob: 976e093c531cb0a33cf89c595fa5fe2761623f45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
-- Island of misfit toys.
--- @diagnostic disable: deprecated

local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')
local clear, eval, eq, ok = n.clear, n.eval, t.eq, t.ok
local api, command, fn = n.api, n.command, n.fn
local pcall_err, assert_alive = t.pcall_err, n.assert_alive

describe('deprecated', function()
  before_each(n.clear)

  describe('nvim_notify', function()
    it('can notify a info message', function()
      n.api.nvim_notify('hello world', 2, {})
    end)

    it('can be overridden', function()
      n.command('lua vim.notify = function(...) return 42 end')
      t.eq(42, n.api.nvim_exec_lua("return vim.notify('Hello world')", {}))
      n.api.nvim_notify('hello world', 4, {})
    end)
  end)

  describe('nvim_*get_option functions', function()
    it('does not leak memory', function()
      -- String opts caused memory leaks in these functions in Github#32361
      n.exec_lua([[
        vim.api.nvim_get_option('rtp')
        vim.api.nvim_win_get_option(vim.api.nvim_get_current_win(), 'foldmethod')
        vim.api.nvim_buf_get_option(0, 'fileformat')
      ]])
    end)
  end)

  describe('API: highlight', function()
    clear()
    Screen.new() -- initialize Screen.colors

    local expected_rgb = {
      background = Screen.colors.Yellow,
      foreground = Screen.colors.Red,
      special = Screen.colors.Blue,
      bold = true,
    }
    local expected_cterm = {
      background = 10,
      underline = true,
    }
    local expected_rgb2 = {
      background = Screen.colors.Yellow,
      foreground = Screen.colors.Red,
      special = Screen.colors.Blue,
      bold = true,
      italic = true,
      reverse = true,
      underline = true,
      strikethrough = true,
      altfont = true,
      nocombine = true,
    }
    local expected_undercurl = {
      background = Screen.colors.Yellow,
      foreground = Screen.colors.Red,
      special = Screen.colors.Blue,
      undercurl = true,
    }

    before_each(function()
      clear()
      command(
        'hi NewHighlight cterm=underline ctermbg=green guifg=red guibg=yellow guisp=blue gui=bold'
      )
    end)

    it('nvim_get_hl_by_id', function()
      local hl_id = eval("hlID('NewHighlight')")
      eq(expected_cterm, api.nvim_get_hl_by_id(hl_id, false))

      hl_id = eval("hlID('NewHighlight')")
      -- Test valid id.
      eq(expected_rgb, api.nvim_get_hl_by_id(hl_id, true))

      -- Test invalid id.
      eq('Invalid highlight id: 30000', pcall_err(api.nvim_get_hl_by_id, 30000, false))

      -- Test all highlight properties.
      command('hi NewHighlight gui=underline,bold,italic,reverse,strikethrough,altfont,nocombine')
      eq(expected_rgb2, api.nvim_get_hl_by_id(hl_id, true))

      -- Test undercurl
      command('hi NewHighlight gui=undercurl')
      eq(expected_undercurl, api.nvim_get_hl_by_id(hl_id, true))

      -- Test nil argument.
      eq(
        'Wrong type for argument 1 when calling nvim_get_hl_by_id, expecting Integer',
        pcall_err(api.nvim_get_hl_by_id, { nil }, false)
      )

      -- Test 0 argument.
      eq('Invalid highlight id: 0', pcall_err(api.nvim_get_hl_by_id, 0, false))

      -- Test -1 argument.
      eq('Invalid highlight id: -1', pcall_err(api.nvim_get_hl_by_id, -1, false))

      -- Test highlight group without ctermbg value.
      command('hi Normal ctermfg=red ctermbg=yellow')
      command('hi NewConstant ctermfg=green guifg=white guibg=blue')
      hl_id = eval("hlID('NewConstant')")
      eq({ foreground = 10 }, api.nvim_get_hl_by_id(hl_id, false))

      -- Test highlight group without ctermfg value.
      command('hi clear NewConstant')
      command('hi NewConstant ctermbg=Magenta guifg=white guibg=blue')
      eq({ background = 13 }, api.nvim_get_hl_by_id(hl_id, false))

      -- Test highlight group with ctermfg and ctermbg values.
      command('hi clear NewConstant')
      command('hi NewConstant ctermfg=green ctermbg=Magenta guifg=white guibg=blue')
      eq({ foreground = 10, background = 13 }, api.nvim_get_hl_by_id(hl_id, false))
    end)

    it('nvim_get_hl_by_name', function()
      local expected_normal = { background = Screen.colors.Yellow, foreground = Screen.colors.Red }

      -- Test `Normal` default values.
      eq({}, api.nvim_get_hl_by_name('Normal', true))

      eq(expected_cterm, api.nvim_get_hl_by_name('NewHighlight', false))
      eq(expected_rgb, api.nvim_get_hl_by_name('NewHighlight', true))

      -- Test `Normal` modified values.
      command('hi Normal guifg=red guibg=yellow')
      eq(expected_normal, api.nvim_get_hl_by_name('Normal', true))

      -- Test invalid name.
      eq(
        "Invalid highlight name: 'unknown_highlight'",
        pcall_err(api.nvim_get_hl_by_name, 'unknown_highlight', false)
      )

      -- Test nil argument.
      eq(
        'Wrong type for argument 1 when calling nvim_get_hl_by_name, expecting String',
        pcall_err(api.nvim_get_hl_by_name, { nil }, false)
      )

      -- Test empty string argument.
      eq('Invalid highlight name', pcall_err(api.nvim_get_hl_by_name, '', false))

      -- Test "standout" attribute. #8054
      eq({ underline = true }, api.nvim_get_hl_by_name('cursorline', false))
      command(
        'hi CursorLine cterm=standout,underline term=standout,underline gui=standout,underline'
      )
      command('set cursorline')
      eq({ underline = true, standout = true }, api.nvim_get_hl_by_name('cursorline', false))

      -- Test cterm & Normal values. #18024 (tail) & #18980
      -- Ensure Normal, and groups that match Normal return their fg & bg cterm values
      api.nvim_set_hl(0, 'Normal', { ctermfg = 17, ctermbg = 213 })
      api.nvim_set_hl(0, 'NotNormal', { ctermfg = 17, ctermbg = 213, nocombine = true })
      -- Note colors are "cterm" values, not rgb-as-ints
      eq({ foreground = 17, background = 213 }, api.nvim_get_hl_by_name('Normal', false))
      eq(
        { foreground = 17, background = 213, nocombine = true },
        api.nvim_get_hl_by_name('NotNormal', false)
      )
    end)

    it('nvim_get_hl_id_by_name', function()
      -- precondition: use a hl group that does not yet exist
      eq(
        "Invalid highlight name: 'Shrubbery'",
        pcall_err(api.nvim_get_hl_by_name, 'Shrubbery', true)
      )
      eq(0, fn.hlID('Shrubbery'))

      local hl_id = api.nvim_get_hl_id_by_name('Shrubbery')
      ok(hl_id > 0)
      eq(hl_id, fn.hlID('Shrubbery'))

      command('hi Shrubbery guifg=#888888 guibg=#888888')
      eq(
        { foreground = tonumber('0x888888'), background = tonumber('0x888888') },
        api.nvim_get_hl_by_id(hl_id, true)
      )
      eq(
        { foreground = tonumber('0x888888'), background = tonumber('0x888888') },
        api.nvim_get_hl_by_name('Shrubbery', true)
      )
    end)

    it("nvim_buf_add_highlight to other buffer doesn't crash if undo is disabled #12873", function()
      command('vsplit file')
      local err, _ = pcall(api.nvim_set_option_value, 'undofile', false, { buf = 1 })
      eq(true, err)
      err, _ = pcall(api.nvim_set_option_value, 'undolevels', -1, { buf = 1 })
      eq(true, err)
      err, _ = pcall(api.nvim_buf_add_highlight, 1, -1, 'Question', 0, 0, -1)
      eq(true, err)
      assert_alive()
    end)
  end)
end)