/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1551.2.27 by Aaron Bentley
Got propogation under test
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
1551.2.28 by Aaron Bentley
Ensure all ProgressBar implementations can be used as parents
17
from StringIO import StringIO
18
19
from bzrlib.progress import *
1551.2.27 by Aaron Bentley
Got propogation under test
20
from bzrlib.tests import TestCase
21
1551.2.29 by Aaron Bentley
Got stack handling under test
22
class FakeStack:
23
    def __init__(self, top):
24
        self.__top = top
25
26
    def top(self):
27
        return self.__top
28
1793.1.1 by Aaron Bentley
Hide TTYProgressBars unless they last more than 1 second
29
class InstrumentedProgress(TTYProgressBar):
30
    """TTYProgress variant that tracks outcomes"""
31
32
    def __init__(self, *args, **kwargs):
33
        self.always_throttled = True
34
        TTYProgressBar.__init__(self, *args, **kwargs)
35
36
    def throttle(self, old_message):
37
        result = TTYProgressBar.throttle(self, old_message)
38
        if result is False:
39
            self.always_throttled = False
40
        
41
1551.2.27 by Aaron Bentley
Got propogation under test
42
class TestProgress(TestCase):
43
    def setUp(self):
1551.2.29 by Aaron Bentley
Got stack handling under test
44
        q = DummyProgress()
45
        self.top = ChildProgress(_stack=FakeStack(q))
1551.2.27 by Aaron Bentley
Got propogation under test
46
47
    def test_propogation(self):
48
        self.top.update('foobles', 1, 2)
49
        self.assertEqual(self.top.message, 'foobles')
50
        self.assertEqual(self.top.current, 1)
51
        self.assertEqual(self.top.total, 2)
52
        self.assertEqual(self.top.child_fraction, 0)
1551.2.29 by Aaron Bentley
Got stack handling under test
53
        child = ChildProgress(_stack=FakeStack(self.top))
1551.2.27 by Aaron Bentley
Got propogation under test
54
        child.update('baubles', 2, 4)
55
        self.assertEqual(self.top.message, 'foobles')
56
        self.assertEqual(self.top.current, 1)
57
        self.assertEqual(self.top.total, 2)
58
        self.assertEqual(self.top.child_fraction, 0.5)
1551.2.29 by Aaron Bentley
Got stack handling under test
59
        grandchild = ChildProgress(_stack=FakeStack(child))
1551.2.27 by Aaron Bentley
Got propogation under test
60
        grandchild.update('barbells', 1, 2)
61
        self.assertEqual(self.top.child_fraction, 0.625)
62
        self.assertEqual(child.child_fraction, 0.5)
63
        child.update('baubles', 3, 4)
64
        self.assertEqual(child.child_fraction, 0)
65
        self.assertEqual(self.top.child_fraction, 0.75)
66
        grandchild.update('barbells', 1, 2)
67
        self.assertEqual(self.top.child_fraction, 0.875)
68
        grandchild.update('barbells', 2, 2)
69
        self.assertEqual(self.top.child_fraction, 1)
70
        child.update('baubles', 4, 4)
71
        self.assertEqual(self.top.child_fraction, 1)
72
        #test clamping
73
        grandchild.update('barbells', 2, 2)
74
        self.assertEqual(self.top.child_fraction, 1)
1551.2.28 by Aaron Bentley
Ensure all ProgressBar implementations can be used as parents
75
76
    def test_implementations(self):
77
        for implementation in (TTYProgressBar, DotsProgressBar, 
78
                               DummyProgress):
79
            self.check_parent_handling(implementation)
80
81
    def check_parent_handling(self, parentclass):
82
        top = parentclass(to_file=StringIO())
83
        top.update('foobles', 1, 2)
1551.2.29 by Aaron Bentley
Got stack handling under test
84
        child = ChildProgress(_stack=FakeStack(top))
1551.2.28 by Aaron Bentley
Ensure all ProgressBar implementations can be used as parents
85
        child.update('baubles', 4, 4)
86
        top.update('lala', 2, 2)
87
        child.update('baubles', 4, 4)
1551.2.29 by Aaron Bentley
Got stack handling under test
88
89
    def test_stacking(self):
90
        self.check_stack(TTYProgressBar, ChildProgress)
91
        self.check_stack(DotsProgressBar, ChildProgress)
92
        self.check_stack(DummyProgress, DummyProgress)
93
94
    def check_stack(self, parent_class, child_class):
95
        stack = ProgressBarStack(klass=parent_class, to_file=StringIO())
96
        parent = stack.get_nested()
97
        try:
98
            self.assertIs(parent.__class__, parent_class)
99
            child = stack.get_nested()
100
            try:
101
                self.assertIs(child.__class__, child_class)
102
            finally:
103
                child.finished()
104
        finally:
105
            parent.finished()
1793.1.1 by Aaron Bentley
Hide TTYProgressBars unless they last more than 1 second
106
107
    def test_throttling(self):
108
        pb = InstrumentedProgress(to_file=StringIO())
109
        # instantaneous updates should be squelched
110
        pb.update('me', 1, 1)
111
        self.assertTrue(pb.always_throttled)
112
        pb = InstrumentedProgress(to_file=StringIO())
113
        # It's like an instant sleep(1)!
114
        pb.start_time -= 1
115
        # Updates after a second should not be squelched
116
        pb.update('me', 1, 1)
117
        self.assertFalse(pb.always_throttled)