test_param_widget.py 1.9 KB

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