trackdb: Give TagStacks next() and previous() operations

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-07-07 13:39:54 -04:00
parent 90cfcd29f6
commit db5bfdb550
2 changed files with 56 additions and 0 deletions

View File

@ -5,9 +5,25 @@ class TagStack:
def __init__(self):
self.tags = [ ]
def __do_next__(self, tag):
if track := tag.next():
track.add_to_playlist("Previous")
return track
def next(self):
if len(self.tags) == 0:
return self.__do_next__(tags.User["Collection"])
if track := self.__do_next__(self.tags[0]):
return track
self.pop()
return self.next()
def pop(self):
self.tags.pop(0)
def previous(self):
return tags.User["Previous"].next()
def push(self, tag):
if tag == tags.User["Previous"]:
return

View File

@ -4,11 +4,42 @@ from . import stack
from . import tags
import unittest
class FakeTrack:
def __init__(self, n):
self.n = n
def add_to_playlist(self, name):
tags.User.add(name, self)
class TestTagStack(unittest.TestCase):
def tearDown(self):
tags.reset()
def test_tag_stack_init(self):
s = stack.TagStack()
self.assertEqual(s.tags, [ ])
def test_tag_stack_next(self):
s = stack.TagStack()
t = tag.Tag("Test")
s.push(t)
t.tracks = [ FakeTrack(1), FakeTrack(2), FakeTrack(3) ]
tags.User["Collection"].tracks = [ FakeTrack(4), FakeTrack(5) ]
self.assertEqual(s.next().n, 1)
self.assertEqual(s.tags, [ t ])
self.assertEqual(s.next().n, 2)
self.assertEqual(s.tags, [ t ])
self.assertEqual(s.next().n, 3)
self.assertEqual(s.tags, [ t ])
self.assertEqual(s.next().n, 4)
self.assertEqual(s.tags, [ ])
self.assertEqual(s.next().n, 5)
self.assertEqual([ t.n for t in tags.User["Previous"].tracks ],
[ 5, 4, 3, 2, 1 ])
def test_tag_stack_pop(self):
s = stack.TagStack()
t1 = tag.Tag("Test")
@ -20,6 +51,15 @@ class TestTagStack(unittest.TestCase):
s.pop()
self.assertEqual(s.tags, [ ])
def test_tag_stack_previous(self):
s = stack.TagStack()
for i in [ 1, 2, 3 ]:
tags.User["Previous"].add_track(i)
self.assertEqual(s.previous(), 2)
self.assertEqual(s.previous(), 1)
self.assertIsNone(s.previous())
def test_tag_stack_push(self):
s = stack.TagStack()
t1 = tag.Tag("Test")