Exemplo n.º 1
0
    def test_ask_and_validate_with_validator(self):
        """
        DialogHelper.ask_and_validate() behaves properly when passing Validator instances
        """
        dialog = DialogHelper()
        helper_set = HelperSet([FormatterHelper()])
        dialog.set_helper_set(helper_set)

        question = 'What color was the white horse of Henry IV?'
        error = 'This is not a color!'

        def validator(color):
            if color not in ['white', 'black']:
                raise Exception(error)

            return color

        dialog.set_input_stream(self.get_input_stream('\nblack\n'))
        self.assertEqual(
            'white',
            dialog.ask_and_validate(self.get_output_stream(), question,
                                    Choice(['white', 'black']), 2, 'white'))
        self.assertEqual(
            'black',
            dialog.ask_and_validate(self.get_output_stream(), question,
                                    Choice(['white', 'black']), 2, 'white'))

        dialog.set_input_stream(
            self.get_input_stream('green\nyellow\norange\n'))
        self.assertRaises(Exception, dialog.ask_and_validate,
                          self.get_output_stream(), question,
                          Choice(['white', 'black']), 2, 'white')
    def test_select_choice_from_simple_choice(self):
        possible_choices = [
            'My environment 1',
            'My environment 2',
            'My environment 3',
        ]

        data = [
            (0, 'My environment 1'),
            (1, 'My environment 2'),
            (2, 'My environment 3'),
            ('My environment 1', 'My environment 1'),
            ('My environment 2', 'My environment 2'),
            ('My environment 3', 'My environment 3'),
        ]

        for d in data:
            dialog = QuestionHelper()
            dialog.input_stream = self.get_input_stream(str(d[0]) + '\n')
            dialog.set_helper_set(HelperSet([FormatterHelper()]))

            question = ChoiceQuestion('Please select the environment to load', possible_choices)
            question.max_attempts = 1
            answer = dialog.ask(self.get_input(), self.get_output_stream(), question)

            self.assertEqual(d[1], answer)
    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))
Exemplo n.º 4
0
    def test_ask_choice_one_option(self):
        question_helper = QuestionHelper()

        helper_set = HelperSet([FormatterHelper()])
        question_helper.set_helper_set(helper_set)

        heroes = ['Superman']

        question_helper.input_stream = self.get_input_stream('0\n')

        question = ChoiceQuestion('What is your favorite superhero?', heroes)
        question.max_attempts = 1
        self.assertEqual(
            'Superman',
            question_helper.ask(self.get_input(), self.get_output_stream(),
                                question))
Exemplo n.º 5
0
    def test_select(self):
        """
        DialogHelper.select() behaves properly
        """
        dialog = DialogHelper()

        helper_set = HelperSet([FormatterHelper()])
        dialog.set_helper_set(helper_set)

        heroes = ['Superman', 'Batman', 'Spiderman']

        dialog.set_input_stream(
            self.get_input_stream('\n1\nSebastien\n1\nSebastien\nSebastien\n'))
        self.assertEqual(
            2,
            dialog.select(self.get_output_stream(),
                          'What is your favorite superhero?', heroes, '2'))
        self.assertEqual(
            1,
            dialog.select(self.get_output_stream(),
                          'What is your favorite superhero?', heroes))

        output = self.get_output_stream()
        self.assertEqual(
            1,
            dialog.select(output, 'What is your favorite superhero?', heroes,
                          None, False, 'Input "%s" is not a superhero!'))

        output.get_stream().seek(0)
        self.assertTrue(
            re.match('.*Input "Sebastien" is not a superhero!.*',
                     output.get_stream().read().decode()) is not None)

        output = self.get_output_stream()
        self.assertRaises(Exception, dialog.select, output,
                          'What is your favorite superhero?', heroes, None, 1)
    def test_ask_choice(self):
        question_helper = QuestionHelper()

        helper_set = HelperSet([FormatterHelper()])
        question_helper.set_helper_set(helper_set)

        heroes = ['Superman', 'Batman', 'Spiderman']

        question_helper.input_stream = self.get_input_stream('\n1\n  1  \nJohn\n1\nJohn\n1\n0,2\n 0 , 2  \n\n\n')

        question = ChoiceQuestion('What is your favorite superhero?', heroes, '2')
        question.max_attempts = 1
        # First answer is an empty answer, we're supposed to receive the default value
        self.assertEqual(
            'Spiderman',
            question_helper.ask(self.get_input(), self.get_output_stream(), question)
        )

        question = ChoiceQuestion('What is your favorite superhero?', heroes)
        question.max_attempts = 1
        self.assertEqual(
            'Batman',
            question_helper.ask(self.get_input(), self.get_output_stream(), question)
        )
        self.assertEqual(
            'Batman',
            question_helper.ask(self.get_input(), self.get_output_stream(), question)
        )

        question = ChoiceQuestion('What is your favorite superhero?', heroes)
        question.error_message = 'Input "%s" is not a superhero!'
        question.max_attempts = 2
        output = self.get_output_stream()
        self.assertEqual(
            'Batman',
            question_helper.ask(self.get_input(), output, question)
        )

        output.get_stream().seek(0)
        self.assertRegex(decode(output.get_stream().read()), 'Input "John" is not a superhero!')

        try:
            question = ChoiceQuestion('What is your favorite superhero?', heroes, '1')
            question.max_attempts = 1
            output = self.get_output_stream()
            question_helper.ask(self.get_input(), output, question)
            self.fail()
        except Exception as e:
            self.assertEqual('Value "John" is invalid', str(e))

        question = ChoiceQuestion('What is your favorite superhero?', heroes, None)
        question.max_attempts = 1
        question.multiselect = True

        self.assertEqual(
            ['Batman'],
            question_helper.ask(self.get_input(), self.get_output_stream(), question)
        )
        self.assertEqual(
            ['Superman', 'Spiderman'],
            question_helper.ask(self.get_input(), self.get_output_stream(), question)
        )
        self.assertEqual(
            ['Superman', 'Spiderman'],
            question_helper.ask(self.get_input(), self.get_output_stream(), question)
        )

        question = ChoiceQuestion('What is your favorite superhero?', heroes, '0,1')
        question.max_attempts = 1
        question.multiselect = True

        self.assertEqual(
            ['Superman', 'Batman'],
            question_helper.ask(self.get_input(), self.get_output_stream(), question)
        )

        question = ChoiceQuestion('What is your favorite superhero?', heroes, ' 0 , 1 ')
        question.max_attempts = 1
        question.multiselect = True

        self.assertEqual(
            ['Superman', 'Batman'],
            question_helper.ask(self.get_input(), self.get_output_stream(), question)
        )