4 # Copyright (C) 2010-2018 CEA/DEN
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.
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.
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
22 class and utilities to define a version as MAJOR.MINOR.PATCH,
25 | Given a version number MAJOR.MINOR.PATCH separator "_" or "."
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.
38 import initializeTest # set PATH etc for test
40 import src.versionMinorMajorPatch as VMMP
42 verbose = False # True
45 class TestCase(unittest.TestCase):
46 "Test the versionMajorMinorPatch.py" ""
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")
59 VMMP.remove_startswith("version_a_b_c", "version_".split()), res
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)
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)
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)
77 VMMP.toList_majorMinorPatch("\n 11 . 222 . 3333 \n"), res
80 VMMP.toList_majorMinorPatch(" \n11.\t222.\r3333\n "), res
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)
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])
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")
114 MMP = VMMP.MinorMajorPatch
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")
120 self.assertEqual(MMP(v).__repr__(), "version_1_2_3")
121 self.assertEqual(MMP(v).__repr__(sep="."), "version_1.2.3")
123 self.assertEqual(MMP(v).strSalome(), "1_2_3")
124 self.assertEqual(MMP(v).strClassic(), "1.2.3")
126 self.assertEqual(MMP([" 123 \n", 2, 10]).strClassic(), "123.2.10")
127 self.assertEqual(MMP([" 123 \n", 2, 10]).strSalome(), "123_2_10")
129 with self.assertRaises(Exception):
131 with self.assertRaises(Exception):
133 with self.assertRaises(Exception):
135 with self.assertRaises(Exception):
139 MMP = VMMP.MinorMajorPatch
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)
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)
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)
173 MMP = VMMP.MinorMajorPatch
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(
192 # print("test '%s'" % a)
193 r1, r2 = VMMP.getRange_majorMinorPatch(a)
194 self.assertEqual(r1, v1)
195 self.assertEqual(r2, v2)
198 r1, r2 = VMMP.getRange_majorMinorPatch(a)
199 self.assertEqual(r1, v0)
200 self.assertEqual(r2, v2)
202 a = "toto_to_Version2"
203 r1, r2 = VMMP.getRange_majorMinorPatch(a)
204 self.assertEqual(r1, v0)
205 self.assertEqual(r2, v2)
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)
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)
217 # _from_ without _to_ does not matter
224 toto_from_2""".split(
229 rx = VMMP.getRange_majorMinorPatch(a, verbose=False)
230 self.assertEqual(rx, None)
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_")
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")
256 if __name__ == "__main__":
257 unittest.main(exit=False)