Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions babel/messages/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,33 @@ def values_to_compare(obj):
return _cmp(values_to_compare(self), values_to_compare(other))

def __gt__(self, other: object) -> bool:
if not isinstance(other, Message):
return NotImplemented
return self.__cmp__(other) > 0

def __lt__(self, other: object) -> bool:
if not isinstance(other, Message):
return NotImplemented
return self.__cmp__(other) < 0

def __ge__(self, other: object) -> bool:
if not isinstance(other, Message):
return NotImplemented
return self.__cmp__(other) >= 0

def __le__(self, other: object) -> bool:
if not isinstance(other, Message):
return NotImplemented
return self.__cmp__(other) <= 0

def __eq__(self, other: object) -> bool:
if not isinstance(other, Message):
return NotImplemented
return self.__cmp__(other) == 0

def __ne__(self, other: object) -> bool:
if not isinstance(other, Message):
return NotImplemented
return self.__cmp__(other) != 0

def is_identical(self, other: Message) -> bool:
Expand Down
31 changes: 31 additions & 0 deletions tests/messages/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import copy
import datetime
import operator
import pickle
from io import StringIO

Expand All @@ -22,6 +23,36 @@
from babel.util import FixedOffsetTimezone


@pytest.mark.parametrize('other', [None, 1, 'foo', object()])
def test_message_equality_with_other_types(other):
message = catalog.Message('foo')
assert not (message == other)
assert not (other == message)
assert message != other
assert other != message


@pytest.mark.parametrize('other', [None, 1, 'foo', object()])
@pytest.mark.parametrize('compare', [operator.lt, operator.le, operator.gt, operator.ge])
def test_message_ordering_with_other_types(other, compare):
message = catalog.Message('foo')
with pytest.raises(TypeError):
compare(message, other)
with pytest.raises(TypeError):
compare(other, message)


def test_message_equality_uses_reflected_comparison():
class EqualToMessage:
def __eq__(self, other):
return isinstance(other, catalog.Message)

message = catalog.Message('foo')
other = EqualToMessage()
assert message == other
assert not (message != other)


def test_message_python_format():
assert catalog.PYTHON_FORMAT.search('foo %d bar')
assert catalog.PYTHON_FORMAT.search('foo %s bar')
Expand Down