summaryrefslogtreecommitdiff
path: root/test/python/test_deoplete_source.py
blob: 280df2e35536ebf90bedd3eddbdfe8d8ce31fdf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import unittest
import imp

ale_module = imp.load_source(
    'deoplete.sources.ale',
    '/testplugin/rplugin/python3/deoplete/sources/ale.py',
)


class VimMock(object):
    def __init__(self, call_list, call_results):
        self.__call_list = call_list
        self.__call_results = call_results

    def call(self, function, *args):
        self.__call_list.append((function, args))

        return self.__call_results.get(function, 0)


class DeopleteSourceTest(unittest.TestCase):
    def setUp(self):
        super(DeopleteSourceTest, self).setUp()

        self.call_list = []
        self.call_results = {'ale#completion#CanProvideCompletions': 1}
        self.source = ale_module.Source('vim')
        self.source.vim = VimMock(self.call_list, self.call_results)

    def test_attributes(self):
        """
        Check all of the attributes we set.
        """
        attributes = dict(
            (key, getattr(self.source, key))
            for key in
            dir(self.source)
            if not key.startswith('__')
            and key != 'vim'
            and not hasattr(getattr(self.source, key), '__self__')
        )

        self.assertEqual(attributes, {
            'input_patterns': {
                '_': r'\.\w*$',
                'rust': r'(\.|::)\w*$',
                'typescript': r'(\.|\'|")\w*$',
            },
            'is_bytepos': True,
            'mark': '[L]',
            'min_pattern_length': 1,
            'name': 'ale',
            'rank': 1000,
        })

    def test_complete_position(self):
        self.call_results['ale#completion#GetCompletionPositionForDeoplete'] = 2
        context = {'input': 'foo'}

        self.assertEqual(self.source.get_complete_position(context), 2)
        self.assertEqual(self.call_list, [
            ('ale#completion#GetCompletionPositionForDeoplete', ('foo',)),
        ])

    def test_request_completion_results(self):
        context = {'is_async': False}

        self.assertEqual(self.source.gather_candidates(context), [])
        self.assertEqual(context, {'is_async': True})
        self.assertEqual(self.call_list, [
            ('ale#completion#CanProvideCompletions', ()),
            ('ale#completion#GetCompletions', ('deoplete',)),
        ])

    def test_request_completion_results_from_buffer_without_providers(self):
        self.call_results['ale#completion#CanProvideCompletions'] = 0
        context = {'is_async': False}

        self.assertIsNone(self.source.gather_candidates(context), [])
        self.assertEqual(context, {'is_async': False})
        self.assertEqual(self.call_list, [
            ('ale#completion#CanProvideCompletions', ()),
        ])

    def test_refresh_completion_results(self):
        context = {'is_async': False}

        self.assertEqual(self.source.gather_candidates(context), [])
        self.assertEqual(context, {'is_async': True})
        self.assertEqual(self.call_list, [
            ('ale#completion#CanProvideCompletions', ()),
            ('ale#completion#GetCompletions', ('deoplete',)),
        ])

        context = {'is_async': True, 'is_refresh': True}

        self.assertEqual(self.source.gather_candidates(context), [])
        self.assertEqual(context, {'is_async': True, 'is_refresh': True})
        self.assertEqual(self.call_list, [
            ('ale#completion#CanProvideCompletions', ()),
            ('ale#completion#GetCompletions', ('deoplete',)),
            ('ale#completion#CanProvideCompletions', ()),
            ('ale#completion#GetCompletions', ('deoplete',)),
        ])

    def test_poll_no_result(self):
        context = {'is_async': True}
        self.call_results['ale#completion#GetCompletionResult'] = None

        self.assertEqual(self.source.gather_candidates(context), [])
        self.assertEqual(context, {'is_async': True})
        self.assertEqual(self.call_list, [
            ('ale#completion#CanProvideCompletions', ()),
            ('ale#completion#GetCompletionResult', ()),
        ])

    def test_poll_empty_result_ready(self):
        context = {'is_async': True}
        self.call_results['ale#completion#GetCompletionResult'] = []

        self.assertEqual(self.source.gather_candidates(context), [])
        self.assertEqual(context, {'is_async': False})
        self.assertEqual(self.call_list, [
            ('ale#completion#CanProvideCompletions', ()),
            ('ale#completion#GetCompletionResult', ()),
        ])

    def test_poll_non_empty_result_ready(self):
        context = {'is_async': True}
        self.call_results['ale#completion#GetCompletionResult'] = [
            {
                'word': 'foobar',
                'kind': 'v',
                'icase': 1,
                'menu': '',
                'info': '',
            },
        ]

        self.assertEqual(self.source.gather_candidates(context), [
            {
                'word': 'foobar',
                'kind': 'v',
                'icase': 1,
                'menu': '',
                'info': '',
            },
        ])
        self.assertEqual(context, {'is_async': False})
        self.assertEqual(self.call_list, [
            ('ale#completion#CanProvideCompletions', ()),
            ('ale#completion#GetCompletionResult', ()),
        ])