trackdb: Create a TagStack with push() and pop() operations

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-07-07 13:27:52 -04:00
parent 1cd9780cdf
commit 90cfcd29f6
2 changed files with 59 additions and 0 deletions

20
trackdb/stack.py Normal file
View File

@ -0,0 +1,20 @@
# Copyright 2021 (c) Anna Schumaker.
from . import tags
class TagStack:
def __init__(self):
self.tags = [ ]
def pop(self):
self.tags.pop(0)
def push(self, tag):
if tag == tags.User["Previous"]:
return
if tag == tags.User["Collection"]:
self.tags.clear()
return
if tag in self.tags:
self.tags.remove(tag)
self.tags.insert(0, tag)
tag.stacked()

39
trackdb/test_stack.py Normal file
View File

@ -0,0 +1,39 @@
# Copyright 2021 (c) Anna Schumaker.
from lib import tag
from . import stack
from . import tags
import unittest
class TestTagStack(unittest.TestCase):
def test_tag_stack_init(self):
s = stack.TagStack()
self.assertEqual(s.tags, [ ])
def test_tag_stack_pop(self):
s = stack.TagStack()
t1 = tag.Tag("Test")
t2 = tag.Tag("Test Two")
s.tags = [ t1, t2 ]
s.pop()
self.assertEqual(s.tags, [ t2 ])
s.pop()
self.assertEqual(s.tags, [ ])
def test_tag_stack_push(self):
s = stack.TagStack()
t1 = tag.Tag("Test")
t2 = tag.Tag("Test Two")
t1.current = 3
s.push(t1)
self.assertEqual(s.tags, [ t1 ])
self.assertEqual(t1.current, -1)
s.push(t2)
self.assertEqual(s.tags, [ t2, t1 ])
s.push(t1)
self.assertEqual(s.tags, [ t1, t2 ])
s.push(tags.User["Previous"])
self.assertEqual(s.tags, [ t1, t2 ])
s.push(tags.User["Collection"])
self.assertEqual(s.tags, [ ])