Skip to content

Commit

Permalink
add support for __eq__ in a BaseDict
Browse files Browse the repository at this point in the history
fixing #343

+ tests
  • Loading branch information
typemytype committed Feb 25, 2021
1 parent 723dbd9 commit b877328
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Lib/defcon/objects/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@ def _get_dict(self):
def __hash__(self):
return id(self)

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

def __setitem__(self, key, value):
oldValue = None
if key in self:
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
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 b877328

Please sign in to comment.