git-cola v4.9.0
[git-cola.git] / test / branch_test.py
blob246107887423247d28af3a205a4c78da00607890
1 """Tests related to the branches widget"""
3 from cola.widgets import branch
5 from .helper import Mock
8 def test_create_tree_entries():
9 names = [
10 'abc',
11 'cat/abc',
12 'cat/def',
13 'xyz/xyz',
15 root = branch.create_tree_entries(names)
16 expect = 3
17 actual = len(root.children)
18 assert expect == actual
20 # 'abc'
21 abc = root.children[0]
22 expect = 'abc'
23 actual = abc.basename
24 assert expect == actual
25 expect = 'abc'
26 actual = abc.refname
27 assert expect == actual
28 expect = []
29 actual = abc.children
30 assert expect == actual
32 # 'cat'
33 cat = root.children[1]
34 expect = 'cat'
35 actual = 'cat'
36 assert expect == actual
37 assert cat.refname is None
38 expect = 2
39 actual = len(cat.children)
40 assert expect == actual
42 # 'cat/abc'
43 cat_abc = cat.children[0]
44 expect = 'abc'
45 actual = cat_abc.basename
46 assert expect == actual
47 expect = 'cat/abc'
48 actual = cat_abc.refname
49 assert expect == actual
50 expect = []
51 actual = cat_abc.children
52 assert expect == actual
54 # 'cat/def'
55 cat_def = cat.children[1]
56 expect = 'def'
57 actual = cat_def.basename
58 assert expect == actual
59 expect = 'cat/def'
60 actual = cat_def.refname
61 assert expect == actual
62 expect = []
63 actual = cat_def.children
64 assert expect == actual
66 # 'xyz'
67 xyz = root.children[2]
68 expect = 'xyz'
69 actual = xyz.basename
70 assert expect == actual
71 assert xyz.refname is None
72 expect = 1
73 actual = len(xyz.children)
74 assert expect == actual
76 # 'xyz/xyz'
77 xyz_xyz = xyz.children[0]
78 expect = 'xyz'
79 actual = xyz_xyz.basename
80 assert expect == actual
82 expect = 'xyz/xyz'
83 actual = xyz_xyz.refname
84 assert expect == actual
86 expect = []
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"""
93 branches = [
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': {}}
100 expect = {
101 'top_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():
111 names = [
112 'child_1',
113 'child_2/child_2_1',
114 'child_2/child_2_2',
116 tree = branch.create_tree_entries(names)
117 tree.basename = 'top'
118 top = branch.create_toplevel_item(tree)
120 expect = 'top'
121 actual = top.name
122 assert expect == actual
124 expect = 2
125 actual = top.childCount()
126 assert expect == actual
128 expect = 'child_1'
129 actual = top.child(0).name
130 assert expect == actual
132 expect = 'child_1'
133 actual = top.child(0).refname
134 assert expect == actual
136 expect = 'child_2'
137 actual = top.child(1).name
138 assert expect == actual
140 assert top.child(1).refname is None
142 expect = 2
143 actual = top.child(1).childCount()
144 assert expect == actual
146 expect = 'child_2_1'
147 actual = top.child(1).child(0).name
148 assert expect == actual
150 expect = 'child_2_2'
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
176 expect = 'child_1'
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'])
207 expect = {
208 'top': {
209 'children': {
210 'child_1': {
211 'children': {},
212 'expanded': False,
213 'selected': False,
215 'child_2': {
216 'children': {
217 'sub_child_2_1': {
218 'children': {},
219 'expanded': False,
220 'selected': False,
222 'sub_child_2_2': {
223 'children': {},
224 'expanded': False,
225 'selected': False,
228 'expanded': True,
229 'selected': False,
232 'expanded': True,
233 'selected': False,
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])
249 return {
250 'top': top,
251 'child_1': child_1,
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)
260 return item