56
class UITests(TestCase):
56
class UITests(tests.TestCase):
58
def test_silent_factory(self):
59
ui = _mod_ui.SilentUIFactory()
61
self.assertEqual(None,
62
self.apply_redirected(None, stdout, stdout,
64
self.assertEqual('', stdout.getvalue())
65
self.assertEqual(None,
66
self.apply_redirected(None, stdout, stdout,
68
u'Hello\u1234 %(user)s',
70
self.assertEqual('', stdout.getvalue())
58
72
def test_text_factory_ascii_password(self):
59
ui = TestUIFactory(stdin='secret\n', stdout=StringIOWrapper(),
60
stderr=StringIOWrapper())
73
ui = tests.TestUIFactory(stdin='secret\n',
74
stdout=tests.StringIOWrapper(),
75
stderr=tests.StringIOWrapper())
61
76
pb = ui.nested_progress_bar()
63
78
self.assertEqual('secret',
78
93
We can't predict what encoding users will have for stdin, so we force
79
94
it to utf8 to test that we transport the password correctly.
81
ui = TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
82
stdout=StringIOWrapper(),
83
stderr=StringIOWrapper())
96
ui = tests.TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
97
stdout=tests.StringIOWrapper(),
98
stderr=tests.StringIOWrapper())
84
99
ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
85
100
pb = ui.nested_progress_bar()
207
225
self.assertEqual(True, factory.get_boolean(""))
208
226
self.assertEqual(False, factory.get_boolean(""))
209
227
self.assertEqual(False, factory.get_boolean(""))
228
self.assertEqual(False, factory.get_boolean(""))
229
self.assertEqual(True, factory.get_boolean(""))
210
230
self.assertEqual("foo\n", factory.stdin.read())
211
231
# stdin should be empty
212
232
self.assertEqual('', factory.stdin.readline())
234
def test_silent_ui_getbool(self):
235
factory = _mod_ui.SilentUIFactory()
236
self.assert_get_bool_acceptance_of_user_input(factory)
238
def test_silent_factory_prompts_silently(self):
239
factory = _mod_ui.SilentUIFactory()
241
factory.stdin = StringIO("y\n")
242
self.assertEqual(True,
243
self.apply_redirected(None, stdout, stdout,
244
factory.get_boolean, "foo"))
245
self.assertEqual("", stdout.getvalue())
246
# stdin should be empty
247
self.assertEqual('', factory.stdin.readline())
214
249
def test_text_ui_getbool(self):
215
250
factory = TextUIFactory(None, None, None)
216
251
self.assert_get_bool_acceptance_of_user_input(factory)
272
307
self.assertEqual('', factory.stdin.readline())
274
309
def test_text_ui_getusername_utf8(self):
275
ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
276
stdout=StringIOWrapper(), stderr=StringIOWrapper())
310
ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
311
stdout=tests.StringIOWrapper(),
312
stderr=tests.StringIOWrapper())
277
313
ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
278
314
pb = ui.nested_progress_bar()
316
352
NotImplementedError,
317
353
self.apply_redirected,
318
354
None, stdout, stdout, factory.get_boolean, "foo")
357
class TestTextProgressView(tests.TestCase):
358
"""Tests for text display of progress bars.
360
# XXX: These might be a bit easier to write if the rendering and
361
# state-maintaining parts of TextProgressView were more separate, and if
362
# the progress task called back directly to its own view not to the ui
363
# factory. -- mbp 20090312
365
def _make_factory(self):
367
uif = TextUIFactory(stderr=out)
368
uif._progress_view._width = 80
371
def test_render_progress_easy(self):
372
"""Just one task and one quarter done"""
373
out, uif = self._make_factory()
374
task = uif.nested_progress_bar()
375
task.update('reticulating splines', 5, 20)
377
'\r[####/ ] reticulating splines 5/20 \r'
380
def test_render_progress_nested(self):
381
"""Tasks proportionally contribute to overall progress"""
382
out, uif = self._make_factory()
383
task = uif.nested_progress_bar()
384
task.update('reticulating splines', 0, 2)
385
task2 = uif.nested_progress_bar()
386
task2.update('stage2', 1, 2)
387
# so we're in the first half of the main task, and half way through
390
r'[####\ ] reticulating splines:stage2 1/2'
391
, uif._progress_view._render_line())
392
# if the nested task is complete, then we're all the way through the
393
# first half of the overall work
394
task2.update('stage2', 2, 2)
396
r'[#########| ] reticulating splines:stage2 2/2'
397
, uif._progress_view._render_line())
399
def test_render_progress_sub_nested(self):
400
"""Intermediate tasks don't mess up calculation."""
401
out, uif = self._make_factory()
402
task_a = uif.nested_progress_bar()
403
task_a.update('a', 0, 2)
404
task_b = uif.nested_progress_bar()
406
task_c = uif.nested_progress_bar()
407
task_c.update('c', 1, 2)
408
# the top-level task is in its first half; the middle one has no
409
# progress indication, just a label; and the bottom one is half done,
410
# so the overall fraction is 1/4
412
r'[####| ] a:b:c 1/2'
413
, uif._progress_view._render_line())