Debian Codegraph @

tests/functional_tests/test_keymap_vscode.py

raw
1 from __future__ import annotations
2
3 from textwrap import dedent
4 from typing import Awaitable, Callable
5 from unittest.mock import MagicMock
6
7 import pytest
8 from textual.css.query import NoMatches
9
10 from harlequin import Harlequin
11
12 QUERY = dedent(
13 """
14 select *
15 from
16 (
17 values
18 (1, 2, 3),
19 (4, 5, 6),
20 (7, 8, 9),
21 (10, 11, 12),
22 (13, 14, 15),
23 (16, 17, 18),
24 (19, 20, 21)
25 ) foo(a, b, c)
26 """
27 ).strip()
28
29
30 @pytest.mark.asyncio
31 async def test_editor_bindings(
32 app: Harlequin,
33 wait_for_workers: Callable[[Harlequin], Awaitable[None]],
34 mock_pyperclip: MagicMock,
35 ) -> None:
36 async with app.run_test() as pilot:
37 await wait_for_workers(app)
38 while app.editor is None:
39 await pilot.pause()
40
41 q = QUERY
42 app.editor.text = q
43 assert app.editor.selection.start == app.editor.selection.end == (0, 0)
44
45 # simple navigation
46 await pilot.press("down")
47 assert app.editor.selection.start == app.editor.selection.end == (1, 0)
48 await pilot.press("right")
49 assert app.editor.selection.start == app.editor.selection.end == (1, 1)
50 await pilot.press("right")
51 assert app.editor.selection.start == app.editor.selection.end == (1, 2)
52 await pilot.press("left")
53 assert app.editor.selection.start == app.editor.selection.end == (1, 1)
54 await pilot.press("up")
55 assert app.editor.selection.start == app.editor.selection.end == (0, 1)
56 await pilot.press("ctrl+right")
57 assert app.editor.selection.start == app.editor.selection.end == (0, 6)
58 await pilot.press("ctrl+left")
59 assert app.editor.selection.start == app.editor.selection.end == (0, 0)
60 await pilot.press("ctrl+end")
61 assert app.editor.selection.start == app.editor.selection.end == (11, 18)
62 await pilot.press("ctrl+home")
63 assert app.editor.selection.start == app.editor.selection.end == (0, 0)
64
65 # simple selection
66 await pilot.press("shift+down")
67 assert app.editor.selection.start == (0, 0)
68 assert app.editor.selection.end == (1, 0)
69 await pilot.press("shift+right")
70 assert app.editor.selection.start == (0, 0)
71 assert app.editor.selection.end == (1, 1)
72 await pilot.press("shift+right")
73 assert app.editor.selection.start == (0, 0)
74 assert app.editor.selection.end == (1, 2)
75 await pilot.press("shift+left")
76 assert app.editor.selection.start == (0, 0)
77 assert app.editor.selection.end == (1, 1)
78 await pilot.press("shift+up")
79 assert app.editor.selection.start == (0, 0)
80 assert app.editor.selection.end == (0, 1)
81 await pilot.press("ctrl+shift+right")
82 assert app.editor.selection.start == (0, 0)
83 assert app.editor.selection.end == (0, 6)
84 await pilot.press("ctrl+shift+left")
85 assert app.editor.selection.start == (0, 0)
86 assert app.editor.selection.end == (0, 0)
87 await pilot.press("ctrl+shift+end")
88 assert app.editor.selection.start == (0, 0)
89 assert app.editor.selection.end == (11, 18)
90 await pilot.press("ctrl+shift+home")
91 assert app.editor.selection.start == (0, 0)
92 assert app.editor.selection.end == (0, 0)
93 await pilot.press("ctrl+a")
94 assert app.editor.selection.start == (0, 0)
95 assert app.editor.selection.end == (11, 18)
96
97 # cut/copy/paste
98 await pilot.press("ctrl+c")
99 assert app.editor.text == QUERY
100 assert app.editor.text_input is not None
101 assert app.editor.text_input.clipboard == QUERY
102 assert app.editor.selection.start == (0, 0)
103 assert app.editor.selection.end == (11, 18)
104 await pilot.press("ctrl+x")
105 assert app.editor.text == ""
106 assert app.editor.text_input.clipboard == QUERY
107 assert app.editor.selection.start == (0, 0)
108 assert app.editor.selection.end == (0, 0)
109 await pilot.press("ctrl+v")
110 assert app.editor.text == QUERY
111 assert app.editor.selection.start == app.editor.selection.end == (11, 18)
112
113 await pilot.press("a")
114 assert app.editor.text == QUERY + "a"
115 await pilot.press("escape") # dismiss autocomplete
116 await pilot.press("enter")
117 assert app.editor.text == QUERY + "a\n "
118
119 # undo/redo
120 await pilot.press("ctrl+z")
121 assert app.editor.text == QUERY + "a"
122 await pilot.press("ctrl+y")
123 assert app.editor.text == QUERY + "a\n "
124
125 # delete
126 await pilot.press("backspace")
127 assert app.editor.text == QUERY + "a\n "
128 await pilot.press("shift+delete")
129 assert app.editor.text == QUERY + "a"
130 await pilot.press("backspace")
131 assert app.editor.text == QUERY
132 await pilot.press("ctrl+home")
133 await pilot.press("delete")
134 assert app.editor.text == QUERY[1:]
135
136 # find
137 await pilot.press("ctrl+f")
138 assert app.query_one("#textarea__find_input")
139 await pilot.press("escape")
140 with pytest.raises(NoMatches):
141 _ = app.query_one("#textarea__find_input")
142 await pilot.press("f3")
143 assert app.query_one("#textarea__find_input")
144 await pilot.press("escape")
145 with pytest.raises(NoMatches):
146 _ = app.query_one("#textarea__find_input")
147
148 # goto line
149 await pilot.press("ctrl+g")
150 assert app.query_one("#textarea__gotoline_input")
151 await pilot.press("escape")
152 with pytest.raises(NoMatches):
153 _ = app.query_one("#textarea__gotoline_input")
154
155 # save
156 await pilot.press("ctrl+s")
157 assert app.query_one("#textarea__save_input")
158 await pilot.press("escape")
159 with pytest.raises(NoMatches):
160 _ = app.query_one("#textarea__save_input")
161
162 # open
163 await pilot.press("ctrl+o")
164 assert app.query_one("#textarea__open_input")
165 await pilot.press("escape")
166 with pytest.raises(NoMatches):
167 _ = app.query_one("#textarea__open_input")
168
169
170 @pytest.mark.asyncio
171 async def test_results_viewer_bindings(
172 app: Harlequin,
173 wait_for_workers: Callable[[Harlequin], Awaitable[None]],
174 mock_pyperclip: MagicMock,
175 ) -> None:
176 async with app.run_test() as pilot:
177 await wait_for_workers(app)
178 while app.editor is None:
179 await pilot.pause()
180
181 q = QUERY
182 app.editor.text = q
183 await pilot.press("ctrl+j")
184
185 while (table := app.results_viewer.get_visible_table()) is None:
186 await pilot.pause()
187
188 assert table is not None
189 assert table.cursor_coordinate == (0, 0)
190 assert table.selection_anchor_coordinate is None
191
192 # simple navigation
193 await pilot.press("down")
194 assert table.cursor_coordinate == (1, 0)
195 assert table.selection_anchor_coordinate is None
196 await pilot.press("right")
197 assert table.cursor_coordinate == (1, 1)
198 assert table.selection_anchor_coordinate is None
199 await pilot.press("right")
200 assert table.cursor_coordinate == (1, 2)
201 assert table.selection_anchor_coordinate is None
202 await pilot.press("left")
203 assert table.cursor_coordinate == (1, 1)
204 assert table.selection_anchor_coordinate is None
205 await pilot.press("up")
206 assert table.cursor_coordinate == (0, 1)
207 assert table.selection_anchor_coordinate is None
208 await pilot.press("ctrl+right")
209 assert table.cursor_coordinate == (0, 2)
210 assert table.selection_anchor_coordinate is None
211 await pilot.press("ctrl+left")
212 assert table.cursor_coordinate == (0, 0)
213 assert table.selection_anchor_coordinate is None
214 await pilot.press("ctrl+end")
215 assert table.cursor_coordinate == (6, 2)
216 assert table.selection_anchor_coordinate is None
217 await pilot.press("ctrl+home")
218 assert table.cursor_coordinate == (0, 0)
219 assert table.selection_anchor_coordinate is None
220
221 # simple selection
222 await pilot.press("shift+down")
223 assert table.cursor_coordinate == (1, 0)
224 assert table.selection_anchor_coordinate == (0, 0)
225 await pilot.press("shift+right")
226 assert table.cursor_coordinate == (1, 1)
227 assert table.selection_anchor_coordinate == (0, 0)
228 await pilot.press("shift+right")
229 assert table.cursor_coordinate == (1, 2)
230 assert table.selection_anchor_coordinate == (0, 0)
231 await pilot.press("shift+left")
232 assert table.cursor_coordinate == (1, 1)
233 assert table.selection_anchor_coordinate == (0, 0)
234 await pilot.press("shift+up")
235 assert table.cursor_coordinate == (0, 1)
236 assert table.selection_anchor_coordinate == (0, 0)
237 await pilot.press("ctrl+shift+right")
238 assert table.cursor_coordinate == (0, 2)
239 assert table.selection_anchor_coordinate == (0, 0)
240 await pilot.press("ctrl+shift+left")
241 assert table.cursor_coordinate == (0, 0)
242 assert table.selection_anchor_coordinate == (0, 0)
243 await pilot.press("ctrl+shift+end")
244 assert table.cursor_coordinate == (6, 2)
245 assert table.selection_anchor_coordinate == (0, 0)
246 await pilot.press("ctrl+shift+home")
247 assert table.cursor_coordinate == (0, 0)
248 assert table.selection_anchor_coordinate == (0, 0)
249 await pilot.press("ctrl+a")
250 assert table.cursor_coordinate == (6, 2)
251 assert table.selection_anchor_coordinate == (0, 0)
252
253 # copy
254 await pilot.press("ctrl+c")
255 assert app.editor.text_input is not None
256 assert app.editor.text_input.clipboard.startswith("1\t2\t3")