blob: 5b1085228831d436041529af5dd9f395733f3e69 [file] [log] [blame]
Bram Moolenaara58883b2017-01-29 21:31:09 +01001" Test for python 2 commands.
2" TODO: move tests from test87.in here.
3
4if !has('python')
Bram Moolenaarb0f94c12019-06-13 22:19:53 +02005 throw 'Skipped, python feature missing'
Bram Moolenaara58883b2017-01-29 21:31:09 +01006endif
7
8func Test_pydo()
9 " Check deleting lines does not trigger ml_get error.
10 py import vim
11 new
12 call setline(1, ['one', 'two', 'three'])
13 pydo vim.command("%d_")
14 bwipe!
15
16 " Check switching to another buffer does not trigger ml_get error.
17 new
18 let wincount = winnr('$')
19 call setline(1, ['one', 'two', 'three'])
20 pydo vim.command("new")
21 call assert_equal(wincount + 1, winnr('$'))
22 bwipe!
23 bwipe!
24endfunc
Bram Moolenaar53901442018-07-25 22:02:36 +020025
26func Test_set_cursor()
27 " Check that setting the cursor position works.
28 py import vim
29 new
30 call setline(1, ['first line', 'second line'])
31 normal gg
32 pydo 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('.')])
38endfunc
Bram Moolenaar9123c0b2018-12-22 18:59:06 +010039
40func Test_vim_function()
41 " Check creating vim.Function object
42 py 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 py f = vim.bindeval('function("s:foo")')
51 call assert_equal(name, pyeval('f.name'))
52 catch
53 call assert_false(v:exception)
54 endtry
55
56 try
57 py f = vim.Function('\x80\xfdR' + vim.eval('s:foo()'))
58 call assert_equal(name, pyeval('f.name'))
59 catch
60 call assert_false(v:exception)
61 endtry
62
63 py del f
64 delfunc s:foo
65endfunc
Bram Moolenaar14816ad2019-02-18 22:04:56 +010066
67func Test_skipped_python_command_does_not_affect_pyxversion()
68 set pyxversion=0
69 if 0
70 python 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.)
73endfunc
Bram Moolenaar63dbfd32019-03-23 17:41:59 +010074
75func _SetUpHiddenBuffer()
76 py 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 )
90endfunc
91
Bram Moolenaarbfd36032019-03-30 12:33:13 +010092func _CleanUpHiddenBuffer()
93 bwipe! hidden
94 bwipe!
95endfunc
96
Bram Moolenaar63dbfd32019-03-23 17:41:59 +010097func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_Clear()
98 call _SetUpHiddenBuffer()
99 py vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][:] = None
100 call assert_equal( line( '.' ), 11 )
Bram Moolenaarbfd36032019-03-30 12:33:13 +0100101 call _CleanUpHiddenBuffer()
Bram Moolenaar63dbfd32019-03-23 17:41:59 +0100102endfunc
103
104func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_List()
105 call _SetUpHiddenBuffer()
106 py vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][:] = [ 'test' ]
107 call assert_equal( line( '.' ), 11 )
Bram Moolenaarbfd36032019-03-30 12:33:13 +0100108 call _CleanUpHiddenBuffer()
Bram Moolenaar63dbfd32019-03-23 17:41:59 +0100109endfunc
110
111func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_Str()
112 call _SetUpHiddenBuffer()
113 py vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][0] = 'test'
114 call assert_equal( line( '.' ), 11 )
Bram Moolenaarbfd36032019-03-30 12:33:13 +0100115 call _CleanUpHiddenBuffer()
Bram Moolenaar63dbfd32019-03-23 17:41:59 +0100116endfunc
117
118func Test_Write_To_HiddenBuffer_Does_Not_Fix_Cursor_ClearLine()
119 call _SetUpHiddenBuffer()
120 py vim.buffers[ int( vim.eval( 'bufnr("hidden")' ) ) ][0] = None
121 call assert_equal( line( '.' ), 11 )
Bram Moolenaarbfd36032019-03-30 12:33:13 +0100122 call _CleanUpHiddenBuffer()
Bram Moolenaar63dbfd32019-03-23 17:41:59 +0100123endfunc
124
125func _SetUpVisibleBuffer()
126 py import vim
127 new
128 let lnum = 0
129 while lnum < 10
130 call append( 1, string( lnum ) )
131 let lnum = lnum + 1
132 endwhile
133 normal G
134 call assert_equal( line( '.' ), 11 )
135endfunc
136
137func Test_Write_To_Current_Buffer_Fixes_Cursor_Clear()
138 call _SetUpVisibleBuffer()
139
140 py vim.current.buffer[:] = None
141 call assert_equal( line( '.' ), 1 )
142
143 bwipe!
144endfunc
145
146func Test_Write_To_Current_Buffer_Fixes_Cursor_List()
147 call _SetUpVisibleBuffer()
148
149 py vim.current.buffer[:] = [ 'test' ]
150 call assert_equal( line( '.' ), 1 )
151
152 bwipe!
153endfunction
154
155func Test_Write_To_Current_Buffer_Fixes_Cursor_Str()
156 call _SetUpVisibleBuffer()
157
158 py vim.current.buffer[-1] = None
159 call assert_equal( line( '.' ), 10 )
160
161 bwipe!
162endfunction
Bram Moolenaar7f3a2842019-05-18 15:02:25 +0200163
164func Test_Catch_Exception_Message()
165 try
166 py raise RuntimeError( 'TEST' )
167 catch /.*/
168 call assert_match( '^Vim(.*):RuntimeError: TEST$', v:exception )
169 endtry
170endfunc