~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Jelmer Vernooij
  • Date: 2011-04-09 19:25:42 UTC
  • mto: (5777.5.1 inventoryworkingtree)
  • mto: This revision was merged to the branch mainline in revision 5781.
  • Revision ID: jelmer@samba.org-20110409192542-8bbedp36s7nj928e
Split InventoryTree out of Tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
    def test_text_factory_confirm(self):
64
64
        # turns into reading a regular boolean
65
65
        ui = self.make_test_ui_factory('n\n')
66
 
        self.assertEquals(ui.confirm_action(u'Should %(thing)s pass?',
 
66
        self.assertEquals(ui.confirm_action('Should %(thing)s pass?',
67
67
            'bzrlib.tests.test_ui.confirmation',
68
68
            {'thing': 'this'},),
69
69
            False)
111
111
    def test_text_ui_get_boolean(self):
112
112
        stdin = tests.StringIOWrapper("y\n" # True
113
113
                                      "n\n" # False
114
 
                                      " \n y \n" # True
115
 
                                      " no \n" # False
116
114
                                      "yes with garbage\nY\n" # True
117
115
                                      "not an answer\nno\n" # False
118
116
                                      "I'm sure!\nyes\n" # True
121
119
        stdout = tests.StringIOWrapper()
122
120
        stderr = tests.StringIOWrapper()
123
121
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
124
 
        self.assertEqual(True, factory.get_boolean(u""))
125
 
        self.assertEqual(False, factory.get_boolean(u""))
126
 
        self.assertEqual(True, factory.get_boolean(u""))
127
 
        self.assertEqual(False, factory.get_boolean(u""))
128
 
        self.assertEqual(True, factory.get_boolean(u""))
129
 
        self.assertEqual(False, factory.get_boolean(u""))
130
 
        self.assertEqual(True, factory.get_boolean(u""))
131
 
        self.assertEqual(False, factory.get_boolean(u""))
132
 
        self.assertEqual("foo\n", factory.stdin.read())
133
 
        # stdin should be empty
134
 
        self.assertEqual('', factory.stdin.readline())
135
 
        # return false on EOF
136
 
        self.assertEqual(False, factory.get_boolean(u""))
137
 
 
138
 
    def test_text_ui_choose_bad_parameters(self):
139
 
        stdin = tests.StringIOWrapper()
140
 
        stdout = tests.StringIOWrapper()
141
 
        stderr = tests.StringIOWrapper()
142
 
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
143
 
        # invalid default index
144
 
        self.assertRaises(ValueError, factory.choose, u"", u"&Yes\n&No", 3)
145
 
        # duplicated choice
146
 
        self.assertRaises(ValueError, factory.choose, u"", u"&choice\n&ChOiCe")
147
 
        # duplicated shortcut
148
 
        self.assertRaises(ValueError, factory.choose, u"", u"&choice1\nchoi&ce2")
149
 
 
150
 
    def test_text_ui_choose_prompt(self):
151
 
        stdin = tests.StringIOWrapper()
152
 
        stdout = tests.StringIOWrapper()
153
 
        stderr = tests.StringIOWrapper()
154
 
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
155
 
        # choices with explicit shortcuts
156
 
        factory.choose(u"prompt", u"&yes\n&No\nmore &info")
157
 
        self.assertEqual("prompt ([y]es, [N]o, more [i]nfo): \n", factory.stderr.getvalue())
158
 
        # automatic shortcuts
159
 
        factory.stderr.truncate(0)
160
 
        factory.choose(u"prompt", u"yes\nNo\nmore info")
161
 
        self.assertEqual("prompt ([y]es, [N]o, [m]ore info): \n", factory.stderr.getvalue())
162
 
 
163
 
    def test_text_ui_choose_return_values(self):
164
 
        choose = lambda: factory.choose(u"", u"&Yes\n&No\nMaybe\nmore &info", 3)
165
 
        stdin = tests.StringIOWrapper("y\n" # 0
166
 
                                      "n\n" # 1
167
 
                                      " \n" # default: 3
168
 
                                      " no \n" # 1
169
 
                                      "b\na\nd \n" # bad shortcuts, all ignored
170
 
                                      "yes with garbage\nY\n" # 0
171
 
                                      "not an answer\nno\n" # 1
172
 
                                      "info\nmore info\n" # 3
173
 
                                      "Maybe\n" # 2
174
 
                                      "foo\n")
175
 
        stdout = tests.StringIOWrapper()
176
 
        stderr = tests.StringIOWrapper()
177
 
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
178
 
        self.assertEqual(0, choose())
179
 
        self.assertEqual(1, choose())
180
 
        self.assertEqual(3, choose())
181
 
        self.assertEqual(1, choose())
182
 
        self.assertEqual(0, choose())
183
 
        self.assertEqual(1, choose())
184
 
        self.assertEqual(3, choose())
185
 
        self.assertEqual(2, choose())
186
 
        self.assertEqual("foo\n", factory.stdin.read())
187
 
        # stdin should be empty
188
 
        self.assertEqual('', factory.stdin.readline())
189
 
        # return None on EOF
190
 
        self.assertEqual(None, choose())
191
 
 
192
 
    def test_text_ui_choose_no_default(self):
193
 
        stdin = tests.StringIOWrapper(" \n" # no default, invalid!
194
 
                                      " yes \n" # 0
195
 
                                      "foo\n")
196
 
        stdout = tests.StringIOWrapper()
197
 
        stderr = tests.StringIOWrapper()
198
 
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
199
 
        self.assertEqual(0, factory.choose(u"", u"&Yes\n&No"))
200
 
        self.assertEqual("foo\n", factory.stdin.read())
 
122
        self.assertEqual(True, factory.get_boolean(""))
 
123
        self.assertEqual(False, factory.get_boolean(""))
 
124
        self.assertEqual(True, factory.get_boolean(""))
 
125
        self.assertEqual(False, factory.get_boolean(""))
 
126
        self.assertEqual(True, factory.get_boolean(""))
 
127
        self.assertEqual(False, factory.get_boolean(""))
 
128
        self.assertEqual("foo\n", factory.stdin.read())
 
129
        # stdin should be empty
 
130
        self.assertEqual('', factory.stdin.readline())
201
131
 
202
132
    def test_text_ui_get_integer(self):
203
133
        stdin = tests.StringIOWrapper(
207
137
        stdout = tests.StringIOWrapper()
208
138
        stderr = tests.StringIOWrapper()
209
139
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
210
 
        self.assertEqual(1, factory.get_integer(u""))
211
 
        self.assertEqual(-2, factory.get_integer(u""))
212
 
        self.assertEqual(42, factory.get_integer(u""))
 
140
        self.assertEqual(1, factory.get_integer(""))
 
141
        self.assertEqual(-2, factory.get_integer(""))
 
142
        self.assertEqual(42, factory.get_integer(""))
213
143
 
214
144
    def test_text_factory_prompt(self):
215
145
        # see <https://launchpad.net/bugs/365891>
216
146
        StringIO = tests.StringIOWrapper
217
147
        factory = _mod_ui_text.TextUIFactory(StringIO(), StringIO(), StringIO())
218
 
        factory.prompt(u'foo %2e')
 
148
        factory.prompt('foo %2e')
219
149
        self.assertEqual('', factory.stdout.getvalue())
220
150
        self.assertEqual('foo %2e', factory.stderr.getvalue())
221
151
 
236
166
                         self.apply_redirected(None, factory.stdout,
237
167
                                               factory.stdout,
238
168
                                               factory.get_boolean,
239
 
                                               u"what do you want"))
 
169
                                               "what do you want"))
240
170
        output = out.getvalue()
241
171
        self.assertContainsRe(output,
242
172
            "| foo *\r\r  *\r*")
243
 
        self.assertContainsString(output,
244
 
            r"what do you want? ([y]es, [n]o): what do you want? ([y]es, [n]o): ")
 
173
        self.assertContainsRe(output,
 
174
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
245
175
        # stdin should have been totally consumed
246
176
        self.assertEqual('', factory.stdin.readline())
247
177
 
265
195
        factory.stdout.encoding = "utf8"
266
196
        # there is no output from the base factory
267
197
        self.assertEqual("someuser",
268
 
                         factory.get_username(u'Hello %(host)s', host='some'))
 
198
                         factory.get_username('Hello %(host)s', host='some'))
269
199
        self.assertEquals("Hello some: ", factory.stderr.getvalue())
270
200
        self.assertEquals('', factory.stdout.getvalue())
271
 
        self.assertEqual("", factory.get_username(u"Gebruiker"))
 
201
        self.assertEqual("", factory.get_username("Gebruiker"))
272
202
        # stdin should be empty
273
203
        self.assertEqual('', factory.stdin.readline())
274
204
 
422
352
        self.assertRaises(
423
353
            NotImplementedError,
424
354
            self.apply_redirected,
425
 
            None, stdout, stdout, factory.get_boolean, u"foo")
 
355
            None, stdout, stdout, factory.get_boolean, "foo")
426
356
 
427
357
 
428
358
class TestUIFactoryTests(tests.TestCase):
441
371
 
442
372
    def test_canned_input_get_input(self):
443
373
        uif = _mod_ui.CannedInputUIFactory([True, 'mbp', 'password', 42])
444
 
        self.assertEqual(True, uif.get_boolean(u'Extra cheese?'))
445
 
        self.assertEqual('mbp', uif.get_username(u'Enter your user name'))
 
374
        self.assertEqual(True, uif.get_boolean('Extra cheese?'))
 
375
        self.assertEqual('mbp', uif.get_username('Enter your user name'))
446
376
        self.assertEqual('password',
447
 
                         uif.get_password(u'Password for %(host)s',
 
377
                         uif.get_password('Password for %(host)s',
448
378
                                          host='example.com'))
449
 
        self.assertEqual(42, uif.get_integer(u'And all that jazz ?'))
 
379
        self.assertEqual(42, uif.get_integer('And all that jazz ?'))
450
380
 
451
381
 
452
382
class TestBoolFromString(tests.TestCase):