예제 #1
0
class RawInputTC(TestCase):

    def auto_input(self, *args):
        self.input_args = args
        return self.input_answer

    def setUp(self):
        null_printer = lambda x: None
        self.qa = RawInput(self.auto_input, null_printer)

    def test_ask_default(self):
        self.input_answer = ''
        answer = self.qa.ask('text', ('yes', 'no'), 'yes')
        self.assertEqual(answer, 'yes')
        self.input_answer = '  '
        answer = self.qa.ask('text', ('yes', 'no'), 'yes')
        self.assertEqual(answer, 'yes')

    def test_ask_case(self):
        self.input_answer = 'no'
        answer = self.qa.ask('text', ('yes', 'no'), 'yes')
        self.assertEqual(answer, 'no')
        self.input_answer = 'No'
        answer = self.qa.ask('text', ('yes', 'no'), 'yes')
        self.assertEqual(answer, 'no')
        self.input_answer = 'NO'
        answer = self.qa.ask('text', ('yes', 'no'), 'yes')
        self.assertEqual(answer, 'no')
        self.input_answer = 'nO'
        answer = self.qa.ask('text', ('yes', 'no'), 'yes')
        self.assertEqual(answer, 'no')
        self.input_answer = 'YES'
        answer = self.qa.ask('text', ('yes', 'no'), 'yes')
        self.assertEqual(answer, 'yes')

    def test_ask_prompt(self):
        self.input_answer = ''
        answer = self.qa.ask('text', ('yes', 'no'), 'yes')
        self.assertEqual(self.input_args[0], 'text [Y(es)/n(o)]: ')
        answer = self.qa.ask('text', ('y', 'n'), 'y')
        self.assertEqual(self.input_args[0], 'text [Y/n]: ')
        answer = self.qa.ask('text', ('n', 'y'), 'y')
        self.assertEqual(self.input_args[0], 'text [n/Y]: ')
        answer = self.qa.ask('text', ('yes', 'no', 'maybe', '1'), 'yes')
        self.assertEqual(self.input_args[0], 'text [Y(es)/n(o)/m(aybe)/1]: ')

    def test_ask_ambiguous(self):
        self.input_answer = 'y'
        self.assertRaises(Exception, self.qa.ask, 'text', ('yes', 'yep'), 'yes')

    def test_confirm(self):
        self.input_answer = 'y'
        self.assertEqual(self.qa.confirm('Say yes'), True)
        self.assertEqual(self.qa.confirm('Say yes', default_is_yes=False), True)
        self.input_answer = 'n'
        self.assertEqual(self.qa.confirm('Say yes'), False)
        self.assertEqual(self.qa.confirm('Say yes', default_is_yes=False), False)
        self.input_answer = ''
        self.assertEqual(self.qa.confirm('Say default'), True)
        self.assertEqual(self.qa.confirm('Say default', default_is_yes=False), False)
예제 #2
0
 def setUp(self):
     null_printer = lambda x: None
     self.qa = RawInput(self.auto_input, null_printer)
예제 #3
0
class RawInputTC(TestCase):
    def auto_input(self, *args):
        self.input_args = args
        return self.input_answer

    def setUp(self):
        null_printer = lambda x: None
        self.qa = RawInput(self.auto_input, null_printer)

    def test_ask_default(self):
        self.input_answer = ""
        answer = self.qa.ask("text", ("yes", "no"), "yes")
        self.assertEqual(answer, "yes")
        self.input_answer = "  "
        answer = self.qa.ask("text", ("yes", "no"), "yes")
        self.assertEqual(answer, "yes")

    def test_ask_case(self):
        self.input_answer = "no"
        answer = self.qa.ask("text", ("yes", "no"), "yes")
        self.assertEqual(answer, "no")
        self.input_answer = "No"
        answer = self.qa.ask("text", ("yes", "no"), "yes")
        self.assertEqual(answer, "no")
        self.input_answer = "NO"
        answer = self.qa.ask("text", ("yes", "no"), "yes")
        self.assertEqual(answer, "no")
        self.input_answer = "nO"
        answer = self.qa.ask("text", ("yes", "no"), "yes")
        self.assertEqual(answer, "no")
        self.input_answer = "YES"
        answer = self.qa.ask("text", ("yes", "no"), "yes")
        self.assertEqual(answer, "yes")

    def test_ask_prompt(self):
        self.input_answer = ""
        answer = self.qa.ask("text", ("yes", "no"), "yes")
        self.assertEqual(self.input_args[0], "text [Y(es)/n(o)]: ")
        answer = self.qa.ask("text", ("y", "n"), "y")
        self.assertEqual(self.input_args[0], "text [Y/n]: ")
        answer = self.qa.ask("text", ("n", "y"), "y")
        self.assertEqual(self.input_args[0], "text [n/Y]: ")
        answer = self.qa.ask("text", ("yes", "no", "maybe", "1"), "yes")
        self.assertEqual(self.input_args[0], "text [Y(es)/n(o)/m(aybe)/1]: ")

    def test_ask_ambiguous(self):
        self.input_answer = "y"
        self.assertRaises(Exception, self.qa.ask, "text", ("yes", "yep"), "yes")

    def test_confirm(self):
        self.input_answer = "y"
        self.assertEqual(self.qa.confirm("Say yes"), True)
        self.assertEqual(self.qa.confirm("Say yes", default_is_yes=False), True)
        self.input_answer = "n"
        self.assertEqual(self.qa.confirm("Say yes"), False)
        self.assertEqual(self.qa.confirm("Say yes", default_is_yes=False), False)
        self.input_answer = ""
        self.assertEqual(self.qa.confirm("Say default"), True)
        self.assertEqual(self.qa.confirm("Say default", default_is_yes=False), False)