Beispiel #1
0
    def test_merge_application_definition(self):
        """
        Command.merge_application_definition() merges command and application.
        """
        application1 = Application()
        application1.get_definition().add_arguments([InputArgument('foo')])
        application1.get_definition().add_options([InputOption('bar')])
        command = SomeCommand()
        command.set_application(application1)
        command.set_definition(
            InputDefinition([
                InputArgument('bar'),
                InputOption('foo')
            ])
        )

        command.merge_application_definition()
        self.assertTrue(command.get_definition().has_argument('foo'))
        self.assertTrue(command.get_definition().has_option('foo'))
        self.assertTrue(command.get_definition().has_argument('bar'))
        self.assertTrue(command.get_definition().has_option('bar'))

        # It should not merge the definitions twice
        command.merge_application_definition()
        self.assertEqual(3, command.get_definition().get_argument_count())
Beispiel #2
0
 def test_get_default(self):
     """
     InputOption.get_default() returns the default value
     """
     option = InputOption('foo', 'f', InputOption.VALUE_OPTIONAL, '',
                          'default')
     self.assertEqual('default',
                      option.get_default(),
                      msg='.get_default() returns the default value')
     option = InputOption('foo', 'f', InputOption.VALUE_REQUIRED, '',
                          'default')
     self.assertEqual('default',
                      option.get_default(),
                      msg='.get_default() returns the default value')
     option = InputOption('foo', 'f', InputOption.VALUE_REQUIRED)
     self.assertEqual(
         None,
         option.get_default(),
         msg='.get_default() returns None if no default value is configured'
     )
     option = InputOption('foo', 'f', InputOption.VALUE_NONE)
     self.assertEqual(
         False,
         option.get_default(),
         msg=
         '.get_default() returns False if the option does not take a value')
Beispiel #3
0
 def get_default_input_definition(self):
     definition = super(Application, self).get_default_input_definition()
     definition.add_option(
         InputOption('--env', None, InputOption.VALUE_OPTIONAL,
                     'Application environment'))
     definition.add_option(
         InputOption('--no-debug', None, InputOption.VALUE_IS_FLAG,
                     'Disable debugging'))
     return definition
Beispiel #4
0
    def test_set_default(self):
        """
        InputOption.set_default() sets the default value
        """
        option = InputOption('foo', 'f', InputOption.VALUE_REQUIRED, '', 'default')
        option.set_default(None)
        self.assertEqual(None, option.get_default(),
                         msg='.set_default() can reset the default value by passing None')
        option.set_default('another')
        self.assertEqual('another', option.get_default(),
                         msg='.set_default() changes the default value')

        option = InputOption('foo', 'f', InputOption.VALUE_NONE)
        self.assertRaises(Exception, option.set_default, 'default')
Beispiel #5
0
 def test_get_description(self):
     """
     InputOption.get_description() returns the message description
     """
     option = InputOption('foo', 'f', None, 'Some description')
     self.assertEqual('Some description', option.get_description(),
                      msg='.get_description() returns the message description')
Beispiel #6
0
 def test_set_get_definition(self):
     """
     Command.get/set_definition properly sets and gets definition
     """
     command = SomeCommand()
     definition = InputDefinition()
     ret = command.set_definition(definition)
     self.assertEqual(command, ret, msg='.set_definition() implements a fluent interface')
     self.assertEqual(definition, command.get_definition(),
                      msg='.set_definition() sets the current InputDefinition instance')
     command.set_definition([InputArgument('foo'), InputOption('bar')])
     self.assertTrue(command.get_definition().has_argument('foo'),
                     msg='.set_definition() also takes an array of InputArguments and InputOptions as an argument')
     self.assertTrue(command.get_definition().has_option('bar'),
                     msg='.set_definition() also takes an array of InputArguments and InputOptions as an argument')
     command.set_definition(InputDefinition())
Beispiel #7
0
    def test_merge_application_definition_without_args_then_with_args_adds_args(self):
        application1 = Application()
        application1.get_definition().add_arguments([InputArgument('foo')])
        application1.get_definition().add_options([InputOption('bar')])
        command = SomeCommand()
        command.set_application(application1)
        command.set_definition(InputDefinition())

        command.merge_application_definition(False)
        self.assertFalse(command.get_definition().has_argument('foo'))
        self.assertTrue(command.get_definition().has_option('bar'))

        command.merge_application_definition(True)
        self.assertTrue(command.get_definition().has_argument('foo'))

        # It should not merge the definitions twice
        command.merge_application_definition()
        self.assertEqual(2, command.get_definition().get_argument_count())
