blob: 2f5e0571aa953a2e4696a660048697884a18f7d9 [file] [log] [blame]
Bram Moolenaar53901442018-07-25 22:02:36 +02001" Test for python 3 commands.
Bram Moolenaar213ed002019-12-11 20:12:26 +01002" TODO: move tests from test87.in here.
Bram Moolenaara58883b2017-01-29 21:31:09 +01003
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02004source check.vim
5CheckFeature python3
Bram Moolenaara58883b2017-01-29 21:31:09 +01006
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
Bram Moolenaar53901442018-07-25 22:02:36 +020024
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
Bram Moolenaar9123c0b2018-12-22 18:59:06 +010038
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())
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020057 call assert_equal(name, 'f.name'->py3eval())
Bram Moolenaar9123c0b2018-12-22 18:59:06 +010058 catch
59 call assert_false(v:exception)
60 endtry
61
62 py3 del f
63 delfunc s:foo
64endfunc
Bram Moolenaar14816ad2019-02-18 22:04:56 +010065
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
Bram Moolenaar63dbfd32019-03-23 17:41:59 +010073
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
Bram Moolenaarbfd36032019-03-30 12:33:13 +010091func _CleanUpHiddenBuffer()
92 bwipe! hidden
93 bwipe!
94endfunc
95
Bram Moolenaar63dbfd32019-03-23 17:41:59 +010096func 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 )
Bram Moolenaarbfd36032019-03-30 12:33:13 +0100100 call _CleanUpHiddenBuffer()
Bram Moolenaar63dbfd32019-03-23 17:41:59 +0100101endfunc
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 )
Bram Moolenaarbfd36032019-03-30 12:33:13 +0100107 call _CleanUpHiddenBuffer()
Bram Moolenaar63dbfd32019-03-23 17:41:59 +0100108endfunc
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 )
Bram Moolenaarbfd36032019-03-30 12:33:13 +0100114 call _CleanUpHiddenBuffer()
Bram Moolenaar63dbfd32019-03-23 17:41:59 +0100115endfunc
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 )
Bram Moolenaarbfd36032019-03-30 12:33:13 +0100121 call _CleanUpHiddenBuffer()
Bram Moolenaar63dbfd32019-03-23 17:41:59 +0100122endfunc
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
Bram Moolenaar7f3a2842019-05-18 15:02:25 +0200162
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
Bram Moolenaar556684f2019-12-31 21:59:01 +0100170
171func Test_unicode()
172 " this crashed Vim once
Bram Moolenaar2466aea2020-01-01 17:09:11 +0100173 if &tenc != ''
174 throw "Skipped: 'termencoding' is not empty"
175 endif
Bram Moolenaar4b7cdca2020-01-01 16:18:38 +0100176
Bram Moolenaar556684f2019-12-31 21:59:01 +0100177 set encoding=utf32
178 py3 print('hello')
Bram Moolenaar4b7cdca2020-01-01 16:18:38 +0100179
Bram Moolenaar955f4e62020-01-01 17:44:56 +0100180 if !has('win32')
181 set encoding=debug
182 py3 print('hello')
Bram Moolenaar4b7cdca2020-01-01 16:18:38 +0100183
Bram Moolenaar7fc47852020-01-02 16:38:07 +0100184 set encoding=euc-tw
185 py3 print('hello')
186 endif
Bram Moolenaar4b7cdca2020-01-01 16:18:38 +0100187
Bram Moolenaar556684f2019-12-31 21:59:01 +0100188 set encoding=utf8
189endfunc
Bram Moolenaar904edab2020-01-19 13:57:54 +0100190
191" Test range objects, see :help python-range
Bram Moolenaar50985eb2020-01-27 22:09:39 +0100192func Test_python3_range()
Bram Moolenaar904edab2020-01-19 13:57:54 +0100193 new
194 py3 b = vim.current.buffer
195
196 call setline(1, range(1, 6))
197 py3 r = b.range(2, 4)
198 call assert_equal(6, py3eval('len(b)'))
199 call assert_equal(3, py3eval('len(r)'))
200 call assert_equal('3', py3eval('b[2]'))
201 call assert_equal('4', py3eval('r[2]'))
202
203 call assert_fails('py3 r[3] = "x"', 'IndexError: line number out of range')
204 call assert_fails('py3 x = r[3]', 'IndexError: line number out of range')
205 call assert_fails('py3 r["a"] = "x"', 'TypeError')
206 call assert_fails('py3 x = r["a"]', 'TypeError')
207
208 py3 del r[:]
209 call assert_equal(['1', '5', '6'], getline(1, '$'))
210
211 %d | call setline(1, range(1, 6))
212 py3 r = b.range(2, 5)
213 py3 del r[2]
214 call assert_equal(['1', '2', '3', '5', '6'], getline(1, '$'))
215
216 %d | call setline(1, range(1, 6))
217 py3 r = b.range(2, 4)
218 py3 vim.command("%d,%dnorm Ax" % (r.start + 1, r.end + 1))
219 call assert_equal(['1', '2x', '3x', '4x', '5', '6'], getline(1, '$'))
220
221 %d | call setline(1, range(1, 4))
222 py3 r = b.range(2, 3)
223 py3 r.append(['a', 'b'])
224 call assert_equal(['1', '2', '3', 'a', 'b', '4'], getline(1, '$'))
225 py3 r.append(['c', 'd'], 0)
226 call assert_equal(['1', 'c', 'd', '2', '3', 'a', 'b', '4'], getline(1, '$'))
227
228 %d | call setline(1, range(1, 5))
229 py3 r = b.range(2, 4)
230 py3 r.append('a')
231 call assert_equal(['1', '2', '3', '4', 'a', '5'], getline(1, '$'))
232 py3 r.append('b', 1)
233 call assert_equal(['1', '2', 'b', '3', '4', 'a', '5'], getline(1, '$'))
234
235 bwipe!
236endfunc