示例#1
0
    def test_ask_and_validate(self):
        """
        DialogHelper.ask_and_validate() behaves properly
        """
        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, validator, 2, 'white'))
        self.assertEqual('black', dialog.ask_and_validate(self.get_output_stream(), question, validator, 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, validator, 2, 'white')
示例#2
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')
示例#3
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)
示例#4
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)