176
176
def assert_get_bool_acceptance_of_user_input(self, factory):
177
factory.stdin = StringIO("y\nyes with garbage\n"
178
"yes\nn\nnot an answer\n"
177
factory.stdin = StringIO("y\n" # True
179
"yes with garbage\nY\n" # True
180
"not an answer\nno\n" # False
181
"I'm sure!\nyes\n" # True
183
184
factory.stdout = StringIO()
184
185
factory.stderr = StringIO()
185
186
# there is no output from the base factory
186
187
self.assertEqual(True, factory.get_boolean(""))
187
self.assertEqual(True, factory.get_boolean(""))
188
self.assertEqual(False, factory.get_boolean(""))
189
self.assertEqual(False, factory.get_boolean(""))
190
self.assertEqual(False, factory.get_boolean(""))
191
self.assertEqual(True, factory.get_boolean(""))
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(""))
192
193
self.assertEqual("foo\n", factory.stdin.read())
193
194
# stdin should be empty
194
195
self.assertEqual('', factory.stdin.readline())
355
356
r'[####| ] a:b:c 1/2'
356
357
, uif._progress_view._render_line())
360
class TestBoolFromString(tests.TestCase):
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)
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)
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)
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')
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')
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)