test_widgets.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #
  2. # Copyright (c) Daniel Sheffield 2023
  3. #
  4. # All rights reserved
  5. #
  6. # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
  7. from typing import Dict, List, Tuple
  8. from pytest import fixture, mark
  9. from app.parse_recipe import parse_recipe
  10. from app.data.QueryManager import QueryManager
  11. from . import ALL_UNITS
  12. from app.widgets import (
  13. SuggestionPopup,
  14. FlowBarGraphWithVScale,
  15. NoTabCheckBox,
  16. )
  17. @fixture
  18. def signals():
  19. return []
  20. @fixture
  21. def signal(signals):
  22. return lambda x, *a: signals.append(x)
  23. @fixture
  24. def tracker():
  25. return dict()
  26. @fixture
  27. def options():
  28. return ['a', 'b', 'c']
  29. @fixture
  30. def suggestion_popup(tracker, options, signal):
  31. popup = SuggestionPopup('identifier', options, lambda x, y: tracker.update({x: y}))
  32. popup._emit = signal
  33. return popup
  34. @mark.parametrize('pos', [0, 1, 2])
  35. def test_suggestion_popup_choice(suggestion_popup: SuggestionPopup, tracker: Dict[str, str], options: List[str], pos: int):
  36. while pos > 0:
  37. pos -= 1
  38. suggestion_popup.keypress((0,0), 'tab')
  39. assert len(tracker.keys()) == 0
  40. suggestion_popup.keypress((0,0), 'enter')
  41. assert tracker['identifier'] == options[pos]
  42. def test_suggestion_popup_banish(suggestion_popup: SuggestionPopup, tracker: Dict[str, str], options: List[str], signals):
  43. suggestion_popup.keypress((0,0), 'esc')
  44. assert len(tracker.keys()) == 0
  45. assert 'close' in signals
  46. @fixture
  47. def bargraph():
  48. return FlowBarGraphWithVScale(
  49. 50, 14,
  50. ['bg','popup_focus', 'badge_neutral' ],
  51. hatt=['dark red', 'dark red', 'dark red']
  52. )
  53. @mark.parametrize('vscale', [
  54. list(map(float, (1, 2, 3))),
  55. None,
  56. ])
  57. def test_flow_bar_graph_with_vscale(bargraph: FlowBarGraphWithVScale, vscale: Tuple[float]):
  58. bargraph.set_data([[1],[2],[3]], 3, vscale=vscale)
  59. bargraph.set_caption('my data')
  60. assert bargraph.total_width == 50
  61. assert bargraph.canvas_width == 50 - bargraph._vscale_width
  62. assert bargraph.height == 14
  63. bargraph.set_bar_width(2)
  64. bargraph.render((100,))
  65. @fixture
  66. def notabcheckbox():
  67. return NoTabCheckBox('label')
  68. def test_no_tab_checkbox(notabcheckbox: NoTabCheckBox):
  69. state = notabcheckbox.get_state()
  70. notabcheckbox.keypress((0,0), 'tab')
  71. assert notabcheckbox.get_state() == state
  72. notabcheckbox.keypress((0,0), 'enter')
  73. assert notabcheckbox.get_state() != state