summaryrefslogtreecommitdiff
path: root/tests/test_parsing.py
blob: 5112ca5a7ed23782906d3564be2c22f13481c05e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2012-2014 The python-semanticversion project
# This code is distributed under the two-clause BSD License.

import unittest

import semantic_version


class ParsingTestCase(unittest.TestCase):
    invalids = [
        None,
        '',
        '0',
        '0.1',
        '0.1.4a',
        '0.1.1.1',
        '0.1.2-rc23,1',
    ]

    valids = [
        '0.1.1',
        '0.1.2-rc1',
        '0.1.2-rc1.3.4',
        '0.1.2+build42-12.2012-01-01.12h23',
        '0.1.2-rc1.3-14.15+build.2012-01-01.11h34',
    ]

    def test_invalid(self):
        for invalid in self.invalids:
            self.assertRaises(ValueError, semantic_version.Version, invalid)

    def test_simple(self):
        for valid in self.valids:
            version = semantic_version.Version(valid)
            self.assertEqual(valid, str(version))


class ComparisonTestCase(unittest.TestCase):
    order = [
        '1.0.0-alpha',
        '1.0.0-alpha.1',
        '1.0.0-beta.2',
        '1.0.0-beta.11',
        '1.0.0-rc.1',
        '1.0.0-rc.1+build.1',
        '1.0.0',
        '1.0.0+0.3.7',
        '1.3.7+build',
        '1.3.7+build.2.b8f12d7',
        '1.3.7+build.11.e0f985a',
    ]

    def test_comparisons(self):
        for i, first in enumerate(self.order):
            first_ver = semantic_version.Version(first)
            for j, second in enumerate(self.order):
                second_ver = semantic_version.Version(second)
                if i < j:
                    self.assertTrue(first_ver < second_ver, '%r !< %r' % (first_ver, second_ver))
                elif i == j:
                    self.assertTrue(first_ver == second_ver, '%r != %r' % (first_ver, second_ver))
                else:
                    self.assertTrue(first_ver > second_ver, '%r !> %r' % (first_ver, second_ver))

                cmp_res = -1 if i < j else (1 if i > j else 0)
                self.assertEqual(cmp_res, semantic_version.compare(first, second))


if __name__ == '__main__':  # pragma: no cover
    unittest.main()