python3_spec.lua 3.2 KB

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