1 """Tests related to the branches widget"""
2 from __future__
import absolute_import
, division
, print_function
, unicode_literals
4 from cola
.widgets
import branch
6 from .helper
import Mock
9 def test_create_tree_entries():
16 root
= branch
.create_tree_entries(names
)
18 actual
= len(root
.children
)
19 assert expect
== actual
22 abc
= root
.children
[0]
25 assert expect
== actual
28 assert expect
== actual
31 assert expect
== actual
34 cat
= root
.children
[1]
37 assert expect
== actual
38 assert cat
.refname
is None
40 actual
= len(cat
.children
)
41 assert expect
== actual
44 cat_abc
= cat
.children
[0]
46 actual
= cat_abc
.basename
47 assert expect
== actual
49 actual
= cat_abc
.refname
50 assert expect
== actual
52 actual
= cat_abc
.children
53 assert expect
== actual
56 cat_def
= cat
.children
[1]
58 actual
= cat_def
.basename
59 assert expect
== actual
61 actual
= cat_def
.refname
62 assert expect
== actual
64 actual
= cat_def
.children
65 assert expect
== actual
68 xyz
= root
.children
[2]
71 assert expect
== actual
72 assert xyz
.refname
is None
74 actual
= len(xyz
.children
)
75 assert expect
== actual
78 xyz_xyz
= xyz
.children
[0]
80 actual
= xyz_xyz
.basename
81 assert expect
== actual
84 actual
= xyz_xyz
.refname
85 assert expect
== actual
88 actual
= xyz_xyz
.children
89 assert expect
== actual
92 def test_create_name_dict():
93 """Test transforming unix path-like names into a nested dict"""
95 'top_1/child_1/child_1_1',
96 'top_1/child_1/child_1_2',
97 'top_1/child_2/child_2_1/child_2_1_1',
98 'top_1/child_2/child_2_1/child_2_1_2',
100 inner_child
= {'child_2_1_2': {}, 'child_2_1_1': {}}
103 'child_1': {'child_1_2': {}, 'child_1_1': {}},
104 'child_2': {'child_2_1': inner_child
},
107 actual
= branch
.create_name_dict(branches
)
108 assert expect
== actual
111 def test_create_toplevel_item():
117 tree
= branch
.create_tree_entries(names
)
118 tree
.basename
= 'top'
119 top
= branch
.create_toplevel_item(tree
)
123 assert expect
== actual
126 actual
= top
.childCount()
127 assert expect
== actual
130 actual
= top
.child(0).name
131 assert expect
== actual
134 actual
= top
.child(0).refname
135 assert expect
== actual
138 actual
= top
.child(1).name
139 assert expect
== actual
141 assert top
.child(1).refname
is None
144 actual
= top
.child(1).childCount()
145 assert expect
== actual
148 actual
= top
.child(1).child(0).name
149 assert expect
== actual
152 actual
= top
.child(1).child(1).name
153 assert expect
== actual
155 expect
= 'child_2/child_2_1'
156 actual
= top
.child(1).child(0).refname
157 assert expect
== actual
159 expect
= 'child_2/child_2_2'
160 actual
= top
.child(1).child(1).refname
161 assert expect
== actual
164 def test_get_toplevel_item():
165 items
= _create_top_item()
166 actual
= branch
.get_toplevel_item(items
['child_1'])
167 assert items
['top'] is actual
169 actual
= branch
.get_toplevel_item(items
['sub_child_2_1'])
170 assert items
['top'] is actual
173 def test_refname_attribute():
174 items
= _create_top_item()
176 actual
= items
['child_1'].refname
178 assert expect
== actual
180 actual
= items
['sub_child_2_2'].refname
181 expect
= 'child_2/sub_child_2_2'
182 assert expect
== actual
185 def test_should_return_a_valid_child_on_find_child():
186 """Test the find_child function."""
187 items
= _create_top_item()
188 child
= branch
.find_by_refname(items
['top'], 'child_1')
189 assert child
.refname
== 'child_1'
191 child
= branch
.find_by_refname(items
['top'], 'child_2/sub_child_2_2')
192 assert child
.name
== 'sub_child_2_2'
195 def test_should_return_empty_state_on_save_state():
196 """Test the save_state function."""
197 top
= _create_item('top', None, False)
198 tree_helper
= branch
.BranchesTreeHelper()
199 actual
= tree_helper
.save_state(top
)
200 assert {'top': {}} == actual
203 def test_should_return_a_valid_state_on_save_state():
204 """Test the save_state function."""
205 items
= _create_top_item()
206 tree_helper
= branch
.BranchesTreeHelper()
207 actual
= tree_helper
.save_state(items
['top'])
211 'child_2': {'sub_child_2_1': {}, 'sub_child_2_2': {}},
214 assert expect
== actual
217 def _create_top_item():
218 top
= _create_item('top', None, True)
219 child_1
= _create_item('child_1', 'child_1', False)
220 child_2
= _create_item('child_2', None, True)
221 sub_child_2_1
= _create_item('sub_child_2_1', 'child_2/sub_child_2_1', False)
222 sub_child_2_2
= _create_item('sub_child_2_2', 'child_2/sub_child_2_2', False)
224 child_2
.addChildren([sub_child_2_1
, sub_child_2_2
])
225 top
.addChildren([child_1
, child_2
])
230 'sub_child_2_1': sub_child_2_1
,
231 'sub_child_2_2': sub_child_2_2
,
235 def _create_item(name
, refname
, expanded
):
236 item
= branch
.BranchTreeWidgetItem(name
, refname
=refname
)
237 item
.isExpanded
= Mock(return_value
=expanded
)