Beispiel #8
0
    def test_init(self):
        """
        InputOption.__init__() behaves properly
        """
        option = InputOption('foo')
        self.assertEqual('foo',
                         option.get_name(),
                         msg='__init__() takes a name as its first argument')
        option = InputOption('--foo')
        self.assertEqual(
            'foo',
            option.get_name(),
            msg='__init__() removes the leading -- of the option name')

        # shortcut argument
        option = InputOption('foo', 'f')
        self.assertEqual(
            'f',
            option.get_shortcut(),
            msg='__init__() can take a shortcut as its second argument')
        option = InputOption('foo', '-f')
        self.assertEqual(
            'f',
            option.get_shortcut(),
            msg='__init__() removes the leading - of the shortcut')
        option = InputOption('foo')
        self.assertEqual(None,
                         option.get_shortcut(),
                         msg='__init__() makes the shortcut null by default')

        # mode argument
        option = InputOption('foo', 'f')
        self.assertFalse(
            option.accept_value(),
            msg='__init__() gives a "InputOption.VALUE_NONE" mode by default')
        self.assertFalse(
            option.is_value_required(),
            msg='__init__() gives a "InputOption.VALUE_NONE" mode by default')
        self.assertFalse(
            option.is_value_optional(),
            msg='__init__() gives a "InputOption.VALUE_NONE" mode by default')

        option = InputOption('foo', 'f', None)
        self.assertFalse(
            option.accept_value(),
            msg='__init__() can take "InputOption.VALUE_NONE" as its mode')
        self.assertFalse(
            option.is_value_required(),
            msg='__init__() can take "InputOption.VALUE_NONE" as its mode')
        self.assertFalse(
            option.is_value_optional(),
            msg='__init__() can take "InputOption.VALUE_NONE" as its mode')

        option = InputOption('foo', 'f', InputOption.VALUE_NONE)
        self.assertFalse(
            option.accept_value(),
            msg='__init__() can take "InputOption.VALUE_NONE" as its mode')
        self.assertFalse(
            option.is_value_required(),
            msg='__init__() can take "InputOption.VALUE_NONE" as its mode')
        self.assertFalse(
            option.is_value_optional(),
            msg='__init__() can take "InputOption.VALUE_NONE" as its mode')

        option = InputOption('foo', 'f', InputOption.VALUE_REQUIRED)
        self.assertTrue(
            option.accept_value(),
            msg='__init__() can take "InputOption.VALUE_REQUIRED" as its mode')
        self.assertTrue(
            option.is_value_required(),
            msg='__init__() can take "InputOption.VALUE_REQUIRED" as its mode')
        self.assertFalse(
            option.is_value_optional(),
            msg='__init__() can take "InputOption.VALUE_REQUIRED" as its mode')

        option = InputOption('foo', 'f', InputOption.VALUE_OPTIONAL)
        self.assertTrue(
            option.accept_value(),
            msg='__init__() can take "InputOption.VALUE_OPTIONAL" as its mode')
        self.assertFalse(
            option.is_value_required(),
            msg='__init__() can take "InputOption.VALUE_OPTIONAL" as its mode')
        self.assertTrue(
            option.is_value_optional(),
            msg='__init__() can take "InputOption.VALUE_OPTIONAL" as its mode')

        self.assertRaises(Exception, InputOption, 'foo', 'f', 'ANOTHER_MODE')
        self.assertRaises(Exception, InputOption, 'foo', 'f', -1)

        validator = Integer()
        option = InputOption('foo', 'f', None, validator=validator)
        self.assertEqual(validator, option.get_validator())

        # Named validator
        validator = 'integer'
        option = InputOption('foo', 'f', None, validator=validator)
        self.assertIsInstance(option.get_validator(), Integer)

        # Native type
        validator = int
        option = InputOption('foo', 'f', None, validator=validator)
        self.assertIsInstance(option.get_validator(), Integer)