def test_ask_and_validate(self):
        dialog = QuestionHelper()
        dialog.set_helper_set(HelperSet([FormatterHelper()]))

        error = 'This is not a color!'
        def validator(color):
            if color not in ['white', 'black']:
                raise Exception(error)

            return color

        question = Question('What color was the white horse of Henry IV?', 'white')
        question.validator = validator
        question.max_attempts = 2

        dialog.input_stream = self.get_input_stream('\nblack\n')
        self.assertEqual(
            'white',
            dialog.ask(self.get_input(), self.get_output_stream(), question)
        )
        self.assertEqual(
            'black',
            dialog.ask(self.get_input(), self.get_output_stream(), question)
        )

        dialog.input_stream = self.get_input_stream('green\nyellow\norange\n')
        try:
            dialog.ask(self.get_input(), self.get_output_stream(), question)
            self.fail()
        except Exception as e:
            self.assertEqual(error, str(e))
    def test_ask_hidden_response(self):
        if not self.has_tty_available():
            self.skipTest('`stty` is required to test hidden response functionality')

        dialog = QuestionHelper()
        dialog.input_stream = self.get_string_input_stream('8AM\n')

        question = Question('What time is it?')
        question.hidden = True

        self.assertEqual(
            '8AM',
            dialog.ask(self.get_input(), self.get_output_stream(), question)
        )
 def test_no_interaction(self):
     dialog = QuestionHelper()
     question = Question('Do you have a job?', 'not yet')
     self.assertEqual(
         'not yet',
         dialog.ask(self.get_input(False), self.get_output_stream(), question)
     )
Example #4
0
    def test_ask(self):
        dialog = QuestionHelper()
        dialog.input_stream = self.get_string_input_stream('\n8AM\n')

        question = Question('What time is it?', '2PM')
        self.assertEqual(
            '2PM',
            dialog.ask(self.get_input(), self.get_output_stream(), question))
        output = self.get_output_stream()
        self.assertEqual('8AM', dialog.ask(self.get_input(), output, question))

        output.get_stream().seek(0)
        self.assertEqual('What time is it?',
                         decode(output.get_stream().read()))