python3_spec.lua 3.8 KB

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