/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
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
19
from bzrlib.progress import (
20
        DummyProgress, ChildProgress,
21
        TTYProgressBar,
22
        DotsProgressBar,
23
        ProgressBarStack,
24
        )
1551.2.27 by Aaron Bentley
Got propogation under test
25
from bzrlib.tests import TestCase
26
1551.2.29 by Aaron Bentley
Got stack handling under test
27
class FakeStack:
28
    def __init__(self, top):
29
        self.__top = top
30
31
    def top(self):
32
        return self.__top
33
1551.2.27 by Aaron Bentley
Got propogation under test
34
class TestProgress(TestCase):
35
    def setUp(self):
1551.2.29 by Aaron Bentley
Got stack handling under test
36
        q = DummyProgress()
37
        self.top = ChildProgress(_stack=FakeStack(q))
1551.2.27 by Aaron Bentley
Got propogation under test
38
39
    def test_propogation(self):
40
        self.top.update('foobles', 1, 2)
41
        self.assertEqual(self.top.message, 'foobles')
42
        self.assertEqual(self.top.current, 1)
43
        self.assertEqual(self.top.total, 2)
44
        self.assertEqual(self.top.child_fraction, 0)
1551.2.29 by Aaron Bentley
Got stack handling under test
45
        child = ChildProgress(_stack=FakeStack(self.top))
1551.2.27 by Aaron Bentley
Got propogation under test
46
        child.update('baubles', 2, 4)
47
        self.assertEqual(self.top.message, 'foobles')
48
        self.assertEqual(self.top.current, 1)
49
        self.assertEqual(self.top.total, 2)
50
        self.assertEqual(self.top.child_fraction, 0.5)
1551.2.29 by Aaron Bentley
Got stack handling under test
51
        grandchild = ChildProgress(_stack=FakeStack(child))
1551.2.27 by Aaron Bentley
Got propogation under test
52
        grandchild.update('barbells', 1, 2)
53
        self.assertEqual(self.top.child_fraction, 0.625)
54
        self.assertEqual(child.child_fraction, 0.5)
55
        child.update('baubles', 3, 4)
56
        self.assertEqual(child.child_fraction, 0)
57
        self.assertEqual(self.top.child_fraction, 0.75)
58
        grandchild.update('barbells', 1, 2)
59
        self.assertEqual(self.top.child_fraction, 0.875)
60
        grandchild.update('barbells', 2, 2)
61
        self.assertEqual(self.top.child_fraction, 1)
62
        child.update('baubles', 4, 4)
63
        self.assertEqual(self.top.child_fraction, 1)
64
        #test clamping
65
        grandchild.update('barbells', 2, 2)
66
        self.assertEqual(self.top.child_fraction, 1)
1551.2.28 by Aaron Bentley
Ensure all ProgressBar implementations can be used as parents
67
68
    def test_implementations(self):
69
        for implementation in (TTYProgressBar, DotsProgressBar, 
70
                               DummyProgress):
71
            self.check_parent_handling(implementation)
72
73
    def check_parent_handling(self, parentclass):
74
        top = parentclass(to_file=StringIO())
75
        top.update('foobles', 1, 2)
1551.2.29 by Aaron Bentley
Got stack handling under test
76
        child = ChildProgress(_stack=FakeStack(top))
1551.2.28 by Aaron Bentley
Ensure all ProgressBar implementations can be used as parents
77
        child.update('baubles', 4, 4)
78
        top.update('lala', 2, 2)
79
        child.update('baubles', 4, 4)
1551.2.29 by Aaron Bentley
Got stack handling under test
80
81
    def test_stacking(self):
82
        self.check_stack(TTYProgressBar, ChildProgress)
83
        self.check_stack(DotsProgressBar, ChildProgress)
84
        self.check_stack(DummyProgress, DummyProgress)
85
86
    def check_stack(self, parent_class, child_class):
87
        stack = ProgressBarStack(klass=parent_class, to_file=StringIO())
88
        parent = stack.get_nested()
89
        try:
90
            self.assertIs(parent.__class__, parent_class)
91
            child = stack.get_nested()
92
            try:
93
                self.assertIs(child.__class__, child_class)
94
            finally:
95
                child.finished()
96
        finally:
97
            parent.finished()