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