Completed
Push — master ( 63635d...401a68 )
by timothy
01:14
created

quit()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
'''
2
Neovim ui api is likely to change,
3
also i do not understand really how and what it tries to do,
4
it feels very granular and clunky.
5
Makes it hard to do unit testing.
6
Focuising on Integration testing...
7
'''
8
9
import os
10
import time
11
import _thread as thread
12
import queue
13
from itertools import count
14
import tkinter as tk
15
16
import pytest
17
18
from pytknvim.tk_ui import NvimTk
19
from pytknvim.util import rand_str, attach_headless
20
from pytknvim.tests.util import compare_screens, send_tk_key
21
from pytknvim.tests.util import STATUS_BAR_HEIGHT, MAX_SCROLL, BUFFER_NUM
22
23
24
def threaded_nvimtk(address, out_queue):
25
    root = tk.Tk()
26
    text = NvimTk(root)
27
    text.nvim_connect('-u', 'NONE', headless=True, address=address)
28
    text.pack()
29
30
    out_queue.put(text)
31
32
    def quit():
33
        root.after_idle(root.quit)
34
    text.quit = quit
35
    root.mainloop()
36
37
def thread_ui():
38
    '''starts our us threaded so we can run tests
39
    this only works if we create the root in the same thread we
40
    call mainloop frome'''
41
    # scroll test will always check scrolling
42
    named_pipe = '/tmp/nvim{0}'.format(rand_str(16))
43
    out_queue = queue.Queue()
44
    thread.start_new_thread(threaded_nvimtk, (named_pipe, out_queue))
45
    nvimtk = out_queue.get(block=True)
46
    time.sleep(1)
47
    test_nvim = attach_headless(path=named_pipe)
48
    time.sleep(1)
49
    return nvimtk, test_nvim
50
51
52
class VimCommands():
53
    '''
54
    Just for readablility
55
    '''
56
    def v_insert_mode(self):
57
        self.send_tk_key('i')
58
59
    def v_normal_mode(self):
60
        self.send_tk_key('Esc')
61
62
    def v_back(self):
63
        self.v_normal_mode()
64
        self.send_tk_key('b')
65
66
    def v_delete_line(self):
67
        self.v_normal_mode()
68
        self.send_tk_key('d')
69
        self.send_tk_key('d')
70
71
    def v_up(self):
72
        self.v_normal_mode()
73
        self.send_tk_key('k')
74
75
    def v_down(self):
76
        self.v_normal_mode()
77
        self.send_tk_key('j')
78
79
    def v_undo(self):
80
        self.v_normal_mode()
81
        self.send_tk_key('u')
82
83
    def v_page_down(self):
84
        self.v_normal_mode()
85
        self.send_tk_key('G')
86
87
    def v_page_up(self):
88
        self.v_normal_mode()
89
        self.send_tk_key('g')
90
        self.send_tk_key('g')
91
92
    def v_jump(self, pos):
93
        self.v_normal_mode()
94
        self.send_tk_key(*"{}G".format(pos))
95
        self.send_tk_key('Enter')
96
97
98
class TestIntegration(VimCommands):
99
100
    def setup_class(cls):
101
        cls.nvimtk, cls.test_nvim = thread_ui()
102
        cls.nvimtk.test_nvim = cls.test_nvim
103
        cls.nvimtk._screen = cls.nvimtk.nvim_handler._screen
104
        cls.nvimtk.text = cls.nvimtk
105
106
        cls.nvimtk.max_scroll = MAX_SCROLL
107
        # This one has to be used because of threads and locks
108
        cls.nvim = cls.nvimtk.test_nvim
109
        # TODO Our compare_screen function doesn't work with number set
110
        cls.test_nvim.command("set nonumber")
111
        cls.test_nvim.command("set norelativenumber")
112
        cls.test_nvim.command("set noswapfile")
113
114
    def teardown_class(cls):
115
        cls.nvimtk.quit()
116
        time.sleep(0.2)
117
118
    def teardown_method(self, method):
119
        '''delete everything so we get a clean slate'''
120
        self.send_tk_key('Esc')
121
        buf = self.nvimtk.test_nvim.buffers[BUFFER_NUM]
122
        buf[:] = [""]
123
124
125
    def send_tk_key(self, *keys, modifyers=None):
126
        for key in keys:
127
            mod = None
128
            if type(key) in (tuple, list):
129
                key, mod = key
130
            send_tk_key(self.nvimtk, key, mod)
131
132
    def compare_screens(self):
133
        compare_screens(self.nvimtk)
134
135
    @pytest.mark.simple
136
    def test_load(self):
137
        self.compare_screens()
138
139
    @pytest.mark.simple
140
    def test_basic_insert(self):
141
        self.v_insert_mode()
142
        self.compare_screens()
143
        self.send_tk_key('a')
144
        self.compare_screens()
145
        self.send_tk_key('b', 'c', 'd', 'e')
146
        self.compare_screens()
147
148
149
    @pytest.mark.simple
150
    def test_enter_key(self):
151
        self.v_insert_mode()
152
        self.send_tk_key('b', 'c', 'd', 'e')
153
        self.send_tk_key('Enter')
154
        self.send_tk_key('Enter')
