Skip to content

Commit 46035b4

Browse files
committed
Test selection containers more comprehensively.
1 parent ab69b2e commit 46035b4

File tree

1 file changed

+134
-25
lines changed

1 file changed

+134
-25
lines changed

python/ipywidgets/ipywidgets/widgets/tests/test_selectioncontainer.py

Lines changed: 134 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,164 @@
55

66
from traitlets import TraitError
77

8-
from ipywidgets.widgets import Accordion, Tab, Stack, HTML
8+
from ipywidgets.widgets import Accordion, Tab, Stacked, HTML
99

10+
class TestTab(TestCase):
11+
12+
def setUp(self):
13+
self.children = [HTML('0'), HTML('1')]
14+
self.widget = Tab
15+
16+
def test_selected_index_none(self):
17+
widget = self.widget(self.children, selected_index=None)
18+
state = widget.get_state()
19+
assert state['selected_index'] == 0
20+
21+
def test_selected_index_default(self):
22+
widget = self.widget(self.children)
23+
state = widget.get_state()
24+
assert state['selected_index'] == 0
25+
26+
def test_selected_index(self):
27+
widget = self.widget(self.children, selected_index=1)
28+
state = widget.get_state()
29+
assert state['selected_index'] == 1
30+
31+
def test_selected_index_out_of_bounds(self):
32+
with self.assertRaises(TraitError):
33+
self.widget(self.children, selected_index=-1)
34+
35+
def test_children_position_argument(self):
36+
self.widget(self.children)
37+
38+
def test_titles(self):
39+
widget = self.widget(self.children, selected_index=None)
40+
assert widget.get_state()['titles'] == ('', '')
41+
assert widget.titles == ('', '')
42+
43+
widget.set_title(1, 'Title 1')
44+
assert widget.get_state()['titles'] == ('', 'Title 1')
45+
assert widget.titles[1] == 'Title 1'
46+
assert widget.get_title(1) == 'Title 1'
47+
48+
# Backwards compatible with 7.x api
49+
widget.set_title(1, None)
50+
assert widget.get_state()['titles'] == ('', '')
51+
assert widget.titles[1] == ''
52+
assert widget.get_title(1) == ''
53+
54+
with self.assertRaises(IndexError):
55+
widget.set_title(2, 'out of bounds')
56+
with self.assertRaises(IndexError):
57+
widget.get_title(2)
58+
59+
widget.children = tuple(widget.children[:1])
60+
assert len(widget.children) == 1
61+
assert widget.titles == ('',)
1062

1163
class TestAccordion(TestCase):
1264

1365
def setUp(self):
1466
self.children = [HTML('0'), HTML('1')]
67+
self.widget = Accordion
1568

1669
def test_selected_index_none(self):
17-
accordion = Accordion(self.children, selected_index=None)
18-
state = accordion.get_state()
70+
widget = self.widget(self.children, selected_index=None)
71+
state = widget.get_state()
72+
assert state['selected_index'] is None
73+
74+
def test_selected_index_default(self):
75+
widget = self.widget(self.children)
76+
state = widget.get_state()
1977
assert state['selected_index'] is None
2078

2179
def test_selected_index(self):
22-
accordion = Accordion(self.children, selected_index=1)
23-
state = accordion.get_state()
80+
widget = self.widget(self.children, selected_index=1)
81+
state = widget.get_state()
2482
assert state['selected_index'] == 1
2583

2684
def test_selected_index_out_of_bounds(self):
2785
with self.assertRaises(TraitError):
28-
Accordion(self.children, selected_index=-1)
86+
self.widget(self.children, selected_index=-1)
2987

3088
def test_children_position_argument(self):
31-
Accordion(self.children)
32-
Tab(self.children)
33-
Stack(self.children)
89+
self.widget(self.children)
3490

91+
def test_titles(self):
92+
widget = self.widget(self.children, selected_index=None)
93+
assert widget.get_state()['titles'] == ('', '')
94+
assert widget.titles == ('', '')
95+
96+
widget.set_title(1, 'Title 1')
97+
assert widget.get_state()['titles'] == ('', 'Title 1')
98+
assert widget.titles[1] == 'Title 1'
99+
assert widget.get_title(1) == 'Title 1'
100+
101+
# Backwards compatible with 7.x api
102+
widget.set_title(1, None)
103+
assert widget.get_state()['titles'] == ('', '')
104+
assert widget.titles[1] == ''
105+
assert widget.get_title(1) == ''
106+
107+
with self.assertRaises(IndexError):
108+
widget.set_title(2, 'out of bounds')
109+
with self.assertRaises(IndexError):
110+
widget.get_title(2)
111+
112+
widget.children = tuple(widget.children[:1])
113+
assert len(widget.children) == 1
114+
assert widget.titles == ('',)
115+
116+
class TestStacked(TestCase):
117+
118+
def setUp(self):
119+
self.children = [HTML('0'), HTML('1')]
120+
self.widget = Stacked
121+
122+
def test_selected_index_none(self):
123+
widget = self.widget(self.children, selected_index=None)
124+
state = widget.get_state()
125+
assert state['selected_index'] is None
126+
127+
def test_selected_index_default(self):
128+
widget = self.widget(self.children)
129+
state = widget.get_state()
130+
assert state['selected_index'] is None
131+
132+
def test_selected_index(self):
133+
widget = self.widget(self.children, selected_index=1)
134+
state = widget.get_state()
135+
assert state['selected_index'] == 1
136+
137+
def test_selected_index_out_of_bounds(self):
138+
with self.assertRaises(TraitError):
139+
self.widget(self.children, selected_index=-1)
140+
141+
def test_children_position_argument(self):
142+
self.widget(self.children)
35143

36144
def test_titles(self):
37-
accordion = Accordion(self.children, selected_index=None)
38-
assert accordion.get_state()['titles'] == ('', '')
39-
assert accordion.titles == ('', '')
145+
widget = self.widget(self.children, selected_index=None)
146+
assert widget.get_state()['titles'] == ('', '')
147+
assert widget.titles == ('', '')
40148

41-
accordion.set_title(1, 'Title 1')
42-
assert accordion.get_state()['titles'] == ('', 'Title 1')
43-
assert accordion.titles[1] == 'Title 1'
44-
assert accordion.get_title(1) == 'Title 1'
149+
widget.set_title(1, 'Title 1')
150+
assert widget.get_state()['titles'] == ('', 'Title 1')
151+
assert widget.titles[1] == 'Title 1'
152+
assert widget.get_title(1) == 'Title 1'
45153

46154
# Backwards compatible with 7.x api
47-
accordion.set_title(1, None)
48-
assert accordion.get_state()['titles'] == ('', '')
49-
assert accordion.titles[1] == ''
50-
assert accordion.get_title(1) == ''
155+
widget.set_title(1, None)
156+
assert widget.get_state()['titles'] == ('', '')
157+
assert widget.titles[1] == ''
158+
assert widget.get_title(1) == ''
51159

52160
with self.assertRaises(IndexError):
53-
accordion.set_title(2, 'out of bounds')
161+
widget.set_title(2, 'out of bounds')
54162
with self.assertRaises(IndexError):
55-
accordion.get_title(2)
163+
widget.get_title(2)
164+
165+
widget.children = tuple(widget.children[:1])
166+
assert len(widget.children) == 1
167+
assert widget.titles == ('',)
56168

57-
accordion.children = tuple(accordion.children[:1])
58-
assert len(accordion.children) == 1
59-
assert accordion.titles == ('',)

0 commit comments

Comments
 (0)