~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_progress.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-02 05:45:50 UTC
  • mto: This revision was merged to the branch mainline in revision 1851.
  • Revision ID: john@arbash-meinel.com-20060702054550-e535f24da694acb7
make_entry refuses to create non-normalized entries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 by Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
from StringIO import StringIO
 
18
 
 
19
from bzrlib.progress import (
 
20
        DummyProgress, ChildProgress,
 
21
        TTYProgressBar,
 
22
        DotsProgressBar,
 
23
        ProgressBarStack,
 
24
        )
 
25
from bzrlib.tests import TestCase
 
26
 
 
27
class FakeStack:
 
28
    def __init__(self, top):
 
29
        self.__top = top
 
30
 
 
31
    def top(self):
 
32
        return self.__top
 
33
 
 
34
class InstrumentedProgress(TTYProgressBar):
 
35
    """TTYProgress variant that tracks outcomes"""
 
36
 
 
37
    def __init__(self, *args, **kwargs):
 
38
        self.always_throttled = True
 
39
        TTYProgressBar.__init__(self, *args, **kwargs)
 
40
 
 
41
    def throttle(self, old_message):
 
42
        result = TTYProgressBar.throttle(self, old_message)
 
43
        if result is False:
 
44
            self.always_throttled = False
 
45
        
 
46
 
 
47
class TestProgress(TestCase):
 
48
    def setUp(self):
 
49
        q = DummyProgress()
 
50
        self.top = ChildProgress(_stack=FakeStack(q))
 
51
 
 
52
    def test_propogation(self):
 
53
        self.top.update('foobles', 1, 2)
 
54
        self.assertEqual(self.top.message, 'foobles')
 
55
        self.assertEqual(self.top.current, 1)
 
56
        self.assertEqual(self.top.total, 2)
 
57
        self.assertEqual(self.top.child_fraction, 0)
 
58
        child = ChildProgress(_stack=FakeStack(self.top))
 
59
        child.update('baubles', 2, 4)
 
60
        self.assertEqual(self.top.message, 'foobles')
 
61
        self.assertEqual(self.top.current, 1)
 
62
        self.assertEqual(self.top.total, 2)
 
63
        self.assertEqual(self.top.child_fraction, 0.5)
 
64
        grandchild = ChildProgress(_stack=FakeStack(child))
 
65
        grandchild.update('barbells', 1, 2)
 
66
        self.assertEqual(self.top.child_fraction, 0.625)
 
67
        self.assertEqual(child.child_fraction, 0.5)
 
68
        child.update('baubles', 3, 4)
 
69
        self.assertEqual(child.child_fraction, 0)
 
70
        self.assertEqual(self.top.child_fraction, 0.75)
 
71
        grandchild.update('barbells', 1, 2)
 
72
        self.assertEqual(self.top.child_fraction, 0.875)
 
73
        grandchild.update('barbells', 2, 2)
 
74
        self.assertEqual(self.top.child_fraction, 1)
 
75
        child.update('baubles', 4, 4)
 
76
        self.assertEqual(self.top.child_fraction, 1)
 
77
        #test clamping
 
78
        grandchild.update('barbells', 2, 2)
 
79
        self.assertEqual(self.top.child_fraction, 1)
 
80
 
 
81
    def test_implementations(self):
 
82
        for implementation in (TTYProgressBar, DotsProgressBar, 
 
83
                               DummyProgress):
 
84
            self.check_parent_handling(implementation)
 
85
 
 
86
    def check_parent_handling(self, parentclass):
 
87
        top = parentclass(to_file=StringIO())
 
88
        top.update('foobles', 1, 2)
 
89
        child = ChildProgress(_stack=FakeStack(top))
 
90
        child.update('baubles', 4, 4)
 
91
        top.update('lala', 2, 2)
 
92
        child.update('baubles', 4, 4)
 
93
 
 
94
    def test_stacking(self):
 
95
        self.check_stack(TTYProgressBar, ChildProgress)
 
96
        self.check_stack(DotsProgressBar, ChildProgress)
 
97
        self.check_stack(DummyProgress, DummyProgress)
 
98
 
 
99
    def check_stack(self, parent_class, child_class):
 
100
        stack = ProgressBarStack(klass=parent_class, to_file=StringIO())
 
101
        parent = stack.get_nested()
 
102
        try:
 
103
            self.assertIs(parent.__class__, parent_class)
 
104
            child = stack.get_nested()
 
105
            try:
 
106
                self.assertIs(child.__class__, child_class)
 
107
            finally:
 
108
                child.finished()
 
109
        finally:
 
110
            parent.finished()
 
111
 
 
112
    def test_throttling(self):
 
113
        pb = InstrumentedProgress(to_file=StringIO())
 
114
        # instantaneous updates should be squelched
 
115
        pb.update('me', 1, 1)
 
116
        self.assertTrue(pb.always_throttled)
 
117
        pb = InstrumentedProgress(to_file=StringIO())
 
118
        # It's like an instant sleep(1)!
 
119
        pb.start_time -= 1
 
120
        # Updates after a second should not be squelched
 
121
        pb.update('me', 1, 1)
 
122
        self.assertFalse(pb.always_throttled)