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")
99 MMP = VMMP.MinorMajorPatch
101 self.assertEqual(MMP(v).__str__(), "1.2.3")
102 self.assertEqual(MMP(v).__str__(sep="_"), "1_2_3")
103 self.assertEqual(str(MMP(v)), "1.2.3")
105 self.assertEqual(MMP(v).__repr__(), "version_1_2_3")
106 self.assertEqual(MMP(v).__repr__(sep="."), "version_1.2.3")
108 self.assertEqual(MMP(v).strSalome(), "1_2_3")
109 self.assertEqual(MMP(v).strClassic(), "1.2.3")
111 self.assertEqual(MMP([' 123 \n', 2, 10]).strClassic(), "123.2.10")
112 self.assertEqual(MMP([' 123 \n', 2, 10]).strSalome(), "123_2_10")
114 with self.assertRaises(Exception): MMP([-5, 2, 10])
115 with self.assertRaises(Exception): MMP([5, -2, 10])
116 with self.assertRaises(Exception): MMP([5, 2, -10])
117 with self.assertRaises(Exception): MMP(['-123', 2, 10])
120 MMP = VMMP.MinorMajorPatch
128 self.assertTrue(va == vb)
129 self.assertTrue(va >= vb)
130 self.assertTrue(va <= vb)
131 self.assertFalse(va != vb)
132 self.assertFalse(va > vb)
133 self.assertFalse(va < vb)
137 self.assertFalse(va == vb)
138 self.assertFalse(va >= vb)
139 self.assertTrue(va <= vb)
140 self.assertTrue(va != vb)
141 self.assertFalse(va > vb)
142 self.assertTrue(va < vb)
146 self.assertFalse(va == vb)
147 self.assertTrue(va >= vb)
148 self.assertFalse(va <= vb)
149 self.assertTrue(va != vb)
150 self.assertTrue(va > vb)
151 self.assertFalse(va < vb)
154 MMP = VMMP.MinorMajorPatch
164 _from_1_0. 0_to_ 2.0_0
165 _from_V1.0.0_to_2.0.0
166 _from_version_1.0.0_to_2.0.0
167 version_1.0.0_to_2.0.0
168 VERSION_1.0.0_to_2.0.0""".split("\n")
171 # print("test '%s'" % a)
172 r1, r2 = VMMP.getRange_majorMinorPatch(a)
173 self.assertEqual(r1, v1)
174 self.assertEqual(r2, v2)
177 r1, r2 = VMMP.getRange_majorMinorPatch(a)
178 self.assertEqual(r1, v0)
179 self.assertEqual(r2, v2)
181 a = "toto_to_Version2"
182 r1, r2 = VMMP.getRange_majorMinorPatch(a)
183 self.assertEqual(r1, v0)
184 self.assertEqual(r2, v2)
186 a = "toto_from_1.2.3_to_Version4_5_6"
187 r1, r2 = VMMP.getRange_majorMinorPatch(a)
188 self.assertEqual(r1, v123)
189 self.assertEqual(r2, v456)
191 a = "toto_from_1.2.3_to_Version1_2_3"
192 r1, r2 = VMMP.getRange_majorMinorPatch(a)
193 self.assertEqual(r1, v123)
194 self.assertEqual(r2, v123)
196 # _from_ without _to_ does not matter
203 toto_from_2""".split("\n")
206 rx = VMMP.getRange_majorMinorPatch(a, verbose=False)
207 self.assertEqual(rx, None)
209 # _to_ without _from_ does not matter, as implicit _from_ '0.0.0'
210 # empty _to_ raise error
211 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("_to_")
212 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("_from_to_")
213 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("_from__to_")
214 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("toto_from__to_")
215 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("toto_from_123_to_")
216 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("version_123_to_")
217 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("version_to_")
219 # min > max does matter
220 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("_from_3_to_2")
221 with self.assertRaises(Exception): VMMP.getRange_majorMinorPatch("_from_3.2.5_to_V2_1_1")
223 if __name__ == '__main__':
224 unittest.main(exit=False)