From 55c58e0f8f16ebe88e31f8811a00958f698ec977 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Wed, 21 Dec 2022 11:39:36 +0000 Subject: [PATCH] added tests --- tests/test_keys.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/test_keys.py diff --git a/tests/test_keys.py b/tests/test_keys.py new file mode 100644 index 000000000..19a9dc136 --- /dev/null +++ b/tests/test_keys.py @@ -0,0 +1,47 @@ +import pytest + +from textual.app import App +from textual.keys import _character_to_key + + +@pytest.mark.parametrize( + "character,key", + [ + ("1", "1"), + ("2", "2"), + ("a", "a"), + ("z", "z"), + ("_", "underscore"), + (" ", "space"), + ("~", "tilde"), + ("?", "question_mark"), + ("£", "pound_sign"), + (",", "comma"), + ], +) +def test_character_to_key(character: str, key: str) -> None: + assert _character_to_key(character) == key + + +async def test_character_bindings(): + """Test you can bind to a character as well as a longer key name.""" + counter = 0 + + class BindApp(App): + BINDINGS = [(".,~,space", "increment", "foo")] + + def action_increment(self) -> None: + nonlocal counter + counter += 1 + + app = BindApp() + print(app._bindings) + async with app.run_test() as pilot: + await pilot.press(".") + assert counter == 1 + await pilot.press("~") + assert counter == 2 + await pilot.press(" ") + assert counter == 3 + await pilot.press("x") + assert counter == 3