• 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            res = StrictVersion(v1)._cmp(v2)
49            self.assertEqual(res, wanted,
50                             'cmp(%s, %s) should be %s, got %s' %
51                             (v1, v2, wanted, res))
52            res = StrictVersion(v1)._cmp(object())
53            self.assertIs(res, NotImplemented,
54                          'cmp(%s, %s) should be NotImplemented, got %s' %
55                          (v1, v2, res))
56
57
58    def test_cmp(self):
59        versions = (('1.5.1', '1.5.2b2', -1),
60                    ('161', '3.10a', 1),
61                    ('8.02', '8.02', 0),
62                    ('3.4j', '1996.07.12', -1),
63                    ('3.2.pl0', '3.1.1.6', 1),
64                    ('2g6', '11g', -1),
65                    ('0.960923', '2.2beta29', -1),
66                    ('1.13++', '5.5.kw', -1))
67
68
69        for v1, v2, wanted in versions:
70            res = LooseVersion(v1)._cmp(LooseVersion(v2))
71            self.assertEqual(res, wanted,
72                             'cmp(%s, %s) should be %s, got %s' %
73                             (v1, v2, wanted, res))
74            res = LooseVersion(v1)._cmp(v2)
75            self.assertEqual(res, wanted,
76                             'cmp(%s, %s) should be %s, got %s' %
77                             (v1, v2, wanted, res))
78            res = LooseVersion(v1)._cmp(object())
79            self.assertIs(res, NotImplemented,
80                          'cmp(%s, %s) should be NotImplemented, got %s' %
81                          (v1, v2, res))
82
83def test_suite():
84    return unittest.makeSuite(VersionTestCase)
85
86if __name__ == "__main__":
87    run_unittest(test_suite())
88