Added util.make_repr() function; added corresponding unit tests

This commit is contained in:
Jason Gerard DeRose 2008-11-13 21:07:47 -07:00
parent 01a7f1f437
commit 8ad5502354
3 changed files with 24 additions and 2 deletions

View File

@ -227,6 +227,7 @@ class Param(plugable.ReadOnly):
)
def __init__(self, name, **override):
self.__override = override
if not ('required' in override or 'multivalue' in override):
(name, kw_from_spec) = parse_param_spec(name)
override.update(kw_from_spec)

View File

@ -137,3 +137,12 @@ class LogFormatter(logging.Formatter):
Log formatter that uses UTC for all timestamps.
"""
converter = time.gmtime
def make_repr(name, *args, **kw):
"""
Construct a standard representation of a class instance.
"""
args = [repr(a) for a in args]
kw = ['%s=%r' % (k, kw[k]) for k in sorted(kw)]
return '%s(%s)' % (name, ', '.join(args + kw))

View File

@ -27,7 +27,7 @@ from ipalib import util
def test_xmlrpc_marshal():
"""
Test the `util.xmlrpc_marshal` function.
Test the `ipalib.util.xmlrpc_marshal` function.
"""
f = util.xmlrpc_marshal
assert f() == ({},)
@ -39,7 +39,7 @@ def test_xmlrpc_marshal():
def test_xmlrpc_unmarshal():
"""
Test the `util.xmlrpc_unmarshal` function.
Test the `ipalib.util.xmlrpc_unmarshal` function.
"""
f = util.xmlrpc_unmarshal
assert f() == (tuple(), {})
@ -47,3 +47,15 @@ def test_xmlrpc_unmarshal():
assert f(dict(one=1, two=2)) == (tuple(), dict(one=1, two=2))
assert f(dict(three=3, four=4), 'one', 'two') == \
(('one', 'two'), dict(three=3, four=4))
def test_make_repr():
"""
Test the `ipalib.util.make_repr` function.
"""
f = util.make_repr
assert f('my') == 'my()'
assert f('my', True, u'hello') == "my(True, u'hello')"
assert f('my', one=1, two='two') == "my(one=1, two='two')"
assert f('my', None, 3, dog='animal', apple='fruit') == \
"my(None, 3, apple='fruit', dog='animal')"