robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
5.8 KiB165 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 eq = t.eq
6local clear = n.clear
7local command = n.command
8local eval = n.eval
9local api = n.api
10local exec = n.exec
11local exec_capture = n.exec_capture
12local expect_exit = n.expect_exit
13local source = n.source
14local testprg = n.testprg
15
16before_each(clear)
17
18describe(':let', function()
19  it('correctly lists variables with curly-braces', function()
20    api.nvim_set_var('v', { 0 })
21    eq('v                     [0]', exec_capture('let {"v"}'))
22  end)
23
24  it('correctly lists variables with subscript', function()
25    api.nvim_set_var('v', { 0 })
26    eq('v[0]                  #0', exec_capture('let v[0]'))
27    eq('g:["v"][0]            #0', exec_capture('let g:["v"][0]'))
28    eq('{"g:"}["v"][0]        #0', exec_capture('let {"g:"}["v"][0]'))
29  end)
30
31  it(':unlet self-referencing node in a List graph #6070', function()
32    -- :unlet-ing a self-referencing List must not allow GC on indirectly
33    -- referenced in-scope Lists. Before #6070 this caused use-after-free.
34    expect_exit(
35      1000,
36      source,
37      [=[
38      let [l1, l2] = [[], []]
39      echo 'l1:' . id(l1)
40      echo 'l2:' . id(l2)
41      echo ''
42      let [l3, l4] = [[], []]
43      call add(l4, l4)
44      call add(l4, l3)
45      call add(l3, 1)
46      call add(l2, l2)
47      call add(l2, l1)
48      call add(l1, 1)
49      unlet l2
50      unlet l4
51      call garbagecollect(1)
52      call feedkeys(":\e:echo l1 l3\n:echo 42\n:cq\n", "t")
53    ]=]
54    )
55  end)
56
57  it('multibyte env var #8398 #9267', function()
58    command("let $NVIM_TEST_LET = 'AìaB'")
59    eq('AìaB', eval('$NVIM_TEST_LET'))
60    command("let $NVIM_TEST_LET = 'AaあB'")
61    eq('AaあB', eval('$NVIM_TEST_LET'))
62    local mbyte = [[\p* .ม .ม .ม .ม่ .ม่ .ม่ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ ֹֻ
63                    .ֹֻ .ֹֻ .ֹֻ ֹֻ ֹֻ ֹֻ .ֹֻ .ֹֻ .ֹֻ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ
64                    .ֹֻ .ֹֻ .ֹֻ a a a ca ca ca à à à]]
65    command("let $NVIM_TEST_LET = '" .. mbyte .. "'")
66    eq(mbyte, eval('$NVIM_TEST_LET'))
67  end)
68
69  it('multibyte env var to child process #8398 #9267', function()
70    local cmd_get_child_env = ("let g:env_from_child = system(['%s', 'NVIM_TEST_LET'])"):format(
71      testprg('printenv-test')
72    )
73    command("let $NVIM_TEST_LET = 'AìaB'")
74    command(cmd_get_child_env)
75    eq(eval('$NVIM_TEST_LET'), eval('g:env_from_child'))
76
77    command("let $NVIM_TEST_LET = 'AaあB'")
78    command(cmd_get_child_env)
79    eq(eval('$NVIM_TEST_LET'), eval('g:env_from_child'))
80
81    local mbyte = [[\p* .ม .ม .ม .ม่ .ม่ .ม่ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ ֹֻ
82                    .ֹֻ .ֹֻ .ֹֻ ֹֻ ֹֻ ֹֻ .ֹֻ .ֹֻ .ֹֻ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ
83                    .ֹֻ .ֹֻ .ֹֻ a a a ca ca ca à à à]]
84    command("let $NVIM_TEST_LET = '" .. mbyte .. "'")
85    command(cmd_get_child_env)
86    eq(eval('$NVIM_TEST_LET'), eval('g:env_from_child'))
87  end)
88
89  it('release of list assigned to l: variable does not trigger assertion #12387, #12430', function()
90    source([[
91      func! s:f()
92        let l:x = [1]
93        let g:x = l:
94      endfunc
95      for _ in range(2)
96        call s:f()
97      endfor
98      call garbagecollect()
99      call feedkeys('i', 't')
100    ]])
101    eq(1, eval('1'))
102  end)
103
104  it('can apply operator to boolean option', function()
105    eq(true, api.nvim_get_option_value('equalalways', {}))
106    command('let &equalalways -= 1')
107    eq(false, api.nvim_get_option_value('equalalways', {}))
108    command('let &equalalways += 1')
109    eq(true, api.nvim_get_option_value('equalalways', {}))
110    command('let &equalalways *= 1')
111    eq(true, api.nvim_get_option_value('equalalways', {}))
112    command('let &equalalways /= 1')
113    eq(true, api.nvim_get_option_value('equalalways', {}))
114    command('let &equalalways %= 1')
115    eq(false, api.nvim_get_option_value('equalalways', {}))
116  end)
117
118  it('ignores legacy TTY assignments after validation', function()
119    for _, name in ipairs({ 't_xx', 't_Co', 'term', 'ttytype' }) do
120      local original = eval('&' .. name)
121      command('let &' .. name .. ' = "ignored"')
122      eq(original, eval('&' .. name))
123      command('let &' .. name .. ' .= "ignored"')
124      eq(original, eval('&' .. name))
125    end
126    eq('Vim(let):E730: Using a List as a String', t.pcall_err(command, 'let &t_xx = []'))
127    eq('Vim(let):E734: Wrong variable type for +=', t.pcall_err(command, 'let &t_xx += 1'))
128  end)
129
130  it('no crash: assigning bool/special to a string option gives E928', function()
131    for _, v in ipairs({ 'v:true', 'v:false', 'v:null' }) do
132      -- Regular string option.
133      eq('Vim(let):E928: String required', t.pcall_err(command, 'let &makeprg = ' .. v))
134      -- TTY option ("t_" pseudo-option).
135      eq('Vim(let):E928: String required', t.pcall_err(command, 'let &t_Co = ' .. v))
136    end
137  end)
138
139  it('no crash: assigning to local value of a global-local option', function()
140    -- Unset local reads as Unset, and -1 unsets it again, so curval/newval types may differ.
141    for _, o in ipairs({ 'autoread', 'autocomplete', 'fsync' }) do
142      command(('setglobal %s'):format(o)) -- global = on
143      command(('let &l:%s = v:false'):format(o)) -- unset local <- bool: sets local off
144      eq(false, api.nvim_get_option_value(o, {}))
145      command(('let &l:%s = -1'):format(o)) -- set local <- -1: unsets local, inheriting global
146      eq(true, api.nvim_get_option_value(o, {}))
147    end
148  end)
149end)
150
151describe(':let and :const', function()
152  it('have the same output when called without arguments', function()
153    eq(exec_capture('let'), exec_capture('const'))
154  end)
155
156  it('can be used in sandbox', function()
157    exec([[
158      func Func()
159        let l:foo = 'foo'
160        const l:bar = 'bar'
161      endfunc
162      sandbox call Func()
163    ]])
164  end)
165end)