Quellcode durchsuchen

add test for SuggestionPopup

Daniel Sheffield vor 1 Jahr
Ursprung
Commit
27f312b067
2 geänderte Dateien mit 53 neuen und 2 gelöschten Zeilen
  1. 1 2
      app/widgets.py
  2. 52 0
      test/test_widgets.py

+ 1 - 2
app/widgets.py

@@ -275,11 +275,10 @@ class SuggestionPopup(WidgetWrap):
         options: Iterable,
         apply_cb: Callable[[str, str], None]
     ):
-        self.apply_cb = lambda _, v: apply_cb(name, v)
         body = []
         for c in options:
             button = Button(c)
-            connect_signal(button, 'click', self.apply_cb, c)
+            connect_signal(button, 'click', lambda w: apply_cb(name, w.get_label()))
             connect_signal(button, 'click', lambda _: self._emit("close"))
             body.append(AttrMap(button, None, focus_map='popup_focus'))
         walker = SimpleFocusListWalker(body, wrap_around=False)

+ 52 - 0
test/test_widgets.py

@@ -0,0 +1,52 @@
+#
+# Copyright (c) Daniel Sheffield 2023
+#
+# All rights reserved
+#
+# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
+from typing import Callable, Dict, Iterable, List
+from pytest import fixture, mark
+from app.parse_recipe import parse_recipe
+from app.data.QueryManager import QueryManager
+from app.rest import ALL_UNITS
+
+from app.widgets import SuggestionPopup
+from urwid import connect_signal
+from urwid import SimpleFocusListWalker
+from additional_urwid_widgets import IndicativeListBox
+
+@fixture
+def signals():
+    return []
+
+@fixture
+def signal(signals):
+    return lambda x, *a: signals.append(x)
+
+@fixture
+def tracker():
+    return dict()
+
+@fixture
+def options():
+    return ['a', 'b', 'c']
+
+@fixture
+def suggestion_popup(tracker, options, signal):
+    popup = SuggestionPopup('identifier', options, lambda x, y: tracker.update({x: y}))
+    popup._emit = signal
+    return popup
+
+@mark.parametrize('pos', [0, 1, 2])
+def test_suggestion_popup_choice(suggestion_popup: SuggestionPopup, tracker: Dict[str, str], options: List[str], pos: int):
+    while pos > 0:
+        pos -= 1
+        suggestion_popup.keypress((0,0), 'tab')
+    assert len(tracker.keys()) == 0
+    suggestion_popup.keypress((0,0), 'enter')
+    assert tracker['identifier'] == options[pos]
+    
+def test_suggestion_popup_banish(suggestion_popup: SuggestionPopup, tracker: Dict[str, str], options: List[str], signals):
+    suggestion_popup.keypress((0,0), 'esc')
+    assert len(tracker.keys()) == 0
+    assert 'close' in signals