Issues (18)

test/test_client_rpc.py (1 issue)

Severity
1
# -*- coding: utf-8 -*-
2
import time
3
4
5
def test_call_and_reply(vim):
6
    cid = vim.channel_id
7
8
    def setup_cb():
9
        cmd = 'let g:result = rpcrequest(%d, "client-call", 1, 2, 3)' % cid
10
        vim.command(cmd)
11
        assert vim.vars['result'] == [4, 5, 6]
12
        vim.stop_loop()
13
14
    def request_cb(name, args):
15
        assert name == 'client-call'
16
        assert args == [1, 2, 3]
17
        return [4, 5, 6]
18
19
    vim.run_loop(request_cb, None, setup_cb)
20
21
22
def test_call_api_before_reply(vim):
23
    cid = vim.channel_id
24
25
    def setup_cb():
26
        cmd = 'let g:result = rpcrequest(%d, "client-call2", 1, 2, 3)' % cid
27
        vim.command(cmd)
28
        assert vim.vars['result'] == [7, 8, 9]
29
        vim.stop_loop()
30
31
    def request_cb(name, args):
32
        vim.command('let g:result2 = [7, 8, 9]')
33
        return vim.vars['result2']
34
35
    vim.run_loop(request_cb, None, setup_cb)
36
37
38
def test_async_call(vim):
39
40
    def request_cb(name, args):
41
        if name == "test-event":
42
            vim.vars['result'] = 17
43
        vim.stop_loop()
44
45
    # this would have dead-locked if not async
46
    vim.funcs.rpcrequest(vim.channel_id, "test-event", async_=True)
47
    vim.run_loop(request_cb, None, None)
48
49
    # TODO(blueyed): This sleep is required on Travis, where it hangs with
50
    # "Entering event loop" otherwise  (asyncio's EpollSelector._epoll.poll).
51
    time.sleep(0.1)
52
53
    assert vim.vars['result'] == 17
54
55
56
def test_recursion(vim):
57
    cid = vim.channel_id
58
59
    def setup_cb():
60
        vim.vars['result1'] = 0
61
        vim.vars['result2'] = 0
62
        vim.vars['result3'] = 0
63
        vim.vars['result4'] = 0
64
        cmd = 'let g:result1 = rpcrequest(%d, "call", %d)' % (cid, 2,)
65
        vim.command(cmd)
66
        assert vim.vars['result1'] == 4
67
        assert vim.vars['result2'] == 8
68
        assert vim.vars['result3'] == 16
69
        assert vim.vars['result4'] == 32
70
        vim.stop_loop()
71
72
    def request_cb(name, args):
73
        n = args[0]
74
        n *= 2
75
        if n <= 16:
76
            if n == 4:
77
                cmd = 'let g:result2 = rpcrequest(%d, "call", %d)' % (cid, n,)
78
            elif n == 8:
79
                cmd = 'let g:result3 = rpcrequest(%d, "call", %d)' % (cid, n,)
80
            elif n == 16:
81
                cmd = 'let g:result4 = rpcrequest(%d, "call", %d)' % (cid, n,)
82
            vim.command(cmd)
0 ignored issues
show
The variable cmd does not seem to be defined for all execution paths.
Loading history...
83
        return n
84
85
    vim.run_loop(request_cb, None, setup_cb)
86