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