Skip to content

Commit

Permalink
Merge pull request #27 from msrb/more-rich
Browse files Browse the repository at this point in the history
Implement rich comparison for !=, >=, <=
  • Loading branch information
tisnik authored Aug 27, 2018
2 parents feac744 + acd9d4c commit c3f3b3e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
30 changes: 30 additions & 0 deletions f8a_version_comparator/comparable_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ def __eq__(self, other):

return self.compare_to(other) == 0

def __ne__(self, other):
"""Compare ComparableVersion objects for equality.
This rich comparison implies whether self != other
"""
if other is None:
return True

return self.compare_to(other) != 0

def __lt__(self, other):
"""Compare ComparableVersion objects.
Expand All @@ -75,6 +85,16 @@ def __lt__(self, other):

return self.compare_to(other) == -1

def __le__(self, other):
"""Compare ComparableVersion objects.
This rich comparison implies whether self <= other
"""
if other is None:
return False

return self.compare_to(other) <= 0

def __gt__(self, other):
"""Compare ComparableVersion objects.
Expand All @@ -85,6 +105,16 @@ def __gt__(self, other):

return self.compare_to(other) == 1

def __ge__(self, other):
"""Compare ComparableVersion objects.
This rich comparison implies whether self >= other
"""
if other is None:
return True

return self.compare_to(other) >= 0

def parse_version(self):
"""Parse version."""
# TODO: reduce cyclomatic complexity
Expand Down
49 changes: 49 additions & 0 deletions tests/test_version_comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ def test_eq_operator():
assert not c3 == None # noqa - because we really want to use == here


def test_ne_operator():
"""Test the != operator."""
version1 = '1.0.0'
version2 = '2.0.0'
c1 = ComparableVersion(version1)
c2 = ComparableVersion(version2)
assert c1 != c2

# ComparableVersion != None
assert c1 != None # noqa - because we really want to use != here

version1 = '2.5.3-alpha'
version2 = '1.5.3-alpha'
c3 = ComparableVersion(version1)
c4 = ComparableVersion(version2)
assert c3 != c4

# ComparableVersion != None
assert c3 != None # noqa - because we really want to use != here


def test_lt_operator():
"""Test the < operator."""
version = '1.0.0'
Expand All @@ -143,6 +164,20 @@ def test_lt_operator():
assert not c3 < None


def test_le_operator():
"""Test the <= operator."""
c1 = ComparableVersion('1.0.0')
c2 = ComparableVersion('2.0.0')
assert c1 <= c2

# ComparableVersion <= None
assert not c1 <= None
assert not c2 <= None

c3 = ComparableVersion('2.0.0')
assert c2 <= c3


def test_gt_operator():
"""Test the > operator."""
version = '2.0.0'
Expand All @@ -165,6 +200,20 @@ def test_gt_operator():
assert c3 > None


def test_ge_operator():
"""Test the >= operator."""
c1 = ComparableVersion('2.0.0')
c2 = ComparableVersion('1.0.0')
assert c1 >= c2

# ComparableVersion >= None
assert c1 >= None
assert c2 >= None

c3 = ComparableVersion('1.0.0')
assert c3 >= c2


def test_comparisons():
"""Test function covering all the cases."""
check_version_order("1-alpha-1", "1.0")
Expand Down

0 comments on commit c3f3b3e

Please sign in to comment.