Skip to content

Commit

Permalink
test: migrate to pytest (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
meitham authored and justinmk committed Apr 25, 2018
1 parent d53415d commit b65f62d
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 392 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -148,21 +148,21 @@ to use it for the plugin host.
To run the tests execute

```sh
nosetests
pytest
```

This will run the tests in an embedded instance of nvim.
If you want to test a different version than `nvim` in `$PATH` use
```sh
NVIM_CHILD_ARGV='["/path/to/nvim", "-u", "NONE", "--embed"]' nosetests
NVIM_CHILD_ARGV='["/path/to/nvim", "-u", "NONE", "--embed"]' pytest
```

Alternatively, if you want to see the state of nvim, you could use

```sh
export NVIM_LISTEN_ADDRESS=/tmp/nvimtest
xterm -e "nvim -u NONE"&
nosetests
pytest
```

But note you need to restart nvim every time you run the tests! Substitute your
Expand Down
10 changes: 10 additions & 0 deletions setup.py
Expand Up @@ -8,6 +8,14 @@
'msgpack>=0.5.0',
]

tests_require = [
'pytest>=3.4.0',
]

extras_require = {
'pyuv': ['pyuv>=1.0.0'],
}

if os.name == 'nt':
install_requires.append('pyuv>=1.0.0')
elif sys.version_info < (3, 4):
Expand All @@ -29,4 +37,6 @@
packages=['neovim', 'neovim.api', 'neovim.msgpack_rpc',
'neovim.msgpack_rpc.event_loop', 'neovim.plugin'],
install_requires=install_requires,
tests_require=tests_require,
extras_require=extras_require,
zip_safe=False)
67 changes: 67 additions & 0 deletions test/conftest.py
@@ -0,0 +1,67 @@
import json
import os
import textwrap

import neovim
import pytest

neovim.setup_logging("test")


@pytest.fixture(autouse=True)
def cleanup_func(vim):
fun = textwrap.dedent(''':function BeforeEachTest()
set all&
redir => groups
silent augroup
redir END
for group in split(groups)
exe 'augroup '.group
autocmd!
augroup END
endfor
autocmd!
tabnew
let curbufnum = eval(bufnr('%'))
redir => buflist
silent ls!
redir END
let bufnums = []
for buf in split(buflist, '\\n')
let bufnum = eval(split(buf, '[ u]')[0])
if bufnum != curbufnum
call add(bufnums, bufnum)
endif
endfor
if len(bufnums) > 0
exe 'silent bwipeout! '.join(bufnums, ' ')
endif
silent tabonly
for k in keys(g:)
exe 'unlet g:'.k
endfor
filetype plugin indent off
mapclear
mapclear!
abclear
comclear
endfunction
''')
vim.input(fun)
vim.command('call BeforeEachTest()')
assert len(vim.tabpages) == len(vim.windows) == len(vim.buffers) == 1


@pytest.fixture
def vim():
child_argv = os.environ.get('NVIM_CHILD_ARGV')
listen_address = os.environ.get('NVIM_LISTEN_ADDRESS')
if child_argv is None and listen_address is None:
child_argv = '["nvim", "-u", "NONE", "--embed"]'

if child_argv is not None:
editor = neovim.attach('child', argv=json.loads(child_argv))
else:
editor = neovim.attach('socket', path=listen_address)

return editor
168 changes: 75 additions & 93 deletions test/test_buffer.py
@@ -1,181 +1,163 @@
import os
from nose.tools import with_setup, eq_ as eq, ok_ as ok
from test_common import vim, cleanup

from neovim.compat import IS_PYTHON3


@with_setup(setup=cleanup)
def test_get_length():
eq(len(vim.current.buffer), 1)
def test_get_length(vim):
assert len(vim.current.buffer) == 1
vim.current.buffer.append('line')
eq(len(vim.current.buffer), 2)
assert len(vim.current.buffer) == 2
vim.current.buffer.append('line')
eq(len(vim.current.buffer), 3)
assert len(vim.current.buffer) == 3
vim.current.buffer[-1] = None
eq(len(vim.current.buffer), 2)
assert len(vim.current.buffer) == 2
vim.current.buffer[-1] = None
vim.current.buffer[-1] = None
# There's always at least one line
eq(len(vim.current.buffer), 1)
assert len(vim.current.buffer) == 1


