forked from cosminstefanxp/version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_version.py
142 lines (110 loc) · 4.53 KB
/
test_version.py
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
from pytest import raises
from version import Version, VersionError
def test_section_2():
"""Section 2: Major, minor, patch.
A normal version number MUST take the form X.Y.Z where
X, Y, and Z are non-negative integers. X is the major
version, Y is the minor version, and Z is the patch
version. Each element MUST increase numerically by
increments of one. For instance: 1.9.0 -> 1.10.0 ->
1.11.0.
"""
assert str(Version('0.0.0')) == '0.0.0'
assert repr(Version('0.0.0')) == "Version('0.0.0')"
assert str(Version('999.999.999')) == '999.999.999'
assert repr(Version('999.999.999')) == "Version('999.999.999')"
with raises(VersionError):
Version('X.Y.Z')
assert Version('1.2.3').major == 1
assert Version('1.2.3').minor == 2
assert Version('1.2.3').patch == 3
assert Version('1.9.0') < Version('1.10.0') < Version('1.11.0')
def test_section_3():
"""Section 3.
When a major version number is incremented, the minor
version and patch version MUST be reset to zero. When a
minor version number is incremented, the patch version
MUST be reset to zero. For instance: 1.1.3 -> 2.0.0 and
2.1.7 -> 2.2.0.
"""
assert Version('1.1.3') < Version('2.0.0')
assert Version('2.1.7') < Version('2.2.0')
def test_section_10():
"""Section 10: Pre-release version.
A pre-release version MAY be denoted by appending a dash
and a series of dot separated identifiers immediately
following the patch version. Identifiers MUST be
comprised of only ASCII alphanumerics and dash
[0-9A-Za-z-]. Pre-release versions satisfy but have a
lower precedence than the associated normal version.
Examples: 1.0.0-alpha, 1.0.0-alpha.1, 1.0.0-0.3.7,
1.0.0-x.7.z.92.
"""
assert Version('1.0.0').pre_release == []
assert Version('1.0.0-alpha').pre_release == ['alpha']
assert Version('1.0.0-alpha.1').pre_release == ['alpha', 1]
assert Version('1.0.0-0.3.7').pre_release == [0, 3, 7]
assert Version('1.0.0-x.7.z.92').pre_release == ['x', 7, 'z', 92]
assert str(Version('1.0.0-x.7.z.92')) == '1.0.0-x.7.z.92'
with raises(VersionError):
Version('1.0.0-')
with raises(VersionError):
Version('1.0.0-$#%')
assert Version('1.0.0') > Version('1.0.0-alpha')
assert Version('1.0.0-alpha') < Version('1.0.0')
def test_section_11():
"""Section 11: Build version.
A build version MAY be denoted by appending a plus sign
and a series of dot separated identifiers immediately
following the patch version or pre-release version.
Identifiers MUST be comprised of only ASCII
alphanumerics and dash [0-9A-Za-z-]. Build versions
satisfy and have a higher precedence than the associated
normal version. Examples: 1.0.0+build.1,
1.3.7+build.11.e0f985a.
"""
assert Version('1.0.0+build.1').build == ['build', 1]
assert Version('1.0.0+build.11.e0f985a').build == ['build', 11, 'e0f985a']
def test_section_12():
"""Section 12: Precedence rules.
Precedence MUST be calculated by separating the version
into major, minor, patch, pre-release, and build
identifiers in that order. Major, minor, and patch
versions are always compared numerically. Pre-release
and build version precedence MUST be determined by
comparing each dot separated identifier as follows:
identifiers consisting of only digits are compared
numerically and identifiers with letters or dashes are
compared lexically in ASCII sort order. Numeric
identifiers always have lower precedence than
non-numeric identifiers. Example: 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.
"""
presorted = [
'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',
]
from random import shuffle
randomized = list(presorted)
shuffle(randomized)
fixed = map(str, sorted(map(Version, randomized)))
assert fixed == presorted
def test_comparing_against_non_version():
with raises(TypeError) as exception:
Version('1.0.0') > None
assert 'cannot compare' in repr(exception.value)
with raises(TypeError) as exception:
Version('1.0.0') == object()
assert 'cannot compare' in repr(exception.value)