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
|
from pathlib import Path
from typing import List
import pytest
from textual.app import App
from textual.message import Message
from textual.widgets import Input
from textual_textarea import TextAreaSaved, TextEditor
@pytest.mark.parametrize("filename", ["foo.py", "empty.py"])
@pytest.mark.asyncio
async def test_open(data_dir: Path, app: App, filename: str) -> None:
p = data_dir / "test_open" / filename
with open(p, "r") as f:
contents = f.read()
async with app.run_test() as pilot:
ta = app.query_one("#ta", expect_type=TextEditor)
assert ta.text == ""
starting_text = "123"
for key in starting_text:
await pilot.press(key)
assert ta.text == starting_text
await pilot.press("ctrl+o")
open_input = ta.query_one(Input)
assert open_input.id and "open" in open_input.id
assert open_input.has_focus
for key in str(p):
await pilot.press(key)
await pilot.press("enter")
assert ta.text == contents
assert ta.text_input is not None
assert ta.text_input.has_focus
# make sure the end of the buffer is formatted properly.
# these previously caused a crash.
await pilot.press("ctrl+end")
assert ta.selection.end[1] >= 0
await pilot.press("enter")
@pytest.mark.asyncio
async def test_save(app: App, tmp_path: Path) -> None:
TEXT = "select\n 1 as a,\n 2 as b,\n 'c' as c"
p = tmp_path / "text.sql"
print(p)
messages: List[Message] = []
async with app.run_test(message_hook=messages.append) as pilot:
ta = app.query_one("#ta", expect_type=TextEditor)
ta.text = TEXT
await pilot.press("ctrl+s")
save_input = ta.query_one(Input)
assert save_input.id and "save" in save_input.id
assert save_input.has_focus
save_input.value = str(p)
await pilot.press("enter")
await pilot.pause()
assert len(messages) > 1
assert Input.Submitted in [msg.__class__ for msg in messages]
assert TextAreaSaved in [msg.__class__ for msg in messages]
with open(p, "r") as f:
saved_text = f.read()
assert saved_text == TEXT
@pytest.mark.asyncio
async def test_multiple_footer_inputs(app: App) -> None:
async with app.run_test() as pilot:
ta = app.query_exactly_one("#ta", expect_type=TextEditor)
ta.text = "select 1"
assert ta.text_input is not None
ta.text_input.focus()
while not ta.text_input.has_focus:
await pilot.pause()
await pilot.press("ctrl+o")
open_input = ta.query_exactly_one(Input)
assert open_input.id and "open" in open_input.id
assert open_input.has_focus
await pilot.press("a")
assert open_input.value == "a"
await pilot.press("ctrl+o")
new_input = ta.query_exactly_one(Input)
assert open_input is new_input
assert open_input.has_focus
assert open_input.value == "a"
await pilot.press("ctrl+s")
save_input = ta.query_exactly_one(Input)
assert open_input is not save_input
assert save_input.id and "save" in save_input.id
assert save_input.has_focus
assert save_input.value == ""
await pilot.click(ta.text_input)
await pilot.pause(0.1)
assert ta.text_input.has_focus
await pilot.click(save_input)
await pilot.pause(0.1)
assert save_input.has_focus
|