blob: 8ef7e057fe2130dc9762c408604daf87daf4b46b [file] [log] [blame]
Tests for various python features. vim: set ft=vim :
NOTE: This will cause errors when run under valgrind.
This would require recompiling Python with:
./configure --without-pymalloc
See http://svn.python.org/view/python/trunk/Misc/README.valgrind?view=markup
STARTTEST
:so small.vim
:if !has('python') | e! test.ok | wq! test.out | endif
:py import vim
:fun Test()
:let l = []
:py l=vim.bindeval('l')
:py f=vim.bindeval('function("strlen")')
:" Extending List directly with different types
:py l.extend([1, "as'd", [1, 2, f, {'a': 1}]])
:$put =string(l)
:$put =string(l[-1])
:try
: $put =string(l[-4])
:catch
: $put =v:exception[:13]
:endtry
:" List assignment
:py l[0]=0
:$put =string(l)
:py l[-2]=f
:$put =string(l)
:"
:" Extending Dictionary directly with different types
:let d = {}
:py d=vim.bindeval('d')
:py d['1']='asd'
:py d['b']=[1, 2, f]
:py d['-1']={'a': 1}
:let dkeys = []
:py dk=vim.bindeval('dkeys')
:py dkeys=d.keys()
:py dkeys.sort()
:py dk.extend(dkeys)
:$put =string(dkeys)
:for [key, val] in sort(items(d))
: $put =string(key) . ' : ' . string(val)
: unlet key val
:endfor
:"
:" removing items with del
:py del l[2]
:$put =string(l)
:let l = range(8)
:py l=vim.bindeval('l')
:try
: py del l[:3]
: py del l[1:]
:catch
: $put =v:exception
:endtry
:$put =string(l)
:"
:py del d['-1']
:$put =string(d)
:"
:" removing items out of range: silently skip items that don't exist
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:" The following two ranges delete nothing as they match empty list:
:py del l[2:1]
:$put =string(l)
:py del l[2:2]
:$put =string(l)
:py del l[2:3]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:py del l[2:4]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:py del l[2:5]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:py del l[2:6]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:" The following two ranges delete nothing as they match empty list:
:py del l[-1:2]
:$put =string(l)
:py del l[-2:2]
:$put =string(l)
:py del l[-3:2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:py del l[-4:2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:py del l[-5:2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:py del l[-6:2]
:$put =string(l)
:"
:" Slice assignment to a list
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:py l[0:0]=['a']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:py l[1:2]=['b']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:py l[2:4]=['c']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:py l[4:4]=['d']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:py l[-1:2]=['e']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:py l[-10:2]=['f']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:py l[2:-10]=['g']
:$put =string(l)
:let l = []
:py l=vim.bindeval('l')
:py l[0:0]=['h']
:$put =string(l)
:"
:" Locked variables
:let l = [0, 1, 2, 3]
:py l=vim.bindeval('l')
:lockvar! l
:py l[2]='i'
:$put =string(l)
:unlockvar! l
:"
:" Function calls
:function New(...)
:return ['NewStart']+a:000+['NewEnd']
:endfunction
:function DictNew(...) dict
:return ['DictNewStart']+a:000+['DictNewEnd', self]
:endfunction
:let l=[function('New'), function('DictNew')]
:py l=vim.bindeval('l')
:py l.extend(list(l[0](1, 2, 3)))
:$put =string(l)
:py l.extend(list(l[1](1, 2, 3, self={'a': 'b'})))
:$put =string(l)
:py l.extend([l[0].name])
:$put =string(l)
:try
: py l[1](1, 2, 3)
:catch
: $put =v:exception[:16]
:endtry
:delfunction New
:try
: py l[0](1, 2, 3)
:catch
: $put =v:exception[:16]
:endtry
:if has('float')
: let l=[0.0]
: py l=vim.bindeval('l')
: py l.extend([0.0])
: $put =string(l)
:else
: $put ='[0.0, 0.0]'
:endif
:let messages=[]
py <<EOF
d=vim.bindeval('{}')
m=vim.bindeval('messages')
def em(expr, g=globals(), l=locals()):
try:
exec(expr, g, l)
except:
m.extend([sys.exc_type.__name__])
em('d["abc"]')
em('d["abc"]="\\0"')
em('d["abc"]=vim')
em('d[""]=1')
em('d["a\\0b"]=1')
em('d[u"a\\0b"]=1')
EOF
:$put =messages
:unlet messages
:" locked and scope attributes
:let d={} | let dl={} | lockvar dl
:for s in split("d dl v: g:")
: let name=tr(s, ':', 's')
: execute 'py '.name.'=vim.bindeval("'.s.'")'
: let toput=s.' : '.join(map(['locked', 'scope'], 'v:val.":".pyeval(name.".".v:val)'), ';')
: $put =toput
:endfor
:silent! let d.abc=1
:silent! let dl.abc=1
:py d.locked=True
:py dl.locked=False
:silent! let d.def=1
:silent! let dl.def=1
:put ='d:'.string(d)
:put ='dl:'.string(dl)
:unlet d dl
:
:let l=[] | let ll=[] | lockvar ll
:for s in split("l ll")
: let name=tr(s, ':', 's')
: execute 'py '.name.'=vim.bindeval("'.s.'")'
: let toput=s.' : locked:'.pyeval(name.'.locked')
: $put =toput
:endfor
:silent! call extend(l, [0])
:silent! call extend(ll, [0])
:py l.locked=True
:py ll.locked=False
:silent! call extend(l, [1])
:silent! call extend(ll, [1])
:put ='l:'.string(l)
:put ='ll:'.string(ll)
:unlet l ll
:"
:" pyeval()
:let l=pyeval('range(3)')
:$put =string(l)
:let d=pyeval('{"a": "b", "c": 1, "d": ["e"]}')
:$put =sort(items(d))
:if has('float')
: let f=pyeval('0.0')
: $put =string(f)
:else
: $put ='0.0'
:endif
:" Invalid values:
:for e in ['"\0"', '{"\0": 1}', 'undefined_name', 'vim']
: try
: let v=pyeval(e)
: catch
: let toput=e.":\t".v:exception[:13]
: $put =toput
: endtry
:endfor
:"
:" threading
:let l = [0]
:py l=vim.bindeval('l')
:py <<EOF
import threading
import time
class T(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.t = 0
self.running = True
def run(self):
while self.running:
self.t += 1
time.sleep(0.1)
t = T()
t.start()
EOF
:sleep 1
:py t.running = False
:py t.join()
:py l[0] = t.t > 8 # check if the background thread is working
:$put =string(l)
:"
:" settrace
:let l = []
:py l=vim.bindeval('l')
:py <<EOF
import sys
def traceit(frame, event, arg):
global l
if event == "line":
l.extend([frame.f_lineno])
return traceit
def trace_main():
for i in range(5):
pass
EOF
:py sys.settrace(traceit)
:py trace_main()
:py sys.settrace(None)
:$put =string(l)
:"
:" Slice
:py ll = vim.bindeval('[0, 1, 2, 3, 4, 5]')
:py l = ll[:4]
:$put =string(pyeval('l'))
:py l = ll[2:]
:$put =string(pyeval('l'))
:py l = ll[:-4]
:$put =string(pyeval('l'))
:py l = ll[-2:]
:$put =string(pyeval('l'))
:py l = ll[2:4]
:$put =string(pyeval('l'))
:py l = ll[4:2]
:$put =string(pyeval('l'))
:py l = ll[-4:-2]
:$put =string(pyeval('l'))
:py l = ll[-2:-4]
:$put =string(pyeval('l'))
:py l = ll[:]
:$put =string(pyeval('l'))
:py l = ll[0:6]
:$put =string(pyeval('l'))
:py l = ll[-10:10]
:$put =string(pyeval('l'))
:"
:" Vars
:let g:foo = 'bac'
:let w:abc = 'def'
:let b:baz = 'bar'
:try
: throw "Abc"
:catch
: put =pyeval('vim.vvars[''exception'']')
:endtry
:put =pyeval('vim.vars[''foo'']')
:put =pyeval('vim.current.window.vars[''abc'']')
:put =pyeval('vim.current.buffer.vars[''baz'']')
:"
:" Options
:" paste: boolean, global
:" previewheight number, global
:" operatorfunc: string, global
:" number: boolean, window-local
:" numberwidth: number, window-local
:" colorcolumn: string, window-local
:" statusline: string, window-local/global
:" autoindent: boolean, buffer-local
:" iminsert: number, buffer-local
:" omnifunc: string, buffer-local
:" preserveindent: boolean, buffer-local/global
:" path: string, buffer-local/global
:let g:bufs=[bufnr('%')]
:new
:let g:bufs+=[bufnr('%')]
:vnew
:let g:bufs+=[bufnr('%')]
:wincmd j
:vnew
:let g:bufs+=[bufnr('%')]
:wincmd l
:fun RecVars(opt)
: let gval =string(eval('&g:'.a:opt))
: let wvals=join(map(range(1, 4), 'v:val.":".string(getwinvar(v:val, "&".a:opt))'))
: let bvals=join(map(copy(g:bufs), 'v:val.":".string(getbufvar(v:val, "&".a:opt))'))
: put =' G: '.gval
: put =' W: '.wvals
: put =' B: '.wvals
:endfun
py << EOF
def e(s, g=globals(), l=locals()):
try:
exec(s, g, l)
except:
vim.command('throw ' + repr(sys.exc_type.__name__))
def ev(s, g=globals(), l=locals()):
try:
return eval(s, g, l)
except:
vim.command('throw ' + repr(sys.exc_type.__name__))
return 0
EOF
:function E(s)
: python e(vim.eval('a:s'))
:endfunction
:function Ev(s)
: return pyeval('ev(vim.eval("a:s"))')
:endfunction
:py gopts1=vim.options
:py wopts1=vim.windows[2].options
:py wopts2=vim.windows[0].options
:py wopts3=vim.windows[1].options
:py bopts1=vim.buffers[vim.bindeval("g:bufs")[2]].options
:py bopts2=vim.buffers[vim.bindeval("g:bufs")[1]].options
:py bopts3=vim.buffers[vim.bindeval("g:bufs")[0]].options
:let lst=[]
:let lst+=[['paste', 1, 0, 1, 2, 1, 1, 0 ]]
:let lst+=[['previewheight', 5, 1, 6, 'a', 0, 1, 0 ]]
:let lst+=[['operatorfunc', 'A', 'B', 'C', 2, 0, 1, 0 ]]
:let lst+=[['number', 0, 1, 1, 0, 1, 0, 1 ]]
:let lst+=[['numberwidth', 2, 3, 5, -100, 0, 0, 1 ]]
:let lst+=[['colorcolumn', '+1', '+2', '+3', 'abc', 0, 0, 1 ]]
:let lst+=[['statusline', '1', '2', '4', 0, 0, 1, 1 ]]
:let lst+=[['autoindent', 0, 1, 1, 2, 1, 0, 2 ]]
:let lst+=[['iminsert', 0, 2, 1, 3, 0, 0, 2 ]]
:let lst+=[['omnifunc', 'A', 'B', 'C', 1, 0, 0, 2 ]]
:let lst+=[['preserveindent', 0, 1, 1, 2, 1, 1, 2 ]]
:let lst+=[['path', '.,,', ',,', '.', 0, 0, 1, 2 ]]
:for [oname, oval1, oval2, oval3, invval, bool, global, local] in lst
: py oname=vim.eval('oname')
: py oval1=vim.bindeval('oval1')
: py oval2=vim.bindeval('oval2')
: py oval3=vim.bindeval('oval3')
: if invval is 0 || invval is 1
: py invval=bool(vim.bindeval('invval'))
: else
: py invval=vim.bindeval('invval')
: endif
: if bool
: py oval1=bool(oval1)
: py oval2=bool(oval2)
: py oval3=bool(oval3)
: endif
: put ='>>> '.oname
: for v in ['gopts1', 'wopts1', 'bopts1']
: try
: put =' p/'.v.': '.Ev('repr('.v.'['''.oname.'''])')
: catch
: put =' p/'.v.'! '.v:exception
: endtry
: try
: call E(v.'["'.oname.'"]=invval')
: catch
: put =' inv: '.string(invval).'! '.v:exception
: endtry
: for vv in (v is# 'gopts1' ? [v] : [v, v[:-2].'2', v[:-2].'3'])
: let val=substitute(vv, '^.opts', 'oval', '')
: try
: call E(vv.'["'.oname.'"]='.val)
: catch
: put =' '.vv.'! '.v:exception
: endtry
: endfor
: endfor
: call RecVars(oname)
: for v in ['wopts3', 'bopts3']
: try
: call E('del '.v.'["'.oname.'"]')
: catch
: put =' del '.v.'! '.v:exception
: endtry
: endfor
: call RecVars(oname)
:endfor
:only
:for buf in g:bufs[1:]
: execute 'bwipeout!' buf
:endfor
:"
:" Test buffer object
:vnew
:put ='First line'
:put ='Second line'
:put ='Third line'
:1 delete _
:py b=vim.current.buffer
:wincmd w
:mark a
py << EOF
cb = vim.current.buffer
# Tests BufferAppend and BufferItem
cb.append(b[0])
# Tests BufferSlice and BufferAssSlice
cb.append('abc') # Will be overwritten
cb[-1:] = b[:-2]
# Test BufferLength and BufferAssSlice
cb.append('def') # Will not be overwritten
cb[len(cb):] = b[:]
# Test BufferAssItem and BufferMark
cb.append('ghi') # Will be overwritten
cb[-1] = repr((len(cb) - cb.mark('a')[0], cb.mark('a')[1]))
# Test BufferRepr
cb.append(repr(cb) + repr(b))
# Modify foreign buffer
b.append('foo')
b[0]='bar'
b[0:0]=['baz']
vim.command('call append("$", getbufline(%i, 1, "$"))' % b.number)
# Test CheckBuffer
vim.command('bwipeout! ' + str(b.number))
for expr in ('b[1]','b[:] = ["A", "B"]','b[:]','b.append("abc")'):
try:
exec(expr)
except vim.error:
pass
else:
# Usually a SEGV here
# Should not happen in any case
cb.append('No exception for ' + expr)
EOF
:"
:" Test vim.buffers object
:set hidden
:edit a
:buffer #
:edit b
:buffer #
:edit c
:buffer #
py << EOF
# Check GCing iterator that was not fully exhausted
i = iter(vim.buffers)
cb.append('i:' + str(next(i)))
# and also check creating more then one iterator at a time
i2 = iter(vim.buffers)
cb.append('i2:' + str(next(i2)))
cb.append('i:' + str(next(i)))
# The following should trigger GC and not cause any problems
del i
del i2
i3 = iter(vim.buffers)
cb.append('i3:' + str(next(i3)))
del i3
prevnum = 0
for b in vim.buffers:
# Check buffer order
if prevnum >= b.number:
cb.append('!!! Buffer numbers not in strictly ascending order')
# Check indexing: vim.buffers[number].number == number
cb.append(str(b.number) + ':' + repr(vim.buffers[b.number]) + '=' + repr(b))
prevnum = b.number
cb.append(str(len(vim.buffers)))
bnums = list(map(lambda b: b.number, vim.buffers))[1:]
# Test wiping out buffer with existing iterator
i4 = iter(vim.buffers)
cb.append('i4:' + str(next(i4)))
vim.command('bwipeout! ' + str(bnums.pop(0)))
try:
next(i4)
except vim.error:
pass
else:
cb.append('!!!! No vim.error')
i4 = iter(vim.buffers)
vim.command('bwipeout! ' + str(bnums.pop(-1)))
vim.command('bwipeout! ' + str(bnums.pop(-1)))
cb.append('i4:' + str(next(i4)))
try:
next(i4)
except StopIteration:
cb.append('StopIteration')
EOF
:endfun
:"
:call Test()
:"
:delfunc Test
:call garbagecollect(1)
:"
:/^start:/,$wq! test.out
:call getchar()
ENDTEST
start: