python3_spec.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local assert_alive = helpers.assert_alive
  3. local eval, command, feed = helpers.eval, helpers.command, helpers.feed
  4. local eq, clear, insert = helpers.eq, helpers.clear, helpers.insert
  5. local expect, write_file = helpers.expect, helpers.write_file
  6. local feed_command = helpers.feed_command
  7. local source = helpers.source
  8. local missing_provider = helpers.missing_provider
  9. local matches = helpers.matches
  10. local pcall_err = helpers.pcall_err
  11. do
  12. clear()
  13. local reason = missing_provider('python3')
  14. if reason then
  15. it(':python3 reports E319 if provider is missing', function()
  16. local expected = [[Vim%(py3.*%):E319: No "python3" provider found.*]]
  17. matches(expected, pcall_err(command, 'py3 print("foo")'))
  18. matches(expected, pcall_err(command, 'py3file foo'))
  19. end)
  20. pending(string.format('Python 3 (or the pynvim module) is broken/missing (%s)', reason), function() end)
  21. return
  22. end
  23. end
  24. describe('python3 provider', function()
  25. before_each(function()
  26. clear()
  27. command('python3 import vim')
  28. end)
  29. it('feature test', function()
  30. eq(1, eval('has("python3")'))
  31. eq(1, eval('has("python3_compiled")'))
  32. eq(1, eval('has("python3_dynamic")'))
  33. eq(0, eval('has("python3_dynamic_")'))
  34. eq(0, eval('has("python3_")'))
  35. end)
  36. it('python3_execute', function()
  37. command('python3 vim.vars["set_by_python3"] = [100, 0]')
  38. eq({100, 0}, eval('g:set_by_python3'))
  39. end)
  40. it('does not truncate error message <1 MB', function()
  41. -- XXX: Python limits the error name to 200 chars, so this test is
  42. -- mostly bogus.
  43. local very_long_symbol = string.rep('a', 1200)
  44. feed_command(':silent! py3 print('..very_long_symbol..' b)')
  45. -- Error message will contain this (last) line.
  46. eq('Error invoking \'python_execute\' on channel 3 (python3-script-host):\n File "<string>", line 1\n print('..very_long_symbol..' b)\n '..string.rep(' ',1200)..' ^\nSyntaxError: invalid syntax', eval('v:errmsg'))
  47. end)
  48. it('python3_execute with nested commands', function()
  49. command([[python3 vim.command('python3 vim.command("python3 vim.command(\'let set_by_nested_python3 = 555\')")')]])
  50. eq(555, eval('g:set_by_nested_python3'))
  51. end)
  52. it('python3_execute with range', function()
  53. insert([[
  54. line1
  55. line2
  56. line3
  57. line4]])
  58. feed('ggjvj:python3 vim.vars["range"] = vim.current.range[:]<CR>')
  59. eq({'line2', 'line3'}, eval('g:range'))
  60. end)
  61. it('py3file', function()
  62. local fname = 'py3file.py'
  63. write_file(fname, 'vim.command("let set_by_py3file = 123")')
  64. command('py3file py3file.py')
  65. eq(123, eval('g:set_by_py3file'))
  66. os.remove(fname)
  67. end)
  68. it('py3do', function()
  69. -- :pydo3 42 returns None for all lines,
  70. -- the buffer should not be changed
  71. command('normal :py3do 42')
  72. eq(0, eval('&mod'))
  73. -- insert some text
  74. insert('abc\ndef\nghi')
  75. expect([[
  76. abc
  77. def
  78. ghi]])
  79. -- go to top and select and replace the first two lines
  80. feed('ggvj:py3do return str(linenr)<CR>')
  81. expect([[
  82. 1
  83. 2
  84. ghi]])
  85. end)
  86. it('py3eval', function()
  87. eq({1, 2, {['key'] = 'val'}}, eval([[py3eval('[1, 2, {"key": "val"}]')]]))
  88. end)
  89. it('pyxeval #10758', function()
  90. eq(0, eval([[&pyxversion]]))
  91. eq(3, eval([[pyxeval('sys.version_info[:3][0]')]]))
  92. eq(3, eval([[&pyxversion]]))
  93. end)
  94. it('RPC call to expand("<afile>") during BufDelete #5245 #5617', function()
  95. helpers.add_builddir_to_rtp()
  96. source([=[
  97. python3 << EOF
  98. import vim
  99. def foo():
  100. vim.eval('expand("<afile>:p")')
  101. vim.eval('bufnr(expand("<afile>:p"))')
  102. EOF
  103. autocmd BufDelete * python3 foo()
  104. autocmd BufUnload * python3 foo()]=])
  105. feed_command("exe 'split' tempname()")
  106. feed_command("bwipeout!")
  107. feed_command('help help')
  108. assert_alive()
  109. end)
  110. end)