forked from neovim/pynvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: migrate to pytest (neovim#266)
- Loading branch information
Showing
13 changed files
with
335 additions
and
392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'] |
Oops, something went wrong.