curds: Store the node's parent in the node

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2019-04-16 10:41:40 -04:00
parent 1a01dd9047
commit 80e09d0378
2 changed files with 18 additions and 0 deletions

View File

@ -6,6 +6,7 @@ class PlaylistNode:
self.icon = icon
self.name = name
self.next = None
self.parent = None
self.prev = None
def __link_children__(self, b, c):
@ -24,6 +25,7 @@ class PlaylistNode:
if len(self.children) > 0:
prev = self.children[-1]
self.children.append(child)
child.parent = self
self.__link_children__(prev, child)
return self.children.index(child)
@ -31,6 +33,7 @@ class PlaylistNode:
for i, n in enumerate(self.children):
if child < n:
self.children.insert(i, child)
child.parent = self
self.__link_children__(child, n)
return self.children.index(child)
return self.append_child(child)

View File

@ -77,3 +77,18 @@ class TestPlaylistNode(unittest.TestCase):
self.assertEqual(root.children, [ a, b, c, d ])
self.assertEqual(a.next, b)
self.assertEqual(b.prev, a)
def test_node_parent(self):
root = node.PlaylistNode()
a = node.PlaylistNode("a")
b = node.PlaylistNode("b")
c = node.PlaylistNode("c")
root.append_child(a)
a.append_child(b)
b.append_child(c)
self.assertEqual(c.parent, b)
self.assertEqual(b.parent, a)
self.assertEqual(a.parent, root)
self.assertIsNone(root.parent)