robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
5.7 KiB186 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, pending = t.describe, t.it, t.before_each, t.pending
5local assert_alive = n.assert_alive
6local eval, command, feed = n.eval, n.command, n.feed
7local eq, clear, insert = t.eq, n.clear, n.insert
8local expect, write_file = n.expect, t.write_file
9local feed_command = n.feed_command
10local source = n.source
11local missing_provider = n.missing_provider
12local matches = t.matches
13local pcall_err = t.pcall_err
14local fn = n.fn
15local dedent = t.dedent
16
17do
18  clear()
19  local reason = missing_provider('python')
20  if reason then
21    it(':python3 reports E319 if provider is missing', function()
22      local expected = [[Vim%(py3.*%):E319: No "python3" provider found.*]]
23      matches(expected, pcall_err(command, 'py3 print("foo")'))
24      matches(expected, pcall_err(command, 'py3file foo'))
25    end)
26    it('feature test when Python 3 provider is missing', function()
27      eq(0, eval('has("python3")'))
28      eq(0, eval('has("python3_compiled")'))
29      eq(0, eval('has("python3_dynamic")'))
30      eq(0, eval('has("pythonx")'))
31    end)
32    pending(
33      string.format('Python 3 (or the pynvim module) is broken/missing (%s)', reason),
34      function() end
35    )
36    return
37  end
38end
39
40describe('python3 provider', function()
41  before_each(function()
42    clear()
43    command('python3 import vim')
44  end)
45
46  it('feature test', function()
47    eq(1, eval('has("python3")'))
48    eq(1, eval('has("python3_compiled")'))
49    eq(1, eval('has("python3_dynamic")'))
50    eq(1, eval('has("pythonx")'))
51    eq(0, eval('has("python3_dynamic_")'))
52    eq(0, eval('has("python3_")'))
53  end)
54
55  it('python3_execute', function()
56    command('python3 vim.vars["set_by_python3"] = [100, 0]')
57    eq({ 100, 0 }, eval('g:set_by_python3'))
58  end)
59
60  it('does not truncate error message <1 MB', function()
61    -- XXX: Python limits the error name to 200 chars, so this test is
62    -- mostly bogus.
63    local very_long_symbol = string.rep('a', 1200)
64    feed_command(':silent! py3 print(' .. very_long_symbol .. ' b)')
65    -- Error message will contain this (last) line.
66    matches(
67      string.format(
68        dedent([[
69          ^Invoking 'python_execute' on channel 3 %%(python3%%-script%%-host%%):
70            File "<string>", line 1
71              print%%(%s b%%)
72          %%C*
73          SyntaxError: invalid syntax%%C*$]]),
74        very_long_symbol
75      ),
76      eval('v:errmsg')
77    )
78  end)
79
80  it('python3_execute with nested commands', function()
81    command(
82      [[python3 vim.command('python3 vim.command("python3 vim.command(\'let set_by_nested_python3 = 555\')")')]]
83    )
84    eq(555, eval('g:set_by_nested_python3'))
85  end)
86
87  it('python3_execute with range', function()
88    insert([[
89      line1
90      line2
91      line3
92      line4]])
93    feed('ggjvj:python3 vim.vars["range"] = vim.current.range[:]<CR>')
94    eq({ 'line2', 'line3' }, eval('g:range'))
95  end)
96
97  it('py3file', function()
98    local fname = 'py3file.py'
99    write_file(fname, 'vim.command("let set_by_py3file = 123")')
100    command('py3file py3file.py')
101    eq(123, eval('g:set_by_py3file'))
102    os.remove(fname)
103  end)
104
105  it('py3do', function()
106    -- :pydo3 42 returns None for all lines,
107    -- the buffer should not be changed
108    command('normal :py3do 42')
109    eq(0, eval('&mod'))
110    -- insert some text
111    insert('abc\ndef\nghi')
112    expect([[
113      abc
114      def
115      ghi]])
116    -- go to top and select and replace the first two lines
117    feed('ggvj:py3do return str(linenr)<CR>')
118    expect([[
119      1
120      2
121      ghi]])
122  end)
123
124  describe('py3eval()', function()
125    it('works', function()
126      eq({ 1, 2, { ['key'] = 'val' } }, fn.py3eval('[1, 2, {"key": "val"}]'))
127    end)
128
129    it('errors out when given non-string', function()
130      eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(10)'))
131      eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(v:_null_dict)'))
132      eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(v:_null_list)'))
133      eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(0.0)'))
134      eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(function("tr"))'))
135      eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(v:true)'))
136      eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(v:false)'))
137      eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(v:null)'))
138    end)
139
140    it('accepts NULL string', function()
141      matches('.*SyntaxError.*', pcall_err(eval, 'py3eval($XXX_NONEXISTENT_VAR_XXX)'))
142    end)
143  end)
144
145  it('pyxeval #10758', function()
146    eq(3, eval([[&pyxversion]]))
147    eq(3, eval([[pyxeval('sys.version_info[:3][0]')]]))
148    eq(3, eval([[&pyxversion]]))
149  end)
150
151  it("setting 'pyxversion'", function()
152    command 'set pyxversion=3' -- no error
153    eq('Vim(set):E474: Invalid argument: pyxversion=2', pcall_err(command, 'set pyxversion=2'))
154    command 'set pyxversion=0' -- allowed, but equivalent to pyxversion=3
155    eq(3, eval '&pyxversion')
156  end)
157
158  it('RPC call to expand("<afile>") during BufDelete #5245 #5617', function()
159    n.add_builddir_to_rtp()
160    source([=[
161      python3 << EOF
162      import vim
163      def foo():
164        vim.eval('expand("<afile>:p")')
165        vim.eval('bufnr(expand("<afile>:p"))')
166      EOF
167      autocmd BufDelete * python3 foo()
168      autocmd BufUnload * python3 foo()]=])
169    feed_command("exe 'split' tempname()")
170    feed_command('bwipeout!')
171    feed_command('help help')
172    assert_alive()
173  end)
174end)
175
176describe('python2 feature test', function()
177  -- python2 is not supported, so correct behaviour is to return 0
178  it('works', function()
179    eq(0, fn.has('python2'))
180    eq(0, fn.has('python'))
181    eq(0, fn.has('python_compiled'))
182    eq(0, fn.has('python_dynamic'))
183    eq(0, fn.has('python_dynamic_'))
184    eq(0, fn.has('python_'))
185  end)
186end)