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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
|
"""Tests some edits using the keyboard.
All tests in this module should press keys on the keyboard which edit the document,
and check that the document content is updated as expected, as well as the cursor
location.
Note that more extensive testing for editing is done at the Document level.
"""
import pytest
from textual.app import App, ComposeResult
from textual.events import Paste
from textual.widgets import TextArea
from textual.widgets.text_area import Selection
TEXT = """I must not fear.
Fear is the mind-killer.
Fear is the little-death that brings total obliteration.
I will face my fear.
"""
SIMPLE_TEXT = """\
ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
Z"""
class TextAreaApp(App):
def compose(self) -> ComposeResult:
text_area = TextArea.code_editor()
text_area.load_text(TEXT)
yield text_area
async def test_single_keypress_printable_character():
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
await pilot.press("x")
assert text_area.text == "x" + TEXT
async def test_single_keypress_enter():
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
await pilot.press("enter")
assert text_area.text == "\n" + TEXT
@pytest.mark.parametrize(
"content,cursor_column,cursor_destination",
[
("", 0, 4),
("x", 0, 4),
("x", 1, 4),
("xxx", 3, 4),
("xxxx", 4, 8),
("xxxxx", 5, 8),
("xxxxxx", 6, 8),
("💩", 1, 3),
("💩💩", 2, 6),
],
)
async def test_tab_with_spaces_goes_to_tab_stop(
content, cursor_column, cursor_destination
):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.indent_width = 4
text_area.load_text(content)
text_area.cursor_location = (0, cursor_column)
await pilot.press("tab")
assert text_area.cursor_location[1] == cursor_destination
async def test_delete_left():
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("Hello, world!")
text_area.move_cursor((0, 6))
await pilot.press("backspace")
assert text_area.text == "Hello world!"
assert text_area.selection == Selection.cursor((0, 5))
async def test_delete_left_start():
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("Hello, world!")
await pilot.press("backspace")
assert text_area.text == "Hello, world!"
assert text_area.selection == Selection.cursor((0, 0))
async def test_delete_left_end():
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("Hello, world!")
text_area.move_cursor((0, 13))
await pilot.press("backspace")
assert text_area.text == "Hello, world"
assert text_area.selection == Selection.cursor((0, 12))
@pytest.mark.parametrize(
"key,selection",
[
("delete", Selection((1, 2), (3, 4))),
("delete", Selection((3, 4), (1, 2))),
("backspace", Selection((1, 2), (3, 4))),
("backspace", Selection((3, 4), (1, 2))),
],
)
async def test_deletion_with_non_empty_selection(key, selection):
"""When there's a selection, pressing backspace or delete should delete everything
that is selected and reset the selection to a cursor at the appropriate location."""
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text(SIMPLE_TEXT)
text_area.selection = selection
await pilot.press(key)
assert text_area.selection == Selection.cursor((1, 2))
assert (
text_area.text
== """\
ABCDE
FGT
UVWXY
Z"""
)
async def test_delete_right():
"""Pressing 'delete' deletes the character to the right of the cursor."""
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("Hello, world!")
text_area.move_cursor((0, 13))
await pilot.press("delete")
assert text_area.text == "Hello, world!"
assert text_area.selection == Selection.cursor((0, 13))
async def test_delete_right_end_of_line():
"""Pressing 'delete' at the end of the line merges this line with the line below."""
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("hello\nworld!")
end_of_line = text_area.get_cursor_line_end_location()
text_area.move_cursor(end_of_line)
await pilot.press("delete")
assert text_area.selection == Selection.cursor((0, 5))
assert text_area.text == "helloworld!"
@pytest.mark.parametrize(
"selection,expected_result,expected_clipboard,cursor_end_location",
[
(Selection.cursor((0, 0)), "", "0123456789", (0, 0)),
(Selection.cursor((0, 4)), "", "0123456789", (0, 0)),
(Selection.cursor((0, 10)), "", "0123456789", (0, 0)),
(Selection((0, 2), (0, 4)), "01456789", "23", (0, 2)),
(Selection((0, 4), (0, 2)), "01456789", "23", (0, 2)),
],
)
async def test_cut(selection, expected_result, expected_clipboard, cursor_end_location):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("0123456789")
text_area.selection = selection
await pilot.press("ctrl+x")
assert text_area.selection == Selection.cursor(cursor_end_location)
assert text_area.text == expected_result
assert app.clipboard == expected_clipboard
@pytest.mark.parametrize(
"selection,expected_result",
[
# Cursors
(Selection.cursor((0, 0)), "345\n678\n9\n"),
(Selection.cursor((0, 2)), "345\n678\n9\n"),
(Selection.cursor((3, 1)), "012\n345\n678\n"),
(Selection.cursor((4, 0)), "012\n345\n678\n9\n"),
# Selections
(Selection((1, 1), (1, 2)), "012\n35\n678\n9\n"),
(Selection((1, 2), (2, 1)), "012\n3478\n9\n"),
],
)
async def test_cut_multiline_document(selection, expected_result):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("012\n345\n678\n9\n")
text_area.selection = selection
await pilot.press("ctrl+x")
cursor_row, cursor_column = text_area.cursor_location
assert text_area.selection == Selection.cursor((cursor_row, cursor_column))
assert text_area.text == expected_result
@pytest.mark.parametrize(
"selection,expected_result",
[
(Selection.cursor((0, 0)), ""),
(Selection.cursor((0, 4)), ""),
(Selection.cursor((0, 10)), ""),
(Selection((0, 2), (0, 4)), ""),
(Selection((0, 4), (0, 2)), ""),
],
)
async def test_delete_line(selection, expected_result):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("0123456789")
text_area.selection = selection
await pilot.press("ctrl+shift+k")
assert text_area.selection == Selection.cursor((0, 0))
assert text_area.text == expected_result
@pytest.mark.parametrize(
"selection,expected_result",
[
# Cursors
(Selection.cursor((0, 0)), "345\n678\n9\n"),
(Selection.cursor((0, 2)), "345\n678\n9\n"),
(Selection.cursor((3, 1)), "012\n345\n678\n"),
(Selection.cursor((4, 0)), "012\n345\n678\n9\n"),
# Selections
(Selection((1, 1), (1, 2)), "012\n678\n9\n"), # non-empty single line selection
(Selection((1, 2), (2, 1)), "012\n9\n"), # delete lines selection touches
(
Selection((1, 2), (3, 0)),
"012\n9\n",
), # cursor at column 0 of line 3, should not be deleted!
(
Selection((3, 0), (1, 2)),
"012\n9\n",
), # opposite direction
(Selection((0, 0), (4, 0)), ""), # delete all lines
],
)
async def test_delete_line_multiline_document(selection, expected_result):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("012\n345\n678\n9\n")
text_area.selection = selection
await pilot.press("ctrl+shift+k")
cursor_row, cursor_column = text_area.cursor_location
assert text_area.selection == Selection.cursor((cursor_row, cursor_column))
assert text_area.text == expected_result
@pytest.mark.parametrize(
"selection,expected_result",
[
# Cursors
(Selection.cursor((0, 0)), ""),
(Selection.cursor((0, 5)), "01234"),
(Selection.cursor((0, 9)), "012345678"),
(Selection.cursor((0, 10)), "0123456789"),
# Selections
(Selection((0, 0), (0, 9)), "012345678"),
(Selection((0, 0), (0, 10)), "0123456789"),
(Selection((0, 2), (0, 5)), "01234"),
(Selection((0, 5), (0, 2)), "01"),
],
)
async def test_delete_to_end_of_line(selection, expected_result):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("0123456789")
text_area.selection = selection
await pilot.press("ctrl+k")
assert text_area.selection == Selection.cursor(selection.end)
assert text_area.text == expected_result
@pytest.mark.parametrize(
"selection,expected_result",
[
# Cursors
(Selection.cursor((0, 0)), "0123456789"),
(Selection.cursor((0, 5)), "56789"),
(Selection.cursor((0, 9)), "9"),
(Selection.cursor((0, 10)), ""),
# Selections
(Selection((0, 0), (0, 9)), "9"),
(Selection((0, 0), (0, 10)), ""),
(Selection((0, 2), (0, 5)), "56789"),
(Selection((0, 5), (0, 2)), "23456789"),
],
)
async def test_delete_to_start_of_line(selection, expected_result):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("0123456789")
text_area.selection = selection
await pilot.press("ctrl+u")
assert text_area.selection == Selection.cursor((0, 0))
assert text_area.text == expected_result
@pytest.mark.parametrize(
"selection,expected_result,final_selection",
[
(Selection.cursor((0, 0)), " 012 345 6789", Selection.cursor((0, 0))),
(Selection.cursor((0, 4)), " 2 345 6789", Selection.cursor((0, 2))),
(Selection.cursor((0, 5)), " 345 6789", Selection.cursor((0, 2))),
(
Selection.cursor((0, 6)),
" 345 6789",
Selection.cursor((0, 2)),
),
(Selection.cursor((0, 14)), " 012 345 ", Selection.cursor((0, 10))),
# When there's a selection and you "delete word left", it just deletes the selection
(Selection((0, 4), (0, 11)), " 01789", Selection.cursor((0, 4))),
],
)
async def test_delete_word_left(selection, expected_result, final_selection):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text(" 012 345 6789")
text_area.selection = selection
await pilot.press("ctrl+w")
assert text_area.text == expected_result
assert text_area.selection == final_selection
@pytest.mark.parametrize(
"selection,expected_result,final_selection",
[
(Selection.cursor((0, 0)), "\t012 \t 345\t6789", Selection.cursor((0, 0))),
(Selection.cursor((0, 4)), "\t \t 345\t6789", Selection.cursor((0, 1))),
(Selection.cursor((0, 5)), "\t\t 345\t6789", Selection.cursor((0, 1))),
(
Selection.cursor((0, 6)),
"\t 345\t6789",
Selection.cursor((0, 1)),
),
(Selection.cursor((0, 15)), "\t012 \t 345\t", Selection.cursor((0, 11))),
# When there's a selection and you "delete word left", it just deletes the selection
(Selection((0, 4), (0, 11)), "\t0126789", Selection.cursor((0, 4))),
],
)
async def test_delete_word_left_with_tabs(selection, expected_result, final_selection):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("\t012 \t 345\t6789")
text_area.selection = selection
await pilot.press("ctrl+w")
assert text_area.text == expected_result
assert text_area.selection == final_selection
async def test_delete_word_left_to_start_of_line():
"""If no word boundary found when we 'delete word left', then
the deletion happens to the start of the line."""
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("0123\n 456789")
text_area.selection = Selection.cursor((1, 3))
await pilot.press("ctrl+w")
assert text_area.text == "0123\n456789"
assert text_area.selection == Selection.cursor((1, 0))
async def test_delete_word_left_at_line_start():
"""If we're at the start of a line and we 'delete word left', the
line merges with the line above (if possible)."""
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("0123\n 456789")
text_area.selection = Selection.cursor((1, 0))
await pilot.press("ctrl+w")
assert text_area.text == "0123 456789"
assert text_area.selection == Selection.cursor((0, 4))
@pytest.mark.parametrize(
"selection,expected_result,final_selection",
[
(Selection.cursor((0, 0)), "012 345 6789", Selection.cursor((0, 0))),
(Selection.cursor((0, 4)), " 01 345 6789", Selection.cursor((0, 4))),
(Selection.cursor((0, 5)), " 012345 6789", Selection.cursor((0, 5))),
(Selection.cursor((0, 14)), " 012 345 6789", Selection.cursor((0, 14))),
# When non-empty selection, "delete word right" just deletes the selection
(Selection((0, 4), (0, 11)), " 01789", Selection.cursor((0, 4))),
],
)
async def test_delete_word_right(selection, expected_result, final_selection):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text(" 012 345 6789")
text_area.selection = selection
await pilot.press("ctrl+f")
assert text_area.text == expected_result
assert text_area.selection == final_selection
async def test_delete_word_right_delete_to_end_of_line():
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("01234\n56789")
text_area.selection = Selection.cursor((0, 3))
await pilot.press("ctrl+f")
assert text_area.text == "012\n56789"
assert text_area.selection == Selection.cursor((0, 3))
async def test_delete_word_right_at_end_of_line():
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("01234\n56789")
text_area.selection = Selection.cursor((0, 5))
await pilot.press("ctrl+f")
assert text_area.text == "0123456789"
assert text_area.selection == Selection.cursor((0, 5))
@pytest.mark.parametrize(
"binding",
[
"enter",
"backspace",
"ctrl+u",
"ctrl+f",
"ctrl+w",
"ctrl+k",
"ctrl+x",
"space",
"1",
"tab",
],
)
async def test_edit_read_only_mode_does_nothing(binding):
"""Try out various key-presses and bindings and ensure they don't alter
the document when read_only=True."""
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.read_only = True
selection = Selection.cursor((0, 2))
text_area.selection = selection
await pilot.press(binding)
assert text_area.text == TEXT
assert text_area.selection == selection
@pytest.mark.parametrize(
"selection",
[
Selection(start=(1, 0), end=(3, 0)),
Selection(start=(3, 0), end=(1, 0)),
],
)
async def test_replace_lines_with_fewer_lines(selection):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.text = SIMPLE_TEXT
text_area.selection = selection
await pilot.press("a")
expected_text = """\
ABCDE
aPQRST
UVWXY
Z"""
assert text_area.text == expected_text
assert text_area.selection == Selection.cursor((1, 1))
@pytest.mark.parametrize(
"selection",
[
Selection(start=(1, 0), end=(3, 0)),
Selection(start=(3, 0), end=(1, 0)),
],
)
async def test_paste(selection):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.text = SIMPLE_TEXT
text_area.selection = selection
app.post_message(Paste("a"))
await pilot.pause()
expected_text = """\
ABCDE
aPQRST
UVWXY
Z"""
assert text_area.text == expected_text
assert text_area.selection == Selection.cursor((1, 1))
async def test_paste_read_only_does_nothing():
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.read_only = True
app.post_message(Paste("hello"))
await pilot.pause()
assert text_area.text == TEXT # No change
|