@with_setup(setup=cleanup)
def test_get_set_del_line():
eq(vim.current.buffer[0], '')
def test_get_set_del_line(vim):
assert vim.current.buffer[0] == ''
vim.current.buffer[0] = 'line1'
eq(vim.current.buffer[0], 'line1')
assert vim.current.buffer[0] == 'line1'
vim.current.buffer[0] = 'line2'
eq(vim.current.buffer[0], 'line2')
assert vim.current.buffer[0] == 'line2'
vim.current.buffer[0] = None
eq(vim.current.buffer[0], '')
assert vim.current.buffer[0] == ''
# __delitem__
vim.current.buffer[:] = ['line1', 'line2', 'line3']
eq(vim.current.buffer[2], 'line3')
assert vim.current.buffer[2] == 'line3'
del vim.current.buffer[0]
eq(vim.current.buffer[0], 'line2')
eq(vim.current.buffer[1], 'line3')
assert vim.current.buffer[0] == 'line2'
assert vim.current.buffer[1] == 'line3'
del vim.current.buffer[-1]
eq(vim.current.buffer[0], 'line2')
eq(len(vim.current.buffer), 1)
assert vim.current.buffer[0] == 'line2'
assert len(vim.current.buffer) == 1


@with_setup(setup=cleanup)
def test_get_set_del_slice():
eq(vim.current.buffer[:], [''])
def test_get_set_del_slice(vim):
assert vim.current.buffer[:] == ['']
# Replace buffer
vim.current.buffer[:] = ['a', 'b', 'c']
eq(vim.current.buffer[:], ['a', 'b', 'c'])
eq(vim.current.buffer[1:], ['b', 'c'])
eq(vim.current.buffer[1:2], ['b'])
eq(vim.current.buffer[1:1], [])
eq(vim.current.buffer[:-1], ['a', 'b'])
eq(vim.current.buffer[1:-1], ['b'])
eq(vim.current.buffer[-2:], ['b', 'c'])
assert vim.current.buffer[:] == ['a', 'b', 'c']
assert vim.current.buffer[1:] == ['b', 'c']
assert vim.current.buffer[1:2] == ['b']
assert vim.current.buffer[1:1] == []
assert vim.current.buffer[:-1] == ['a', 'b']
assert vim.current.buffer[1:-1] == ['b']
assert vim.current.buffer[-2:] == ['b', 'c']
vim.current.buffer[1:2] = ['a', 'b', 'c']
eq(vim.current.buffer[:], ['a', 'a', 'b', 'c', 'c'])
assert vim.current.buffer[:] == ['a', 'a', 'b', 'c', 'c']
vim.current.buffer[-1:] = ['a', 'b', 'c']
eq(vim.current.buffer[:], ['a', 'a', 'b', 'c', 'a', 'b', 'c'])
assert vim.current.buffer[:] == ['a', 'a', 'b', 'c', 'a', 'b', 'c']
vim.current.buffer[:-3] = None
eq(vim.current.buffer[:], ['a', 'b', 'c'])
assert vim.current.buffer[:] == ['a', 'b', 'c']
vim.current.buffer[:] = None
eq(vim.current.buffer[:], [''])
assert vim.current.buffer[:] == ['']
# __delitem__
vim.current.buffer[:] = ['a', 'b', 'c']
del vim.current.buffer[:]
eq(vim.current.buffer[:], [''])
assert vim.current.buffer[:] == ['']
vim.current.buffer[:] = ['a', 'b', 'c']
del vim.current.buffer[:1]
eq(vim.current.buffer[:], ['b', 'c'])
assert vim.current.buffer[:] == ['b', 'c']
del vim.current.buffer[:-1]
eq(vim.current.buffer[:], ['c'])
assert vim.current.buffer[:] == ['c']


@with_setup(setup=cleanup)
def test_vars():
def test_vars(vim):
vim.current.buffer.vars['python'] = [1, 2, {'3': 1}]
eq(vim.current.buffer.vars['python'], [1, 2, {'3': 1}])
eq(vim.eval('b:python'), [1, 2, {'3': 1}])
assert vim.current.buffer.vars['python'] == [1, 2, {'3': 1}]
assert vim.eval('b:python') == [1, 2, {'3': 1}]


@with_setup(setup=cleanup)
def test_api():
def test_api(vim):
vim.current.buffer.api.set_var('myvar', 'thetext')
eq(vim.current.buffer.api.get_var('myvar'), 'thetext')
eq(vim.eval('b:myvar'), 'thetext')
vim.current.buffer.api.set_lines(0,-1,True,['alpha', 'beta'])
eq(vim.current.buffer.api.get_lines(0,-1,True), ['alpha', 'beta'])
eq(vim.current.buffer[:], ['alpha', 'beta'])
assert vim.current.buffer.api.get_var('myvar') == 'thetext'
assert vim.eval('b:myvar') == 'thetext'
vim.current.buffer.api.set_lines(0, -1, True, ['alpha', 'beta'])
assert vim.current.buffer.api.get_lines(0, -1, True) == ['alpha', 'beta']
assert vim.current.buffer[:] == ['alpha', 'beta']


