Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1 | Tests for various python features. vim: set ft=vim : |
| 2 | |
| 3 | STARTTEST |
| 4 | :so small.vim |
| 5 | :if !has('python3') | e! test.ok | wq! test.out | endif |
Bram Moolenaar | c24c1ac | 2013-05-16 20:47:56 +0200 | [diff] [blame] | 6 | :lang C |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 7 | :py3 import vim |
| 8 | :fun Test() |
| 9 | :let l = [] |
| 10 | :py3 l=vim.bindeval('l') |
| 11 | :py3 f=vim.bindeval('function("strlen")') |
| 12 | :" Extending List directly with different types |
| 13 | :py3 l+=[1, "as'd", [1, 2, f, {'a': 1}]] |
| 14 | :$put =string(l) |
| 15 | :$put =string(l[-1]) |
| 16 | :try |
| 17 | : $put =string(l[-4]) |
| 18 | :catch |
| 19 | : $put =v:exception[:13] |
| 20 | :endtry |
| 21 | :" List assignment |
| 22 | :py3 l[0]=0 |
| 23 | :$put =string(l) |
| 24 | :py3 l[-2]=f |
| 25 | :$put =string(l) |
| 26 | :" |
| 27 | :" Extending Dictionary directly with different types |
| 28 | :let d = {} |
| 29 | :py3 d=vim.bindeval('d') |
| 30 | :py3 d['1']='asd' |
| 31 | :py3 d['b']=[1, 2, f] |
| 32 | :py3 d['-1']={'a': 1} |
| 33 | :let dkeys = [] |
| 34 | :py3 dk=vim.bindeval('dkeys') |
| 35 | :py3 dkeys=d.keys() |
| 36 | :py3 dkeys.sort() |
| 37 | :py3 dk+=dkeys |
| 38 | :$put =string(dkeys) |
| 39 | :for [key, val] in sort(items(d)) |
| 40 | : $put =string(key) . ' : ' . string(val) |
| 41 | : unlet key val |
| 42 | :endfor |
| 43 | :" |
| 44 | :" removing items with del |
| 45 | :py3 del l[2] |
| 46 | :$put =string(l) |
| 47 | :let l = range(8) |
| 48 | :py3 l=vim.bindeval('l') |
| 49 | :try |
| 50 | : py3 del l[:3] |
| 51 | : py3 del l[1:] |
| 52 | :catch |
| 53 | : $put =v:exception |
| 54 | :endtry |
| 55 | :$put =string(l) |
| 56 | :" |
| 57 | :py3 del d['-1'] |
| 58 | :$put =string(d) |
| 59 | :" |
| 60 | :" removing items out of range: silently skip items that don't exist |
| 61 | :let l = [0, 1, 2, 3] |
| 62 | :py3 l=vim.bindeval('l') |
| 63 | :" The following two ranges delete nothing as they match empty list: |
| 64 | :py3 del l[2:1] |
| 65 | :$put =string(l) |
| 66 | :py3 del l[2:2] |
| 67 | :$put =string(l) |
| 68 | :py3 del l[2:3] |
| 69 | :$put =string(l) |
| 70 | :let l = [0, 1, 2, 3] |
| 71 | :py3 l=vim.bindeval('l') |
| 72 | :py3 del l[2:4] |
| 73 | :$put =string(l) |
| 74 | :let l = [0, 1, 2, 3] |
| 75 | :py3 l=vim.bindeval('l') |
| 76 | :py3 del l[2:5] |
| 77 | :$put =string(l) |
| 78 | :let l = [0, 1, 2, 3] |
| 79 | :py3 l=vim.bindeval('l') |
| 80 | :py3 del l[2:6] |
| 81 | :$put =string(l) |
| 82 | :let l = [0, 1, 2, 3] |
| 83 | :py3 l=vim.bindeval('l') |
| 84 | :" The following two ranges delete nothing as they match empty list: |
| 85 | :py3 del l[-1:2] |
| 86 | :$put =string(l) |
| 87 | :py3 del l[-2:2] |
| 88 | :$put =string(l) |
| 89 | :py3 del l[-3:2] |
| 90 | :$put =string(l) |
| 91 | :let l = [0, 1, 2, 3] |
| 92 | :py3 l=vim.bindeval('l') |
| 93 | :py3 del l[-4:2] |
| 94 | :$put =string(l) |
| 95 | :let l = [0, 1, 2, 3] |
| 96 | :py3 l=vim.bindeval('l') |
| 97 | :py3 del l[-5:2] |
| 98 | :$put =string(l) |
| 99 | :let l = [0, 1, 2, 3] |
| 100 | :py3 l=vim.bindeval('l') |
| 101 | :py3 del l[-6:2] |
| 102 | :$put =string(l) |
| 103 | :" |
| 104 | :" Slice assignment to a list |
| 105 | :let l = [0, 1, 2, 3] |
| 106 | :py3 l=vim.bindeval('l') |
| 107 | :py3 l[0:0]=['a'] |
| 108 | :$put =string(l) |
| 109 | :let l = [0, 1, 2, 3] |
| 110 | :py3 l=vim.bindeval('l') |
| 111 | :py3 l[1:2]=['b'] |
| 112 | :$put =string(l) |
| 113 | :let l = [0, 1, 2, 3] |
| 114 | :py3 l=vim.bindeval('l') |
| 115 | :py3 l[2:4]=['c'] |
| 116 | :$put =string(l) |
| 117 | :let l = [0, 1, 2, 3] |
| 118 | :py3 l=vim.bindeval('l') |
| 119 | :py3 l[4:4]=['d'] |
| 120 | :$put =string(l) |
| 121 | :let l = [0, 1, 2, 3] |
| 122 | :py3 l=vim.bindeval('l') |
| 123 | :py3 l[-1:2]=['e'] |
| 124 | :$put =string(l) |
| 125 | :let l = [0, 1, 2, 3] |
| 126 | :py3 l=vim.bindeval('l') |
| 127 | :py3 l[-10:2]=['f'] |
| 128 | :$put =string(l) |
| 129 | :let l = [0, 1, 2, 3] |
| 130 | :py3 l=vim.bindeval('l') |
| 131 | :py3 l[2:-10]=['g'] |
| 132 | :$put =string(l) |
| 133 | :let l = [] |
| 134 | :py3 l=vim.bindeval('l') |
| 135 | :py3 l[0:0]=['h'] |
| 136 | :$put =string(l) |
| 137 | :" |
| 138 | :" Locked variables |
| 139 | :let l = [0, 1, 2, 3] |
| 140 | :py3 l=vim.bindeval('l') |
| 141 | :lockvar! l |
| 142 | :py3 l[2]='i' |
| 143 | :$put =string(l) |
| 144 | :unlockvar! l |
| 145 | :" |
| 146 | :" Function calls |
| 147 | :function New(...) |
| 148 | :return ['NewStart']+a:000+['NewEnd'] |
| 149 | :endfunction |
| 150 | :function DictNew(...) dict |
| 151 | :return ['DictNewStart']+a:000+['DictNewEnd', self] |
| 152 | :endfunction |
| 153 | :let l=[function('New'), function('DictNew')] |
| 154 | :py3 l=vim.bindeval('l') |
| 155 | :py3 l.extend(list(l[0](1, 2, 3))) |
| 156 | :$put =string(l) |
| 157 | :py3 l.extend(list(l[1](1, 2, 3, self={'a': 'b'}))) |
| 158 | :$put =string(l) |
| 159 | :py3 l+=[l[0].name] |
| 160 | :$put =string(l) |
| 161 | :try |
| 162 | : py3 l[1](1, 2, 3) |
| 163 | :catch |
| 164 | : $put =v:exception[:13] |
| 165 | :endtry |
| 166 | :delfunction New |
| 167 | :try |
| 168 | : py3 l[0](1, 2, 3) |
| 169 | :catch |
| 170 | : $put =v:exception[:13] |
| 171 | :endtry |
| 172 | :if has('float') |
| 173 | : let l=[0.0] |
| 174 | : py3 l=vim.bindeval('l') |
| 175 | : py3 l.extend([0.0]) |
| 176 | : $put =string(l) |
| 177 | :else |
| 178 | : $put ='[0.0, 0.0]' |
| 179 | :endif |
Bram Moolenaar | c11073c | 2012-09-05 19:17:42 +0200 | [diff] [blame] | 180 | :let messages=[] |
| 181 | :py3 <<EOF |
| 182 | d=vim.bindeval('{}') |
| 183 | m=vim.bindeval('messages') |
| 184 | try: |
| 185 | d['abc'] |
| 186 | except Exception as e: |
| 187 | m.extend([e.__class__.__name__]) |
| 188 | |
| 189 | try: |
| 190 | d['abc']="\0" |
| 191 | except Exception as e: |
| 192 | m.extend([e.__class__.__name__]) |
| 193 | |
| 194 | try: |
| 195 | d['abc']=vim |
| 196 | except Exception as e: |
| 197 | m.extend([e.__class__.__name__]) |
| 198 | |
| 199 | try: |
| 200 | d['']=1 |
| 201 | except Exception as e: |
| 202 | m.extend([e.__class__.__name__]) |
| 203 | |
| 204 | try: |
| 205 | d['a\0b']=1 |
| 206 | except Exception as e: |
| 207 | m.extend([e.__class__.__name__]) |
| 208 | |
| 209 | try: |
| 210 | d[b'a\0b']=1 |
| 211 | except Exception as e: |
| 212 | m.extend([e.__class__.__name__]) |
| 213 | EOF |
| 214 | :$put =messages |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 215 | :unlet messages |
| 216 | :" locked and scope attributes |
| 217 | :let d={} | let dl={} | lockvar dl |
| 218 | :for s in split("d dl v: g:") |
| 219 | : let name=tr(s, ':', 's') |
| 220 | : execute 'py3 '.name.'=vim.bindeval("'.s.'")' |
| 221 | : let toput=s.' : '.join(map(['locked', 'scope'], 'v:val.":".py3eval(name.".".v:val)'), ';') |
| 222 | : $put =toput |
| 223 | :endfor |
| 224 | :silent! let d.abc=1 |
| 225 | :silent! let dl.abc=1 |
| 226 | :py3 d.locked=True |
| 227 | :py3 dl.locked=False |
| 228 | :silent! let d.def=1 |
| 229 | :silent! let dl.def=1 |
| 230 | :put ='d:'.string(d) |
| 231 | :put ='dl:'.string(dl) |
| 232 | :unlet d dl |
| 233 | : |
| 234 | :let l=[] | let ll=[] | lockvar ll |
| 235 | :for s in split("l ll") |
| 236 | : let name=tr(s, ':', 's') |
| 237 | : execute 'py3 '.name.'=vim.bindeval("'.s.'")' |
| 238 | : let toput=s.' : locked:'.py3eval(name.'.locked') |
| 239 | : $put =toput |
| 240 | :endfor |
| 241 | :silent! call extend(l, [0]) |
| 242 | :silent! call extend(ll, [0]) |
| 243 | :py3 l.locked=True |
| 244 | :py3 ll.locked=False |
| 245 | :silent! call extend(l, [1]) |
| 246 | :silent! call extend(ll, [1]) |
| 247 | :put ='l:'.string(l) |
| 248 | :put ='ll:'.string(ll) |
| 249 | :unlet l ll |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 250 | :" |
| 251 | :" py3eval() |
| 252 | :let l=py3eval('[0, 1, 2]') |
| 253 | :$put =string(l) |
| 254 | :let d=py3eval('{"a": "b", "c": 1, "d": ["e"]}') |
| 255 | :$put =sort(items(d)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 256 | :if has('float') |
| 257 | : let f=py3eval('0.0') |
| 258 | : $put =string(f) |
| 259 | :else |
| 260 | : $put ='0.0' |
| 261 | :endif |
Bram Moolenaar | c11073c | 2012-09-05 19:17:42 +0200 | [diff] [blame] | 262 | :" Invalid values: |
| 263 | :for e in ['"\0"', '{"\0": 1}', 'undefined_name', 'vim'] |
| 264 | : try |
| 265 | : let v=py3eval(e) |
| 266 | : catch |
| 267 | : let toput=e.":\t".v:exception[:13] |
| 268 | : $put =toput |
| 269 | : endtry |
| 270 | :endfor |
Bram Moolenaar | 76d711c | 2013-02-13 14:17:08 +0100 | [diff] [blame] | 271 | :" |
| 272 | :" threading |
| 273 | :let l = [0] |
| 274 | :py3 l=vim.bindeval('l') |
| 275 | :py3 <<EOF |
| 276 | import threading |
| 277 | import time |
| 278 | |
| 279 | class T(threading.Thread): |
| 280 | def __init__(self): |
| 281 | threading.Thread.__init__(self) |
| 282 | self.t = 0 |
| 283 | self.running = True |
| 284 | |
| 285 | def run(self): |
| 286 | while self.running: |
| 287 | self.t += 1 |
| 288 | time.sleep(0.1) |
| 289 | |
| 290 | t = T() |
| 291 | t.start() |
| 292 | EOF |
| 293 | :sleep 1 |
| 294 | :py3 t.running = False |
| 295 | :py3 t.join() |
| 296 | :py3 l[0] = t.t > 8 # check if the background thread is working |
| 297 | :$put =string(l) |
| 298 | :" |
| 299 | :" settrace |
| 300 | :let l = [] |
| 301 | :py3 l=vim.bindeval('l') |
| 302 | :py3 <<EOF |
| 303 | import sys |
| 304 | |
| 305 | def traceit(frame, event, arg): |
| 306 | global l |
| 307 | if event == "line": |
| 308 | l += [frame.f_lineno] |
| 309 | return traceit |
| 310 | |
| 311 | def trace_main(): |
| 312 | for i in range(5): |
| 313 | pass |
| 314 | EOF |
| 315 | :py3 sys.settrace(traceit) |
| 316 | :py3 trace_main() |
| 317 | :py3 sys.settrace(None) |
| 318 | :$put =string(l) |
Bram Moolenaar | 230bb3f | 2013-04-24 14:07:45 +0200 | [diff] [blame] | 319 | :" |
| 320 | :" Vars |
| 321 | :let g:foo = 'bac' |
| 322 | :let w:abc = 'def' |
| 323 | :let b:baz = 'bar' |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 324 | :let t:bar = 'jkl' |
Bram Moolenaar | 230bb3f | 2013-04-24 14:07:45 +0200 | [diff] [blame] | 325 | :try |
| 326 | : throw "Abc" |
| 327 | :catch |
| 328 | : put =py3eval('vim.vvars[''exception'']') |
| 329 | :endtry |
| 330 | :put =py3eval('vim.vars[''foo'']') |
| 331 | :put =py3eval('vim.current.window.vars[''abc'']') |
| 332 | :put =py3eval('vim.current.buffer.vars[''baz'']') |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 333 | :put =py3eval('vim.current.tabpage.vars[''bar'']') |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 334 | :" |
| 335 | :" Options |
| 336 | :" paste: boolean, global |
| 337 | :" previewheight number, global |
| 338 | :" operatorfunc: string, global |
| 339 | :" number: boolean, window-local |
| 340 | :" numberwidth: number, window-local |
| 341 | :" colorcolumn: string, window-local |
| 342 | :" statusline: string, window-local/global |
| 343 | :" autoindent: boolean, buffer-local |
Bram Moolenaar | 55b8ad3 | 2013-05-17 13:38:04 +0200 | [diff] [blame] | 344 | :" shiftwidth: number, buffer-local |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 345 | :" omnifunc: string, buffer-local |
| 346 | :" preserveindent: boolean, buffer-local/global |
| 347 | :" path: string, buffer-local/global |
| 348 | :let g:bufs=[bufnr('%')] |
| 349 | :new |
| 350 | :let g:bufs+=[bufnr('%')] |
| 351 | :vnew |
| 352 | :let g:bufs+=[bufnr('%')] |
| 353 | :wincmd j |
| 354 | :vnew |
| 355 | :let g:bufs+=[bufnr('%')] |
| 356 | :wincmd l |
| 357 | :fun RecVars(opt) |
| 358 | : let gval =string(eval('&g:'.a:opt)) |
| 359 | : let wvals=join(map(range(1, 4), 'v:val.":".string(getwinvar(v:val, "&".a:opt))')) |
| 360 | : let bvals=join(map(copy(g:bufs), 'v:val.":".string(getbufvar(v:val, "&".a:opt))')) |
| 361 | : put =' G: '.gval |
| 362 | : put =' W: '.wvals |
| 363 | : put =' B: '.wvals |
| 364 | :endfun |
| 365 | py3 << EOF |
| 366 | def e(s, g=globals(), l=locals()): |
| 367 | try: |
| 368 | exec(s, g, l) |
| 369 | except Exception as e: |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 370 | vim.command('return ' + repr(e.__class__.__name__)) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 371 | |
| 372 | def ev(s, g=globals(), l=locals()): |
| 373 | try: |
| 374 | return eval(s, g, l) |
| 375 | except Exception as e: |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 376 | vim.command('let exc=' + repr(e.__class__.__name__)) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 377 | return 0 |
| 378 | EOF |
| 379 | :function E(s) |
| 380 | : python3 e(vim.eval('a:s')) |
| 381 | :endfunction |
| 382 | :function Ev(s) |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 383 | : let r=py3eval('ev(vim.eval("a:s"))') |
| 384 | : if exists('exc') |
| 385 | : throw exc |
| 386 | : endif |
| 387 | : return r |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 388 | :endfunction |
| 389 | :py3 gopts1=vim.options |
| 390 | :py3 wopts1=vim.windows[2].options |
| 391 | :py3 wopts2=vim.windows[0].options |
| 392 | :py3 wopts3=vim.windows[1].options |
| 393 | :py3 bopts1=vim.buffers[vim.bindeval("g:bufs")[2]].options |
| 394 | :py3 bopts2=vim.buffers[vim.bindeval("g:bufs")[1]].options |
| 395 | :py3 bopts3=vim.buffers[vim.bindeval("g:bufs")[0]].options |
| 396 | :let lst=[] |
| 397 | :let lst+=[['paste', 1, 0, 1, 2, 1, 1, 0 ]] |
| 398 | :let lst+=[['previewheight', 5, 1, 6, 'a', 0, 1, 0 ]] |
| 399 | :let lst+=[['operatorfunc', 'A', 'B', 'C', 2, 0, 1, 0 ]] |
| 400 | :let lst+=[['number', 0, 1, 1, 0, 1, 0, 1 ]] |
| 401 | :let lst+=[['numberwidth', 2, 3, 5, -100, 0, 0, 1 ]] |
| 402 | :let lst+=[['colorcolumn', '+1', '+2', '+3', 'abc', 0, 0, 1 ]] |
| 403 | :let lst+=[['statusline', '1', '2', '4', 0, 0, 1, 1 ]] |
| 404 | :let lst+=[['autoindent', 0, 1, 1, 2, 1, 0, 2 ]] |
Bram Moolenaar | 55b8ad3 | 2013-05-17 13:38:04 +0200 | [diff] [blame] | 405 | :let lst+=[['shiftwidth', 0, 2, 1, 3, 0, 0, 2 ]] |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 406 | :let lst+=[['omnifunc', 'A', 'B', 'C', 1, 0, 0, 2 ]] |
| 407 | :let lst+=[['preserveindent', 0, 1, 1, 2, 1, 1, 2 ]] |
| 408 | :let lst+=[['path', '.,,', ',,', '.', 0, 0, 1, 2 ]] |
| 409 | :for [oname, oval1, oval2, oval3, invval, bool, global, local] in lst |
| 410 | : py3 oname=vim.eval('oname') |
| 411 | : py3 oval1=vim.bindeval('oval1') |
| 412 | : py3 oval2=vim.bindeval('oval2') |
| 413 | : py3 oval3=vim.bindeval('oval3') |
| 414 | : if invval is 0 || invval is 1 |
| 415 | : py3 invval=bool(vim.bindeval('invval')) |
| 416 | : else |
| 417 | : py3 invval=vim.bindeval('invval') |
| 418 | : endif |
| 419 | : if bool |
| 420 | : py3 oval1=bool(oval1) |
| 421 | : py3 oval2=bool(oval2) |
| 422 | : py3 oval3=bool(oval3) |
| 423 | : endif |
| 424 | : put ='>>> '.oname |
| 425 | : for v in ['gopts1', 'wopts1', 'bopts1'] |
| 426 | : try |
| 427 | : put =' p/'.v.': '.Ev('repr('.v.'['''.oname.'''])') |
| 428 | : catch |
| 429 | : put =' p/'.v.'! '.v:exception |
| 430 | : endtry |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 431 | : let r=E(v.'['''.oname.''']=invval') |
| 432 | : if r isnot 0 |
| 433 | : put =' inv: '.string(invval).'! '.r |
| 434 | : endif |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 435 | : for vv in (v is# 'gopts1' ? [v] : [v, v[:-2].'2', v[:-2].'3']) |
| 436 | : let val=substitute(vv, '^.opts', 'oval', '') |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 437 | : let r=E(vv.'['''.oname.''']='.val) |
| 438 | : if r isnot 0 |
| 439 | : put =' '.vv.'! '.r |
| 440 | : endif |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 441 | : endfor |
| 442 | : endfor |
| 443 | : call RecVars(oname) |
| 444 | : for v in ['wopts3', 'bopts3'] |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 445 | : let r=E('del '.v.'["'.oname.'"]') |
| 446 | : if r isnot 0 |
| 447 | : put =' del '.v.'! '.r |
| 448 | : endif |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 449 | : endfor |
| 450 | : call RecVars(oname) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 451 | :endfor |
| 452 | :only |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 453 | :for buf in g:bufs[1:] |
| 454 | : execute 'bwipeout!' buf |
| 455 | :endfor |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 456 | :" |
| 457 | :" Test buffer object |
| 458 | :vnew |
| 459 | :put ='First line' |
| 460 | :put ='Second line' |
| 461 | :put ='Third line' |
| 462 | :1 delete _ |
| 463 | :py3 b=vim.current.buffer |
| 464 | :wincmd w |
| 465 | :mark a |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 466 | :augroup BUFS |
| 467 | : autocmd BufFilePost * python3 cb.append(vim.eval('expand("<abuf>")') + ':BufFilePost:' + vim.eval('bufnr("%")')) |
| 468 | : autocmd BufFilePre * python3 cb.append(vim.eval('expand("<abuf>")') + ':BufFilePre:' + vim.eval('bufnr("%")')) |
| 469 | :augroup END |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 470 | py3 << EOF |
| 471 | cb = vim.current.buffer |
| 472 | # Tests BufferAppend and BufferItem |
| 473 | cb.append(b[0]) |
| 474 | # Tests BufferSlice and BufferAssSlice |
| 475 | cb.append('abc') # Will be overwritten |
| 476 | cb[-1:] = b[:-2] |
| 477 | # Test BufferLength and BufferAssSlice |
| 478 | cb.append('def') # Will not be overwritten |
| 479 | cb[len(cb):] = b[:] |
| 480 | # Test BufferAssItem and BufferMark |
| 481 | cb.append('ghi') # Will be overwritten |
| 482 | cb[-1] = repr((len(cb) - cb.mark('a')[0], cb.mark('a')[1])) |
| 483 | # Test BufferRepr |
| 484 | cb.append(repr(cb) + repr(b)) |
| 485 | # Modify foreign buffer |
| 486 | b.append('foo') |
| 487 | b[0]='bar' |
| 488 | b[0:0]=['baz'] |
| 489 | vim.command('call append("$", getbufline(%i, 1, "$"))' % b.number) |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 490 | # Test assigning to name property |
| 491 | old_name = cb.name |
| 492 | cb.name = 'foo' |
| 493 | cb.append(cb.name[-11:]) |
| 494 | b.name = 'bar' |
| 495 | cb.append(b.name[-11:]) |
| 496 | cb.name = old_name |
| 497 | cb.append(cb.name[-17:]) |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 498 | # Test CheckBuffer |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 499 | for _b in vim.buffers: |
| 500 | if _b is not cb: |
| 501 | vim.command('bwipeout! ' + str(_b.number)) |
| 502 | del _b |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 503 | cb.append('valid: b:%s, cb:%s' % (repr(b.valid), repr(cb.valid))) |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 504 | for expr in ('b[1]','b[:] = ["A", "B"]','b[:]','b.append("abc")'): |
| 505 | try: |
| 506 | exec(expr) |
| 507 | except vim.error: |
| 508 | pass |
| 509 | else: |
| 510 | # Usually a SEGV here |
| 511 | # Should not happen in any case |
| 512 | cb.append('No exception for ' + expr) |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 513 | vim.command('cd .') |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 514 | EOF |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 515 | :" |
| 516 | :" Test vim.buffers object |
| 517 | :set hidden |
| 518 | :edit a |
| 519 | :buffer # |
| 520 | :edit b |
| 521 | :buffer # |
| 522 | :edit c |
| 523 | :buffer # |
| 524 | py3 << EOF |
| 525 | # Check GCing iterator that was not fully exhausted |
| 526 | i = iter(vim.buffers) |
| 527 | cb.append('i:' + str(next(i))) |
| 528 | # and also check creating more then one iterator at a time |
| 529 | i2 = iter(vim.buffers) |
| 530 | cb.append('i2:' + str(next(i2))) |
| 531 | cb.append('i:' + str(next(i))) |
| 532 | # The following should trigger GC and not cause any problems |
| 533 | del i |
| 534 | del i2 |
| 535 | i3 = iter(vim.buffers) |
| 536 | cb.append('i3:' + str(next(i3))) |
| 537 | del i3 |
| 538 | |
| 539 | prevnum = 0 |
| 540 | for b in vim.buffers: |
| 541 | # Check buffer order |
| 542 | if prevnum >= b.number: |
| 543 | cb.append('!!! Buffer numbers not in strictly ascending order') |
| 544 | # Check indexing: vim.buffers[number].number == number |
| 545 | cb.append(str(b.number) + ':' + repr(vim.buffers[b.number]) + '=' + repr(b)) |
| 546 | prevnum = b.number |
| 547 | |
| 548 | cb.append(str(len(vim.buffers))) |
| 549 | |
| 550 | bnums = list(map(lambda b: b.number, vim.buffers))[1:] |
| 551 | |
| 552 | # Test wiping out buffer with existing iterator |
| 553 | i4 = iter(vim.buffers) |
| 554 | cb.append('i4:' + str(next(i4))) |
| 555 | vim.command('bwipeout! ' + str(bnums.pop(0))) |
| 556 | try: |
| 557 | next(i4) |
| 558 | except vim.error: |
| 559 | pass |
| 560 | else: |
| 561 | cb.append('!!!! No vim.error') |
| 562 | i4 = iter(vim.buffers) |
| 563 | vim.command('bwipeout! ' + str(bnums.pop(-1))) |
| 564 | vim.command('bwipeout! ' + str(bnums.pop(-1))) |
| 565 | cb.append('i4:' + str(next(i4))) |
| 566 | try: |
| 567 | next(i4) |
| 568 | except StopIteration: |
| 569 | cb.append('StopIteration') |
| 570 | EOF |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 571 | :" |
| 572 | :" Test vim.{tabpage,window}list and vim.{tabpage,window} objects |
| 573 | :tabnew 0 |
| 574 | :tabnew 1 |
| 575 | :vnew a.1 |
| 576 | :tabnew 2 |
| 577 | :vnew a.2 |
| 578 | :vnew b.2 |
| 579 | :vnew c.2 |
| 580 | py3 << EOF |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 581 | cb.append('Number of tabs: ' + str(len(vim.tabpages))) |
| 582 | cb.append('Current tab pages:') |
| 583 | |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 584 | def W(w): |
| 585 | if '(unknown)' in repr(w): |
| 586 | return '<window object (unknown)>' |
| 587 | else: |
| 588 | return repr(w) |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 589 | |
| 590 | def Cursor(w, start=len(cb)): |
| 591 | if w.buffer is cb: |
| 592 | return repr((start - w.cursor[0], w.cursor[1])) |
| 593 | else: |
| 594 | return repr(w.cursor) |
| 595 | |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 596 | for t in vim.tabpages: |
| 597 | cb.append(' ' + repr(t) + '(' + str(t.number) + ')' + ': ' + str(len(t.windows)) + ' windows, current is ' + W(t.window)) |
| 598 | cb.append(' Windows:') |
| 599 | for w in t.windows: |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 600 | cb.append(' ' + W(w) + '(' + str(w.number) + ')' + ': displays buffer ' + repr(w.buffer) + '; cursor is at ' + Cursor(w)) |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 601 | # Other values depend on the size of the terminal, so they are checked partly: |
| 602 | for attr in ('height', 'row', 'width', 'col'): |
| 603 | try: |
| 604 | aval = getattr(w, attr) |
| 605 | if type(aval) is not int: |
| 606 | raise TypeError |
| 607 | if aval < 0: |
| 608 | raise ValueError |
| 609 | except Exception as e: |
| 610 | cb.append('!!!!!! Error while getting attribute ' + attr + ': ' + e.__class__.__name__) |
| 611 | w.cursor = (len(w.buffer), 0) |
| 612 | cb.append('Number of windows in current tab page: ' + str(len(vim.windows))) |
| 613 | if list(vim.windows) != list(vim.current.tabpage.windows): |
| 614 | cb.append('!!!!!! Windows differ') |
| 615 | EOF |
| 616 | :" |
| 617 | :" Test vim.current |
| 618 | py3 << EOF |
| 619 | def H(o): |
| 620 | return repr(o) |
| 621 | cb.append('Current tab page: ' + repr(vim.current.tabpage)) |
| 622 | cb.append('Current window: ' + repr(vim.current.window) + ': ' + H(vim.current.window) + ' is ' + H(vim.current.tabpage.window)) |
| 623 | cb.append('Current buffer: ' + repr(vim.current.buffer) + ': ' + H(vim.current.buffer) + ' is ' + H(vim.current.window.buffer)+ ' is ' + H(vim.current.tabpage.window.buffer)) |
| 624 | # Assigning: fails |
| 625 | try: |
| 626 | vim.current.window = vim.tabpages[0].window |
| 627 | except ValueError: |
| 628 | cb.append('ValueError at assigning foreign tab window') |
| 629 | |
| 630 | for attr in ('window', 'tabpage', 'buffer'): |
| 631 | try: |
| 632 | setattr(vim.current, attr, None) |
| 633 | except TypeError: |
| 634 | cb.append('Type error at assigning None to vim.current.' + attr) |
| 635 | |
| 636 | # Assigning: success |
| 637 | vim.current.tabpage = vim.tabpages[-2] |
| 638 | vim.current.buffer = cb |
| 639 | vim.current.window = vim.windows[0] |
| 640 | vim.current.window.cursor = (len(vim.current.buffer), 0) |
| 641 | cb.append('Current tab page: ' + repr(vim.current.tabpage)) |
| 642 | cb.append('Current window: ' + repr(vim.current.window)) |
| 643 | cb.append('Current buffer: ' + repr(vim.current.buffer)) |
| 644 | cb.append('Current line: ' + repr(vim.current.line)) |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 645 | ws = list(vim.windows) |
| 646 | ts = list(vim.tabpages) |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 647 | for b in vim.buffers: |
| 648 | if b is not cb: |
| 649 | vim.command('bwipeout! ' + str(b.number)) |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 650 | cb.append('w.valid: ' + repr([w.valid for w in ws])) |
| 651 | cb.append('t.valid: ' + repr([t.valid for t in ts])) |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 652 | EOF |
| 653 | :tabonly! |
| 654 | :only! |
Bram Moolenaar | cac867a | 2013-05-21 19:50:34 +0200 | [diff] [blame] | 655 | :" |
| 656 | :" Test types |
| 657 | py3 << EOF |
| 658 | for expr, attr in ( |
| 659 | ('vim.vars', 'Dictionary'), |
| 660 | ('vim.options', 'Options'), |
| 661 | ('vim.bindeval("{}")', 'Dictionary'), |
| 662 | ('vim.bindeval("[]")', 'List'), |
| 663 | ('vim.bindeval("function(\'tr\')")', 'Function'), |
| 664 | ('vim.current.buffer', 'Buffer'), |
| 665 | ('vim.current.range', 'Range'), |
| 666 | ('vim.current.window', 'Window'), |
| 667 | ('vim.current.tabpage', 'TabPage'), |
| 668 | ): |
| 669 | cb.append(expr + ':' + attr + ':' + repr(type(eval(expr)) is getattr(vim, attr))) |
| 670 | EOF |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 671 | :" |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 672 | :" Test __dir__() method |
| 673 | py3 << EOF |
| 674 | for name, o in ( |
| 675 | ('current', vim.current), |
| 676 | ('buffer', vim.current.buffer), |
| 677 | ('window', vim.current.window), |
| 678 | ('tabpage', vim.current.tabpage), |
| 679 | ('range', vim.current.range), |
| 680 | ('dictionary', vim.bindeval('{}')), |
| 681 | ('list', vim.bindeval('[]')), |
| 682 | ('function', vim.bindeval('function("tr")')), |
| 683 | ('output', sys.stdout), |
| 684 | ): |
| 685 | cb.append(name + ':' + ','.join(dir(o))) |
| 686 | del name |
| 687 | del o |
| 688 | EOF |
| 689 | :" |
Bram Moolenaar | 01a7a72 | 2013-05-30 12:26:58 +0200 | [diff] [blame^] | 690 | :" |
| 691 | :" Test stdout/stderr |
| 692 | :redir => messages |
| 693 | :py sys.stdout.write('abc') ; sys.stdout.write('def') |
| 694 | :py sys.stderr.write('abc') ; sys.stderr.write('def') |
| 695 | :py sys.stdout.writelines(iter('abc')) |
| 696 | :py sys.stderr.writelines(iter('abc')) |
| 697 | :redir END |
| 698 | :$put =string(substitute(messages, '\d\+', '', 'g')) |
| 699 | :" |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 700 | :" Test exceptions |
| 701 | :fun Exe(e) |
| 702 | : execute a:e |
| 703 | :endfun |
| 704 | py3 << EOF |
| 705 | def ee(expr, g=globals(), l=locals()): |
| 706 | try: |
| 707 | exec(expr, g, l) |
| 708 | except Exception as e: |
| 709 | cb.append(repr((e.__class__, e))) |
| 710 | Exe = vim.bindeval('function("Exe")') |
| 711 | ee('vim.command("throw \'abc\'")') |
| 712 | ee('Exe("throw \'def\'")') |
| 713 | ee('vim.eval("Exe(\'throw \'\'ghi\'\'\')")') |
| 714 | ee('vim.eval("Exe(\'echoerr \'\'jkl\'\'\')")') |
| 715 | ee('vim.eval("Exe(\'xxx_non_existent_command_xxx\')")') |
| 716 | ee('vim.bindeval("Exe(\'xxx_non_existent_command_xxx\')")') |
| 717 | EOF |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 718 | :endfun |
| 719 | :" |
| 720 | :call Test() |
| 721 | :" |
| 722 | :delfunc Test |
| 723 | :call garbagecollect(1) |
| 724 | :" |
| 725 | :/^start:/,$wq! test.out |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 726 | :call getchar() |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 727 | ENDTEST |
| 728 | |
| 729 | start: |