Ensure node 'remove' and 'disassociate' can not be called when frozen
When a tree node is frozen it should not be possible to remove nodes from it or cause that tree node to disassociate itself from its parent, so make sure that the appropriate decorator is used to ensure this can not happen (and add tests that verify an exception is raised when mutations are attempted). Change-Id: I27e4af7b891ea3fd1826d5a5e71e808ad5af7647
This commit is contained in:
@@ -390,6 +390,18 @@ CEO
|
|||||||
root = tree.Node("josh")
|
root = tree.Node("josh")
|
||||||
self.assertTrue(root.empty())
|
self.assertTrue(root.empty())
|
||||||
|
|
||||||
|
def test_after_frozen(self):
|
||||||
|
root = tree.Node("josh")
|
||||||
|
root.add(tree.Node("josh.1"))
|
||||||
|
root.freeze()
|
||||||
|
self.assertTrue(
|
||||||
|
all(n.frozen for n in root.dfs_iter(include_self=True)))
|
||||||
|
self.assertRaises(tree.FrozenNode,
|
||||||
|
root.remove, "josh.1")
|
||||||
|
self.assertRaises(tree.FrozenNode, root.disassociate)
|
||||||
|
self.assertRaises(tree.FrozenNode, root.add,
|
||||||
|
tree.Node("josh.2"))
|
||||||
|
|
||||||
def test_removal(self):
|
def test_removal(self):
|
||||||
root = self._make_species()
|
root = self._make_species()
|
||||||
self.assertIsNotNone(root.remove('reptile'))
|
self.assertIsNotNone(root.remove('reptile'))
|
||||||
|
@@ -200,6 +200,7 @@ class Node(object):
|
|||||||
only_direct=only_direct,
|
only_direct=only_direct,
|
||||||
include_self=include_self)
|
include_self=include_self)
|
||||||
|
|
||||||
|
@misc.disallow_when_frozen(FrozenNode)
|
||||||
def disassociate(self):
|
def disassociate(self):
|
||||||
"""Removes this node from its parent (if any).
|
"""Removes this node from its parent (if any).
|
||||||
|
|
||||||
@@ -219,6 +220,7 @@ class Node(object):
|
|||||||
occurrences += 1
|
occurrences += 1
|
||||||
return occurrences
|
return occurrences
|
||||||
|
|
||||||
|
@misc.disallow_when_frozen(FrozenNode)
|
||||||
def remove(self, item, only_direct=False, include_self=True):
|
def remove(self, item, only_direct=False, include_self=True):
|
||||||
"""Removes a item from this nodes children.
|
"""Removes a item from this nodes children.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user