Add item_type and bounds to ListOpt

This change adds keyword parameter "item_type" and
"bounds" to ListOpt. It can be helpful for users to validate
items in lists.

DocImpact
Closes-Bug: #1500737
Change-Id: Ib224ac4b654fa9997d4ec044db2f1fde5a48e5f5
This commit is contained in:
Masaki Matsushita 2015-09-29 16:32:50 +09:00
parent 8881f66f9e
commit b901942231
2 changed files with 28 additions and 2 deletions

View File

@ -1076,8 +1076,11 @@ class ListOpt(Opt):
`Kept for backward-compatibility with options not using Opt directly`.
"""
def __init__(self, name, **kwargs):
super(ListOpt, self).__init__(name, type=types.List(), **kwargs)
def __init__(self, name, item_type=None, bounds=None, **kwargs):
super(ListOpt, self).__init__(name,
type=types.List(item_type=item_type,
bounds=bounds),
**kwargs)
class DictOpt(Opt):

View File

@ -1127,6 +1127,29 @@ class ConfigFileOptsTestCase(BaseTestCase):
self.assertTrue(hasattr(self.conf, 'foo'))
self.assertEqual(self.conf.foo, ['b', 'a', 'r'])
def test_conf_file_list_item_type(self):
self.conf.register_cli_opt(cfg.ListOpt('foo',
item_type=types.Integer()))
paths = self.create_tempfiles([('1',
'[DEFAULT]\n'
'foo = 1,2\n')])
self.conf(['--config-file', paths[0]])
self.assertTrue(hasattr(self.conf, 'foo'))
self.assertEqual([1, 2], self.conf.foo)
@mock.patch.object(cfg, 'LOG')
def test_conf_file_list_item_wrong_type(self, mock_log):
cfg.ListOpt('foo', default="bar", item_type=types.Integer())
self.assertEqual(1, mock_log.debug.call_count)
@mock.patch.object(cfg, 'LOG')
def test_conf_file_list_bounds(self, mock_log):
cfg.ListOpt('foo', default="1,2", bounds=True)
self.assertEqual(1, mock_log.debug.call_count)
def test_conf_file_list_use_dname(self):
self._do_dname_test_use(cfg.ListOpt, 'a,b,c', ['a', 'b', 'c'])