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
|
import pytest
from textual.app import App, ComposeResult
from textual.geometry import Offset
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.
"""
class TextAreaApp(App):
def __init__(self, read_only: bool = False):
super().__init__()
self.read_only = read_only
def compose(self) -> ComposeResult:
yield TextArea(TEXT, show_line_numbers=True, read_only=self.read_only)
@pytest.fixture(params=[True, False])
async def app(request):
"""Each test that receives an `app` will execute twice.
Once with read_only=True, and once with read_only=False.
"""
return TextAreaApp(read_only=request.param)
async def test_mouse_click(app: TextAreaApp):
"""When you click the TextArea, the cursor moves to the expected location."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
await pilot.click(TextArea, Offset(x=5, y=2))
assert text_area.selection == Selection.cursor((1, 0))
async def test_mouse_click_clamp_from_right(app: TextAreaApp):
"""When you click to the right of the document bounds, the cursor is clamped
to within the document bounds."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
await pilot.click(TextArea, Offset(x=8, y=20))
assert text_area.selection == Selection.cursor((4, 0))
async def test_mouse_click_gutter_clamp(app: TextAreaApp):
"""When you click the gutter, it selects the start of the line."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
await pilot.click(TextArea, Offset(x=0, y=3))
assert text_area.selection == Selection.cursor((2, 0))
async def test_cursor_movement_basic():
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("01234567\n012345\n0123456789")
await pilot.press("right")
assert text_area.selection == Selection.cursor((0, 1))
await pilot.press("down")
assert text_area.selection == Selection.cursor((1, 1))
await pilot.press("left")
assert text_area.selection == Selection.cursor((1, 0))
await pilot.press("up")
assert text_area.selection == Selection.cursor((0, 0))
async def test_cursor_selection_right(app: TextAreaApp):
"""When you press shift+right the selection is updated correctly."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
await pilot.press(*["shift+right"] * 3)
assert text_area.selection == Selection((0, 0), (0, 3))
async def test_cursor_selection_right_to_previous_line(app: TextAreaApp):
"""When you press shift+right resulting in the cursor moving to the next line,
the selection is updated correctly."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.selection = Selection.cursor((0, 15))
await pilot.press(*["shift+right"] * 4)
assert text_area.selection == Selection((0, 15), (1, 2))
async def test_cursor_selection_left(app: TextAreaApp):
"""When you press shift+left the selection is updated correctly."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.selection = Selection.cursor((2, 5))
await pilot.press(*["shift+left"] * 3)
assert text_area.selection == Selection((2, 5), (2, 2))
async def test_cursor_selection_left_to_previous_line(app: TextAreaApp):
"""When you press shift+left resulting in the cursor moving back to the previous line,
the selection is updated correctly."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.selection = Selection.cursor((2, 2))
await pilot.press(*["shift+left"] * 3)
# The cursor jumps up to the end of the line above.
end_of_previous_line = len(TEXT.splitlines()[1])
assert text_area.selection == Selection((2, 2), (1, end_of_previous_line))
async def test_cursor_selection_up(app: TextAreaApp):
"""When you press shift+up the selection is updated correctly."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.move_cursor((2, 3))
await pilot.press("shift+up")
assert text_area.selection == Selection((2, 3), (1, 3))
async def test_cursor_selection_up_when_cursor_on_first_line(app: TextAreaApp):
"""When you press shift+up the on the first line, it selects to the start."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.move_cursor((0, 4))
await pilot.press("shift+up")
assert text_area.selection == Selection((0, 4), (0, 0))
await pilot.press("shift+up")
assert text_area.selection == Selection((0, 4), (0, 0))
async def test_cursor_selection_down(app: TextAreaApp):
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.move_cursor((2, 5))
await pilot.press("shift+down")
assert text_area.selection == Selection((2, 5), (3, 5))
async def test_cursor_selection_down_when_cursor_on_last_line(app: TextAreaApp):
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("ABCDEF\nGHIJK")
text_area.move_cursor((1, 2))
await pilot.press("shift+down")
assert text_area.selection == Selection((1, 2), (1, 5))
await pilot.press("shift+down")
assert text_area.selection == Selection((1, 2), (1, 5))
async def test_cursor_word_right(app: TextAreaApp):
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("ABC DEF\nGHIJK")
await pilot.press("ctrl+right")
assert text_area.selection == Selection.cursor((0, 3))
async def test_cursor_word_right_select(app: TextAreaApp):
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("ABC DEF\nGHIJK")
await pilot.press("ctrl+shift+right")
assert text_area.selection == Selection((0, 0), (0, 3))
async def test_cursor_word_left(app: TextAreaApp):
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("ABC DEF\nGHIJK")
text_area.move_cursor((0, 7))
await pilot.press("ctrl+left")
assert text_area.selection == Selection.cursor((0, 4))
async def test_cursor_word_left_select(app: TextAreaApp):
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("ABC DEF\nGHIJK")
text_area.move_cursor((0, 7))
await pilot.press("ctrl+shift+left")
assert text_area.selection == Selection((0, 7), (0, 4))
@pytest.mark.parametrize("key", ["end", "ctrl+e"])
async def test_cursor_to_line_end(key, app: TextAreaApp):
"""You can use the keyboard to jump the cursor to the end of the current line."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.selection = Selection.cursor((2, 2))
await pilot.press(key)
eol_index = len(TEXT.splitlines()[2])
assert text_area.cursor_location == (2, eol_index)
assert text_area.selection.is_empty
@pytest.mark.parametrize("key", ["home", "ctrl+a"])
async def test_cursor_to_line_home_basic_behaviour(key, app: TextAreaApp):
"""You can use the keyboard to jump the cursor to the start of the current line."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.selection = Selection.cursor((2, 2))
await pilot.press(key)
assert text_area.cursor_location == (2, 0)
assert text_area.selection.is_empty
@pytest.mark.parametrize(
"cursor_start,cursor_destination",
[
((0, 0), (0, 4)),
((0, 2), (0, 0)),
((0, 4), (0, 0)),
((0, 5), (0, 4)),
((0, 9), (0, 4)),
((0, 15), (0, 4)),
],
)
async def test_cursor_line_home_smart_home(
cursor_start, cursor_destination, app: TextAreaApp
):
"""If the line begins with whitespace, pressing home firstly goes
to the start of the (non-whitespace) content. Pressing it again takes you to column
0. If you press it again, it goes back to the first non-whitespace column."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text(" hello world")
text_area.move_cursor(cursor_start)
await pilot.press("home")
assert text_area.selection == Selection.cursor(cursor_destination)
async def test_cursor_page_down(app: TextAreaApp):
"""Pagedown moves the cursor down 1 page, retaining column index."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("XXX\n" * 200)
text_area.selection = Selection.cursor((0, 1))
await pilot.press("pagedown")
margin = 2
assert text_area.selection == Selection.cursor(
(app.size.height - margin, 1)
)
async def test_cursor_page_up(app: TextAreaApp):
"""Pageup moves the cursor up 1 page, retaining column index."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.load_text("XXX\n" * 200)
text_area.selection = Selection.cursor((100, 1))
await pilot.press("pageup")
margin = 2
assert text_area.selection == Selection.cursor(
(100 - app.size.height + margin, 1)
)
async def test_cursor_vertical_movement_visual_alignment_snapping(app: TextAreaApp):
"""When you move the cursor vertically, it should stay vertically
aligned even when double-width characters are used."""
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.text = "こんにちは\n012345"
text_area.move_cursor((1, 3), record_width=True)
# The '3' is aligned with ん at (0, 1)
# こんにちは
# 012345
# Pressing `up` takes us from (1, 3) to (0, 1) because record_width=True.
await pilot.press("up")
assert text_area.selection == Selection.cursor((0, 1))
# Pressing `down` takes us from (0, 1) to (1, 3)
await pilot.press("down")
assert text_area.selection == Selection.cursor((1, 3))
async def test_select_line_binding(app: TextAreaApp):
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.move_cursor((2, 2))
await pilot.press("f6")
assert text_area.selection == Selection((2, 0), (2, 56))
async def test_select_all_binding(app: TextAreaApp):
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
await pilot.press("f7")
assert text_area.selection == Selection((0, 0), (4, 0))
|