simplify api by abstracting layer changes

This commit is contained in:
Jakob Ketterl 2021-03-06 22:20:47 +01:00
parent 6af0ad0262
commit 3d20e3ed80
3 changed files with 19 additions and 11 deletions

View File

@ -353,10 +353,12 @@ class PropertyCarousel(PropertyDelegator):
return self.emptyLayer return self.emptyLayer
def addLayer(self, key, value): def addLayer(self, key, value):
self.layers[key] = value if key in self.layers and self.layers[key] is self.pm:
self.layers[key] = value
def hasLayer(self, key): # switch after introducing the new value
return key in self.layers self.switch(key)
else:
self.layers[key] = value
def removeLayer(self, key): def removeLayer(self, key):
if key in self.layers and self.layers[key] is self.pm: if key in self.layers and self.layers[key] is self.pm:

View File

@ -84,7 +84,7 @@ class SdrProfileCarousel(PropertyCarousel):
for profile_id, profile in changes.items(): for profile_id, profile in changes.items():
if profile is PropertyDeleted: if profile is PropertyDeleted:
self.removeLayer(profile_id) self.removeLayer(profile_id)
elif not self.hasLayer(profile_id): else:
self.addLayer(profile_id, profile) self.addLayer(profile_id, profile)
def _getDefaultLayer(self): def _getDefaultLayer(self):

View File

@ -77,12 +77,6 @@ class PropertyCarouselTest(TestCase):
with self.assertRaises(KeyError): with self.assertRaises(KeyError):
pc.switch("doesntmatter") pc.switch("doesntmatter")
def testHasLayer(self):
pc = PropertyCarousel()
pc.addLayer("testkey", PropertyLayer())
self.assertTrue(pc.hasLayer("testkey"))
self.assertFalse(pc.hasLayer("otherkey"))
def testRemoveLayer(self): def testRemoveLayer(self):
pc = PropertyCarousel() pc = PropertyCarousel()
pl = PropertyLayer(testkey="testvalue") pl = PropertyLayer(testkey="testvalue")
@ -117,3 +111,15 @@ class PropertyCarouselTest(TestCase):
pc = PropertyCarousel() pc = PropertyCarousel()
with self.assertRaises(PropertyWriteError): with self.assertRaises(PropertyWriteError):
pc["testkey"] = "testvalue" pc["testkey"] = "testvalue"
def testSendsChangesIfActiveLayerIsReplaced(self):
pc = PropertyCarousel()
pl = PropertyLayer(testkey="testvalue")
pc.addLayer("x", pl)
pc.switch("x")
self.assertEqual(pc["testkey"], "testvalue")
mock = Mock()
pc.wire(mock.method)
pl = PropertyLayer(testkey="othervalue")
pc.addLayer("x", pl)
mock.method.assert_called_once_with({"testkey": "othervalue"})