blob: 9abc17b1e38d6ddeafb76df650d9740e25f1eb72 [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001Tests for various python features. vim: set ft=vim :
2
3STARTTEST
4:so small.vim
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005:set noswapfile
Bram Moolenaardb913952012-06-29 12:54:53 +02006:if !has('python3') | e! test.ok | wq! test.out | endif
Bram Moolenaarc24c1ac2013-05-16 20:47:56 +02007:lang C
Bram Moolenaardb913952012-06-29 12:54:53 +02008:fun Test()
Bram Moolenaar841fbd22013-06-23 14:37:07 +02009:py3 import vim
Bram Moolenaardb913952012-06-29 12:54:53 +020010:let l = []
11:py3 l=vim.bindeval('l')
12:py3 f=vim.bindeval('function("strlen")')
13:" Extending List directly with different types
14:py3 l+=[1, "as'd", [1, 2, f, {'a': 1}]]
15:$put =string(l)
16:$put =string(l[-1])
17:try
18: $put =string(l[-4])
19:catch
20: $put =v:exception[:13]
21:endtry
22:" List assignment
23:py3 l[0]=0
24:$put =string(l)
25:py3 l[-2]=f
26:$put =string(l)
27:"
28:" Extending Dictionary directly with different types
29:let d = {}
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020030:fun d.f()
31: return 1
32:endfun
Bram Moolenaara9922d62013-05-30 13:01:18 +020033py3 << EOF
34d=vim.bindeval('d')
35d['1']='asd'
36d.update(b=[1, 2, f])
37d.update((('-1', {'a': 1}),))
38d.update({'0': -1})
39dk = d.keys()
40dv = d.values()
41di = d.items()
42dk.sort(key=repr)
43dv.sort(key=repr)
44di.sort(key=repr)
45EOF
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020046:$put =py3eval('d[''f''](self={})')
Bram Moolenaara9922d62013-05-30 13:01:18 +020047:$put =py3eval('repr(dk)')
48:$put =substitute(py3eval('repr(dv)'),'0x\x\+','','g')
49:$put =substitute(py3eval('repr(di)'),'0x\x\+','','g')
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020050:for [key, Val] in sort(items(d))
51: $put =string(key) . ' : ' . string(Val)
52: unlet key Val
Bram Moolenaardb913952012-06-29 12:54:53 +020053:endfor
Bram Moolenaar841fbd22013-06-23 14:37:07 +020054:py3 del dk
55:py3 del di
56:py3 del dv
Bram Moolenaardb913952012-06-29 12:54:53 +020057:"
58:" removing items with del
59:py3 del l[2]
60:$put =string(l)
61:let l = range(8)
62:py3 l=vim.bindeval('l')
63:try
64: py3 del l[:3]
65: py3 del l[1:]
66:catch
67: $put =v:exception
68:endtry
69:$put =string(l)
70:"
71:py3 del d['-1']
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020072:py3 del d['f']
Bram Moolenaara9922d62013-05-30 13:01:18 +020073:$put =string(py3eval('d.get(''b'', 1)'))
74:$put =string(py3eval('d.pop(''b'')'))
75:$put =string(py3eval('d.get(''b'', 1)'))
76:$put =string(py3eval('d.pop(''1'', 2)'))
77:$put =string(py3eval('d.pop(''1'', 2)'))
78:$put =py3eval('repr(d.has_key(''0''))')
79:$put =py3eval('repr(d.has_key(''1''))')
80:$put =py3eval('repr(''0'' in d)')
81:$put =py3eval('repr(''1'' in d)')
82:$put =py3eval('repr(list(iter(d)))')
Bram Moolenaardb913952012-06-29 12:54:53 +020083:$put =string(d)
Bram Moolenaarde71b562013-06-02 17:41:54 +020084:$put =py3eval('repr(d.popitem())')
Bram Moolenaara9922d62013-05-30 13:01:18 +020085:$put =py3eval('repr(d.get(''0''))')
86:$put =py3eval('repr(list(iter(d)))')
Bram Moolenaardb913952012-06-29 12:54:53 +020087:"
88:" removing items out of range: silently skip items that don't exist
89:let l = [0, 1, 2, 3]
90:py3 l=vim.bindeval('l')
91:" The following two ranges delete nothing as they match empty list:
92:py3 del l[2:1]
93:$put =string(l)
94:py3 del l[2:2]
95:$put =string(l)
96:py3 del l[2:3]
97:$put =string(l)
98:let l = [0, 1, 2, 3]
99:py3 l=vim.bindeval('l')
100:py3 del l[2:4]
101:$put =string(l)
102:let l = [0, 1, 2, 3]
103:py3 l=vim.bindeval('l')
104:py3 del l[2:5]
105:$put =string(l)
106:let l = [0, 1, 2, 3]
107:py3 l=vim.bindeval('l')
108:py3 del l[2:6]
109:$put =string(l)
110:let l = [0, 1, 2, 3]
111:py3 l=vim.bindeval('l')
112:" The following two ranges delete nothing as they match empty list:
113:py3 del l[-1:2]
114:$put =string(l)
115:py3 del l[-2:2]
116:$put =string(l)
117:py3 del l[-3:2]
118:$put =string(l)
119:let l = [0, 1, 2, 3]
120:py3 l=vim.bindeval('l')
121:py3 del l[-4:2]
122:$put =string(l)
123:let l = [0, 1, 2, 3]
124:py3 l=vim.bindeval('l')
125:py3 del l[-5:2]
126:$put =string(l)
127:let l = [0, 1, 2, 3]
128:py3 l=vim.bindeval('l')
129:py3 del l[-6:2]
130:$put =string(l)
131:"
132:" Slice assignment to a list
133:let l = [0, 1, 2, 3]
134:py3 l=vim.bindeval('l')
135:py3 l[0:0]=['a']
136:$put =string(l)
137:let l = [0, 1, 2, 3]
138:py3 l=vim.bindeval('l')
139:py3 l[1:2]=['b']
140:$put =string(l)
141:let l = [0, 1, 2, 3]
142:py3 l=vim.bindeval('l')
143:py3 l[2:4]=['c']
144:$put =string(l)
145:let l = [0, 1, 2, 3]
146:py3 l=vim.bindeval('l')
147:py3 l[4:4]=['d']
148:$put =string(l)
149:let l = [0, 1, 2, 3]
150:py3 l=vim.bindeval('l')
151:py3 l[-1:2]=['e']
152:$put =string(l)
153:let l = [0, 1, 2, 3]
154:py3 l=vim.bindeval('l')
155:py3 l[-10:2]=['f']
156:$put =string(l)
157:let l = [0, 1, 2, 3]
158:py3 l=vim.bindeval('l')
159:py3 l[2:-10]=['g']
160:$put =string(l)
161:let l = []
162:py3 l=vim.bindeval('l')
163:py3 l[0:0]=['h']
164:$put =string(l)
165:"
166:" Locked variables
167:let l = [0, 1, 2, 3]
168:py3 l=vim.bindeval('l')
169:lockvar! l
170:py3 l[2]='i'
171:$put =string(l)
172:unlockvar! l
173:"
174:" Function calls
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100175py3 << EOF
176import sys
177import re
178
179py33_type_error_pattern = re.compile('^__call__\(\) takes (\d+) positional argument but (\d+) were given$')
180
181def ee(expr, g=globals(), l=locals()):
182 cb = vim.current.buffer
183 try:
184 try:
185 exec(expr, g, l)
186 except Exception as e:
187 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."):
188 cb.append(expr + ':' + repr((e.__class__, AttributeError(str(e)[str(e).rfind(" '") + 2:-1]))))
189 elif sys.version_info >= (3, 3) and e.__class__ is ImportError and str(e).find('No module named \'') >= 0:
190 cb.append(expr + ':' + repr((e.__class__, ImportError(str(e).replace("'", '')))))
191 elif sys.version_info >= (3, 3) and e.__class__ is TypeError:
192 m = py33_type_error_pattern.search(str(e))
193 if m:
194 msg = '__call__() takes exactly {0} positional argument ({1} given)'.format(m.group(1), m.group(2))
195 cb.append(expr + ':' + repr((e.__class__, TypeError(msg))))
196 else:
197 cb.append(expr + ':' + repr((e.__class__, e)))
198 else:
199 cb.append(expr + ':' + repr((e.__class__, e)))
200 else:
201 cb.append(expr + ':NOT FAILED')
202 except Exception as e:
203 cb.append(expr + '::' + repr((e.__class__, e)))
204EOF
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200205:fun New(...)
206: return ['NewStart']+a:000+['NewEnd']
207:endfun
208:fun DictNew(...) dict
209: return ['DictNewStart']+a:000+['DictNewEnd', self]
210:endfun
Bram Moolenaardb913952012-06-29 12:54:53 +0200211:let l=[function('New'), function('DictNew')]
212:py3 l=vim.bindeval('l')
213:py3 l.extend(list(l[0](1, 2, 3)))
214:$put =string(l)
215:py3 l.extend(list(l[1](1, 2, 3, self={'a': 'b'})))
216:$put =string(l)
217:py3 l+=[l[0].name]
218:$put =string(l)
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100219:py3 ee('l[1](1, 2, 3)')
Bram Moolenaar355fd9b2013-05-30 13:14:13 +0200220:py3 f=l[0]
Bram Moolenaardb913952012-06-29 12:54:53 +0200221:delfunction New
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100222:py3 ee('f(1, 2, 3)')
Bram Moolenaardb913952012-06-29 12:54:53 +0200223:if has('float')
224: let l=[0.0]
225: py3 l=vim.bindeval('l')
226: py3 l.extend([0.0])
227: $put =string(l)
228:else
229: $put ='[0.0, 0.0]'
230:endif
Bram Moolenaarc11073c2012-09-05 19:17:42 +0200231:let messages=[]
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200232:delfunction DictNew
233py3 <<EOF
Bram Moolenaarc11073c2012-09-05 19:17:42 +0200234d=vim.bindeval('{}')
235m=vim.bindeval('messages')
Bram Moolenaara9922d62013-05-30 13:01:18 +0200236def em(expr, g=globals(), l=locals()):
237 try:
238 exec(expr, g, l)
239 except Exception as e:
240 m.extend([e.__class__.__name__])
Bram Moolenaarc11073c2012-09-05 19:17:42 +0200241
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200242em('d["abc1"]')
243em('d["abc1"]="\\0"')
244em('d["abc1"]=vim')
Bram Moolenaara9922d62013-05-30 13:01:18 +0200245em('d[""]=1')
246em('d["a\\0b"]=1')
247em('d[b"a\\0b"]=1')
Bram Moolenaarc11073c2012-09-05 19:17:42 +0200248
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200249em('d.pop("abc1")')
Bram Moolenaarde71b562013-06-02 17:41:54 +0200250em('d.popitem()')
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200251del em
252del m
Bram Moolenaarc11073c2012-09-05 19:17:42 +0200253EOF
254:$put =messages
Bram Moolenaar66b79852012-09-21 14:00:35 +0200255:unlet messages
256:" locked and scope attributes
257:let d={} | let dl={} | lockvar dl
258:for s in split("d dl v: g:")
259: let name=tr(s, ':', 's')
260: execute 'py3 '.name.'=vim.bindeval("'.s.'")'
261: let toput=s.' : '.join(map(['locked', 'scope'], 'v:val.":".py3eval(name.".".v:val)'), ';')
262: $put =toput
263:endfor
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200264:silent! let d.abc2=1
265:silent! let dl.abc3=1
Bram Moolenaar66b79852012-09-21 14:00:35 +0200266:py3 d.locked=True
267:py3 dl.locked=False
268:silent! let d.def=1
269:silent! let dl.def=1
270:put ='d:'.string(d)
271:put ='dl:'.string(dl)
272:unlet d dl
273:
274:let l=[] | let ll=[] | lockvar ll
275:for s in split("l ll")
276: let name=tr(s, ':', 's')
277: execute 'py3 '.name.'=vim.bindeval("'.s.'")'
278: let toput=s.' : locked:'.py3eval(name.'.locked')
279: $put =toput
280:endfor
281:silent! call extend(l, [0])
282:silent! call extend(ll, [0])
283:py3 l.locked=True
284:py3 ll.locked=False
285:silent! call extend(l, [1])
286:silent! call extend(ll, [1])
287:put ='l:'.string(l)
288:put ='ll:'.string(ll)
289:unlet l ll
Bram Moolenaardb913952012-06-29 12:54:53 +0200290:"
291:" py3eval()
292:let l=py3eval('[0, 1, 2]')
293:$put =string(l)
294:let d=py3eval('{"a": "b", "c": 1, "d": ["e"]}')
295:$put =sort(items(d))
Bram Moolenaardb913952012-06-29 12:54:53 +0200296:if has('float')
297: let f=py3eval('0.0')
298: $put =string(f)
299:else
300: $put ='0.0'
301:endif
Bram Moolenaarc11073c2012-09-05 19:17:42 +0200302:" Invalid values:
303:for e in ['"\0"', '{"\0": 1}', 'undefined_name', 'vim']
304: try
305: let v=py3eval(e)
306: catch
307: let toput=e.":\t".v:exception[:13]
308: $put =toput
309: endtry
310:endfor
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100311:"
312:" threading
313:let l = [0]
314:py3 l=vim.bindeval('l')
Bram Moolenaardee2e312013-06-23 16:35:47 +0200315py3 <<EOF
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100316import threading
317import time
318
319class T(threading.Thread):
320 def __init__(self):
321 threading.Thread.__init__(self)
322 self.t = 0
323 self.running = True
324
325 def run(self):
326 while self.running:
327 self.t += 1
328 time.sleep(0.1)
329
330t = T()
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200331del T
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100332t.start()
333EOF
334:sleep 1
335:py3 t.running = False
336:py3 t.join()
337:py3 l[0] = t.t > 8 # check if the background thread is working
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200338:py3 del time
339:py3 del threading
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100340:py3 del t
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100341:$put =string(l)
342:"
343:" settrace
344:let l = []
345:py3 l=vim.bindeval('l')
Bram Moolenaardee2e312013-06-23 16:35:47 +0200346py3 <<EOF
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100347import sys
348
349def traceit(frame, event, arg):
350 global l
351 if event == "line":
352 l += [frame.f_lineno]
353 return traceit
354
355def trace_main():
356 for i in range(5):
357 pass
358EOF
359:py3 sys.settrace(traceit)
360:py3 trace_main()
Bram Moolenaardee2e312013-06-23 16:35:47 +0200361:py3 sys.settrace(None)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200362:py3 del traceit
363:py3 del trace_main
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100364:$put =string(l)
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200365:"
366:" Vars
367:let g:foo = 'bac'
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200368:let w:abc3 = 'def'
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200369:let b:baz = 'bar'
Bram Moolenaara4720012013-05-15 16:27:37 +0200370:let t:bar = 'jkl'
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200371:try
372: throw "Abc"
373:catch
374: put =py3eval('vim.vvars[''exception'']')
375:endtry
376:put =py3eval('vim.vars[''foo'']')
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200377:put =py3eval('vim.current.window.vars[''abc3'']')
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200378:put =py3eval('vim.current.buffer.vars[''baz'']')
Bram Moolenaara4720012013-05-15 16:27:37 +0200379:put =py3eval('vim.current.tabpage.vars[''bar'']')
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200380:"
381:" Options
382:" paste: boolean, global
383:" previewheight number, global
384:" operatorfunc: string, global
385:" number: boolean, window-local
386:" numberwidth: number, window-local
387:" colorcolumn: string, window-local
388:" statusline: string, window-local/global
389:" autoindent: boolean, buffer-local
Bram Moolenaar55b8ad32013-05-17 13:38:04 +0200390:" shiftwidth: number, buffer-local
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200391:" omnifunc: string, buffer-local
392:" preserveindent: boolean, buffer-local/global
393:" path: string, buffer-local/global
394:let g:bufs=[bufnr('%')]
395:new
396:let g:bufs+=[bufnr('%')]
397:vnew
398:let g:bufs+=[bufnr('%')]
399:wincmd j
400:vnew
401:let g:bufs+=[bufnr('%')]
402:wincmd l
403:fun RecVars(opt)
404: let gval =string(eval('&g:'.a:opt))
405: let wvals=join(map(range(1, 4), 'v:val.":".string(getwinvar(v:val, "&".a:opt))'))
406: let bvals=join(map(copy(g:bufs), 'v:val.":".string(getbufvar(v:val, "&".a:opt))'))
407: put =' G: '.gval
408: put =' W: '.wvals
409: put =' B: '.wvals
410:endfun
411py3 << EOF
412def e(s, g=globals(), l=locals()):
413 try:
414 exec(s, g, l)
415 except Exception as e:
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200416 vim.command('return ' + repr(e.__class__.__name__))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200417
418def ev(s, g=globals(), l=locals()):
419 try:
420 return eval(s, g, l)
421 except Exception as e:
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200422 vim.command('let exc=' + repr(e.__class__.__name__))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200423 return 0
424EOF
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200425:fun E(s)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200426: python3 e(vim.eval('a:s'))
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200427:endfun
428:fun Ev(s)
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200429: let r=py3eval('ev(vim.eval("a:s"))')
430: if exists('exc')
431: throw exc
432: endif
433: return r
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200434:endfun
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200435:py3 gopts1=vim.options
436:py3 wopts1=vim.windows[2].options
437:py3 wopts2=vim.windows[0].options
438:py3 wopts3=vim.windows[1].options
439:py3 bopts1=vim.buffers[vim.bindeval("g:bufs")[2]].options
440:py3 bopts2=vim.buffers[vim.bindeval("g:bufs")[1]].options
441:py3 bopts3=vim.buffers[vim.bindeval("g:bufs")[0]].options
Bram Moolenaar04188112013-06-01 20:32:12 +0200442:set path=.,..,,
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200443:let lst=[]
444:let lst+=[['paste', 1, 0, 1, 2, 1, 1, 0 ]]
445:let lst+=[['previewheight', 5, 1, 6, 'a', 0, 1, 0 ]]
446:let lst+=[['operatorfunc', 'A', 'B', 'C', 2, 0, 1, 0 ]]
447:let lst+=[['number', 0, 1, 1, 0, 1, 0, 1 ]]
448:let lst+=[['numberwidth', 2, 3, 5, -100, 0, 0, 1 ]]
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200449:let lst+=[['colorcolumn', '+1', '+2', '+3', 'abc4', 0, 0, 1 ]]
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200450:let lst+=[['statusline', '1', '2', '4', 0, 0, 1, 1 ]]
451:let lst+=[['autoindent', 0, 1, 1, 2, 1, 0, 2 ]]
Bram Moolenaar55b8ad32013-05-17 13:38:04 +0200452:let lst+=[['shiftwidth', 0, 2, 1, 3, 0, 0, 2 ]]
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200453:let lst+=[['omnifunc', 'A', 'B', 'C', 1, 0, 0, 2 ]]
454:let lst+=[['preserveindent', 0, 1, 1, 2, 1, 1, 2 ]]
455:let lst+=[['path', '.,,', ',,', '.', 0, 0, 1, 2 ]]
456:for [oname, oval1, oval2, oval3, invval, bool, global, local] in lst
457: py3 oname=vim.eval('oname')
458: py3 oval1=vim.bindeval('oval1')
459: py3 oval2=vim.bindeval('oval2')
460: py3 oval3=vim.bindeval('oval3')
461: if invval is 0 || invval is 1
462: py3 invval=bool(vim.bindeval('invval'))
463: else
464: py3 invval=vim.bindeval('invval')
465: endif
466: if bool
467: py3 oval1=bool(oval1)
468: py3 oval2=bool(oval2)
469: py3 oval3=bool(oval3)
470: endif
471: put ='>>> '.oname
472: for v in ['gopts1', 'wopts1', 'bopts1']
473: try
474: put =' p/'.v.': '.Ev('repr('.v.'['''.oname.'''])')
475: catch
476: put =' p/'.v.'! '.v:exception
477: endtry
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200478: let r=E(v.'['''.oname.''']=invval')
479: if r isnot 0
480: put =' inv: '.string(invval).'! '.r
481: endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200482: for vv in (v is# 'gopts1' ? [v] : [v, v[:-2].'2', v[:-2].'3'])
483: let val=substitute(vv, '^.opts', 'oval', '')
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200484: let r=E(vv.'['''.oname.''']='.val)
485: if r isnot 0
486: put =' '.vv.'! '.r
487: endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200488: endfor
489: endfor
490: call RecVars(oname)
491: for v in ['wopts3', 'bopts3']
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200492: let r=E('del '.v.'["'.oname.'"]')
493: if r isnot 0
494: put =' del '.v.'! '.r
495: endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200496: endfor
497: call RecVars(oname)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200498:endfor
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200499:delfunction RecVars
500:delfunction E
501:delfunction Ev
502:py3 del ev
503:py3 del e
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +0200504:only
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200505:for buf in g:bufs[1:]
506: execute 'bwipeout!' buf
507:endfor
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200508:py3 del gopts1
509:py3 del wopts1
510:py3 del wopts2
511:py3 del wopts3
512:py3 del bopts1
513:py3 del bopts2
514:py3 del bopts3
515:py3 del oval1
516:py3 del oval2
517:py3 del oval3
518:py3 del oname
519:py3 del invval
Bram Moolenaarbd80f352013-05-12 21:16:23 +0200520:"
521:" Test buffer object
522:vnew
523:put ='First line'
524:put ='Second line'
525:put ='Third line'
526:1 delete _
527:py3 b=vim.current.buffer
528:wincmd w
529:mark a
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200530:augroup BUFS
531: autocmd BufFilePost * python3 cb.append(vim.eval('expand("<abuf>")') + ':BufFilePost:' + vim.eval('bufnr("%")'))
532: autocmd BufFilePre * python3 cb.append(vim.eval('expand("<abuf>")') + ':BufFilePre:' + vim.eval('bufnr("%")'))
533:augroup END
Bram Moolenaarbd80f352013-05-12 21:16:23 +0200534py3 << EOF
535cb = vim.current.buffer
536# Tests BufferAppend and BufferItem
537cb.append(b[0])
538# Tests BufferSlice and BufferAssSlice
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200539cb.append('abc5') # Will be overwritten
Bram Moolenaarbd80f352013-05-12 21:16:23 +0200540cb[-1:] = b[:-2]
541# Test BufferLength and BufferAssSlice
542cb.append('def') # Will not be overwritten
543cb[len(cb):] = b[:]
544# Test BufferAssItem and BufferMark
545cb.append('ghi') # Will be overwritten
546cb[-1] = repr((len(cb) - cb.mark('a')[0], cb.mark('a')[1]))
547# Test BufferRepr
548cb.append(repr(cb) + repr(b))
549# Modify foreign buffer
550b.append('foo')
551b[0]='bar'
552b[0:0]=['baz']
553vim.command('call append("$", getbufline(%i, 1, "$"))' % b.number)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200554# Test assigning to name property
Bram Moolenaar04188112013-06-01 20:32:12 +0200555import os
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200556old_name = cb.name
557cb.name = 'foo'
Bram Moolenaar04188112013-06-01 20:32:12 +0200558cb.append(cb.name[-11:].replace(os.path.sep, '/'))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200559b.name = 'bar'
Bram Moolenaar04188112013-06-01 20:32:12 +0200560cb.append(b.name[-11:].replace(os.path.sep, '/'))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200561cb.name = old_name
Bram Moolenaar04188112013-06-01 20:32:12 +0200562cb.append(cb.name[-17:].replace(os.path.sep, '/'))
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200563del old_name
Bram Moolenaarbd80f352013-05-12 21:16:23 +0200564# Test CheckBuffer
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200565for _b in vim.buffers:
566 if _b is not cb:
567 vim.command('bwipeout! ' + str(_b.number))
568del _b
Bram Moolenaar9e822c02013-05-29 22:15:30 +0200569cb.append('valid: b:%s, cb:%s' % (repr(b.valid), repr(cb.valid)))
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200570for expr in ('b[1]','b[:] = ["A", "B"]','b[:]','b.append("abc6")'):
Bram Moolenaarbd80f352013-05-12 21:16:23 +0200571 try:
572 exec(expr)
573 except vim.error:
574 pass
575 else:
576 # Usually a SEGV here
577 # Should not happen in any case
578 cb.append('No exception for ' + expr)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200579vim.command('cd .')
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200580del b
Bram Moolenaarbd80f352013-05-12 21:16:23 +0200581EOF
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200582:"
583:" Test vim.buffers object
584:set hidden
585:edit a
586:buffer #
587:edit b
588:buffer #
589:edit c
590:buffer #
591py3 << EOF
592# Check GCing iterator that was not fully exhausted
593i = iter(vim.buffers)
594cb.append('i:' + str(next(i)))
595# and also check creating more then one iterator at a time
596i2 = iter(vim.buffers)
597cb.append('i2:' + str(next(i2)))
598cb.append('i:' + str(next(i)))
599# The following should trigger GC and not cause any problems
600del i
601del i2
602i3 = iter(vim.buffers)
603cb.append('i3:' + str(next(i3)))
604del i3
605
606prevnum = 0
607for b in vim.buffers:
608 # Check buffer order
609 if prevnum >= b.number:
610 cb.append('!!! Buffer numbers not in strictly ascending order')
611 # Check indexing: vim.buffers[number].number == number
612 cb.append(str(b.number) + ':' + repr(vim.buffers[b.number]) + '=' + repr(b))
613 prevnum = b.number
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200614del prevnum
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200615
616cb.append(str(len(vim.buffers)))
617
618bnums = list(map(lambda b: b.number, vim.buffers))[1:]
619
620# Test wiping out buffer with existing iterator
621i4 = iter(vim.buffers)
622cb.append('i4:' + str(next(i4)))
623vim.command('bwipeout! ' + str(bnums.pop(0)))
624try:
625 next(i4)
626except vim.error:
627 pass
628else:
629 cb.append('!!!! No vim.error')
630i4 = iter(vim.buffers)
631vim.command('bwipeout! ' + str(bnums.pop(-1)))
632vim.command('bwipeout! ' + str(bnums.pop(-1)))
633cb.append('i4:' + str(next(i4)))
634try:
635 next(i4)
636except StopIteration:
637 cb.append('StopIteration')
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200638del i4
639del bnums
Bram Moolenaarb6c589a2013-05-15 14:39:52 +0200640EOF
Bram Moolenaara4720012013-05-15 16:27:37 +0200641:"
642:" Test vim.{tabpage,window}list and vim.{tabpage,window} objects
643:tabnew 0
644:tabnew 1
645:vnew a.1
646:tabnew 2
647:vnew a.2
648:vnew b.2
649:vnew c.2
650py3 << EOF
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200651cb.append('Number of tabs: ' + str(len(vim.tabpages)))
652cb.append('Current tab pages:')
653
Bram Moolenaara4720012013-05-15 16:27:37 +0200654def W(w):
655 if '(unknown)' in repr(w):
656 return '<window object (unknown)>'
657 else:
658 return repr(w)
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200659
660def Cursor(w, start=len(cb)):
661 if w.buffer is cb:
662 return repr((start - w.cursor[0], w.cursor[1]))
663 else:
664 return repr(w.cursor)
665
Bram Moolenaara4720012013-05-15 16:27:37 +0200666for t in vim.tabpages:
667 cb.append(' ' + repr(t) + '(' + str(t.number) + ')' + ': ' + str(len(t.windows)) + ' windows, current is ' + W(t.window))
668 cb.append(' Windows:')
669 for w in t.windows:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200670 cb.append(' ' + W(w) + '(' + str(w.number) + ')' + ': displays buffer ' + repr(w.buffer) + '; cursor is at ' + Cursor(w))
Bram Moolenaara4720012013-05-15 16:27:37 +0200671 # Other values depend on the size of the terminal, so they are checked partly:
672 for attr in ('height', 'row', 'width', 'col'):
673 try:
674 aval = getattr(w, attr)
675 if type(aval) is not int:
676 raise TypeError
677 if aval < 0:
678 raise ValueError
679 except Exception as e:
680 cb.append('!!!!!! Error while getting attribute ' + attr + ': ' + e.__class__.__name__)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200681 del aval
682 del attr
Bram Moolenaara4720012013-05-15 16:27:37 +0200683 w.cursor = (len(w.buffer), 0)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200684del W
685del Cursor
Bram Moolenaara4720012013-05-15 16:27:37 +0200686cb.append('Number of windows in current tab page: ' + str(len(vim.windows)))
687if list(vim.windows) != list(vim.current.tabpage.windows):
688 cb.append('!!!!!! Windows differ')
689EOF
690:"
691:" Test vim.current
692py3 << EOF
693def H(o):
694 return repr(o)
695cb.append('Current tab page: ' + repr(vim.current.tabpage))
696cb.append('Current window: ' + repr(vim.current.window) + ': ' + H(vim.current.window) + ' is ' + H(vim.current.tabpage.window))
697cb.append('Current buffer: ' + repr(vim.current.buffer) + ': ' + H(vim.current.buffer) + ' is ' + H(vim.current.window.buffer)+ ' is ' + H(vim.current.tabpage.window.buffer))
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200698del H
Bram Moolenaara4720012013-05-15 16:27:37 +0200699# Assigning: fails
700try:
701 vim.current.window = vim.tabpages[0].window
702except ValueError:
703 cb.append('ValueError at assigning foreign tab window')
704
705for attr in ('window', 'tabpage', 'buffer'):
706 try:
707 setattr(vim.current, attr, None)
708 except TypeError:
709 cb.append('Type error at assigning None to vim.current.' + attr)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200710del attr
Bram Moolenaara4720012013-05-15 16:27:37 +0200711
712# Assigning: success
713vim.current.tabpage = vim.tabpages[-2]
714vim.current.buffer = cb
715vim.current.window = vim.windows[0]
716vim.current.window.cursor = (len(vim.current.buffer), 0)
717cb.append('Current tab page: ' + repr(vim.current.tabpage))
718cb.append('Current window: ' + repr(vim.current.window))
719cb.append('Current buffer: ' + repr(vim.current.buffer))
720cb.append('Current line: ' + repr(vim.current.line))
Bram Moolenaar9e822c02013-05-29 22:15:30 +0200721ws = list(vim.windows)
722ts = list(vim.tabpages)
Bram Moolenaara4720012013-05-15 16:27:37 +0200723for b in vim.buffers:
724 if b is not cb:
725 vim.command('bwipeout! ' + str(b.number))
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200726del b
Bram Moolenaar9e822c02013-05-29 22:15:30 +0200727cb.append('w.valid: ' + repr([w.valid for w in ws]))
728cb.append('t.valid: ' + repr([t.valid for t in ts]))
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200729del w
730del t
731del ts
732del ws
Bram Moolenaara4720012013-05-15 16:27:37 +0200733EOF
734:tabonly!
735:only!
Bram Moolenaarcac867a2013-05-21 19:50:34 +0200736:"
737:" Test types
738py3 << EOF
739for expr, attr in (
740 ('vim.vars', 'Dictionary'),
741 ('vim.options', 'Options'),
742 ('vim.bindeval("{}")', 'Dictionary'),
743 ('vim.bindeval("[]")', 'List'),
744 ('vim.bindeval("function(\'tr\')")', 'Function'),
745 ('vim.current.buffer', 'Buffer'),
746 ('vim.current.range', 'Range'),
747 ('vim.current.window', 'Window'),
748 ('vim.current.tabpage', 'TabPage'),
749):
750 cb.append(expr + ':' + attr + ':' + repr(type(eval(expr)) is getattr(vim, attr)))
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200751del expr
752del attr
Bram Moolenaarcac867a2013-05-21 19:50:34 +0200753EOF
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200754:"
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200755:" Test __dir__() method
756py3 << EOF
757for name, o in (
758 ('current', vim.current),
759 ('buffer', vim.current.buffer),
760 ('window', vim.current.window),
761 ('tabpage', vim.current.tabpage),
762 ('range', vim.current.range),
763 ('dictionary', vim.bindeval('{}')),
764 ('list', vim.bindeval('[]')),
765 ('function', vim.bindeval('function("tr")')),
766 ('output', sys.stdout),
767 ):
768 cb.append(name + ':' + ','.join(dir(o)))
769del name
770del o
771EOF
772:"
Bram Moolenaar78cddbe2013-05-30 13:05:58 +0200773:" Test vim.*.__new__
Bram Moolenaara9922d62013-05-30 13:01:18 +0200774:$put =string(py3eval('vim.Dictionary({})'))
775:$put =string(py3eval('vim.Dictionary(a=1)'))
776:$put =string(py3eval('vim.Dictionary(((''a'', 1),))'))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +0200777:$put =string(py3eval('vim.List()'))
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200778:$put =string(py3eval('vim.List(iter(''abc7''))'))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +0200779:$put =string(py3eval('vim.Function(''tr'')'))
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200780:"
781:" Test stdout/stderr
782:redir => messages
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200783:py3 sys.stdout.write('abc8') ; sys.stdout.write('def')
784:py3 sys.stderr.write('abc9') ; sys.stderr.write('def')
785:py3 sys.stdout.writelines(iter('abcA'))
786:py3 sys.stderr.writelines(iter('abcB'))
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200787:redir END
788:$put =string(substitute(messages, '\d\+', '', 'g'))
Bram Moolenaara9922d62013-05-30 13:01:18 +0200789:" Test subclassing
Bram Moolenaar355fd9b2013-05-30 13:14:13 +0200790:fun Put(...)
791: $put =string(a:000)
792: return a:000
793:endfun
Bram Moolenaara9922d62013-05-30 13:01:18 +0200794py3 << EOF
795class DupDict(vim.Dictionary):
796 def __setitem__(self, key, value):
797 super(DupDict, self).__setitem__(key, value)
798 super(DupDict, self).__setitem__('dup_' + key, value)
799dd = DupDict()
800dd['a'] = 'b'
Bram Moolenaar78cddbe2013-05-30 13:05:58 +0200801
802class DupList(vim.List):
803 def __getitem__(self, idx):
804 return [super(DupList, self).__getitem__(idx)] * 2
805
806dl = DupList()
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200807dl2 = DupList(iter('abcC'))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +0200808dl.extend(dl2[0])
Bram Moolenaar355fd9b2013-05-30 13:14:13 +0200809
810class DupFun(vim.Function):
811 def __call__(self, arg):
812 return super(DupFun, self).__call__(arg, arg)
813
814df = DupFun('Put')
Bram Moolenaara9922d62013-05-30 13:01:18 +0200815EOF
816:$put =string(sort(keys(py3eval('dd'))))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +0200817:$put =string(py3eval('dl'))
818:$put =string(py3eval('dl2'))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +0200819:$put =string(py3eval('df(2)'))
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200820:$put =string(py3eval('dl') is# py3eval('dl'))
821:$put =string(py3eval('dd') is# py3eval('dd'))
822:$put =string(py3eval('df'))
823:delfunction Put
824py3 << EOF
825del DupDict
826del DupList
827del DupFun
828del dd
829del dl
830del dl2
831del df
832EOF
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200833:"
Bram Moolenaarf4258302013-06-02 18:20:17 +0200834:" Test chdir
835py3 << EOF
836import os
837fnamemodify = vim.Function('fnamemodify')
838cb.append(str(fnamemodify('.', ':p:h:t')))
839cb.append(vim.eval('@%'))
840os.chdir('..')
841cb.append(str(fnamemodify('.', ':p:h:t')))
842cb.append(vim.eval('@%').replace(os.path.sep, '/'))
843os.chdir('testdir')
844cb.append(str(fnamemodify('.', ':p:h:t')))
845cb.append(vim.eval('@%'))
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200846del fnamemodify
Bram Moolenaarf4258302013-06-02 18:20:17 +0200847EOF
848:"
Bram Moolenaar8600e402013-05-30 13:28:41 +0200849:" Test errors
850:fun F() dict
851:endfun
852:fun D()
853:endfun
854py3 << EOF
Bram Moolenaar8600e402013-05-30 13:28:41 +0200855d = vim.Dictionary()
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200856ned = vim.Dictionary(foo='bar', baz='abcD')
Bram Moolenaar8600e402013-05-30 13:28:41 +0200857dl = vim.Dictionary(a=1)
858dl.locked = True
859l = vim.List()
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200860ll = vim.List('abcE')
Bram Moolenaar8600e402013-05-30 13:28:41 +0200861ll.locked = True
862f = vim.Function('string')
863fd = vim.Function('F')
864fdel = vim.Function('D')
865vim.command('delfunction D')
866
867def subexpr_test(expr, name, subexprs):
868 cb.append('>>> Testing %s using %s' % (name, expr))
869 for subexpr in subexprs:
870 ee(expr % subexpr)
871 cb.append('<<< Finished')
872
873def stringtochars_test(expr):
874 return subexpr_test(expr, 'StringToChars', (
875 '1', # Fail type checks
Bram Moolenaar96c7dfd2013-05-31 18:46:11 +0200876 'b"\\0"', # Fail PyString_AsStringAndSize(object, , NULL) check
877 '"\\0"', # Fail PyString_AsStringAndSize(bytes, , NULL) check
Bram Moolenaar8600e402013-05-30 13:28:41 +0200878 ))
879
880class Mapping(object):
881 def __init__(self, d):
882 self.d = d
883
884 def __getitem__(self, key):
885 return self.d[key]
886
887 def keys(self):
888 return self.d.keys()
889
890 def items(self):
891 return self.d.items()
892
893def convertfrompyobject_test(expr, recurse=True):
894 # pydict_to_tv
895 stringtochars_test(expr % '{%s : 1}')
896 if recurse:
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200897 convertfrompyobject_test(expr % '{"abcF" : %s}', False)
Bram Moolenaar8600e402013-05-30 13:28:41 +0200898 # pymap_to_tv
899 stringtochars_test(expr % 'Mapping({%s : 1})')
900 if recurse:
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200901 convertfrompyobject_test(expr % 'Mapping({"abcG" : %s})', False)
Bram Moolenaar8600e402013-05-30 13:28:41 +0200902 # pyseq_to_tv
903 iter_test(expr)
904 return subexpr_test(expr, 'ConvertFromPyObject', (
905 'None', # Not conversible
Bram Moolenaar78b59572013-06-02 18:54:21 +0200906 '{b"": 1}', # Empty key not allowed
907 '{"": 1}', # Same, but with unicode object
Bram Moolenaar8600e402013-05-30 13:28:41 +0200908 'FailingMapping()', #
909 'FailingMappingKey()', #
Bram Moolenaardee2e312013-06-23 16:35:47 +0200910 'FailingNumber()', #
Bram Moolenaar8600e402013-05-30 13:28:41 +0200911 ))
912
913def convertfrompymapping_test(expr):
914 convertfrompyobject_test(expr)
915 return subexpr_test(expr, 'ConvertFromPyMapping', (
916 '[]',
917 ))
918
919def iter_test(expr):
920 return subexpr_test(expr, '*Iter*', (
921 'FailingIter()',
922 'FailingIterNext()',
923 ))
924
Bram Moolenaardee2e312013-06-23 16:35:47 +0200925def number_test(expr, natural=False, unsigned=False):
926 if natural:
927 unsigned = True
928 return subexpr_test(expr, 'NumberToLong', (
929 '[]',
930 'None',
931 ) + (('-1',) if unsigned else ())
932 + (('0',) if natural else ()))
933
Bram Moolenaar8600e402013-05-30 13:28:41 +0200934class FailingTrue(object):
935 def __bool__(self):
Bram Moolenaardee2e312013-06-23 16:35:47 +0200936 raise NotImplementedError('bool')
Bram Moolenaar8600e402013-05-30 13:28:41 +0200937
938class FailingIter(object):
939 def __iter__(self):
Bram Moolenaardee2e312013-06-23 16:35:47 +0200940 raise NotImplementedError('iter')
Bram Moolenaar8600e402013-05-30 13:28:41 +0200941
942class FailingIterNext(object):
943 def __iter__(self):
944 return self
945
946 def __next__(self):
Bram Moolenaardee2e312013-06-23 16:35:47 +0200947 raise NotImplementedError('next')
Bram Moolenaar8600e402013-05-30 13:28:41 +0200948
949class FailingMappingKey(object):
950 def __getitem__(self, item):
Bram Moolenaardee2e312013-06-23 16:35:47 +0200951 raise NotImplementedError('getitem:mappingkey')
Bram Moolenaar8600e402013-05-30 13:28:41 +0200952
953 def keys(self):
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200954 return list("abcH")
Bram Moolenaar8600e402013-05-30 13:28:41 +0200955
956class FailingMapping(object):
957 def __getitem__(self):
Bram Moolenaardee2e312013-06-23 16:35:47 +0200958 raise NotImplementedError('getitem:mapping')
Bram Moolenaar8600e402013-05-30 13:28:41 +0200959
960 def keys(self):
Bram Moolenaardee2e312013-06-23 16:35:47 +0200961 raise NotImplementedError('keys')
Bram Moolenaar8600e402013-05-30 13:28:41 +0200962
963class FailingList(list):
964 def __getitem__(self, idx):
965 if i == 2:
Bram Moolenaardee2e312013-06-23 16:35:47 +0200966 raise NotImplementedError('getitem:list')
Bram Moolenaar8600e402013-05-30 13:28:41 +0200967 else:
968 return super(FailingList, self).__getitem__(idx)
969
Bram Moolenaardee2e312013-06-23 16:35:47 +0200970class NoArgsCall(object):
971 def __call__(self):
972 pass
973
974class FailingCall(object):
975 def __call__(self, path):
976 raise NotImplementedError('call')
977
978class FailingNumber(object):
979 def __int__(self):
980 raise NotImplementedError('int')
981
Bram Moolenaar8600e402013-05-30 13:28:41 +0200982cb.append("> Output")
983cb.append(">> OutputSetattr")
984ee('del sys.stdout.softspace')
Bram Moolenaardee2e312013-06-23 16:35:47 +0200985number_test('sys.stdout.softspace = %s', unsigned=True)
986number_test('sys.stderr.softspace = %s', unsigned=True)
Bram Moolenaar8600e402013-05-30 13:28:41 +0200987ee('sys.stdout.attr = None')
988cb.append(">> OutputWrite")
989ee('sys.stdout.write(None)')
990cb.append(">> OutputWriteLines")
991ee('sys.stdout.writelines(None)')
992ee('sys.stdout.writelines([1])')
993iter_test('sys.stdout.writelines(%s)')
994cb.append("> VimCommand")
Bram Moolenaardee2e312013-06-23 16:35:47 +0200995stringtochars_test('vim.command(%s)')
996ee('vim.command("", 2)')
Bram Moolenaar8600e402013-05-30 13:28:41 +0200997#! Not checked: vim->python exceptions translating: checked later
998cb.append("> VimToPython")
999#! Not checked: everything: needs errors in internal python functions
1000cb.append("> VimEval")
Bram Moolenaardee2e312013-06-23 16:35:47 +02001001stringtochars_test('vim.eval(%s)')
1002ee('vim.eval("", FailingTrue())')
Bram Moolenaar8600e402013-05-30 13:28:41 +02001003#! Not checked: everything: needs errors in internal python functions
1004cb.append("> VimEvalPy")
Bram Moolenaardee2e312013-06-23 16:35:47 +02001005stringtochars_test('vim.bindeval(%s)')
1006ee('vim.eval("", 2)')
Bram Moolenaar8600e402013-05-30 13:28:41 +02001007#! Not checked: vim->python exceptions translating: checked later
1008cb.append("> VimStrwidth")
Bram Moolenaardee2e312013-06-23 16:35:47 +02001009stringtochars_test('vim.strwidth(%s)')
1010cb.append("> VimForeachRTP")
1011ee('vim.foreach_rtp(None)')
1012ee('vim.foreach_rtp(NoArgsCall())')
1013ee('vim.foreach_rtp(FailingCall())')
1014ee('vim.foreach_rtp(int, 2)')
1015cb.append('> import')
1016old_rtp = vim.options['rtp']
1017vim.options['rtp'] = os.getcwd().replace(',', '\\,').replace('\\', '\\\\')
1018ee('import xxx_no_such_module_xxx')
1019ee('import failing_import')
1020ee('import failing')
1021vim.options['rtp'] = old_rtp
1022del old_rtp
Bram Moolenaar8600e402013-05-30 13:28:41 +02001023cb.append("> Dictionary")
1024cb.append(">> DictionaryConstructor")
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001025ee('vim.Dictionary("abcI")')
Bram Moolenaar8600e402013-05-30 13:28:41 +02001026##! Not checked: py_dict_alloc failure
1027cb.append(">> DictionarySetattr")
1028ee('del d.locked')
1029ee('d.locked = FailingTrue()')
1030ee('vim.vvars.locked = False')
1031ee('d.scope = True')
1032ee('d.xxx = True')
1033cb.append(">> _DictionaryItem")
1034ee('d.get("a", 2, 3)')
1035stringtochars_test('d.get(%s)')
1036ee('d.pop("a")')
1037ee('dl.pop("a")')
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001038cb.append(">> DictionaryContains")
1039ee('"" in d')
1040ee('0 in d')
Bram Moolenaar8600e402013-05-30 13:28:41 +02001041cb.append(">> DictionaryIterNext")
1042ee('for i in ned: ned["a"] = 1')
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001043del i
Bram Moolenaar8600e402013-05-30 13:28:41 +02001044cb.append(">> DictionaryAssItem")
1045ee('dl["b"] = 1')
1046stringtochars_test('d[%s] = 1')
1047convertfrompyobject_test('d["a"] = %s')
1048cb.append(">> DictionaryUpdate")
1049cb.append(">>> kwargs")
1050cb.append(">>> iter")
1051ee('d.update(FailingMapping())')
1052ee('d.update([FailingIterNext()])')
1053iter_test('d.update(%s)')
1054convertfrompyobject_test('d.update(%s)')
1055stringtochars_test('d.update(((%s, 0),))')
1056convertfrompyobject_test('d.update((("a", %s),))')
1057cb.append(">> DictionaryPopItem")
1058ee('d.popitem(1, 2)')
1059cb.append(">> DictionaryHasKey")
1060ee('d.has_key()')
1061cb.append("> List")
1062cb.append(">> ListConstructor")
1063ee('vim.List(1, 2)')
1064ee('vim.List(a=1)')
1065iter_test('vim.List(%s)')
1066convertfrompyobject_test('vim.List([%s])')
1067cb.append(">> ListItem")
1068ee('l[1000]')
1069cb.append(">> ListAssItem")
1070ee('ll[1] = 2')
1071ee('l[1000] = 3')
1072cb.append(">> ListAssSlice")
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001073ee('ll[1:100] = "abcJ"')
Bram Moolenaar8600e402013-05-30 13:28:41 +02001074iter_test('l[:] = %s')
1075convertfrompyobject_test('l[:] = [%s]')
1076cb.append(">> ListConcatInPlace")
1077iter_test('l.extend(%s)')
1078convertfrompyobject_test('l.extend([%s])')
1079cb.append(">> ListSetattr")
1080ee('del l.locked')
1081ee('l.locked = FailingTrue()')
1082ee('l.xxx = True')
1083cb.append("> Function")
1084cb.append(">> FunctionConstructor")
1085ee('vim.Function("123")')
1086ee('vim.Function("xxx_non_existent_function_xxx")')
1087ee('vim.Function("xxx#non#existent#function#xxx")')
1088cb.append(">> FunctionCall")
1089convertfrompyobject_test('f(%s)')
1090convertfrompymapping_test('fd(self=%s)')
1091cb.append("> TabPage")
1092cb.append(">> TabPageAttr")
1093ee('vim.current.tabpage.xxx')
1094cb.append("> TabList")
1095cb.append(">> TabListItem")
1096ee('vim.tabpages[1000]')
1097cb.append("> Window")
1098cb.append(">> WindowAttr")
1099ee('vim.current.window.xxx')
1100cb.append(">> WindowSetattr")
1101ee('vim.current.window.buffer = 0')
Bram Moolenaar96c7dfd2013-05-31 18:46:11 +02001102ee('vim.current.window.cursor = (100000000, 100000000)')
Bram Moolenaar8600e402013-05-30 13:28:41 +02001103ee('vim.current.window.cursor = True')
Bram Moolenaardee2e312013-06-23 16:35:47 +02001104number_test('vim.current.window.height = %s', unsigned=True)
1105number_test('vim.current.window.width = %s', unsigned=True)
Bram Moolenaar8600e402013-05-30 13:28:41 +02001106ee('vim.current.window.xxxxxx = True')
1107cb.append("> WinList")
1108cb.append(">> WinListItem")
1109ee('vim.windows[1000]')
1110cb.append("> Buffer")
1111cb.append(">> StringToLine (indirect)")
1112ee('vim.current.buffer[0] = "\\na"')
Bram Moolenaardee2e312013-06-23 16:35:47 +02001113ee('vim.current.buffer[0] = b"\\na"')
Bram Moolenaar8600e402013-05-30 13:28:41 +02001114cb.append(">> SetBufferLine (indirect)")
1115ee('vim.current.buffer[0] = True')
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001116cb.append(">> SetBufferLineList (indirect)")
Bram Moolenaar8600e402013-05-30 13:28:41 +02001117ee('vim.current.buffer[:] = True')
1118ee('vim.current.buffer[:] = ["\\na", "bc"]')
1119cb.append(">> InsertBufferLines (indirect)")
1120ee('vim.current.buffer.append(None)')
1121ee('vim.current.buffer.append(["\\na", "bc"])')
1122ee('vim.current.buffer.append("\\nbc")')
1123cb.append(">> RBItem")
Bram Moolenaar96c7dfd2013-05-31 18:46:11 +02001124ee('vim.current.buffer[100000000]')
Bram Moolenaar8600e402013-05-30 13:28:41 +02001125cb.append(">> RBAsItem")
Bram Moolenaar96c7dfd2013-05-31 18:46:11 +02001126ee('vim.current.buffer[100000000] = ""')
Bram Moolenaar8600e402013-05-30 13:28:41 +02001127cb.append(">> BufferAttr")
1128ee('vim.current.buffer.xxx')
1129cb.append(">> BufferSetattr")
1130ee('vim.current.buffer.name = True')
1131ee('vim.current.buffer.xxx = True')
1132cb.append(">> BufferMark")
1133ee('vim.current.buffer.mark(0)')
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001134ee('vim.current.buffer.mark("abcM")')
Bram Moolenaar8600e402013-05-30 13:28:41 +02001135ee('vim.current.buffer.mark("!")')
1136cb.append(">> BufferRange")
1137ee('vim.current.buffer.range(1, 2, 3)')
1138cb.append("> BufMap")
1139cb.append(">> BufMapItem")
Bram Moolenaar8600e402013-05-30 13:28:41 +02001140ee('vim.buffers[100000000]')
Bram Moolenaardee2e312013-06-23 16:35:47 +02001141number_test('vim.buffers[%s]', natural=True)
Bram Moolenaar8600e402013-05-30 13:28:41 +02001142cb.append("> Current")
1143cb.append(">> CurrentGetattr")
1144ee('vim.current.xxx')
1145cb.append(">> CurrentSetattr")
1146ee('vim.current.line = True')
1147ee('vim.current.buffer = True')
1148ee('vim.current.window = True')
1149ee('vim.current.tabpage = True')
1150ee('vim.current.xxx = True')
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001151del d
1152del ned
1153del dl
1154del l
1155del ll
1156del f
1157del fd
1158del fdel
1159del subexpr_test
1160del stringtochars_test
1161del Mapping
1162del convertfrompyobject_test
1163del convertfrompymapping_test
1164del iter_test
Bram Moolenaardee2e312013-06-23 16:35:47 +02001165del number_test
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001166del FailingTrue
1167del FailingIter
1168del FailingIterNext
1169del FailingMapping
1170del FailingMappingKey
1171del FailingList
Bram Moolenaardee2e312013-06-23 16:35:47 +02001172del NoArgsCall
1173del FailingCall
1174del FailingNumber
Bram Moolenaar8600e402013-05-30 13:28:41 +02001175EOF
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001176:delfunction F
Bram Moolenaar8600e402013-05-30 13:28:41 +02001177:"
Bram Moolenaara9f22202013-06-11 18:48:21 +02001178:" Test import
1179py3 << EOF
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02001180sys.path.insert(0, os.path.join(os.getcwd(), 'python_before'))
1181sys.path.append(os.path.join(os.getcwd(), 'python_after'))
Bram Moolenaara9f22202013-06-11 18:48:21 +02001182vim.options['rtp'] = os.getcwd().replace(',', '\\,').replace('\\', '\\\\')
Bram Moolenaardee2e312013-06-23 16:35:47 +02001183l = []
1184def callback(path):
1185 l.append(os.path.relpath(path))
1186vim.foreach_rtp(callback)
1187cb.append(repr(l))
1188del l
1189def callback(path):
1190 return os.path.relpath(path)
1191cb.append(repr(vim.foreach_rtp(callback)))
1192del callback
Bram Moolenaara9f22202013-06-11 18:48:21 +02001193from module import dir as d
1194from modulex import ddir
1195cb.append(d + ',' + ddir)
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02001196import before
1197cb.append(before.dir)
1198import after
1199cb.append(after.dir)
Bram Moolenaardee2e312013-06-23 16:35:47 +02001200import topmodule as tm
1201import topmodule.submodule as tms
1202import topmodule.submodule.subsubmodule.subsubsubmodule as tmsss
Bram Moolenaar877aa002013-06-26 21:49:51 +02001203cb.append(tm.__file__.replace(os.path.sep, '/')[-len('modulex/topmodule/__init__.py'):])
1204cb.append(tms.__file__.replace(os.path.sep, '/')[-len('modulex/topmodule/submodule/__init__.py'):])
1205cb.append(tmsss.__file__.replace(os.path.sep, '/')[-len('modulex/topmodule/submodule/subsubmodule/subsubsubmodule.py'):])
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001206del before
1207del after
1208del d
1209del ddir
Bram Moolenaardee2e312013-06-23 16:35:47 +02001210del tm
1211del tms
1212del tmsss
Bram Moolenaara9f22202013-06-11 18:48:21 +02001213EOF
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001214:"
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001215:" Test exceptions
1216:fun Exe(e)
1217: execute a:e
1218:endfun
1219py3 << EOF
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001220Exe = vim.bindeval('function("Exe")')
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001221ee('vim.command("throw \'abcN\'")')
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001222ee('Exe("throw \'def\'")')
1223ee('vim.eval("Exe(\'throw \'\'ghi\'\'\')")')
1224ee('vim.eval("Exe(\'echoerr \'\'jkl\'\'\')")')
1225ee('vim.eval("Exe(\'xxx_non_existent_command_xxx\')")')
Bram Moolenaar9fee7d42013-11-28 17:04:43 +01001226ee('vim.eval("xxx_unknown_function_xxx()")')
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001227ee('vim.bindeval("Exe(\'xxx_non_existent_command_xxx\')")')
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001228del Exe
1229EOF
1230:delfunction Exe
1231:"
Bram Moolenaard6b8a522013-11-11 01:05:48 +01001232:" Regression: interrupting vim.command propagates to next vim.command
1233py3 << EOF
1234def test_keyboard_interrupt():
1235 try:
1236 vim.command('while 1 | endwhile')
1237 except KeyboardInterrupt:
1238 cb.append('Caught KeyboardInterrupt')
1239 except Exception as e:
1240 cb.append('!!!!!!!! Caught exception: ' + repr(e))
1241 else:
1242 cb.append('!!!!!!!! No exception')
1243 try:
1244 vim.command('$ put =\'Running :put\'')
1245 except KeyboardInterrupt:
1246 cb.append('!!!!!!!! Caught KeyboardInterrupt')
1247 except Exception as e:
1248 cb.append('!!!!!!!! Caught exception: ' + repr(e))
1249 else:
1250 cb.append('No exception')
1251EOF
1252:debuggreedy
1253:call inputsave()
1254:call feedkeys("s\ns\ns\ns\nq\n")
1255:redir => output
1256:debug silent! py3 test_keyboard_interrupt()
1257:redir END
1258:0 debuggreedy
1259:silent $put =output
1260:unlet output
1261:py3 del test_keyboard_interrupt
1262:"
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001263:" Cleanup
1264py3 << EOF
1265del cb
1266del ee
1267del sys
1268del os
1269del vim
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02001270EOF
Bram Moolenaardb913952012-06-29 12:54:53 +02001271:endfun
1272:"
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001273:fun RunTest()
1274:let checkrefs = !empty($PYTHONDUMPREFS)
1275:let start = getline(1, '$')
1276:for i in range(checkrefs ? 10 : 1)
1277: if i != 0
1278: %d _
1279: call setline(1, start)
1280: endif
1281: call Test()
1282: if i == 0
1283: let result = getline(1, '$')
1284: endif
1285:endfor
1286:if checkrefs
1287: %d _
1288: call setline(1, result)
1289:endif
1290:endfun
Bram Moolenaardb913952012-06-29 12:54:53 +02001291:"
Bram Moolenaar841fbd22013-06-23 14:37:07 +02001292:call RunTest()
1293:delfunction RunTest
1294:delfunction Test
Bram Moolenaardb913952012-06-29 12:54:53 +02001295:call garbagecollect(1)
1296:"
1297:/^start:/,$wq! test.out
Bram Moolenaardee2e312013-06-23 16:35:47 +02001298:" vim: et ts=4 isk-=\:
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001299:call getchar()
Bram Moolenaardb913952012-06-29 12:54:53 +02001300ENDTEST
1301
1302start: