|
5 | 5 |
|
6 | 6 | from traitlets import TraitError
|
7 | 7 |
|
8 |
| -from ipywidgets.widgets import Accordion, Tab, Stack, HTML |
| 8 | +from ipywidgets.widgets import Accordion, Tab, Stacked, HTML |
9 | 9 |
|
| 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 == ('',) |
10 | 62 |
|
11 | 63 | class TestAccordion(TestCase):
|
12 | 64 |
|
13 | 65 | def setUp(self):
|
14 | 66 | self.children = [HTML('0'), HTML('1')]
|
| 67 | + self.widget = Accordion |
15 | 68 |
|
16 | 69 | 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() |
19 | 77 | assert state['selected_index'] is None
|
20 | 78 |
|
21 | 79 | 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() |
24 | 82 | assert state['selected_index'] == 1
|
25 | 83 |
|
26 | 84 | def test_selected_index_out_of_bounds(self):
|
27 | 85 | with self.assertRaises(TraitError):
|
28 |
| - Accordion(self.children, selected_index=-1) |
| 86 | + self.widget(self.children, selected_index=-1) |
29 | 87 |
|
30 | 88 | def test_children_position_argument(self):
|
31 |
| - Accordion(self.children) |
32 |
| - Tab(self.children) |
33 |
| - Stack(self.children) |
| 89 | + self.widget(self.children) |
34 | 90 |
|
| 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) |
35 | 143 |
|
36 | 144 | 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 == ('', '') |
40 | 148 |
|
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' |
45 | 153 |
|
46 | 154 | # 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) == '' |
51 | 159 |
|
52 | 160 | with self.assertRaises(IndexError):
|
53 |
| - accordion.set_title(2, 'out of bounds') |
| 161 | + widget.set_title(2, 'out of bounds') |
54 | 162 | 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 == ('',) |
56 | 168 |
|
57 |
| - accordion.children = tuple(accordion.children[:1]) |
58 |
| - assert len(accordion.children) == 1 |
59 |
| - assert accordion.titles == ('',) |
|
0 commit comments