/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3948.2.6 by Martin Pool
ProgressBarStack is deprecated
1
# Copyright (C) 2005, 2008, 2009 Canonical Ltd
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
16
17
"""Tests for the bzrlib ui
18
"""
19
20
import os
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
21
from StringIO import StringIO
1704.2.9 by Martin Pool
Make text_factory test not depend on 80-col terminal
22
import re
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
23
import sys
4017.1.1 by John Arbash Meinel
Get a pb.tick() to work after calling pb.update()
24
import time
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
25
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
26
from bzrlib import (
27
    errors,
28
    tests,
29
    ui as _mod_ui,
30
    )
3948.2.6 by Martin Pool
ProgressBarStack is deprecated
31
from bzrlib.symbol_versioning import (
32
    deprecated_in,
33
    )
1843.3.10 by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm)
34
from bzrlib.tests.test_progress import _TTYStringIO
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
35
from bzrlib.ui.text import (
4449.2.4 by Martin Pool
Add tests for BZR_PROGRESS_BAR
36
    NullProgressView,
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
37
    TextProgressView,
38
    TextUIFactory,
39
    )
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
40
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
41
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
42
class UITests(tests.TestCase):
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
43
44
    def test_silent_factory(self):
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
45
        ui = _mod_ui.SilentUIFactory()
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
46
        stdout = StringIO()
47
        self.assertEqual(None,
48
                         self.apply_redirected(None, stdout, stdout,
49
                                               ui.get_password))
50
        self.assertEqual('', stdout.getvalue())
51
        self.assertEqual(None,
52
                         self.apply_redirected(None, stdout, stdout,
53
                                               ui.get_password,
54
                                               u'Hello\u1234 %(user)s',
55
                                               user=u'some\u1234'))
56
        self.assertEqual('', stdout.getvalue())
57
58
    def test_text_factory_ascii_password(self):
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
59
        ui = tests.TestUIFactory(stdin='secret\n',
60
                                 stdout=tests.StringIOWrapper(),
61
                                 stderr=tests.StringIOWrapper())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
62
        pb = ui.nested_progress_bar()
63
        try:
64
            self.assertEqual('secret',
65
                             self.apply_redirected(ui.stdin, ui.stdout,
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
66
                                                   ui.stderr,
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
67
                                                   ui.get_password))
68
            # ': ' is appended to prompt
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
69
            self.assertEqual(': ', ui.stderr.getvalue())
70
            self.assertEqual('', ui.stdout.readline())
2363.4.3 by Vincent Ladeuil
Tidy-up tests.
71
            # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
72
            self.assertEqual('', ui.stdin.readline())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
73
        finally:
74
            pb.finished()
75
76
    def test_text_factory_utf8_password(self):
77
        """Test an utf8 password.
78
79
        We can't predict what encoding users will have for stdin, so we force
80
        it to utf8 to test that we transport the password correctly.
81
        """
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
82
        ui = tests.TestUIFactory(stdin=u'baz\u1234'.encode('utf8'),
83
                                 stdout=tests.StringIOWrapper(),
84
                                 stderr=tests.StringIOWrapper())
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
85
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8'
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
86
        pb = ui.nested_progress_bar()
87
        try:
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
88
            password = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
89
                                             ui.get_password,
90
                                             u'Hello \u1234 %(user)s',
91
                                             user=u'some\u1234')
92
            # We use StringIO objects, we need to decode them
93
            self.assertEqual(u'baz\u1234', password.decode('utf8'))
94
            self.assertEqual(u'Hello \u1234 some\u1234: ',
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
95
                             ui.stderr.getvalue().decode('utf8'))
96
            # stdin and stdout should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
97
            self.assertEqual('', ui.stdin.readline())
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
98
            self.assertEqual('', ui.stdout.readline())
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
99
        finally:
100
            pb.finished()
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
101
4449.2.4 by Martin Pool
Add tests for BZR_PROGRESS_BAR
102
    def test_progress_construction(self):
103
        """TextUIFactory constructs the right progress view.
104
        """
105
        os.environ['BZR_PROGRESS_BAR'] = 'none'
106
        self.assertIsInstance(TextUIFactory()._progress_view,
107
            NullProgressView)
108
109
        os.environ['BZR_PROGRESS_BAR'] = 'text'
