python3_spec.lua 5.6 KB

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