curds: Add a way to walk the node tree
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
8f3178bfc7
commit
718d44a8d8
|
@ -100,3 +100,9 @@ class PlaylistNode:
|
|||
for child in self.children:
|
||||
del nodes[id(child)]
|
||||
self.children.clear()
|
||||
|
||||
def walk(self, root=False):
|
||||
if self.parent != None or root == True:
|
||||
yield self
|
||||
for node in self.children:
|
||||
yield from node.walk()
|
||||
|
|
|
@ -250,3 +250,25 @@ class TestPlaylistNode(unittest.TestCase):
|
|||
root.reset()
|
||||
self.assertEqual(root.children, [ ])
|
||||
self.assertEqual(node.nodes, { id(root) : root })
|
||||
|
||||
def test_node_walk(self):
|
||||
root = node.PlaylistNode()
|
||||
a = node.PlaylistNode("a")
|
||||
b = node.PlaylistNode("b")
|
||||
c = node.PlaylistNode("c")
|
||||
d = node.PlaylistNode("d")
|
||||
vals = [ a, b, d, c ]
|
||||
|
||||
root.append_child(a)
|
||||
a.append_child(b)
|
||||
a.append_child(c)
|
||||
b.append_child(d)
|
||||
|
||||
for i, n in enumerate(root.walk()):
|
||||
self.assertEqual(n, vals[i])
|
||||
self.assertEqual(i, 3)
|
||||
|
||||
vals = [ root ] + vals
|
||||
for i, n in enumerate(root.walk(root=True)):
|
||||
self.assertEqual(n, vals[i])
|
||||
self.assertEqual(i, 4)
|
||||
|
|
Loading…
Reference in New Issue