14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
from ..progress import (
18
from cStringIO import StringIO
23
from brzlib.progress import (
23
from ..ui.text import (
26
from brzlib.ui.text import (
35
31
class TestTextProgressView(tests.TestCase):
36
32
"""Tests for text display of progress bars.
50
46
def make_view(self):
51
out = ui_testing.StringIOWithEncoding()
52
48
return out, self.make_view_only(out)
54
50
def make_task(self, parent_task, view, msg, curr, total):
67
63
task = self.make_task(None, view, 'reticulating splines', 5, 20)
68
64
view.show_progress(task)
70
'\r/ reticulating splines 5/20 \r', out.getvalue())
66
'\r/ reticulating splines 5/20 \r'
73
'\r/ reticulating splines 5/20 \r' +
74
'\r' + 79 * ' ' + '\r',
70
'\r/ reticulating splines 5/20 \r'
71
+ '\r' + 79 * ' ' + '\r',
77
74
def test_render_progress_no_bar(self):
81
78
task = self.make_task(None, view, 'reticulating splines', 5, 20)
82
79
view.show_progress(task)
84
'\r/ reticulating splines 5/20 \r', out.getvalue())
81
'\r/ reticulating splines 5/20 \r'
86
84
def test_render_progress_easy(self):
87
85
"""Just one task and one quarter done"""
90
88
task = self.make_task(None, view, 'reticulating splines', 5, 20)
91
89
view.show_progress(task)
93
'\r[####/ ] reticulating splines 5/20 \r', out.getvalue())
91
'\r[####/ ] reticulating splines 5/20 \r'
95
94
def test_render_progress_nested(self):
96
95
"""Tasks proportionally contribute to overall progress"""
102
101
# so we're in the first half of the main task, and half way through
104
103
self.assertEqual(
105
'[####- ] reticulating splines:stage2 1/2 ', view._render_line())
104
'[####- ] reticulating splines:stage2 1/2 '
105
, view._render_line())
106
106
# if the nested task is complete, then we're all the way through the
107
107
# first half of the overall work
108
108
task2.update('stage2', 2, 2)
109
109
self.assertEqual(
110
'[#########\\ ] reticulating splines:stage2 2/2 ', view._render_line())
110
'[#########\ ] reticulating splines:stage2 2/2 '
111
, view._render_line())
112
113
def test_render_progress_sub_nested(self):
113
114
"""Intermediate tasks don't mess up calculation."""
123
124
# progress indication, just a label; and the bottom one is half done,
124
125
# so the overall fraction is 1/4
125
126
self.assertEqual(
126
'[####| ] a:b:c 1/2 ', view._render_line())
127
'[####| ] a:b:c 1/2 '
128
, view._render_line())
128
130
def test_render_truncated(self):
129
131
# when the bar is too long for the terminal, we prefer not to truncate
134
136
task_a.update('start_' + 'a' * 200 + '_end', 2000, 5000)
135
137
line = view._render_line()
136
138
self.assertEqual(
137
'- start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
139
'- start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
139
141
self.assertEqual(len(line), 79)
141
144
def test_render_with_activity(self):
142
145
# if the progress view has activity, it's shown before the spinner
143
146
out, view = self.make_view()
145
148
view._last_transport_msg = ' 123kB 100kB/s '
146
149
line = view._render_line()
147
150
self.assertEqual(
150
153
self.assertEqual(len(line), 79)
152
155
task_a.update('start_' + 'a' * 200 + '_end', 2000, 5000)
153
156
view._last_transport_msg = ' 123kB 100kB/s '
154
157
line = view._render_line()
155
158
self.assertEqual(
156
' 123kB 100kB/s \\ start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
159
' 123kB 100kB/s \\ start_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.. 2000/5000',
158
161
self.assertEqual(len(line), 79)
160
163
def test_render_progress_unicode_enc_utf8(self):
161
out = ui_testing.StringIOWithEncoding()
164
out = tests.StringIOWrapper()
162
165
out.encoding = "utf-8"
163
166
view = self.make_view_only(out, 20)
164
167
task = self.make_task(None, view, u"\xa7", 0, 1)
165
168
view.show_progress(task)
166
self.assertEqual(u'\r/ \xa7 0/1 \r', out.getvalue())
169
self.assertEqual('\r/ \xc2\xa7 0/1 \r',
168
172
def test_render_progress_unicode_enc_missing(self):
169
out = codecs.getwriter("ascii")(io.BytesIO())
170
174
self.assertRaises(AttributeError, getattr, out, "encoding")
171
175
view = self.make_view_only(out, 20)
172
176
task = self.make_task(None, view, u"\xa7", 0, 1)
173
177
view.show_progress(task)
174
self.assertEqual(b'\r/ ? 0/1 \r', out.getvalue())
178
self.assertEqual('\r/ ? 0/1 \r',
176
181
def test_render_progress_unicode_enc_none(self):
177
out = ui_testing.StringIOWithEncoding()
182
out = tests.StringIOWrapper()
178
183
out.encoding = None
179
184
view = self.make_view_only(out, 20)
180
185
task = self.make_task(None, view, u"\xa7", 0, 1)
181
186
view.show_progress(task)
182
self.assertEqual(u'\r/ ? 0/1 \r', out.getvalue())
187
self.assertEqual('\r/ ? 0/1 \r',