1" Test for python 3 commands. 2" TODO: move tests from test87.in here. 3 4source check.vim 5CheckFeature python3 6 7func Test_py3do() 8 " Check deleting lines does not trigger an ml_get error. 9 py3 import vim 10 new 11 call setline(1, ['one', 'two', 'three']) 12 py3do vim.command("%d_") 13 bwipe! 14 15 " Check switching to another buffer does not trigger an ml_get error. 16 new 17 let wincount = winnr('$') 18 call setline(1, ['one', 'two', 'three']) 19 py3do vim.command("new") 20 call assert_equal(wincount + 1, winnr('$')) 21 bwipe! 22 bwipe! 23endfunc 24 25func Test_set_cursor() 26 " Check that setting the cursor position works. 27 py3 import vim 28 new 29 call setline(1, ['first line', 'second line']) 30 normal gg 31 py3do vim.current.window.cursor = (1, 5) 32 call assert_equal([1, 6], [line('.'), col('.')]) 33 34 " Check that movement after setting cursor position keeps current column. 35 normal j 36 call assert_equal([2, 6], [line('.'), col('.')]) 37endfunc 38 39func Test_vim_function() 40 " Check creating vim.Function object 41 py3 import vim 42 43 func s:foo() 44 return matchstr(expand('<sfile>'), '<SNR>\zs\d\+_foo$') 45 endfunc 46 let name = '<SNR>' . s:foo() 47 48 try 49 py3 f = vim.bindeval('function("s:foo")') 50 call assert_equal(name, py3eval('f.name')) 51 catch 52 call assert_false(v:exception) 53 endtry 54 55 try 56 py3 f = vim.Function(b'\x80\xfdR' + vim.eval('s:foo()').encode()) 57 call assert_equal(name, 'f.name'->py3eval()) 58 catch 59 call assert_false(v:exception) 60 endtry 61 62 py3 del f 63 delfunc s:foo 64endfunc 65 66func Test_skipped_python3_command_does_not_affect_pyxversion() 67 set pyxversion=0 68 if 0 69 python3 import vim 70 endif 71 call assert_equal(0, &pyxversion) " This assertion would have failed with Vim 8.0.0251. (pyxversion was introduced in 8.0.0251.) 72endfunc 73 74func _SetUpHiddenBuffer() 75 py3 import vim 76 new 77 edit hidden 78 setlocal bufhidden=hide 79 80 enew 81 let lnum = 0 82 while lnum < 10 83 call append( 1, string( lnum ) ) 84 let lnum = lnum + 1 85 endwhile 86 normal G 87 88 call assert_equal( line( '.' ), 11 ) 89endfunc 90 91func _CleanUpHiddenBuffer() 92 bwipe! hidden 93 bwipe! 94endfunc 95 96func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_Clear() 97 call _SetUpHiddenBuffer() 98 py3 vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][:] = None 99 call assert_equal( line( '.' ), 11 ) 100 call _CleanUpHiddenBuffer() 101endfunc 102 103func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_List() 104 call _SetUpHiddenBuffer() 105 py3 vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][:] = [ 'test' ] 106 call assert_equal( line( '.' ), 11 ) 107 call _CleanUpHiddenBuffer() 108endfunc 109 110func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_Str() 111 call _SetUpHiddenBuffer() 112 py3 vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][0] = 'test' 113 call assert_equal( line( '.' ), 11 ) 114 call _CleanUpHiddenBuffer() 115endfunc 116 117func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_ClearLine() 118 call _SetUpHiddenBuffer() 119 py3 vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][0] = None 120 call assert_equal( line( '.' ), 11 ) 121 call _CleanUpHiddenBuffer() 122endfunc 123 124func _SetUpVisibleBuffer() 125 py3 import vim 126 new 127 let lnum = 0 128 while lnum < 10 129 call append( 1, string( lnum ) ) 130 let lnum = lnum + 1 131 endwhile 132 normal G 133 call assert_equal( line( '.' ), 11 ) 134endfunc 135 136func Test_Write_To_Current_Buffer_Fixes_Cursor_Clear() 137 call _SetUpVisibleBuffer() 138 139 py3 vim.current.buffer[:] = None 140 call assert_equal( line( '.' ), 1 ) 141 142 bwipe! 143endfunc 144 145func Test_Write_To_Current_Buffer_Fixes_Cursor_List() 146 call _SetUpVisibleBuffer() 147 148 py3 vim.current.buffer[:] = [ 'test' ] 149 call assert_equal( line( '.' ), 1 ) 150 151 bwipe! 152endfunction 153 154func Test_Write_To_Current_Buffer_Fixes_Cursor_Str() 155 call _SetUpVisibleBuffer() 156 157 py3 vim.current.buffer[-1] = None 158 call assert_equal( line( '.' ), 10 ) 159 160 bwipe! 161endfunction 162 163func Test_Catch_Exception_Message() 164 try 165 py3 raise RuntimeError( 'TEST' ) 166 catch /.*/ 167 call assert_match( '^Vim(.*):RuntimeError: TEST$', v:exception ) 168 endtry 169endfunc 170 171func Test_unicode() 172 " this crashed Vim once 173 if &tenc != '' 174 throw "Skipped: 'termencoding' is not empty" 175 endif 176 177 set encoding=utf32 178 py3 print('hello') 179 180 if !has('win32') 181 set encoding=debug 182 py3 print('hello') 183 184 set encoding=euc-tw 185 py3 print('hello') 186 endif 187 188 set encoding=utf8 189endfunc 190 191" Test vim.eval() with various types. 192func Test_python3_vim_val() 193 call assert_equal("\n8", execute('py3 print(vim.eval("3+5"))')) 194 if has('float') 195 call assert_equal("\n3.140000", execute('py3 print(vim.eval("1.01+2.13"))')) 196 call assert_equal("\n0.000000", execute('py3 print(vim.eval("0.0/(1.0/0.0)"))')) 197 call assert_equal("\n0.000000", execute('py3 print(vim.eval("0.0/(1.0/0.0)"))')) 198 call assert_equal("\n-0.000000", execute('py3 print(vim.eval("0.0/(-1.0/0.0)"))')) 199 " Commented out: output of infinity and nan depend on platforms. 200 " call assert_equal("\ninf", execute('py3 print(vim.eval("1.0/0.0"))')) 201 " call assert_equal("\n-inf", execute('py3 print(vim.eval("-1.0/0.0"))')) 202 " call assert_equal("\n-nan", execute('py3 print(vim.eval("0.0/0.0"))')) 203 endif 204 call assert_equal("\nabc", execute('py3 print(vim.eval("\"abc\""))')) 205 call assert_equal("\n['1', '2']", execute('py3 print(vim.eval("[1, 2]"))')) 206 call assert_equal("\n{'1': '2'}", execute('py3 print(vim.eval("{1:2}"))')) 207 call assert_equal("\nTrue", execute('py3 print(vim.eval("v:true"))')) 208 call assert_equal("\nFalse", execute('py3 print(vim.eval("v:false"))')) 209 call assert_equal("\nNone", execute('py3 print(vim.eval("v:null"))')) 210 call assert_equal("\nNone", execute('py3 print(vim.eval("v:none"))')) 211 call assert_equal("\nb'\\xab\\x12'", execute('py3 print(vim.eval("0zab12"))')) 212 213 call assert_fails('py3 vim.eval("1+")', 'vim.error: invalid expression') 214endfunc 215 216" Test range objects, see :help python-range 217func Test_python3_range() 218 new 219 py3 b = vim.current.buffer 220 221 call setline(1, range(1, 6)) 222 py3 r = b.range(2, 4) 223 call assert_equal(6, py3eval('len(b)')) 224 call assert_equal(3, py3eval('len(r)')) 225 call assert_equal('3', py3eval('b[2]')) 226 call assert_equal('4', py3eval('r[2]')) 227 228 call assert_fails('py3 r[3] = "x"', 'IndexError: line number out of range') 229 call assert_fails('py3 x = r[3]', 'IndexError: line number out of range') 230 call assert_fails('py3 r["a"] = "x"', 'TypeError') 231 call assert_fails('py3 x = r["a"]', 'TypeError') 232 233 py3 del r[:] 234 call assert_equal(['1', '5', '6'], getline(1, '$')) 235 236 %d | call setline(1, range(1, 6)) 237 py3 r = b.range(2, 5) 238 py3 del r[2] 239 call assert_equal(['1', '2', '3', '5', '6'], getline(1, '$')) 240 241 %d | call setline(1, range(1, 6)) 242 py3 r = b.range(2, 4) 243 py3 vim.command("%d,%dnorm Ax" % (r.start + 1, r.end + 1)) 244 call assert_equal(['1', '2x', '3x', '4x', '5', '6'], getline(1, '$')) 245 246 %d | call setline(1, range(1, 4)) 247 py3 r = b.range(2, 3) 248 py3 r.append(['a', 'b']) 249 call assert_equal(['1', '2', '3', 'a', 'b', '4'], getline(1, '$')) 250 py3 r.append(['c', 'd'], 0) 251 call assert_equal(['1', 'c', 'd', '2', '3', 'a', 'b', '4'], getline(1, '$')) 252 253 %d | call setline(1, range(1, 5)) 254 py3 r = b.range(2, 4) 255 py3 r.append('a') 256 call assert_equal(['1', '2', '3', '4', 'a', '5'], getline(1, '$')) 257 py3 r.append('b', 1) 258 call assert_equal(['1', '2', 'b', '3', '4', 'a', '5'], getline(1, '$')) 259 260 bwipe! 261endfunc 262 263" Test for resetting options with local values to global values 264func Test_python3_opt_reset_local_to_global() 265 new 266 267 py3 curbuf = vim.current.buffer 268 py3 curwin = vim.current.window 269 270 " List of buffer-local options. Each list item has [option name, global 271 " value, buffer-local value, buffer-local value after reset] to use in the 272 " test. 273 let bopts = [ 274 \ ['autoread', 1, 0, -1], 275 \ ['equalprg', 'geprg', 'leprg', ''], 276 \ ['keywordprg', 'gkprg', 'lkprg', ''], 277 \ ['path', 'gpath', 'lpath', ''], 278 \ ['backupcopy', 'yes', 'no', ''], 279 \ ['tags', 'gtags', 'ltags', ''], 280 \ ['tagcase', 'ignore', 'match', ''], 281 \ ['define', 'gdef', 'ldef', ''], 282 \ ['include', 'ginc', 'linc', ''], 283 \ ['dict', 'gdict', 'ldict', ''], 284 \ ['thesaurus', 'gtsr', 'ltsr', ''], 285 \ ['formatprg', 'gfprg', 'lfprg', ''], 286 \ ['errorformat', '%f:%l:%m', '%s-%l-%m', ''], 287 \ ['grepprg', 'ggprg', 'lgprg', ''], 288 \ ['makeprg', 'gmprg', 'lmprg', ''], 289 \ ['balloonexpr', 'gbexpr', 'lbexpr', ''], 290 \ ['cryptmethod', 'blowfish2', 'zip', ''], 291 \ ['lispwords', 'abc', 'xyz', ''], 292 \ ['makeencoding', 'utf-8', 'latin1', ''], 293 \ ['undolevels', 100, 200, -123456]] 294 295 " Set the global and buffer-local option values and then clear the 296 " buffer-local option value. 297 for opt in bopts 298 py3 << trim END 299 pyopt = vim.bindeval("opt") 300 vim.options[pyopt[0]] = pyopt[1] 301 curbuf.options[pyopt[0]] = pyopt[2] 302 END 303 exe "call assert_equal(opt[2], &" .. opt[0] .. ")" 304 exe "call assert_equal(opt[1], &g:" .. opt[0] .. ")" 305 exe "call assert_equal(opt[2], &l:" .. opt[0] .. ")" 306 py3 del curbuf.options[pyopt[0]] 307 exe "call assert_equal(opt[1], &" .. opt[0] .. ")" 308 exe "call assert_equal(opt[1], &g:" .. opt[0] .. ")" 309 exe "call assert_equal(opt[3], &l:" .. opt[0] .. ")" 310 exe "set " .. opt[0] .. "&" 311 endfor 312 313 " Set the global and window-local option values and then clear the 314 " window-local option value. 315 let wopts = [ 316 \ ['scrolloff', 5, 10, -1], 317 \ ['sidescrolloff', 6, 12, -1], 318 \ ['statusline', '%<%f', '%<%F', '']] 319 for opt in wopts 320 py3 << trim 321 pyopt = vim.bindeval("opt") 322 vim.options[pyopt[0]] = pyopt[1] 323 curwin.options[pyopt[0]] = pyopt[2] 324 . 325 exe "call assert_equal(opt[2], &" .. opt[0] .. ")" 326 exe "call assert_equal(opt[1], &g:" .. opt[0] .. ")" 327 exe "call assert_equal(opt[2], &l:" .. opt[0] .. ")" 328 py3 del curwin.options[pyopt[0]] 329 exe "call assert_equal(opt[1], &" .. opt[0] .. ")" 330 exe "call assert_equal(opt[1], &g:" .. opt[0] .. ")" 331 exe "call assert_equal(opt[3], &l:" .. opt[0] .. ")" 332 exe "set " .. opt[0] .. "&" 333 endfor 334 335 close! 336endfunc 337 338" Test for various heredoc syntax 339func Test_python3_heredoc() 340 python3 << END 341s='A' 342END 343 python3 << 344s+='B' 345. 346 python3 << trim END 347 s+='C' 348 END 349 python3 << trim 350 s+='D' 351 . 352 python3 << trim eof 353 s+='E' 354 eof 355 call assert_equal('ABCDE', pyxeval('s')) 356endfunc 357 358" vim: shiftwidth=2 sts=2 expandtab 359