示例#1
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()))
    def generate_output(self, expected):
        if isinstance(expected, list):
            expected_out = ''

            for exp in expected:
                expected_out += self.generate_output(exp)
        else:
            count = expected.count('\n')

            expected_out = '\x0D'
            if count:
                expected_out += '\033[%dA' % count

            expected_out += expected

        return decode(expected_out)
示例#3
0
    def test_render_add_rows(self):
        """
        TableHelper.render() should behave properly after adding rows
        """
        for data_set in self.render_data:
            headers, rows, layout, expected = data_set

            output = self.get_output_stream()
            table = Table(output)
            table.set_headers(headers)\
                .add_rows(rows)\
                .set_style(layout)

            table.render()

            self.assertEqual(decode(expected), self.get_output_content(output))
    def get_output_content(self, output):
        output.get_stream().seek(0)

        value = output.get_stream().getvalue()

        return decode(value)
示例#5
0
    def get_output_content(self, output):
        output.get_stream().seek(0)

        value = output.get_stream().getvalue()

        return decode(value).replace(os.linesep, "\n")
    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)
        )