From e199dc226b4c88b1b2a366786894e70c2eaae2ed Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Mon, 30 Jan 2023 09:51:46 +0000 Subject: [PATCH] Start Input unit tests for actions that modify the text --- .../test_input_key_modification_actions.py | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 tests/input/test_input_key_modification_actions.py diff --git a/tests/input/test_input_key_modification_actions.py b/tests/input/test_input_key_modification_actions.py new file mode 100644 index 000000000..53344d2a3 --- /dev/null +++ b/tests/input/test_input_key_modification_actions.py @@ -0,0 +1,83 @@ +"""Unit tests for Input widget value modification actions.""" + +from textual.app import App, ComposeResult +from textual.widgets import Input + + +TEST_INPUTS: dict[str | None, str] = { + "empty": "", + "multi-no-punctuation": "Curse your sudden but inevitable betrayal", + "multi-punctuation": "We have done the impossible, and that makes us mighty.", + "multi-and-hyphenated": "Long as she does it quiet-like", +} + + +class InputTester(App[None]): + """Input widget testing app.""" + + def compose(self) -> ComposeResult: + for input_id, value in TEST_INPUTS.items(): + yield Input(value, id=input_id) + + +async def test_delete_left_from_home() -> None: + """Deleting left from home should do nothing.""" + async with InputTester().run_test() as pilot: + for input in pilot.app.query(Input): + input.action_delete_left() + assert input.cursor_position == 0 + assert input.value == TEST_INPUTS[input.id] + + +async def test_delete_left_from_end() -> None: + """Deleting left from home should do remove the last character (if there is one).""" + async with InputTester().run_test() as pilot: + for input in pilot.app.query(Input): + input.action_end() + input.action_delete_left() + assert input.cursor_position == len(input.value) + assert input.value == TEST_INPUTS[input.id][:-1] + + +async def test_delete_left_word_from_home() -> None: + """Deleting word left from home should do nothing.""" + async with InputTester().run_test() as pilot: + for input in pilot.app.query(Input): + input.action_delete_left_word() + assert input.cursor_position == 0 + assert input.value == TEST_INPUTS[input.id] + + +async def test_delete_left_word_from_end() -> None: + """Deleting word left from end should remove the expected text.""" + async with InputTester().run_test() as pilot: + expected: dict[str | None, str] = { + "empty": "", + "multi-no-punctuation": "Curse your sudden but inevitable ", + "multi-punctuation": "We have done the impossible, and that makes us ", + "multi-and-hyphenated": "Long as she does it quiet-", + } + for input in pilot.app.query(Input): + input.action_end() + input.action_delete_left_word() + assert input.cursor_position == len(input.value) + assert input.value == expected[input.id] + + +async def test_delete_left_all_from_home() -> None: + """Deleting all left from home should do nothing.""" + async with InputTester().run_test() as pilot: + for input in pilot.app.query(Input): + input.action_delete_left_all() + assert input.cursor_position == 0 + assert input.value == TEST_INPUTS[input.id] + + +async def test_delete_left_all_from_end() -> None: + """Deleting all left from end should empty the input value.""" + async with InputTester().run_test() as pilot: + for input in pilot.app.query(Input): + input.action_end() + input.action_delete_left_all() + assert input.cursor_position == 0 + assert input.value == ""