test_python3.vim 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. " Test for python 3 commands.
  2. " TODO: move tests from test87.in here.
  3. source check.vim
  4. CheckFeature python3
  5. func Test_py3do()
  6. " Check deleting lines does not trigger an ml_get error.
  7. py3 import vim
  8. new
  9. call setline(1, ['one', 'two', 'three'])
  10. py3do vim.command("%d_")
  11. bwipe!
  12. " Disabled until neovim/neovim#8554 is resolved
  13. if 0
  14. " Check switching to another buffer does not trigger an ml_get error.
  15. new
  16. let wincount = winnr('$')
  17. call setline(1, ['one', 'two', 'three'])
  18. py3do vim.command("new")
  19. call assert_equal(wincount + 1, winnr('$'))
  20. bwipe!
  21. bwipe!
  22. endif
  23. endfunc
  24. func Test_set_cursor()
  25. " Check that setting the cursor position works.
  26. py3 import vim
  27. new
  28. call setline(1, ['first line', 'second line'])
  29. normal gg
  30. py3do vim.current.window.cursor = (1, 5)
  31. call assert_equal([1, 6], [line('.'), col('.')])
  32. " Check that movement after setting cursor position keeps current column.
  33. normal j
  34. call assert_equal([2, 6], [line('.'), col('.')])
  35. endfunc
  36. func Test_vim_function()
  37. throw 'Skipped: Nvim does not support vim.bindeval()'
  38. " Check creating vim.Function object
  39. py3 import vim
  40. func s:foo()
  41. return matchstr(expand('<sfile>'), '<SNR>\zs\d\+_foo$')
  42. endfunc
  43. let name = '<SNR>' . s:foo()
  44. try
  45. py3 f = vim.bindeval('function("s:foo")')
  46. call assert_equal(name, py3eval('f.name'))
  47. catch
  48. call assert_false(v:exception)
  49. endtry
  50. try
  51. py3 f = vim.Function(b'\x80\xfdR' + vim.eval('s:foo()').encode())
  52. call assert_equal(name, 'f.name'->py3eval())
  53. catch
  54. call assert_false(v:exception)
  55. endtry
  56. py3 del f
  57. delfunc s:foo
  58. endfunc
  59. func Test_skipped_python3_command_does_not_affect_pyxversion()
  60. throw 'Skipped: Nvim hardcodes pyxversion=3'
  61. set pyxversion=0
  62. if 0
  63. python3 import vim
  64. endif
  65. call assert_equal(0, &pyxversion) " This assertion would have failed with Vim 8.0.0251. (pyxversion was introduced in 8.0.0251.)
  66. endfunc
  67. func _SetUpHiddenBuffer()
  68. py3 import vim
  69. new
  70. edit hidden
  71. setlocal bufhidden=hide
  72. enew
  73. let lnum = 0
  74. while lnum < 10
  75. call append( 1, string( lnum ) )
  76. let lnum = lnum + 1
  77. endwhile
  78. normal G
  79. call assert_equal( line( '.' ), 11 )
  80. endfunc
  81. func _CleanUpHiddenBuffer()
  82. bwipe! hidden
  83. bwipe!
  84. endfunc
  85. func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_Clear()
  86. call _SetUpHiddenBuffer()
  87. py3 vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][:] = None
  88. call assert_equal( line( '.' ), 11 )
  89. call _CleanUpHiddenBuffer()
  90. endfunc
  91. func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_List()
  92. call _SetUpHiddenBuffer()
  93. py3 vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][:] = [ 'test' ]
  94. call assert_equal( line( '.' ), 11 )
  95. call _CleanUpHiddenBuffer()
  96. endfunc
  97. func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_Str()
  98. call _SetUpHiddenBuffer()
  99. py3 vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][0] = 'test'
  100. call assert_equal( line( '.' ), 11 )
  101. call _CleanUpHiddenBuffer()
  102. endfunc
  103. func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_ClearLine()
  104. call _SetUpHiddenBuffer()
  105. py3 vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][0] = None
  106. call assert_equal( line( '.' ), 11 )
  107. call _CleanUpHiddenBuffer()
  108. endfunc
  109. func _SetUpVisibleBuffer()
  110. py3 import vim
  111. new
  112. let lnum = 0
  113. while lnum < 10
  114. call append( 1, string( lnum ) )
  115. let lnum = lnum + 1
  116. endwhile
  117. normal G
  118. call assert_equal( line( '.' ), 11 )
  119. endfunc
  120. func Test_Write_To_Current_Buffer_Fixes_Cursor_Clear()
  121. call _SetUpVisibleBuffer()
  122. py3 vim.current.buffer[:] = None
  123. call assert_equal( line( '.' ), 1 )
  124. bwipe!
  125. endfunc
  126. func Test_Write_To_Current_Buffer_Fixes_Cursor_List()
  127. call _SetUpVisibleBuffer()
  128. py3 vim.current.buffer[:] = [ 'test' ]
  129. call assert_equal( line( '.' ), 1 )
  130. bwipe!
  131. endfunction
  132. func Test_Write_To_Current_Buffer_Fixes_Cursor_Str()
  133. call _SetUpVisibleBuffer()
  134. py3 vim.current.buffer[-1] = None
  135. call assert_equal( line( '.' ), 10 )
  136. bwipe!
  137. endfunction
  138. func Test_Catch_Exception_Message()
  139. try
  140. py3 raise RuntimeError( 'TEST' )
  141. catch /.*/
  142. call assert_match('^Vim(.*):.*RuntimeError: TEST.*$', v:exception )
  143. endtry
  144. endfunc
  145. func Test_unicode()
  146. " this crashed Vim once
  147. throw "Skipped: nvim does not support changing 'encoding'"
  148. set encoding=utf32
  149. py3 print('hello')
  150. if !has('win32')
  151. set encoding=debug
  152. py3 print('hello')
  153. set encoding=euc-tw
  154. py3 print('hello')
  155. endif
  156. set encoding=utf8
  157. endfunc
  158. " Test for resetting options with local values to global values
  159. func Test_python3_opt_reset_local_to_global()
  160. throw 'Skipped: Nvim does not support vim.bindeval()'
  161. new
  162. py3 curbuf = vim.current.buffer
  163. py3 curwin = vim.current.window
  164. " List of buffer-local options. Each list item has [option name, global
  165. " value, buffer-local value, buffer-local value after reset] to use in the
  166. " test.
  167. let bopts = [
  168. \ ['autoread', 1, 0, -1],
  169. \ ['equalprg', 'geprg', 'leprg', ''],
  170. \ ['keywordprg', 'gkprg', 'lkprg', ''],
  171. \ ['path', 'gpath', 'lpath', ''],
  172. \ ['backupcopy', 'yes', 'no', ''],
  173. \ ['tags', 'gtags', 'ltags', ''],
  174. \ ['tagcase', 'ignore', 'match', ''],
  175. \ ['define', 'gdef', 'ldef', ''],
  176. \ ['include', 'ginc', 'linc', ''],
  177. \ ['dict', 'gdict', 'ldict', ''],
  178. \ ['thesaurus', 'gtsr', 'ltsr', ''],
  179. \ ['formatprg', 'gfprg', 'lfprg', ''],
  180. \ ['errorformat', '%f:%l:%m', '%s-%l-%m', ''],
  181. \ ['grepprg', 'ggprg', 'lgprg', ''],
  182. \ ['makeprg', 'gmprg', 'lmprg', ''],
  183. \ ['balloonexpr', 'gbexpr', 'lbexpr', ''],
  184. \ ['cryptmethod', 'blowfish2', 'zip', ''],
  185. \ ['lispwords', 'abc', 'xyz', ''],
  186. \ ['makeencoding', 'utf-8', 'latin1', ''],
  187. \ ['undolevels', 100, 200, -123456]]
  188. " Set the global and buffer-local option values and then clear the
  189. " buffer-local option value.
  190. for opt in bopts
  191. py3 << trim END
  192. pyopt = vim.bindeval("opt")
  193. vim.options[pyopt[0]] = pyopt[1]
  194. curbuf.options[pyopt[0]] = pyopt[2]
  195. END
  196. exe "call assert_equal(opt[2], &" .. opt[0] .. ")"
  197. exe "call assert_equal(opt[1], &g:" .. opt[0] .. ")"
  198. exe "call assert_equal(opt[2], &l:" .. opt[0] .. ")"
  199. py3 del curbuf.options[pyopt[0]]
  200. exe "call assert_equal(opt[1], &" .. opt[0] .. ")"
  201. exe "call assert_equal(opt[1], &g:" .. opt[0] .. ")"
  202. exe "call assert_equal(opt[3], &l:" .. opt[0] .. ")"
  203. exe "set " .. opt[0] .. "&"
  204. endfor
  205. " Set the global and window-local option values and then clear the
  206. " window-local option value.
  207. let wopts = [
  208. \ ['scrolloff', 5, 10, -1],
  209. \ ['sidescrolloff', 6, 12, -1],
  210. \ ['statusline', '%<%f', '%<%F', '']]
  211. for opt in wopts
  212. py3 << trim
  213. pyopt = vim.bindeval("opt")
  214. vim.options[pyopt[0]] = pyopt[1]
  215. curwin.options[pyopt[0]] = pyopt[2]
  216. .
  217. exe "call assert_equal(opt[2], &" .. opt[0] .. ")"
  218. exe "call assert_equal(opt[1], &g:" .. opt[0] .. ")"
  219. exe "call assert_equal(opt[2], &l:" .. opt[0] .. ")"
  220. py3 del curwin.options[pyopt[0]]
  221. exe "call assert_equal(opt[1], &" .. opt[0] .. ")"
  222. exe "call assert_equal(opt[1], &g:" .. opt[0] .. ")"
  223. exe "call assert_equal(opt[3], &l:" .. opt[0] .. ")"
  224. exe "set " .. opt[0] .. "&"
  225. endfor
  226. close!
  227. endfunc
  228. " Test for various heredoc syntax
  229. func Test_python3_heredoc()
  230. python3 << END
  231. s='A'
  232. END
  233. python3 <<
  234. s+='B'
  235. .
  236. python3 << trim END
  237. s+='C'
  238. END
  239. python3 << trim
  240. s+='D'
  241. .
  242. python3 << trim eof
  243. s+='E'
  244. eof
  245. call assert_equal('ABCDE', pyxeval('s'))
  246. endfunc
  247. " vim: shiftwidth=2 sts=2 expandtab