110
        self.assertIsInstance(TextUIFactory()._progress_view,
111
            TextProgressView)
112
113
        os.environ['BZR_PROGRESS_BAR'] = 'text'
114
        self.assertIsInstance(TextUIFactory()._progress_view,
115
            TextProgressView)
116
117
        del os.environ['BZR_PROGRESS_BAR']
118
        self.assertIsInstance(TextUIFactory()._progress_view,
119
            TextProgressView)
120
1534.5.6 by Robert Collins
split out converter logic into per-format objects.
121
    def test_progress_note(self):
122
        stderr = StringIO()
123
        stdout = StringIO()
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
124
        ui_factory = TextUIFactory(stdin=StringIO(''),
125
            stderr=stderr,
126
            stdout=stdout)
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
127
        pb = ui_factory.nested_progress_bar()
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
128
        try:
129
            result = pb.note('t')
130
            self.assertEqual(None, result)
131
            self.assertEqual("t\n", stdout.getvalue())
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
132
            # Since there was no update() call, there should be no clear() call
2363.4.4 by Vincent Ladeuil
More tidying-up.
133
            self.failIf(re.search(r'^\r {10,}\r$',
134
                                  stderr.getvalue()) is not None,
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
135
                        'We cleared the stderr without anything to put there')
136
        finally:
137
            pb.finished()
138
139
    def test_progress_note_clears(self):
140
        stderr = StringIO()
141
        stdout = StringIO()
1843.3.10 by John Arbash Meinel
ui tests were failing when output was redirected to a file. (thus blocked by the pqm)
142
        # The PQM redirects the output to a file, so it
143
        # defaults to creating a Dots progress bar. we
144
        # need to force it to believe we are a TTY
3882.8.8 by Martin Pool
Progress and UI test cleanups
145
        ui_factory = TextUIFactory(
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
146
            stdin=StringIO(''),
3882.8.4 by Martin Pool
All UI factories should support note()
147
            stdout=stdout, stderr=stderr)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
148
        pb = ui_factory.nested_progress_bar()
149
        try:
150
            # Create a progress update that isn't throttled
151
            pb.update('x', 1, 1)
152
            result = pb.note('t')
153
            self.assertEqual(None, result)
154
            self.assertEqual("t\n", stdout.getvalue())
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
155
            # the exact contents will depend on the terminal width and we don't
156
            # care about that right now - but you're probably running it on at
157
            # least a 10-character wide terminal :)
1843.3.2 by John Arbash Meinel
Fix a ui test that depended on clearing
158
            self.assertContainsRe(stderr.getvalue(), r'\r {10,}\r$')
1558.8.4 by Aaron Bentley
Fixed test case for pb.note
159
        finally:
1558.8.5 by Aaron Bentley
Pass note up the stack instead of using bzrlib.ui_factory
160
            pb.finished()
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
161
162
    def test_progress_nested(self):
163
        # test factory based nested and popping.
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
164
        ui = TextUIFactory(None, None, None)
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
165
        pb1 = ui.nested_progress_bar()
166
        pb2 = ui.nested_progress_bar()
3948.2.2 by Martin Pool
Corrections to finishing progress bars
167
        # You do get a warning if the outermost progress bar wasn't finished
168
        # first - it's not clear if this is really useful or if it should just
169
        # become orphaned -- mbp 20090120
3882.8.12 by Martin Pool
Give a warning, not an error, if a progress bar is not finished in order
170
        warnings, _ = self.callCatchWarnings(pb1.finished)
3948.2.2 by Martin Pool
Corrections to finishing progress bars
171
        if len(warnings) != 1:
172
            self.fail("unexpected warnings: %r" % (warnings,))
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
173
        pb2.finished()
174
        pb1.finished()
175
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
176
    def assert_get_bool_acceptance_of_user_input(self, factory):
4503.2.5 by Vincent Ladeuil
ui.get_boolean can also use bool_from_string.
177
        factory.stdin = StringIO("y\n" # True
178
                                 "n\n" # False
179
                                 "yes with garbage\nY\n" # True
180
                                 "not an answer\nno\n" # False
181
                                 "I'm sure!\nyes\n" # True
182
                                 "NO\n" # False
183
                                 "foo\n")
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
184
        factory.stdout = StringIO()
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
185
        factory.stderr = StringIO()
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
186
        # there is no output from the base factory
