Commits

ta...@MacZiade  committed 7b9e01d

separated tests from code

  • Participants
  • Parent commits 097818d
  • Branches tarek-postdev

Comments (0)

Files changed (2)

File test_verlib.py

+"""Tests for distutils.version."""
+import unittest
+import doctest
+
+from verlib import RationalVersion as V
+from verlib import IrrationalVersionError
+from verlib import suggest_rational_version as suggest
+
+class VersionTestCase(unittest.TestCase):
+
+    versions = ((V('1.0'), '1.0'),
+                (V('1.1'), '1.1'),
+                (V('1.2.3'), '1.2.3'),
+                (V('1.2'), '1.2'),
+                (V('1.2.3a4'), '1.2.3a4'),
+                (V('1.2c4'), '1.2c4'),
+                (V('1.2.3.4'), '1.2.3.4'),
+                (V('1.2.3.4.0b3'), '1.2.3.4b3'),
+                (V('1.2.0.0.0'), '1.2'),
+                (V('1.0.dev345'), '1.0.dev345'),
+                (V('1.0.dev456post623'), '1.0.dev456post623'))
+
+    def test_basic_versions(self):
+
+        for v, s in self.versions:
+            self.assertEquals(str(v), s)
+
+    def test_from_parts(self):
+
+        for v, s in self.versions:
+            parts = v.parts
+            v2 = V.from_parts(*v.parts)
+            self.assertEquals(v, v2)
+            self.assertEquals(str(v), str(v2))
+
+    def test_irrational_versions(self):
+
+        irrational = ('1', '1.2a', '1.2.3b', '1.02',
+                      '1.2a03', '1.2a3.04')
+
+        for s in irrational:
+            self.assertRaises(IrrationalVersionError, V, s)
+
+    def test_comparison(self):
+        r"""
+        >>> V('1.2.0') == '1.2'
+        Traceback (most recent call last):
+        ...
+        TypeError: cannot compare RationalVersion and str
+
+        >>> V('1.2.0') == V('1.2')
+        True
+        >>> V('1.2.0') == V('1.2.3')
+        False
+        >>> V('1.2.0') < V('1.2.3')
+        True
+        >>> (V('1.0') > V('1.0b2'))
+        True
+        >>> (V('1.0') > V('1.0c2') > V('1.0c1') > V('1.0b2') > V('1.0b1')
+        ...  > V('1.0a2') > V('1.0a1'))
+        True
+        >>> (V('1.0.0') > V('1.0.0c2') > V('1.0.0c1') > V('1.0.0b2') > V('1.0.0b1')
+        ...  > V('1.0.0a2') > V('1.0.0a1'))
+        True
+
+        >>> V('1.0') < V('1.0.dev456post623')
+        True
+
+        >>> V('1.0.dev456post623') < V('1.0.post456')
+        True
+
+        >>> (V('1.0a1')
+        ...  < V('1.0a2.dev456')
+        ...  < V('1.0a2')
+        ...  < V('1.0a2.1.dev456')  # e.g. need to do a quick post release on 1.0a2
+        ...  < V('1.0a2.1')
+        ...  < V('1.0b1.dev456')
+        ...  < V('1.0b2')
+        ...  < V('1.0c1.dev456')
+        ...  < V('1.0c1')
+        ...  < V('1.0.dev456')
+        ...  < V('1.0')
+        ...  < V('1.0.dev456post623')  # development version of a post release
+        ...  < V('1.0.post456'))
+        True
+        """
+        # must be a simpler way to call the docstrings
+        doctest.run_docstring_examples(self.test_comparison, globals(),
+                                       name='test_comparison')
+
+    def test_suggest_rational_version(self):
+
+        self.assertEquals(suggest('1.0'), '1.0')
+        self.assertEquals(suggest('1.0-alpha1'), '1.0a1')
+        self.assertEquals(suggest('1.0rc2'), '1.0c2')
+        self.assertEquals(suggest('walla walla washington'), None)
+
+def test_suite():
+    return unittest.makeSuite(VersionTestCase)
+
+if __name__ == "__main__":
+    unittest.main(defaultTest="test_suite")
-
-r"""
-"Rational" version definition and parsing for DistutilsVersionFight
-discussion at PyCon 2009.
-
->>> from verlib import RationalVersion as V
-
-
->>> v = V('1.0')
->>> str(v)
-'1.0'
-
->>> v = V('1.1')
->>> str(v)
-'1.1'
-
->>> v = V('1.2.3')
->>> str(v)
-'1.2.3'
-
->>> v = V('1.2')
->>> str(v)
-'1.2'
-
->>> v = V('1.2.3a4')
->>> str(v)
-'1.2.3a4'
-
->>> v = V('1.2c4')
->>> str(v)
-'1.2c4'
-
->>> v = V('1.2.3.4')
->>> str(v)
-'1.2.3.4'
-
->>> v = V('1.2.3.4.0b3')
->>> str(v)
-'1.2.3.4b3'
-
->>> V('1.2.0.0.0') == V('1.2')
-True
-
->>> v = V('1.0.dev345')
->>> str(v)
-'1.0.dev345'
-
->>> v = V('1.0.dev456post623')
->>> str(v)
-'1.0.dev456post623'
-
->>> parts = v.parts
->>> v2 = V.from_parts(*v.parts)
->>> v2 == v
-True
->>> str(v2) == str(v)
-True
-
-# Irrational version strings
-
->>> v = V('1')
-Traceback (most recent call last):
-  ...
-IrrationalVersionError: 1
->>> v = V('1.2a')
-Traceback (most recent call last):
-  ...
-IrrationalVersionError: 1.2a
->>> v = V('1.2.3b')
-Traceback (most recent call last):
-  ...
-IrrationalVersionError: 1.2.3b
->>> v = V('1.02')
-Traceback (most recent call last):
-  ...
-IrrationalVersionError: cannot have leading zero in version number segment: '02' in '1.02'
->>> v = V('1.2a03')
-Traceback (most recent call last):
-  ...
-IrrationalVersionError: cannot have leading zero in version number segment: '03' in '1.2a03'
->>> v = V('1.2a3.04')
-Traceback (most recent call last):
-  ...
-IrrationalVersionError: cannot have leading zero in version number segment: '04' in '1.2a3.04'
-
-# Comparison
-
->>> V('1.2.0') == '1.2'
-Traceback (most recent call last):
-  ...
-TypeError: cannot compare RationalVersion and str
-
->>> V('1.2.0') == V('1.2')
-True
->>> V('1.2.0') == V('1.2.3')
-False
->>> V('1.2.0') < V('1.2.3')
-True
->>> (V('1.0') > V('1.0b2'))
-True
->>> (V('1.0') > V('1.0c2') > V('1.0c1') > V('1.0b2') > V('1.0b1')
-...  > V('1.0a2') > V('1.0a1'))
-True
->>> (V('1.0.0') > V('1.0.0c2') > V('1.0.0c1') > V('1.0.0b2') > V('1.0.0b1')
-...  > V('1.0.0a2') > V('1.0.0a1'))
-True
-
->>> V('1.0') < V('1.0.dev456post623')
-True
-
->>> V('1.0.dev456post623') < V('1.0.post456')
-True
-
->>> (V('1.0a1')
-...  < V('1.0a2.dev456')
-...  < V('1.0a2')
-...  < V('1.0a2.1.dev456')  # e.g. need to do a quick post release on 1.0a2
-...  < V('1.0a2.1')
-...  < V('1.0b1.dev456')
-...  < V('1.0b2')
-...  < V('1.0c1.dev456')
-...  < V('1.0c1')
-...  < V('1.0.dev456')
-...  < V('1.0')
-...  < V('1.0.dev456post623')  # development version of a post release
-...  < V('1.0.post456'))
-True
-
 """
-
+"Rational" version definition and parsing for DistutilsVersionFight discussion at PyCon 2009.
+"""
 import sys
 import re
-from pprint import pprint
-
 
 class IrrationalVersionError(Exception):
     """This is an irrational version."""
     This guard can be disabled by setting that option False.
     """
 
-
 class RationalVersion(object):
     """A rational version.
 
         pass
     return None
 
-
-#---- mainline and test
-
-def _test():
-    import doctest
-    doctest.testmod()
-
-def _play():
-    V = RationalVersion
-    print V('1.0.dev123') < V('1.0.dev456') < V('1.0') < V('1.0.post456') < V('1.0.post789')
-
-if __name__ == "__main__":
-    #_play()
-    _test()