Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 1 | " Test for python 3 commands. |
Bram Moolenaar | a58883b | 2017-01-29 21:31:09 +0100 | [diff] [blame] | 2 | " TODO: move tests from test88.in here. |
| 3 | |
| 4 | if !has('python3') |
| 5 | finish |
| 6 | endif |
| 7 | |
| 8 | func Test_py3do() |
| 9 | " Check deleting lines does not trigger an ml_get error. |
| 10 | py3 import vim |
| 11 | new |
| 12 | call setline(1, ['one', 'two', 'three']) |
| 13 | py3do vim.command("%d_") |
| 14 | bwipe! |
| 15 | |
| 16 | " Check switching to another buffer does not trigger an ml_get error. |
| 17 | new |
| 18 | let wincount = winnr('$') |
| 19 | call setline(1, ['one', 'two', 'three']) |
| 20 | py3do vim.command("new") |
| 21 | call assert_equal(wincount + 1, winnr('$')) |
| 22 | bwipe! |
| 23 | bwipe! |
| 24 | endfunc |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 25 | |
| 26 | func Test_set_cursor() |
| 27 | " Check that setting the cursor position works. |
| 28 | py3 import vim |
| 29 | new |
| 30 | call setline(1, ['first line', 'second line']) |
| 31 | normal gg |
| 32 | py3do vim.current.window.cursor = (1, 5) |
| 33 | call assert_equal([1, 6], [line('.'), col('.')]) |
| 34 | |
| 35 | " Check that movement after setting cursor position keeps current column. |
| 36 | normal j |
| 37 | call assert_equal([2, 6], [line('.'), col('.')]) |
| 38 | endfunc |
Bram Moolenaar | 9123c0b | 2018-12-22 18:59:06 +0100 | [diff] [blame] | 39 | |
| 40 | func Test_vim_function() |
| 41 | " Check creating vim.Function object |
| 42 | py3 import vim |
| 43 | |
| 44 | func s:foo() |
| 45 | return matchstr(expand('<sfile>'), '<SNR>\zs\d\+_foo$') |
| 46 | endfunc |
| 47 | let name = '<SNR>' . s:foo() |
| 48 | |
| 49 | try |
| 50 | py3 f = vim.bindeval('function("s:foo")') |
| 51 | call assert_equal(name, py3eval('f.name')) |
| 52 | catch |
| 53 | call assert_false(v:exception) |
| 54 | endtry |
| 55 | |
| 56 | try |
| 57 | py3 f = vim.Function(b'\x80\xfdR' + vim.eval('s:foo()').encode()) |
| 58 | call assert_equal(name, py3eval('f.name')) |
| 59 | catch |
| 60 | call assert_false(v:exception) |
| 61 | endtry |
| 62 | |
| 63 | py3 del f |
| 64 | delfunc s:foo |
| 65 | endfunc |
Bram Moolenaar | 14816ad | 2019-02-18 22:04:56 +0100 | [diff] [blame] | 66 | |
| 67 | func Test_skipped_python3_command_does_not_affect_pyxversion() |
| 68 | set pyxversion=0 |
| 69 | if 0 |
| 70 | python3 import vim |
| 71 | endif |
| 72 | call assert_equal(0, &pyxversion) " This assertion would have failed with Vim 8.0.0251. (pyxversion was introduced in 8.0.0251.) |
| 73 | endfunc |
Bram Moolenaar | 63dbfd3 | 2019-03-23 17:41:59 +0100 | [diff] [blame] | 74 | |
| 75 | func _SetUpHiddenBuffer() |
| 76 | py3 import vim |
| 77 | new |
| 78 | edit hidden |
| 79 | setlocal bufhidden=hide |
| 80 | |
| 81 | enew |
| 82 | let lnum = 0 |
| 83 | while lnum < 10 |
| 84 | call append( 1, string( lnum ) ) |
| 85 | let lnum = lnum + 1 |
| 86 | endwhile |
| 87 | normal G |
| 88 | |
| 89 | call assert_equal( line( '.' ), 11 ) |
| 90 | endfunc |
| 91 | |
| 92 | func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_Clear() |
| 93 | call _SetUpHiddenBuffer() |
| 94 | py3 vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][:] = None |
| 95 | call assert_equal( line( '.' ), 11 ) |
| 96 | bwipe! |
| 97 | endfunc |
| 98 | |
| 99 | func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_List() |
| 100 | call _SetUpHiddenBuffer() |
| 101 | py3 vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][:] = [ 'test' ] |
| 102 | call assert_equal( line( '.' ), 11 ) |
| 103 | bwipe! |
| 104 | endfunc |
| 105 | |
| 106 | func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_Str() |
| 107 | call _SetUpHiddenBuffer() |
| 108 | py3 vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][0] = 'test' |
| 109 | call assert_equal( line( '.' ), 11 ) |
| 110 | bwipe! |
| 111 | endfunc |
| 112 | |
| 113 | func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_ClearLine() |
| 114 | call _SetUpHiddenBuffer() |
| 115 | py3 vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][0] = None |
| 116 | call assert_equal( line( '.' ), 11 ) |
| 117 | bwipe! |
| 118 | endfunc |
| 119 | |
| 120 | func _SetUpVisibleBuffer() |
| 121 | py3 import vim |
| 122 | new |
| 123 | let lnum = 0 |
| 124 | while lnum < 10 |
| 125 | call append( 1, string( lnum ) ) |
| 126 | let lnum = lnum + 1 |
| 127 | endwhile |
| 128 | normal G |
| 129 | call assert_equal( line( '.' ), 11 ) |
| 130 | endfunc |
| 131 | |
| 132 | func Test_Write_To_Current_Buffer_Fixes_Cursor_Clear() |
| 133 | call _SetUpVisibleBuffer() |
| 134 | |
| 135 | py3 vim.current.buffer[:] = None |
| 136 | call assert_equal( line( '.' ), 1 ) |
| 137 | |
| 138 | bwipe! |
| 139 | endfunc |
| 140 | |
| 141 | func Test_Write_To_Current_Buffer_Fixes_Cursor_List() |
| 142 | call _SetUpVisibleBuffer() |
| 143 | |
| 144 | py3 vim.current.buffer[:] = [ 'test' ] |
| 145 | call assert_equal( line( '.' ), 1 ) |
| 146 | |
| 147 | bwipe! |
| 148 | endfunction |
| 149 | |
| 150 | func Test_Write_To_Current_Buffer_Fixes_Cursor_Str() |
| 151 | call _SetUpVisibleBuffer() |
| 152 | |
| 153 | py3 vim.current.buffer[-1] = None |
| 154 | call assert_equal( line( '.' ), 10 ) |
| 155 | |
| 156 | bwipe! |
| 157 | endfunction |