def test_pyfile(): fname = 'pyfile.py' text = 'vim.command("let set_by_pyfile = 123")' with open(fname, 'w') as f: f.write(text) vim.command('pyfile pyfile.py') eq(vim.vars['set_by_pyfile'], 123) os.unlink(fname)
def test_pydo(): # insert some text vim.command('normal iabc\ndef\nghi') eq(vim.current.buffer[:], ['abc', 'def', 'ghi']) # go to top and select and replace the first two lines vim.command('normal ggvj:pydo return linenr\n') eq(vim.current.buffer[:], ['1', '2', 'ghi'])
def test_valid(): vim.command('split') window = vim.windows[1] vim.current.window = window ok(window.valid) vim.command('q') ok(not window.valid)
def test_buffers(): eq(len(vim.buffers), 1) eq(vim.buffers[0], vim.current.buffer) vim.command('new') eq(len(vim.buffers), 2) eq(vim.buffers[1], vim.current.buffer) vim.current.buffer = vim.buffers[0] eq(vim.buffers[0], vim.current.buffer)
def test_height(): vim.command('vsplit') eq(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) vim.windows[1].height = 2 eq(vim.windows[1].height, 2)
def test_width(): vim.command('split') eq(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) vim.windows[1].width = 2 eq(vim.windows[1].width, 2)
def test_windows(): 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]) vim.current.window = vim.windows[2] eq(vim.tabpages[1].window, vim.windows[2])
def test_cursor(): eq(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]) vim.current.window.cursor = [2, 6] vim.command('normal i dumb') eq(vim.current.buffer[:], ['typing', ' some dumb text'])
def test_windows(): eq(len(vim.windows), 1) eq(vim.windows[0], vim.current.window) vim.command('vsplit') vim.command('split') eq(len(vim.windows), 3) eq(vim.windows[0], vim.current.window) vim.current.window = vim.windows[1] eq(vim.windows[1], vim.current.window)
def test_name(): vim.command("new") eq(vim.current.buffer.name, "") new_name = vim.eval("tempname()") vim.current.buffer.name = new_name eq(vim.current.buffer.name, new_name) vim.command("w!") ok(os.path.isfile(new_name)) os.unlink(new_name)
def test_name(): vim.command('new') eq(vim.current.buffer.name, '') new_name = vim.eval('resolve(tempname())') vim.current.buffer.name = new_name eq(vim.current.buffer.name, new_name) vim.command('silent w!') ok(os.path.isfile(new_name)) os.unlink(new_name)
def test_broadcast(): vim.subscribe('event2') vim.command('call send_event(0, "event1", [1,2,3])') vim.command('call send_event(0, "event2", [4,5,6])') vim.command('call send_event(0, "event2", [7,8,9])') events = vim.expect('event2', 2, lambda e: e[1][0] == 7) eq(events, [['event2', [4, 5, 6]], ['event2', [7, 8, 9]]]) vim.unsubscribe('event2') vim.subscribe('event1') vim.command('call send_event(0, "event2", [10,11,12])') vim.command('call send_event(0, "event1", [13,14,15])') eq(vim.next_event(), ['event1', [13, 14, 15]])
def request_cb(name, args): n = args[0] n *= 2 if n <= 16: if n == 4: cmd = 'let g:result2 = rpcrequest(%d, "call", %d)' % (cid, n,) elif n == 8: cmd = 'let g:result3 = rpcrequest(%d, "call", %d)' % (cid, n,) elif n == 16: cmd = 'let g:result4 = rpcrequest(%d, "call", %d)' % (cid, n,) vim.command(cmd) return n
def test_position(): 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)) 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)
def setup_cb(): vim.vars['result1'] = 0 vim.vars['result2'] = 0 vim.vars['result3'] = 0 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) vim.session.stop()
def notification_cb(name, args): eq(name, 'setup3') vim.vars['result1'] = 0 vim.vars['result2'] = 0 vim.vars['result3'] = 0 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) vim.session.stop()
def notification_cb(name, args): eq(name, 'setup3') vim.vars['result1'] = 0 vim.vars['result2'] = 0 vim.vars['result3'] = 0 vim.vars['result4'] = 0 cmd = 'let g:result1 = send_call(%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) vim.loop_stop()
def test_command(): fname = tempfile.mkstemp()[1] vim.command("new") vim.command("edit %s" % fname) vim.command("normal itesting\npython\napi") vim.command("w") ok(os.path.isfile(fname)) eq(file(fname).read(), "testing\npython\napi\n") os.unlink(fname)
def test_command(): fname = tempfile.mkstemp()[1] vim.command('new') vim.command('edit %s' % fname) vim.command('normal itesting\npython\napi') vim.command('w') ok(os.path.isfile(fname)) eq(file(fname).read(), 'testing\npython\napi\n') os.unlink(fname)
def test_tabpages(): eq(len(vim.tabpages), 1) eq(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) 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) vim.current.tabpage = vim.tabpages[1] eq(vim.tabpages[1], vim.current.tabpage) eq(vim.windows[1], vim.current.window)
def host_setup(): cleanup() # Spawn the python host vim.command( 'let pyhost_id = ' + 'rpcstart("python", ["-c", "import neovim; neovim.start_host()"])') ok(vim.eval('g:pyhost_id')) # Use rpc_request to wait for the host setup(rpc_spawn will return a channel # id but only after a while the channel will register handlers for python_* # methods) ok(vim.eval('rpcrequest(g:pyhost_id, "python_eval", "10")') == 10) # Verify the feature ok(vim.eval('has("python")')) # Import the vim module vim.command('python import vim') # Ensure the python host was updated accordingly ok(vim.eval('pyeval("vim.channel_id") == g:pyhost_id'))
def test_broadcast(): 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.session.next_message() eq(event[1], 'event2') eq(event[2], [4, 5, 6]) event = vim.session.next_message() eq(event[1], 'event2') eq(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.session.next_message() eq(msg[1], 'event1') eq(msg[2], [13, 14, 15])
def test_broadcast(): vim.subscribe('event2') vim.command('call send_event(0, "event1", [1,2,3])') vim.command('call send_event(0, "event2", [4,5,6])') vim.command('call send_event(0, "event2", [7,8,9])') event = vim.next_message() eq(event.name, 'event2') eq(event.arg, [4, 5, 6]) event = vim.next_message() eq(event.name, 'event2') eq(event.arg, [7, 8, 9]) vim.unsubscribe('event2') vim.subscribe('event1') vim.command('call send_event(0, "event2", [10,11,12])') vim.command('call send_event(0, "event1", [13,14,15])') msg = vim.next_message() eq(msg.name, 'event1') eq(msg.arg, [13, 14, 15])
def test_command(): fname = tempfile.mkstemp()[1] vim.command('new') vim.command('edit %s' % fname) # skip the "press return" state, which does not handle deferred calls vim.input('\r') vim.command('normal itesting\npython\napi') vim.command('w') ok(os.path.isfile(fname)) eq(open(fname).read(), 'testing\npython\napi\n') os.unlink(fname)
def test_receiving_events(): vim.command('call send_event(%d, "test-event", [1,2,3])' % vim.channel_id) events = vim.expect('test-event', lambda e: set(e[1]) == set([1, 2, 3])) eq(events, [['test-event', [1, 2, 3]]]) vim.command('au FileType python call send_event(%d, "py!", bufnr("$"))' % vim.channel_id) vim.command('set filetype=python') events = vim.expect('py!', lambda e: e[1] == vim.current.buffer.number) eq(events, [['py!', vim.current.buffer.number]])
def test_receiving_events(): vim.command('call send_event(%d, "test-event", [1,2,3])' % vim.channel_id) event = vim.next_message() eq(event.name, 'test-event') eq(event.arg, [1, 2, 3]) vim.command('au FileType python call send_event(%d, "py!", bufnr("$"))' % vim.channel_id) vim.command('set filetype=python') event = vim.next_message() eq(event.name, 'py!') eq(event.arg, vim.current.buffer.number)
def test_receiving_events(): vim.command('call rpcnotify(%d, "test-event", 1, 2, 3)' % vim.channel_id) event = vim.session.next_message() eq(event[1], 'test-event') eq(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.session.next_message() eq(event[1], 'py!') eq(event[2], [vim.current.buffer.number])
def test_hash(): d = {} d[vim.current.buffer] = "alpha" eq(d[vim.current.buffer], "alpha") vim.command('new') d[vim.current.buffer] = "beta" eq(d[vim.current.buffer], "beta") vim.command('winc w') eq(d[vim.current.buffer], "alpha") vim.command('winc w') eq(d[vim.current.buffer], "beta")
def test_sending_notify(): # 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.session.next_message() eq(event[1], 'test-event') eq(event[2], [3]) # request after notify vim.command("let g:data = 'xyz'", async=True) eq(vim.eval('g:data'), 'xyz')
def test_pydo(): # :pydo 42 returns None for all lines, # the buffer should not be changed vim.command('normal :pydo 42\n') eq(vim.current.buffer.options['mod'], False) # insert some text vim.command('normal iabc\ndef\nghi') eq(vim.current.buffer[:], ['abc', 'def', 'ghi']) # go to top and select and replace the first two lines vim.command('normal ggvj:pydo return str(linenr)\n') eq(vim.current.buffer[:], ['1', '2', 'ghi'])
def request_cb(name, args): vim.command('let g:result2 = [7, 8, 9]') return vim.vars['result2']
def source(code): fd, fname = tempfile.mkstemp() with os.fdopen(fd, 'w') as f: f.write(code) vim.command('source ' + fname) os.unlink(fname)
def test_eval(): 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}]})