Skip to content

Commit

Permalink
Merge pull request #233 from HalfWhitt/fix-font-eq
Browse files Browse the repository at this point in the history
Fixed equality check between Font and non-Font
  • Loading branch information
freakboy3742 authored Nov 8, 2024
2 parents bae5b49 + 05cf7af commit ccd7063
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions changes/233.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug which caused an equality check between a Font object and a non-Font to throw an exception instead of returning False.
17 changes: 10 additions & 7 deletions src/travertino/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ def __repr__(self):
)

def __eq__(self, other):
return (
self.family == other.family
and self.size == other.size
and self.style == other.style
and self.variant == other.variant
and self.weight == other.weight
)
try:
return (
self.family == other.family
and self.size == other.size
and self.style == other.style
and self.variant == other.variant
and self.weight == other.weight
)
except AttributeError:
return False

def normal_style(self):
"Generate a normal style version of this font"
Expand Down
26 changes: 24 additions & 2 deletions tests/fonts/test_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,30 @@ def assert_font(font, family, size, style, variant, weight):
assert font.weight == weight


def test_equality():
assert Font("Comic Sans", "12 pt") == Font("Comic Sans", 12, NORMAL, NORMAL, NORMAL)
@pytest.mark.parametrize(
"font",
[
Font("Comic Sans", "12 pt"),
Font("Comic Sans", 12),
Font("Comic Sans", 12, NORMAL, NORMAL, NORMAL),
],
)
def test_equality(font):
assert font == Font("Comic Sans", "12 pt")


@pytest.mark.parametrize(
"font",
[
Font("Comic Sans", 13),
Font("Comic Sans", 12, ITALIC),
Font("Times New Roman", 12, NORMAL, NORMAL, NORMAL),
"a string",
5,
],
)
def test_inqequality(font):
assert font != Font("Comic Sans", "12 pt")


def test_hash():
Expand Down

0 comments on commit ccd7063

Please sign in to comment.