Salome HOME
style: black format
[tools/sat.git] / test / test_021_versionMinorMajorPatch.py
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3
4 #  Copyright (C) 2010-2018  CEA/DEN
5 #
6 #  This library is free software; you can redistribute it and/or
7 #  modify it under the terms of the GNU Lesser General Public
8 #  License as published by the Free Software Foundation; either
9 #  version 2.1 of the License.
10 #
11 #  This library is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 #  Lesser General Public License for more details.
15 #
16 #  You should have received a copy of the GNU Lesser General Public
17 #  License along with this library; if not, write to the Free Software
18 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19
20
21 """\
22 class and utilities to define a version as MAJOR.MINOR.PATCH,
23 and range of versions
24
25 | Given a version number MAJOR.MINOR.PATCH separator "_" or "."
26 | increment the
27 | MAJOR version when you make incompatible API changes,
28 | MINOR version when you add functionality in a backwards-compatible manner,
29 | PATCH version when you make backwards-compatible bug fixes.
30 """
31
32 import os
33 import sys
34
35 import unittest
36 import pprint as PP
37
38 import initializeTest  # set PATH etc for test
39
40 import src.versionMinorMajorPatch as VMMP
41
42 verbose = False  # True
43
44
45 class TestCase(unittest.TestCase):
46     "Test the versionMajorMinorPatch.py" ""
47
48     def test_010(self):
49         if verbose:
50             print(PP.pformat(dir(self)))
51         self.assertTrue(VMMP.only_numbers("") is None)
52         self.assertEqual(VMMP.only_numbers("1.2.3"), "123")
53         self.assertEqual(VMMP.only_numbers("\n11.12.13\n"), "111213")
54         self.assertEqual(VMMP.only_numbers(" \n 11.\t\n\t..12.13-rc2\n"), "1112132")
55
56     def test_015(self):
57         res = "a_b_c"
58         self.assertEqual(
59             VMMP.remove_startswith("version_a_b_c", "version_".split()), res
60         )
61         self.assertEqual(VMMP.remove_startswith("v_a_b_c", "version_ v_".split()), res)
62         self.assertEqual(VMMP.remove_startswith("va_b_c", "version_ v_ v".split()), res)
63
64         ini = "version_a_b_c"
65         self.assertEqual(VMMP.remove_startswith(ini, "V".split()), ini)
66         self.assertEqual(VMMP.remove_startswith(ini, "_".split()), ini)
67         self.assertEqual(VMMP.remove_startswith(ini, "a_b_c".split()), ini)
68         self.assertEqual(VMMP.remove_startswith(ini, "VERSION".split()), ini)
69
70     def test_020(self):
71         res = [11, 222, 3333]
72         self.assertEqual(VMMP.toList_majorMinorPatch("11.222.3333"), res)
73         self.assertEqual(VMMP.toList_majorMinorPatch("11_222_3333"), res)
74         self.assertEqual(VMMP.toList_majorMinorPatch("11.222_3333"), res)
75         self.assertEqual(VMMP.toList_majorMinorPatch("  11.  222 . 3333  "), res)
76         self.assertEqual(
77             VMMP.toList_majorMinorPatch("\n  11  .    222 .   3333   \n"), res
78         )
79         self.assertEqual(
80             VMMP.toList_majorMinorPatch(" \n11.\t222.\r3333\n "), res
81         )  # could be tricky
82
83         self.assertEqual(VMMP.toList_majorMinorPatch("V11.222.3333"), res)
84         self.assertEqual(VMMP.toList_majorMinorPatch("Version11_222_3333"), res)
85         self.assertEqual(VMMP.toList_majorMinorPatch("Version_11_222_3333"), res)
86
87         self.assertEqual(VMMP.toList_majorMinorPatch("11"), [11, 0, 0])
88         self.assertEqual(VMMP.toList_majorMinorPatch("11.0"), [11, 0, 0])
89         self.assertEqual(VMMP.toList_majorMinorPatch("11.2"), [11, 2, 0])
90         self.assertEqual(VMMP.toList_majorMinorPatch("\n1 .    2  \n"), [1, 2, 0])
91
92         with self.assertRaises(Exception):
93             VMMP.toList_majorMinorPatch("")
94         with self.assertRaises(Exception):
95             VMMP.toList_majorMinorPatch("11.")
96         with self.assertRaises(Exception):
97             VMMP.toList_majorMinorPatch("11.2.")
98         with self.assertRaises(Exception):
99             VMMP.toList_majorMinorPatch("11.2.3.")
100         with self.assertRaises(Exception):
101             VMMP.toList_majorMinorPatch(".11")
102         with self.assertRaises(Exception):
103             VMMP.toList_majorMinorPatch("1_2_3_4")
104         with self.assertRaises(Exception):
105             VMMP.toList_majorMinorPatch("_1_2_3_")
106         with self.assertRaises(Exception):
107             VMMP.toList_majorMinorPatch(" \n 11...22.333-rc2\n")
108         with self.assertRaises(Exception):
109             VMMP.toList_majorMinorPatch(" \n 11...22.333-rc2\n")
110         with self.assertRaises(Exception):
111             VMMP.toList_majorMinorPatch(" \n 11...22.333-rc2\n")
112
113     def test_040(self):
114         MMP = VMMP.MinorMajorPatch
115         v = [1, 2, 3]
116         self.assertEqual(MMP(v).__str__(), "1.2.3")
117         self.assertEqual(MMP(v).__str__(sep="_"), "1_2_3")
118         self.assertEqual(str(MMP(v)), "1.2.3")
119
120         self.assertEqual(MMP(v).__repr__(), "version_1_2_3")
121         self.assertEqual(MMP(v).__repr__(sep="."), "version_1.2.3")
122
123         self.assertEqual(MMP(v).strSalome(), "1_2_3")
124         self.assertEqual(MMP(v).strClassic(), "1.2.3")
125
126         self.assertEqual(MMP(["  123 \n", 2, 10]).strClassic(), "123.2.10")
127         self.assertEqual(MMP(["  123 \n", 2, 10]).strSalome(), "123_2_10")
128
129         with self.assertRaises(Exception):
130             MMP([-5, 2, 10])
131         with self.assertRaises(Exception):
132             MMP([5, -2, 10])
133         with self.assertRaises(Exception):
134             MMP([5, 2, -10])
135         with self.assertRaises(Exception):
136             MMP(["-123", 2, 10])
137
138     def test_050(self):
139         MMP = VMMP.MinorMajorPatch
140         v000 = MMP("0.0.0")
141         v010 = MMP("0.1.0")
142         v100 = MMP("1.0.0")
143         v101 = MMP("1.0.1")
144
145         va = v000
146         vb = MMP("0.0.0")
147         self.assertTrue(va == vb)
148         self.assertTrue(va >= vb)
149         self.assertTrue(va <= vb)
150         self.assertFalse(va != vb)
151         self.assertFalse(va > vb)
152         self.assertFalse(va < vb)
153
154         va = v000
155         vb = v010
156         self.assertFalse(va == vb)
157         self.assertFalse(va >= vb)
158         self.assertTrue(va <= vb)
159         self.assertTrue(va != vb)
160         self.assertFalse(va > vb)
161         self.assertTrue(va < vb)
162
163         va = v101
164         vb = v100
165         self.assertFalse(va == vb)
166         self.assertTrue(va >= vb)
167         self.assertFalse(va <= vb)
168         self.assertTrue(va != vb)
169         self.assertTrue(va > vb)
170         self.assertFalse(va < vb)
171
172     def test_060(self):
173         MMP = VMMP.MinorMajorPatch
174         v0 = MMP("0")
175         v1 = MMP("1")
176         v2 = MMP("2")
177         v123 = MMP("1.2.3")
178         v456 = MMP("4.5.6")
179
180         tests = """\
181 toto_from_1_to_2
182    _from_1.0.0_to_2.0.0
183 _from_1_0.  0_to_  2.0_0
184 _from_V1.0.0_to_2.0.0
185 _from_version_1.0.0_to_2.0.0
186 version_1.0.0_to_2.0.0
187 VERSION_1.0.0_to_2.0.0""".split(
188             "\n"
189         )
190
191         for a in tests:
192             # print("test '%s'" % a)
193             r1, r2 = VMMP.getRange_majorMinorPatch(a)
194             self.assertEqual(r1, v1)
195             self.assertEqual(r2, v2)
196
197         a = "toto_to_2"
198         r1, r2 = VMMP.getRange_majorMinorPatch(a)
199         self.assertEqual(r1, v0)
200         self.assertEqual(r2, v2)
201
202         a = "toto_to_Version2"
203         r1, r2 = VMMP.getRange_majorMinorPatch(a)
204         self.assertEqual(r1, v0)
205         self.assertEqual(r2, v2)
206
207         a = "toto_from_1.2.3_to_Version4_5_6"
208         r1, r2 = VMMP.getRange_majorMinorPatch(a)
209         self.assertEqual(r1, v123)
210         self.assertEqual(r2, v456)
211
212         a = "toto_from_1.2.3_to_Version1_2_3"
213         r1, r2 = VMMP.getRange_majorMinorPatch(a)
214         self.assertEqual(r1, v123)
215         self.assertEqual(r2, v123)
216
217         # _from_ without _to_ does not matter
218         tests = """\
219
220 toto
221 from
222 to
223 _from_
224 toto_from_2""".split(
225             "\n"
226         )
227
228         for a in tests:
229             rx = VMMP.getRange_majorMinorPatch(a, verbose=False)
230             self.assertEqual(rx, None)
231
232         # _to_ without _from_ does not matter, as implicit _from_ '0.0.0'
233         # empty _to_ raise error
234         with self.assertRaises(Exception):
235             VMMP.getRange_majorMinorPatch("_to_")
236         with self.assertRaises(Exception):
237             VMMP.getRange_majorMinorPatch("_from_to_")
238         with self.assertRaises(Exception):
239             VMMP.getRange_majorMinorPatch("_from__to_")
240         with self.assertRaises(Exception):
241             VMMP.getRange_majorMinorPatch("toto_from__to_")
242         with self.assertRaises(Exception):
243             VMMP.getRange_majorMinorPatch("toto_from_123_to_")
244         with self.assertRaises(Exception):
245             VMMP.getRange_majorMinorPatch("version_123_to_")
246         with self.assertRaises(Exception):
247             VMMP.getRange_majorMinorPatch("version_to_")
248
249         # min > max does matter
250         with self.assertRaises(Exception):
251             VMMP.getRange_majorMinorPatch("_from_3_to_2")
252         with self.assertRaises(Exception):
253             VMMP.getRange_majorMinorPatch("_from_3.2.5_to_V2_1_1")
254
255
256 if __name__ == "__main__":
257     unittest.main(exit=False)
258     pass