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""))
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)
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")
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())
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
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
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())
190
self.assertEqual(None, choose())
192
def test_text_ui_choose_no_default(self):
193
stdin = tests.StringIOWrapper(" \n" # no default, invalid!
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())
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(""))
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())
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 ?'))
452
382
class TestBoolFromString(tests.TestCase):