~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-03-06 06:48:25 UTC
  • mfrom: (4070.8.6 debug-config)
  • Revision ID: pqm@pqm.ubuntu.com-20090306064825-kbpwggw21dygeix6
(mbp) debug_flags configuration option

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for the bzrlib ui
18
18
"""
28
28
from bzrlib.progress import (
29
29
    DotsProgressBar,
30
30
    ProgressBarStack,
31
 
    ProgressTask,
32
31
    TTYProgressBar,
33
32
    )
34
33
from bzrlib.symbol_versioning import (
44
43
    CLIUIFactory,
45
44
    SilentUIFactory,
46
45
    )
47
 
from bzrlib.ui.text import (
48
 
    TextProgressView,
49
 
    TextUIFactory,
50
 
    )
 
46
from bzrlib.ui.text import TextUIFactory
51
47
 
52
48
 
53
49
class UITests(TestCase):
253
249
            pb.tick()
254
250
        finally:
255
251
            pb.finished()
256
 
 
257
 
    def test_silent_ui_getusername(self):
258
 
        factory = SilentUIFactory()
259
 
        factory.stdin = StringIO("someuser\n\n")
260
 
        factory.stdout = StringIO()
261
 
        self.assertEquals(None, 
262
 
            factory.get_username(u'Hello\u1234 %(host)s', host=u'some\u1234'))
263
 
        self.assertEquals("", factory.stdout.getvalue())
264
 
        self.assertEquals("someuser\n\n", factory.stdin.getvalue())
265
 
 
266
 
    def test_text_ui_getusername(self):
267
 
        factory = TextUIFactory(None, None, None)
268
 
        factory.stdin = StringIO("someuser\n\n")
269
 
        factory.stdout = StringIO()
270
 
        factory.stdout.encoding = "utf8"
271
 
        # there is no output from the base factory
272
 
        self.assertEqual("someuser", 
273
 
            factory.get_username('Hello %(host)s', host='some'))
274
 
        self.assertEquals("Hello some: ", factory.stdout.getvalue())
275
 
        self.assertEqual("", factory.get_username("Gebruiker"))
276
 
        # stdin should be empty
277
 
        self.assertEqual('', factory.stdin.readline())
278
 
 
279
 
    def test_text_ui_getusername_utf8(self):
280
 
        ui = TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
281
 
                           stdout=StringIOWrapper())
282
 
        ui.stdin.encoding = "utf8"
283
 
        ui.stdout.encoding = ui.stdin.encoding
284
 
        pb = ui.nested_progress_bar()
285
 
        try:
286
 
            # there is no output from the base factory
287
 
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stdout,
288
 
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
289
 
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
290
 
            self.assertEquals(u"Hello\u1234 some\u1234: ", 
291
 
                ui.stdout.getvalue().decode("utf8"))
292
 
        finally:
293
 
            pb.finished()
294
 
 
295
 
 
296
 
class TestTextProgressView(TestCase):
297
 
    """Tests for text display of progress bars.
298
 
    """
299
 
    # XXX: These might be a bit easier to write if the rendering and
300
 
    # state-maintaining parts of TextProgressView were more separate, and if
301
 
    # the progress task called back directly to its own view not to the ui
302
 
    # factory. -- mbp 20090312
303
 
    
304
 
    def _make_factory(self):
305
 
        out = StringIO()
306
 
        uif = TextUIFactory(stderr=out)
307
 
        uif._progress_view._width = 80
308
 
        return out, uif
309
 
 
310
 
    def test_render_progress_easy(self):
311
 
        """Just one task and one quarter done"""
312
 
        out, uif = self._make_factory()
313
 
        task = uif.nested_progress_bar()
314
 
        task.update('reticulating splines', 5, 20)
315
 
        self.assertEqual(
316
 
'\r[####/               ] reticulating splines 5/20                               \r'
317
 
            , out.getvalue())
318
 
 
319
 
    def test_render_progress_nested(self):
320
 
        """Tasks proportionally contribute to overall progress"""
321
 
        out, uif = self._make_factory()
322
 
        task = uif.nested_progress_bar()
323
 
        task.update('reticulating splines', 0, 2)
324
 
        task2 = uif.nested_progress_bar()
325
 
        task2.update('stage2', 1, 2)
326
 
        # so we're in the first half of the main task, and half way through
327
 
        # that
328
 
        self.assertEqual(
329
 
r'[####\               ] reticulating splines:stage2 1/2'
330
 
            , uif._progress_view._render_line())
331
 
        # if the nested task is complete, then we're all the way through the
332
 
        # first half of the overall work
333
 
        task2.update('stage2', 2, 2)
334
 
        self.assertEqual(
335
 
r'[#########|          ] reticulating splines:stage2 2/2'
336
 
            , uif._progress_view._render_line())
337
 
 
338
 
    def test_render_progress_sub_nested(self):
339
 
        """Intermediate tasks don't mess up calculation."""
340
 
        out, uif = self._make_factory()
341
 
        task_a = uif.nested_progress_bar()
342
 
        task_a.update('a', 0, 2)
343
 
        task_b = uif.nested_progress_bar()
344
 
        task_b.update('b')
345
 
        task_c = uif.nested_progress_bar()
346
 
        task_c.update('c', 1, 2)
347
 
        # the top-level task is in its first half; the middle one has no
348
 
        # progress indication, just a label; and the bottom one is half done,
349
 
        # so the overall fraction is 1/4
350
 
        self.assertEqual(
351
 
            r'[####|               ] a:b:c 1/2'
352
 
            , uif._progress_view._render_line())
353