summaryrefslogtreecommitdiff
path: root/tests/test_match.py
blob: f807828feb64b515f3ef6ea96ed926737ba099f9 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) The python-semanticversion project
# This code is distributed under the two-clause BSD License.

import unittest
import sys

import semantic_version


class MatchTestCase(unittest.TestCase):
    if sys.version_info[0] <= 2:
        import contextlib
        @contextlib.contextmanager
        def subTest(self, **kwargs):
            yield

    invalid_specs = [
        '',
        '!0.1',
        '<=0.1.4a',
        '>0.1.1.1',
        '<0.1.2-rc1.3-14.15+build.2012-01-01.11h34',
    ]

    valid_specs = [
        '*',
        '==0.1.0',
        '=0.1.0',
        '0.1.0',
        '<=0.1.1',
        '<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',
        '^0.1.2',
        '~0.1.2',
        '~=0.1.2',
    ]

    matches = {
        '*': [
            '0.1.1',
            '0.1.1+build4.5',
            '0.1.2-rc1',
            '0.1.2-rc1.3',
            '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',
            '0.2.0',
            '1.0.0',
        ],
        '==0.1.2': [
            '0.1.2+build42-12.2012-01-01.12h23',
        ],
        '=0.1.2': [
            '0.1.2+build42-12.2012-01-01.12h23',
        ],
        '0.1.2': [
            '0.1.2+build42-12.2012-01-01.12h23',
        ],
        '<=0.1.2': [
            '0.1.1',
            '0.1.2-rc1',
            '0.1.2-rc1.3.4',
            '0.1.2',
            '0.1.2+build4',
        ],
        '!=0.1.2+': [
            '0.1.2+1',
            '0.1.2-rc1',
        ],
        '!=0.1.2-': [
            '0.1.1',
            '0.1.2-rc1',
        ],
        '!=0.1.2+345': [
            '0.1.1',
            '0.1.2-rc1+345',
            '0.1.2+346',
            '0.2.3+345',
        ],
        '>=0.1.1': [
            '0.1.1',
            '0.1.1+build4.5',
            '0.1.2-rc1.3',
            '0.2.0',
            '1.0.0',
        ],
        '>0.1.1': [
            '0.1.2+build4.5',
            '0.1.2-rc1.3',
            '0.2.0',
            '1.0.0',
        ],
        '<0.1.1-': [
            '0.1.1-alpha',
            '0.1.1-rc4',
            '0.1.0+12.3',
        ],
        '^0.1.2': [
            '0.1.2',
            '0.1.2+build4.5',
            '0.1.3-rc1.3',
            '0.1.4',
        ],
        '~0.1.2': [
            '0.1.2',
            '0.1.2+build4.5',
            '0.1.3-rc1.3',
        ],
        '~=1.4.5': (
            '1.4.5',
            '1.4.10-alpha',
            '1.4.10',
        ),
        '~=1.4': [
            '1.4.0',
            '1.6.10-alpha',
            '1.6.10',
        ],
    }

    def test_invalid(self):
        for invalid in self.invalid_specs:
            with self.subTest(spec=invalid):
                with self.assertRaises(ValueError, msg="Spec(%r) should be invalid" % invalid):
                    semantic_version.Spec(invalid)

    def test_simple(self):
        for valid in self.valid_specs:
            with self.subTest(spec=valid):
                spec = semantic_version.SpecItem(valid)
                normalized = str(spec)
                self.assertEqual(spec, semantic_version.SpecItem(normalized))

    def test_match(self):
        for spec_text, versions in self.matches.items():
            for version_text in versions:
                with self.subTest(spec=spec_text, version=version_text):
                    spec = semantic_version.Spec(spec_text)
                    self.assertNotEqual(spec, spec_text)
                    version = semantic_version.Version(version_text)
                    self.assertIn(version, spec)
                    self.assertTrue(spec.match(version), "%r does not match %r" % (version, spec))
                    self.assertTrue(semantic_version.match(spec_text, version_text))

    def test_contains(self):
        spec = semantic_version.Spec('<=0.1.1')
        self.assertFalse('0.1.0' in spec, "0.1.0 should not be in %r" % spec)

        version = semantic_version.Version('0.1.1+4.2')
        self.assertTrue(version in spec, "%r should be in %r" % (version, spec))

        version = semantic_version.Version('0.1.1-rc1+4.2')
        self.assertTrue(version in spec, "%r should be in %r" % (version, spec))

    def test_prerelease_check(self):
        strict_spec = semantic_version.Spec('>=0.1.1-')
        lax_spec = semantic_version.Spec('>=0.1.1')
        version = semantic_version.Version('0.1.1-rc1+4.2')
        self.assertFalse(version in lax_spec, "%r should not be in %r" % (version, lax_spec))
        self.assertFalse(version in strict_spec, "%r should not be in %r" % (version, strict_spec))

    def test_build_check(self):
        spec = semantic_version.Spec('<=0.1.1-rc1')
        version = semantic_version.Version('0.1.1-rc1+4.2')
        self.assertTrue(version in spec, "%r should be in %r" % (version, spec))


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