aboutsummaryrefslogtreecommitdiffstats
path: root/build/plugins/lib/nots/semver/tests/test_version.py
blob: e6c0e44225d52e6c04de98a50822da981ba46c4a (plain) (blame)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from functools import cmp_to_key

from build.plugins.lib.nots.semver import Version


def test_from_str():
    # arrange
    version_str = "1.2.3"

    # act
    version = Version.from_str(version_str)

    # assert
    assert version.major == 1
    assert version.minor == 2
    assert version.patch == 3


def test_from_str_bad_version():
    # arrange
    version_str = "best version imaginable"
    error = None

    # act
    try:
        Version.from_str(version_str)
    except Exception as exception:
        error = exception

    # assert
    assert error is not None


def test_is_stable_true():
    # arrange
    version_str = "1.2.3"

    # act + assert
    assert Version.is_stable(version_str)


def test_is_stable_false():
    # arrange
    version_str = "1.2.3-beta1"

    # act + assert
    assert not Version.is_stable(version_str)


def test_is_stable_incorrect():
    # arrange
    version_str = "v1.2.3"

    # act + assert
    assert not Version.is_stable(version_str)


def test_cmp_lt():
    # arrange
    a = Version.from_str("1.2.3")
    b = Version.from_str("1.2.5")

    # act + assert
    assert Version.cmp(a, b) == -1


def test_cmp_gt():
    # arrange
    a = Version.from_str("1.2.3")
    b = Version.from_str("1.2.2")

    # act + assert
    assert Version.cmp(a, b) == 1


def test_cmp_eq():
    # arrange
    a = Version.from_str("1.2.3")
    b = Version.from_str("1.2.3")

    # act + assert
    assert Version.cmp(a, b) == 0


def test_cmp_lt_str():
    # arrange
    a = "1.2.3"
    b = "1.2.5"

    # act + assert
    assert Version.cmp(a, b) == -1


def test_cmp_gt_str():
    # arrange
    a = "1.2.3"
    b = "1.2.2"

    # act + assert
    assert Version.cmp(a, b) == 1


def test_cmp_eq_str():
    # arrange
    a = "1.2.3"
    b = "1.2.3"

    # act + assert
    assert Version.cmp(a, b) == 0


def test_cmp_usage_in_sorted_asc():
    # arrange
    unsorted = ["1.2.3", "2.4.2", "1.2.7"]

    # act + assert
    assert sorted(unsorted, key=cmp_to_key(Version.cmp)) == ["1.2.3", "1.2.7", "2.4.2"]


def test_cmp_usage_in_sorted_desc():
    # arrange
    unsorted = ["1.2.3", "2.4.2", "1.2.7"]

    # act + assert
    assert sorted(unsorted, key=cmp_to_key(Version.cmp), reverse=True) == ["2.4.2", "1.2.7", "1.2.3"]


def test_init_negative_numbers():
    # arrange
    major = 1
    minor = -2
    patch = 3

    error = None

    # act
    try:
        Version(major, minor, patch)
    except Exception as exception:
        error = exception

    # assert
    assert isinstance(error, ValueError)
    assert str(error) == "'minor' is negative. A version can only be positive."


def test_eq():
    # arrange
    version_a = Version.from_str("1.2.3")
    version_b = Version.from_str("1.2.3")

    # act + assert
    assert version_a == version_b


def test_eq_negative():
    # arrange
    version_a = Version.from_str("1.2.3")
    version_b = Version.from_str("3.2.1")

    # act + assert
    assert not version_a == version_b


def test_eq_with_str():
    # arrange
    version = Version.from_str("1.2.3")

    # act + assert
    assert version == "1.2.3"
    assert not version == "1.2.4"


def test_eq_with_invalid_str():
    # arrange
    version = Version.from_str("1.2.3")

    # act + assert
    assert not version == "bla-bla"
    assert not version == "1.2.3-beta"


def test_ne():
    # arrange
    version_a = Version.from_str("3.2.1")
    version_b = Version.from_str("1.2.3")

    # act + assert
    assert version_a != version_b


def test_ne_negative():
    # arrange
    version_a = Version.from_str("1.2.3")
    version_b = Version.from_str("1.2.3")

    # act + assert
    assert not version_a != version_b


def test_ne_with_str():
    # arrange
    version = Version.from_str("1.2.3")

    # act + assert
    assert version != "1.2.4"
    assert not version != "1.2.3"


def test_gt():
    # arrange
    version_a = Version.from_str("3.2.1")
    version_b = Version.from_str("1.2.3")

    # act + assert
    assert version_a > version_b


def test_ge_equals():
    # arrange
    version_a = Version.from_str("1.2.3")
    version_b = Version.from_str("1.2.3")

    # act + assert
    assert version_a >= version_b


def test_ge_exceeds():
    # arrange
    version_a = Version.from_str("3.2.1")
    version_b = Version.from_str("1.2.3")

    # act + assert
    assert version_a >= version_b


def test_lt():
    # arrange
    version_a = Version.from_str("1.2.3")
    version_b = Version.from_str("3.2.1")

    # act + assert
    assert version_a < version_b


def test_le_equals():
    # arrange
    version_a = Version.from_str("1.2.3")
    version_b = Version.from_str("1.2.3")

    # act + assert
    assert version_a <= version_b


def test_le_is_less():
    # arrange
    version_a = Version.from_str("1.2.3")
    version_b = Version.from_str("3.2.1")

    # act + assert
    assert version_a <= version_b


def test_to_tuple():
    # arrange
    version = Version.from_str("1.2.3")

    # act + assert
    assert version.as_tuple() == (1, 2, 3)