From acd9d4cb3eb59ff48865f38a4a26a60ce2c44f87 Mon Sep 17 00:00:00 2001 From: Michal Srb Date: Mon, 27 Aug 2018 14:06:20 +0200 Subject: [PATCH] Implement rich comparison for !=, >=, <= --- f8a_version_comparator/comparable_version.py | 30 ++++++++++++ tests/test_version_comparator.py | 49 ++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/f8a_version_comparator/comparable_version.py b/f8a_version_comparator/comparable_version.py index 08f134d..7f540c7 100644 --- a/f8a_version_comparator/comparable_version.py +++ b/f8a_version_comparator/comparable_version.py @@ -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. @@ -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. @@ -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 diff --git a/tests/test_version_comparator.py b/tests/test_version_comparator.py index 9ee893c..3c45c07 100644 --- a/tests/test_version_comparator.py +++ b/tests/test_version_comparator.py @@ -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' @@ -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' @@ -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")