187
        self.assertEqual(True, factory.get_boolean(""))
4503.2.5 by Vincent Ladeuil
ui.get_boolean can also use bool_from_string.
188
        self.assertEqual(False, factory.get_boolean(""))
189
        self.assertEqual(True, factory.get_boolean(""))
190
        self.assertEqual(False, factory.get_boolean(""))
191
        self.assertEqual(True, factory.get_boolean(""))
192
        self.assertEqual(False, factory.get_boolean(""))
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
193
        self.assertEqual("foo\n", factory.stdin.read())
2363.4.4 by Vincent Ladeuil
More tidying-up.
194
        # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
195
        self.assertEqual('', factory.stdin.readline())
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
196
197
    def test_silent_ui_getbool(self):
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
198
        factory = _mod_ui.SilentUIFactory()
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
199
        self.assert_get_bool_acceptance_of_user_input(factory)
200
201
    def test_silent_factory_prompts_silently(self):
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
202
        factory = _mod_ui.SilentUIFactory()
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
203
        stdout = StringIO()
204
        factory.stdin = StringIO("y\n")
2363.4.4 by Vincent Ladeuil
More tidying-up.
205
        self.assertEqual(True,
206
                         self.apply_redirected(None, stdout, stdout,
207
                                               factory.get_boolean, "foo"))
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
208
        self.assertEqual("", stdout.getvalue())
2363.4.4 by Vincent Ladeuil
More tidying-up.
209
        # stdin should be empty
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
210
        self.assertEqual('', factory.stdin.readline())
2363.4.4 by Vincent Ladeuil
More tidying-up.
211
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
212
    def test_text_ui_getbool(self):
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
213
        factory = TextUIFactory(None, None, None)
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
214
        self.assert_get_bool_acceptance_of_user_input(factory)
215
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
216
    def test_text_factory_prompt(self):
217
        # see <https://launchpad.net/bugs/365891>
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
218
        factory = TextUIFactory(None, StringIO(), StringIO(), StringIO())
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
219
        factory.prompt('foo %2e')
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
220
        self.assertEqual('', factory.stdout.getvalue())
221
        self.assertEqual('foo %2e', factory.stderr.getvalue())
4300.3.1 by Martin Pool
Fix string expansion in TextUIFactory.prompt
222
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
223
    def test_text_factory_prompts_and_clears(self):
224
        # a get_boolean call should clear the pb before prompting
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
225
        out = _TTYStringIO()
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
226
        factory = TextUIFactory(stdin=StringIO("yada\ny\n"),
227
                                stdout=out, stderr=out)
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
228
        pb = factory.nested_progress_bar()
229
        pb.show_bar = False
230
        pb.show_spinner = False
231
        pb.show_count = False
232
        pb.update("foo", 0, 1)
2363.4.4 by Vincent Ladeuil
More tidying-up.
233
        self.assertEqual(True,
234
                         self.apply_redirected(None, factory.stdout,
235
                                               factory.stdout,
236
                                               factory.get_boolean,
237
                                               "what do you want"))
3882.8.10 by Martin Pool
Fix up test_ui for new progress bars
238
        output = out.getvalue()
239
        self.assertContainsRe(factory.stdout.getvalue(),
240
            "foo *\r\r  *\r*")
