test_param_widget.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import functools
  2. from pathlib import Path
  3. from PySide6.QtWidgets import QWidget
  4. from ..param_widgets import (
  5. BoolParamWidget,
  6. StrParamWidget,
  7. PosIntParamWidget,
  8. ChoiceParamWidget,
  9. PathParamWidget,
  10. load_parameter_widget,
  11. )
  12. from ..param_multival_widget import MultiValueInputWidget
  13. from ..utils import _NoValue
  14. def test_GooeyParamWidgetMixin():
  15. # can we set and get a supported value to/from any widget
  16. # through the GooeyParamWidgetMixin API
  17. for pw_factory, val, default in (
  18. (BoolParamWidget, False, True),
  19. (BoolParamWidget, False, None),
  20. (PosIntParamWidget, 4, 1),
  21. (functools.partial(PosIntParamWidget, True), 4, None),
  22. (StrParamWidget, 'dummy', 'mydefault'),
  23. (functools.partial(ChoiceParamWidget, ['a', 'b', 'c']), 'b', 'c'),
  24. (PathParamWidget, str(Path.cwd()), 'mypath'),
  25. (PathParamWidget, str(Path.cwd()), None),
  26. # cannot include MultiValueInputWidget, leads to Python segfault
  27. # on garbage collection?!
  28. (functools.partial(
  29. MultiValueInputWidget, PathParamWidget),
  30. [str(Path.cwd()), 'temp'],
  31. 'mypath'),
  32. (functools.partial(
  33. MultiValueInputWidget, PathParamWidget),
  34. [str(Path.cwd()), 'temp'],
  35. None),
  36. ):
  37. # this is how all parameter widgets are instantiated
  38. parent = QWidget() # we need parent to stick around,
  39. # so nothing gets picked up by GC
  40. pw = load_parameter_widget(
  41. parent,
  42. pw_factory,
  43. name='peewee',
  44. docs='EXPLAIN!',
  45. default=default,
  46. )
  47. # If nothing was set yet, we expect `_NoValue` as the "representation of
  48. # default" here:
  49. assert pw.get_gooey_param_spec() == {'peewee': _NoValue}, \
  50. f"Default value not retrieved from {pw_factory.__class__}"
  51. pw.set_gooey_param_value(val)
  52. # we get the set value back, not the default
  53. assert pw.get_gooey_param_spec() == {'peewee': val}