Skip to content

Commit

Permalink
correct for coding standard, brevity, and add explanatory comment
Browse files Browse the repository at this point in the history
  • Loading branch information
glyph committed Mar 11, 2024
1 parent a78e493 commit 002c528
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 56 deletions.
10 changes: 8 additions & 2 deletions src/twisted/conch/insults/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class Widget:
focused = False
parent = None
dirty = False
width = height = None
width: int | None = None
height: int | None = None

def repaint(self):
if not self.dirty:
Expand Down Expand Up @@ -109,7 +110,12 @@ def functionKeyReceived(self, keyID, modifier):
name = keyID
if not isinstance(keyID, str):
name = name.decode("utf-8")
func = getattr(self, "func_" + name[1:-1], None)

# Peel off the square brackets added by the computed definition of
# twisted.conch.insults.insults.FUNCTION_KEYS.
methodName = "func_" + name[1:-1]

func = getattr(self, methodName, None)
if func is not None:
func(modifier)

Expand Down
79 changes: 25 additions & 54 deletions src/twisted/conch/test/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,31 @@ def setUp(self) -> None:
"""
seq: list[bytes] = [f"{_num}".encode("ascii") for _num in range(10)]
self.widget = Selection(seq, None)
self.widget.height = 10 # type: ignore[assignment]
self.widget.height = 10
self.widget.focusedIndex = 5

def test_selection_down_arrow(self) -> None:
def test_selectionDownArrow(self) -> None:
"""
Send DOWN_ARROW to select element just below the current one.
"""
self.widget.keystrokeReceived(ServerProtocol.DOWN_ARROW, None) # type: ignore[attr-defined]
self.assertIs(self.widget.focusedIndex, 6)

def test_selection_up_arrow(self) -> None:
def test_selectionUpArrow(self) -> None:
"""
Send UP_ARROW to select element just above the current one.
"""
self.widget.keystrokeReceived(ServerProtocol.UP_ARROW, None) # type: ignore[attr-defined]
self.assertIs(self.widget.focusedIndex, 4)

def test_selection_pgdn(self) -> None:
def test_selectionPGDN(self) -> None:
"""
Send PGDN to select element one page down (here: last element).
"""
self.widget.keystrokeReceived(ServerProtocol.PGDN, None) # type: ignore[attr-defined]
self.assertIs(self.widget.focusedIndex, 9)

def test_selection_pgup(self) -> None:
def test_selectionPGUP(self) -> None:
"""
Send PGUP to select element one page up (here: first element).
"""
Expand All @@ -128,22 +128,22 @@ def __init__(self) -> None:
Widget.__init__(self)
self.triggered: list[str] = []

def func_F1(self, modifier) -> None: # type: ignore[no-untyped-def]
def func_F1(self, modifier: str) -> None:
self.triggered.append("F1")

def func_HOME(self, modifier) -> None: # type: ignore[no-untyped-def]
def func_HOME(self, modifier: str) -> None:
self.triggered.append("HOME")

def func_DOWN_ARROW(self, modifier) -> None: # type: ignore[no-untyped-def]
def func_DOWN_ARROW(self, modifier: str) -> None:
self.triggered.append("DOWN_ARROW")

def func_UP_ARROW(self, modifier) -> None: # type: ignore[no-untyped-def]
def func_UP_ARROW(self, modifier: str) -> None:
self.triggered.append("UP_ARROW")

def func_PGDN(self, modifier) -> None: # type: ignore[no-untyped-def]
def func_PGDN(self, modifier: str) -> None:
self.triggered.append("PGDN")

def func_PGUP(self, modifier) -> None: # type: ignore[no-untyped-def]
def func_PGUP(self, modifier: str) -> None:
self.triggered.append("PGUP")


Expand All @@ -152,50 +152,21 @@ class WidgetFunctionKeyTests(TestCase):
Call functionKeyReceived with key values from insults.ServerProtocol
"""

def test_widget_function_key_f1(self) -> None:
def test_functionKeyReceivedDispatch(self) -> None:
"""
Widget receives F1 key.
L{Widget.functionKeyReceived} dispatches its input, a constant on
ServerProtocol, to a matched C{func_KEY} method.
"""
widget = RecordingWidget()
widget.functionKeyReceived(ServerProtocol.F1, None) # type: ignore[attr-defined]
self.assertEqual(["F1"], widget.triggered)

def test_widget_function_key_home(self) -> None:
"""
Widget receives HOME key.
"""
widget = RecordingWidget()
widget.functionKeyReceived(ServerProtocol.HOME, None) # type: ignore[attr-defined]
self.assertEqual(["HOME"], widget.triggered)

def test_widget_function_key_down_arrow(self) -> None:
"""
Widget receives DOWN_ARROW key.
"""
widget = RecordingWidget()
widget.functionKeyReceived(ServerProtocol.DOWN_ARROW, None) # type: ignore[attr-defined]
self.assertEqual(["DOWN_ARROW"], widget.triggered)

def test_widget_function_key_up_arrow(self) -> None:
"""
Widget receives UP_ARROW key.
"""
widget = RecordingWidget()
widget.functionKeyReceived(ServerProtocol.UP_ARROW, None) # type: ignore[attr-defined]
self.assertEqual(["UP_ARROW"], widget.triggered)

def test_widget_function_key_pgdn(self) -> None:
"""
Widget receives PGDN key.
"""
widget = RecordingWidget()
widget.functionKeyReceived(ServerProtocol.PGDN, None) # type: ignore[attr-defined]
self.assertEqual(["PGDN"], widget.triggered)

def test_widget_function_key_pgup(self) -> None:
"""
Widget receives PGUP key.
"""
widget = RecordingWidget()
widget.functionKeyReceived(ServerProtocol.PGUP, None) # type: ignore[attr-defined]
self.assertEqual(["PGUP"], widget.triggered)
def checkOneKey(key: str) -> None:
widget.functionKeyReceived(getattr(ServerProtocol, key), None)
self.assertEqual([key], widget.triggered)
widget.triggered.clear()

checkOneKey("F1")
checkOneKey("HOME")
checkOneKey("DOWN_ARROW")
checkOneKey("UP_ARROW")
checkOneKey("PGDN")
checkOneKey("PGUP")

0 comments on commit 002c528

Please sign in to comment.