curds: Add a way to get a nodes nth child

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2019-04-16 11:00:31 -04:00
parent 35e02b5732
commit 7aa2244f97
2 changed files with 22 additions and 0 deletions

View File

@ -45,3 +45,8 @@ class PlaylistNode:
def n_children(self):
return len(self.children)
def nth_child(self, n):
if n < len(self.children):
return self.children[n]
return None

View File

@ -93,6 +93,23 @@ class TestPlaylistNode(unittest.TestCase):
self.assertEqual(a.next, b)
self.assertEqual(b.prev, a)
def test_node_nth_child(self):
root = node.PlaylistNode()
a = node.PlaylistNode("a")
b = node.PlaylistNode("b")
c = node.PlaylistNode("c")
root.append_child(a)
root.append_child(b)
a.append_child(c)
self.assertEqual( root.nth_child(0), a)
self.assertEqual( root.nth_child(1), b)
self.assertIsNone(root.nth_child(2))
self.assertEqual( a.nth_child(0), c)
self.assertIsNone(a.nth_child(1))
def test_node_parent(self):
root = node.PlaylistNode()
a = node.PlaylistNode("a")