• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Tests for distutils.version."""
2import unittest
3from distutils.version import LooseVersion
4from distutils.version import StrictVersion
5from test.support import run_unittest
6
7class VersionTestCase(unittest.TestCase):
8
9    def test_prerelease(self):
10        version = StrictVersion('1.2.3a1')
11        self.assertEqual(version.version, (1, 2, 3))
12        self.assertEqual(version.prerelease, ('a', 1))
13        self.assertEqual(str(version), '1.2.3a1')
14
15        version = StrictVersion('1.2.0')
16        self.assertEqual(str(version), '1.2')
17
18    def test_cmp_strict(self):
19        versions = (('1.5.1', '1.5.2b2', -1),
20                    ('161', '3.10a', ValueError),
21                    ('8.02', '8.02', 0),
22                    ('3.4j', '1996.07.12', ValueError),
23                    ('3.2.pl0', '3.1.1.6', ValueError),
24                    ('2g6', '11g', ValueError),
25                    ('0.9', '2.2', -1),
26                    ('1.2.1', '1.2', 1),
27                    ('1.1', '1.2.2', -1),
28                    ('1.2', '1.1', 1),
29                    ('1.2.1', '1.2.2', -1),
30                    ('1.2.2', '1.2', 1),
31                    ('1.2', '1.2.2', -1),
32                    ('0.4.0', '0.4', 0),
33                    ('1.13++', '5.5.kw', ValueError))
34
35        for v1, v2, wanted in versions:
36            try:
37                res = StrictVersion(v1)._cmp(StrictVersion(v2))
38            except ValueError:
39                if wanted is ValueError:
40                    continue
41                else:
42                    raise AssertionError(("cmp(%s, %s) "
43                                          "shouldn't raise ValueError")
44                                            % (v1, v2))
45            self.assertEqual(res, wanted,
46                             'cmp(%s, %s) should be %s, got %s' %
47                             (v1, v2, wanted, res))
48
49
50    def test_cmp(self):
51        versions = (('1.5.1', '1.5.2b2', -1),
52                    ('161', '3.10a', 1),
53                    ('8.02', '8.02', 0),
54                    ('3.4j', '1996.07.12', -1),
55                    ('3.2.pl0', '3.1.1.6', 1),
56                    ('2g6', '11g', -1),
57                    ('0.960923', '2.2beta29', -1),
58                    ('1.13++', '5.5.kw', -1))
59
60
61        for v1, v2, wanted in versions:
62            res = LooseVersion(v1)._cmp(LooseVersion(v2))
63            self.assertEqual(res, wanted,
64                             'cmp(%s, %s) should be %s, got %s' %
65                             (v1, v2, wanted, res))
66
67def test_suite():
68    return unittest.makeSuite(VersionTestCase)
69
70if __name__ == "__main__":
71    run_unittest(test_suite())
72