diff --git a/changes/233.bugfix.rst b/changes/233.bugfix.rst new file mode 100644 index 0000000..b789682 --- /dev/null +++ b/changes/233.bugfix.rst @@ -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. diff --git a/src/travertino/fonts.py b/src/travertino/fonts.py index 7612e49..a707f88 100644 --- a/src/travertino/fonts.py +++ b/src/travertino/fonts.py @@ -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" diff --git a/tests/fonts/test_constructor.py b/tests/fonts/test_constructor.py index d19860c..8729b51 100644 --- a/tests/fonts/test_constructor.py +++ b/tests/fonts/test_constructor.py @@ -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():