1 """Tests related to the branches widget"""
3 from cola
.widgets
import branch
5 from .helper
import Mock
8 def test_create_tree_entries():
15 root
= branch
.create_tree_entries(names
)
17 actual
= len(root
.children
)
18 assert expect
== actual
21 abc
= root
.children
[0]
24 assert expect
== actual
27 assert expect
== actual
30 assert expect
== actual
33 cat
= root
.children
[1]
36 assert expect
== actual
37 assert cat
.refname
is None
39 actual
= len(cat
.children
)
40 assert expect
== actual
43 cat_abc
= cat
.children
[0]
45 actual
= cat_abc
.basename
46 assert expect
== actual
48 actual
= cat_abc
.refname
49 assert expect
== actual
51 actual
= cat_abc
.children
52 assert expect
== actual
55 cat_def
= cat
.children
[1]
57 actual
= cat_def
.basename
58 assert expect
== actual
60 actual
= cat_def
.refname
61 assert expect
== actual
63 actual
= cat_def
.children
64 assert expect
== actual
67 xyz
= root
.children
[2]
70 assert expect
== actual
71 assert xyz
.refname
is None
73 actual
= len(xyz
.children
)
74 assert expect
== actual
77 xyz_xyz
= xyz
.children
[0]
79 actual
= xyz_xyz
.basename
80 assert expect
== actual
83 actual
= xyz_xyz
.refname
84 assert expect
== actual
87 actual
= xyz_xyz
.children
88 assert expect
== actual
91 def test_create_name_dict():
92 """Test transforming unix path-like names into a nested dict"""
94 'top_1/child_1/child_1_1',
95 'top_1/child_1/child_1_2',
96 'top_1/child_2/child_2_1/child_2_1_1',
97 'top_1/child_2/child_2_1/child_2_1_2',
99 inner_child
= {'child_2_1_2': {}, 'child_2_1_1': {}}
102 'child_1': {'child_1_2': {}, 'child_1_1': {}},
103 'child_2': {'child_2_1': inner_child
},
106 actual
= branch
.create_name_dict(branches
)
107 assert expect
== actual
110 def test_create_toplevel_item():
116 tree
= branch
.create_tree_entries(names
)
117 tree
.basename
= 'top'
118 top
= branch
.create_toplevel_item(tree
)
122 assert expect
== actual
125 actual
= top
.childCount()
126 assert expect
== actual
129 actual
= top
.child(0).name
130 assert expect
== actual
133 actual
= top
.child(0).refname
134 assert expect
== actual
137 actual
= top
.child(1).name
138 assert expect
== actual
140 assert top
.child(1).refname
is None
143 actual
= top
.child(1).childCount()
144 assert expect
== actual
147 actual
= top
.child(1).child(0).name
148 assert expect
== actual
151 actual
= top
.child(1).child(1).name
152 assert expect
== actual
154 expect
= 'child_2/child_2_1'
155 actual
= top
.child(1).child(0).refname
156 assert expect
== actual
158 expect
= 'child_2/child_2_2'
159 actual
= top
.child(1).child(1).refname
160 assert expect
== actual
163 def test_get_toplevel_item():
164 items
= _create_top_item()
165 actual
= branch
.get_toplevel_item(items
['child_1'])
166 assert items
['top'] is actual
168 actual
= branch
.get_toplevel_item(items
['sub_child_2_1'])
169 assert items
['top'] is actual
172 def test_refname_attribute():
173 items
= _create_top_item()
175 actual
= items
['child_1'].refname
177 assert expect
== actual
179 actual
= items
['sub_child_2_2'].refname
180 expect
= 'child_2/sub_child_2_2'
181 assert expect
== actual
184 def test_should_return_a_valid_child_on_find_child():
185 """Test the find_child function."""
186 items
= _create_top_item()
187 child
= branch
.find_by_refname(items
['top'], 'child_1')
188 assert child
.refname
== 'child_1'
190 child
= branch
.find_by_refname(items
['top'], 'child_2/sub_child_2_2')
191 assert child
.name
== 'sub_child_2_2'
194 def test_should_return_empty_state_on_save_state():
195 """Test the save_state function."""
196 top
= _create_item('top', None, False)
197 tree_helper
= branch
.BranchesTreeHelper(Mock())
198 actual
= tree_helper
.save_state(top
)
199 assert {'top': {'children': {}, 'expanded': False, 'selected': False}} == actual
202 def test_should_return_a_valid_state_on_save_state():
203 """Test the save_state function."""
204 items
= _create_top_item()
205 tree_helper
= branch
.BranchesTreeHelper(Mock())
206 actual
= tree_helper
.save_state(items
['top'])
236 assert expect
== actual
239 def _create_top_item():
240 top
= _create_item('top', None, True)
241 child_1
= _create_item('child_1', 'child_1', False)
242 child_2
= _create_item('child_2', None, True)
243 sub_child_2_1
= _create_item('sub_child_2_1', 'child_2/sub_child_2_1', False)
244 sub_child_2_2
= _create_item('sub_child_2_2', 'child_2/sub_child_2_2', False)
246 child_2
.addChildren([sub_child_2_1
, sub_child_2_2
])
247 top
.addChildren([child_1
, child_2
])
252 'sub_child_2_1': sub_child_2_1
,
253 'sub_child_2_2': sub_child_2_2
,
257 def _create_item(name
, refname
, expanded
):
258 item
= branch
.BranchTreeWidgetItem(name
, refname
=refname
)
259 item
.isExpanded
= Mock(return_value
=expanded
)