From 2fe73c0c28f4cc5d5db75afd2b665afa935edaf3 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Wed, 8 Feb 2023 14:01:42 +0000 Subject: [PATCH] Testing highlighted events via keyboard cursor movement --- src/textual/widgets/_data_table.py | 5 ++++- tests/test_data_table.py | 32 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/textual/widgets/_data_table.py b/src/textual/widgets/_data_table.py index f82691ce2..62bcef1e5 100644 --- a/src/textual/widgets/_data_table.py +++ b/src/textual/widgets/_data_table.py @@ -67,7 +67,10 @@ class StringKey: def __eq__(self, other: object) -> bool: # Strings will match Keys containing the same string value. # Otherwise, you'll need to supply the exact same key object. - return hash(self) == hash(other) + try: + return hash(self) == hash(other) + except TypeError: + return False def __lt__(self, other): if isinstance(other, str): diff --git a/tests/test_data_table.py b/tests/test_data_table.py index 19580913f..dd0109447 100644 --- a/tests/test_data_table.py +++ b/tests/test_data_table.py @@ -617,6 +617,38 @@ async def test_sort_reverse_coordinate_and_key_access(): assert table.ordered_rows[2].key == row_one +async def test_cell_cursor_highlight_events(): + app = DataTableApp() + async with app.run_test(): + table = app.query_one(DataTable) + column_one_key, column_two_key = table.add_columns("A", "B") + _ = table.add_row(0, 1) + row_two_key = table.add_row(2, 3) + + # Since initial position is (0, 0), cursor doesn't move so no event posted + table.action_cursor_up() + table.action_cursor_left() + + await wait_for_idle(0) + assert table.app.message_names == ["CellHighlighted"] + + table.action_cursor_down() + await wait_for_idle(0) + assert len(table.app.messages) == 2 + latest_message: DataTable.CellHighlighted = table.app.messages[-1] + assert latest_message.value == 2 + assert latest_message.coordinate == Coordinate(1, 0) + + assert latest_message.cell_key == CellKey(row_two_key, column_one_key) + + table.action_cursor_right() + await wait_for_idle(0) + assert len(table.app.messages) == 3 + latest_message = table.app.messages[-1] + assert latest_message.coordinate == Coordinate(1, 1) + assert latest_message.cell_key == CellKey(row_two_key, column_two_key) + + def test_key_equals_equivalent_string(): text = "Hello" key = RowKey(text)