def test_adding_multiple_arguments(self): """Test that we can successfully add multiple arguments to the ArgParser.""" # we will not actually pass anything in, so the Namespace will receive # the defaults (if any) - only check the keys of the Namespace derived # dictionary args_to_add = [(['--foo'], {}), (['--bar', '--b'], {})] expected_namespace_keys = ['foo', 'bar'] # + compulsory... # explicitly pass nothing in - will only have compulsory arguments # and the ones we added... parser = ArgParser(central_arguments=None, specific_arguments=None) parser.add_arguments(args_to_add) result_args = parser.parse_args() result_args = vars(result_args).keys() # we could also add compulsory arguments to expected_namespace_keys # and then assertItemsEqual - (order unimportant), but this # is unnecessary - just use loop: # (or we could patch compulsory arguments to be an empty dictionary) for expected_arg in expected_namespace_keys: self.assertIn(expected_arg, result_args)
def test_adding_single_argument_with_unexpected_length_argspec(self): """Test that attempting to add an argument to the ArgParser when the wrong format argspec raises an exception.""" # length of argspec is 3 - this is unexpected args_to_add = [(['--foo'], 'bar', {})] parser = ArgParser(central_arguments=None, specific_arguments=None) with self.assertRaises(AttributeError): parser.add_arguments(args_to_add)
def test_adding_argument_with_defined_kwargs_dict_has_defualt(self): """Test that we can successfully add an argument to the ArgParser, when the argspec contained kwargs, and that the default value is captured.""" args_to_add = [(['--one'], {'default': 1})] parser = ArgParser(central_arguments=None, specific_arguments=None) parser.add_arguments(args_to_add) result_args = parser.parse_args() # `--one` was not passed in, so we pick up the default - let's check # they agree... self.assertEqual(1, result_args.one)
def test_adding_argument_with_defined_kwargs_dict(self): """Test that we can successfully add an argument to the ArgParser, when the argspec contained kwargs.""" # length of argspec is 2... args_to_add = [(['--foo'], {'default': 1})] expected_arg = 'foo' parser = ArgParser(central_arguments=None, specific_arguments=None) parser.add_arguments(args_to_add) result_args = parser.parse_args() result_args = vars(result_args).keys() self.assertIn(expected_arg, result_args)
def test_adding_empty_argument_list_does_nothing(self): """Test that attempting to add an empty list of argspecs to the ArgParser does not add any new arguments.""" args_to_add = [] # add a specific (optional) argument - ensures that even if there are # no compulsory arguments, we have something... # adding arguments after calling parse_args/args will do nothing, so # instead create 2 instances: parser1 = ArgParser(central_arguments=None, specific_arguments=[[['--optional'], {}]]) parser2 = ArgParser(central_arguments=None, specific_arguments=[[['--optional'], {}]]) parser2.add_arguments(args_to_add) self.assertEqual(parser1.parse_args(), parser2.parse_args())