From b65f62dc3f40bab3262218c60ecb6b5a0bb327a1 Mon Sep 17 00:00:00 2001 From: Meitham Date: Thu, 26 Apr 2018 00:16:07 +0100 Subject: [PATCH] test: migrate to pytest (#266) --- README.md | 6 +- setup.py | 10 +++ test/conftest.py | 67 ++++++++++++++++ test/test_buffer.py | 168 +++++++++++++++++---------------------- test/test_client_rpc.py | 37 ++++----- test/test_common.py | 67 ---------------- test/test_concurrency.py | 14 ++-- test/test_decorators.py | 14 ++-- test/test_events.py | 38 ++++----- test/test_tabpage.py | 37 ++++----- test/test_vim.py | 159 +++++++++++++++++------------------- test/test_window.py | 105 ++++++++++-------------- tox.ini | 5 +- 13 files changed, 335 insertions(+), 392 deletions(-) create mode 100644 test/conftest.py delete mode 100644 test/test_common.py diff --git a/README.md b/README.md index b853aae1..5ebd2a4c 100644 --- a/README.md +++ b/README.md @@ -148,13 +148,13 @@ 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 @@ -162,7 +162,7 @@ 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 diff --git a/setup.py b/setup.py index c7367085..efe26f7b 100644 --- a/setup.py +++ b/setup.py @@ -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): @@ -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) diff --git a/test/conftest.py b/test/conftest.py new file mode 100644 index 00000000..df3c7767 --- /dev/null +++ b/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 diff --git a/test/test_buffer.py b/test/test_buffer.py index 16710483..b5ecfaa2 100644 --- a/test/test_buffer.py +++ b/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'] diff --git a/test/test_client_rpc.py b/test/test_client_rpc.py index cf564f34..0c150479 100644 --- a/test/test_client_rpc.py +++ b/test/test_client_rpc.py @@ -1,32 +1,28 @@ # -*- coding: utf-8 -*- -from nose.tools import with_setup, eq_ as eq -from test_common import vim, cleanup -cid = vim.channel_id - -@with_setup(setup=cleanup) -def test_call_and_reply(): +def test_call_and_reply(vim): + cid = vim.channel_id def setup_cb(): cmd = 'let g:result = rpcrequest(%d, "client-call", 1, 2, 3)' % cid vim.command(cmd) - eq(vim.vars['result'], [4, 5, 6]) + assert vim.vars['result'] == [4, 5, 6] vim.stop_loop() def request_cb(name, args): - eq(name, 'client-call') - eq(args, [1, 2, 3]) + assert name == 'client-call' + assert args == [1, 2, 3] return [4, 5, 6] vim.run_loop(request_cb, None, setup_cb) -@with_setup(setup=cleanup) -def test_call_api_before_reply(): +def test_call_api_before_reply(vim): + cid = vim.channel_id def setup_cb(): cmd = 'let g:result = rpcrequest(%d, "client-call2", 1, 2, 3)' % cid vim.command(cmd) - eq(vim.vars['result'], [7, 8, 9]) + assert vim.vars['result'] == [7, 8, 9] vim.stop_loop() def request_cb(name, args): @@ -35,8 +31,7 @@ def request_cb(name, args): vim.run_loop(request_cb, None, setup_cb) -@with_setup(setup=cleanup) -def test_async_call(): +def test_async_call(vim): def request_cb(name, args): if name == "test-event": @@ -46,11 +41,11 @@ def request_cb(name, args): # this would have dead-locked if not async vim.funcs.rpcrequest(vim.channel_id, "test-event", async_=True) vim.run_loop(request_cb, None, None) - eq(vim.vars['result'], 17) + assert vim.vars['result'] == 17 -@with_setup(setup=cleanup) -def test_recursion(): +def test_recursion(vim): + cid = vim.channel_id def setup_cb(): vim.vars['result1'] = 0 vim.vars['result2'] = 0 @@ -58,10 +53,10 @@ def setup_cb(): vim.vars['result4'] = 0 cmd = 'let g:result1 = rpcrequest(%d, "call", %d)' % (cid, 2,) vim.command(cmd) - eq(vim.vars['result1'], 4) - eq(vim.vars['result2'], 8) - eq(vim.vars['result3'], 16) - eq(vim.vars['result4'], 32) + assert vim.vars['result1'] == 4 + assert vim.vars['result2'] == 8 + assert vim.vars['result3'] == 16 + assert vim.vars['result4'] == 32 vim.stop_loop() def request_cb(name, args): diff --git a/test/test_common.py b/test/test_common.py deleted file mode 100644 index c17823f4..00000000 --- a/test/test_common.py +++ /dev/null @@ -1,67 +0,0 @@ -import json -import os -import sys - -import neovim - -from nose.tools import eq_ as eq - -neovim.setup_logging("test") - -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: - vim = neovim.attach('child', argv=json.loads(child_argv)) -else: - vim = neovim.attach('socket', path=listen_address) - -cleanup_func = ''':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(cleanup_func) - - -def cleanup(): - # cleanup nvim - vim.command('call BeforeEachTest()') - eq(len(vim.tabpages), 1) - eq(len(vim.windows), 1) - eq(len(vim.buffers), 1) diff --git a/test/test_concurrency.py b/test/test_concurrency.py index 4619fcc6..33ec7c3b 100644 --- a/test/test_concurrency.py +++ b/test/test_concurrency.py @@ -1,21 +1,19 @@ -from nose.tools import with_setup, eq_ as eq -from test_common import vim, cleanup from threading import Timer -@with_setup(setup=cleanup) -def test_interrupt_from_another_thread(): + +def test_interrupt_from_another_thread(vim): timer = Timer(0.5, lambda: vim.async_call(lambda: vim.stop_loop())) timer.start() - eq(vim.next_message(), None) + assert vim.next_message() == None + -@with_setup(setup=cleanup) -def test_exception_in_threadsafe_call(): +def test_exception_in_threadsafe_call(vim): # an exception in a threadsafe_call shouldn't crash the entire host msgs = [] vim.async_call(lambda: [vim.eval("3"), undefined_variable]) timer = Timer(0.5, lambda: vim.async_call(lambda: vim.stop_loop())) timer.start() vim.run_loop(None, None, err_cb=msgs.append) - eq(len(msgs), 1) + assert len(msgs) == 1 msgs[0].index('NameError') msgs[0].index('undefined_variable') diff --git a/test/test_decorators.py b/test/test_decorators.py index 1cecf298..6e0acb1e 100644 --- a/test/test_decorators.py +++ b/test/test_decorators.py @@ -1,5 +1,3 @@ -from nose.tools import with_setup, eq_ as eq, ok_ as ok - from neovim.plugin.decorators import command @@ -10,21 +8,21 @@ def function(): # ensure absence with default value of None decorated = command('test')(function) - ok('count' not in decorated._nvim_rpc_spec['opts']) + assert 'count' not in decorated._nvim_rpc_spec['opts'] # ensure absence with explicit value of None count_value = None decorated = command('test', count=count_value)(function) - ok('count' not in decorated._nvim_rpc_spec['opts']) + assert 'count' not in decorated._nvim_rpc_spec['opts'] # Test presesence with value of 0 count_value = 0 decorated = command('test', count=count_value)(function) - ok('count' in decorated._nvim_rpc_spec['opts']) - eq(decorated._nvim_rpc_spec['opts']['count'], count_value) + assert 'count' in decorated._nvim_rpc_spec['opts'] + assert decorated._nvim_rpc_spec['opts']['count'] == count_value # Test presence with value of 1 count_value = 1 decorated = command('test', count=count_value)(function) - ok('count' in decorated._nvim_rpc_spec['opts']) - eq(decorated._nvim_rpc_spec['opts']['count'], count_value) + assert 'count' in decorated._nvim_rpc_spec['opts'] + assert decorated._nvim_rpc_spec['opts']['count'] == count_value diff --git a/test/test_events.py b/test/test_events.py index 369ac73a..54c6c8df 100644 --- a/test/test_events.py +++ b/test/test_events.py @@ -1,52 +1,48 @@ # -*- coding: utf-8 -*- -from nose.tools import with_setup, eq_ as eq -from test_common import vim, cleanup -@with_setup(setup=cleanup) -def test_receiving_events(): +def test_receiving_events(vim): vim.command('call rpcnotify(%d, "test-event", 1, 2, 3)' % vim.channel_id) event = vim.next_message() - eq(event[1], 'test-event') - eq(event[2], [1, 2, 3]) + assert event[1] == 'test-event' + assert event[2] == [1, 2, 3] vim.command('au FileType python call rpcnotify(%d, "py!", bufnr("$"))' % vim.channel_id) vim.command('set filetype=python') event = vim.next_message() - eq(event[1], 'py!') - eq(event[2], [vim.current.buffer.number]) + assert event[1] == 'py!' + assert event[2] == [vim.current.buffer.number] -@with_setup(setup=cleanup) -def test_sending_notify(): + +def test_sending_notify(vim): # notify after notify vim.command("let g:test = 3", async_=True) cmd = 'call rpcnotify(%d, "test-event", g:test)' % vim.channel_id vim.command(cmd, async_=True) event = vim.next_message() - eq(event[1], 'test-event') - eq(event[2], [3]) + assert event[1] == 'test-event' + assert event[2] == [3] # request after notify vim.command("let g:data = 'xyz'", async_=True) - eq(vim.eval('g:data'), 'xyz') + assert vim.eval('g:data') == 'xyz' -@with_setup(setup=cleanup) -def test_broadcast(): +def test_broadcast(vim): vim.subscribe('event2') vim.command('call rpcnotify(0, "event1", 1, 2, 3)') vim.command('call rpcnotify(0, "event2", 4, 5, 6)') vim.command('call rpcnotify(0, "event2", 7, 8, 9)') event = vim.next_message() - eq(event[1], 'event2') - eq(event[2], [4, 5, 6]) + assert event[1] == 'event2' + assert event[2] == [4, 5, 6] event = vim.next_message() - eq(event[1], 'event2') - eq(event[2], [7, 8, 9]) + assert event[1] == 'event2' + assert event[2] == [7, 8, 9] vim.unsubscribe('event2') vim.subscribe('event1') vim.command('call rpcnotify(0, "event2", 10, 11, 12)') vim.command('call rpcnotify(0, "event1", 13, 14, 15)') msg = vim.next_message() - eq(msg[1], 'event1') - eq(msg[2], [13, 14, 15]) + assert msg[1] == 'event1' + assert msg[2] == [13, 14, 15] diff --git a/test/test_tabpage.py b/test/test_tabpage.py index 82bea07f..f98f6aaa 100644 --- a/test/test_tabpage.py +++ b/test/test_tabpage.py @@ -1,39 +1,30 @@ -import os -from nose.tools import with_setup, eq_ as eq, ok_ as ok -from test_common import vim, cleanup - - -@with_setup(setup=cleanup) -def test_windows(): +def test_windows(vim): vim.command('tabnew') vim.command('vsplit') - eq(list(vim.tabpages[0].windows), [vim.windows[0]]) - eq(list(vim.tabpages[1].windows), [vim.windows[1], vim.windows[2]]) - eq(vim.tabpages[1].window, vim.windows[1]) + assert list(vim.tabpages[0].windows) == [vim.windows[0]] + assert list(vim.tabpages[1].windows) == [vim.windows[1], vim.windows[2]] + assert vim.tabpages[1].window == vim.windows[1] vim.current.window = vim.windows[2] - eq(vim.tabpages[1].window, vim.windows[2]) + assert vim.tabpages[1].window == vim.windows[2] -@with_setup(setup=cleanup) -def test_vars(): +def test_vars(vim): vim.current.tabpage.vars['python'] = [1, 2, {'3': 1}] - eq(vim.current.tabpage.vars['python'], [1, 2, {'3': 1}]) - eq(vim.eval('t:python'), [1, 2, {'3': 1}]) + assert vim.current.tabpage.vars['python'] == [1, 2, {'3': 1}] + assert vim.eval('t:python') == [1, 2, {'3': 1}] -@with_setup(setup=cleanup) -def test_valid(): +def test_valid(vim): vim.command('tabnew') tabpage = vim.tabpages[1] - ok(tabpage.valid) + assert tabpage.valid vim.command('tabclose') - ok(not tabpage.valid) + assert not tabpage.valid -@with_setup(setup=cleanup) -def test_number(): +def test_number(vim): curnum = vim.current.tabpage.number vim.command('tabnew') - eq(vim.current.tabpage.number, curnum + 1) + assert vim.current.tabpage.number == curnum + 1 vim.command('tabnew') - eq(vim.current.tabpage.number, curnum + 2) + assert vim.current.tabpage.number == curnum + 2 diff --git a/test/test_vim.py b/test/test_vim.py index df51ba37..dadef5af 100644 --- a/test/test_vim.py +++ b/test/test_vim.py @@ -1,18 +1,18 @@ # -*- coding: utf-8 -*- -import os, sys, tempfile -from nose.tools import with_setup, eq_ as eq, ok_ as ok -from test_common import vim, cleanup +import os +import sys +import tempfile -def source(code): + +def source(vim, code): fd, fname = tempfile.mkstemp() - with os.fdopen(fd,'w') as f: + with os.fdopen(fd, 'w') as f: f.write(code) - vim.command('source '+fname) + vim.command('source ' + fname) os.unlink(fname) -@with_setup(setup=cleanup) -def test_command(): +def test_command(vim): fname = tempfile.mkstemp()[1] vim.command('new') vim.command('edit {}'.format(fname)) @@ -20,152 +20,142 @@ def test_command(): vim.input('\r') vim.command('normal itesting\npython\napi') vim.command('w') - ok(os.path.isfile(fname)) + assert os.path.isfile(fname) with open(fname) as f: - eq(f.read(), 'testing\npython\napi\n') + assert f.read() == 'testing\npython\napi\n' os.unlink(fname) -def test_command_output(): - eq(vim.command_output('echon "test"'), 'test') +def test_command_output(vim): + assert vim.command_output('echo "test"') == 'test' + -@with_setup(setup=cleanup) -def test_eval(): +def test_eval(vim): vim.command('let g:v1 = "a"') vim.command('let g:v2 = [1, 2, {"v3": 3}]') - eq(vim.eval('g:'), {'v1': 'a', 'v2': [1, 2, {'v3': 3}]}) + assert vim.eval('g:'), {'v1': 'a', 'v2': [1, 2 == {'v3': 3}]} + -@with_setup(setup=cleanup) -def test_call(): - eq(vim.funcs.join(['first', 'last'], ', '), 'first, last') - source(""" +def test_call(vim): + assert vim.funcs.join(['first', 'last'], ', '), 'first == last' + source(vim, """ function! Testfun(a,b) return string(a:a).":".a:b endfunction """) - eq(vim.funcs.Testfun(3, 'alpha'), '3:alpha') + assert vim.funcs.Testfun(3, 'alpha') == '3:alpha' -@with_setup(setup=cleanup) -def test_api(): +def test_api(vim): vim.api.command('let g:var = 3') - eq(vim.api.eval('g:var'), 3) + assert vim.api.eval('g:var') == 3 -@with_setup(setup=cleanup) -def test_strwidth(): - eq(vim.strwidth('abc'), 3) +def test_strwidth(vim): + assert vim.strwidth('abc') == 3 # 6 + (neovim) # 19 * 2 (each japanese character occupies two cells) - eq(vim.strwidth('neovimのデザインかなりまともなのになってる。'), 44) + assert vim.strwidth('neovimのデザインかなりまともなのになってる。') == 44 -@with_setup(setup=cleanup) -def test_chdir(): + +def test_chdir(vim): pwd = vim.eval('getcwd()') root = os.path.abspath(os.sep) # We can chdir to '/' on Windows, but then the pwd will be the root drive vim.chdir('/') - eq(vim.eval('getcwd()'), root) + assert vim.eval('getcwd()') == root vim.chdir(pwd) - eq(vim.eval('getcwd()'), pwd) + assert vim.eval('getcwd()') == pwd -@with_setup(setup=cleanup) -def test_current_line(): - eq(vim.current.line, '') +def test_current_line(vim): + assert vim.current.line == '' vim.current.line = 'abc' - eq(vim.current.line, 'abc') + assert vim.current.line == 'abc' -@with_setup(setup=cleanup) -def test_vars(): +def test_vars(vim): vim.vars['python'] = [1, 2, {'3': 1}] - eq(vim.vars['python'], [1, 2, {'3': 1}]) - eq(vim.eval('g:python'), [1, 2, {'3': 1}]) + assert vim.vars['python'], [1, 2 == {'3': 1}] + assert vim.eval('g:python'), [1, 2 == {'3': 1}] -@with_setup(setup=cleanup) -def test_options(): - eq(vim.options['listchars'], 'tab:> ,trail:-,nbsp:+') +def test_options(vim): + assert vim.options['listchars'] == 'tab:> ,trail:-,nbsp:+' vim.options['listchars'] = 'tab:xy' - eq(vim.options['listchars'], 'tab:xy') + assert vim.options['listchars'] == 'tab:xy' -@with_setup(setup=cleanup) -def test_buffers(): +def test_buffers(vim): buffers = [] # Number of elements - eq(len(vim.buffers), 1) + assert len(vim.buffers) == 1 # Indexing (by buffer number) - eq(vim.buffers[vim.current.buffer.number], vim.current.buffer) + assert vim.buffers[vim.current.buffer.number] == vim.current.buffer buffers.append(vim.current.buffer) vim.command('new') - eq(len(vim.buffers), 2) + assert len(vim.buffers) == 2 buffers.append(vim.current.buffer) - eq(vim.buffers[vim.current.buffer.number], vim.current.buffer) + assert vim.buffers[vim.current.buffer.number] == vim.current.buffer vim.current.buffer = buffers[0] - eq(vim.buffers[vim.current.buffer.number], buffers[0]) + assert vim.buffers[vim.current.buffer.number] == buffers[0] # Membership test - ok(buffers[0] in vim.buffers) - ok(buffers[1] in vim.buffers) - ok({} not in vim.buffers) + assert buffers[0] in vim.buffers + assert buffers[1] in vim.buffers + assert {} not in vim.buffers # Iteration - eq(buffers, list(vim.buffers)) + assert buffers == list(vim.buffers) -@with_setup(setup=cleanup) -def test_windows(): - eq(len(vim.windows), 1) - eq(vim.windows[0], vim.current.window) +def test_windows(vim): + assert len(vim.windows) == 1 + assert vim.windows[0] == vim.current.window vim.command('vsplit') vim.command('split') - eq(len(vim.windows), 3) - eq(vim.windows[0], vim.current.window) + assert len(vim.windows) == 3 + assert vim.windows[0] == vim.current.window vim.current.window = vim.windows[1] - eq(vim.windows[1], vim.current.window) + assert vim.windows[1] == vim.current.window -@with_setup(setup=cleanup) -def test_tabpages(): - eq(len(vim.tabpages), 1) - eq(vim.tabpages[0], vim.current.tabpage) +def test_tabpages(vim): + assert len(vim.tabpages) == 1 + assert vim.tabpages[0] == vim.current.tabpage vim.command('tabnew') - eq(len(vim.tabpages), 2) - eq(len(vim.windows), 2) - eq(vim.windows[1], vim.current.window) - eq(vim.tabpages[1], vim.current.tabpage) + assert len(vim.tabpages) == 2 + assert len(vim.windows) == 2 + assert vim.windows[1] == vim.current.window + assert vim.tabpages[1] == vim.current.tabpage vim.current.window = vim.windows[0] # Switching window also switches tabpages if necessary(this probably # isn't the current behavior, but compatibility will be handled in the # python client with an optional parameter) - eq(vim.tabpages[0], vim.current.tabpage) - eq(vim.windows[0], vim.current.window) + assert vim.tabpages[0] == vim.current.tabpage + assert vim.windows[0] == vim.current.window vim.current.tabpage = vim.tabpages[1] - eq(vim.tabpages[1], vim.current.tabpage) - eq(vim.windows[1], vim.current.window) + assert vim.tabpages[1] == vim.current.tabpage + assert vim.windows[1] == vim.current.window -@with_setup(setup=cleanup) -def test_hash(): +def test_hash(vim): d = {} d[vim.current.buffer] = "alpha" - eq(d[vim.current.buffer], "alpha") + assert d[vim.current.buffer] == 'alpha' vim.command('new') d[vim.current.buffer] = "beta" - eq(d[vim.current.buffer], "beta") + assert d[vim.current.buffer] == 'beta' vim.command('winc w') - eq(d[vim.current.buffer], "alpha") + assert d[vim.current.buffer] == 'alpha' vim.command('winc w') - eq(d[vim.current.buffer], "beta") + assert d[vim.current.buffer] == 'beta' -@with_setup(setup=cleanup) -def test_cwd(): +def test_cwd(vim, tmpdir): pycmd = 'python' if sys.version_info >= (3, 0): pycmd = 'python3' @@ -173,9 +163,8 @@ def test_cwd(): vim.command('{} import os'.format(pycmd)) cwd_before = vim.command_output('{} print(os.getcwd())'.format(pycmd)) - vim.command('cd test') + vim.command('cd {}'.format(tmpdir.strpath)) cwd_vim = vim.command_output('pwd') cwd_python = vim.command_output('{} print(os.getcwd())'.format(pycmd)) - eq(cwd_vim, cwd_python) - ok(cwd_python != cwd_before) - + assert cwd_python == cwd_vim + assert cwd_python != cwd_before diff --git a/test/test_window.py b/test/test_window.py index 23e2b3d8..9afa0db7 100644 --- a/test/test_window.py +++ b/test/test_window.py @@ -1,119 +1,102 @@ -import os -from nose.tools import with_setup, eq_ as eq, ok_ as ok -from test_common import vim, cleanup - - -@with_setup(setup=cleanup) -def test_buffer(): - eq(vim.current.buffer, vim.windows[0].buffer) +def test_buffer(vim): + assert vim.current.buffer == vim.windows[0].buffer vim.command('new') vim.current.window = vim.windows[1] - eq(vim.current.buffer, vim.windows[1].buffer) - ok(vim.windows[0].buffer != vim.windows[1].buffer) + assert vim.current.buffer == vim.windows[1].buffer + assert vim.windows[0].buffer != vim.windows[1].buffer -@with_setup(setup=cleanup) -def test_cursor(): - eq(vim.current.window.cursor, [1, 0]) +def test_cursor(vim): + assert vim.current.window.cursor == [1, 0] vim.command('normal ityping\033o some text') - eq(vim.current.buffer[:], ['typing', ' some text']) - eq(vim.current.window.cursor, [2, 10]) + assert vim.current.buffer[:] == ['typing', ' some text'] + assert vim.current.window.cursor == [2, 10] vim.current.window.cursor = [2, 6] vim.command('normal i dumb') - eq(vim.current.buffer[:], ['typing', ' some dumb text']) + assert vim.current.buffer[:] == ['typing', ' some dumb text'] -@with_setup(setup=cleanup) -def test_height(): +def test_height(vim): vim.command('vsplit') - eq(vim.windows[1].height, vim.windows[0].height) + assert vim.windows[1].height == vim.windows[0].height vim.current.window = vim.windows[1] vim.command('split') - eq(vim.windows[1].height, vim.windows[0].height // 2) + assert vim.windows[1].height == vim.windows[0].height // 2 vim.windows[1].height = 2 - eq(vim.windows[1].height, 2) + assert vim.windows[1].height == 2 -@with_setup(setup=cleanup) -def test_width(): +def test_width(vim): vim.command('split') - eq(vim.windows[1].width, vim.windows[0].width) + assert vim.windows[1].width == vim.windows[0].width vim.current.window = vim.windows[1] vim.command('vsplit') - eq(vim.windows[1].width, vim.windows[0].width // 2) + assert vim.windows[1].width == vim.windows[0].width // 2 vim.windows[1].width = 2 - eq(vim.windows[1].width, 2) + assert vim.windows[1].width == 2 -@with_setup(setup=cleanup) -def test_vars(): +def test_vars(vim): vim.current.window.vars['python'] = [1, 2, {'3': 1}] - eq(vim.current.window.vars['python'], [1, 2, {'3': 1}]) - eq(vim.eval('w:python'), [1, 2, {'3': 1}]) + assert vim.current.window.vars['python'] == [1, 2, {'3': 1}] + assert vim.eval('w:python') == [1, 2, {'3': 1}] -@with_setup(setup=cleanup) -def test_options(): +def test_options(vim): vim.current.window.options['colorcolumn'] = '4,3' - eq(vim.current.window.options['colorcolumn'], '4,3') + assert vim.current.window.options['colorcolumn'] == '4,3' # global-local option vim.current.window.options['statusline'] = 'window-status' - eq(vim.current.window.options['statusline'], 'window-status') - eq(vim.options['statusline'], '') + assert vim.current.window.options['statusline'] == 'window-status' + assert vim.options['statusline'] == '' -@with_setup(setup=cleanup) -def test_position(): +def test_position(vim): height = vim.windows[0].height width = vim.windows[0].width vim.command('split') vim.command('vsplit') - eq((vim.windows[0].row, vim.windows[0].col), (0, 0)) + assert (vim.windows[0].row, vim.windows[0].col) == (0, 0) vsplit_pos = width / 2 split_pos = height / 2 - eq(vim.windows[1].row, 0) - ok(vsplit_pos - 1 <= vim.windows[1].col <= vsplit_pos + 1) - ok(split_pos - 1 <= vim.windows[2].row <= split_pos + 1) - eq(vim.windows[2].col, 0) + assert vim.windows[1].row == 0 + assert vsplit_pos - 1 <= vim.windows[1].col <= vsplit_pos + 1 + assert split_pos - 1 <= vim.windows[2].row <= split_pos + 1 + assert vim.windows[2].col == 0 -@with_setup(setup=cleanup) -def test_tabpage(): +def test_tabpage(vim): vim.command('tabnew') vim.command('vsplit') - eq(vim.windows[0].tabpage, vim.tabpages[0]) - eq(vim.windows[1].tabpage, vim.tabpages[1]) - eq(vim.windows[2].tabpage, vim.tabpages[1]) + assert vim.windows[0].tabpage == vim.tabpages[0] + assert vim.windows[1].tabpage == vim.tabpages[1] + assert vim.windows[2].tabpage == vim.tabpages[1] -@with_setup(setup=cleanup) -def test_valid(): +def test_valid(vim): vim.command('split') window = vim.windows[1] vim.current.window = window - ok(window.valid) + assert window.valid vim.command('q') - ok(not window.valid) + assert not window.valid -@with_setup(setup=cleanup) -def test_number(): +def test_number(vim): curnum = vim.current.window.number vim.command('bot split') - eq(vim.current.window.number, curnum + 1) + assert vim.current.window.number == curnum + 1 vim.command('bot split') - eq(vim.current.window.number, curnum + 2) + assert vim.current.window.number == curnum + 2 -@with_setup(setup=cleanup) -def test_handle(): +def test_handle(vim): hnd1 = vim.current.window.handle vim.command('bot split') hnd2 = vim.current.window.handle - ok(hnd2 != hnd1) + assert hnd2 != hnd1 vim.command('bot split') hnd3 = vim.current.window.handle - ok(hnd3 != hnd1) - ok(hnd3 != hnd2) + assert hnd1 != hnd2 != hnd3 vim.command('wincmd w') - eq(vim.current.window.handle,hnd1) + assert vim.current.window.handle == hnd1 diff --git a/tox.ini b/tox.ini index 30ff8c76..241f7de2 100644 --- a/tox.ini +++ b/tox.ini @@ -5,9 +5,10 @@ envlist = [testenv] deps= - nose + pytest + pytest-xdist pyuv: pyuv -commands=nosetests +commands=python -m pytest -s [testenv:checkqa] deps =