1 | n/a | import enum |
---|
2 | n/a | import inspect |
---|
3 | n/a | import pydoc |
---|
4 | n/a | import unittest |
---|
5 | n/a | from collections import OrderedDict |
---|
6 | n/a | from enum import Enum, IntEnum, EnumMeta, Flag, IntFlag, unique, auto |
---|
7 | n/a | from io import StringIO |
---|
8 | n/a | from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL |
---|
9 | n/a | from test import support |
---|
10 | n/a | try: |
---|
11 | n/a | import threading |
---|
12 | n/a | except ImportError: |
---|
13 | n/a | threading = None |
---|
14 | n/a | |
---|
15 | n/a | |
---|
16 | n/a | # for pickle tests |
---|
17 | n/a | try: |
---|
18 | n/a | class Stooges(Enum): |
---|
19 | n/a | LARRY = 1 |
---|
20 | n/a | CURLY = 2 |
---|
21 | n/a | MOE = 3 |
---|
22 | n/a | except Exception as exc: |
---|
23 | n/a | Stooges = exc |
---|
24 | n/a | |
---|
25 | n/a | try: |
---|
26 | n/a | class IntStooges(int, Enum): |
---|
27 | n/a | LARRY = 1 |
---|
28 | n/a | CURLY = 2 |
---|
29 | n/a | MOE = 3 |
---|
30 | n/a | except Exception as exc: |
---|
31 | n/a | IntStooges = exc |
---|
32 | n/a | |
---|
33 | n/a | try: |
---|
34 | n/a | class FloatStooges(float, Enum): |
---|
35 | n/a | LARRY = 1.39 |
---|
36 | n/a | CURLY = 2.72 |
---|
37 | n/a | MOE = 3.142596 |
---|
38 | n/a | except Exception as exc: |
---|
39 | n/a | FloatStooges = exc |
---|
40 | n/a | |
---|
41 | n/a | try: |
---|
42 | n/a | class FlagStooges(Flag): |
---|
43 | n/a | LARRY = 1 |
---|
44 | n/a | CURLY = 2 |
---|
45 | n/a | MOE = 3 |
---|
46 | n/a | except Exception as exc: |
---|
47 | n/a | FlagStooges = exc |
---|
48 | n/a | |
---|
49 | n/a | # for pickle test and subclass tests |
---|
50 | n/a | try: |
---|
51 | n/a | class StrEnum(str, Enum): |
---|
52 | n/a | 'accepts only string values' |
---|
53 | n/a | class Name(StrEnum): |
---|
54 | n/a | BDFL = 'Guido van Rossum' |
---|
55 | n/a | FLUFL = 'Barry Warsaw' |
---|
56 | n/a | except Exception as exc: |
---|
57 | n/a | Name = exc |
---|
58 | n/a | |
---|
59 | n/a | try: |
---|
60 | n/a | Question = Enum('Question', 'who what when where why', module=__name__) |
---|
61 | n/a | except Exception as exc: |
---|
62 | n/a | Question = exc |
---|
63 | n/a | |
---|
64 | n/a | try: |
---|
65 | n/a | Answer = Enum('Answer', 'him this then there because') |
---|
66 | n/a | except Exception as exc: |
---|
67 | n/a | Answer = exc |
---|
68 | n/a | |
---|
69 | n/a | try: |
---|
70 | n/a | Theory = Enum('Theory', 'rule law supposition', qualname='spanish_inquisition') |
---|
71 | n/a | except Exception as exc: |
---|
72 | n/a | Theory = exc |
---|
73 | n/a | |
---|
74 | n/a | # for doctests |
---|
75 | n/a | try: |
---|
76 | n/a | class Fruit(Enum): |
---|
77 | n/a | TOMATO = 1 |
---|
78 | n/a | BANANA = 2 |
---|
79 | n/a | CHERRY = 3 |
---|
80 | n/a | except Exception: |
---|
81 | n/a | pass |
---|
82 | n/a | |
---|
83 | n/a | def test_pickle_dump_load(assertion, source, target=None): |
---|
84 | n/a | if target is None: |
---|
85 | n/a | target = source |
---|
86 | n/a | for protocol in range(HIGHEST_PROTOCOL + 1): |
---|
87 | n/a | assertion(loads(dumps(source, protocol=protocol)), target) |
---|
88 | n/a | |
---|
89 | n/a | def test_pickle_exception(assertion, exception, obj): |
---|
90 | n/a | for protocol in range(HIGHEST_PROTOCOL + 1): |
---|
91 | n/a | with assertion(exception): |
---|
92 | n/a | dumps(obj, protocol=protocol) |
---|
93 | n/a | |
---|
94 | n/a | class TestHelpers(unittest.TestCase): |
---|
95 | n/a | # _is_descriptor, _is_sunder, _is_dunder |
---|
96 | n/a | |
---|
97 | n/a | def test_is_descriptor(self): |
---|
98 | n/a | class foo: |
---|
99 | n/a | pass |
---|
100 | n/a | for attr in ('__get__','__set__','__delete__'): |
---|
101 | n/a | obj = foo() |
---|
102 | n/a | self.assertFalse(enum._is_descriptor(obj)) |
---|
103 | n/a | setattr(obj, attr, 1) |
---|
104 | n/a | self.assertTrue(enum._is_descriptor(obj)) |
---|
105 | n/a | |
---|
106 | n/a | def test_is_sunder(self): |
---|
107 | n/a | for s in ('_a_', '_aa_'): |
---|
108 | n/a | self.assertTrue(enum._is_sunder(s)) |
---|
109 | n/a | |
---|
110 | n/a | for s in ('a', 'a_', '_a', '__a', 'a__', '__a__', '_a__', '__a_', '_', |
---|
111 | n/a | '__', '___', '____', '_____',): |
---|
112 | n/a | self.assertFalse(enum._is_sunder(s)) |
---|
113 | n/a | |
---|
114 | n/a | def test_is_dunder(self): |
---|
115 | n/a | for s in ('__a__', '__aa__'): |
---|
116 | n/a | self.assertTrue(enum._is_dunder(s)) |
---|
117 | n/a | for s in ('a', 'a_', '_a', '__a', 'a__', '_a_', '_a__', '__a_', '_', |
---|
118 | n/a | '__', '___', '____', '_____',): |
---|
119 | n/a | self.assertFalse(enum._is_dunder(s)) |
---|
120 | n/a | |
---|
121 | n/a | # tests |
---|
122 | n/a | |
---|
123 | n/a | class TestEnum(unittest.TestCase): |
---|
124 | n/a | |
---|
125 | n/a | def setUp(self): |
---|
126 | n/a | class Season(Enum): |
---|
127 | n/a | SPRING = 1 |
---|
128 | n/a | SUMMER = 2 |
---|
129 | n/a | AUTUMN = 3 |
---|
130 | n/a | WINTER = 4 |
---|
131 | n/a | self.Season = Season |
---|
132 | n/a | |
---|
133 | n/a | class Konstants(float, Enum): |
---|
134 | n/a | E = 2.7182818 |
---|
135 | n/a | PI = 3.1415926 |
---|
136 | n/a | TAU = 2 * PI |
---|
137 | n/a | self.Konstants = Konstants |
---|
138 | n/a | |
---|
139 | n/a | class Grades(IntEnum): |
---|
140 | n/a | A = 5 |
---|
141 | n/a | B = 4 |
---|
142 | n/a | C = 3 |
---|
143 | n/a | D = 2 |
---|
144 | n/a | F = 0 |
---|
145 | n/a | self.Grades = Grades |
---|
146 | n/a | |
---|
147 | n/a | class Directional(str, Enum): |
---|
148 | n/a | EAST = 'east' |
---|
149 | n/a | WEST = 'west' |
---|
150 | n/a | NORTH = 'north' |
---|
151 | n/a | SOUTH = 'south' |
---|
152 | n/a | self.Directional = Directional |
---|
153 | n/a | |
---|
154 | n/a | from datetime import date |
---|
155 | n/a | class Holiday(date, Enum): |
---|
156 | n/a | NEW_YEAR = 2013, 1, 1 |
---|
157 | n/a | IDES_OF_MARCH = 2013, 3, 15 |
---|
158 | n/a | self.Holiday = Holiday |
---|
159 | n/a | |
---|
160 | n/a | def test_dir_on_class(self): |
---|
161 | n/a | Season = self.Season |
---|
162 | n/a | self.assertEqual( |
---|
163 | n/a | set(dir(Season)), |
---|
164 | n/a | set(['__class__', '__doc__', '__members__', '__module__', |
---|
165 | n/a | 'SPRING', 'SUMMER', 'AUTUMN', 'WINTER']), |
---|
166 | n/a | ) |
---|
167 | n/a | |
---|
168 | n/a | def test_dir_on_item(self): |
---|
169 | n/a | Season = self.Season |
---|
170 | n/a | self.assertEqual( |
---|
171 | n/a | set(dir(Season.WINTER)), |
---|
172 | n/a | set(['__class__', '__doc__', '__module__', 'name', 'value']), |
---|
173 | n/a | ) |
---|
174 | n/a | |
---|
175 | n/a | def test_dir_with_added_behavior(self): |
---|
176 | n/a | class Test(Enum): |
---|
177 | n/a | this = 'that' |
---|
178 | n/a | these = 'those' |
---|
179 | n/a | def wowser(self): |
---|
180 | n/a | return ("Wowser! I'm %s!" % self.name) |
---|
181 | n/a | self.assertEqual( |
---|
182 | n/a | set(dir(Test)), |
---|
183 | n/a | set(['__class__', '__doc__', '__members__', '__module__', 'this', 'these']), |
---|
184 | n/a | ) |
---|
185 | n/a | self.assertEqual( |
---|
186 | n/a | set(dir(Test.this)), |
---|
187 | n/a | set(['__class__', '__doc__', '__module__', 'name', 'value', 'wowser']), |
---|
188 | n/a | ) |
---|
189 | n/a | |
---|
190 | n/a | def test_dir_on_sub_with_behavior_on_super(self): |
---|
191 | n/a | # see issue22506 |
---|
192 | n/a | class SuperEnum(Enum): |
---|
193 | n/a | def invisible(self): |
---|
194 | n/a | return "did you see me?" |
---|
195 | n/a | class SubEnum(SuperEnum): |
---|
196 | n/a | sample = 5 |
---|
197 | n/a | self.assertEqual( |
---|
198 | n/a | set(dir(SubEnum.sample)), |
---|
199 | n/a | set(['__class__', '__doc__', '__module__', 'name', 'value', 'invisible']), |
---|
200 | n/a | ) |
---|
201 | n/a | |
---|
202 | n/a | def test_enum_in_enum_out(self): |
---|
203 | n/a | Season = self.Season |
---|
204 | n/a | self.assertIs(Season(Season.WINTER), Season.WINTER) |
---|
205 | n/a | |
---|
206 | n/a | def test_enum_value(self): |
---|
207 | n/a | Season = self.Season |
---|
208 | n/a | self.assertEqual(Season.SPRING.value, 1) |
---|
209 | n/a | |
---|
210 | n/a | def test_intenum_value(self): |
---|
211 | n/a | self.assertEqual(IntStooges.CURLY.value, 2) |
---|
212 | n/a | |
---|
213 | n/a | def test_enum(self): |
---|
214 | n/a | Season = self.Season |
---|
215 | n/a | lst = list(Season) |
---|
216 | n/a | self.assertEqual(len(lst), len(Season)) |
---|
217 | n/a | self.assertEqual(len(Season), 4, Season) |
---|
218 | n/a | self.assertEqual( |
---|
219 | n/a | [Season.SPRING, Season.SUMMER, Season.AUTUMN, Season.WINTER], lst) |
---|
220 | n/a | |
---|
221 | n/a | for i, season in enumerate('SPRING SUMMER AUTUMN WINTER'.split(), 1): |
---|
222 | n/a | e = Season(i) |
---|
223 | n/a | self.assertEqual(e, getattr(Season, season)) |
---|
224 | n/a | self.assertEqual(e.value, i) |
---|
225 | n/a | self.assertNotEqual(e, i) |
---|
226 | n/a | self.assertEqual(e.name, season) |
---|
227 | n/a | self.assertIn(e, Season) |
---|
228 | n/a | self.assertIs(type(e), Season) |
---|
229 | n/a | self.assertIsInstance(e, Season) |
---|
230 | n/a | self.assertEqual(str(e), 'Season.' + season) |
---|
231 | n/a | self.assertEqual( |
---|
232 | n/a | repr(e), |
---|
233 | n/a | '<Season.{0}: {1}>'.format(season, i), |
---|
234 | n/a | ) |
---|
235 | n/a | |
---|
236 | n/a | def test_value_name(self): |
---|
237 | n/a | Season = self.Season |
---|
238 | n/a | self.assertEqual(Season.SPRING.name, 'SPRING') |
---|
239 | n/a | self.assertEqual(Season.SPRING.value, 1) |
---|
240 | n/a | with self.assertRaises(AttributeError): |
---|
241 | n/a | Season.SPRING.name = 'invierno' |
---|
242 | n/a | with self.assertRaises(AttributeError): |
---|
243 | n/a | Season.SPRING.value = 2 |
---|
244 | n/a | |
---|
245 | n/a | def test_changing_member(self): |
---|
246 | n/a | Season = self.Season |
---|
247 | n/a | with self.assertRaises(AttributeError): |
---|
248 | n/a | Season.WINTER = 'really cold' |
---|
249 | n/a | |
---|
250 | n/a | def test_attribute_deletion(self): |
---|
251 | n/a | class Season(Enum): |
---|
252 | n/a | SPRING = 1 |
---|
253 | n/a | SUMMER = 2 |
---|
254 | n/a | AUTUMN = 3 |
---|
255 | n/a | WINTER = 4 |
---|
256 | n/a | |
---|
257 | n/a | def spam(cls): |
---|
258 | n/a | pass |
---|
259 | n/a | |
---|
260 | n/a | self.assertTrue(hasattr(Season, 'spam')) |
---|
261 | n/a | del Season.spam |
---|
262 | n/a | self.assertFalse(hasattr(Season, 'spam')) |
---|
263 | n/a | |
---|
264 | n/a | with self.assertRaises(AttributeError): |
---|
265 | n/a | del Season.SPRING |
---|
266 | n/a | with self.assertRaises(AttributeError): |
---|
267 | n/a | del Season.DRY |
---|
268 | n/a | with self.assertRaises(AttributeError): |
---|
269 | n/a | del Season.SPRING.name |
---|
270 | n/a | |
---|
271 | n/a | def test_bool_of_class(self): |
---|
272 | n/a | class Empty(Enum): |
---|
273 | n/a | pass |
---|
274 | n/a | self.assertTrue(bool(Empty)) |
---|
275 | n/a | |
---|
276 | n/a | def test_bool_of_member(self): |
---|
277 | n/a | class Count(Enum): |
---|
278 | n/a | zero = 0 |
---|
279 | n/a | one = 1 |
---|
280 | n/a | two = 2 |
---|
281 | n/a | for member in Count: |
---|
282 | n/a | self.assertTrue(bool(member)) |
---|
283 | n/a | |
---|
284 | n/a | def test_invalid_names(self): |
---|
285 | n/a | with self.assertRaises(ValueError): |
---|
286 | n/a | class Wrong(Enum): |
---|
287 | n/a | mro = 9 |
---|
288 | n/a | with self.assertRaises(ValueError): |
---|
289 | n/a | class Wrong(Enum): |
---|
290 | n/a | _create_= 11 |
---|
291 | n/a | with self.assertRaises(ValueError): |
---|
292 | n/a | class Wrong(Enum): |
---|
293 | n/a | _get_mixins_ = 9 |
---|
294 | n/a | with self.assertRaises(ValueError): |
---|
295 | n/a | class Wrong(Enum): |
---|
296 | n/a | _find_new_ = 1 |
---|
297 | n/a | with self.assertRaises(ValueError): |
---|
298 | n/a | class Wrong(Enum): |
---|
299 | n/a | _any_name_ = 9 |
---|
300 | n/a | |
---|
301 | n/a | def test_bool(self): |
---|
302 | n/a | # plain Enum members are always True |
---|
303 | n/a | class Logic(Enum): |
---|
304 | n/a | true = True |
---|
305 | n/a | false = False |
---|
306 | n/a | self.assertTrue(Logic.true) |
---|
307 | n/a | self.assertTrue(Logic.false) |
---|
308 | n/a | # unless overridden |
---|
309 | n/a | class RealLogic(Enum): |
---|
310 | n/a | true = True |
---|
311 | n/a | false = False |
---|
312 | n/a | def __bool__(self): |
---|
313 | n/a | return bool(self._value_) |
---|
314 | n/a | self.assertTrue(RealLogic.true) |
---|
315 | n/a | self.assertFalse(RealLogic.false) |
---|
316 | n/a | # mixed Enums depend on mixed-in type |
---|
317 | n/a | class IntLogic(int, Enum): |
---|
318 | n/a | true = 1 |
---|
319 | n/a | false = 0 |
---|
320 | n/a | self.assertTrue(IntLogic.true) |
---|
321 | n/a | self.assertFalse(IntLogic.false) |
---|
322 | n/a | |
---|
323 | n/a | def test_contains(self): |
---|
324 | n/a | Season = self.Season |
---|
325 | n/a | self.assertIn(Season.AUTUMN, Season) |
---|
326 | n/a | self.assertNotIn(3, Season) |
---|
327 | n/a | |
---|
328 | n/a | val = Season(3) |
---|
329 | n/a | self.assertIn(val, Season) |
---|
330 | n/a | |
---|
331 | n/a | class OtherEnum(Enum): |
---|
332 | n/a | one = 1; two = 2 |
---|
333 | n/a | self.assertNotIn(OtherEnum.two, Season) |
---|
334 | n/a | |
---|
335 | n/a | def test_comparisons(self): |
---|
336 | n/a | Season = self.Season |
---|
337 | n/a | with self.assertRaises(TypeError): |
---|
338 | n/a | Season.SPRING < Season.WINTER |
---|
339 | n/a | with self.assertRaises(TypeError): |
---|
340 | n/a | Season.SPRING > 4 |
---|
341 | n/a | |
---|
342 | n/a | self.assertNotEqual(Season.SPRING, 1) |
---|
343 | n/a | |
---|
344 | n/a | class Part(Enum): |
---|
345 | n/a | SPRING = 1 |
---|
346 | n/a | CLIP = 2 |
---|
347 | n/a | BARREL = 3 |
---|
348 | n/a | |
---|
349 | n/a | self.assertNotEqual(Season.SPRING, Part.SPRING) |
---|
350 | n/a | with self.assertRaises(TypeError): |
---|
351 | n/a | Season.SPRING < Part.CLIP |
---|
352 | n/a | |
---|
353 | n/a | def test_enum_duplicates(self): |
---|
354 | n/a | class Season(Enum): |
---|
355 | n/a | SPRING = 1 |
---|
356 | n/a | SUMMER = 2 |
---|
357 | n/a | AUTUMN = FALL = 3 |
---|
358 | n/a | WINTER = 4 |
---|
359 | n/a | ANOTHER_SPRING = 1 |
---|
360 | n/a | lst = list(Season) |
---|
361 | n/a | self.assertEqual( |
---|
362 | n/a | lst, |
---|
363 | n/a | [Season.SPRING, Season.SUMMER, |
---|
364 | n/a | Season.AUTUMN, Season.WINTER, |
---|
365 | n/a | ]) |
---|
366 | n/a | self.assertIs(Season.FALL, Season.AUTUMN) |
---|
367 | n/a | self.assertEqual(Season.FALL.value, 3) |
---|
368 | n/a | self.assertEqual(Season.AUTUMN.value, 3) |
---|
369 | n/a | self.assertIs(Season(3), Season.AUTUMN) |
---|
370 | n/a | self.assertIs(Season(1), Season.SPRING) |
---|
371 | n/a | self.assertEqual(Season.FALL.name, 'AUTUMN') |
---|
372 | n/a | self.assertEqual( |
---|
373 | n/a | [k for k,v in Season.__members__.items() if v.name != k], |
---|
374 | n/a | ['FALL', 'ANOTHER_SPRING'], |
---|
375 | n/a | ) |
---|
376 | n/a | |
---|
377 | n/a | def test_duplicate_name(self): |
---|
378 | n/a | with self.assertRaises(TypeError): |
---|
379 | n/a | class Color(Enum): |
---|
380 | n/a | red = 1 |
---|
381 | n/a | green = 2 |
---|
382 | n/a | blue = 3 |
---|
383 | n/a | red = 4 |
---|
384 | n/a | |
---|
385 | n/a | with self.assertRaises(TypeError): |
---|
386 | n/a | class Color(Enum): |
---|
387 | n/a | red = 1 |
---|
388 | n/a | green = 2 |
---|
389 | n/a | blue = 3 |
---|
390 | n/a | def red(self): |
---|
391 | n/a | return 'red' |
---|
392 | n/a | |
---|
393 | n/a | with self.assertRaises(TypeError): |
---|
394 | n/a | class Color(Enum): |
---|
395 | n/a | @property |
---|
396 | n/a | def red(self): |
---|
397 | n/a | return 'redder' |
---|
398 | n/a | red = 1 |
---|
399 | n/a | green = 2 |
---|
400 | n/a | blue = 3 |
---|
401 | n/a | |
---|
402 | n/a | |
---|
403 | n/a | def test_enum_with_value_name(self): |
---|
404 | n/a | class Huh(Enum): |
---|
405 | n/a | name = 1 |
---|
406 | n/a | value = 2 |
---|
407 | n/a | self.assertEqual( |
---|
408 | n/a | list(Huh), |
---|
409 | n/a | [Huh.name, Huh.value], |
---|
410 | n/a | ) |
---|
411 | n/a | self.assertIs(type(Huh.name), Huh) |
---|
412 | n/a | self.assertEqual(Huh.name.name, 'name') |
---|
413 | n/a | self.assertEqual(Huh.name.value, 1) |
---|
414 | n/a | |
---|
415 | n/a | def test_format_enum(self): |
---|
416 | n/a | Season = self.Season |
---|
417 | n/a | self.assertEqual('{}'.format(Season.SPRING), |
---|
418 | n/a | '{}'.format(str(Season.SPRING))) |
---|
419 | n/a | self.assertEqual( '{:}'.format(Season.SPRING), |
---|
420 | n/a | '{:}'.format(str(Season.SPRING))) |
---|
421 | n/a | self.assertEqual('{:20}'.format(Season.SPRING), |
---|
422 | n/a | '{:20}'.format(str(Season.SPRING))) |
---|
423 | n/a | self.assertEqual('{:^20}'.format(Season.SPRING), |
---|
424 | n/a | '{:^20}'.format(str(Season.SPRING))) |
---|
425 | n/a | self.assertEqual('{:>20}'.format(Season.SPRING), |
---|
426 | n/a | '{:>20}'.format(str(Season.SPRING))) |
---|
427 | n/a | self.assertEqual('{:<20}'.format(Season.SPRING), |
---|
428 | n/a | '{:<20}'.format(str(Season.SPRING))) |
---|
429 | n/a | |
---|
430 | n/a | def test_format_enum_custom(self): |
---|
431 | n/a | class TestFloat(float, Enum): |
---|
432 | n/a | one = 1.0 |
---|
433 | n/a | two = 2.0 |
---|
434 | n/a | def __format__(self, spec): |
---|
435 | n/a | return 'TestFloat success!' |
---|
436 | n/a | self.assertEqual('{}'.format(TestFloat.one), 'TestFloat success!') |
---|
437 | n/a | |
---|
438 | n/a | def assertFormatIsValue(self, spec, member): |
---|
439 | n/a | self.assertEqual(spec.format(member), spec.format(member.value)) |
---|
440 | n/a | |
---|
441 | n/a | def test_format_enum_date(self): |
---|
442 | n/a | Holiday = self.Holiday |
---|
443 | n/a | self.assertFormatIsValue('{}', Holiday.IDES_OF_MARCH) |
---|
444 | n/a | self.assertFormatIsValue('{:}', Holiday.IDES_OF_MARCH) |
---|
445 | n/a | self.assertFormatIsValue('{:20}', Holiday.IDES_OF_MARCH) |
---|
446 | n/a | self.assertFormatIsValue('{:^20}', Holiday.IDES_OF_MARCH) |
---|
447 | n/a | self.assertFormatIsValue('{:>20}', Holiday.IDES_OF_MARCH) |
---|
448 | n/a | self.assertFormatIsValue('{:<20}', Holiday.IDES_OF_MARCH) |
---|
449 | n/a | self.assertFormatIsValue('{:%Y %m}', Holiday.IDES_OF_MARCH) |
---|
450 | n/a | self.assertFormatIsValue('{:%Y %m %M:00}', Holiday.IDES_OF_MARCH) |
---|
451 | n/a | |
---|
452 | n/a | def test_format_enum_float(self): |
---|
453 | n/a | Konstants = self.Konstants |
---|
454 | n/a | self.assertFormatIsValue('{}', Konstants.TAU) |
---|
455 | n/a | self.assertFormatIsValue('{:}', Konstants.TAU) |
---|
456 | n/a | self.assertFormatIsValue('{:20}', Konstants.TAU) |
---|
457 | n/a | self.assertFormatIsValue('{:^20}', Konstants.TAU) |
---|
458 | n/a | self.assertFormatIsValue('{:>20}', Konstants.TAU) |
---|
459 | n/a | self.assertFormatIsValue('{:<20}', Konstants.TAU) |
---|
460 | n/a | self.assertFormatIsValue('{:n}', Konstants.TAU) |
---|
461 | n/a | self.assertFormatIsValue('{:5.2}', Konstants.TAU) |
---|
462 | n/a | self.assertFormatIsValue('{:f}', Konstants.TAU) |
---|
463 | n/a | |
---|
464 | n/a | def test_format_enum_int(self): |
---|
465 | n/a | Grades = self.Grades |
---|
466 | n/a | self.assertFormatIsValue('{}', Grades.C) |
---|
467 | n/a | self.assertFormatIsValue('{:}', Grades.C) |
---|
468 | n/a | self.assertFormatIsValue('{:20}', Grades.C) |
---|
469 | n/a | self.assertFormatIsValue('{:^20}', Grades.C) |
---|
470 | n/a | self.assertFormatIsValue('{:>20}', Grades.C) |
---|
471 | n/a | self.assertFormatIsValue('{:<20}', Grades.C) |
---|
472 | n/a | self.assertFormatIsValue('{:+}', Grades.C) |
---|
473 | n/a | self.assertFormatIsValue('{:08X}', Grades.C) |
---|
474 | n/a | self.assertFormatIsValue('{:b}', Grades.C) |
---|
475 | n/a | |
---|
476 | n/a | def test_format_enum_str(self): |
---|
477 | n/a | Directional = self.Directional |
---|
478 | n/a | self.assertFormatIsValue('{}', Directional.WEST) |
---|
479 | n/a | self.assertFormatIsValue('{:}', Directional.WEST) |
---|
480 | n/a | self.assertFormatIsValue('{:20}', Directional.WEST) |
---|
481 | n/a | self.assertFormatIsValue('{:^20}', Directional.WEST) |
---|
482 | n/a | self.assertFormatIsValue('{:>20}', Directional.WEST) |
---|
483 | n/a | self.assertFormatIsValue('{:<20}', Directional.WEST) |
---|
484 | n/a | |
---|
485 | n/a | def test_hash(self): |
---|
486 | n/a | Season = self.Season |
---|
487 | n/a | dates = {} |
---|
488 | n/a | dates[Season.WINTER] = '1225' |
---|
489 | n/a | dates[Season.SPRING] = '0315' |
---|
490 | n/a | dates[Season.SUMMER] = '0704' |
---|
491 | n/a | dates[Season.AUTUMN] = '1031' |
---|
492 | n/a | self.assertEqual(dates[Season.AUTUMN], '1031') |
---|
493 | n/a | |
---|
494 | n/a | def test_intenum_from_scratch(self): |
---|
495 | n/a | class phy(int, Enum): |
---|
496 | n/a | pi = 3 |
---|
497 | n/a | tau = 2 * pi |
---|
498 | n/a | self.assertTrue(phy.pi < phy.tau) |
---|
499 | n/a | |
---|
500 | n/a | def test_intenum_inherited(self): |
---|
501 | n/a | class IntEnum(int, Enum): |
---|
502 | n/a | pass |
---|
503 | n/a | class phy(IntEnum): |
---|
504 | n/a | pi = 3 |
---|
505 | n/a | tau = 2 * pi |
---|
506 | n/a | self.assertTrue(phy.pi < phy.tau) |
---|
507 | n/a | |
---|
508 | n/a | def test_floatenum_from_scratch(self): |
---|
509 | n/a | class phy(float, Enum): |
---|
510 | n/a | pi = 3.1415926 |
---|
511 | n/a | tau = 2 * pi |
---|
512 | n/a | self.assertTrue(phy.pi < phy.tau) |
---|
513 | n/a | |
---|
514 | n/a | def test_floatenum_inherited(self): |
---|
515 | n/a | class FloatEnum(float, Enum): |
---|
516 | n/a | pass |
---|
517 | n/a | class phy(FloatEnum): |
---|
518 | n/a | pi = 3.1415926 |
---|
519 | n/a | tau = 2 * pi |
---|
520 | n/a | self.assertTrue(phy.pi < phy.tau) |
---|
521 | n/a | |
---|
522 | n/a | def test_strenum_from_scratch(self): |
---|
523 | n/a | class phy(str, Enum): |
---|
524 | n/a | pi = 'Pi' |
---|
525 | n/a | tau = 'Tau' |
---|
526 | n/a | self.assertTrue(phy.pi < phy.tau) |
---|
527 | n/a | |
---|
528 | n/a | def test_strenum_inherited(self): |
---|
529 | n/a | class StrEnum(str, Enum): |
---|
530 | n/a | pass |
---|
531 | n/a | class phy(StrEnum): |
---|
532 | n/a | pi = 'Pi' |
---|
533 | n/a | tau = 'Tau' |
---|
534 | n/a | self.assertTrue(phy.pi < phy.tau) |
---|
535 | n/a | |
---|
536 | n/a | |
---|
537 | n/a | def test_intenum(self): |
---|
538 | n/a | class WeekDay(IntEnum): |
---|
539 | n/a | SUNDAY = 1 |
---|
540 | n/a | MONDAY = 2 |
---|
541 | n/a | TUESDAY = 3 |
---|
542 | n/a | WEDNESDAY = 4 |
---|
543 | n/a | THURSDAY = 5 |
---|
544 | n/a | FRIDAY = 6 |
---|
545 | n/a | SATURDAY = 7 |
---|
546 | n/a | |
---|
547 | n/a | self.assertEqual(['a', 'b', 'c'][WeekDay.MONDAY], 'c') |
---|
548 | n/a | self.assertEqual([i for i in range(WeekDay.TUESDAY)], [0, 1, 2]) |
---|
549 | n/a | |
---|
550 | n/a | lst = list(WeekDay) |
---|
551 | n/a | self.assertEqual(len(lst), len(WeekDay)) |
---|
552 | n/a | self.assertEqual(len(WeekDay), 7) |
---|
553 | n/a | target = 'SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY' |
---|
554 | n/a | target = target.split() |
---|
555 | n/a | for i, weekday in enumerate(target, 1): |
---|
556 | n/a | e = WeekDay(i) |
---|
557 | n/a | self.assertEqual(e, i) |
---|
558 | n/a | self.assertEqual(int(e), i) |
---|
559 | n/a | self.assertEqual(e.name, weekday) |
---|
560 | n/a | self.assertIn(e, WeekDay) |
---|
561 | n/a | self.assertEqual(lst.index(e)+1, i) |
---|
562 | n/a | self.assertTrue(0 < e < 8) |
---|
563 | n/a | self.assertIs(type(e), WeekDay) |
---|
564 | n/a | self.assertIsInstance(e, int) |
---|
565 | n/a | self.assertIsInstance(e, Enum) |
---|
566 | n/a | |
---|
567 | n/a | def test_intenum_duplicates(self): |
---|
568 | n/a | class WeekDay(IntEnum): |
---|
569 | n/a | SUNDAY = 1 |
---|
570 | n/a | MONDAY = 2 |
---|
571 | n/a | TUESDAY = TEUSDAY = 3 |
---|
572 | n/a | WEDNESDAY = 4 |
---|
573 | n/a | THURSDAY = 5 |
---|
574 | n/a | FRIDAY = 6 |
---|
575 | n/a | SATURDAY = 7 |
---|
576 | n/a | self.assertIs(WeekDay.TEUSDAY, WeekDay.TUESDAY) |
---|
577 | n/a | self.assertEqual(WeekDay(3).name, 'TUESDAY') |
---|
578 | n/a | self.assertEqual([k for k,v in WeekDay.__members__.items() |
---|
579 | n/a | if v.name != k], ['TEUSDAY', ]) |
---|
580 | n/a | |
---|
581 | n/a | def test_intenum_from_bytes(self): |
---|
582 | n/a | self.assertIs(IntStooges.from_bytes(b'\x00\x03', 'big'), IntStooges.MOE) |
---|
583 | n/a | with self.assertRaises(ValueError): |
---|
584 | n/a | IntStooges.from_bytes(b'\x00\x05', 'big') |
---|
585 | n/a | |
---|
586 | n/a | def test_floatenum_fromhex(self): |
---|
587 | n/a | h = float.hex(FloatStooges.MOE.value) |
---|
588 | n/a | self.assertIs(FloatStooges.fromhex(h), FloatStooges.MOE) |
---|
589 | n/a | h = float.hex(FloatStooges.MOE.value + 0.01) |
---|
590 | n/a | with self.assertRaises(ValueError): |
---|
591 | n/a | FloatStooges.fromhex(h) |
---|
592 | n/a | |
---|
593 | n/a | def test_pickle_enum(self): |
---|
594 | n/a | if isinstance(Stooges, Exception): |
---|
595 | n/a | raise Stooges |
---|
596 | n/a | test_pickle_dump_load(self.assertIs, Stooges.CURLY) |
---|
597 | n/a | test_pickle_dump_load(self.assertIs, Stooges) |
---|
598 | n/a | |
---|
599 | n/a | def test_pickle_int(self): |
---|
600 | n/a | if isinstance(IntStooges, Exception): |
---|
601 | n/a | raise IntStooges |
---|
602 | n/a | test_pickle_dump_load(self.assertIs, IntStooges.CURLY) |
---|
603 | n/a | test_pickle_dump_load(self.assertIs, IntStooges) |
---|
604 | n/a | |
---|
605 | n/a | def test_pickle_float(self): |
---|
606 | n/a | if isinstance(FloatStooges, Exception): |
---|
607 | n/a | raise FloatStooges |
---|
608 | n/a | test_pickle_dump_load(self.assertIs, FloatStooges.CURLY) |
---|
609 | n/a | test_pickle_dump_load(self.assertIs, FloatStooges) |
---|
610 | n/a | |
---|
611 | n/a | def test_pickle_enum_function(self): |
---|
612 | n/a | if isinstance(Answer, Exception): |
---|
613 | n/a | raise Answer |
---|
614 | n/a | test_pickle_dump_load(self.assertIs, Answer.him) |
---|
615 | n/a | test_pickle_dump_load(self.assertIs, Answer) |
---|
616 | n/a | |
---|
617 | n/a | def test_pickle_enum_function_with_module(self): |
---|
618 | n/a | if isinstance(Question, Exception): |
---|
619 | n/a | raise Question |
---|
620 | n/a | test_pickle_dump_load(self.assertIs, Question.who) |
---|
621 | n/a | test_pickle_dump_load(self.assertIs, Question) |
---|
622 | n/a | |
---|
623 | n/a | def test_enum_function_with_qualname(self): |
---|
624 | n/a | if isinstance(Theory, Exception): |
---|
625 | n/a | raise Theory |
---|
626 | n/a | self.assertEqual(Theory.__qualname__, 'spanish_inquisition') |
---|
627 | n/a | |
---|
628 | n/a | def test_class_nested_enum_and_pickle_protocol_four(self): |
---|
629 | n/a | # would normally just have this directly in the class namespace |
---|
630 | n/a | class NestedEnum(Enum): |
---|
631 | n/a | twigs = 'common' |
---|
632 | n/a | shiny = 'rare' |
---|
633 | n/a | |
---|
634 | n/a | self.__class__.NestedEnum = NestedEnum |
---|
635 | n/a | self.NestedEnum.__qualname__ = '%s.NestedEnum' % self.__class__.__name__ |
---|
636 | n/a | test_pickle_dump_load(self.assertIs, self.NestedEnum.twigs) |
---|
637 | n/a | |
---|
638 | n/a | def test_pickle_by_name(self): |
---|
639 | n/a | class ReplaceGlobalInt(IntEnum): |
---|
640 | n/a | ONE = 1 |
---|
641 | n/a | TWO = 2 |
---|
642 | n/a | ReplaceGlobalInt.__reduce_ex__ = enum._reduce_ex_by_name |
---|
643 | n/a | for proto in range(HIGHEST_PROTOCOL): |
---|
644 | n/a | self.assertEqual(ReplaceGlobalInt.TWO.__reduce_ex__(proto), 'TWO') |
---|
645 | n/a | |
---|
646 | n/a | def test_exploding_pickle(self): |
---|
647 | n/a | BadPickle = Enum( |
---|
648 | n/a | 'BadPickle', 'dill sweet bread-n-butter', module=__name__) |
---|
649 | n/a | globals()['BadPickle'] = BadPickle |
---|
650 | n/a | # now break BadPickle to test exception raising |
---|
651 | n/a | enum._make_class_unpicklable(BadPickle) |
---|
652 | n/a | test_pickle_exception(self.assertRaises, TypeError, BadPickle.dill) |
---|
653 | n/a | test_pickle_exception(self.assertRaises, PicklingError, BadPickle) |
---|
654 | n/a | |
---|
655 | n/a | def test_string_enum(self): |
---|
656 | n/a | class SkillLevel(str, Enum): |
---|
657 | n/a | master = 'what is the sound of one hand clapping?' |
---|
658 | n/a | journeyman = 'why did the chicken cross the road?' |
---|
659 | n/a | apprentice = 'knock, knock!' |
---|
660 | n/a | self.assertEqual(SkillLevel.apprentice, 'knock, knock!') |
---|
661 | n/a | |
---|
662 | n/a | def test_getattr_getitem(self): |
---|
663 | n/a | class Period(Enum): |
---|
664 | n/a | morning = 1 |
---|
665 | n/a | noon = 2 |
---|
666 | n/a | evening = 3 |
---|
667 | n/a | night = 4 |
---|
668 | n/a | self.assertIs(Period(2), Period.noon) |
---|
669 | n/a | self.assertIs(getattr(Period, 'night'), Period.night) |
---|
670 | n/a | self.assertIs(Period['morning'], Period.morning) |
---|
671 | n/a | |
---|
672 | n/a | def test_getattr_dunder(self): |
---|
673 | n/a | Season = self.Season |
---|
674 | n/a | self.assertTrue(getattr(Season, '__eq__')) |
---|
675 | n/a | |
---|
676 | n/a | def test_iteration_order(self): |
---|
677 | n/a | class Season(Enum): |
---|
678 | n/a | SUMMER = 2 |
---|
679 | n/a | WINTER = 4 |
---|
680 | n/a | AUTUMN = 3 |
---|
681 | n/a | SPRING = 1 |
---|
682 | n/a | self.assertEqual( |
---|
683 | n/a | list(Season), |
---|
684 | n/a | [Season.SUMMER, Season.WINTER, Season.AUTUMN, Season.SPRING], |
---|
685 | n/a | ) |
---|
686 | n/a | |
---|
687 | n/a | def test_reversed_iteration_order(self): |
---|
688 | n/a | self.assertEqual( |
---|
689 | n/a | list(reversed(self.Season)), |
---|
690 | n/a | [self.Season.WINTER, self.Season.AUTUMN, self.Season.SUMMER, |
---|
691 | n/a | self.Season.SPRING] |
---|
692 | n/a | ) |
---|
693 | n/a | |
---|
694 | n/a | def test_programmatic_function_string(self): |
---|
695 | n/a | SummerMonth = Enum('SummerMonth', 'june july august') |
---|
696 | n/a | lst = list(SummerMonth) |
---|
697 | n/a | self.assertEqual(len(lst), len(SummerMonth)) |
---|
698 | n/a | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
---|
699 | n/a | self.assertEqual( |
---|
700 | n/a | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
---|
701 | n/a | lst, |
---|
702 | n/a | ) |
---|
703 | n/a | for i, month in enumerate('june july august'.split(), 1): |
---|
704 | n/a | e = SummerMonth(i) |
---|
705 | n/a | self.assertEqual(int(e.value), i) |
---|
706 | n/a | self.assertNotEqual(e, i) |
---|
707 | n/a | self.assertEqual(e.name, month) |
---|
708 | n/a | self.assertIn(e, SummerMonth) |
---|
709 | n/a | self.assertIs(type(e), SummerMonth) |
---|
710 | n/a | |
---|
711 | n/a | def test_programmatic_function_string_with_start(self): |
---|
712 | n/a | SummerMonth = Enum('SummerMonth', 'june july august', start=10) |
---|
713 | n/a | lst = list(SummerMonth) |
---|
714 | n/a | self.assertEqual(len(lst), len(SummerMonth)) |
---|
715 | n/a | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
---|
716 | n/a | self.assertEqual( |
---|
717 | n/a | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
---|
718 | n/a | lst, |
---|
719 | n/a | ) |
---|
720 | n/a | for i, month in enumerate('june july august'.split(), 10): |
---|
721 | n/a | e = SummerMonth(i) |
---|
722 | n/a | self.assertEqual(int(e.value), i) |
---|
723 | n/a | self.assertNotEqual(e, i) |
---|
724 | n/a | self.assertEqual(e.name, month) |
---|
725 | n/a | self.assertIn(e, SummerMonth) |
---|
726 | n/a | self.assertIs(type(e), SummerMonth) |
---|
727 | n/a | |
---|
728 | n/a | def test_programmatic_function_string_list(self): |
---|
729 | n/a | SummerMonth = Enum('SummerMonth', ['june', 'july', 'august']) |
---|
730 | n/a | lst = list(SummerMonth) |
---|
731 | n/a | self.assertEqual(len(lst), len(SummerMonth)) |
---|
732 | n/a | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
---|
733 | n/a | self.assertEqual( |
---|
734 | n/a | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
---|
735 | n/a | lst, |
---|
736 | n/a | ) |
---|
737 | n/a | for i, month in enumerate('june july august'.split(), 1): |
---|
738 | n/a | e = SummerMonth(i) |
---|
739 | n/a | self.assertEqual(int(e.value), i) |
---|
740 | n/a | self.assertNotEqual(e, i) |
---|
741 | n/a | self.assertEqual(e.name, month) |
---|
742 | n/a | self.assertIn(e, SummerMonth) |
---|
743 | n/a | self.assertIs(type(e), SummerMonth) |
---|
744 | n/a | |
---|
745 | n/a | def test_programmatic_function_string_list_with_start(self): |
---|
746 | n/a | SummerMonth = Enum('SummerMonth', ['june', 'july', 'august'], start=20) |
---|
747 | n/a | lst = list(SummerMonth) |
---|
748 | n/a | self.assertEqual(len(lst), len(SummerMonth)) |
---|
749 | n/a | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
---|
750 | n/a | self.assertEqual( |
---|
751 | n/a | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
---|
752 | n/a | lst, |
---|
753 | n/a | ) |
---|
754 | n/a | for i, month in enumerate('june july august'.split(), 20): |
---|
755 | n/a | e = SummerMonth(i) |
---|
756 | n/a | self.assertEqual(int(e.value), i) |
---|
757 | n/a | self.assertNotEqual(e, i) |
---|
758 | n/a | self.assertEqual(e.name, month) |
---|
759 | n/a | self.assertIn(e, SummerMonth) |
---|
760 | n/a | self.assertIs(type(e), SummerMonth) |
---|
761 | n/a | |
---|
762 | n/a | def test_programmatic_function_iterable(self): |
---|
763 | n/a | SummerMonth = Enum( |
---|
764 | n/a | 'SummerMonth', |
---|
765 | n/a | (('june', 1), ('july', 2), ('august', 3)) |
---|
766 | n/a | ) |
---|
767 | n/a | lst = list(SummerMonth) |
---|
768 | n/a | self.assertEqual(len(lst), len(SummerMonth)) |
---|
769 | n/a | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
---|
770 | n/a | self.assertEqual( |
---|
771 | n/a | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
---|
772 | n/a | lst, |
---|
773 | n/a | ) |
---|
774 | n/a | for i, month in enumerate('june july august'.split(), 1): |
---|
775 | n/a | e = SummerMonth(i) |
---|
776 | n/a | self.assertEqual(int(e.value), i) |
---|
777 | n/a | self.assertNotEqual(e, i) |
---|
778 | n/a | self.assertEqual(e.name, month) |
---|
779 | n/a | self.assertIn(e, SummerMonth) |
---|
780 | n/a | self.assertIs(type(e), SummerMonth) |
---|
781 | n/a | |
---|
782 | n/a | def test_programmatic_function_from_dict(self): |
---|
783 | n/a | SummerMonth = Enum( |
---|
784 | n/a | 'SummerMonth', |
---|
785 | n/a | OrderedDict((('june', 1), ('july', 2), ('august', 3))) |
---|
786 | n/a | ) |
---|
787 | n/a | lst = list(SummerMonth) |
---|
788 | n/a | self.assertEqual(len(lst), len(SummerMonth)) |
---|
789 | n/a | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
---|
790 | n/a | self.assertEqual( |
---|
791 | n/a | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
---|
792 | n/a | lst, |
---|
793 | n/a | ) |
---|
794 | n/a | for i, month in enumerate('june july august'.split(), 1): |
---|
795 | n/a | e = SummerMonth(i) |
---|
796 | n/a | self.assertEqual(int(e.value), i) |
---|
797 | n/a | self.assertNotEqual(e, i) |
---|
798 | n/a | self.assertEqual(e.name, month) |
---|
799 | n/a | self.assertIn(e, SummerMonth) |
---|
800 | n/a | self.assertIs(type(e), SummerMonth) |
---|
801 | n/a | |
---|
802 | n/a | def test_programmatic_function_type(self): |
---|
803 | n/a | SummerMonth = Enum('SummerMonth', 'june july august', type=int) |
---|
804 | n/a | lst = list(SummerMonth) |
---|
805 | n/a | self.assertEqual(len(lst), len(SummerMonth)) |
---|
806 | n/a | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
---|
807 | n/a | self.assertEqual( |
---|
808 | n/a | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
---|
809 | n/a | lst, |
---|
810 | n/a | ) |
---|
811 | n/a | for i, month in enumerate('june july august'.split(), 1): |
---|
812 | n/a | e = SummerMonth(i) |
---|
813 | n/a | self.assertEqual(e, i) |
---|
814 | n/a | self.assertEqual(e.name, month) |
---|
815 | n/a | self.assertIn(e, SummerMonth) |
---|
816 | n/a | self.assertIs(type(e), SummerMonth) |
---|
817 | n/a | |
---|
818 | n/a | def test_programmatic_function_type_with_start(self): |
---|
819 | n/a | SummerMonth = Enum('SummerMonth', 'june july august', type=int, start=30) |
---|
820 | n/a | lst = list(SummerMonth) |
---|
821 | n/a | self.assertEqual(len(lst), len(SummerMonth)) |
---|
822 | n/a | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
---|
823 | n/a | self.assertEqual( |
---|
824 | n/a | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
---|
825 | n/a | lst, |
---|
826 | n/a | ) |
---|
827 | n/a | for i, month in enumerate('june july august'.split(), 30): |
---|
828 | n/a | e = SummerMonth(i) |
---|
829 | n/a | self.assertEqual(e, i) |
---|
830 | n/a | self.assertEqual(e.name, month) |
---|
831 | n/a | self.assertIn(e, SummerMonth) |
---|
832 | n/a | self.assertIs(type(e), SummerMonth) |
---|
833 | n/a | |
---|
834 | n/a | def test_programmatic_function_type_from_subclass(self): |
---|
835 | n/a | SummerMonth = IntEnum('SummerMonth', 'june july august') |
---|
836 | n/a | lst = list(SummerMonth) |
---|
837 | n/a | self.assertEqual(len(lst), len(SummerMonth)) |
---|
838 | n/a | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
---|
839 | n/a | self.assertEqual( |
---|
840 | n/a | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
---|
841 | n/a | lst, |
---|
842 | n/a | ) |
---|
843 | n/a | for i, month in enumerate('june july august'.split(), 1): |
---|
844 | n/a | e = SummerMonth(i) |
---|
845 | n/a | self.assertEqual(e, i) |
---|
846 | n/a | self.assertEqual(e.name, month) |
---|
847 | n/a | self.assertIn(e, SummerMonth) |
---|
848 | n/a | self.assertIs(type(e), SummerMonth) |
---|
849 | n/a | |
---|
850 | n/a | def test_programmatic_function_type_from_subclass_with_start(self): |
---|
851 | n/a | SummerMonth = IntEnum('SummerMonth', 'june july august', start=40) |
---|
852 | n/a | lst = list(SummerMonth) |
---|
853 | n/a | self.assertEqual(len(lst), len(SummerMonth)) |
---|
854 | n/a | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
---|
855 | n/a | self.assertEqual( |
---|
856 | n/a | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
---|
857 | n/a | lst, |
---|
858 | n/a | ) |
---|
859 | n/a | for i, month in enumerate('june july august'.split(), 40): |
---|
860 | n/a | e = SummerMonth(i) |
---|
861 | n/a | self.assertEqual(e, i) |
---|
862 | n/a | self.assertEqual(e.name, month) |
---|
863 | n/a | self.assertIn(e, SummerMonth) |
---|
864 | n/a | self.assertIs(type(e), SummerMonth) |
---|
865 | n/a | |
---|
866 | n/a | def test_subclassing(self): |
---|
867 | n/a | if isinstance(Name, Exception): |
---|
868 | n/a | raise Name |
---|
869 | n/a | self.assertEqual(Name.BDFL, 'Guido van Rossum') |
---|
870 | n/a | self.assertTrue(Name.BDFL, Name('Guido van Rossum')) |
---|
871 | n/a | self.assertIs(Name.BDFL, getattr(Name, 'BDFL')) |
---|
872 | n/a | test_pickle_dump_load(self.assertIs, Name.BDFL) |
---|
873 | n/a | |
---|
874 | n/a | def test_extending(self): |
---|
875 | n/a | class Color(Enum): |
---|
876 | n/a | red = 1 |
---|
877 | n/a | green = 2 |
---|
878 | n/a | blue = 3 |
---|
879 | n/a | with self.assertRaises(TypeError): |
---|
880 | n/a | class MoreColor(Color): |
---|
881 | n/a | cyan = 4 |
---|
882 | n/a | magenta = 5 |
---|
883 | n/a | yellow = 6 |
---|
884 | n/a | |
---|
885 | n/a | def test_exclude_methods(self): |
---|
886 | n/a | class whatever(Enum): |
---|
887 | n/a | this = 'that' |
---|
888 | n/a | these = 'those' |
---|
889 | n/a | def really(self): |
---|
890 | n/a | return 'no, not %s' % self.value |
---|
891 | n/a | self.assertIsNot(type(whatever.really), whatever) |
---|
892 | n/a | self.assertEqual(whatever.this.really(), 'no, not that') |
---|
893 | n/a | |
---|
894 | n/a | def test_wrong_inheritance_order(self): |
---|
895 | n/a | with self.assertRaises(TypeError): |
---|
896 | n/a | class Wrong(Enum, str): |
---|
897 | n/a | NotHere = 'error before this point' |
---|
898 | n/a | |
---|
899 | n/a | def test_intenum_transitivity(self): |
---|
900 | n/a | class number(IntEnum): |
---|
901 | n/a | one = 1 |
---|
902 | n/a | two = 2 |
---|
903 | n/a | three = 3 |
---|
904 | n/a | class numero(IntEnum): |
---|
905 | n/a | uno = 1 |
---|
906 | n/a | dos = 2 |
---|
907 | n/a | tres = 3 |
---|
908 | n/a | self.assertEqual(number.one, numero.uno) |
---|
909 | n/a | self.assertEqual(number.two, numero.dos) |
---|
910 | n/a | self.assertEqual(number.three, numero.tres) |
---|
911 | n/a | |
---|
912 | n/a | def test_wrong_enum_in_call(self): |
---|
913 | n/a | class Monochrome(Enum): |
---|
914 | n/a | black = 0 |
---|
915 | n/a | white = 1 |
---|
916 | n/a | class Gender(Enum): |
---|
917 | n/a | male = 0 |
---|
918 | n/a | female = 1 |
---|
919 | n/a | self.assertRaises(ValueError, Monochrome, Gender.male) |
---|
920 | n/a | |
---|
921 | n/a | def test_wrong_enum_in_mixed_call(self): |
---|
922 | n/a | class Monochrome(IntEnum): |
---|
923 | n/a | black = 0 |
---|
924 | n/a | white = 1 |
---|
925 | n/a | class Gender(Enum): |
---|
926 | n/a | male = 0 |
---|
927 | n/a | female = 1 |
---|
928 | n/a | self.assertRaises(ValueError, Monochrome, Gender.male) |
---|
929 | n/a | |
---|
930 | n/a | def test_mixed_enum_in_call_1(self): |
---|
931 | n/a | class Monochrome(IntEnum): |
---|
932 | n/a | black = 0 |
---|
933 | n/a | white = 1 |
---|
934 | n/a | class Gender(IntEnum): |
---|
935 | n/a | male = 0 |
---|
936 | n/a | female = 1 |
---|
937 | n/a | self.assertIs(Monochrome(Gender.female), Monochrome.white) |
---|
938 | n/a | |
---|
939 | n/a | def test_mixed_enum_in_call_2(self): |
---|
940 | n/a | class Monochrome(Enum): |
---|
941 | n/a | black = 0 |
---|
942 | n/a | white = 1 |
---|
943 | n/a | class Gender(IntEnum): |
---|
944 | n/a | male = 0 |
---|
945 | n/a | female = 1 |
---|
946 | n/a | self.assertIs(Monochrome(Gender.male), Monochrome.black) |
---|
947 | n/a | |
---|
948 | n/a | def test_flufl_enum(self): |
---|
949 | n/a | class Fluflnum(Enum): |
---|
950 | n/a | def __int__(self): |
---|
951 | n/a | return int(self.value) |
---|
952 | n/a | class MailManOptions(Fluflnum): |
---|
953 | n/a | option1 = 1 |
---|
954 | n/a | option2 = 2 |
---|
955 | n/a | option3 = 3 |
---|
956 | n/a | self.assertEqual(int(MailManOptions.option1), 1) |
---|
957 | n/a | |
---|
958 | n/a | def test_introspection(self): |
---|
959 | n/a | class Number(IntEnum): |
---|
960 | n/a | one = 100 |
---|
961 | n/a | two = 200 |
---|
962 | n/a | self.assertIs(Number.one._member_type_, int) |
---|
963 | n/a | self.assertIs(Number._member_type_, int) |
---|
964 | n/a | class String(str, Enum): |
---|
965 | n/a | yarn = 'soft' |
---|
966 | n/a | rope = 'rough' |
---|
967 | n/a | wire = 'hard' |
---|
968 | n/a | self.assertIs(String.yarn._member_type_, str) |
---|
969 | n/a | self.assertIs(String._member_type_, str) |
---|
970 | n/a | class Plain(Enum): |
---|
971 | n/a | vanilla = 'white' |
---|
972 | n/a | one = 1 |
---|
973 | n/a | self.assertIs(Plain.vanilla._member_type_, object) |
---|
974 | n/a | self.assertIs(Plain._member_type_, object) |
---|
975 | n/a | |
---|
976 | n/a | def test_no_such_enum_member(self): |
---|
977 | n/a | class Color(Enum): |
---|
978 | n/a | red = 1 |
---|
979 | n/a | green = 2 |
---|
980 | n/a | blue = 3 |
---|
981 | n/a | with self.assertRaises(ValueError): |
---|
982 | n/a | Color(4) |
---|
983 | n/a | with self.assertRaises(KeyError): |
---|
984 | n/a | Color['chartreuse'] |
---|
985 | n/a | |
---|
986 | n/a | def test_new_repr(self): |
---|
987 | n/a | class Color(Enum): |
---|
988 | n/a | red = 1 |
---|
989 | n/a | green = 2 |
---|
990 | n/a | blue = 3 |
---|
991 | n/a | def __repr__(self): |
---|
992 | n/a | return "don't you just love shades of %s?" % self.name |
---|
993 | n/a | self.assertEqual( |
---|
994 | n/a | repr(Color.blue), |
---|
995 | n/a | "don't you just love shades of blue?", |
---|
996 | n/a | ) |
---|
997 | n/a | |
---|
998 | n/a | def test_inherited_repr(self): |
---|
999 | n/a | class MyEnum(Enum): |
---|
1000 | n/a | def __repr__(self): |
---|
1001 | n/a | return "My name is %s." % self.name |
---|
1002 | n/a | class MyIntEnum(int, MyEnum): |
---|
1003 | n/a | this = 1 |
---|
1004 | n/a | that = 2 |
---|
1005 | n/a | theother = 3 |
---|
1006 | n/a | self.assertEqual(repr(MyIntEnum.that), "My name is that.") |
---|
1007 | n/a | |
---|
1008 | n/a | def test_multiple_mixin_mro(self): |
---|
1009 | n/a | class auto_enum(type(Enum)): |
---|
1010 | n/a | def __new__(metacls, cls, bases, classdict): |
---|
1011 | n/a | temp = type(classdict)() |
---|
1012 | n/a | names = set(classdict._member_names) |
---|
1013 | n/a | i = 0 |
---|
1014 | n/a | for k in classdict._member_names: |
---|
1015 | n/a | v = classdict[k] |
---|
1016 | n/a | if v is Ellipsis: |
---|
1017 | n/a | v = i |
---|
1018 | n/a | else: |
---|
1019 | n/a | i = v |
---|
1020 | n/a | i += 1 |
---|
1021 | n/a | temp[k] = v |
---|
1022 | n/a | for k, v in classdict.items(): |
---|
1023 | n/a | if k not in names: |
---|
1024 | n/a | temp[k] = v |
---|
1025 | n/a | return super(auto_enum, metacls).__new__( |
---|
1026 | n/a | metacls, cls, bases, temp) |
---|
1027 | n/a | |
---|
1028 | n/a | class AutoNumberedEnum(Enum, metaclass=auto_enum): |
---|
1029 | n/a | pass |
---|
1030 | n/a | |
---|
1031 | n/a | class AutoIntEnum(IntEnum, metaclass=auto_enum): |
---|
1032 | n/a | pass |
---|
1033 | n/a | |
---|
1034 | n/a | class TestAutoNumber(AutoNumberedEnum): |
---|
1035 | n/a | a = ... |
---|
1036 | n/a | b = 3 |
---|
1037 | n/a | c = ... |
---|
1038 | n/a | |
---|
1039 | n/a | class TestAutoInt(AutoIntEnum): |
---|
1040 | n/a | a = ... |
---|
1041 | n/a | b = 3 |
---|
1042 | n/a | c = ... |
---|
1043 | n/a | |
---|
1044 | n/a | def test_subclasses_with_getnewargs(self): |
---|
1045 | n/a | class NamedInt(int): |
---|
1046 | n/a | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
---|
1047 | n/a | def __new__(cls, *args): |
---|
1048 | n/a | _args = args |
---|
1049 | n/a | name, *args = args |
---|
1050 | n/a | if len(args) == 0: |
---|
1051 | n/a | raise TypeError("name and value must be specified") |
---|
1052 | n/a | self = int.__new__(cls, *args) |
---|
1053 | n/a | self._intname = name |
---|
1054 | n/a | self._args = _args |
---|
1055 | n/a | return self |
---|
1056 | n/a | def __getnewargs__(self): |
---|
1057 | n/a | return self._args |
---|
1058 | n/a | @property |
---|
1059 | n/a | def __name__(self): |
---|
1060 | n/a | return self._intname |
---|
1061 | n/a | def __repr__(self): |
---|
1062 | n/a | # repr() is updated to include the name and type info |
---|
1063 | n/a | return "{}({!r}, {})".format(type(self).__name__, |
---|
1064 | n/a | self.__name__, |
---|
1065 | n/a | int.__repr__(self)) |
---|
1066 | n/a | def __str__(self): |
---|
1067 | n/a | # str() is unchanged, even if it relies on the repr() fallback |
---|
1068 | n/a | base = int |
---|
1069 | n/a | base_str = base.__str__ |
---|
1070 | n/a | if base_str.__objclass__ is object: |
---|
1071 | n/a | return base.__repr__(self) |
---|
1072 | n/a | return base_str(self) |
---|
1073 | n/a | # for simplicity, we only define one operator that |
---|
1074 | n/a | # propagates expressions |
---|
1075 | n/a | def __add__(self, other): |
---|
1076 | n/a | temp = int(self) + int( other) |
---|
1077 | n/a | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
---|
1078 | n/a | return NamedInt( |
---|
1079 | n/a | '({0} + {1})'.format(self.__name__, other.__name__), |
---|
1080 | n/a | temp ) |
---|
1081 | n/a | else: |
---|
1082 | n/a | return temp |
---|
1083 | n/a | |
---|
1084 | n/a | class NEI(NamedInt, Enum): |
---|
1085 | n/a | __qualname__ = 'NEI' # needed for pickle protocol 4 |
---|
1086 | n/a | x = ('the-x', 1) |
---|
1087 | n/a | y = ('the-y', 2) |
---|
1088 | n/a | |
---|
1089 | n/a | |
---|
1090 | n/a | self.assertIs(NEI.__new__, Enum.__new__) |
---|
1091 | n/a | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
---|
1092 | n/a | globals()['NamedInt'] = NamedInt |
---|
1093 | n/a | globals()['NEI'] = NEI |
---|
1094 | n/a | NI5 = NamedInt('test', 5) |
---|
1095 | n/a | self.assertEqual(NI5, 5) |
---|
1096 | n/a | test_pickle_dump_load(self.assertEqual, NI5, 5) |
---|
1097 | n/a | self.assertEqual(NEI.y.value, 2) |
---|
1098 | n/a | test_pickle_dump_load(self.assertIs, NEI.y) |
---|
1099 | n/a | test_pickle_dump_load(self.assertIs, NEI) |
---|
1100 | n/a | |
---|
1101 | n/a | def test_subclasses_with_getnewargs_ex(self): |
---|
1102 | n/a | class NamedInt(int): |
---|
1103 | n/a | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
---|
1104 | n/a | def __new__(cls, *args): |
---|
1105 | n/a | _args = args |
---|
1106 | n/a | name, *args = args |
---|
1107 | n/a | if len(args) == 0: |
---|
1108 | n/a | raise TypeError("name and value must be specified") |
---|
1109 | n/a | self = int.__new__(cls, *args) |
---|
1110 | n/a | self._intname = name |
---|
1111 | n/a | self._args = _args |
---|
1112 | n/a | return self |
---|
1113 | n/a | def __getnewargs_ex__(self): |
---|
1114 | n/a | return self._args, {} |
---|
1115 | n/a | @property |
---|
1116 | n/a | def __name__(self): |
---|
1117 | n/a | return self._intname |
---|
1118 | n/a | def __repr__(self): |
---|
1119 | n/a | # repr() is updated to include the name and type info |
---|
1120 | n/a | return "{}({!r}, {})".format(type(self).__name__, |
---|
1121 | n/a | self.__name__, |
---|
1122 | n/a | int.__repr__(self)) |
---|
1123 | n/a | def __str__(self): |
---|
1124 | n/a | # str() is unchanged, even if it relies on the repr() fallback |
---|
1125 | n/a | base = int |
---|
1126 | n/a | base_str = base.__str__ |
---|
1127 | n/a | if base_str.__objclass__ is object: |
---|
1128 | n/a | return base.__repr__(self) |
---|
1129 | n/a | return base_str(self) |
---|
1130 | n/a | # for simplicity, we only define one operator that |
---|
1131 | n/a | # propagates expressions |
---|
1132 | n/a | def __add__(self, other): |
---|
1133 | n/a | temp = int(self) + int( other) |
---|
1134 | n/a | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
---|
1135 | n/a | return NamedInt( |
---|
1136 | n/a | '({0} + {1})'.format(self.__name__, other.__name__), |
---|
1137 | n/a | temp ) |
---|
1138 | n/a | else: |
---|
1139 | n/a | return temp |
---|
1140 | n/a | |
---|
1141 | n/a | class NEI(NamedInt, Enum): |
---|
1142 | n/a | __qualname__ = 'NEI' # needed for pickle protocol 4 |
---|
1143 | n/a | x = ('the-x', 1) |
---|
1144 | n/a | y = ('the-y', 2) |
---|
1145 | n/a | |
---|
1146 | n/a | |
---|
1147 | n/a | self.assertIs(NEI.__new__, Enum.__new__) |
---|
1148 | n/a | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
---|
1149 | n/a | globals()['NamedInt'] = NamedInt |
---|
1150 | n/a | globals()['NEI'] = NEI |
---|
1151 | n/a | NI5 = NamedInt('test', 5) |
---|
1152 | n/a | self.assertEqual(NI5, 5) |
---|
1153 | n/a | test_pickle_dump_load(self.assertEqual, NI5, 5) |
---|
1154 | n/a | self.assertEqual(NEI.y.value, 2) |
---|
1155 | n/a | test_pickle_dump_load(self.assertIs, NEI.y) |
---|
1156 | n/a | test_pickle_dump_load(self.assertIs, NEI) |
---|
1157 | n/a | |
---|
1158 | n/a | def test_subclasses_with_reduce(self): |
---|
1159 | n/a | class NamedInt(int): |
---|
1160 | n/a | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
---|
1161 | n/a | def __new__(cls, *args): |
---|
1162 | n/a | _args = args |
---|
1163 | n/a | name, *args = args |
---|
1164 | n/a | if len(args) == 0: |
---|
1165 | n/a | raise TypeError("name and value must be specified") |
---|
1166 | n/a | self = int.__new__(cls, *args) |
---|
1167 | n/a | self._intname = name |
---|
1168 | n/a | self._args = _args |
---|
1169 | n/a | return self |
---|
1170 | n/a | def __reduce__(self): |
---|
1171 | n/a | return self.__class__, self._args |
---|
1172 | n/a | @property |
---|
1173 | n/a | def __name__(self): |
---|
1174 | n/a | return self._intname |
---|
1175 | n/a | def __repr__(self): |
---|
1176 | n/a | # repr() is updated to include the name and type info |
---|
1177 | n/a | return "{}({!r}, {})".format(type(self).__name__, |
---|
1178 | n/a | self.__name__, |
---|
1179 | n/a | int.__repr__(self)) |
---|
1180 | n/a | def __str__(self): |
---|
1181 | n/a | # str() is unchanged, even if it relies on the repr() fallback |
---|
1182 | n/a | base = int |
---|
1183 | n/a | base_str = base.__str__ |
---|
1184 | n/a | if base_str.__objclass__ is object: |
---|
1185 | n/a | return base.__repr__(self) |
---|
1186 | n/a | return base_str(self) |
---|
1187 | n/a | # for simplicity, we only define one operator that |
---|
1188 | n/a | # propagates expressions |
---|
1189 | n/a | def __add__(self, other): |
---|
1190 | n/a | temp = int(self) + int( other) |
---|
1191 | n/a | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
---|
1192 | n/a | return NamedInt( |
---|
1193 | n/a | '({0} + {1})'.format(self.__name__, other.__name__), |
---|
1194 | n/a | temp ) |
---|
1195 | n/a | else: |
---|
1196 | n/a | return temp |
---|
1197 | n/a | |
---|
1198 | n/a | class NEI(NamedInt, Enum): |
---|
1199 | n/a | __qualname__ = 'NEI' # needed for pickle protocol 4 |
---|
1200 | n/a | x = ('the-x', 1) |
---|
1201 | n/a | y = ('the-y', 2) |
---|
1202 | n/a | |
---|
1203 | n/a | |
---|
1204 | n/a | self.assertIs(NEI.__new__, Enum.__new__) |
---|
1205 | n/a | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
---|
1206 | n/a | globals()['NamedInt'] = NamedInt |
---|
1207 | n/a | globals()['NEI'] = NEI |
---|
1208 | n/a | NI5 = NamedInt('test', 5) |
---|
1209 | n/a | self.assertEqual(NI5, 5) |
---|
1210 | n/a | test_pickle_dump_load(self.assertEqual, NI5, 5) |
---|
1211 | n/a | self.assertEqual(NEI.y.value, 2) |
---|
1212 | n/a | test_pickle_dump_load(self.assertIs, NEI.y) |
---|
1213 | n/a | test_pickle_dump_load(self.assertIs, NEI) |
---|
1214 | n/a | |
---|
1215 | n/a | def test_subclasses_with_reduce_ex(self): |
---|
1216 | n/a | class NamedInt(int): |
---|
1217 | n/a | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
---|
1218 | n/a | def __new__(cls, *args): |
---|
1219 | n/a | _args = args |
---|
1220 | n/a | name, *args = args |
---|
1221 | n/a | if len(args) == 0: |
---|
1222 | n/a | raise TypeError("name and value must be specified") |
---|
1223 | n/a | self = int.__new__(cls, *args) |
---|
1224 | n/a | self._intname = name |
---|
1225 | n/a | self._args = _args |
---|
1226 | n/a | return self |
---|
1227 | n/a | def __reduce_ex__(self, proto): |
---|
1228 | n/a | return self.__class__, self._args |
---|
1229 | n/a | @property |
---|
1230 | n/a | def __name__(self): |
---|
1231 | n/a | return self._intname |
---|
1232 | n/a | def __repr__(self): |
---|
1233 | n/a | # repr() is updated to include the name and type info |
---|
1234 | n/a | return "{}({!r}, {})".format(type(self).__name__, |
---|
1235 | n/a | self.__name__, |
---|
1236 | n/a | int.__repr__(self)) |
---|
1237 | n/a | def __str__(self): |
---|
1238 | n/a | # str() is unchanged, even if it relies on the repr() fallback |
---|
1239 | n/a | base = int |
---|
1240 | n/a | base_str = base.__str__ |
---|
1241 | n/a | if base_str.__objclass__ is object: |
---|
1242 | n/a | return base.__repr__(self) |
---|
1243 | n/a | return base_str(self) |
---|
1244 | n/a | # for simplicity, we only define one operator that |
---|
1245 | n/a | # propagates expressions |
---|
1246 | n/a | def __add__(self, other): |
---|
1247 | n/a | temp = int(self) + int( other) |
---|
1248 | n/a | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
---|
1249 | n/a | return NamedInt( |
---|
1250 | n/a | '({0} + {1})'.format(self.__name__, other.__name__), |
---|
1251 | n/a | temp ) |
---|
1252 | n/a | else: |
---|
1253 | n/a | return temp |
---|
1254 | n/a | |
---|
1255 | n/a | class NEI(NamedInt, Enum): |
---|
1256 | n/a | __qualname__ = 'NEI' # needed for pickle protocol 4 |
---|
1257 | n/a | x = ('the-x', 1) |
---|
1258 | n/a | y = ('the-y', 2) |
---|
1259 | n/a | |
---|
1260 | n/a | |
---|
1261 | n/a | self.assertIs(NEI.__new__, Enum.__new__) |
---|
1262 | n/a | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
---|
1263 | n/a | globals()['NamedInt'] = NamedInt |
---|
1264 | n/a | globals()['NEI'] = NEI |
---|
1265 | n/a | NI5 = NamedInt('test', 5) |
---|
1266 | n/a | self.assertEqual(NI5, 5) |
---|
1267 | n/a | test_pickle_dump_load(self.assertEqual, NI5, 5) |
---|
1268 | n/a | self.assertEqual(NEI.y.value, 2) |
---|
1269 | n/a | test_pickle_dump_load(self.assertIs, NEI.y) |
---|
1270 | n/a | test_pickle_dump_load(self.assertIs, NEI) |
---|
1271 | n/a | |
---|
1272 | n/a | def test_subclasses_without_direct_pickle_support(self): |
---|
1273 | n/a | class NamedInt(int): |
---|
1274 | n/a | __qualname__ = 'NamedInt' |
---|
1275 | n/a | def __new__(cls, *args): |
---|
1276 | n/a | _args = args |
---|
1277 | n/a | name, *args = args |
---|
1278 | n/a | if len(args) == 0: |
---|
1279 | n/a | raise TypeError("name and value must be specified") |
---|
1280 | n/a | self = int.__new__(cls, *args) |
---|
1281 | n/a | self._intname = name |
---|
1282 | n/a | self._args = _args |
---|
1283 | n/a | return self |
---|
1284 | n/a | @property |
---|
1285 | n/a | def __name__(self): |
---|
1286 | n/a | return self._intname |
---|
1287 | n/a | def __repr__(self): |
---|
1288 | n/a | # repr() is updated to include the name and type info |
---|
1289 | n/a | return "{}({!r}, {})".format(type(self).__name__, |
---|
1290 | n/a | self.__name__, |
---|
1291 | n/a | int.__repr__(self)) |
---|
1292 | n/a | def __str__(self): |
---|
1293 | n/a | # str() is unchanged, even if it relies on the repr() fallback |
---|
1294 | n/a | base = int |
---|
1295 | n/a | base_str = base.__str__ |
---|
1296 | n/a | if base_str.__objclass__ is object: |
---|
1297 | n/a | return base.__repr__(self) |
---|
1298 | n/a | return base_str(self) |
---|
1299 | n/a | # for simplicity, we only define one operator that |
---|
1300 | n/a | # propagates expressions |
---|
1301 | n/a | def __add__(self, other): |
---|
1302 | n/a | temp = int(self) + int( other) |
---|
1303 | n/a | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
---|
1304 | n/a | return NamedInt( |
---|
1305 | n/a | '({0} + {1})'.format(self.__name__, other.__name__), |
---|
1306 | n/a | temp ) |
---|
1307 | n/a | else: |
---|
1308 | n/a | return temp |
---|
1309 | n/a | |
---|
1310 | n/a | class NEI(NamedInt, Enum): |
---|
1311 | n/a | __qualname__ = 'NEI' |
---|
1312 | n/a | x = ('the-x', 1) |
---|
1313 | n/a | y = ('the-y', 2) |
---|
1314 | n/a | |
---|
1315 | n/a | self.assertIs(NEI.__new__, Enum.__new__) |
---|
1316 | n/a | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
---|
1317 | n/a | globals()['NamedInt'] = NamedInt |
---|
1318 | n/a | globals()['NEI'] = NEI |
---|
1319 | n/a | NI5 = NamedInt('test', 5) |
---|
1320 | n/a | self.assertEqual(NI5, 5) |
---|
1321 | n/a | self.assertEqual(NEI.y.value, 2) |
---|
1322 | n/a | test_pickle_exception(self.assertRaises, TypeError, NEI.x) |
---|
1323 | n/a | test_pickle_exception(self.assertRaises, PicklingError, NEI) |
---|
1324 | n/a | |
---|
1325 | n/a | def test_subclasses_without_direct_pickle_support_using_name(self): |
---|
1326 | n/a | class NamedInt(int): |
---|
1327 | n/a | __qualname__ = 'NamedInt' |
---|
1328 | n/a | def __new__(cls, *args): |
---|
1329 | n/a | _args = args |
---|
1330 | n/a | name, *args = args |
---|
1331 | n/a | if len(args) == 0: |
---|
1332 | n/a | raise TypeError("name and value must be specified") |
---|
1333 | n/a | self = int.__new__(cls, *args) |
---|
1334 | n/a | self._intname = name |
---|
1335 | n/a | self._args = _args |
---|
1336 | n/a | return self |
---|
1337 | n/a | @property |
---|
1338 | n/a | def __name__(self): |
---|
1339 | n/a | return self._intname |
---|
1340 | n/a | def __repr__(self): |
---|
1341 | n/a | # repr() is updated to include the name and type info |
---|
1342 | n/a | return "{}({!r}, {})".format(type(self).__name__, |
---|
1343 | n/a | self.__name__, |
---|
1344 | n/a | int.__repr__(self)) |
---|
1345 | n/a | def __str__(self): |
---|
1346 | n/a | # str() is unchanged, even if it relies on the repr() fallback |
---|
1347 | n/a | base = int |
---|
1348 | n/a | base_str = base.__str__ |
---|
1349 | n/a | if base_str.__objclass__ is object: |
---|
1350 | n/a | return base.__repr__(self) |
---|
1351 | n/a | return base_str(self) |
---|
1352 | n/a | # for simplicity, we only define one operator that |
---|
1353 | n/a | # propagates expressions |
---|
1354 | n/a | def __add__(self, other): |
---|
1355 | n/a | temp = int(self) + int( other) |
---|
1356 | n/a | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
---|
1357 | n/a | return NamedInt( |
---|
1358 | n/a | '({0} + {1})'.format(self.__name__, other.__name__), |
---|
1359 | n/a | temp ) |
---|
1360 | n/a | else: |
---|
1361 | n/a | return temp |
---|
1362 | n/a | |
---|
1363 | n/a | class NEI(NamedInt, Enum): |
---|
1364 | n/a | __qualname__ = 'NEI' |
---|
1365 | n/a | x = ('the-x', 1) |
---|
1366 | n/a | y = ('the-y', 2) |
---|
1367 | n/a | def __reduce_ex__(self, proto): |
---|
1368 | n/a | return getattr, (self.__class__, self._name_) |
---|
1369 | n/a | |
---|
1370 | n/a | self.assertIs(NEI.__new__, Enum.__new__) |
---|
1371 | n/a | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
---|
1372 | n/a | globals()['NamedInt'] = NamedInt |
---|
1373 | n/a | globals()['NEI'] = NEI |
---|
1374 | n/a | NI5 = NamedInt('test', 5) |
---|
1375 | n/a | self.assertEqual(NI5, 5) |
---|
1376 | n/a | self.assertEqual(NEI.y.value, 2) |
---|
1377 | n/a | test_pickle_dump_load(self.assertIs, NEI.y) |
---|
1378 | n/a | test_pickle_dump_load(self.assertIs, NEI) |
---|
1379 | n/a | |
---|
1380 | n/a | def test_tuple_subclass(self): |
---|
1381 | n/a | class SomeTuple(tuple, Enum): |
---|
1382 | n/a | __qualname__ = 'SomeTuple' # needed for pickle protocol 4 |
---|
1383 | n/a | first = (1, 'for the money') |
---|
1384 | n/a | second = (2, 'for the show') |
---|
1385 | n/a | third = (3, 'for the music') |
---|
1386 | n/a | self.assertIs(type(SomeTuple.first), SomeTuple) |
---|
1387 | n/a | self.assertIsInstance(SomeTuple.second, tuple) |
---|
1388 | n/a | self.assertEqual(SomeTuple.third, (3, 'for the music')) |
---|
1389 | n/a | globals()['SomeTuple'] = SomeTuple |
---|
1390 | n/a | test_pickle_dump_load(self.assertIs, SomeTuple.first) |
---|
1391 | n/a | |
---|
1392 | n/a | def test_duplicate_values_give_unique_enum_items(self): |
---|
1393 | n/a | class AutoNumber(Enum): |
---|
1394 | n/a | first = () |
---|
1395 | n/a | second = () |
---|
1396 | n/a | third = () |
---|
1397 | n/a | def __new__(cls): |
---|
1398 | n/a | value = len(cls.__members__) + 1 |
---|
1399 | n/a | obj = object.__new__(cls) |
---|
1400 | n/a | obj._value_ = value |
---|
1401 | n/a | return obj |
---|
1402 | n/a | def __int__(self): |
---|
1403 | n/a | return int(self._value_) |
---|
1404 | n/a | self.assertEqual( |
---|
1405 | n/a | list(AutoNumber), |
---|
1406 | n/a | [AutoNumber.first, AutoNumber.second, AutoNumber.third], |
---|
1407 | n/a | ) |
---|
1408 | n/a | self.assertEqual(int(AutoNumber.second), 2) |
---|
1409 | n/a | self.assertEqual(AutoNumber.third.value, 3) |
---|
1410 | n/a | self.assertIs(AutoNumber(1), AutoNumber.first) |
---|
1411 | n/a | |
---|
1412 | n/a | def test_inherited_new_from_enhanced_enum(self): |
---|
1413 | n/a | class AutoNumber(Enum): |
---|
1414 | n/a | def __new__(cls): |
---|
1415 | n/a | value = len(cls.__members__) + 1 |
---|
1416 | n/a | obj = object.__new__(cls) |
---|
1417 | n/a | obj._value_ = value |
---|
1418 | n/a | return obj |
---|
1419 | n/a | def __int__(self): |
---|
1420 | n/a | return int(self._value_) |
---|
1421 | n/a | class Color(AutoNumber): |
---|
1422 | n/a | red = () |
---|
1423 | n/a | green = () |
---|
1424 | n/a | blue = () |
---|
1425 | n/a | self.assertEqual(list(Color), [Color.red, Color.green, Color.blue]) |
---|
1426 | n/a | self.assertEqual(list(map(int, Color)), [1, 2, 3]) |
---|
1427 | n/a | |
---|
1428 | n/a | def test_inherited_new_from_mixed_enum(self): |
---|
1429 | n/a | class AutoNumber(IntEnum): |
---|
1430 | n/a | def __new__(cls): |
---|
1431 | n/a | value = len(cls.__members__) + 1 |
---|
1432 | n/a | obj = int.__new__(cls, value) |
---|
1433 | n/a | obj._value_ = value |
---|
1434 | n/a | return obj |
---|
1435 | n/a | class Color(AutoNumber): |
---|
1436 | n/a | red = () |
---|
1437 | n/a | green = () |
---|
1438 | n/a | blue = () |
---|
1439 | n/a | self.assertEqual(list(Color), [Color.red, Color.green, Color.blue]) |
---|
1440 | n/a | self.assertEqual(list(map(int, Color)), [1, 2, 3]) |
---|
1441 | n/a | |
---|
1442 | n/a | def test_equality(self): |
---|
1443 | n/a | class AlwaysEqual: |
---|
1444 | n/a | def __eq__(self, other): |
---|
1445 | n/a | return True |
---|
1446 | n/a | class OrdinaryEnum(Enum): |
---|
1447 | n/a | a = 1 |
---|
1448 | n/a | self.assertEqual(AlwaysEqual(), OrdinaryEnum.a) |
---|
1449 | n/a | self.assertEqual(OrdinaryEnum.a, AlwaysEqual()) |
---|
1450 | n/a | |
---|
1451 | n/a | def test_ordered_mixin(self): |
---|
1452 | n/a | class OrderedEnum(Enum): |
---|
1453 | n/a | def __ge__(self, other): |
---|
1454 | n/a | if self.__class__ is other.__class__: |
---|
1455 | n/a | return self._value_ >= other._value_ |
---|
1456 | n/a | return NotImplemented |
---|
1457 | n/a | def __gt__(self, other): |
---|
1458 | n/a | if self.__class__ is other.__class__: |
---|
1459 | n/a | return self._value_ > other._value_ |
---|
1460 | n/a | return NotImplemented |
---|
1461 | n/a | def __le__(self, other): |
---|
1462 | n/a | if self.__class__ is other.__class__: |
---|
1463 | n/a | return self._value_ <= other._value_ |
---|
1464 | n/a | return NotImplemented |
---|
1465 | n/a | def __lt__(self, other): |
---|
1466 | n/a | if self.__class__ is other.__class__: |
---|
1467 | n/a | return self._value_ < other._value_ |
---|
1468 | n/a | return NotImplemented |
---|
1469 | n/a | class Grade(OrderedEnum): |
---|
1470 | n/a | A = 5 |
---|
1471 | n/a | B = 4 |
---|
1472 | n/a | C = 3 |
---|
1473 | n/a | D = 2 |
---|
1474 | n/a | F = 1 |
---|
1475 | n/a | self.assertGreater(Grade.A, Grade.B) |
---|
1476 | n/a | self.assertLessEqual(Grade.F, Grade.C) |
---|
1477 | n/a | self.assertLess(Grade.D, Grade.A) |
---|
1478 | n/a | self.assertGreaterEqual(Grade.B, Grade.B) |
---|
1479 | n/a | self.assertEqual(Grade.B, Grade.B) |
---|
1480 | n/a | self.assertNotEqual(Grade.C, Grade.D) |
---|
1481 | n/a | |
---|
1482 | n/a | def test_extending2(self): |
---|
1483 | n/a | class Shade(Enum): |
---|
1484 | n/a | def shade(self): |
---|
1485 | n/a | print(self.name) |
---|
1486 | n/a | class Color(Shade): |
---|
1487 | n/a | red = 1 |
---|
1488 | n/a | green = 2 |
---|
1489 | n/a | blue = 3 |
---|
1490 | n/a | with self.assertRaises(TypeError): |
---|
1491 | n/a | class MoreColor(Color): |
---|
1492 | n/a | cyan = 4 |
---|
1493 | n/a | magenta = 5 |
---|
1494 | n/a | yellow = 6 |
---|
1495 | n/a | |
---|
1496 | n/a | def test_extending3(self): |
---|
1497 | n/a | class Shade(Enum): |
---|
1498 | n/a | def shade(self): |
---|
1499 | n/a | return self.name |
---|
1500 | n/a | class Color(Shade): |
---|
1501 | n/a | def hex(self): |
---|
1502 | n/a | return '%s hexlified!' % self.value |
---|
1503 | n/a | class MoreColor(Color): |
---|
1504 | n/a | cyan = 4 |
---|
1505 | n/a | magenta = 5 |
---|
1506 | n/a | yellow = 6 |
---|
1507 | n/a | self.assertEqual(MoreColor.magenta.hex(), '5 hexlified!') |
---|
1508 | n/a | |
---|
1509 | n/a | |
---|
1510 | n/a | def test_no_duplicates(self): |
---|
1511 | n/a | class UniqueEnum(Enum): |
---|
1512 | n/a | def __init__(self, *args): |
---|
1513 | n/a | cls = self.__class__ |
---|
1514 | n/a | if any(self.value == e.value for e in cls): |
---|
1515 | n/a | a = self.name |
---|
1516 | n/a | e = cls(self.value).name |
---|
1517 | n/a | raise ValueError( |
---|
1518 | n/a | "aliases not allowed in UniqueEnum: %r --> %r" |
---|
1519 | n/a | % (a, e) |
---|
1520 | n/a | ) |
---|
1521 | n/a | class Color(UniqueEnum): |
---|
1522 | n/a | red = 1 |
---|
1523 | n/a | green = 2 |
---|
1524 | n/a | blue = 3 |
---|
1525 | n/a | with self.assertRaises(ValueError): |
---|
1526 | n/a | class Color(UniqueEnum): |
---|
1527 | n/a | red = 1 |
---|
1528 | n/a | green = 2 |
---|
1529 | n/a | blue = 3 |
---|
1530 | n/a | grene = 2 |
---|
1531 | n/a | |
---|
1532 | n/a | def test_init(self): |
---|
1533 | n/a | class Planet(Enum): |
---|
1534 | n/a | MERCURY = (3.303e+23, 2.4397e6) |
---|
1535 | n/a | VENUS = (4.869e+24, 6.0518e6) |
---|
1536 | n/a | EARTH = (5.976e+24, 6.37814e6) |
---|
1537 | n/a | MARS = (6.421e+23, 3.3972e6) |
---|
1538 | n/a | JUPITER = (1.9e+27, 7.1492e7) |
---|
1539 | n/a | SATURN = (5.688e+26, 6.0268e7) |
---|
1540 | n/a | URANUS = (8.686e+25, 2.5559e7) |
---|
1541 | n/a | NEPTUNE = (1.024e+26, 2.4746e7) |
---|
1542 | n/a | def __init__(self, mass, radius): |
---|
1543 | n/a | self.mass = mass # in kilograms |
---|
1544 | n/a | self.radius = radius # in meters |
---|
1545 | n/a | @property |
---|
1546 | n/a | def surface_gravity(self): |
---|
1547 | n/a | # universal gravitational constant (m3 kg-1 s-2) |
---|
1548 | n/a | G = 6.67300E-11 |
---|
1549 | n/a | return G * self.mass / (self.radius * self.radius) |
---|
1550 | n/a | self.assertEqual(round(Planet.EARTH.surface_gravity, 2), 9.80) |
---|
1551 | n/a | self.assertEqual(Planet.EARTH.value, (5.976e+24, 6.37814e6)) |
---|
1552 | n/a | |
---|
1553 | n/a | def test_nonhash_value(self): |
---|
1554 | n/a | class AutoNumberInAList(Enum): |
---|
1555 | n/a | def __new__(cls): |
---|
1556 | n/a | value = [len(cls.__members__) + 1] |
---|
1557 | n/a | obj = object.__new__(cls) |
---|
1558 | n/a | obj._value_ = value |
---|
1559 | n/a | return obj |
---|
1560 | n/a | class ColorInAList(AutoNumberInAList): |
---|
1561 | n/a | red = () |
---|
1562 | n/a | green = () |
---|
1563 | n/a | blue = () |
---|
1564 | n/a | self.assertEqual(list(ColorInAList), [ColorInAList.red, ColorInAList.green, ColorInAList.blue]) |
---|
1565 | n/a | for enum, value in zip(ColorInAList, range(3)): |
---|
1566 | n/a | value += 1 |
---|
1567 | n/a | self.assertEqual(enum.value, [value]) |
---|
1568 | n/a | self.assertIs(ColorInAList([value]), enum) |
---|
1569 | n/a | |
---|
1570 | n/a | def test_conflicting_types_resolved_in_new(self): |
---|
1571 | n/a | class LabelledIntEnum(int, Enum): |
---|
1572 | n/a | def __new__(cls, *args): |
---|
1573 | n/a | value, label = args |
---|
1574 | n/a | obj = int.__new__(cls, value) |
---|
1575 | n/a | obj.label = label |
---|
1576 | n/a | obj._value_ = value |
---|
1577 | n/a | return obj |
---|
1578 | n/a | |
---|
1579 | n/a | class LabelledList(LabelledIntEnum): |
---|
1580 | n/a | unprocessed = (1, "Unprocessed") |
---|
1581 | n/a | payment_complete = (2, "Payment Complete") |
---|
1582 | n/a | |
---|
1583 | n/a | self.assertEqual(list(LabelledList), [LabelledList.unprocessed, LabelledList.payment_complete]) |
---|
1584 | n/a | self.assertEqual(LabelledList.unprocessed, 1) |
---|
1585 | n/a | self.assertEqual(LabelledList(1), LabelledList.unprocessed) |
---|
1586 | n/a | |
---|
1587 | n/a | def test_auto_number(self): |
---|
1588 | n/a | class Color(Enum): |
---|
1589 | n/a | red = auto() |
---|
1590 | n/a | blue = auto() |
---|
1591 | n/a | green = auto() |
---|
1592 | n/a | |
---|
1593 | n/a | self.assertEqual(list(Color), [Color.red, Color.blue, Color.green]) |
---|
1594 | n/a | self.assertEqual(Color.red.value, 1) |
---|
1595 | n/a | self.assertEqual(Color.blue.value, 2) |
---|
1596 | n/a | self.assertEqual(Color.green.value, 3) |
---|
1597 | n/a | |
---|
1598 | n/a | def test_auto_name(self): |
---|
1599 | n/a | class Color(Enum): |
---|
1600 | n/a | def _generate_next_value_(name, start, count, last): |
---|
1601 | n/a | return name |
---|
1602 | n/a | red = auto() |
---|
1603 | n/a | blue = auto() |
---|
1604 | n/a | green = auto() |
---|
1605 | n/a | |
---|
1606 | n/a | self.assertEqual(list(Color), [Color.red, Color.blue, Color.green]) |
---|
1607 | n/a | self.assertEqual(Color.red.value, 'red') |
---|
1608 | n/a | self.assertEqual(Color.blue.value, 'blue') |
---|
1609 | n/a | self.assertEqual(Color.green.value, 'green') |
---|
1610 | n/a | |
---|
1611 | n/a | def test_auto_name_inherit(self): |
---|
1612 | n/a | class AutoNameEnum(Enum): |
---|
1613 | n/a | def _generate_next_value_(name, start, count, last): |
---|
1614 | n/a | return name |
---|
1615 | n/a | class Color(AutoNameEnum): |
---|
1616 | n/a | red = auto() |
---|
1617 | n/a | blue = auto() |
---|
1618 | n/a | green = auto() |
---|
1619 | n/a | |
---|
1620 | n/a | self.assertEqual(list(Color), [Color.red, Color.blue, Color.green]) |
---|
1621 | n/a | self.assertEqual(Color.red.value, 'red') |
---|
1622 | n/a | self.assertEqual(Color.blue.value, 'blue') |
---|
1623 | n/a | self.assertEqual(Color.green.value, 'green') |
---|
1624 | n/a | |
---|
1625 | n/a | def test_auto_garbage(self): |
---|
1626 | n/a | class Color(Enum): |
---|
1627 | n/a | red = 'red' |
---|
1628 | n/a | blue = auto() |
---|
1629 | n/a | self.assertEqual(Color.blue.value, 1) |
---|
1630 | n/a | |
---|
1631 | n/a | def test_auto_garbage_corrected(self): |
---|
1632 | n/a | class Color(Enum): |
---|
1633 | n/a | red = 'red' |
---|
1634 | n/a | blue = 2 |
---|
1635 | n/a | green = auto() |
---|
1636 | n/a | |
---|
1637 | n/a | self.assertEqual(list(Color), [Color.red, Color.blue, Color.green]) |
---|
1638 | n/a | self.assertEqual(Color.red.value, 'red') |
---|
1639 | n/a | self.assertEqual(Color.blue.value, 2) |
---|
1640 | n/a | self.assertEqual(Color.green.value, 3) |
---|
1641 | n/a | |
---|
1642 | n/a | def test_duplicate_auto(self): |
---|
1643 | n/a | class Dupes(Enum): |
---|
1644 | n/a | first = primero = auto() |
---|
1645 | n/a | second = auto() |
---|
1646 | n/a | third = auto() |
---|
1647 | n/a | self.assertEqual([Dupes.first, Dupes.second, Dupes.third], list(Dupes)) |
---|
1648 | n/a | |
---|
1649 | n/a | |
---|
1650 | n/a | class TestOrder(unittest.TestCase): |
---|
1651 | n/a | |
---|
1652 | n/a | def test_same_members(self): |
---|
1653 | n/a | class Color(Enum): |
---|
1654 | n/a | _order_ = 'red green blue' |
---|
1655 | n/a | red = 1 |
---|
1656 | n/a | green = 2 |
---|
1657 | n/a | blue = 3 |
---|
1658 | n/a | |
---|
1659 | n/a | def test_same_members_with_aliases(self): |
---|
1660 | n/a | class Color(Enum): |
---|
1661 | n/a | _order_ = 'red green blue' |
---|
1662 | n/a | red = 1 |
---|
1663 | n/a | green = 2 |
---|
1664 | n/a | blue = 3 |
---|
1665 | n/a | verde = green |
---|
1666 | n/a | |
---|
1667 | n/a | def test_same_members_wrong_order(self): |
---|
1668 | n/a | with self.assertRaisesRegex(TypeError, 'member order does not match _order_'): |
---|
1669 | n/a | class Color(Enum): |
---|
1670 | n/a | _order_ = 'red green blue' |
---|
1671 | n/a | red = 1 |
---|
1672 | n/a | blue = 3 |
---|
1673 | n/a | green = 2 |
---|
1674 | n/a | |
---|
1675 | n/a | def test_order_has_extra_members(self): |
---|
1676 | n/a | with self.assertRaisesRegex(TypeError, 'member order does not match _order_'): |
---|
1677 | n/a | class Color(Enum): |
---|
1678 | n/a | _order_ = 'red green blue purple' |
---|
1679 | n/a | red = 1 |
---|
1680 | n/a | green = 2 |
---|
1681 | n/a | blue = 3 |
---|
1682 | n/a | |
---|
1683 | n/a | def test_order_has_extra_members_with_aliases(self): |
---|
1684 | n/a | with self.assertRaisesRegex(TypeError, 'member order does not match _order_'): |
---|
1685 | n/a | class Color(Enum): |
---|
1686 | n/a | _order_ = 'red green blue purple' |
---|
1687 | n/a | red = 1 |
---|
1688 | n/a | green = 2 |
---|
1689 | n/a | blue = 3 |
---|
1690 | n/a | verde = green |
---|
1691 | n/a | |
---|
1692 | n/a | def test_enum_has_extra_members(self): |
---|
1693 | n/a | with self.assertRaisesRegex(TypeError, 'member order does not match _order_'): |
---|
1694 | n/a | class Color(Enum): |
---|
1695 | n/a | _order_ = 'red green blue' |
---|
1696 | n/a | red = 1 |
---|
1697 | n/a | green = 2 |
---|
1698 | n/a | blue = 3 |
---|
1699 | n/a | purple = 4 |
---|
1700 | n/a | |
---|
1701 | n/a | def test_enum_has_extra_members_with_aliases(self): |
---|
1702 | n/a | with self.assertRaisesRegex(TypeError, 'member order does not match _order_'): |
---|
1703 | n/a | class Color(Enum): |
---|
1704 | n/a | _order_ = 'red green blue' |
---|
1705 | n/a | red = 1 |
---|
1706 | n/a | green = 2 |
---|
1707 | n/a | blue = 3 |
---|
1708 | n/a | purple = 4 |
---|
1709 | n/a | verde = green |
---|
1710 | n/a | |
---|
1711 | n/a | |
---|
1712 | n/a | class TestFlag(unittest.TestCase): |
---|
1713 | n/a | """Tests of the Flags.""" |
---|
1714 | n/a | |
---|
1715 | n/a | class Perm(Flag): |
---|
1716 | n/a | R, W, X = 4, 2, 1 |
---|
1717 | n/a | |
---|
1718 | n/a | class Open(Flag): |
---|
1719 | n/a | RO = 0 |
---|
1720 | n/a | WO = 1 |
---|
1721 | n/a | RW = 2 |
---|
1722 | n/a | AC = 3 |
---|
1723 | n/a | CE = 1<<19 |
---|
1724 | n/a | |
---|
1725 | n/a | def test_str(self): |
---|
1726 | n/a | Perm = self.Perm |
---|
1727 | n/a | self.assertEqual(str(Perm.R), 'Perm.R') |
---|
1728 | n/a | self.assertEqual(str(Perm.W), 'Perm.W') |
---|
1729 | n/a | self.assertEqual(str(Perm.X), 'Perm.X') |
---|
1730 | n/a | self.assertEqual(str(Perm.R | Perm.W), 'Perm.R|W') |
---|
1731 | n/a | self.assertEqual(str(Perm.R | Perm.W | Perm.X), 'Perm.R|W|X') |
---|
1732 | n/a | self.assertEqual(str(Perm(0)), 'Perm.0') |
---|
1733 | n/a | self.assertEqual(str(~Perm.R), 'Perm.W|X') |
---|
1734 | n/a | self.assertEqual(str(~Perm.W), 'Perm.R|X') |
---|
1735 | n/a | self.assertEqual(str(~Perm.X), 'Perm.R|W') |
---|
1736 | n/a | self.assertEqual(str(~(Perm.R | Perm.W)), 'Perm.X') |
---|
1737 | n/a | self.assertEqual(str(~(Perm.R | Perm.W | Perm.X)), 'Perm.0') |
---|
1738 | n/a | self.assertEqual(str(Perm(~0)), 'Perm.R|W|X') |
---|
1739 | n/a | |
---|
1740 | n/a | Open = self.Open |
---|
1741 | n/a | self.assertEqual(str(Open.RO), 'Open.RO') |
---|
1742 | n/a | self.assertEqual(str(Open.WO), 'Open.WO') |
---|
1743 | n/a | self.assertEqual(str(Open.AC), 'Open.AC') |
---|
1744 | n/a | self.assertEqual(str(Open.RO | Open.CE), 'Open.CE') |
---|
1745 | n/a | self.assertEqual(str(Open.WO | Open.CE), 'Open.CE|WO') |
---|
1746 | n/a | self.assertEqual(str(~Open.RO), 'Open.CE|AC|RW|WO') |
---|
1747 | n/a | self.assertEqual(str(~Open.WO), 'Open.CE|RW') |
---|
1748 | n/a | self.assertEqual(str(~Open.AC), 'Open.CE') |
---|
1749 | n/a | self.assertEqual(str(~(Open.RO | Open.CE)), 'Open.AC') |
---|
1750 | n/a | self.assertEqual(str(~(Open.WO | Open.CE)), 'Open.RW') |
---|
1751 | n/a | |
---|
1752 | n/a | def test_repr(self): |
---|
1753 | n/a | Perm = self.Perm |
---|
1754 | n/a | self.assertEqual(repr(Perm.R), '<Perm.R: 4>') |
---|
1755 | n/a | self.assertEqual(repr(Perm.W), '<Perm.W: 2>') |
---|
1756 | n/a | self.assertEqual(repr(Perm.X), '<Perm.X: 1>') |
---|
1757 | n/a | self.assertEqual(repr(Perm.R | Perm.W), '<Perm.R|W: 6>') |
---|
1758 | n/a | self.assertEqual(repr(Perm.R | Perm.W | Perm.X), '<Perm.R|W|X: 7>') |
---|
1759 | n/a | self.assertEqual(repr(Perm(0)), '<Perm.0: 0>') |
---|
1760 | n/a | self.assertEqual(repr(~Perm.R), '<Perm.W|X: 3>') |
---|
1761 | n/a | self.assertEqual(repr(~Perm.W), '<Perm.R|X: 5>') |
---|
1762 | n/a | self.assertEqual(repr(~Perm.X), '<Perm.R|W: 6>') |
---|
1763 | n/a | self.assertEqual(repr(~(Perm.R | Perm.W)), '<Perm.X: 1>') |
---|
1764 | n/a | self.assertEqual(repr(~(Perm.R | Perm.W | Perm.X)), '<Perm.0: 0>') |
---|
1765 | n/a | self.assertEqual(repr(Perm(~0)), '<Perm.R|W|X: 7>') |
---|
1766 | n/a | |
---|
1767 | n/a | Open = self.Open |
---|
1768 | n/a | self.assertEqual(repr(Open.RO), '<Open.RO: 0>') |
---|
1769 | n/a | self.assertEqual(repr(Open.WO), '<Open.WO: 1>') |
---|
1770 | n/a | self.assertEqual(repr(Open.AC), '<Open.AC: 3>') |
---|
1771 | n/a | self.assertEqual(repr(Open.RO | Open.CE), '<Open.CE: 524288>') |
---|
1772 | n/a | self.assertEqual(repr(Open.WO | Open.CE), '<Open.CE|WO: 524289>') |
---|
1773 | n/a | self.assertEqual(repr(~Open.RO), '<Open.CE|AC|RW|WO: 524291>') |
---|
1774 | n/a | self.assertEqual(repr(~Open.WO), '<Open.CE|RW: 524290>') |
---|
1775 | n/a | self.assertEqual(repr(~Open.AC), '<Open.CE: 524288>') |
---|
1776 | n/a | self.assertEqual(repr(~(Open.RO | Open.CE)), '<Open.AC: 3>') |
---|
1777 | n/a | self.assertEqual(repr(~(Open.WO | Open.CE)), '<Open.RW: 2>') |
---|
1778 | n/a | |
---|
1779 | n/a | def test_or(self): |
---|
1780 | n/a | Perm = self.Perm |
---|
1781 | n/a | for i in Perm: |
---|
1782 | n/a | for j in Perm: |
---|
1783 | n/a | self.assertEqual((i | j), Perm(i.value | j.value)) |
---|
1784 | n/a | self.assertEqual((i | j).value, i.value | j.value) |
---|
1785 | n/a | self.assertIs(type(i | j), Perm) |
---|
1786 | n/a | for i in Perm: |
---|
1787 | n/a | self.assertIs(i | i, i) |
---|
1788 | n/a | Open = self.Open |
---|
1789 | n/a | self.assertIs(Open.RO | Open.CE, Open.CE) |
---|
1790 | n/a | |
---|
1791 | n/a | def test_and(self): |
---|
1792 | n/a | Perm = self.Perm |
---|
1793 | n/a | RW = Perm.R | Perm.W |
---|
1794 | n/a | RX = Perm.R | Perm.X |
---|
1795 | n/a | WX = Perm.W | Perm.X |
---|
1796 | n/a | RWX = Perm.R | Perm.W | Perm.X |
---|
1797 | n/a | values = list(Perm) + [RW, RX, WX, RWX, Perm(0)] |
---|
1798 | n/a | for i in values: |
---|
1799 | n/a | for j in values: |
---|
1800 | n/a | self.assertEqual((i & j).value, i.value & j.value) |
---|
1801 | n/a | self.assertIs(type(i & j), Perm) |
---|
1802 | n/a | for i in Perm: |
---|
1803 | n/a | self.assertIs(i & i, i) |
---|
1804 | n/a | self.assertIs(i & RWX, i) |
---|
1805 | n/a | self.assertIs(RWX & i, i) |
---|
1806 | n/a | Open = self.Open |
---|
1807 | n/a | self.assertIs(Open.RO & Open.CE, Open.RO) |
---|
1808 | n/a | |
---|
1809 | n/a | def test_xor(self): |
---|
1810 | n/a | Perm = self.Perm |
---|
1811 | n/a | for i in Perm: |
---|
1812 | n/a | for j in Perm: |
---|
1813 | n/a | self.assertEqual((i ^ j).value, i.value ^ j.value) |
---|
1814 | n/a | self.assertIs(type(i ^ j), Perm) |
---|
1815 | n/a | for i in Perm: |
---|
1816 | n/a | self.assertIs(i ^ Perm(0), i) |
---|
1817 | n/a | self.assertIs(Perm(0) ^ i, i) |
---|
1818 | n/a | Open = self.Open |
---|
1819 | n/a | self.assertIs(Open.RO ^ Open.CE, Open.CE) |
---|
1820 | n/a | self.assertIs(Open.CE ^ Open.CE, Open.RO) |
---|
1821 | n/a | |
---|
1822 | n/a | def test_invert(self): |
---|
1823 | n/a | Perm = self.Perm |
---|
1824 | n/a | RW = Perm.R | Perm.W |
---|
1825 | n/a | RX = Perm.R | Perm.X |
---|
1826 | n/a | WX = Perm.W | Perm.X |
---|
1827 | n/a | RWX = Perm.R | Perm.W | Perm.X |
---|
1828 | n/a | values = list(Perm) + [RW, RX, WX, RWX, Perm(0)] |
---|
1829 | n/a | for i in values: |
---|
1830 | n/a | self.assertIs(type(~i), Perm) |
---|
1831 | n/a | self.assertEqual(~~i, i) |
---|
1832 | n/a | for i in Perm: |
---|
1833 | n/a | self.assertIs(~~i, i) |
---|
1834 | n/a | Open = self.Open |
---|
1835 | n/a | self.assertIs(Open.WO & ~Open.WO, Open.RO) |
---|
1836 | n/a | self.assertIs((Open.WO|Open.CE) & ~Open.WO, Open.CE) |
---|
1837 | n/a | |
---|
1838 | n/a | def test_bool(self): |
---|
1839 | n/a | Perm = self.Perm |
---|
1840 | n/a | for f in Perm: |
---|
1841 | n/a | self.assertTrue(f) |
---|
1842 | n/a | Open = self.Open |
---|
1843 | n/a | for f in Open: |
---|
1844 | n/a | self.assertEqual(bool(f.value), bool(f)) |
---|
1845 | n/a | |
---|
1846 | n/a | def test_programatic_function_string(self): |
---|
1847 | n/a | Perm = Flag('Perm', 'R W X') |
---|
1848 | n/a | lst = list(Perm) |
---|
1849 | n/a | self.assertEqual(len(lst), len(Perm)) |
---|
1850 | n/a | self.assertEqual(len(Perm), 3, Perm) |
---|
1851 | n/a | self.assertEqual(lst, [Perm.R, Perm.W, Perm.X]) |
---|
1852 | n/a | for i, n in enumerate('R W X'.split()): |
---|
1853 | n/a | v = 1<<i |
---|
1854 | n/a | e = Perm(v) |
---|
1855 | n/a | self.assertEqual(e.value, v) |
---|
1856 | n/a | self.assertEqual(type(e.value), int) |
---|
1857 | n/a | self.assertEqual(e.name, n) |
---|
1858 | n/a | self.assertIn(e, Perm) |
---|
1859 | n/a | self.assertIs(type(e), Perm) |
---|
1860 | n/a | |
---|
1861 | n/a | def test_programatic_function_string_with_start(self): |
---|
1862 | n/a | Perm = Flag('Perm', 'R W X', start=8) |
---|
1863 | n/a | lst = list(Perm) |
---|
1864 | n/a | self.assertEqual(len(lst), len(Perm)) |
---|
1865 | n/a | self.assertEqual(len(Perm), 3, Perm) |
---|
1866 | n/a | self.assertEqual(lst, [Perm.R, Perm.W, Perm.X]) |
---|
1867 | n/a | for i, n in enumerate('R W X'.split()): |
---|
1868 | n/a | v = 8<<i |
---|
1869 | n/a | e = Perm(v) |
---|
1870 | n/a | self.assertEqual(e.value, v) |
---|
1871 | n/a | self.assertEqual(type(e.value), int) |
---|
1872 | n/a | self.assertEqual(e.name, n) |
---|
1873 | n/a | self.assertIn(e, Perm) |
---|
1874 | n/a | self.assertIs(type(e), Perm) |
---|
1875 | n/a | |
---|
1876 | n/a | def test_programatic_function_string_list(self): |
---|
1877 | n/a | Perm = Flag('Perm', ['R', 'W', 'X']) |
---|
1878 | n/a | lst = list(Perm) |
---|
1879 | n/a | self.assertEqual(len(lst), len(Perm)) |
---|
1880 | n/a | self.assertEqual(len(Perm), 3, Perm) |
---|
1881 | n/a | self.assertEqual(lst, [Perm.R, Perm.W, Perm.X]) |
---|
1882 | n/a | for i, n in enumerate('R W X'.split()): |
---|
1883 | n/a | v = 1<<i |
---|
1884 | n/a | e = Perm(v) |
---|
1885 | n/a | self.assertEqual(e.value, v) |
---|
1886 | n/a | self.assertEqual(type(e.value), int) |
---|
1887 | n/a | self.assertEqual(e.name, n) |
---|
1888 | n/a | self.assertIn(e, Perm) |
---|
1889 | n/a | self.assertIs(type(e), Perm) |
---|
1890 | n/a | |
---|
1891 | n/a | def test_programatic_function_iterable(self): |
---|
1892 | n/a | Perm = Flag('Perm', (('R', 2), ('W', 8), ('X', 32))) |
---|
1893 | n/a | lst = list(Perm) |
---|
1894 | n/a | self.assertEqual(len(lst), len(Perm)) |
---|
1895 | n/a | self.assertEqual(len(Perm), 3, Perm) |
---|
1896 | n/a | self.assertEqual(lst, [Perm.R, Perm.W, Perm.X]) |
---|
1897 | n/a | for i, n in enumerate('R W X'.split()): |
---|
1898 | n/a | v = 1<<(2*i+1) |
---|
1899 | n/a | e = Perm(v) |
---|
1900 | n/a | self.assertEqual(e.value, v) |
---|
1901 | n/a | self.assertEqual(type(e.value), int) |
---|
1902 | n/a | self.assertEqual(e.name, n) |
---|
1903 | n/a | self.assertIn(e, Perm) |
---|
1904 | n/a | self.assertIs(type(e), Perm) |
---|
1905 | n/a | |
---|
1906 | n/a | def test_programatic_function_from_dict(self): |
---|
1907 | n/a | Perm = Flag('Perm', OrderedDict((('R', 2), ('W', 8), ('X', 32)))) |
---|
1908 | n/a | lst = list(Perm) |
---|
1909 | n/a | self.assertEqual(len(lst), len(Perm)) |
---|
1910 | n/a | self.assertEqual(len(Perm), 3, Perm) |
---|
1911 | n/a | self.assertEqual(lst, [Perm.R, Perm.W, Perm.X]) |
---|
1912 | n/a | for i, n in enumerate('R W X'.split()): |
---|
1913 | n/a | v = 1<<(2*i+1) |
---|
1914 | n/a | e = Perm(v) |
---|
1915 | n/a | self.assertEqual(e.value, v) |
---|
1916 | n/a | self.assertEqual(type(e.value), int) |
---|
1917 | n/a | self.assertEqual(e.name, n) |
---|
1918 | n/a | self.assertIn(e, Perm) |
---|
1919 | n/a | self.assertIs(type(e), Perm) |
---|
1920 | n/a | |
---|
1921 | n/a | def test_pickle(self): |
---|
1922 | n/a | if isinstance(FlagStooges, Exception): |
---|
1923 | n/a | raise FlagStooges |
---|
1924 | n/a | test_pickle_dump_load(self.assertIs, FlagStooges.CURLY|FlagStooges.MOE) |
---|
1925 | n/a | test_pickle_dump_load(self.assertIs, FlagStooges) |
---|
1926 | n/a | |
---|
1927 | n/a | def test_containment(self): |
---|
1928 | n/a | Perm = self.Perm |
---|
1929 | n/a | R, W, X = Perm |
---|
1930 | n/a | RW = R | W |
---|
1931 | n/a | RX = R | X |
---|
1932 | n/a | WX = W | X |
---|
1933 | n/a | RWX = R | W | X |
---|
1934 | n/a | self.assertTrue(R in RW) |
---|
1935 | n/a | self.assertTrue(R in RX) |
---|
1936 | n/a | self.assertTrue(R in RWX) |
---|
1937 | n/a | self.assertTrue(W in RW) |
---|
1938 | n/a | self.assertTrue(W in WX) |
---|
1939 | n/a | self.assertTrue(W in RWX) |
---|
1940 | n/a | self.assertTrue(X in RX) |
---|
1941 | n/a | self.assertTrue(X in WX) |
---|
1942 | n/a | self.assertTrue(X in RWX) |
---|
1943 | n/a | self.assertFalse(R in WX) |
---|
1944 | n/a | self.assertFalse(W in RX) |
---|
1945 | n/a | self.assertFalse(X in RW) |
---|
1946 | n/a | |
---|
1947 | n/a | def test_auto_number(self): |
---|
1948 | n/a | class Color(Flag): |
---|
1949 | n/a | red = auto() |
---|
1950 | n/a | blue = auto() |
---|
1951 | n/a | green = auto() |
---|
1952 | n/a | |
---|
1953 | n/a | self.assertEqual(list(Color), [Color.red, Color.blue, Color.green]) |
---|
1954 | n/a | self.assertEqual(Color.red.value, 1) |
---|
1955 | n/a | self.assertEqual(Color.blue.value, 2) |
---|
1956 | n/a | self.assertEqual(Color.green.value, 4) |
---|
1957 | n/a | |
---|
1958 | n/a | def test_auto_number_garbage(self): |
---|
1959 | n/a | with self.assertRaisesRegex(TypeError, 'Invalid Flag value: .not an int.'): |
---|
1960 | n/a | class Color(Flag): |
---|
1961 | n/a | red = 'not an int' |
---|
1962 | n/a | blue = auto() |
---|
1963 | n/a | |
---|
1964 | n/a | def test_cascading_failure(self): |
---|
1965 | n/a | class Bizarre(Flag): |
---|
1966 | n/a | c = 3 |
---|
1967 | n/a | d = 4 |
---|
1968 | n/a | f = 6 |
---|
1969 | n/a | # Bizarre.c | Bizarre.d |
---|
1970 | n/a | self.assertRaisesRegex(ValueError, "5 is not a valid Bizarre", Bizarre, 5) |
---|
1971 | n/a | self.assertRaisesRegex(ValueError, "5 is not a valid Bizarre", Bizarre, 5) |
---|
1972 | n/a | self.assertRaisesRegex(ValueError, "2 is not a valid Bizarre", Bizarre, 2) |
---|
1973 | n/a | self.assertRaisesRegex(ValueError, "2 is not a valid Bizarre", Bizarre, 2) |
---|
1974 | n/a | self.assertRaisesRegex(ValueError, "1 is not a valid Bizarre", Bizarre, 1) |
---|
1975 | n/a | self.assertRaisesRegex(ValueError, "1 is not a valid Bizarre", Bizarre, 1) |
---|
1976 | n/a | |
---|
1977 | n/a | def test_duplicate_auto(self): |
---|
1978 | n/a | class Dupes(Enum): |
---|
1979 | n/a | first = primero = auto() |
---|
1980 | n/a | second = auto() |
---|
1981 | n/a | third = auto() |
---|
1982 | n/a | self.assertEqual([Dupes.first, Dupes.second, Dupes.third], list(Dupes)) |
---|
1983 | n/a | |
---|
1984 | n/a | def test_bizarre(self): |
---|
1985 | n/a | class Bizarre(Flag): |
---|
1986 | n/a | b = 3 |
---|
1987 | n/a | c = 4 |
---|
1988 | n/a | d = 6 |
---|
1989 | n/a | self.assertEqual(repr(Bizarre(7)), '<Bizarre.d|c|b: 7>') |
---|
1990 | n/a | |
---|
1991 | n/a | @unittest.skipUnless(threading, 'Threading required for this test.') |
---|
1992 | n/a | @support.reap_threads |
---|
1993 | n/a | def test_unique_composite(self): |
---|
1994 | n/a | # override __eq__ to be identity only |
---|
1995 | n/a | class TestFlag(Flag): |
---|
1996 | n/a | one = auto() |
---|
1997 | n/a | two = auto() |
---|
1998 | n/a | three = auto() |
---|
1999 | n/a | four = auto() |
---|
2000 | n/a | five = auto() |
---|
2001 | n/a | six = auto() |
---|
2002 | n/a | seven = auto() |
---|
2003 | n/a | eight = auto() |
---|
2004 | n/a | def __eq__(self, other): |
---|
2005 | n/a | return self is other |
---|
2006 | n/a | def __hash__(self): |
---|
2007 | n/a | return hash(self._value_) |
---|
2008 | n/a | # have multiple threads competing to complete the composite members |
---|
2009 | n/a | seen = set() |
---|
2010 | n/a | failed = False |
---|
2011 | n/a | def cycle_enum(): |
---|
2012 | n/a | nonlocal failed |
---|
2013 | n/a | try: |
---|
2014 | n/a | for i in range(256): |
---|
2015 | n/a | seen.add(TestFlag(i)) |
---|
2016 | n/a | except Exception: |
---|
2017 | n/a | failed = True |
---|
2018 | n/a | threads = [ |
---|
2019 | n/a | threading.Thread(target=cycle_enum) |
---|
2020 | n/a | for _ in range(8) |
---|
2021 | n/a | ] |
---|
2022 | n/a | with support.start_threads(threads): |
---|
2023 | n/a | pass |
---|
2024 | n/a | # check that only 248 members were created |
---|
2025 | n/a | self.assertFalse( |
---|
2026 | n/a | failed, |
---|
2027 | n/a | 'at least one thread failed while creating composite members') |
---|
2028 | n/a | self.assertEqual(256, len(seen), 'too many composite members created') |
---|
2029 | n/a | |
---|
2030 | n/a | |
---|
2031 | n/a | class TestIntFlag(unittest.TestCase): |
---|
2032 | n/a | """Tests of the IntFlags.""" |
---|
2033 | n/a | |
---|
2034 | n/a | class Perm(IntFlag): |
---|
2035 | n/a | X = 1 << 0 |
---|
2036 | n/a | W = 1 << 1 |
---|
2037 | n/a | R = 1 << 2 |
---|
2038 | n/a | |
---|
2039 | n/a | class Open(IntFlag): |
---|
2040 | n/a | RO = 0 |
---|
2041 | n/a | WO = 1 |
---|
2042 | n/a | RW = 2 |
---|
2043 | n/a | AC = 3 |
---|
2044 | n/a | CE = 1<<19 |
---|
2045 | n/a | |
---|
2046 | n/a | def test_type(self): |
---|
2047 | n/a | Perm = self.Perm |
---|
2048 | n/a | Open = self.Open |
---|
2049 | n/a | for f in Perm: |
---|
2050 | n/a | self.assertTrue(isinstance(f, Perm)) |
---|
2051 | n/a | self.assertEqual(f, f.value) |
---|
2052 | n/a | self.assertTrue(isinstance(Perm.W | Perm.X, Perm)) |
---|
2053 | n/a | self.assertEqual(Perm.W | Perm.X, 3) |
---|
2054 | n/a | for f in Open: |
---|
2055 | n/a | self.assertTrue(isinstance(f, Open)) |
---|
2056 | n/a | self.assertEqual(f, f.value) |
---|
2057 | n/a | self.assertTrue(isinstance(Open.WO | Open.RW, Open)) |
---|
2058 | n/a | self.assertEqual(Open.WO | Open.RW, 3) |
---|
2059 | n/a | |
---|
2060 | n/a | |
---|
2061 | n/a | def test_str(self): |
---|
2062 | n/a | Perm = self.Perm |
---|
2063 | n/a | self.assertEqual(str(Perm.R), 'Perm.R') |
---|
2064 | n/a | self.assertEqual(str(Perm.W), 'Perm.W') |
---|
2065 | n/a | self.assertEqual(str(Perm.X), 'Perm.X') |
---|
2066 | n/a | self.assertEqual(str(Perm.R | Perm.W), 'Perm.R|W') |
---|
2067 | n/a | self.assertEqual(str(Perm.R | Perm.W | Perm.X), 'Perm.R|W|X') |
---|
2068 | n/a | self.assertEqual(str(Perm.R | 8), 'Perm.8|R') |
---|
2069 | n/a | self.assertEqual(str(Perm(0)), 'Perm.0') |
---|
2070 | n/a | self.assertEqual(str(Perm(8)), 'Perm.8') |
---|
2071 | n/a | self.assertEqual(str(~Perm.R), 'Perm.W|X') |
---|
2072 | n/a | self.assertEqual(str(~Perm.W), 'Perm.R|X') |
---|
2073 | n/a | self.assertEqual(str(~Perm.X), 'Perm.R|W') |
---|
2074 | n/a | self.assertEqual(str(~(Perm.R | Perm.W)), 'Perm.X') |
---|
2075 | n/a | self.assertEqual(str(~(Perm.R | Perm.W | Perm.X)), 'Perm.-8') |
---|
2076 | n/a | self.assertEqual(str(~(Perm.R | 8)), 'Perm.W|X') |
---|
2077 | n/a | self.assertEqual(str(Perm(~0)), 'Perm.R|W|X') |
---|
2078 | n/a | self.assertEqual(str(Perm(~8)), 'Perm.R|W|X') |
---|
2079 | n/a | |
---|
2080 | n/a | Open = self.Open |
---|
2081 | n/a | self.assertEqual(str(Open.RO), 'Open.RO') |
---|
2082 | n/a | self.assertEqual(str(Open.WO), 'Open.WO') |
---|
2083 | n/a | self.assertEqual(str(Open.AC), 'Open.AC') |
---|
2084 | n/a | self.assertEqual(str(Open.RO | Open.CE), 'Open.CE') |
---|
2085 | n/a | self.assertEqual(str(Open.WO | Open.CE), 'Open.CE|WO') |
---|
2086 | n/a | self.assertEqual(str(Open(4)), 'Open.4') |
---|
2087 | n/a | self.assertEqual(str(~Open.RO), 'Open.CE|AC|RW|WO') |
---|
2088 | n/a | self.assertEqual(str(~Open.WO), 'Open.CE|RW') |
---|
2089 | n/a | self.assertEqual(str(~Open.AC), 'Open.CE') |
---|
2090 | n/a | self.assertEqual(str(~(Open.RO | Open.CE)), 'Open.AC|RW|WO') |
---|
2091 | n/a | self.assertEqual(str(~(Open.WO | Open.CE)), 'Open.RW') |
---|
2092 | n/a | self.assertEqual(str(Open(~4)), 'Open.CE|AC|RW|WO') |
---|
2093 | n/a | |
---|
2094 | n/a | def test_repr(self): |
---|
2095 | n/a | Perm = self.Perm |
---|
2096 | n/a | self.assertEqual(repr(Perm.R), '<Perm.R: 4>') |
---|
2097 | n/a | self.assertEqual(repr(Perm.W), '<Perm.W: 2>') |
---|
2098 | n/a | self.assertEqual(repr(Perm.X), '<Perm.X: 1>') |
---|
2099 | n/a | self.assertEqual(repr(Perm.R | Perm.W), '<Perm.R|W: 6>') |
---|
2100 | n/a | self.assertEqual(repr(Perm.R | Perm.W | Perm.X), '<Perm.R|W|X: 7>') |
---|
2101 | n/a | self.assertEqual(repr(Perm.R | 8), '<Perm.8|R: 12>') |
---|
2102 | n/a | self.assertEqual(repr(Perm(0)), '<Perm.0: 0>') |
---|
2103 | n/a | self.assertEqual(repr(Perm(8)), '<Perm.8: 8>') |
---|
2104 | n/a | self.assertEqual(repr(~Perm.R), '<Perm.W|X: -5>') |
---|
2105 | n/a | self.assertEqual(repr(~Perm.W), '<Perm.R|X: -3>') |
---|
2106 | n/a | self.assertEqual(repr(~Perm.X), '<Perm.R|W: -2>') |
---|
2107 | n/a | self.assertEqual(repr(~(Perm.R | Perm.W)), '<Perm.X: -7>') |
---|
2108 | n/a | self.assertEqual(repr(~(Perm.R | Perm.W | Perm.X)), '<Perm.-8: -8>') |
---|
2109 | n/a | self.assertEqual(repr(~(Perm.R | 8)), '<Perm.W|X: -13>') |
---|
2110 | n/a | self.assertEqual(repr(Perm(~0)), '<Perm.R|W|X: -1>') |
---|
2111 | n/a | self.assertEqual(repr(Perm(~8)), '<Perm.R|W|X: -9>') |
---|
2112 | n/a | |
---|
2113 | n/a | Open = self.Open |
---|
2114 | n/a | self.assertEqual(repr(Open.RO), '<Open.RO: 0>') |
---|
2115 | n/a | self.assertEqual(repr(Open.WO), '<Open.WO: 1>') |
---|
2116 | n/a | self.assertEqual(repr(Open.AC), '<Open.AC: 3>') |
---|
2117 | n/a | self.assertEqual(repr(Open.RO | Open.CE), '<Open.CE: 524288>') |
---|
2118 | n/a | self.assertEqual(repr(Open.WO | Open.CE), '<Open.CE|WO: 524289>') |
---|
2119 | n/a | self.assertEqual(repr(Open(4)), '<Open.4: 4>') |
---|
2120 | n/a | self.assertEqual(repr(~Open.RO), '<Open.CE|AC|RW|WO: -1>') |
---|
2121 | n/a | self.assertEqual(repr(~Open.WO), '<Open.CE|RW: -2>') |
---|
2122 | n/a | self.assertEqual(repr(~Open.AC), '<Open.CE: -4>') |
---|
2123 | n/a | self.assertEqual(repr(~(Open.RO | Open.CE)), '<Open.AC|RW|WO: -524289>') |
---|
2124 | n/a | self.assertEqual(repr(~(Open.WO | Open.CE)), '<Open.RW: -524290>') |
---|
2125 | n/a | self.assertEqual(repr(Open(~4)), '<Open.CE|AC|RW|WO: -5>') |
---|
2126 | n/a | |
---|
2127 | n/a | def test_or(self): |
---|
2128 | n/a | Perm = self.Perm |
---|
2129 | n/a | for i in Perm: |
---|
2130 | n/a | for j in Perm: |
---|
2131 | n/a | self.assertEqual(i | j, i.value | j.value) |
---|
2132 | n/a | self.assertEqual((i | j).value, i.value | j.value) |
---|
2133 | n/a | self.assertIs(type(i | j), Perm) |
---|
2134 | n/a | for j in range(8): |
---|
2135 | n/a | self.assertEqual(i | j, i.value | j) |
---|
2136 | n/a | self.assertEqual((i | j).value, i.value | j) |
---|
2137 | n/a | self.assertIs(type(i | j), Perm) |
---|
2138 | n/a | self.assertEqual(j | i, j | i.value) |
---|
2139 | n/a | self.assertEqual((j | i).value, j | i.value) |
---|
2140 | n/a | self.assertIs(type(j | i), Perm) |
---|
2141 | n/a | for i in Perm: |
---|
2142 | n/a | self.assertIs(i | i, i) |
---|
2143 | n/a | self.assertIs(i | 0, i) |
---|
2144 | n/a | self.assertIs(0 | i, i) |
---|
2145 | n/a | Open = self.Open |
---|
2146 | n/a | self.assertIs(Open.RO | Open.CE, Open.CE) |
---|
2147 | n/a | |
---|
2148 | n/a | def test_and(self): |
---|
2149 | n/a | Perm = self.Perm |
---|
2150 | n/a | RW = Perm.R | Perm.W |
---|
2151 | n/a | RX = Perm.R | Perm.X |
---|
2152 | n/a | WX = Perm.W | Perm.X |
---|
2153 | n/a | RWX = Perm.R | Perm.W | Perm.X |
---|
2154 | n/a | values = list(Perm) + [RW, RX, WX, RWX, Perm(0)] |
---|
2155 | n/a | for i in values: |
---|
2156 | n/a | for j in values: |
---|
2157 | n/a | self.assertEqual(i & j, i.value & j.value, 'i is %r, j is %r' % (i, j)) |
---|
2158 | n/a | self.assertEqual((i & j).value, i.value & j.value, 'i is %r, j is %r' % (i, j)) |
---|
2159 | n/a | self.assertIs(type(i & j), Perm, 'i is %r, j is %r' % (i, j)) |
---|
2160 | n/a | for j in range(8): |
---|
2161 | n/a | self.assertEqual(i & j, i.value & j) |
---|
2162 | n/a | self.assertEqual((i & j).value, i.value & j) |
---|
2163 | n/a | self.assertIs(type(i & j), Perm) |
---|
2164 | n/a | self.assertEqual(j & i, j & i.value) |
---|
2165 | n/a | self.assertEqual((j & i).value, j & i.value) |
---|
2166 | n/a | self.assertIs(type(j & i), Perm) |
---|
2167 | n/a | for i in Perm: |
---|
2168 | n/a | self.assertIs(i & i, i) |
---|
2169 | n/a | self.assertIs(i & 7, i) |
---|
2170 | n/a | self.assertIs(7 & i, i) |
---|
2171 | n/a | Open = self.Open |
---|
2172 | n/a | self.assertIs(Open.RO & Open.CE, Open.RO) |
---|
2173 | n/a | |
---|
2174 | n/a | def test_xor(self): |
---|
2175 | n/a | Perm = self.Perm |
---|
2176 | n/a | for i in Perm: |
---|
2177 | n/a | for j in Perm: |
---|
2178 | n/a | self.assertEqual(i ^ j, i.value ^ j.value) |
---|
2179 | n/a | self.assertEqual((i ^ j).value, i.value ^ j.value) |
---|
2180 | n/a | self.assertIs(type(i ^ j), Perm) |
---|
2181 | n/a | for j in range(8): |
---|
2182 | n/a | self.assertEqual(i ^ j, i.value ^ j) |
---|
2183 | n/a | self.assertEqual((i ^ j).value, i.value ^ j) |
---|
2184 | n/a | self.assertIs(type(i ^ j), Perm) |
---|
2185 | n/a | self.assertEqual(j ^ i, j ^ i.value) |
---|
2186 | n/a | self.assertEqual((j ^ i).value, j ^ i.value) |
---|
2187 | n/a | self.assertIs(type(j ^ i), Perm) |
---|
2188 | n/a | for i in Perm: |
---|
2189 | n/a | self.assertIs(i ^ 0, i) |
---|
2190 | n/a | self.assertIs(0 ^ i, i) |
---|
2191 | n/a | Open = self.Open |
---|
2192 | n/a | self.assertIs(Open.RO ^ Open.CE, Open.CE) |
---|
2193 | n/a | self.assertIs(Open.CE ^ Open.CE, Open.RO) |
---|
2194 | n/a | |
---|
2195 | n/a | def test_invert(self): |
---|
2196 | n/a | Perm = self.Perm |
---|
2197 | n/a | RW = Perm.R | Perm.W |
---|
2198 | n/a | RX = Perm.R | Perm.X |
---|
2199 | n/a | WX = Perm.W | Perm.X |
---|
2200 | n/a | RWX = Perm.R | Perm.W | Perm.X |
---|
2201 | n/a | values = list(Perm) + [RW, RX, WX, RWX, Perm(0)] |
---|
2202 | n/a | for i in values: |
---|
2203 | n/a | self.assertEqual(~i, ~i.value) |
---|
2204 | n/a | self.assertEqual((~i).value, ~i.value) |
---|
2205 | n/a | self.assertIs(type(~i), Perm) |
---|
2206 | n/a | self.assertEqual(~~i, i) |
---|
2207 | n/a | for i in Perm: |
---|
2208 | n/a | self.assertIs(~~i, i) |
---|
2209 | n/a | Open = self.Open |
---|
2210 | n/a | self.assertIs(Open.WO & ~Open.WO, Open.RO) |
---|
2211 | n/a | self.assertIs((Open.WO|Open.CE) & ~Open.WO, Open.CE) |
---|
2212 | n/a | |
---|
2213 | n/a | def test_programatic_function_string(self): |
---|
2214 | n/a | Perm = IntFlag('Perm', 'R W X') |
---|
2215 | n/a | lst = list(Perm) |
---|
2216 | n/a | self.assertEqual(len(lst), len(Perm)) |
---|
2217 | n/a | self.assertEqual(len(Perm), 3, Perm) |
---|
2218 | n/a | self.assertEqual(lst, [Perm.R, Perm.W, Perm.X]) |
---|
2219 | n/a | for i, n in enumerate('R W X'.split()): |
---|
2220 | n/a | v = 1<<i |
---|
2221 | n/a | e = Perm(v) |
---|
2222 | n/a | self.assertEqual(e.value, v) |
---|
2223 | n/a | self.assertEqual(type(e.value), int) |
---|
2224 | n/a | self.assertEqual(e, v) |
---|
2225 | n/a | self.assertEqual(e.name, n) |
---|
2226 | n/a | self.assertIn(e, Perm) |
---|
2227 | n/a | self.assertIs(type(e), Perm) |
---|
2228 | n/a | |
---|
2229 | n/a | def test_programatic_function_string_with_start(self): |
---|
2230 | n/a | Perm = IntFlag('Perm', 'R W X', start=8) |
---|
2231 | n/a | lst = list(Perm) |
---|
2232 | n/a | self.assertEqual(len(lst), len(Perm)) |
---|
2233 | n/a | self.assertEqual(len(Perm), 3, Perm) |
---|
2234 | n/a | self.assertEqual(lst, [Perm.R, Perm.W, Perm.X]) |
---|
2235 | n/a | for i, n in enumerate('R W X'.split()): |
---|
2236 | n/a | v = 8<<i |
---|
2237 | n/a | e = Perm(v) |
---|
2238 | n/a | self.assertEqual(e.value, v) |
---|
2239 | n/a | self.assertEqual(type(e.value), int) |
---|
2240 | n/a | self.assertEqual(e, v) |
---|
2241 | n/a | self.assertEqual(e.name, n) |
---|
2242 | n/a | self.assertIn(e, Perm) |
---|
2243 | n/a | self.assertIs(type(e), Perm) |
---|
2244 | n/a | |
---|
2245 | n/a | def test_programatic_function_string_list(self): |
---|
2246 | n/a | Perm = IntFlag('Perm', ['R', 'W', 'X']) |
---|
2247 | n/a | lst = list(Perm) |
---|
2248 | n/a | self.assertEqual(len(lst), len(Perm)) |
---|
2249 | n/a | self.assertEqual(len(Perm), 3, Perm) |
---|
2250 | n/a | self.assertEqual(lst, [Perm.R, Perm.W, Perm.X]) |
---|
2251 | n/a | for i, n in enumerate('R W X'.split()): |
---|
2252 | n/a | v = 1<<i |
---|
2253 | n/a | e = Perm(v) |
---|
2254 | n/a | self.assertEqual(e.value, v) |
---|
2255 | n/a | self.assertEqual(type(e.value), int) |
---|
2256 | n/a | self.assertEqual(e, v) |
---|
2257 | n/a | self.assertEqual(e.name, n) |
---|
2258 | n/a | self.assertIn(e, Perm) |
---|
2259 | n/a | self.assertIs(type(e), Perm) |
---|
2260 | n/a | |
---|
2261 | n/a | def test_programatic_function_iterable(self): |
---|
2262 | n/a | Perm = IntFlag('Perm', (('R', 2), ('W', 8), ('X', 32))) |
---|
2263 | n/a | lst = list(Perm) |
---|
2264 | n/a | self.assertEqual(len(lst), len(Perm)) |
---|
2265 | n/a | self.assertEqual(len(Perm), 3, Perm) |
---|
2266 | n/a | self.assertEqual(lst, [Perm.R, Perm.W, Perm.X]) |
---|
2267 | n/a | for i, n in enumerate('R W X'.split()): |
---|
2268 | n/a | v = 1<<(2*i+1) |
---|
2269 | n/a | e = Perm(v) |
---|
2270 | n/a | self.assertEqual(e.value, v) |
---|
2271 | n/a | self.assertEqual(type(e.value), int) |
---|
2272 | n/a | self.assertEqual(e, v) |
---|
2273 | n/a | self.assertEqual(e.name, n) |
---|
2274 | n/a | self.assertIn(e, Perm) |
---|
2275 | n/a | self.assertIs(type(e), Perm) |
---|
2276 | n/a | |
---|
2277 | n/a | def test_programatic_function_from_dict(self): |
---|
2278 | n/a | Perm = IntFlag('Perm', OrderedDict((('R', 2), ('W', 8), ('X', 32)))) |
---|
2279 | n/a | lst = list(Perm) |
---|
2280 | n/a | self.assertEqual(len(lst), len(Perm)) |
---|
2281 | n/a | self.assertEqual(len(Perm), 3, Perm) |
---|
2282 | n/a | self.assertEqual(lst, [Perm.R, Perm.W, Perm.X]) |
---|
2283 | n/a | for i, n in enumerate('R W X'.split()): |
---|
2284 | n/a | v = 1<<(2*i+1) |
---|
2285 | n/a | e = Perm(v) |
---|
2286 | n/a | self.assertEqual(e.value, v) |
---|
2287 | n/a | self.assertEqual(type(e.value), int) |
---|
2288 | n/a | self.assertEqual(e, v) |
---|
2289 | n/a | self.assertEqual(e.name, n) |
---|
2290 | n/a | self.assertIn(e, Perm) |
---|
2291 | n/a | self.assertIs(type(e), Perm) |
---|
2292 | n/a | |
---|
2293 | n/a | |
---|
2294 | n/a | def test_containment(self): |
---|
2295 | n/a | Perm = self.Perm |
---|
2296 | n/a | R, W, X = Perm |
---|
2297 | n/a | RW = R | W |
---|
2298 | n/a | RX = R | X |
---|
2299 | n/a | WX = W | X |
---|
2300 | n/a | RWX = R | W | X |
---|
2301 | n/a | self.assertTrue(R in RW) |
---|
2302 | n/a | self.assertTrue(R in RX) |
---|
2303 | n/a | self.assertTrue(R in RWX) |
---|
2304 | n/a | self.assertTrue(W in RW) |
---|
2305 | n/a | self.assertTrue(W in WX) |
---|
2306 | n/a | self.assertTrue(W in RWX) |
---|
2307 | n/a | self.assertTrue(X in RX) |
---|
2308 | n/a | self.assertTrue(X in WX) |
---|
2309 | n/a | self.assertTrue(X in RWX) |
---|
2310 | n/a | self.assertFalse(R in WX) |
---|
2311 | n/a | self.assertFalse(W in RX) |
---|
2312 | n/a | self.assertFalse(X in RW) |
---|
2313 | n/a | |
---|
2314 | n/a | def test_bool(self): |
---|
2315 | n/a | Perm = self.Perm |
---|
2316 | n/a | for f in Perm: |
---|
2317 | n/a | self.assertTrue(f) |
---|
2318 | n/a | Open = self.Open |
---|
2319 | n/a | for f in Open: |
---|
2320 | n/a | self.assertEqual(bool(f.value), bool(f)) |
---|
2321 | n/a | |
---|
2322 | n/a | @unittest.skipUnless(threading, 'Threading required for this test.') |
---|
2323 | n/a | @support.reap_threads |
---|
2324 | n/a | def test_unique_composite(self): |
---|
2325 | n/a | # override __eq__ to be identity only |
---|
2326 | n/a | class TestFlag(IntFlag): |
---|
2327 | n/a | one = auto() |
---|
2328 | n/a | two = auto() |
---|
2329 | n/a | three = auto() |
---|
2330 | n/a | four = auto() |
---|
2331 | n/a | five = auto() |
---|
2332 | n/a | six = auto() |
---|
2333 | n/a | seven = auto() |
---|
2334 | n/a | eight = auto() |
---|
2335 | n/a | def __eq__(self, other): |
---|
2336 | n/a | return self is other |
---|
2337 | n/a | def __hash__(self): |
---|
2338 | n/a | return hash(self._value_) |
---|
2339 | n/a | # have multiple threads competing to complete the composite members |
---|
2340 | n/a | seen = set() |
---|
2341 | n/a | failed = False |
---|
2342 | n/a | def cycle_enum(): |
---|
2343 | n/a | nonlocal failed |
---|
2344 | n/a | try: |
---|
2345 | n/a | for i in range(256): |
---|
2346 | n/a | seen.add(TestFlag(i)) |
---|
2347 | n/a | except Exception: |
---|
2348 | n/a | failed = True |
---|
2349 | n/a | threads = [ |
---|
2350 | n/a | threading.Thread(target=cycle_enum) |
---|
2351 | n/a | for _ in range(8) |
---|
2352 | n/a | ] |
---|
2353 | n/a | with support.start_threads(threads): |
---|
2354 | n/a | pass |
---|
2355 | n/a | # check that only 248 members were created |
---|
2356 | n/a | self.assertFalse( |
---|
2357 | n/a | failed, |
---|
2358 | n/a | 'at least one thread failed while creating composite members') |
---|
2359 | n/a | self.assertEqual(256, len(seen), 'too many composite members created') |
---|
2360 | n/a | |
---|
2361 | n/a | |
---|
2362 | n/a | class TestUnique(unittest.TestCase): |
---|
2363 | n/a | |
---|
2364 | n/a | def test_unique_clean(self): |
---|
2365 | n/a | @unique |
---|
2366 | n/a | class Clean(Enum): |
---|
2367 | n/a | one = 1 |
---|
2368 | n/a | two = 'dos' |
---|
2369 | n/a | tres = 4.0 |
---|
2370 | n/a | @unique |
---|
2371 | n/a | class Cleaner(IntEnum): |
---|
2372 | n/a | single = 1 |
---|
2373 | n/a | double = 2 |
---|
2374 | n/a | triple = 3 |
---|
2375 | n/a | |
---|
2376 | n/a | def test_unique_dirty(self): |
---|
2377 | n/a | with self.assertRaisesRegex(ValueError, 'tres.*one'): |
---|
2378 | n/a | @unique |
---|
2379 | n/a | class Dirty(Enum): |
---|
2380 | n/a | one = 1 |
---|
2381 | n/a | two = 'dos' |
---|
2382 | n/a | tres = 1 |
---|
2383 | n/a | with self.assertRaisesRegex( |
---|
2384 | n/a | ValueError, |
---|
2385 | n/a | 'double.*single.*turkey.*triple', |
---|
2386 | n/a | ): |
---|
2387 | n/a | @unique |
---|
2388 | n/a | class Dirtier(IntEnum): |
---|
2389 | n/a | single = 1 |
---|
2390 | n/a | double = 1 |
---|
2391 | n/a | triple = 3 |
---|
2392 | n/a | turkey = 3 |
---|
2393 | n/a | |
---|
2394 | n/a | def test_unique_with_name(self): |
---|
2395 | n/a | @unique |
---|
2396 | n/a | class Silly(Enum): |
---|
2397 | n/a | one = 1 |
---|
2398 | n/a | two = 'dos' |
---|
2399 | n/a | name = 3 |
---|
2400 | n/a | @unique |
---|
2401 | n/a | class Sillier(IntEnum): |
---|
2402 | n/a | single = 1 |
---|
2403 | n/a | name = 2 |
---|
2404 | n/a | triple = 3 |
---|
2405 | n/a | value = 4 |
---|
2406 | n/a | |
---|
2407 | n/a | |
---|
2408 | n/a | expected_help_output_with_docs = """\ |
---|
2409 | n/a | Help on class Color in module %s: |
---|
2410 | n/a | |
---|
2411 | n/a | class Color(enum.Enum) |
---|
2412 | n/a | | Color(value, names=None, *, module=None, qualname=None, type=None, start=1) |
---|
2413 | n/a | |\x20\x20 |
---|
2414 | n/a | | An enumeration. |
---|
2415 | n/a | |\x20\x20 |
---|
2416 | n/a | | Method resolution order: |
---|
2417 | n/a | | Color |
---|
2418 | n/a | | enum.Enum |
---|
2419 | n/a | | builtins.object |
---|
2420 | n/a | |\x20\x20 |
---|
2421 | n/a | | Data and other attributes defined here: |
---|
2422 | n/a | |\x20\x20 |
---|
2423 | n/a | | blue = <Color.blue: 3> |
---|
2424 | n/a | |\x20\x20 |
---|
2425 | n/a | | green = <Color.green: 2> |
---|
2426 | n/a | |\x20\x20 |
---|
2427 | n/a | | red = <Color.red: 1> |
---|
2428 | n/a | |\x20\x20 |
---|
2429 | n/a | | ---------------------------------------------------------------------- |
---|
2430 | n/a | | Data descriptors inherited from enum.Enum: |
---|
2431 | n/a | |\x20\x20 |
---|
2432 | n/a | | name |
---|
2433 | n/a | | The name of the Enum member. |
---|
2434 | n/a | |\x20\x20 |
---|
2435 | n/a | | value |
---|
2436 | n/a | | The value of the Enum member. |
---|
2437 | n/a | |\x20\x20 |
---|
2438 | n/a | | ---------------------------------------------------------------------- |
---|
2439 | n/a | | Data descriptors inherited from enum.EnumMeta: |
---|
2440 | n/a | |\x20\x20 |
---|
2441 | n/a | | __members__ |
---|
2442 | n/a | | Returns a mapping of member name->value. |
---|
2443 | n/a | |\x20\x20\x20\x20\x20\x20 |
---|
2444 | n/a | | This mapping lists all enum members, including aliases. Note that this |
---|
2445 | n/a | | is a read-only view of the internal mapping.""" |
---|
2446 | n/a | |
---|
2447 | n/a | expected_help_output_without_docs = """\ |
---|
2448 | n/a | Help on class Color in module %s: |
---|
2449 | n/a | |
---|
2450 | n/a | class Color(enum.Enum) |
---|
2451 | n/a | | Color(value, names=None, *, module=None, qualname=None, type=None, start=1) |
---|
2452 | n/a | |\x20\x20 |
---|
2453 | n/a | | Method resolution order: |
---|
2454 | n/a | | Color |
---|
2455 | n/a | | enum.Enum |
---|
2456 | n/a | | builtins.object |
---|
2457 | n/a | |\x20\x20 |
---|
2458 | n/a | | Data and other attributes defined here: |
---|
2459 | n/a | |\x20\x20 |
---|
2460 | n/a | | blue = <Color.blue: 3> |
---|
2461 | n/a | |\x20\x20 |
---|
2462 | n/a | | green = <Color.green: 2> |
---|
2463 | n/a | |\x20\x20 |
---|
2464 | n/a | | red = <Color.red: 1> |
---|
2465 | n/a | |\x20\x20 |
---|
2466 | n/a | | ---------------------------------------------------------------------- |
---|
2467 | n/a | | Data descriptors inherited from enum.Enum: |
---|
2468 | n/a | |\x20\x20 |
---|
2469 | n/a | | name |
---|
2470 | n/a | |\x20\x20 |
---|
2471 | n/a | | value |
---|
2472 | n/a | |\x20\x20 |
---|
2473 | n/a | | ---------------------------------------------------------------------- |
---|
2474 | n/a | | Data descriptors inherited from enum.EnumMeta: |
---|
2475 | n/a | |\x20\x20 |
---|
2476 | n/a | | __members__""" |
---|
2477 | n/a | |
---|
2478 | n/a | class TestStdLib(unittest.TestCase): |
---|
2479 | n/a | |
---|
2480 | n/a | maxDiff = None |
---|
2481 | n/a | |
---|
2482 | n/a | class Color(Enum): |
---|
2483 | n/a | red = 1 |
---|
2484 | n/a | green = 2 |
---|
2485 | n/a | blue = 3 |
---|
2486 | n/a | |
---|
2487 | n/a | def test_pydoc(self): |
---|
2488 | n/a | # indirectly test __objclass__ |
---|
2489 | n/a | if StrEnum.__doc__ is None: |
---|
2490 | n/a | expected_text = expected_help_output_without_docs % __name__ |
---|
2491 | n/a | else: |
---|
2492 | n/a | expected_text = expected_help_output_with_docs % __name__ |
---|
2493 | n/a | output = StringIO() |
---|
2494 | n/a | helper = pydoc.Helper(output=output) |
---|
2495 | n/a | helper(self.Color) |
---|
2496 | n/a | result = output.getvalue().strip() |
---|
2497 | n/a | self.assertEqual(result, expected_text) |
---|
2498 | n/a | |
---|
2499 | n/a | def test_inspect_getmembers(self): |
---|
2500 | n/a | values = dict(( |
---|
2501 | n/a | ('__class__', EnumMeta), |
---|
2502 | n/a | ('__doc__', 'An enumeration.'), |
---|
2503 | n/a | ('__members__', self.Color.__members__), |
---|
2504 | n/a | ('__module__', __name__), |
---|
2505 | n/a | ('blue', self.Color.blue), |
---|
2506 | n/a | ('green', self.Color.green), |
---|
2507 | n/a | ('name', Enum.__dict__['name']), |
---|
2508 | n/a | ('red', self.Color.red), |
---|
2509 | n/a | ('value', Enum.__dict__['value']), |
---|
2510 | n/a | )) |
---|
2511 | n/a | result = dict(inspect.getmembers(self.Color)) |
---|
2512 | n/a | self.assertEqual(values.keys(), result.keys()) |
---|
2513 | n/a | failed = False |
---|
2514 | n/a | for k in values.keys(): |
---|
2515 | n/a | if result[k] != values[k]: |
---|
2516 | n/a | print() |
---|
2517 | n/a | print('\n%s\n key: %s\n result: %s\nexpected: %s\n%s\n' % |
---|
2518 | n/a | ('=' * 75, k, result[k], values[k], '=' * 75), sep='') |
---|
2519 | n/a | failed = True |
---|
2520 | n/a | if failed: |
---|
2521 | n/a | self.fail("result does not equal expected, see print above") |
---|
2522 | n/a | |
---|
2523 | n/a | def test_inspect_classify_class_attrs(self): |
---|
2524 | n/a | # indirectly test __objclass__ |
---|
2525 | n/a | from inspect import Attribute |
---|
2526 | n/a | values = [ |
---|
2527 | n/a | Attribute(name='__class__', kind='data', |
---|
2528 | n/a | defining_class=object, object=EnumMeta), |
---|
2529 | n/a | Attribute(name='__doc__', kind='data', |
---|
2530 | n/a | defining_class=self.Color, object='An enumeration.'), |
---|
2531 | n/a | Attribute(name='__members__', kind='property', |
---|
2532 | n/a | defining_class=EnumMeta, object=EnumMeta.__members__), |
---|
2533 | n/a | Attribute(name='__module__', kind='data', |
---|
2534 | n/a | defining_class=self.Color, object=__name__), |
---|
2535 | n/a | Attribute(name='blue', kind='data', |
---|
2536 | n/a | defining_class=self.Color, object=self.Color.blue), |
---|
2537 | n/a | Attribute(name='green', kind='data', |
---|
2538 | n/a | defining_class=self.Color, object=self.Color.green), |
---|
2539 | n/a | Attribute(name='red', kind='data', |
---|
2540 | n/a | defining_class=self.Color, object=self.Color.red), |
---|
2541 | n/a | Attribute(name='name', kind='data', |
---|
2542 | n/a | defining_class=Enum, object=Enum.__dict__['name']), |
---|
2543 | n/a | Attribute(name='value', kind='data', |
---|
2544 | n/a | defining_class=Enum, object=Enum.__dict__['value']), |
---|
2545 | n/a | ] |
---|
2546 | n/a | values.sort(key=lambda item: item.name) |
---|
2547 | n/a | result = list(inspect.classify_class_attrs(self.Color)) |
---|
2548 | n/a | result.sort(key=lambda item: item.name) |
---|
2549 | n/a | failed = False |
---|
2550 | n/a | for v, r in zip(values, result): |
---|
2551 | n/a | if r != v: |
---|
2552 | n/a | print('\n%s\n%s\n%s\n%s\n' % ('=' * 75, r, v, '=' * 75), sep='') |
---|
2553 | n/a | failed = True |
---|
2554 | n/a | if failed: |
---|
2555 | n/a | self.fail("result does not equal expected, see print above") |
---|
2556 | n/a | |
---|
2557 | n/a | |
---|
2558 | n/a | class MiscTestCase(unittest.TestCase): |
---|
2559 | n/a | def test__all__(self): |
---|
2560 | n/a | support.check__all__(self, enum) |
---|
2561 | n/a | |
---|
2562 | n/a | |
---|
2563 | n/a | # These are unordered here on purpose to ensure that declaration order |
---|
2564 | n/a | # makes no difference. |
---|
2565 | n/a | CONVERT_TEST_NAME_D = 5 |
---|
2566 | n/a | CONVERT_TEST_NAME_C = 5 |
---|
2567 | n/a | CONVERT_TEST_NAME_B = 5 |
---|
2568 | n/a | CONVERT_TEST_NAME_A = 5 # This one should sort first. |
---|
2569 | n/a | CONVERT_TEST_NAME_E = 5 |
---|
2570 | n/a | CONVERT_TEST_NAME_F = 5 |
---|
2571 | n/a | |
---|
2572 | n/a | class TestIntEnumConvert(unittest.TestCase): |
---|
2573 | n/a | def test_convert_value_lookup_priority(self): |
---|
2574 | n/a | test_type = enum.IntEnum._convert( |
---|
2575 | n/a | 'UnittestConvert', |
---|
2576 | n/a | ('test.test_enum', '__main__')[__name__=='__main__'], |
---|
2577 | n/a | filter=lambda x: x.startswith('CONVERT_TEST_')) |
---|
2578 | n/a | # We don't want the reverse lookup value to vary when there are |
---|
2579 | n/a | # multiple possible names for a given value. It should always |
---|
2580 | n/a | # report the first lexigraphical name in that case. |
---|
2581 | n/a | self.assertEqual(test_type(5).name, 'CONVERT_TEST_NAME_A') |
---|
2582 | n/a | |
---|
2583 | n/a | def test_convert(self): |
---|
2584 | n/a | test_type = enum.IntEnum._convert( |
---|
2585 | n/a | 'UnittestConvert', |
---|
2586 | n/a | ('test.test_enum', '__main__')[__name__=='__main__'], |
---|
2587 | n/a | filter=lambda x: x.startswith('CONVERT_TEST_')) |
---|
2588 | n/a | # Ensure that test_type has all of the desired names and values. |
---|
2589 | n/a | self.assertEqual(test_type.CONVERT_TEST_NAME_F, |
---|
2590 | n/a | test_type.CONVERT_TEST_NAME_A) |
---|
2591 | n/a | self.assertEqual(test_type.CONVERT_TEST_NAME_B, 5) |
---|
2592 | n/a | self.assertEqual(test_type.CONVERT_TEST_NAME_C, 5) |
---|
2593 | n/a | self.assertEqual(test_type.CONVERT_TEST_NAME_D, 5) |
---|
2594 | n/a | self.assertEqual(test_type.CONVERT_TEST_NAME_E, 5) |
---|
2595 | n/a | # Ensure that test_type only picked up names matching the filter. |
---|
2596 | n/a | self.assertEqual([name for name in dir(test_type) |
---|
2597 | n/a | if name[0:2] not in ('CO', '__')], |
---|
2598 | n/a | [], msg='Names other than CONVERT_TEST_* found.') |
---|
2599 | n/a | |
---|
2600 | n/a | |
---|
2601 | n/a | if __name__ == '__main__': |
---|
2602 | n/a | unittest.main() |
---|