@with_setup(setup=cleanup)
def test_options():
eq(vim.current.buffer.options['shiftwidth'], 8)
def test_options(vim):
assert vim.current.buffer.options['shiftwidth'] == 8
vim.current.buffer.options['shiftwidth'] = 4
eq(vim.current.buffer.options['shiftwidth'], 4)
assert vim.current.buffer.options['shiftwidth'] == 4
# global-local option
vim.current.buffer.options['define'] = 'test'
eq(vim.current.buffer.options['define'], 'test')
assert vim.current.buffer.options['define'] == 'test'
# Doesn't change the global value
eq(vim.options['define'], '^\s*#\s*define')
assert vim.options['define'] == '^\s*#\s*define'


@with_setup(setup=cleanup)
def test_number():
def test_number(vim):
curnum = vim.current.buffer.number
vim.command('new')
eq(vim.current.buffer.number, curnum + 1)
assert vim.current.buffer.number == curnum + 1
vim.command('new')
eq(vim.current.buffer.number, curnum + 2)
assert vim.current.buffer.number == curnum + 2


@with_setup(setup=cleanup)
def test_name():
def test_name(vim):
vim.command('new')
eq(vim.current.buffer.name, '')
assert vim.current.buffer.name == ''
new_name = vim.eval('resolve(tempname())')
vim.current.buffer.name = new_name
eq(vim.current.buffer.name, new_name)
assert vim.current.buffer.name == new_name
vim.command('silent w!')
ok(os.path.isfile(new_name))
assert os.path.isfile(new_name)
os.unlink(new_name)


@with_setup(setup=cleanup)
def test_valid():
def test_valid(vim):
vim.command('new')
buffer = vim.current.buffer
ok(buffer.valid)
assert buffer.valid
vim.command('bw!')
ok(not buffer.valid)
assert not buffer.valid


@with_setup(setup=cleanup)
def test_append():
def test_append(vim):
vim.current.buffer.append('a')
eq(vim.current.buffer[:], ['', 'a'])
assert vim.current.buffer[:] == ['', 'a']
vim.current.buffer.append('b', 0)
eq(vim.current.buffer[:], ['b', '', 'a'])
assert vim.current.buffer[:] == ['b', '', 'a']
vim.current.buffer.append(['c', 'd'])
eq(vim.current.buffer[:], ['b', '', 'a', 'c', 'd'])
assert vim.current.buffer[:] == ['b', '', 'a', 'c', 'd']
vim.current.buffer.append(['c', 'd'], 2)
eq(vim.current.buffer[:], ['b', '', 'c', 'd', 'a', 'c', 'd'])
assert vim.current.buffer[:] == ['b', '', 'c', 'd', 'a', 'c', 'd']
vim.current.buffer.append(b'bytes')
eq(vim.current.buffer[:], ['b', '', 'c', 'd', 'a', 'c', 'd', 'bytes'])
assert vim.current.buffer[:] == ['b', '', 'c', 'd', 'a', 'c', 'd', 'bytes']


@with_setup(setup=cleanup)
def test_mark():
def test_mark(vim):
vim.current.buffer.append(['a', 'bit of', 'text'])
vim.current.window.cursor = [3, 4]
vim.command('mark V')
eq(vim.current.buffer.mark('V'), [3, 0])
assert vim.current.buffer.mark('V') == [3, 0]

@with_setup(setup=cleanup)
def test_invalid_utf8():

def test_invalid_utf8(vim):
vim.command('normal "=printf("%c", 0xFF)\np')
eq(vim.eval("char2nr(getline(1))"), 0xFF)
assert vim.eval("char2nr(getline(1))") == 0xFF

eq(vim.current.buffer[:], ['\udcff'] if IS_PYTHON3 else ['\xff'])
assert vim.current.buffer[:] == ['\udcff'] if IS_PYTHON3 else ['\xff']
vim.current.line += 'x'
eq(vim.eval("getline(1)", decode=False), b'\xFFx')
eq(vim.current.buffer[:], ['\udcffx'] if IS_PYTHON3 else ['\xffx'])
assert vim.eval("getline(1)", decode=False) == b'\xFFx'
assert vim.current.buffer[:] == ['\udcffx'] if IS_PYTHON3 else ['\xffx']


@with_setup(setup=cleanup)
def test_get_exceptions():
def test_get_exceptions(vim):
try:
vim.current.buffer.options['invalid-option']
ok(False)
assert False
except vim.error:
pass

@with_setup(setup=cleanup)
def test_contains():
ok(vim.current.buffer in vim.buffers)

@with_setup(setup=cleanup)
def test_set_items_for_range():
def test_set_items_for_range(vim):
vim.current.buffer[:] = ['a', 'b', 'c', 'd', 'e']
r = vim.current.buffer.range(1, 3)
r[1:3] = ['foo']*3
eq(vim.current.buffer[:], ['a', 'foo', 'foo', 'foo', 'd', 'e'])
assert vim.current.buffer[:] == ['a', 'foo', 'foo', 'foo', 'd', 'e']

0 comments on commit b65f62d

Please sign in to comment.