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
44 class TestCase(unittest.TestCase):
45 "Test the versionMajorMinorPatch.py"""
48 if verbose: print(PP.pformat(dir(self)))
49 self.assertTrue(VMMP.only_numbers("") is None)
50 self.assertEqual(VMMP.only_numbers("1.2.3"), "123")
51 self.assertEqual(VMMP.only_numbers("\n11.12.13\n"), "111213")
52 self.assertEqual(VMMP.only_numbers(" \n 11.\t\n\t..12.13-rc2\n"), "1112132")
56 self.assertEqual(VMMP.remove_startswith("version_a_b_c", "version_".split()), res)
57 self.assertEqual(VMMP.remove_startswith("v_a_b_c", "version_ v_".split()), res)
58 self.assertEqual(VMMP.remove_startswith("va_b_c", "version_ v_ v".split()), res)
61 self.assertEqual(VMMP.remove_startswith(ini, "V".split()), ini)
62 self.assertEqual(VMMP.remove_startswith(ini, "_".split()), ini)
63 self.assertEqual(VMMP.remove_startswith(ini, "a_b_c".split()), ini)
64 self.assertEqual(VMMP.remove_startswith(ini, "VERSION".split()), ini)
69 self.assertEqual(VMMP.toList_majorMinorPatch("11.222.3333"), res)
70 self.assertEqual(VMMP.toList_majorMinorPatch("11_222_3333"), res)
71 self.assertEqual(VMMP.toList_majorMinorPatch("11.222_3333"), res)
72 self.assertEqual(VMMP.toList_majorMinorPatch(" 11. 222 . 3333 "), res)
73 self.assertEqual(VMMP.toList_majorMinorPatch("\n 11 . 222 . 3333 \n"), res)
74 self.assertEqual(VMMP.toList_majorMinorPatch(" \n11.\t222.\r3333\n "), res) # could be tricky
76 self.assertEqual(VMMP.toList_majorMinorPatch("V11.222.3333"), res)
77 self.assertEqual(VMMP.toList_majorMinorPatch("Version11_222_3333"), res)
78 self.assertEqual(VMMP.toList_majorMinorPatch("Version_11_222_3333"), res)
81 self.assertEqual(VMMP.toList_majorMinorPatch("11"), [11, 0, 0])
82 self.assertEqual(VMMP.toList_majorMinorPatch("11.0"), [11, 0, 0])
83 self.assertEqual(VMMP.toList_majorMinorPatch("11.2"), [11, 2, 0])
84 self.assertEqual(VMMP.toList_majorMinorPatch("\n1 . 2 \n"), [1, 2, 0])
86 with self.assertRaises(Exception): VMMP.toList_majorMinorPatch("")
87 with self.assertRaises(Exception): VMMP.toList_majorMinorPatch("11.")
88 with self.assertRaises(Exception): VMMP.toList_majorMinorPatch("11.2.")
89 with self.assertRaises(Exception): VMMP.toList_majorMinorPatch("11.2.3.")
90 with self.assertRaises(Exception): VMMP.toList_majorMinorPatch(".11")
91 with self.assertRaises(Exception): VMMP.toList_majorMinorPatch("1_2_3_4")
92 with self.assertRaises(Exception): VMMP.toList_majorMinorPatch("_1_2_3_")
93 with self.assertRaises(Exception): VMMP.toList_majorMinorPatch(" \n 11...22.333-rc2\n")
94 with self.assertRaises(Exception): VMMP.toList_majorMinorPatch(" \n 11...22.333-rc2\n")
95 with self.assertRaises(Exception): VMMP.toList_majorMinorPatch(" \n 11...22.333-rc2\n")
98 self.assertEqual(VMMP.toCompactStr_majorMinorPatch([1, 2, 3]), "123")
99 self.assertEqual(VMMP.toCompactStr_majorMinorPatch([11, 2, 3]), "1123")
100 self.assertEqual(VMMP.toCompactStr_majorMinorPatch([1, 9, 9]), "199")
102 with self.assertRaises(Exception): VMMP.toCompactStr_majorMinorPatch([1, 2, 10])
103 with self.assertRaises(Exception): VMMP.toCompactStr_majorMinorPatch([1, 10, 3])
104 with self.assertRaises(Exception): VMMP.toCompactStr_majorMinorPatch([10, 10, 10])
107 MMP = VMMP.MinorMajorPatch
109 self.assertEqual(MMP(v).__str__(), "1.2.3")
110 self.assertEqual(MMP(v).__str__(sep="_"), "1_2_3")
111 self.assertEqual(str(MMP(v)), "1.2.3")
113 self.assertEqual(MMP(v).__repr__(), "version_1_2_3")
114 self.assertEqual(MMP(v).__repr__(sep="."), "version_1.2.3")
116 self.assertEqual(MMP(v).strSalome(), "1_2_3")
117 self.assertEqual(MMP(v).strClassic(), "1.2.3")
119 self.assertEqual(MMP([' 123 \n', 2, 10]).strClassic(), "123.2.10")
120 self.assertEqual(MMP([' 123 \n', 2, 10]).strSalome(), "123_2_10")
121 self.assertEqual(MMP([' 123 \n', 2, 9]).strCompact(), "12329") # no ambigous
123 with self.assertRaises(Exception): MMP([-5, 2, 10])
124 with self.assertRaises(Exception): MMP([5, -2, 10])
125 with self.assertRaises(Exception): MMP([5, 2, -10])
126 with self.assertRaises(Exception): MMP(['-123', 2, 10])
127 with self.assertRaises(Exception): MMP([123, 2, 10].strCompact()) # ambigous
130 MMP = VMMP.MinorMajorPatch
138 self.assertTrue(va == vb)
139 self.assertTrue(va >= vb)
140 self.assertTrue(va <= vb)
141 self.assertFalse(va != vb)
142 self.assertFalse(va > vb)
143 self.assertFalse(va < vb)
147 self.assertFalse(va == vb)
148 self.assertFalse(va >= vb)
149 self.assertTrue(va <= vb)
150 self.assertTrue(va != vb)
151 self.assertFalse(va > vb)
152 self.assertTrue(va < vb)
156 self.assertFalse(va == vb)
157 self.assertTrue(va >= vb)
158 self.assertFalse(va <= vb)
159 self.assertTrue(va != vb)
160 self.assertTrue(va > vb)
161 self.assertFalse(va < vb)
164 MMP = VMMP.MinorMajorPatch
174 _from_1_0. 0_to_ 2.0_0
175 _from_V1.0.0_to_2.0.0
176 _from_version_1.0.0_to_2.0.0
177 version_1.0.0_to_2.0.0
178 VERSION_1.0.0_to_2.0.0""".split("\n")
181 # print("test '%s'" % a)
182 r1, r2 = VMMP.getRange_majorMinorPatch(a)
183 self.assertEqual(r1, v1)
184 self.assertEqual(r2, v2)
187 r1, r2 = VMMP.getRange_majorMinorPatch(a)
188 self.assertEqual(r1, v0)
189 self.assertEqual(r2, v2)
191 a = "toto_to_Version2"
192 r1, r2 = VMMP.getRange_majorMinorPatch(a)
193 self.assertEqual(r1, v0)
194 self.assertEqual(r2, v2)
196 a = "toto_from_1.2.3_to_Version4_5_6"
197 r1, r2 = VMMP.getRange_majorMinorPatch(a)
198 self.assertEqual(r1, v123)
199 self.assertEqual(r2, v456)
201 a = "toto_from_1.2.3_to_Version1_2_3"
202 r1, r2 = VMMP.getRange_majorMinorPatch(a)
203 self.assertEqual(r1, v123)
204 self.assertEqual(r2, v123)
206 # _from_ without _to_ does not matter
213 toto_from_2""".split("\n")
216 rx = VMMP.getRange_majorMinorPatch(a, verbose=False)
217 self.assertEqual(rx, None)
219 # _to_ without _from_ does not matter, as implicit _from_ '0.0.0'
220 # empty _to_ raise error
221 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("_to_")
222 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("_from_to_")
223 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("_from__to_")
224 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("toto_from__to_")
225 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("toto_from_123_to_")
226 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("version_123_to_")
227 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("version_to_")
229 # min > max does matter
230 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("_from_3_to_2")
231 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("_from_3.2.5_to_V2_1_1")
233 if __name__ == '__main__':
234 unittest.main(exit=False)