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 |
Bram Moolenaar | 8600e40 | 2013-05-30 13:28:41 +0200 | [diff] [blame^] | 10 | :set encoding=latin1 |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 11 | :if !has('python') | e! test.ok | wq! test.out | endif |
Bram Moolenaar | c24c1ac | 2013-05-16 20:47:56 +0200 | [diff] [blame] | 12 | :lang C |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 13 | :py import vim |
| 14 | :fun Test() |
| 15 | :let l = [] |
| 16 | :py l=vim.bindeval('l') |
| 17 | :py f=vim.bindeval('function("strlen")') |
| 18 | :" Extending List directly with different types |
| 19 | :py l.extend([1, "as'd", [1, 2, f, {'a': 1}]]) |
| 20 | :$put =string(l) |
| 21 | :$put =string(l[-1]) |
| 22 | :try |
| 23 | : $put =string(l[-4]) |
| 24 | :catch |
| 25 | : $put =v:exception[:13] |
| 26 | :endtry |
| 27 | :" List assignment |
| 28 | :py l[0]=0 |
| 29 | :$put =string(l) |
| 30 | :py l[-2]=f |
| 31 | :$put =string(l) |
| 32 | :" |
| 33 | :" Extending Dictionary directly with different types |
| 34 | :let d = {} |
Bram Moolenaar | 355fd9b | 2013-05-30 13:14:13 +0200 | [diff] [blame] | 35 | :fun d.f() |
| 36 | : return 1 |
| 37 | :endfun |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 38 | py << EOF |
| 39 | d=vim.bindeval('d') |
| 40 | d['1']='asd' |
| 41 | d.update(b=[1, 2, f]) |
| 42 | d.update((('-1', {'a': 1}),)) |
| 43 | d.update({'0': -1}) |
| 44 | dk = d.keys() |
| 45 | dv = d.values() |
| 46 | di = d.items() |
| 47 | dk.sort(key=repr) |
| 48 | dv.sort(key=repr) |
| 49 | di.sort(key=repr) |
| 50 | EOF |
Bram Moolenaar | 355fd9b | 2013-05-30 13:14:13 +0200 | [diff] [blame] | 51 | :$put =pyeval('d[''f''](self={})') |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 52 | :$put =pyeval('repr(dk)') |
| 53 | :$put =substitute(pyeval('repr(dv)'),'0x\x\+','','g') |
| 54 | :$put =substitute(pyeval('repr(di)'),'0x\x\+','','g') |
Bram Moolenaar | 355fd9b | 2013-05-30 13:14:13 +0200 | [diff] [blame] | 55 | :for [key, Val] in sort(items(d)) |
| 56 | : $put =string(key) . ' : ' . string(Val) |
| 57 | : unlet key Val |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 58 | :endfor |
| 59 | :" |
| 60 | :" removing items with del |
| 61 | :py del l[2] |
| 62 | :$put =string(l) |
| 63 | :let l = range(8) |
| 64 | :py l=vim.bindeval('l') |
| 65 | :try |
| 66 | : py del l[:3] |
| 67 | : py del l[1:] |
| 68 | :catch |
| 69 | : $put =v:exception |
| 70 | :endtry |
| 71 | :$put =string(l) |
| 72 | :" |
| 73 | :py del d['-1'] |
Bram Moolenaar | 355fd9b | 2013-05-30 13:14:13 +0200 | [diff] [blame] | 74 | :py del d['f'] |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 75 | :$put =string(pyeval('d.get(''b'', 1)')) |
| 76 | :$put =string(pyeval('d.pop(''b'')')) |
| 77 | :$put =string(pyeval('d.get(''b'', 1)')) |
| 78 | :$put =string(pyeval('d.pop(''1'', 2)')) |
| 79 | :$put =string(pyeval('d.pop(''1'', 2)')) |
| 80 | :$put =pyeval('repr(d.has_key(''0''))') |
| 81 | :$put =pyeval('repr(d.has_key(''1''))') |
| 82 | :$put =pyeval('repr(''0'' in d)') |
| 83 | :$put =pyeval('repr(''1'' in d)') |
| 84 | :$put =pyeval('repr(list(iter(d)))') |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 85 | :$put =string(d) |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 86 | :$put =pyeval('repr(d.popitem(''0''))') |
| 87 | :$put =pyeval('repr(d.get(''0''))') |
| 88 | :$put =pyeval('repr(list(iter(d)))') |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 89 | :" |
| 90 | :" removing items out of range: silently skip items that don't exist |
| 91 | :let l = [0, 1, 2, 3] |
| 92 | :py l=vim.bindeval('l') |
| 93 | :" The following two ranges delete nothing as they match empty list: |
| 94 | :py del l[2:1] |
| 95 | :$put =string(l) |
| 96 | :py del l[2:2] |
| 97 | :$put =string(l) |
| 98 | :py del l[2:3] |
| 99 | :$put =string(l) |
| 100 | :let l = [0, 1, 2, 3] |
| 101 | :py l=vim.bindeval('l') |
| 102 | :py del l[2:4] |
| 103 | :$put =string(l) |
| 104 | :let l = [0, 1, 2, 3] |
| 105 | :py l=vim.bindeval('l') |
| 106 | :py del l[2:5] |
| 107 | :$put =string(l) |
| 108 | :let l = [0, 1, 2, 3] |
| 109 | :py l=vim.bindeval('l') |
| 110 | :py del l[2:6] |
| 111 | :$put =string(l) |
| 112 | :let l = [0, 1, 2, 3] |
| 113 | :py l=vim.bindeval('l') |
| 114 | :" The following two ranges delete nothing as they match empty list: |
| 115 | :py del l[-1:2] |
| 116 | :$put =string(l) |
| 117 | :py del l[-2:2] |
| 118 | :$put =string(l) |
| 119 | :py del l[-3:2] |
| 120 | :$put =string(l) |
| 121 | :let l = [0, 1, 2, 3] |
| 122 | :py l=vim.bindeval('l') |
| 123 | :py del l[-4:2] |
| 124 | :$put =string(l) |
| 125 | :let l = [0, 1, 2, 3] |
| 126 | :py l=vim.bindeval('l') |
| 127 | :py del l[-5:2] |
| 128 | :$put =string(l) |
| 129 | :let l = [0, 1, 2, 3] |
| 130 | :py l=vim.bindeval('l') |
| 131 | :py del l[-6:2] |
| 132 | :$put =string(l) |
| 133 | :" |
| 134 | :" Slice assignment to a list |
| 135 | :let l = [0, 1, 2, 3] |
| 136 | :py l=vim.bindeval('l') |
| 137 | :py l[0:0]=['a'] |
| 138 | :$put =string(l) |
| 139 | :let l = [0, 1, 2, 3] |
| 140 | :py l=vim.bindeval('l') |
| 141 | :py l[1:2]=['b'] |
| 142 | :$put =string(l) |
| 143 | :let l = [0, 1, 2, 3] |
| 144 | :py l=vim.bindeval('l') |
| 145 | :py l[2:4]=['c'] |
| 146 | :$put =string(l) |
| 147 | :let l = [0, 1, 2, 3] |
| 148 | :py l=vim.bindeval('l') |
| 149 | :py l[4:4]=['d'] |
| 150 | :$put =string(l) |
| 151 | :let l = [0, 1, 2, 3] |
| 152 | :py l=vim.bindeval('l') |
| 153 | :py l[-1:2]=['e'] |
| 154 | :$put =string(l) |
| 155 | :let l = [0, 1, 2, 3] |
| 156 | :py l=vim.bindeval('l') |
| 157 | :py l[-10:2]=['f'] |
| 158 | :$put =string(l) |
| 159 | :let l = [0, 1, 2, 3] |
| 160 | :py l=vim.bindeval('l') |
| 161 | :py l[2:-10]=['g'] |
| 162 | :$put =string(l) |
| 163 | :let l = [] |
| 164 | :py l=vim.bindeval('l') |
| 165 | :py l[0:0]=['h'] |
| 166 | :$put =string(l) |
| 167 | :" |
| 168 | :" Locked variables |
| 169 | :let l = [0, 1, 2, 3] |
| 170 | :py l=vim.bindeval('l') |
| 171 | :lockvar! l |
| 172 | :py l[2]='i' |
| 173 | :$put =string(l) |
| 174 | :unlockvar! l |
| 175 | :" |
| 176 | :" Function calls |
| 177 | :function New(...) |
| 178 | :return ['NewStart']+a:000+['NewEnd'] |
| 179 | :endfunction |
| 180 | :function DictNew(...) dict |
| 181 | :return ['DictNewStart']+a:000+['DictNewEnd', self] |
| 182 | :endfunction |
| 183 | :let l=[function('New'), function('DictNew')] |
| 184 | :py l=vim.bindeval('l') |
| 185 | :py l.extend(list(l[0](1, 2, 3))) |
| 186 | :$put =string(l) |
| 187 | :py l.extend(list(l[1](1, 2, 3, self={'a': 'b'}))) |
| 188 | :$put =string(l) |
| 189 | :py l.extend([l[0].name]) |
| 190 | :$put =string(l) |
| 191 | :try |
| 192 | : py l[1](1, 2, 3) |
| 193 | :catch |
| 194 | : $put =v:exception[:16] |
| 195 | :endtry |
Bram Moolenaar | 355fd9b | 2013-05-30 13:14:13 +0200 | [diff] [blame] | 196 | :py f=l[0] |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 197 | :delfunction New |
| 198 | :try |
Bram Moolenaar | 355fd9b | 2013-05-30 13:14:13 +0200 | [diff] [blame] | 199 | : py f(1, 2, 3) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 200 | :catch |
| 201 | : $put =v:exception[:16] |
| 202 | :endtry |
| 203 | :if has('float') |
| 204 | : let l=[0.0] |
| 205 | : py l=vim.bindeval('l') |
| 206 | : py l.extend([0.0]) |
| 207 | : $put =string(l) |
| 208 | :else |
| 209 | : $put ='[0.0, 0.0]' |
| 210 | :endif |
Bram Moolenaar | c11073c | 2012-09-05 19:17:42 +0200 | [diff] [blame] | 211 | :let messages=[] |
Bram Moolenaar | 03db85b | 2013-05-15 14:51:35 +0200 | [diff] [blame] | 212 | py <<EOF |
Bram Moolenaar | c11073c | 2012-09-05 19:17:42 +0200 | [diff] [blame] | 213 | d=vim.bindeval('{}') |
| 214 | m=vim.bindeval('messages') |
Bram Moolenaar | 03db85b | 2013-05-15 14:51:35 +0200 | [diff] [blame] | 215 | def em(expr, g=globals(), l=locals()): |
| 216 | try: |
| 217 | exec(expr, g, l) |
| 218 | except: |
| 219 | m.extend([sys.exc_type.__name__]) |
Bram Moolenaar | c11073c | 2012-09-05 19:17:42 +0200 | [diff] [blame] | 220 | |
Bram Moolenaar | 03db85b | 2013-05-15 14:51:35 +0200 | [diff] [blame] | 221 | em('d["abc"]') |
| 222 | em('d["abc"]="\\0"') |
| 223 | em('d["abc"]=vim') |
| 224 | em('d[""]=1') |
| 225 | em('d["a\\0b"]=1') |
| 226 | em('d[u"a\\0b"]=1') |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 227 | |
| 228 | em('d.pop("abc")') |
| 229 | em('d.popitem("abc")') |
Bram Moolenaar | c11073c | 2012-09-05 19:17:42 +0200 | [diff] [blame] | 230 | EOF |
| 231 | :$put =messages |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 232 | :unlet messages |
| 233 | :" locked and scope attributes |
| 234 | :let d={} | let dl={} | lockvar dl |
| 235 | :for s in split("d dl v: g:") |
| 236 | : let name=tr(s, ':', 's') |
| 237 | : execute 'py '.name.'=vim.bindeval("'.s.'")' |
| 238 | : let toput=s.' : '.join(map(['locked', 'scope'], 'v:val.":".pyeval(name.".".v:val)'), ';') |
| 239 | : $put =toput |
| 240 | :endfor |
| 241 | :silent! let d.abc=1 |
| 242 | :silent! let dl.abc=1 |
| 243 | :py d.locked=True |
| 244 | :py dl.locked=False |
| 245 | :silent! let d.def=1 |
| 246 | :silent! let dl.def=1 |
| 247 | :put ='d:'.string(d) |
| 248 | :put ='dl:'.string(dl) |
| 249 | :unlet d dl |
| 250 | : |
| 251 | :let l=[] | let ll=[] | lockvar ll |
| 252 | :for s in split("l ll") |
| 253 | : let name=tr(s, ':', 's') |
| 254 | : execute 'py '.name.'=vim.bindeval("'.s.'")' |
| 255 | : let toput=s.' : locked:'.pyeval(name.'.locked') |
| 256 | : $put =toput |
| 257 | :endfor |
| 258 | :silent! call extend(l, [0]) |
| 259 | :silent! call extend(ll, [0]) |
| 260 | :py l.locked=True |
| 261 | :py ll.locked=False |
| 262 | :silent! call extend(l, [1]) |
| 263 | :silent! call extend(ll, [1]) |
| 264 | :put ='l:'.string(l) |
| 265 | :put ='ll:'.string(ll) |
| 266 | :unlet l ll |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 267 | :" |
| 268 | :" pyeval() |
| 269 | :let l=pyeval('range(3)') |
| 270 | :$put =string(l) |
| 271 | :let d=pyeval('{"a": "b", "c": 1, "d": ["e"]}') |
| 272 | :$put =sort(items(d)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 273 | :if has('float') |
| 274 | : let f=pyeval('0.0') |
| 275 | : $put =string(f) |
| 276 | :else |
| 277 | : $put ='0.0' |
| 278 | :endif |
Bram Moolenaar | c11073c | 2012-09-05 19:17:42 +0200 | [diff] [blame] | 279 | :" Invalid values: |
| 280 | :for e in ['"\0"', '{"\0": 1}', 'undefined_name', 'vim'] |
| 281 | : try |
| 282 | : let v=pyeval(e) |
| 283 | : catch |
| 284 | : let toput=e.":\t".v:exception[:13] |
| 285 | : $put =toput |
| 286 | : endtry |
| 287 | :endfor |
Bram Moolenaar | 76d711c | 2013-02-13 14:17:08 +0100 | [diff] [blame] | 288 | :" |
| 289 | :" threading |
| 290 | :let l = [0] |
| 291 | :py l=vim.bindeval('l') |
| 292 | :py <<EOF |
| 293 | import threading |
| 294 | import time |
| 295 | |
| 296 | class T(threading.Thread): |
| 297 | def __init__(self): |
| 298 | threading.Thread.__init__(self) |
| 299 | self.t = 0 |
| 300 | self.running = True |
| 301 | |
| 302 | def run(self): |
| 303 | while self.running: |
| 304 | self.t += 1 |
| 305 | time.sleep(0.1) |
| 306 | |
| 307 | t = T() |
| 308 | t.start() |
| 309 | EOF |
| 310 | :sleep 1 |
| 311 | :py t.running = False |
| 312 | :py t.join() |
| 313 | :py l[0] = t.t > 8 # check if the background thread is working |
| 314 | :$put =string(l) |
| 315 | :" |
| 316 | :" settrace |
| 317 | :let l = [] |
| 318 | :py l=vim.bindeval('l') |
| 319 | :py <<EOF |
| 320 | import sys |
| 321 | |
| 322 | def traceit(frame, event, arg): |
| 323 | global l |
| 324 | if event == "line": |
| 325 | l.extend([frame.f_lineno]) |
| 326 | return traceit |
| 327 | |
| 328 | def trace_main(): |
| 329 | for i in range(5): |
| 330 | pass |
| 331 | EOF |
| 332 | :py sys.settrace(traceit) |
| 333 | :py trace_main() |
| 334 | :py sys.settrace(None) |
| 335 | :$put =string(l) |
Bram Moolenaar | 24b11fb | 2013-04-05 19:32:36 +0200 | [diff] [blame] | 336 | :" |
| 337 | :" Slice |
| 338 | :py ll = vim.bindeval('[0, 1, 2, 3, 4, 5]') |
| 339 | :py l = ll[:4] |
| 340 | :$put =string(pyeval('l')) |
| 341 | :py l = ll[2:] |
| 342 | :$put =string(pyeval('l')) |
| 343 | :py l = ll[:-4] |
| 344 | :$put =string(pyeval('l')) |
| 345 | :py l = ll[-2:] |
| 346 | :$put =string(pyeval('l')) |
| 347 | :py l = ll[2:4] |
| 348 | :$put =string(pyeval('l')) |
| 349 | :py l = ll[4:2] |
| 350 | :$put =string(pyeval('l')) |
| 351 | :py l = ll[-4:-2] |
| 352 | :$put =string(pyeval('l')) |
| 353 | :py l = ll[-2:-4] |
| 354 | :$put =string(pyeval('l')) |
| 355 | :py l = ll[:] |
| 356 | :$put =string(pyeval('l')) |
| 357 | :py l = ll[0:6] |
| 358 | :$put =string(pyeval('l')) |
| 359 | :py l = ll[-10:10] |
| 360 | :$put =string(pyeval('l')) |
Bram Moolenaar | 230bb3f | 2013-04-24 14:07:45 +0200 | [diff] [blame] | 361 | :" |
| 362 | :" Vars |
| 363 | :let g:foo = 'bac' |
| 364 | :let w:abc = 'def' |
| 365 | :let b:baz = 'bar' |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 366 | :let t:bar = 'jkl' |
Bram Moolenaar | 230bb3f | 2013-04-24 14:07:45 +0200 | [diff] [blame] | 367 | :try |
| 368 | : throw "Abc" |
| 369 | :catch |
| 370 | : put =pyeval('vim.vvars[''exception'']') |
| 371 | :endtry |
| 372 | :put =pyeval('vim.vars[''foo'']') |
| 373 | :put =pyeval('vim.current.window.vars[''abc'']') |
| 374 | :put =pyeval('vim.current.buffer.vars[''baz'']') |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 375 | :put =pyeval('vim.current.tabpage.vars[''bar'']') |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 376 | :" |
| 377 | :" Options |
| 378 | :" paste: boolean, global |
| 379 | :" previewheight number, global |
| 380 | :" operatorfunc: string, global |
| 381 | :" number: boolean, window-local |
| 382 | :" numberwidth: number, window-local |
| 383 | :" colorcolumn: string, window-local |
| 384 | :" statusline: string, window-local/global |
| 385 | :" autoindent: boolean, buffer-local |
Bram Moolenaar | 55b8ad3 | 2013-05-17 13:38:04 +0200 | [diff] [blame] | 386 | :" shiftwidth: number, buffer-local |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 387 | :" omnifunc: string, buffer-local |
| 388 | :" preserveindent: boolean, buffer-local/global |
| 389 | :" path: string, buffer-local/global |
| 390 | :let g:bufs=[bufnr('%')] |
| 391 | :new |
| 392 | :let g:bufs+=[bufnr('%')] |
| 393 | :vnew |
| 394 | :let g:bufs+=[bufnr('%')] |
| 395 | :wincmd j |
| 396 | :vnew |
| 397 | :let g:bufs+=[bufnr('%')] |
| 398 | :wincmd l |
| 399 | :fun RecVars(opt) |
| 400 | : let gval =string(eval('&g:'.a:opt)) |
| 401 | : let wvals=join(map(range(1, 4), 'v:val.":".string(getwinvar(v:val, "&".a:opt))')) |
| 402 | : let bvals=join(map(copy(g:bufs), 'v:val.":".string(getbufvar(v:val, "&".a:opt))')) |
| 403 | : put =' G: '.gval |
| 404 | : put =' W: '.wvals |
| 405 | : put =' B: '.wvals |
| 406 | :endfun |
| 407 | py << EOF |
| 408 | def e(s, g=globals(), l=locals()): |
| 409 | try: |
| 410 | exec(s, g, l) |
Bram Moolenaar | 03db85b | 2013-05-15 14:51:35 +0200 | [diff] [blame] | 411 | except: |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 412 | vim.command('return ' + repr(sys.exc_type.__name__)) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 413 | |
| 414 | def ev(s, g=globals(), l=locals()): |
| 415 | try: |
| 416 | return eval(s, g, l) |
Bram Moolenaar | 03db85b | 2013-05-15 14:51:35 +0200 | [diff] [blame] | 417 | except: |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 418 | vim.command('let exc=' + repr(sys.exc_type.__name__)) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 419 | return 0 |
| 420 | EOF |
| 421 | :function E(s) |
| 422 | : python e(vim.eval('a:s')) |
| 423 | :endfunction |
| 424 | :function Ev(s) |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 425 | : let r=pyeval('ev(vim.eval("a:s"))') |
| 426 | : if exists('exc') |
| 427 | : throw exc |
| 428 | : endif |
| 429 | : return r |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 430 | :endfunction |
| 431 | :py gopts1=vim.options |
| 432 | :py wopts1=vim.windows[2].options |
| 433 | :py wopts2=vim.windows[0].options |
| 434 | :py wopts3=vim.windows[1].options |
| 435 | :py bopts1=vim.buffers[vim.bindeval("g:bufs")[2]].options |
| 436 | :py bopts2=vim.buffers[vim.bindeval("g:bufs")[1]].options |
| 437 | :py bopts3=vim.buffers[vim.bindeval("g:bufs")[0]].options |
| 438 | :let lst=[] |
| 439 | :let lst+=[['paste', 1, 0, 1, 2, 1, 1, 0 ]] |
| 440 | :let lst+=[['previewheight', 5, 1, 6, 'a', 0, 1, 0 ]] |
| 441 | :let lst+=[['operatorfunc', 'A', 'B', 'C', 2, 0, 1, 0 ]] |
| 442 | :let lst+=[['number', 0, 1, 1, 0, 1, 0, 1 ]] |
| 443 | :let lst+=[['numberwidth', 2, 3, 5, -100, 0, 0, 1 ]] |
| 444 | :let lst+=[['colorcolumn', '+1', '+2', '+3', 'abc', 0, 0, 1 ]] |
| 445 | :let lst+=[['statusline', '1', '2', '4', 0, 0, 1, 1 ]] |
| 446 | :let lst+=[['autoindent', 0, 1, 1, 2, 1, 0, 2 ]] |
Bram Moolenaar | 55b8ad3 | 2013-05-17 13:38:04 +0200 | [diff] [blame] | 447 | :let lst+=[['shiftwidth', 0, 2, 1, 3, 0, 0, 2 ]] |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 448 | :let lst+=[['omnifunc', 'A', 'B', 'C', 1, 0, 0, 2 ]] |
| 449 | :let lst+=[['preserveindent', 0, 1, 1, 2, 1, 1, 2 ]] |
| 450 | :let lst+=[['path', '.,,', ',,', '.', 0, 0, 1, 2 ]] |
| 451 | :for [oname, oval1, oval2, oval3, invval, bool, global, local] in lst |
| 452 | : py oname=vim.eval('oname') |
| 453 | : py oval1=vim.bindeval('oval1') |
| 454 | : py oval2=vim.bindeval('oval2') |
| 455 | : py oval3=vim.bindeval('oval3') |
| 456 | : if invval is 0 || invval is 1 |
| 457 | : py invval=bool(vim.bindeval('invval')) |
| 458 | : else |
| 459 | : py invval=vim.bindeval('invval') |
| 460 | : endif |
| 461 | : if bool |
| 462 | : py oval1=bool(oval1) |
| 463 | : py oval2=bool(oval2) |
| 464 | : py oval3=bool(oval3) |
| 465 | : endif |
| 466 | : put ='>>> '.oname |
| 467 | : for v in ['gopts1', 'wopts1', 'bopts1'] |
| 468 | : try |
| 469 | : put =' p/'.v.': '.Ev('repr('.v.'['''.oname.'''])') |
| 470 | : catch |
| 471 | : put =' p/'.v.'! '.v:exception |
| 472 | : endtry |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 473 | : let r=E(v.'['''.oname.''']=invval') |
| 474 | : if r isnot 0 |
| 475 | : put =' inv: '.string(invval).'! '.r |
| 476 | : endif |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 477 | : for vv in (v is# 'gopts1' ? [v] : [v, v[:-2].'2', v[:-2].'3']) |
| 478 | : let val=substitute(vv, '^.opts', 'oval', '') |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 479 | : let r=E(vv.'['''.oname.''']='.val) |
| 480 | : if r isnot 0 |
| 481 | : put =' '.vv.'! '.r |
| 482 | : endif |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 483 | : endfor |
| 484 | : endfor |
| 485 | : call RecVars(oname) |
| 486 | : for v in ['wopts3', 'bopts3'] |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 487 | : let r=E('del '.v.'["'.oname.'"]') |
| 488 | : if r isnot 0 |
| 489 | : put =' del '.v.'! '.r |
| 490 | : endif |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 491 | : endfor |
| 492 | : call RecVars(oname) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 493 | :endfor |
| 494 | :only |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 495 | :for buf in g:bufs[1:] |
| 496 | : execute 'bwipeout!' buf |
| 497 | :endfor |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 498 | :" |
| 499 | :" Test buffer object |
| 500 | :vnew |
| 501 | :put ='First line' |
| 502 | :put ='Second line' |
| 503 | :put ='Third line' |
| 504 | :1 delete _ |
| 505 | :py b=vim.current.buffer |
| 506 | :wincmd w |
| 507 | :mark a |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 508 | :augroup BUFS |
| 509 | : autocmd BufFilePost * python cb.append(vim.eval('expand("<abuf>")') + ':BufFilePost:' + vim.eval('bufnr("%")')) |
| 510 | : autocmd BufFilePre * python cb.append(vim.eval('expand("<abuf>")') + ':BufFilePre:' + vim.eval('bufnr("%")')) |
| 511 | :augroup END |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 512 | py << EOF |
| 513 | cb = vim.current.buffer |
| 514 | # Tests BufferAppend and BufferItem |
| 515 | cb.append(b[0]) |
| 516 | # Tests BufferSlice and BufferAssSlice |
| 517 | cb.append('abc') # Will be overwritten |
| 518 | cb[-1:] = b[:-2] |
| 519 | # Test BufferLength and BufferAssSlice |
| 520 | cb.append('def') # Will not be overwritten |
| 521 | cb[len(cb):] = b[:] |
| 522 | # Test BufferAssItem and BufferMark |
| 523 | cb.append('ghi') # Will be overwritten |
| 524 | cb[-1] = repr((len(cb) - cb.mark('a')[0], cb.mark('a')[1])) |
| 525 | # Test BufferRepr |
| 526 | cb.append(repr(cb) + repr(b)) |
| 527 | # Modify foreign buffer |
| 528 | b.append('foo') |
| 529 | b[0]='bar' |
| 530 | b[0:0]=['baz'] |
| 531 | vim.command('call append("$", getbufline(%i, 1, "$"))' % b.number) |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 532 | # Test assigning to name property |
| 533 | old_name = cb.name |
| 534 | cb.name = 'foo' |
| 535 | cb.append(cb.name[-11:]) |
| 536 | b.name = 'bar' |
| 537 | cb.append(b.name[-11:]) |
| 538 | cb.name = old_name |
| 539 | cb.append(cb.name[-17:]) |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 540 | # Test CheckBuffer |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 541 | for _b in vim.buffers: |
| 542 | if _b is not cb: |
| 543 | vim.command('bwipeout! ' + str(_b.number)) |
| 544 | del _b |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 545 | cb.append('valid: b:%s, cb:%s' % (repr(b.valid), repr(cb.valid))) |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 546 | for expr in ('b[1]','b[:] = ["A", "B"]','b[:]','b.append("abc")', 'b.name = "!"'): |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 547 | try: |
| 548 | exec(expr) |
| 549 | except vim.error: |
| 550 | pass |
| 551 | else: |
| 552 | # Usually a SEGV here |
| 553 | # Should not happen in any case |
| 554 | cb.append('No exception for ' + expr) |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 555 | vim.command('cd .') |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 556 | EOF |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 557 | :augroup BUFS |
| 558 | : autocmd! |
| 559 | :augroup END |
| 560 | :augroup! BUFS |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 561 | :" |
| 562 | :" Test vim.buffers object |
| 563 | :set hidden |
| 564 | :edit a |
| 565 | :buffer # |
| 566 | :edit b |
| 567 | :buffer # |
| 568 | :edit c |
| 569 | :buffer # |
| 570 | py << EOF |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 571 | try: |
| 572 | from __builtin__ import next |
| 573 | except ImportError: |
| 574 | next = lambda o: o.next() |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 575 | # Check GCing iterator that was not fully exhausted |
| 576 | i = iter(vim.buffers) |
| 577 | cb.append('i:' + str(next(i))) |
| 578 | # and also check creating more then one iterator at a time |
| 579 | i2 = iter(vim.buffers) |
| 580 | cb.append('i2:' + str(next(i2))) |
| 581 | cb.append('i:' + str(next(i))) |
| 582 | # The following should trigger GC and not cause any problems |
| 583 | del i |
| 584 | del i2 |
| 585 | i3 = iter(vim.buffers) |
| 586 | cb.append('i3:' + str(next(i3))) |
| 587 | del i3 |
| 588 | |
| 589 | prevnum = 0 |
| 590 | for b in vim.buffers: |
| 591 | # Check buffer order |
| 592 | if prevnum >= b.number: |
| 593 | cb.append('!!! Buffer numbers not in strictly ascending order') |
| 594 | # Check indexing: vim.buffers[number].number == number |
| 595 | cb.append(str(b.number) + ':' + repr(vim.buffers[b.number]) + '=' + repr(b)) |
| 596 | prevnum = b.number |
| 597 | |
| 598 | cb.append(str(len(vim.buffers))) |
| 599 | |
| 600 | bnums = list(map(lambda b: b.number, vim.buffers))[1:] |
| 601 | |
| 602 | # Test wiping out buffer with existing iterator |
| 603 | i4 = iter(vim.buffers) |
| 604 | cb.append('i4:' + str(next(i4))) |
| 605 | vim.command('bwipeout! ' + str(bnums.pop(0))) |
| 606 | try: |
| 607 | next(i4) |
| 608 | except vim.error: |
| 609 | pass |
| 610 | else: |
| 611 | cb.append('!!!! No vim.error') |
| 612 | i4 = iter(vim.buffers) |
| 613 | vim.command('bwipeout! ' + str(bnums.pop(-1))) |
| 614 | vim.command('bwipeout! ' + str(bnums.pop(-1))) |
| 615 | cb.append('i4:' + str(next(i4))) |
| 616 | try: |
| 617 | next(i4) |
| 618 | except StopIteration: |
| 619 | cb.append('StopIteration') |
| 620 | EOF |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 621 | :" |
| 622 | :" Test vim.{tabpage,window}list and vim.{tabpage,window} objects |
| 623 | :tabnew 0 |
| 624 | :tabnew 1 |
| 625 | :vnew a.1 |
| 626 | :tabnew 2 |
| 627 | :vnew a.2 |
| 628 | :vnew b.2 |
| 629 | :vnew c.2 |
| 630 | py << EOF |
| 631 | cb.append('Number of tabs: ' + str(len(vim.tabpages))) |
| 632 | cb.append('Current tab pages:') |
| 633 | def W(w): |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 634 | if repr(w).find('(unknown)') != -1: |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 635 | return '<window object (unknown)>' |
| 636 | else: |
| 637 | return repr(w) |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 638 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 639 | start = len(cb) |
| 640 | |
| 641 | def Cursor(w): |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 642 | if w.buffer is cb: |
| 643 | return repr((start - w.cursor[0], w.cursor[1])) |
| 644 | else: |
| 645 | return repr(w.cursor) |
| 646 | |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 647 | for t in vim.tabpages: |
| 648 | cb.append(' ' + repr(t) + '(' + str(t.number) + ')' + ': ' + str(len(t.windows)) + ' windows, current is ' + W(t.window)) |
| 649 | cb.append(' Windows:') |
| 650 | for w in t.windows: |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 651 | 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] | 652 | # Other values depend on the size of the terminal, so they are checked partly: |
| 653 | for attr in ('height', 'row', 'width', 'col'): |
| 654 | try: |
| 655 | aval = getattr(w, attr) |
| 656 | if type(aval) is not long: |
| 657 | raise TypeError |
| 658 | if aval < 0: |
| 659 | raise ValueError |
| 660 | except Exception: |
| 661 | cb.append('!!!!!! Error while getting attribute ' + attr + ': ' + sys.exc_type.__name__) |
| 662 | w.cursor = (len(w.buffer), 0) |
| 663 | cb.append('Number of windows in current tab page: ' + str(len(vim.windows))) |
| 664 | if list(vim.windows) != list(vim.current.tabpage.windows): |
| 665 | cb.append('!!!!!! Windows differ') |
| 666 | EOF |
| 667 | :" |
| 668 | :" Test vim.current |
| 669 | py << EOF |
| 670 | def H(o): |
| 671 | return repr(o) |
| 672 | cb.append('Current tab page: ' + repr(vim.current.tabpage)) |
| 673 | cb.append('Current window: ' + repr(vim.current.window) + ': ' + H(vim.current.window) + ' is ' + H(vim.current.tabpage.window)) |
| 674 | 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)) |
| 675 | # Assigning: fails |
| 676 | try: |
| 677 | vim.current.window = vim.tabpages[0].window |
| 678 | except ValueError: |
| 679 | cb.append('ValueError at assigning foreign tab window') |
| 680 | |
| 681 | for attr in ('window', 'tabpage', 'buffer'): |
| 682 | try: |
| 683 | setattr(vim.current, attr, None) |
| 684 | except TypeError: |
| 685 | cb.append('Type error at assigning None to vim.current.' + attr) |
| 686 | |
| 687 | # Assigning: success |
| 688 | vim.current.tabpage = vim.tabpages[-2] |
| 689 | vim.current.buffer = cb |
| 690 | vim.current.window = vim.windows[0] |
| 691 | vim.current.window.cursor = (len(vim.current.buffer), 0) |
| 692 | cb.append('Current tab page: ' + repr(vim.current.tabpage)) |
| 693 | cb.append('Current window: ' + repr(vim.current.window)) |
| 694 | cb.append('Current buffer: ' + repr(vim.current.buffer)) |
| 695 | cb.append('Current line: ' + repr(vim.current.line)) |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 696 | ws = list(vim.windows) |
| 697 | ts = list(vim.tabpages) |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 698 | for b in vim.buffers: |
| 699 | if b is not cb: |
Bram Moolenaar | cac867a | 2013-05-21 19:50:34 +0200 | [diff] [blame] | 700 | vim.command('bwipeout! ' + str(b.number)) |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 701 | cb.append('w.valid: ' + repr([w.valid for w in ws])) |
| 702 | cb.append('t.valid: ' + repr([t.valid for t in ts])) |
Bram Moolenaar | a472001 | 2013-05-15 16:27:37 +0200 | [diff] [blame] | 703 | EOF |
| 704 | :tabonly! |
| 705 | :only! |
Bram Moolenaar | cac867a | 2013-05-21 19:50:34 +0200 | [diff] [blame] | 706 | :" |
| 707 | :" Test types |
| 708 | py << EOF |
| 709 | for expr, attr in ( |
| 710 | ('vim.vars', 'Dictionary'), |
| 711 | ('vim.options', 'Options'), |
| 712 | ('vim.bindeval("{}")', 'Dictionary'), |
| 713 | ('vim.bindeval("[]")', 'List'), |
| 714 | ('vim.bindeval("function(\'tr\')")', 'Function'), |
| 715 | ('vim.current.buffer', 'Buffer'), |
| 716 | ('vim.current.range', 'Range'), |
| 717 | ('vim.current.window', 'Window'), |
| 718 | ('vim.current.tabpage', 'TabPage'), |
| 719 | ): |
| 720 | cb.append(expr + ':' + attr + ':' + repr(type(eval(expr)) is getattr(vim, attr))) |
| 721 | EOF |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 722 | :" |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 723 | :" Test __dir__() method |
| 724 | py << EOF |
| 725 | for name, o in ( |
| 726 | ('current', vim.current), |
| 727 | ('buffer', vim.current.buffer), |
| 728 | ('window', vim.current.window), |
| 729 | ('tabpage', vim.current.tabpage), |
| 730 | ('range', vim.current.range), |
| 731 | ('dictionary', vim.bindeval('{}')), |
| 732 | ('list', vim.bindeval('[]')), |
| 733 | ('function', vim.bindeval('function("tr")')), |
| 734 | ('output', sys.stdout), |
| 735 | ): |
| 736 | cb.append(name + ':' + ','.join(dir(o))) |
| 737 | del name |
| 738 | del o |
| 739 | EOF |
| 740 | :" |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 741 | :" Test vim.*.__new__ |
| 742 | :$put =string(pyeval('vim.Dictionary({})')) |
| 743 | :$put =string(pyeval('vim.Dictionary(a=1)')) |
| 744 | :$put =string(pyeval('vim.Dictionary(((''a'', 1),))')) |
Bram Moolenaar | 78cddbe | 2013-05-30 13:05:58 +0200 | [diff] [blame] | 745 | :$put =string(pyeval('vim.List()')) |
| 746 | :$put =string(pyeval('vim.List(iter(''abc''))')) |
Bram Moolenaar | 355fd9b | 2013-05-30 13:14:13 +0200 | [diff] [blame] | 747 | :$put =string(pyeval('vim.Function(''tr'')')) |
Bram Moolenaar | 01a7a72 | 2013-05-30 12:26:58 +0200 | [diff] [blame] | 748 | :" |
| 749 | :" Test stdout/stderr |
| 750 | :redir => messages |
| 751 | :py sys.stdout.write('abc') ; sys.stdout.write('def') |
| 752 | :py sys.stderr.write('abc') ; sys.stderr.write('def') |
| 753 | :py sys.stdout.writelines(iter('abc')) |
| 754 | :py sys.stderr.writelines(iter('abc')) |
| 755 | :redir END |
| 756 | :$put =string(substitute(messages, '\d\+', '', 'g')) |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 757 | :" Test subclassing |
Bram Moolenaar | 355fd9b | 2013-05-30 13:14:13 +0200 | [diff] [blame] | 758 | :fun Put(...) |
| 759 | : $put =string(a:000) |
| 760 | : return a:000 |
| 761 | :endfun |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 762 | py << EOF |
| 763 | class DupDict(vim.Dictionary): |
| 764 | def __setitem__(self, key, value): |
| 765 | super(DupDict, self).__setitem__(key, value) |
| 766 | super(DupDict, self).__setitem__('dup_' + key, value) |
| 767 | dd = DupDict() |
| 768 | dd['a'] = 'b' |
Bram Moolenaar | 78cddbe | 2013-05-30 13:05:58 +0200 | [diff] [blame] | 769 | |
| 770 | class DupList(vim.List): |
| 771 | def __getitem__(self, idx): |
| 772 | return [super(DupList, self).__getitem__(idx)] * 2 |
| 773 | |
| 774 | dl = DupList() |
| 775 | dl2 = DupList(iter('abc')) |
| 776 | dl.extend(dl2[0]) |
Bram Moolenaar | 355fd9b | 2013-05-30 13:14:13 +0200 | [diff] [blame] | 777 | |
| 778 | class DupFun(vim.Function): |
| 779 | def __call__(self, arg): |
| 780 | return super(DupFun, self).__call__(arg, arg) |
| 781 | |
| 782 | df = DupFun('Put') |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 783 | EOF |
| 784 | :$put =string(sort(keys(pyeval('dd')))) |
Bram Moolenaar | 78cddbe | 2013-05-30 13:05:58 +0200 | [diff] [blame] | 785 | :$put =string(pyeval('dl')) |
| 786 | :$put =string(pyeval('dl2')) |
Bram Moolenaar | 355fd9b | 2013-05-30 13:14:13 +0200 | [diff] [blame] | 787 | :$put =string(pyeval('df(2)')) |
Bram Moolenaar | 01a7a72 | 2013-05-30 12:26:58 +0200 | [diff] [blame] | 788 | :" |
Bram Moolenaar | 8600e40 | 2013-05-30 13:28:41 +0200 | [diff] [blame^] | 789 | :" Test errors |
| 790 | :fun F() dict |
| 791 | :endfun |
| 792 | :fun D() |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 793 | :endfun |
| 794 | py << EOF |
| 795 | def ee(expr, g=globals(), l=locals()): |
| 796 | try: |
| 797 | exec(expr, g, l) |
| 798 | except: |
Bram Moolenaar | 8600e40 | 2013-05-30 13:28:41 +0200 | [diff] [blame^] | 799 | cb.append(expr + ':' + repr(sys.exc_info()[:2])) |
| 800 | else: |
| 801 | cb.append(expr + ':NOT FAILED') |
| 802 | d = vim.Dictionary() |
| 803 | ned = vim.Dictionary(foo='bar', baz='abc') |
| 804 | dl = vim.Dictionary(a=1) |
| 805 | dl.locked = True |
| 806 | l = vim.List() |
| 807 | ll = vim.List('abc') |
| 808 | ll.locked = True |
| 809 | f = vim.Function('string') |
| 810 | fd = vim.Function('F') |
| 811 | fdel = vim.Function('D') |
| 812 | vim.command('delfunction D') |
| 813 | |
| 814 | def subexpr_test(expr, name, subexprs): |
| 815 | cb.append('>>> Testing %s using %s' % (name, expr)) |
| 816 | for subexpr in subexprs: |
| 817 | ee(expr % subexpr) |
| 818 | cb.append('<<< Finished') |
| 819 | |
| 820 | def stringtochars_test(expr): |
| 821 | return subexpr_test(expr, 'StringToChars', ( |
| 822 | '1', # Fail type checks |
| 823 | 'u"\\0"', # Fail PyString_AsStringAndSize(bytes, , NULL) check |
| 824 | '"\\0"', # Fail PyString_AsStringAndSize(object, , NULL) check |
| 825 | )) |
| 826 | |
| 827 | class Mapping(object): |
| 828 | def __init__(self, d): |
| 829 | self.d = d |
| 830 | |
| 831 | def __getitem__(self, key): |
| 832 | return self.d[key] |
| 833 | |
| 834 | def keys(self): |
| 835 | return self.d.keys() |
| 836 | |
| 837 | def items(self): |
| 838 | return self.d.items() |
| 839 | |
| 840 | def convertfrompyobject_test(expr, recurse=True): |
| 841 | # pydict_to_tv |
| 842 | stringtochars_test(expr % '{%s : 1}') |
| 843 | if recurse: |
| 844 | convertfrompyobject_test(expr % '{"abc" : %s}', False) |
| 845 | # pymap_to_tv |
| 846 | stringtochars_test(expr % 'Mapping({%s : 1})') |
| 847 | if recurse: |
| 848 | convertfrompyobject_test(expr % 'Mapping({"abc" : %s})', False) |
| 849 | # pyseq_to_tv |
| 850 | iter_test(expr) |
| 851 | return subexpr_test(expr, 'ConvertFromPyObject', ( |
| 852 | 'None', # Not conversible |
| 853 | '{"": 1}', # Empty key not allowed |
| 854 | 'FailingMapping()', # |
| 855 | 'FailingMappingKey()', # |
| 856 | )) |
| 857 | |
| 858 | def convertfrompymapping_test(expr): |
| 859 | convertfrompyobject_test(expr) |
| 860 | return subexpr_test(expr, 'ConvertFromPyMapping', ( |
| 861 | '[]', |
| 862 | )) |
| 863 | |
| 864 | def iter_test(expr): |
| 865 | return subexpr_test(expr, '*Iter*', ( |
| 866 | 'FailingIter()', |
| 867 | 'FailingIterNext()', |
| 868 | )) |
| 869 | |
| 870 | class FailingTrue(object): |
| 871 | def __nonzero__(self): |
| 872 | raise NotImplementedError |
| 873 | |
| 874 | class FailingIter(object): |
| 875 | def __iter__(self): |
| 876 | raise NotImplementedError |
| 877 | |
| 878 | class FailingIterNext(object): |
| 879 | def __iter__(self): |
| 880 | return self |
| 881 | |
| 882 | def next(self): |
| 883 | raise NotImplementedError |
| 884 | |
| 885 | class FailingMappingKey(object): |
| 886 | def __getitem__(self, item): |
| 887 | raise NotImplementedError |
| 888 | |
| 889 | def keys(self): |
| 890 | return list("abc") |
| 891 | |
| 892 | class FailingMapping(object): |
| 893 | def __getitem__(self): |
| 894 | raise NotImplementedError |
| 895 | |
| 896 | def keys(self): |
| 897 | raise NotImplementedError |
| 898 | |
| 899 | class FailingList(list): |
| 900 | def __getitem__(self, idx): |
| 901 | if i == 2: |
| 902 | raise NotImplementedError |
| 903 | else: |
| 904 | return super(FailingList, self).__getitem__(idx) |
| 905 | |
| 906 | cb.append("> Output") |
| 907 | cb.append(">> OutputSetattr") |
| 908 | ee('del sys.stdout.softspace') |
| 909 | ee('sys.stdout.softspace = []') |
| 910 | ee('sys.stdout.attr = None') |
| 911 | cb.append(">> OutputWrite") |
| 912 | ee('sys.stdout.write(None)') |
| 913 | cb.append(">> OutputWriteLines") |
| 914 | ee('sys.stdout.writelines(None)') |
| 915 | ee('sys.stdout.writelines([1])') |
| 916 | iter_test('sys.stdout.writelines(%s)') |
| 917 | cb.append("> VimCommand") |
| 918 | ee('vim.command(1)') |
| 919 | #! Not checked: vim->python exceptions translating: checked later |
| 920 | cb.append("> VimToPython") |
| 921 | #! Not checked: everything: needs errors in internal python functions |
| 922 | cb.append("> VimEval") |
| 923 | ee('vim.eval(1)') |
| 924 | #! Not checked: everything: needs errors in internal python functions |
| 925 | cb.append("> VimEvalPy") |
| 926 | ee('vim.bindeval(1)') |
| 927 | #! Not checked: vim->python exceptions translating: checked later |
| 928 | cb.append("> VimStrwidth") |
| 929 | ee('vim.strwidth(1)') |
| 930 | cb.append("> Dictionary") |
| 931 | cb.append(">> DictionaryConstructor") |
| 932 | ee('vim.Dictionary("abc")') |
| 933 | ##! Not checked: py_dict_alloc failure |
| 934 | cb.append(">> DictionarySetattr") |
| 935 | ee('del d.locked') |
| 936 | ee('d.locked = FailingTrue()') |
| 937 | ee('vim.vvars.locked = False') |
| 938 | ee('d.scope = True') |
| 939 | ee('d.xxx = True') |
| 940 | cb.append(">> _DictionaryItem") |
| 941 | ee('d.get("a", 2, 3)') |
| 942 | stringtochars_test('d.get(%s)') |
| 943 | ee('d.pop("a")') |
| 944 | ee('dl.pop("a")') |
| 945 | cb.append(">> DictionaryIterNext") |
| 946 | ee('for i in ned: ned["a"] = 1') |
| 947 | cb.append(">> DictionaryAssItem") |
| 948 | ee('dl["b"] = 1') |
| 949 | stringtochars_test('d[%s] = 1') |
| 950 | convertfrompyobject_test('d["a"] = %s') |
| 951 | cb.append(">> DictionaryUpdate") |
| 952 | cb.append(">>> kwargs") |
| 953 | cb.append(">>> iter") |
| 954 | ee('d.update(FailingMapping())') |
| 955 | ee('d.update([FailingIterNext()])') |
| 956 | iter_test('d.update(%s)') |
| 957 | convertfrompyobject_test('d.update(%s)') |
| 958 | stringtochars_test('d.update(((%s, 0),))') |
| 959 | convertfrompyobject_test('d.update((("a", %s),))') |
| 960 | cb.append(">> DictionaryPopItem") |
| 961 | ee('d.popitem(1, 2)') |
| 962 | cb.append(">> DictionaryHasKey") |
| 963 | ee('d.has_key()') |
| 964 | cb.append("> List") |
| 965 | cb.append(">> ListConstructor") |
| 966 | ee('vim.List(1, 2)') |
| 967 | ee('vim.List(a=1)') |
| 968 | iter_test('vim.List(%s)') |
| 969 | convertfrompyobject_test('vim.List([%s])') |
| 970 | cb.append(">> ListItem") |
| 971 | ee('l[1000]') |
| 972 | cb.append(">> ListAssItem") |
| 973 | ee('ll[1] = 2') |
| 974 | ee('l[1000] = 3') |
| 975 | cb.append(">> ListAssSlice") |
| 976 | ee('ll[1:100] = "abc"') |
| 977 | iter_test('l[:] = %s') |
| 978 | convertfrompyobject_test('l[:] = [%s]') |
| 979 | cb.append(">> ListConcatInPlace") |
| 980 | iter_test('l.extend(%s)') |
| 981 | convertfrompyobject_test('l.extend([%s])') |
| 982 | cb.append(">> ListSetattr") |
| 983 | ee('del l.locked') |
| 984 | ee('l.locked = FailingTrue()') |
| 985 | ee('l.xxx = True') |
| 986 | cb.append("> Function") |
| 987 | cb.append(">> FunctionConstructor") |
| 988 | ee('vim.Function("123")') |
| 989 | ee('vim.Function("xxx_non_existent_function_xxx")') |
| 990 | ee('vim.Function("xxx#non#existent#function#xxx")') |
| 991 | cb.append(">> FunctionCall") |
| 992 | convertfrompyobject_test('f(%s)') |
| 993 | convertfrompymapping_test('fd(self=%s)') |
| 994 | cb.append("> TabPage") |
| 995 | cb.append(">> TabPageAttr") |
| 996 | ee('vim.current.tabpage.xxx') |
| 997 | cb.append("> TabList") |
| 998 | cb.append(">> TabListItem") |
| 999 | ee('vim.tabpages[1000]') |
| 1000 | cb.append("> Window") |
| 1001 | cb.append(">> WindowAttr") |
| 1002 | ee('vim.current.window.xxx') |
| 1003 | cb.append(">> WindowSetattr") |
| 1004 | ee('vim.current.window.buffer = 0') |
| 1005 | ee('vim.current.window.cursor = (10000000000, 100000000)') |
| 1006 | ee('vim.current.window.cursor = True') |
| 1007 | ee('vim.current.window.height = "abc"') |
| 1008 | ee('vim.current.window.width = "abc"') |
| 1009 | ee('vim.current.window.xxxxxx = True') |
| 1010 | cb.append("> WinList") |
| 1011 | cb.append(">> WinListItem") |
| 1012 | ee('vim.windows[1000]') |
| 1013 | cb.append("> Buffer") |
| 1014 | cb.append(">> StringToLine (indirect)") |
| 1015 | ee('vim.current.buffer[0] = "\\na"') |
| 1016 | cb.append(">> SetBufferLine (indirect)") |
| 1017 | ee('vim.current.buffer[0] = True') |
| 1018 | cb.append(">> SetBufferLines (indirect)") |
| 1019 | ee('vim.current.buffer[:] = True') |
| 1020 | ee('vim.current.buffer[:] = ["\\na", "bc"]') |
| 1021 | cb.append(">> InsertBufferLines (indirect)") |
| 1022 | ee('vim.current.buffer.append(None)') |
| 1023 | ee('vim.current.buffer.append(["\\na", "bc"])') |
| 1024 | ee('vim.current.buffer.append("\\nbc")') |
| 1025 | cb.append(">> RBItem") |
| 1026 | ee('vim.current.buffer[10000000000]') |
| 1027 | cb.append(">> RBAsItem") |
| 1028 | ee('vim.current.buffer[10000000000] = ""') |
| 1029 | cb.append(">> BufferAttr") |
| 1030 | ee('vim.current.buffer.xxx') |
| 1031 | cb.append(">> BufferSetattr") |
| 1032 | ee('vim.current.buffer.name = True') |
| 1033 | ee('vim.current.buffer.xxx = True') |
| 1034 | cb.append(">> BufferMark") |
| 1035 | ee('vim.current.buffer.mark(0)') |
| 1036 | ee('vim.current.buffer.mark("abc")') |
| 1037 | ee('vim.current.buffer.mark("!")') |
| 1038 | cb.append(">> BufferRange") |
| 1039 | ee('vim.current.buffer.range(1, 2, 3)') |
| 1040 | cb.append("> BufMap") |
| 1041 | cb.append(">> BufMapItem") |
| 1042 | ee('vim.buffers[None]') |
| 1043 | ee('vim.buffers[100000000]') |
| 1044 | cb.append("> Current") |
| 1045 | cb.append(">> CurrentGetattr") |
| 1046 | ee('vim.current.xxx') |
| 1047 | cb.append(">> CurrentSetattr") |
| 1048 | ee('vim.current.line = True') |
| 1049 | ee('vim.current.buffer = True') |
| 1050 | ee('vim.current.window = True') |
| 1051 | ee('vim.current.tabpage = True') |
| 1052 | ee('vim.current.xxx = True') |
| 1053 | EOF |
| 1054 | :" |
| 1055 | :" Test exceptions |
| 1056 | :fun Exe(e) |
| 1057 | : execute a:e |
| 1058 | :endfun |
| 1059 | py << EOF |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 1060 | Exe = vim.bindeval('function("Exe")') |
| 1061 | ee('vim.command("throw \'abc\'")') |
| 1062 | ee('Exe("throw \'def\'")') |
| 1063 | ee('vim.eval("Exe(\'throw \'\'ghi\'\'\')")') |
| 1064 | ee('vim.eval("Exe(\'echoerr \'\'jkl\'\'\')")') |
| 1065 | ee('vim.eval("Exe(\'xxx_non_existent_command_xxx\')")') |
| 1066 | ee('vim.bindeval("Exe(\'xxx_non_existent_command_xxx\')")') |
| 1067 | EOF |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1068 | :endfun |
| 1069 | :" |
| 1070 | :call Test() |
| 1071 | :" |
| 1072 | :delfunc Test |
| 1073 | :call garbagecollect(1) |
| 1074 | :" |
| 1075 | :/^start:/,$wq! test.out |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1076 | :call getchar() |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1077 | ENDTEST |
| 1078 | |
| 1079 | start: |