summaryrefslogtreecommitdiffstats
path: root/ipalib
diff options
context:
space:
mode:
Diffstat (limited to 'ipalib')
-rw-r--r--ipalib/plugable.py12
-rw-r--r--ipalib/tests/test_plugable.py113
2 files changed, 68 insertions, 57 deletions
diff --git a/ipalib/plugable.py b/ipalib/plugable.py
index b4a6fb10..63de6deb 100644
--- a/ipalib/plugable.py
+++ b/ipalib/plugable.py
@@ -278,13 +278,17 @@ class Plugin(ProxyTarget):
class NameSpace(ReadOnly):
"""
- A read-only namespace of (key, value) pairs that can be accessed
- both as instance attributes and as dictionary items.
+ A read-only namespace of Proxy instances. Proxy.name is used to name the
+ attributes pointing to the Proxy instances, which can also be accesses
+ through a dictionary interface, for example:
+
+ >>> assert namespace.my_proxy is namespace['my_proxy'] # True
"""
def __init__(self, proxies):
"""
- NameSpace
+ `proxies` - an iterable returning the Proxy instances to be contained
+ in this NameSpace.
"""
self.__proxies = tuple(proxies)
self.__d = dict()
@@ -299,7 +303,7 @@ class NameSpace(ReadOnly):
def __iter__(self):
"""
Iterates through the proxies in this NameSpace in the same order they
- were passed in the contructor.
+ were passed to the constructor.
"""
for proxy in self.__proxies:
yield proxy
diff --git a/ipalib/tests/test_plugable.py b/ipalib/tests/test_plugable.py
index af52f4ee..b8242dce 100644
--- a/ipalib/tests/test_plugable.py
+++ b/ipalib/tests/test_plugable.py
@@ -332,62 +332,69 @@ class test_Plugin(ClassChecker):
raises(AssertionError, sub.finalize, api)
-def test_NameSpace():
- cls = plugable.NameSpace
- assert issubclass(cls, plugable.ReadOnly)
+class test_NameSpace(ClassChecker):
+ """
+ Tests the `NameSpace` class.
+ """
+ _cls = plugable.NameSpace
- class base(object):
- __public__ = frozenset((
- 'plusplus',
- ))
+ def test_class(self):
+ assert self.cls.__bases__ == (plugable.ReadOnly,)
- def plusplus(self, n):
- return n + 1
+ def test_namespace(self):
+ class base(object):
+ __public__ = frozenset((
+ 'plusplus',
+ ))
+
+ def plusplus(self, n):
+ return n + 1
- class plugin(base):
- def __init__(self, name):
- self.name = name
-
- def get_name(i):
- return 'noun_verb%d' % i
-
- def get_proxies(n):
- for i in xrange(n):
- yield plugable.Proxy(base, plugin(get_name(i)))
-
- cnt = 20
- ns = cls(get_proxies(cnt))
-
- # Test __len__
- assert len(ns) == cnt
-
- # Test __iter__
- i = None
- for (i, proxy) in enumerate(ns):
- assert type(proxy) is plugable.Proxy
- assert proxy.name == get_name(i)
- assert i == cnt - 1
-
- # Test __contains__, __getitem__, getattr():
- proxies = frozenset(ns)
- for i in xrange(cnt):
- name = get_name(i)
- assert name in ns
- proxy = ns[name]
- assert proxy.name == name
- assert type(proxy) is plugable.Proxy
- assert proxy in proxies
- assert read_only(ns, name) is proxy
-
- # Test dir():
- assert set(get_name(i) for i in xrange(cnt)).issubset(set(dir(ns)))
-
- # Test that KeyError, AttributeError is raised:
- name = get_name(cnt)
- assert name not in ns
- raises(KeyError, getitem, ns, name)
- raises(AttributeError, getattr, ns, name)
- no_set(ns, name)
+ class plugin(base):
+ def __init__(self, name):
+ self.name = name
+
+ def get_name(i):
+ return 'noun_verb%d' % i
+
+ def get_proxies(n):
+ for i in xrange(n):
+ yield plugable.Proxy(base, plugin(get_name(i)))
+
+ cnt = 20
+ ns = self.cls(get_proxies(cnt))
+ assert ns.__islocked__() is True
+
+ # Test __len__
+ assert len(ns) == cnt
+
+ # Test __iter__
+ i = None
+ for (i, proxy) in enumerate(ns):
+ assert type(proxy) is plugable.Proxy
+ assert proxy.name == get_name(i)
+ assert i == cnt - 1
+
+ # Test __contains__, __getitem__, getattr():
+ proxies = frozenset(ns)
+ for i in xrange(cnt):
+ name = get_name(i)
+ assert name in ns
+ proxy = ns[name]
+ assert proxy.name == name
+ assert type(proxy) is plugable.Proxy
+ assert proxy in proxies
+ assert read_only(ns, name) is proxy
+
+ # Test dir():
+ assert set(get_name(i) for i in xrange(cnt)).issubset(dir(ns))
+
+ # Test that KeyError, AttributeError is raised:
+ name = get_name(cnt)
+ assert name not in ns
+ raises(KeyError, getitem, ns, name)
+ raises(AttributeError, getattr, ns, name)
+ no_set(ns, name)
def test_Registrar():