updated docstrings
This commit is contained in:
@@ -5,6 +5,9 @@ class Composite(object):
|
|||||||
def __init__(self, interface):
|
def __init__(self, interface):
|
||||||
"""
|
"""
|
||||||
Initialize a new Composite instance.
|
Initialize a new Composite instance.
|
||||||
|
|
||||||
|
@param interface: The interface the all child components must adhere to when added to this composite.
|
||||||
|
@type interface: class
|
||||||
"""
|
"""
|
||||||
self.components = set()
|
self.components = set()
|
||||||
self.interface = interface
|
self.interface = interface
|
||||||
@@ -14,6 +17,7 @@ class Composite(object):
|
|||||||
Add a component to this composite.
|
Add a component to this composite.
|
||||||
|
|
||||||
@param component: The component to add to this Composite
|
@param component: The component to add to this Composite
|
||||||
|
@raise AttributeError: If the component does not adhere to this Composites interface.
|
||||||
"""
|
"""
|
||||||
valid = False
|
valid = False
|
||||||
try:
|
try:
|
||||||
@@ -40,12 +44,13 @@ class Composite(object):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def delegate(self, func_name):
|
def _delegate(self, func_name):
|
||||||
"""
|
"""
|
||||||
Apply a function to all child components by function name.
|
Apply a function to all child components by function name.
|
||||||
|
|
||||||
@param func_name: The name of the function to call with all child components.
|
@param func_name: The name of the function to call with all child components.
|
||||||
@type func_name: str
|
@type func_name: str
|
||||||
|
@raise AttributeError: If a child component does not have a callable function with the given name.
|
||||||
"""
|
"""
|
||||||
for component in self.components:
|
for component in self.components:
|
||||||
attribute = getattr(component, func_name)
|
attribute = getattr(component, func_name)
|
||||||
@@ -62,4 +67,4 @@ class Composite(object):
|
|||||||
@type item: str
|
@type item: str
|
||||||
@return: A function that when called will call all child functions with the given function name.
|
@return: A function that when called will call all child functions with the given function name.
|
||||||
"""
|
"""
|
||||||
return lambda: self.delegate(item)
|
return lambda: self._delegate(item)
|
||||||
|
|||||||
@@ -31,7 +31,11 @@ class CompositeTestCase(TestCase):
|
|||||||
self.leaf_three = Leaf()
|
self.leaf_three = Leaf()
|
||||||
|
|
||||||
def test_add_component(self):
|
def test_add_component(self):
|
||||||
|
"""
|
||||||
|
Test the add_component method.
|
||||||
|
|
||||||
|
@raise AssertionError: If the test fails.
|
||||||
|
"""
|
||||||
composite = Composite(self.component_class)
|
composite = Composite(self.component_class)
|
||||||
composite.add_component(self.leaf_one)
|
composite.add_component(self.leaf_one)
|
||||||
|
|
||||||
@@ -56,7 +60,11 @@ class CompositeTestCase(TestCase):
|
|||||||
self.assertSetEqual({self.leaf_three}, composite_three.components)
|
self.assertSetEqual({self.leaf_three}, composite_three.components)
|
||||||
|
|
||||||
def test_remove_component(self):
|
def test_remove_component(self):
|
||||||
|
"""
|
||||||
|
Test the remove_component method.
|
||||||
|
|
||||||
|
@raise AssertionError: If the test fails.
|
||||||
|
"""
|
||||||
composite = Composite(self.component_class)
|
composite = Composite(self.component_class)
|
||||||
|
|
||||||
composite_two = Composite(self.component_class)
|
composite_two = Composite(self.component_class)
|
||||||
@@ -78,7 +86,11 @@ class CompositeTestCase(TestCase):
|
|||||||
self.assertSetEqual(set(), composite.components)
|
self.assertSetEqual(set(), composite.components)
|
||||||
|
|
||||||
def test_delegate(self):
|
def test_delegate(self):
|
||||||
|
"""
|
||||||
|
Test the delegate method.
|
||||||
|
|
||||||
|
@raise AssertionError: If the test fails
|
||||||
|
"""
|
||||||
composite = Composite(self.component_class)
|
composite = Composite(self.component_class)
|
||||||
composite_two = Composite(self.component_class)
|
composite_two = Composite(self.component_class)
|
||||||
composite_three = Composite(self.component_class)
|
composite_three = Composite(self.component_class)
|
||||||
@@ -90,7 +102,7 @@ class CompositeTestCase(TestCase):
|
|||||||
composite_two.add_component(composite_three)
|
composite_two.add_component(composite_three)
|
||||||
composite.add_component(composite_two)
|
composite.add_component(composite_two)
|
||||||
|
|
||||||
composite.delegate('do_something')
|
composite._delegate('do_something')
|
||||||
|
|
||||||
self.assertTrue(self.leaf_one.did_something)
|
self.assertTrue(self.leaf_one.did_something)
|
||||||
self.assertTrue(self.leaf_two.did_something)
|
self.assertTrue(self.leaf_two.did_something)
|
||||||
@@ -101,7 +113,11 @@ class CompositeTestCase(TestCase):
|
|||||||
self.leaf_three.did_something = False
|
self.leaf_three.did_something = False
|
||||||
|
|
||||||
def test_getattr(self):
|
def test_getattr(self):
|
||||||
|
"""
|
||||||
|
Test the getattr method.
|
||||||
|
|
||||||
|
@raise AssertionError: If the test fails.
|
||||||
|
"""
|
||||||
composite = Composite(self.component_class)
|
composite = Composite(self.component_class)
|
||||||
composite_two = Composite(self.component_class)
|
composite_two = Composite(self.component_class)
|
||||||
composite_three = Composite(self.component_class)
|
composite_three = Composite(self.component_class)
|
||||||
@@ -124,7 +140,11 @@ class CompositeTestCase(TestCase):
|
|||||||
self.leaf_three.did_something = False
|
self.leaf_three.did_something = False
|
||||||
|
|
||||||
def test_invalid_getattr(self):
|
def test_invalid_getattr(self):
|
||||||
|
"""
|
||||||
|
Test the getattr method with an invalid attribute.
|
||||||
|
|
||||||
|
@raise AssertionError: If the test fails.
|
||||||
|
"""
|
||||||
composite = Composite(self.component_class)
|
composite = Composite(self.component_class)
|
||||||
composite_two = Composite(self.component_class)
|
composite_two = Composite(self.component_class)
|
||||||
composite_three = Composite(self.component_class)
|
composite_three = Composite(self.component_class)
|
||||||
@@ -141,12 +161,19 @@ class CompositeTestCase(TestCase):
|
|||||||
composite.did_something()
|
composite.did_something()
|
||||||
|
|
||||||
def test_interface(self):
|
def test_interface(self):
|
||||||
|
"""
|
||||||
|
Test the interface functionality.
|
||||||
|
|
||||||
|
@raise AssertionError: If the test fails.
|
||||||
|
"""
|
||||||
class BadComponent(object):
|
class BadComponent(object):
|
||||||
def foo(self):
|
def foo(self):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
class BadLeaf(BadComponent):
|
class BadLeaf(BadComponent):
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
def foo(self):
|
def foo(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -157,4 +184,3 @@ class CompositeTestCase(TestCase):
|
|||||||
self.assertRaises(AttributeError, composite_two.add_component, self.leaf_one)
|
self.assertRaises(AttributeError, composite_two.add_component, self.leaf_one)
|
||||||
self.assertRaises(AttributeError, composite.add_component, composite_two)
|
self.assertRaises(AttributeError, composite.add_component, composite_two)
|
||||||
self.assertRaises(AttributeError, composite.add_component, BadLeaf())
|
self.assertRaises(AttributeError, composite.add_component, BadLeaf())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user