Skip to content

Commit

Permalink
Merge pull request #344 from robotools/baseDict_eq
Browse files Browse the repository at this point in the history
add support for __eq__ in a BaseDict
  • Loading branch information
typesupply authored Mar 8, 2021
2 parents be091ae + 88cbd86 commit 4dd4a74
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Lib/defcon/objects/anchor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import absolute_import
import weakref
from defcon.objects.base import BaseDictObject
from defcon.objects.base import BaseDictCompareObject
from defcon.objects.color import Color
from defcon.tools.identifiers import makeRandomIdentifier


class Anchor(BaseDictObject):
class Anchor(BaseDictCompareObject):

"""
This object represents an anchor point.
Expand Down
11 changes: 11 additions & 0 deletions Lib/defcon/objects/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,17 @@ def setDataFromSerialization(self, data):
self.update(data)


class BaseDictCompareObject(BaseDictObject):

# hash and eq must happen on the same class
# otherwise the object becomes unhashable
def __hash__(self):
return id(self)

def __eq__(self, other):
return id(self) == id(other)


def setUfoLibReadValidate(value):
"""
Set the default read validation.
Expand Down
4 changes: 3 additions & 1 deletion Lib/defcon/objects/glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,7 @@ def insertAnchor(self, index, anchor):
if not isinstance(anchor, self._anchorClass):
anchor = self.instantiateAnchor(anchorDict=anchor)

assert anchor not in self._anchors
assert anchor.glyph in (self, None), "This anchor belongs to another glyph."

self.postNotification(notification="Glyph.AnchorWillBeAdded", data=dict(object=anchor))
Expand Down Expand Up @@ -983,7 +984,8 @@ def insertGuideline(self, index, guideline):
This will post a *Glyph.Changed* notification.
"""
assert id(guideline) not in [id(guide) for guide in self.guidelines]

assert guideline not in self.guidelines
if not isinstance(guideline, self._guidelineClass):
guideline = self.instantiateGuideline(guidelineDict=guideline)

Expand Down
4 changes: 2 additions & 2 deletions Lib/defcon/objects/guideline.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import absolute_import
import weakref
from defcon.objects.base import BaseDictObject
from defcon.objects.base import BaseDictCompareObject
from defcon.objects.color import Color
from defcon.tools.identifiers import makeRandomIdentifier


class Guideline(BaseDictObject):
class Guideline(BaseDictCompareObject):

"""
This object represents a guideline.
Expand Down
14 changes: 7 additions & 7 deletions Lib/defcon/test/objects/test_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def test_instantiateGuideline(self):
guideline = font.instantiateGuideline()
self.assertIsInstance(guideline, Guideline)
guideline = font.instantiateGuideline(guidelineDict={"x": 100})
self.assertEqual(guideline, {'x': 100})
self.assertEqual(dict(guideline), {'x': 100})

def test_beginSelfGuidelineNotificationObservation(self):
font = Font(getTestFontPath())
Expand All @@ -370,25 +370,25 @@ def test_appendGuideline(self):
font = Font(getTestFontPath())
guideline1 = Guideline(guidelineDict={"x": 100})
font.appendGuideline(guideline1)
self.assertEqual(font.guidelines, [{'x': 100}])
self.assertEqual([dict(guideline) for guideline in font.guidelines], [{'x': 100}])
guideline2 = Guideline(guidelineDict={"y": 200})
font.appendGuideline(guideline2)
self.assertEqual(font.guidelines, [{'x': 100}, {'y': 200}])
self.assertEqual([dict(guideline) for guideline in font.guidelines], [{'x': 100}, {'y': 200}])
guideline3 = Guideline(guidelineDict={"y": 100})
font.appendGuideline(guideline3)
self.assertEqual(font.guidelines, [{'x': 100}, {'y': 200}, {'y': 100}])
self.assertEqual([dict(guideline) for guideline in font.guidelines], [{'x': 100}, {'y': 200}, {'y': 100}])

def test_insertGuideline(self):
font = Font(getTestFontPath())
guideline1 = Guideline(guidelineDict={"x": 100})
font.insertGuideline(0, guideline1)
self.assertEqual(font.guidelines, [{'x': 100}])
self.assertEqual([dict(guideline) for guideline in font.guidelines], [{'x': 100}])
guideline2 = Guideline(guidelineDict={"y": 200})
font.insertGuideline(0, guideline2)
self.assertEqual(font.guidelines, [{'y': 200}, {'x': 100}])
self.assertEqual([dict(guideline) for guideline in font.guidelines], [{'y': 200}, {'x': 100}])
guideline3 = Guideline(guidelineDict={"y": 100})
font.insertGuideline(2, guideline3)
self.assertEqual(font.guidelines, [{'y': 200}, {'x': 100}, {'y': 100}])
self.assertEqual([dict(guideline) for guideline in font.guidelines], [{'y': 200}, {'x': 100}, {'y': 100}])

def test_removeGuideline(self):
font = Font(getTestFontPath())
Expand Down
14 changes: 14 additions & 0 deletions Lib/defcon/test/objects/test_glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,13 @@ def test_clearAnchors(self):
glyph.clearAnchors()
self.assertEqual(len(glyph.anchors), 0)

def test_duplicatedAnchors(self):
font = Font(getTestFontPath())
glyph = font["A"]
anchor = glyph.anchors[0]
with self.assertRaises(AssertionError):
glyph.appendAnchor(anchor)

def test_appendGuideline(self):
glyph = Glyph()
glyph.dirty = False
Expand All @@ -504,6 +511,13 @@ def test_clearGuidelines(self):
glyph.clearGuidelines()
self.assertEqual(len(glyph.guidelines), 0)

def test_duplicatedGuideline(self):
font = Font(getTestFontPath())
glyph = font.layers["Layer 1"]["A"]
guideline = glyph.guidelines[0]
with self.assertRaises(AssertionError):
glyph.appendGuideline(guideline)

def test_len(self):
font = Font(getTestFontPath())
glyph = font["A"]
Expand Down

0 comments on commit 4dd4a74

Please sign in to comment.