doc: use rst code blocks
[git-cola.git] / test / branch_test.py
blob089f1e2efd6aac3b6fa7e5edf83dd944b5ad5d0d
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():
10 names = [
11 'abc',
12 'cat/abc',
13 'cat/def',
14 'xyz/xyz',
16 root = branch.create_tree_entries(names)
17 expect = 3
18 actual = len(root.children)
19 assert expect == actual
21 # 'abc'
22 abc = root.children[0]
23 expect = 'abc'
24 actual = abc.basename
25 assert expect == actual
26 expect = 'abc'
27 actual = abc.refname
28 assert expect == actual
29 expect = []
30 actual = abc.children
31 assert expect == actual
33 # 'cat'
34 cat = root.children[1]
35 expect = 'cat'
36 actual = 'cat'
37 assert expect == actual
38 assert cat.refname is None
39 expect = 2
40 actual = len(cat.children)
41 assert expect == actual
43 # 'cat/abc'
44 cat_abc = cat.children[0]
45 expect = 'abc'
46 actual = cat_abc.basename
47 assert expect == actual
48 expect = 'cat/abc'
49 actual = cat_abc.refname
50 assert expect == actual
51 expect = []
52 actual = cat_abc.children
53 assert expect == actual
55 # 'cat/def'
56 cat_def = cat.children[1]
57 expect = 'def'
58 actual = cat_def.basename
59 assert expect == actual
60 expect = 'cat/def'
61 actual = cat_def.refname
62 assert expect == actual
63 expect = []
64 actual = cat_def.children
65 assert expect == actual
67 # 'xyz'
68 xyz = root.children[2]
69 expect = 'xyz'
70 actual = xyz.basename
71 assert expect == actual
72 assert xyz.refname is None
73 expect = 1
74 actual = len(xyz.children)
75 assert expect == actual
77 # 'xyz/xyz'
78 xyz_xyz = xyz.children[0]
79 expect = 'xyz'
80 actual = xyz_xyz.basename
81 assert expect == actual
83 expect = 'xyz/xyz'
84 actual = xyz_xyz.refname
85 assert expect == actual
87 expect = []
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"""
94 branches = [
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': {}}
101 expect = {
102 'top_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():
112 names = [
113 'child_1',
114 'child_2/child_2_1',
115 'child_2/child_2_2',
117 tree = branch.create_tree_entries(names)
118 tree.basename = 'top'
119 top = branch.create_toplevel_item(tree)
121 expect = 'top'
122 actual = top.name
123 assert expect == actual
125 expect = 2
126 actual = top.childCount()
127 assert expect == actual
129 expect = 'child_1'
130 actual = top.child(0).name
131 assert expect == actual
133 expect = 'child_1'
134 actual = top.child(0).refname
135 assert expect == actual
137 expect = 'child_2'
138 actual = top.child(1).name
139 assert expect == actual
141 assert top.child(1).refname is None
143 expect = 2
144 actual = top.child(1).childCount()
145 assert expect == actual
147 expect = 'child_2_1'
148 actual = top.child(1).child(0).name
149 assert expect == actual
151 expect = 'child_2_2'
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
177 expect = 'child_1'
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'])
208 expect = {
209 'top': {
210 'child_1': {},
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])
227 return {
228 'top': top,
229 'child_1': child_1,
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)
238 return item