241
        self.assertContainsRe(factory.stdout.getvalue(),
242
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
243
        # stdin should have been totally consumed
2363.4.6 by Vincent Ladeuil
Fix tests around stdin emptyness.
244
        self.assertEqual('', factory.stdin.readline())
4017.1.1 by John Arbash Meinel
Get a pb.tick() to work after calling pb.update()
245
246
    def test_text_tick_after_update(self):
247
        ui_factory = TextUIFactory(stdout=StringIO(), stderr=StringIO())
248
        pb = ui_factory.nested_progress_bar()
249
        try:
250
            pb.update('task', 0, 3)
251
            # Reset the clock, so that it actually tries to repaint itself
252
            ui_factory._progress_view._last_repaint = time.time() - 1.0
253
            pb.tick()
254
        finally:
255
            pb.finished()
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
256
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
257
    def test_silent_ui_getusername(self):
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
258
        factory = _mod_ui.SilentUIFactory()
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
259
        factory.stdin = StringIO("someuser\n\n")
260
        factory.stdout = StringIO()
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
261
        factory.stderr = StringIO()
262
        self.assertEquals(None,
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
263
            factory.get_username(u'Hello\u1234 %(host)s', host=u'some\u1234'))
264
        self.assertEquals("", factory.stdout.getvalue())
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
265
        self.assertEquals("", factory.stderr.getvalue())
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
266
        self.assertEquals("someuser\n\n", factory.stdin.getvalue())
267
268
    def test_text_ui_getusername(self):
269
        factory = TextUIFactory(None, None, None)
270
        factory.stdin = StringIO("someuser\n\n")
271
        factory.stdout = StringIO()
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
272
        factory.stderr = StringIO()
4222.2.6 by Jelmer Vernooij
Remove use of NotATerminal.
273
        factory.stdout.encoding = "utf8"
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
274
        # there is no output from the base factory
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
275
        self.assertEqual("someuser",
276
                         factory.get_username('Hello %(host)s', host='some'))
277
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
278
        self.assertEquals('', factory.stdout.getvalue())
4222.2.1 by Jelmer Vernooij
Add get_username() call to the UIFactory.
279
        self.assertEqual("", factory.get_username("Gebruiker"))
280
        # stdin should be empty
281
        self.assertEqual('', factory.stdin.readline())
282
4222.2.2 by Jelmer Vernooij
Review from vila: Deal with UTF8 strings in prompts, fix typo.
283
    def test_text_ui_getusername_utf8(self):
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
284
        ui = tests.TestUIFactory(stdin=u'someuser\u1234'.encode('utf8'),
285
                                 stdout=tests.StringIOWrapper(),
286
                                 stderr=tests.StringIOWrapper())
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
287
        ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8"
4222.2.3 by Jelmer Vernooij
Also check for unicode usernames.
288
        pb = ui.nested_progress_bar()
289
        try:
290
            # there is no output from the base factory
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
291
            username = self.apply_redirected(ui.stdin, ui.stdout, ui.stderr,
4222.2.12 by Jelmer Vernooij
Redirect to fix utf8 test with LC_ALL=C.
292
                ui.get_username, u'Hello\u1234 %(host)s', host=u'some\u1234')
4222.2.8 by Jelmer Vernooij
Fix copy-n-paste error.
293
            self.assertEquals(u"someuser\u1234", username.decode('utf8'))
4368.3.1 by Vincent Ladeuil
Use stderr for UI prompt to address bug #376582.
294
            self.assertEquals(u"Hello\u1234 some\u1234: ",
295
                              ui.stderr.getvalue().decode("utf8"))
296
            self.assertEquals('', ui.stdout.getvalue())
4222.2.3 by Jelmer Vernooij
Also check for unicode usernames.
297
        finally:
298
            pb.finished()
4222.2.2 by Jelmer Vernooij
Review from vila: Deal with UTF8 strings in prompts, fix typo.
299
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
300
4488.1.1 by Vincent Ladeuil
(vila) Cleanup imports in some test files
301
class TestTextProgressView(tests.TestCase):
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
302
    """Tests for text display of progress bars.
303
    """
304
    # XXX: These might be a bit easier to write if the rendering and
305
    # state-maintaining parts of TextProgressView were more separate, and if
306
    # the progress task called back directly to its own view not to the ui
307
    # factory. -- mbp 20090312
4110.2.16 by Martin Pool
Refactor TextProgressView a bit and add another test
308
    
309
    def _make_factory(self):
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
310
        out = StringIO()
311
        uif = TextUIFactory(stderr=out)
312
        uif._progress_view._width = 80
4110.2.16 by Martin Pool
Refactor TextProgressView a bit and add another test
313
        return out, uif
314
315
    def test_render_progress_easy(self):
316
        """Just one task and one quarter done"""
317
        out, uif = self._make_factory()
4110.2.15 by Martin Pool
Fix bug in showing task progress and add a test
318
        task = uif.nested_progress_bar()
319
        task.update('reticulating splines', 5, 20)
320
        self.assertEqual(
321
'\r[####/               ] reticulating splines 5/20                               \r'
322
            , out.getvalue())
4110.2.16 by Martin Pool
Refactor TextProgressView a bit and add another test
323
4110.2.17 by Martin Pool
If one ProgressTask has no count, it passes through that of its child
324
    def test_render_progress_nested(self):
325
        """Tasks proportionally contribute to overall progress"""
4110.2.16 by Martin Pool
Refactor TextProgressView a bit and add another test
326
        out, uif = self._make_factory()
327
        task = uif.nested_progress_bar()
328
        task.update('reticulating splines', 0, 2)
329
        task2 = uif.nested_progress_bar()
330
        task2.update('stage2', 1, 2)
331
        # so we're in the first half of the main task, and half way through
332
        # that
333
        self.assertEqual(
4110.2.18 by Martin Pool
Progress bars always repaint when task structure is changed
334
r'[####\               ] reticulating splines:stage2 1/2'
4110.2.16 by Martin Pool
Refactor TextProgressView a bit and add another test
335
            , uif._progress_view._render_line())
4110.2.17 by Martin Pool
If one ProgressTask has no count, it passes through that of its child
336
        # if the nested task is complete, then we're all the way through the
337
        # first half of the overall work
338
        task2.update('stage2', 2, 2)
339
        self.assertEqual(
4110.2.18 by Martin Pool
Progress bars always repaint when task structure is changed
340
r'[#########|          ] reticulating splines:stage2 2/2'
4110.2.17 by Martin Pool
If one ProgressTask has no count, it passes through that of its child
341
            , uif._progress_view._render_line())
342
343
    def test_render_progress_sub_nested(self):
344
        """Intermediate tasks don't mess up calculation."""
345
        out, uif = self._make_factory()
346
        task_a = uif.nested_progress_bar()
347
        task_a.update('a', 0, 2)
348
        task_b = uif.nested_progress_bar()
349
        task_b.update('b')
350
        task_c = uif.nested_progress_bar()
351
        task_c.update('c', 1, 2)
352
        # the top-level task is in its first half; the middle one has no
353
        # progress indication, just a label; and the bottom one is half done,
354
        # so the overall fraction is 1/4
355
        self.assertEqual(
4110.2.18 by Martin Pool
Progress bars always repaint when task structure is changed
356
            r'[####|               ] a:b:c 1/2'
4110.2.17 by Martin Pool
If one ProgressTask has no count, it passes through that of its child
357
            , uif._progress_view._render_line())
358
4503.2.1 by Vincent Ladeuil
Get a bool from a string.
359
360
class TestBoolFromString(tests.TestCase):
361
362
    def assertIsTrue(self, s, accepted_values=None):
363
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
364
        self.assertEquals(True, res)
365
366
    def assertIsFalse(self, s, accepted_values=None):
367
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
368
        self.assertEquals(False, res)
369
370
    def assertIsNone(self, s, accepted_values=None):
371
        res = _mod_ui.bool_from_string(s, accepted_values=accepted_values)
372
        self.assertIs(None, res)
373
374
    def test_know_valid_values(self):
375
        self.assertIsTrue('true')
376
        self.assertIsFalse('false')
377
        self.assertIsTrue('1')
378
        self.assertIsFalse('0')
379
        self.assertIsTrue('on')
380
        self.assertIsFalse('off')
381
        self.assertIsTrue('yes')
382
        self.assertIsFalse('no')
383
        self.assertIsTrue('y')
384
        self.assertIsFalse('n')
385
        # Also try some case variations
386
        self.assertIsTrue('True')
387
        self.assertIsFalse('False')
388
        self.assertIsTrue('On')
389
        self.assertIsFalse('Off')
390
        self.assertIsTrue('ON')
391
        self.assertIsFalse('OFF')
392
        self.assertIsTrue('oN')
393
        self.assertIsFalse('oFf')
394
395
    def test_invalid_values(self):
396
        self.assertIsNone(None)
397
        self.assertIsNone('doubt')
398
        self.assertIsNone('frue')
399
        self.assertIsNone('talse')
400
        self.assertIsNone('42')
401
402
    def test_provided_values(self):
403
        av = dict(y=True, n=False, yes=True, no=False)
404
        self.assertIsTrue('y', av)
405
        self.assertIsTrue('Y', av)
406
        self.assertIsTrue('Yes', av)
407
        self.assertIsFalse('n', av)
408
        self.assertIsFalse('N', av)
409
        self.assertIsFalse('No', av)
410
        self.assertIsNone('1', av)
411
        self.assertIsNone('0', av)
412
        self.assertIsNone('on', av)
413
        self.assertIsNone('off', av)