Skip to content

Commit

Permalink
Allow string comprehensions with csemver objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebi2020 committed Mar 10, 2019
1 parent b6ad82b commit 5a709d4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
22 changes: 16 additions & 6 deletions csemver/csemver.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,24 @@ def _zip_vers(self, o):
return zip(list(self._version.values())[:4], list(o._version.values())[:4])

def __eq__(self, value):
if not isinstance(value,csemver):
if isinstance(value,str):
csem_val = csemver(value)
return self == csem_val

if not isinstance(value,csemver):
raise TypeError("This type combination is not supported!");

parts = self._zip_vers(value)
for i,v in parts:
if i != v:
return False
return True
parts = self._zip_vers(value)
for i,v in parts:
if i != v:
return False
return True


def __gt__(self, value):
if isinstance(value,str):
csem_val = csemver(value)
return self > csem_val
if not isinstance(value,csemver):
raise TypeError("This type combination is not supported!");
parts = self._zip_vers(value)
Expand Down Expand Up @@ -197,6 +204,9 @@ def __gt__(self, value):
return False

def __lt__(self, value):
if isinstance(value,str):
csem_val = csemver(value)
return self < csem_val
if not isinstance(value,csemver):
raise TypeError("This type combination is not supported!");
parts = self._zip_vers(value)
Expand Down
22 changes: 22 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def test_del_version(self):
s.delNumber()
self.assertEqual(str(s),"0.1.0")

def test_del_op(self):
if sys.version_info < (3,0):
raise SkipTest("mustbe Python 3.0 or greater")
s = cs.parse("1.0.0")
self.assertEqual(s,"1.0.0")
del s.number
self.assertEqual(s, "0.1.0")

def test_addition(self):
a = cs.parse("0.0.1");
b = cs.parse("0.2.0");
Expand Down Expand Up @@ -92,6 +100,7 @@ def test_comprehension_greater(self):
b = cs.parse("0.1.0-pre")
self.assertTrue(a>b)
self.assertFalse(b>a)
self.assertTrue(a > "0.1.0-pre")

def test_comprehension_smaller(self):
a = cs.parse("1.0.0");
Expand All @@ -112,6 +121,7 @@ def test_comprehension_smaller(self):
b = cs.parse("0.1.0-pre.1")
self.assertTrue(a<b)
self.assertFalse(b<a)
self.assertTrue(a < "0.1.0-pre.1")

def test_comprehension_equal(self):
a = cs.parse("1.0.0");
Expand All @@ -134,11 +144,18 @@ def test_comprehension_equal(self):
b = cs.parse("1.0.0-pre+build");
self.assertFalse(a == b);

a = cs.parse("1.0.0")
self.assertTrue(a == "1.0.0")
self.assertFalse(a == "0.1.0")

def test_comprehension_not_equal(self):
a = cs.parse("1.0.0-pre+build");
b = cs.parse("1.0.0-pre+build1");
self.assertFalse(a != b);
self.assertTrue(a == b);
a = cs.parse("1.0.0")
self.assertTrue(a != "0.1.0")


def test_index_ops(self):
a = cs.parse(); # defaults to 0.1.0
Expand Down Expand Up @@ -204,6 +221,10 @@ def test_greater_equal(self):
self.assertFalse(a >= b)
self.assertTrue(b >= a)
self.assertTrue(a >= a)
self.assertTrue(b >= "1.0.0-pre")
self.assertTrue(b >= "1.0.0")
self.assertFalse(b >= "1.1.0")

a = cs.parse("1.0.0-pre.1.3")
b = cs.parse("1.0.0-pre.1")
self.assertTrue(a >= b)
Expand All @@ -217,6 +238,7 @@ def test_less_equal(self):
self.assertTrue(a <= b)
self.assertFalse(b <= a)
self.assertTrue(a <= a)
self.assertTrue(b <= "1.0.1")

def test_len(self):
a = cs.parse("1.0.0")
Expand Down

0 comments on commit 5a709d4

Please sign in to comment.