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, fail=False): tests = [ 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 def callback(newPosition): nonlocal requestedPosition requestedPosition = newPosition originalCallback = uut.setPositionCallback uut.setPositionCallback = callback for t in tests: uut.positionChanged(t.currentPosition) assert requestedPosition == t.requestedPosition uut.setPositionCallback = originalCallback def test_oneSetOfLimits(): song = "/path/to/song" uut = ABController() uut.setCurrentSong(song) uut.addLimits(abLimits.a, abLimits.b) uut.setCurrentLimits(0) checkLimits(uut, abLimits.a, abLimits.b) assert uut.getLimits(song) == [abLimits] def test_multipleSetsOfLimits(): song = "/path/to/song" abLimits = [ AB(0.2, 0.4), AB(0.3, 0.5), AB(0.0, 1.2) ] uut = ABController() uut.setCurrentSong(song) for l in abLimits: uut.addLimits(l.a, l.b) for i, l in enumerate(abLimits): uut.setCurrentLimits(i) checkLimits(uut, l.a, l.b) assert uut.getLimits(song) == abLimits def test_multipleSongs(): 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) assert uut.getLimits(s) == [abLimits[i]] def test_disableAbRepeat(): 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) 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)