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