155
        self.compare_screens()
156
        self.send_tk_key('f', 'g', 'h')
157
        self.compare_screens()
158
        self.v_back()
159
        self.v_insert_mode()
160
        self.send_tk_key('1','2','3')
161
        self.send_tk_key('Enter')
162
        self.compare_screens()
163
164
165
    @pytest.mark.simple
166
    def test_delete_line(self):
167
        self.v_insert_mode()
168
        self.send_tk_key('o', 'n', 'e')
169
        self.v_delete_line()
170
        self.compare_screens()
171
        self.v_insert_mode()
172
        self.send_tk_key('o', 'n', 'e')
173
        self.send_tk_key('Enter')
174
        self.send_tk_key('t', 'w', 'o')
175
        self.send_tk_key('Enter')
176
        self.send_tk_key('t', 'h', 'r', 'e', 'e')
177
        self.compare_screens()
178
        self.v_delete_line()
179
        self.compare_screens()
180
        self.v_undo()
181
        self.compare_screens()
182
183
184
    def test_o_and_shift_o(self):
185
        self.v_insert_mode()
186
        self.send_tk_key('1')
187
        self.v_normal_mode()
188
        # Open onto new line
189
        self.send_tk_key('o')
190
        self.send_tk_key('3')
191
        self.compare_screens()
192
        self.v_normal_mode()
193
        #  Open onto previous line
194
        self.send_tk_key('O')
195
        self.send_tk_key('2')
196
        self.compare_screens()
197
        # The end state of this tests should be
198
        # 1
199
        # 2
200
        #
201
        # 3
202
203
204
    # @nvimtester.register_test
205
    def test_scroll(self):
206
        # Force a scroll of an amount then compare_screens
207
        def _do(to_top):
208
            self.compare_screens()
209
            for i in range(0, to_top):
210
                self.v_up()
211
            self.compare_screens()
212
            for i in range(0, to_top):
213
                self.v_down()
214
            self.compare_screens()
215
216
        # TODO GET THIS DYNAMICALLY
217
        for i in count(1):
218
            self.v_page_down()
219
            self.v_insert_mode()
220
            scrolled = i\
221
                       - self.nvimtk.nvim_handler.current_rows\
222
                       + STATUS_BAR_HEIGHT
223
            self.send_tk_key(*str(i-1))
224
            self.send_tk_key('Enter')
225
226
            self.nvimtk.max_scroll = 50
227
            if scrolled in (1, 2, self.nvimtk.max_scroll):
228
                to_top = self.nvimtk.nvim_handler.current_rows + scrolled
229
                _do(to_top)
230
            if scrolled == self.nvimtk.max_scroll:
231
                break
232
233
234
    def test_page_up_down(self):
235
        def _do(to_top):
236
            self.compare_screens()
237
            self.v_page_up()
238
            self.compare_screens()
239
            self.v_page_down()
240
            self.compare_screens()
241
242
        for i in count(1):
243
            self.v_page_down()
244
            self.v_insert_mode()
245
            scrolled = i\
246
                       - self.nvimtk.nvim_handler.current_rows\
247
                       + STATUS_BAR_HEIGHT
248
            self.send_tk_key(*str(i-1))
249
            self.send_tk_key('Enter')
250
251
            # Make sure our last jump covers an entire page
252
            last_scroll = old_scroll = self.nvimtk.max_scroll
253
            if last_scroll < self.nvimtk.nvim_handler.current_rows:
254
                last_scroll = self.nvimtk.nvim_handler.current_rows + 1
255
                self.nvimtk.max_scroll = last_scroll
256
257
            if scrolled in (1, 2, last_scroll):
258
                to_top = self.nvimtk.nvim_handler.current_rows + scrolled
259
                time.sleep(1)
260
                _do(to_top)
261
            if scrolled == last_scroll:
262
                self.nvimtk.max_scroll = old_scroll
263
                break
264
265
266
    def test_big_file(self):
267
        '''
268
        doesn't test anything that our other tests doesn't,
269
        but just paves the way for testing a file
270
        '''
271
        test_file = os.path.join(os.path.dirname(__file__),
272
                                 'test_file')
273
        self.v_normal_mode()
274
        self.send_tk_key(*':e! '+test_file)
275
        self.send_tk_key('Enter')
276
        # Give time for actions to take place
277
        time.sleep(1)
278
        self.compare_screens()
279
        # TODO READ LEN OF FILE
280
        old_max = self.nvimtk.max_scroll
281
        self.nvimtk.max_scroll = 500
282
        self.v_page_down()
283
        time.sleep(0.5)
284
        self.compare_screens()
285
        self.v_page_up()
286
        time.sleep(0.5)
287
        self.compare_screens()
288
        self.nvimtk.max_sroll = old_max
289
        self.nvim.command('bwipeout')
290
291
292
    def test_number(self):
293
        to_test = ('load', 'basic_insert', 'enter_key')
294
        self.nvim.command("set nonumber")
295
        self.nvim.command("set relativenumber")
296
        time.sleep(0.5)
297
        for test in to_test:
298
            method = getattr(self, 'test_' + test)
299
            method()
300
            self.teardown_method(method)
301