test_widget_selection.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. import inspect
  4. import warnings
  5. from unittest import TestCase
  6. from traitlets import TraitError
  7. from ipywidgets import Dropdown, SelectionSlider, Select
  8. class TestDropdown(TestCase):
  9. def test_construction(self):
  10. Dropdown()
  11. def test_deprecation_warning_mapping_options(self):
  12. with warnings.catch_warnings(record=True) as w:
  13. warnings.simplefilter("always")
  14. # Clearing the internal __warningregistry__ seems to be required for
  15. # Python 2 (but not for Python 3)
  16. module = inspect.getmodule(Dropdown)
  17. getattr(module, '__warningregistry__', {}).clear()
  18. Dropdown(options={'One': 1, 'Two': 2, 'Three': 3})
  19. assert len(w) > 0
  20. assert issubclass(w[-1].category, DeprecationWarning)
  21. assert "Support for mapping types has been deprecated" in str(w[-1].message)
  22. class TestSelectionSlider(TestCase):
  23. def test_construction(self):
  24. SelectionSlider(options=['a', 'b', 'c'])
  25. def test_index_trigger(self):
  26. slider = SelectionSlider(options=['a', 'b', 'c'])
  27. observations = []
  28. def f(change):
  29. observations.append(change.new)
  30. slider.observe(f, 'index')
  31. assert slider.index == 0
  32. slider.options = [4, 5, 6]
  33. assert slider.index == 0
  34. assert slider.value == 4
  35. assert slider.label == '4'
  36. assert observations == [0]
  37. class TestSelection(TestCase):
  38. def test_construction(self):
  39. select = Select(options=['a', 'b', 'c'])
  40. def test_index_trigger(self):
  41. select = Select(options=[1, 2, 3])
  42. observations = []
  43. def f(change):
  44. observations.append(change.new)
  45. select.observe(f, 'index')
  46. assert select.index == 0
  47. select.options = [4, 5, 6]
  48. assert select.index == 0
  49. assert select.value == 4
  50. assert select.label == '4'
  51. assert observations == [0]
  52. def test_duplicate(self):
  53. select = Select(options=['first', 1, 'dup', 'dup'])
  54. observations = []
  55. def f(change):
  56. observations.append(change.new)
  57. select.observe(f, 'index')
  58. select.index = 3
  59. assert select.index == 3
  60. assert select.value == 'dup'
  61. assert select.label == 'dup'
  62. assert observations == [3]
  63. select.index = 2
  64. assert select.index == 2
  65. assert select.value == 'dup'
  66. assert select.label == 'dup'
  67. assert observations == [3, 2]
  68. select.index = 0
  69. assert select.index == 0
  70. assert select.value == 'first'
  71. assert select.label == 'first'
  72. assert observations == [3, 2, 0]
  73. # picks the first matching value
  74. select.value = 'dup'
  75. assert select.index == 2
  76. assert select.value == 'dup'
  77. assert select.label == 'dup'
  78. assert observations == [3, 2, 0, 2]