From c1bfcc6064b3a22c76b986d9339daf6cbd403c80 Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Tue, 21 Dec 2021 15:23:27 +0100 Subject: Added more AB controller tests and features --- abcontroller.py | 24 +++++++++--- abcontroller_unittest.py | 95 +++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 100 insertions(+), 19 deletions(-) diff --git a/abcontroller.py b/abcontroller.py index 8c14c0c..302bc9e 100644 --- a/abcontroller.py +++ b/abcontroller.py @@ -3,25 +3,34 @@ from collections import namedtuple _AB = namedtuple("_AB", ["a", "b"]) class ABController: - def __init__(self): + def __init__(self, enabled=True): self.setPositionCallback = None self._limits = dict() # dictionary of all songs self._songLimits = None # list of limits for selected song self._currentLimits = None # a/b positions of selected limit + self._enabled = enabled - def setCurrentSong(self, path): + def _ensureSongExists(self, path): if path not in self._limits: self._limits[path] = list() + def setCurrentSong(self, path): + self._ensureSongExists(path) self._songLimits = self._limits[path] self._currentLimits = None - def addLimits(self, aLimit, bLimit): - if self._songLimits is None: + def addLimits(self, aLimit, bLimit, song=None): + if song is not None: + self._ensureSongExists(song) + songLimits = self._limits[song] + else: + songLimits = self._songLimits + + if songLimits is None: return ab = _AB(aLimit, bLimit) - self._songLimits.append(ab) + songLimits.append(ab) def setCurrentLimits(self, index): if not self._songLimits: @@ -34,5 +43,8 @@ class ABController: if not self._currentLimits: return - if position > self._currentLimits.b and self.setPositionCallback: + if position > self._currentLimits.b and self.setPositionCallback and self._enabled: self.setPositionCallback(self._currentLimits.a) + + def setEnable(self, enable): + self._enabled = enable diff --git a/abcontroller_unittest.py b/abcontroller_unittest.py index 0ee1554..839c88a 100644 --- a/abcontroller_unittest.py +++ b/abcontroller_unittest.py @@ -1,16 +1,17 @@ from abcontroller import ABController from collections import namedtuple +TCase = namedtuple("TCase", ["currentPosition", "requestedPosition"]) AB = namedtuple("AB", ["a", "b"]) +abLimits = AB(0.2, 0.4) -def checkLimits(uut, aLimit, bLimit): - Test = namedtuple("Test", ["currentPosition", "requestedPosition"]) +def checkLimits(uut, aLimit, bLimit, fail=False): tests = [ - Test(aLimit - 0.1, None), - Test(aLimit, None), - Test(bLimit - 0.1, None), - Test(bLimit, None), - Test(bLimit + 0.1, aLimit) + TCase(aLimit - 0.1, None), + TCase(aLimit, None), + TCase(bLimit - 0.1, None), + TCase(bLimit, None), + TCase(bLimit + 0.1, aLimit if not fail else None) ] requestedPosition = None @@ -29,7 +30,6 @@ def checkLimits(uut, aLimit, bLimit): def test_oneSetOfLimits(): song = "/path/to/song" - abLimits = AB(0.2, 0.4) uut = ABController() uut.setCurrentSong(song) @@ -56,11 +56,80 @@ def test_multipleSetsOfLimits(): checkLimits(uut, l.a, l.b) def test_multipleSongs(): - # different limits in each song - pass + songs = [ + "/path/to/song", + "/path/to/another/song" + ] + abLimits = [ + AB(0.2, 0.4), + AB(0.3, 0.5) + ] + uut = ABController() + for i, s in enumerate(songs): + uut.addLimits(abLimits[i].a, abLimits[i].b, s) + + for i, s in enumerate(songs): + uut.setCurrentSong(s) + uut.setCurrentLimits(0) + + checkLimits(uut, abLimits[i].a, abLimits[i].b) def test_disableAbRepeat(): - pass + song = "/path/to/song" + + uut = ABController() + uut.setCurrentSong(song) + uut.addLimits(abLimits.a, abLimits.b) + uut.setCurrentLimits(0) + + uut.setEnable(False) + checkLimits(uut, abLimits.a, abLimits.b, fail=True) + + uut.setEnable(True) + checkLimits(uut, abLimits.a, abLimits.b) + +def test_addLimitsToSpecificSong(): + song = "/path/to/song" + + uut = ABController() + uut.addLimits(abLimits.a, abLimits.b, song) + uut.setCurrentSong(song) + uut.setCurrentLimits(0) + + checkLimits(uut, abLimits.a, abLimits.b) + +def test_addLimitsWithoutCurrentSong(): + uut = ABController() + uut.addLimits(abLimits.a, abLimits.b) + uut.setCurrentLimits(0) + + checkLimits(uut, abLimits.a, abLimits.b, fail=True) + +def test_addLimitsToSongWithoutCurrentSong(): + song = "/path/to/song" + uut = ABController() + uut.addLimits(abLimits.a, abLimits.b, song) + uut.setCurrentLimits(0) -def test_addLimitsToSong(): - pass + checkLimits(uut, abLimits.a, abLimits.b, fail=True) + + uut.setCurrentSong(song) + + checkLimits(uut, abLimits.a, abLimits.b, fail=True) + + uut.setCurrentLimits(0) + + checkLimits(uut, abLimits.a, abLimits.b) + +def test_addLimitsToCurrentSongButDoNotSetCurrentLimits(): + song = "/path/to/song" + uut = ABController() + uut.setCurrentSong(song) + uut.addLimits(abLimits.a, abLimits.b) + + checkLimits(uut, abLimits.a, abLimits.b, fail=True) + + uut.setCurrentLimits(0) + + checkLimits(uut, abLimits.a, abLimits.b) + -- cgit v1.2.3