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