| 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 |
from textual.widgets import Input |
| 10 |
|
| 11 |
from harlequin import Harlequin |
| 12 |
from tests.functional_tests.helpers import wait_for_any_table, wait_for_editor |
| 13 |
|
| 14 |
QUERY = dedent( |
| 15 |
""" |
| 16 |
select * |
| 17 |
from |
| 18 |
( |
| 19 |
values |
| 20 |
(1, 2, 3), |
| 21 |
(4, 5, 6), |
| 22 |
(7, 8, 9), |
| 23 |
(10, 11, 12), |
| 24 |
(13, 14, 15), |
| 25 |
(16, 17, 18), |
| 26 |
(19, 20, 21) |
| 27 |
) foo(a, b, c) |
| 28 |
""" |
| 29 |
).strip() |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.asyncio |
| 33 |
async def test_editor_bindings( |
| 34 |
app: Harlequin, |
| 35 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 36 |
mock_pyperclip: MagicMock, |
| 37 |
) -> None: |
| 38 |
async with app.run_test() as pilot: |
| 39 |
await wait_for_workers(app) |
| 40 |
editor = await wait_for_editor(pilot, app) |
| 41 |
|
| 42 |
q = QUERY |
| 43 |
editor.text = q |
| 44 |
assert editor.selection.start == editor.selection.end == (0, 0) |
| 45 |
|
| 46 |
# simple navigation |
| 47 |
await pilot.press("down") |
| 48 |
assert editor.selection.start == editor.selection.end == (1, 0) |
| 49 |
await pilot.press("right") |
| 50 |
assert editor.selection.start == editor.selection.end == (1, 1) |
| 51 |
await pilot.press("right") |
| 52 |
assert editor.selection.start == editor.selection.end == (1, 2) |
| 53 |
await pilot.press("left") |
| 54 |
assert editor.selection.start == editor.selection.end == (1, 1) |
| 55 |
await pilot.press("up") |
| 56 |
assert editor.selection.start == editor.selection.end == (0, 1) |
| 57 |
await pilot.press("ctrl+right") |
| 58 |
assert editor.selection.start == editor.selection.end == (0, 6) |
| 59 |
await pilot.press("ctrl+left") |
| 60 |
assert editor.selection.start == editor.selection.end == (0, 0) |
| 61 |
await pilot.press("ctrl+end") |
| 62 |
assert editor.selection.start == editor.selection.end == (11, 18) |
| 63 |
await pilot.press("ctrl+home") |
| 64 |
assert editor.selection.start == editor.selection.end == (0, 0) |
| 65 |
|
| 66 |
# simple selection |
| 67 |
await pilot.press("shift+down") |
| 68 |
assert editor.selection.start == (0, 0) |
| 69 |
assert editor.selection.end == (1, 0) |
| 70 |
await pilot.press("shift+right") |
| 71 |
assert editor.selection.start == (0, 0) |
| 72 |
assert editor.selection.end == (1, 1) |
| 73 |
await pilot.press("shift+right") |
| 74 |
assert editor.selection.start == (0, 0) |
| 75 |
assert editor.selection.end == (1, 2) |
| 76 |
await pilot.press("shift+left") |
| 77 |
assert editor.selection.start == (0, 0) |
| 78 |
assert editor.selection.end == (1, 1) |
| 79 |
await pilot.press("shift+up") |
| 80 |
assert editor.selection.start == (0, 0) |
| 81 |
assert editor.selection.end == (0, 1) |
| 82 |
await pilot.press("ctrl+shift+right") |
| 83 |
assert editor.selection.start == (0, 0) |
| 84 |
assert editor.selection.end == (0, 6) |
| 85 |
await pilot.press("ctrl+shift+left") |
| 86 |
assert editor.selection.start == (0, 0) |
| 87 |
assert editor.selection.end == (0, 0) |
| 88 |
await pilot.press("ctrl+shift+end") |
| 89 |
assert editor.selection.start == (0, 0) |
| 90 |
assert editor.selection.end == (11, 18) |
| 91 |
await pilot.press("ctrl+shift+home") |
| 92 |
assert editor.selection.start == (0, 0) |
| 93 |
assert editor.selection.end == (0, 0) |
| 94 |
await pilot.press("ctrl+a") |
| 95 |
assert editor.selection.start == (0, 0) |
| 96 |
assert editor.selection.end == (11, 18) |
| 97 |
|
| 98 |
# cut/copy/paste |
| 99 |
await pilot.press("ctrl+c") |
| 100 |
assert editor.text == QUERY |
| 101 |
assert editor.text_input is not None |
| 102 |
assert editor.text_input.clipboard == QUERY |
| 103 |
assert editor.selection.start == (0, 0) |
| 104 |
assert editor.selection.end == (11, 18) |
| 105 |
await pilot.press("ctrl+x") |
| 106 |
assert editor.text == "" |
| 107 |
assert editor.text_input.clipboard == QUERY |
| 108 |
assert editor.selection.start == (0, 0) |
| 109 |
assert editor.selection.end == (0, 0) |
| 110 |
await pilot.press("ctrl+v") |
| 111 |
assert editor.text == QUERY |
| 112 |
assert editor.selection.start == editor.selection.end == (11, 18) |
| 113 |
|
| 114 |
await pilot.press("a") |
| 115 |
assert editor.text == QUERY + "a" |
| 116 |
await pilot.press("escape") # dismiss autocomplete |
| 117 |
await pilot.press("enter") |
| 118 |
assert editor.text == QUERY + "a\n " |
| 119 |
|
| 120 |
# undo/redo |
| 121 |
await pilot.press("ctrl+z") |
| 122 |
assert editor.text == QUERY + "a" |
| 123 |
await pilot.press("ctrl+y") |
| 124 |
assert editor.text == QUERY + "a\n " |
| 125 |
|
| 126 |
# delete |
| 127 |
await pilot.press("backspace") |
| 128 |
assert editor.text == QUERY + "a\n " |
| 129 |
await pilot.press("shift+delete") |
| 130 |
assert editor.text == QUERY + "a" |
| 131 |
await pilot.press("backspace") |
| 132 |
assert editor.text == QUERY |
| 133 |
await pilot.press("ctrl+home") |
| 134 |
await pilot.press("delete") |
| 135 |
assert editor.text == QUERY[1:] |
| 136 |
|
| 137 |
# find |
| 138 |
await pilot.press("ctrl+f") |
| 139 |
assert app.query_one("#textarea__find_input") |
| 140 |
await pilot.press("escape") |
| 141 |
with pytest.raises(NoMatches): |
| 142 |
_ = app.query_one("#textarea__find_input") |
| 143 |
await pilot.press("f3") |
| 144 |
assert app.query_one("#textarea__find_input") |
| 145 |
await pilot.press("escape") |
| 146 |
with pytest.raises(NoMatches): |
| 147 |
_ = app.query_one("#textarea__find_input") |
| 148 |
|
| 149 |
# goto line |
| 150 |
await pilot.press("ctrl+g") |
| 151 |
assert app.query_one("#textarea__gotoline_input") |
| 152 |
await pilot.press("escape") |
| 153 |
with pytest.raises(NoMatches): |
| 154 |
_ = app.query_one("#textarea__gotoline_input") |
| 155 |
|
| 156 |
# save |
| 157 |
await pilot.press("ctrl+s") |
| 158 |
assert app.query_one("#textarea__save_input") |
| 159 |
await pilot.press("escape") |
| 160 |
with pytest.raises(NoMatches): |
| 161 |
_ = app.query_one("#textarea__save_input") |
| 162 |
|
| 163 |
# open |
| 164 |
await pilot.press("ctrl+o") |
| 165 |
assert app.query_one("#textarea__open_input") |
| 166 |
await pilot.press("escape") |
| 167 |
with pytest.raises(NoMatches): |
| 168 |
_ = app.query_one("#textarea__open_input") |
| 169 |
|
| 170 |
|
| 171 |
@pytest.mark.asyncio |
| 172 |
async def test_results_viewer_bindings( |
| 173 |
app: Harlequin, |
| 174 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 175 |
mock_pyperclip: MagicMock, |
| 176 |
) -> None: |
| 177 |
async with app.run_test() as pilot: |
| 178 |
await wait_for_workers(app) |
| 179 |
editor = await wait_for_editor(pilot, app) |
| 180 |
|
| 181 |
q = QUERY |
| 182 |
editor.text = q |
| 183 |
await pilot.press("ctrl+j") |
| 184 |
|
| 185 |
table = await wait_for_any_table(pilot, app) |
| 186 |
|
| 187 |
assert table is not None |
| 188 |
assert table.cursor_coordinate == (0, 0) |
| 189 |
assert table.selection_anchor_coordinate is None |
| 190 |
|
| 191 |
# simple navigation |
| 192 |
await pilot.press("down") |
| 193 |
assert table.cursor_coordinate == (1, 0) |
| 194 |
assert table.selection_anchor_coordinate is None |
| 195 |
await pilot.press("right") |
| 196 |
assert table.cursor_coordinate == (1, 1) |
| 197 |
assert table.selection_anchor_coordinate is None |
| 198 |
await pilot.press("right") |
| 199 |
assert table.cursor_coordinate == (1, 2) |
| 200 |
assert table.selection_anchor_coordinate is None |
| 201 |
await pilot.press("left") |
| 202 |
assert table.cursor_coordinate == (1, 1) |
| 203 |
assert table.selection_anchor_coordinate is None |
| 204 |
await pilot.press("up") |
| 205 |
assert table.cursor_coordinate == (0, 1) |
| 206 |
assert table.selection_anchor_coordinate is None |
| 207 |
await pilot.press("ctrl+right") |
| 208 |
assert table.cursor_coordinate == (0, 2) |
| 209 |
assert table.selection_anchor_coordinate is None |
| 210 |
await pilot.press("ctrl+left") |
| 211 |
assert table.cursor_coordinate == (0, 0) |
| 212 |
assert table.selection_anchor_coordinate is None |
| 213 |
await pilot.press("ctrl+end") |
| 214 |
assert table.cursor_coordinate == (6, 2) |
| 215 |
assert table.selection_anchor_coordinate is None |
| 216 |
await pilot.press("ctrl+home") |
| 217 |
assert table.cursor_coordinate == (0, 0) |
| 218 |
assert table.selection_anchor_coordinate is None |
| 219 |
|
| 220 |
# simple selection |
| 221 |
await pilot.press("shift+down") |
| 222 |
assert table.cursor_coordinate == (1, 0) |
| 223 |
assert table.selection_anchor_coordinate == (0, 0) |
| 224 |
await pilot.press("shift+right") |
| 225 |
assert table.cursor_coordinate == (1, 1) |
| 226 |
assert table.selection_anchor_coordinate == (0, 0) |
| 227 |
await pilot.press("shift+right") |
| 228 |
assert table.cursor_coordinate == (1, 2) |
| 229 |
assert table.selection_anchor_coordinate == (0, 0) |
| 230 |
await pilot.press("shift+left") |
| 231 |
assert table.cursor_coordinate == (1, 1) |
| 232 |
assert table.selection_anchor_coordinate == (0, 0) |
| 233 |
await pilot.press("shift+up") |
| 234 |
assert table.cursor_coordinate == (0, 1) |
| 235 |
assert table.selection_anchor_coordinate == (0, 0) |
| 236 |
await pilot.press("ctrl+shift+right") |
| 237 |
assert table.cursor_coordinate == (0, 2) |
| 238 |
assert table.selection_anchor_coordinate == (0, 0) |
| 239 |
await pilot.press("ctrl+shift+left") |
| 240 |
assert table.cursor_coordinate == (0, 0) |
| 241 |
assert table.selection_anchor_coordinate == (0, 0) |
| 242 |
await pilot.press("ctrl+shift+end") |
| 243 |
assert table.cursor_coordinate == (6, 2) |
| 244 |
assert table.selection_anchor_coordinate == (0, 0) |
| 245 |
await pilot.press("ctrl+shift+home") |
| 246 |
assert table.cursor_coordinate == (0, 0) |
| 247 |
assert table.selection_anchor_coordinate == (0, 0) |
| 248 |
await pilot.press("ctrl+a") |
| 249 |
assert table.cursor_coordinate == (6, 2) |
| 250 |
assert table.selection_anchor_coordinate == (0, 0) |
| 251 |
|
| 252 |
# copy |
| 253 |
await pilot.press("ctrl+c") |
| 254 |
assert editor.text_input is not None |
| 255 |
assert editor.text_input.clipboard.startswith("1\t2\t3") |
| 256 |
|
| 257 |
|
| 258 |
@pytest.mark.asyncio |
| 259 |
async def test_editor_bindings_do_not_beat_the_find_input( |
| 260 |
app: Harlequin, |
| 261 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 262 |
) -> None: |
| 263 |
"""ctrl+w and ctrl+k edit the find input, rather than closing or switching a buffer. |
| 264 |
|
| 265 |
Both are bound to a `code_editor.*` action on `EditorCollection`, an ancestor |
| 266 |
of the find input, and the input's own editing keys have to win. |
| 267 |
""" |
| 268 |
async with app.run_test() as pilot: |
| 269 |
await wait_for_workers(app) |
| 270 |
await wait_for_editor(pilot, app) |
| 271 |
|
| 272 |
await pilot.press("ctrl+n") |
| 273 |
await pilot.pause() |
| 274 |
await pilot.wait_for_scheduled_animations() |
| 275 |
assert app.editor_collection.tab_count == 2 |
| 276 |
assert app.editor_collection.active == "tab-2" |
| 277 |
|
| 278 |
await pilot.press("ctrl+f") |
| 279 |
find_input = app.query_one("#textarea__find_input", Input) |
| 280 |
await pilot.press("f", "o", "o", "space", "b", "a", "r") |
| 281 |
assert find_input.value == "foo bar" |
| 282 |
|
| 283 |
await pilot.press("ctrl+w") |
| 284 |
assert find_input.value == "foo " |
| 285 |
assert app.editor_collection.tab_count == 2 |
| 286 |
|
| 287 |
await pilot.press("ctrl+k") |
| 288 |
assert find_input.value == "foo " |
| 289 |
assert app.editor_collection.active == "tab-2" |