Fix SemanticVersion comparison (#69395)
* Fix SemanticVersion comparison. * Complete tests for _Alpha and _Numeric comparators. * Linting, and add comment.
This commit is contained in:
parent
de59b17c7f
commit
5b9418c06c
2 changed files with 58 additions and 8 deletions
|
@ -72,14 +72,14 @@ class _Alpha:
|
|||
|
||||
raise ValueError
|
||||
|
||||
def __gt__(self, other):
|
||||
return not self.__lt__(other)
|
||||
|
||||
def __le__(self, other):
|
||||
return self.__lt__(other) or self.__eq__(other)
|
||||
|
||||
def __gt__(self, other):
|
||||
return not self.__le__(other)
|
||||
|
||||
def __ge__(self, other):
|
||||
return self.__gt__(other) or self.__eq__(other)
|
||||
return not self.__lt__(other)
|
||||
|
||||
|
||||
class _Numeric:
|
||||
|
@ -115,14 +115,14 @@ class _Numeric:
|
|||
|
||||
raise ValueError
|
||||
|
||||
def __gt__(self, other):
|
||||
return not self.__lt__(other)
|
||||
|
||||
def __le__(self, other):
|
||||
return self.__lt__(other) or self.__eq__(other)
|
||||
|
||||
def __gt__(self, other):
|
||||
return not self.__le__(other)
|
||||
|
||||
def __ge__(self, other):
|
||||
return self.__gt__(other) or self.__eq__(other)
|
||||
return not self.__lt__(other)
|
||||
|
||||
|
||||
class SemanticVersion(Version):
|
||||
|
|
|
@ -268,6 +268,31 @@ def test_alpha():
|
|||
assert _Alpha('b') >= _Alpha('a')
|
||||
assert _Alpha('b') >= _Alpha('b')
|
||||
|
||||
# The following 3*6 tests check that all comparison operators perform
|
||||
# as expected. DO NOT remove any of them, or reformulate them (to remove
|
||||
# the explicit `not`)!
|
||||
|
||||
assert _Alpha('a') == _Alpha('a')
|
||||
assert not _Alpha('a') != _Alpha('a') # pylint: disable=unneeded-not
|
||||
assert not _Alpha('a') < _Alpha('a') # pylint: disable=unneeded-not
|
||||
assert _Alpha('a') <= _Alpha('a')
|
||||
assert not _Alpha('a') > _Alpha('a') # pylint: disable=unneeded-not
|
||||
assert _Alpha('a') >= _Alpha('a')
|
||||
|
||||
assert not _Alpha('a') == _Alpha('b') # pylint: disable=unneeded-not
|
||||
assert _Alpha('a') != _Alpha('b')
|
||||
assert _Alpha('a') < _Alpha('b')
|
||||
assert _Alpha('a') <= _Alpha('b')
|
||||
assert not _Alpha('a') > _Alpha('b') # pylint: disable=unneeded-not
|
||||
assert not _Alpha('a') >= _Alpha('b') # pylint: disable=unneeded-not
|
||||
|
||||
assert not _Alpha('b') == _Alpha('a') # pylint: disable=unneeded-not
|
||||
assert _Alpha('b') != _Alpha('a')
|
||||
assert not _Alpha('b') < _Alpha('a') # pylint: disable=unneeded-not
|
||||
assert not _Alpha('b') <= _Alpha('a') # pylint: disable=unneeded-not
|
||||
assert _Alpha('b') > _Alpha('a')
|
||||
assert _Alpha('b') >= _Alpha('a')
|
||||
|
||||
|
||||
def test_numeric():
|
||||
assert _Numeric(1) == _Numeric(1)
|
||||
|
@ -283,3 +308,28 @@ def test_numeric():
|
|||
assert _Numeric(1) <= _Numeric(2)
|
||||
assert _Numeric(2) >= _Numeric(1)
|
||||
assert _Numeric(2) >= _Numeric(2)
|
||||
|
||||
# The following 3*6 tests check that all comparison operators perform
|
||||
# as expected. DO NOT remove any of them, or reformulate them (to remove
|
||||
# the explicit `not`)!
|
||||
|
||||
assert _Numeric(1) == _Numeric(1)
|
||||
assert not _Numeric(1) != _Numeric(1) # pylint: disable=unneeded-not
|
||||
assert not _Numeric(1) < _Numeric(1) # pylint: disable=unneeded-not
|
||||
assert _Numeric(1) <= _Numeric(1)
|
||||
assert not _Numeric(1) > _Numeric(1) # pylint: disable=unneeded-not
|
||||
assert _Numeric(1) >= _Numeric(1)
|
||||
|
||||
assert not _Numeric(1) == _Numeric(2) # pylint: disable=unneeded-not
|
||||
assert _Numeric(1) != _Numeric(2)
|
||||
assert _Numeric(1) < _Numeric(2)
|
||||
assert _Numeric(1) <= _Numeric(2)
|
||||
assert not _Numeric(1) > _Numeric(2) # pylint: disable=unneeded-not
|
||||
assert not _Numeric(1) >= _Numeric(2) # pylint: disable=unneeded-not
|
||||
|
||||
assert not _Numeric(2) == _Numeric(1) # pylint: disable=unneeded-not
|
||||
assert _Numeric(2) != _Numeric(1)
|
||||
assert not _Numeric(2) < _Numeric(1) # pylint: disable=unneeded-not
|
||||
assert not _Numeric(2) <= _Numeric(1) # pylint: disable=unneeded-not
|
||||
assert _Numeric(2) > _Numeric(1)
|
||||
assert _Numeric(2) >= _Numeric(1)
|
||||
|
|
Loading…
Reference in a new issue