Change Param.__repr__() so it returns the exact expression that could create it; added unit test for Param.__repre__()

This commit is contained in:
Jason Gerard DeRose 2008-11-13 22:16:04 -07:00
parent 1f635269e8
commit 860d391f3e
3 changed files with 31 additions and 15 deletions

View File

@ -431,11 +431,11 @@ containing a command's arguments and options, respectively, as you can see:
>>> list(api.Command.nudge.args) # Iterates through argument names
['programmer']
>>> api.Command.nudge.args.programmer
Param('programmer', Unicode())
Param('programmer')
>>> list(api.Command.nudge.options) # Iterates through option names
['stuff']
>>> api.Command.nudge.options.stuff
Param('stuff', Unicode())
Param('stuff', default=u'documentation')
>>> api.Command.nudge.options.stuff.default
u'documentation'

View File

@ -228,34 +228,34 @@ class Param(plugable.ReadOnly):
)
def __init__(self, name, **override):
self.__param_spec = name
self.__override = override
self.__kw = dict(self.__defaults)
if not ('required' in override or 'multivalue' in override):
(name, kw_from_spec) = parse_param_spec(name)
override.update(kw_from_spec)
kw = dict(self.__defaults)
kw['cli_name'] = name
if not set(kw).issuperset(override):
extra = sorted(set(override) - set(kw))
self.__kw.update(kw_from_spec)
self.__kw['cli_name'] = name
if not set(self.__kw).issuperset(override):
extra = sorted(set(override) - set(self.__kw))
raise TypeError(
'Param.__init__() takes no such kwargs: %s' % ', '.join(extra)
)
kw.update(override)
self.__kw = kw
self.__kw.update(override)
self.name = check_name(name)
self.cli_name = check_name(kw.get('cli_name', name))
self.cli_name = check_name(self.__kw.get('cli_name', name))
self.type = self.__check_isinstance(ipa_types.Type, 'type')
self.doc = self.__check_type(str, 'doc')
self.required = self.__check_type(bool, 'required')
self.multivalue = self.__check_type(bool, 'multivalue')
self.default = kw['default']
df = kw['default_from']
self.default = self.__kw['default']
df = self.__kw['default_from']
if callable(df) and not isinstance(df, DefaultFrom):
df = DefaultFrom(df)
self.default_from = check_type(df, DefaultFrom, 'default_from',
allow_none=True
)
self.flags = frozenset(kw['flags'])
self.__normalize = kw['normalize']
self.flags = frozenset(self.__kw['flags'])
self.__normalize = self.__kw['normalize']
self.rules = self.__check_type(tuple, 'rules')
self.all_rules = (self.type.validate,) + self.rules
self.primary_key = self.__check_type(bool, 'primary_key')
@ -454,7 +454,11 @@ class Param(plugable.ReadOnly):
"""
Return an expresion that could construct this `Param` instance.
"""
return make_repr(self.__class__.__name__, self.name, **self.__override)
return make_repr(
self.__class__.__name__,
self.__param_spec,
**self.__override
)
def create_param(spec):

View File

@ -430,6 +430,18 @@ class test_Param(ClassChecker):
o = self.cls(name, type=ipa_types.Enum(*values))
assert o.get_values() == values
def test_repr(self):
"""
Test the `ipalib.frontend.Param.__repr__` method.
"""
for name in ['name', 'name?', 'name*', 'name+']:
o = self.cls(name)
assert repr(o) == 'Param(%r)' % name
o = self.cls('name', required=False)
assert repr(o) == "Param('name', required=False)"
o = self.cls('name', multivalue=True)
assert repr(o) == "Param('name', multivalue=True)"
def test_create_param():
"""