Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(160778)

Side by Side Diff: Lib/test/test_enum.py

Issue 17947: Code, test, and doc review for PEP-0435 Enum
Patch Set: Created 6 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« Lib/enum.py ('K') | « Lib/enum.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 from collections import OrderedDict
2 from pickle import dumps, loads
3 import unittest
4 from enum import Enum, IntEnum
5 import sys
6
7
8 # for pickle tests
9 _errors = []
10 try:
11 class Stooges(Enum):
12 LARRY = 1
13 CURLY = 2
14 MOE = 3
15 except Exception as exc:
16 _errors.append(exc)
17
18 try:
19 class IntStooges(int, Enum):
20 LARRY = 1
21 CURLY = 2
22 MOE = 3
23 except Exception as exc:
24 _errors.append(exc)
25
26 try:
27 class FloatStooges(float, Enum):
28 LARRY = 1.39
29 CURLY = 2.72
30 MOE = 3.142596
31 except Exception as exc:
32 _errors.append(exc)
33
34 # for pickle test and subclass tests
35 try:
36 class StrEnum(str, Enum):
37 'accepts only string values'
38 class Name(StrEnum):
39 BDFL = 'Guido van Rossum'
40 FLUFL = 'Barry Warsaw'
41 except Exception as exc:
42 _errors.append(exc)
43
44 try:
45 Question = Enum('Question', 'who what when where why', module='__main__')
46 except Exception as exc:
47 _errors.append(exc)
48
49 try:
50 Answer = Enum('Answer', 'him this then there because')
51 except Exception as exc:
52 _errors.append(exc)
53
54 class Test_Enum(unittest.TestCase):
ezio.melotti 2013/05/10 20:34:34 TestEnum
stoneleaf 2013/05/12 14:47:01 Done.
55 def setUp(self):
56 class Season(Enum):
57 SPRING = 1
58 SUMMER = 2
59 AUTUMN = 3
60 WINTER = 4
61 self.Season = Season
62
63 def test_global_class_creation(self):
64 self.assertFalse(_errors)
Zach Ware 2013/05/10 22:43:10 This is better, but now any exceptions that are ra
stoneleaf 2013/05/12 14:47:01 Saving exceptions and reraising them in the approp
65
66 def test_enum_in_enum_out(self):
67 Season = self.Season
68 self.assertIs(Season(Season.WINTER), Season.WINTER)
69
70 def test_dir_on_class(self):
71 Season = self.Season
72 self.assertEqual(
73 set(dir(Season)),
74 set(['__class__', '__doc__', '__members__',
75 'SPRING', 'SUMMER', 'AUTUMN', 'WINTER']),
76 )
77
78 def test_dir_on_item(self):
79 Season = self.Season
80 self.assertEqual(
81 set(dir(Season.WINTER)),
82 set(['__class__', '__doc__', 'name', 'value']),
83 )
84
85 def test_enum(self):
86 Season = self.Season
87 lst = list(Season)
88 self.assertEqual(len(lst), len(Season))
89 self.assertEqual(len(Season), 4, Season)
90 self.assertEqual(
91 [Season.SPRING, Season.SUMMER, Season.AUTUMN, Season.WINTER], lst)
92
93 for i, season in enumerate('SPRING SUMMER AUTUMN WINTER'.split(), 1):
94 e = Season(i)
95 self.assertEqual(e, getattr(Season, season))
96 self.assertEqual(e.value, i)
97 self.assertNotEqual(e, i)
98 self.assertEqual(e.name, season)
99 self.assertIn(e, Season)
100 self.assertTrue(type(e) is Season)
ezio.melotti 2013/05/10 20:34:34 assertIs
stoneleaf 2013/05/12 14:47:01 Done.
101 self.assertIsInstance(e, Season)
102 self.assertEqual(str(e), 'Season.' + season)
103 self.assertEqual(repr(e),
104 '<Season.{0}: {1}>'.format(season, i))
105
106 def test_value_name(self):
107 Season = self.Season
108 self.assertEqual(Season.SPRING.name, 'SPRING')
109 self.assertEqual(Season.SPRING.value, 1)
110 with self.assertRaises(AttributeError):
111 Season.SPRING.name = 'invierno'
112 with self.assertRaises(AttributeError):
113 Season.SPRING.value = 2
114
115 def test_contains(self):
116 Season = self.Season
117 self.assertIn(Season.AUTUMN, Season)
118 self.assertNotIn(3, Season)
119
120 val = Season(3)
121 self.assertIn(val, Season)
122
123 class OtherEnum(Enum):
124 one = 1; two = 2
125 self.assertNotIn(OtherEnum.two, Season)
126
127 def test_comparisons(self):
128 Season = self.Season
129 with self.assertRaises(TypeError):
130 Season.SPRING < Season.WINTER
131 with self.assertRaises(TypeError):
132 Season.SPRING > 4
133
134 self.assertNotEqual(Season.SPRING, 1)
135
136 class Part(Enum):
137 SPRING = 1
138 CLIP = 2
139 BARREL = 3
140
141 self.assertNotEqual(Season.SPRING, Part.SPRING)
142 with self.assertRaises(TypeError):
143 Season.SPRING < Part.CLIP
144
145 def test_enum_duplicates(self):
146 class Season(Enum):
147 SPRING = 1
148 SUMMER = 2
149 AUTUMN = FALL = 3
150 WINTER = 4
151 ANOTHER_SPRING = 1
152 lst = list(Season)
153 self.assertEqual(
154 lst,
155 [Season.SPRING, Season.SUMMER,
156 Season.AUTUMN, Season.WINTER,
157 ])
158 self.assertIs(Season.FALL, Season.AUTUMN)
159 self.assertEqual(Season.FALL.value, 3)
160 self.assertEqual(Season.AUTUMN.value, 3)
161 self.assertIs(Season(3), Season.AUTUMN)
162 self.assertIs(Season(1), Season.SPRING)
163 self.assertEqual(Season.FALL.name, 'AUTUMN')
164 self.assertEqual([k for k,v in Season.__members__.items() if v.name != k ], ['FALL', 'ANOTHER_SPRING'])
ezio.melotti 2013/05/10 20:34:34 Line too long.
stoneleaf 2013/05/12 14:47:01 Done.
165
166 def test_enum_with_value_name(self):
167 class Huh(Enum):
168 name = 1
169 value = 2
170 self.assertEqual(
171 list(Huh),
172 [Huh.name, Huh.value],
173 )
174 self.assertTrue(type(Huh.name) is Huh)
ezio.melotti 2013/05/10 20:34:34 assertIs
stoneleaf 2013/05/12 14:47:01 Done.
175 self.assertEqual(Huh.name.name, 'name')
176 self.assertEqual(Huh.name.value, 1)
177
178 def test_intenum(self):
179 class WeekDay(IntEnum):
180 SUNDAY = 1
181 MONDAY = 2
182 TUESDAY = 3
183 WEDNESDAY = 4
184 THURSDAY = 5
185 FRIDAY = 6
186 SATURDAY = 7
187
188 self.assertEqual(['a', 'b', 'c'][WeekDay.MONDAY], 'c')
189 self.assertEqual([i for i in range(WeekDay.TUESDAY)], [0, 1, 2])
190
191 lst = list(WeekDay)
192 self.assertEqual(len(lst), len(WeekDay))
193 self.assertEqual(len(WeekDay), 7)
194 for i, weekday in enumerate(r'''SUNDAY MONDAY TUESDAY WEDNESDAY
195 THURSDAY FRIDAY SATURDAY'''.split(), 1):
196 e = WeekDay(i)
197 self.assertEqual(e, i)
198 self.assertEqual(int(e), i)
199 self.assertEqual(e.name, weekday)
200 self.assertIn(e, WeekDay)
201 self.assertEqual(lst.index(e)+1, i)
202 self.assertTrue(0 < e < 8)
203 self.assertIs(type(e), WeekDay)
204 self.assertIsInstance(e, int)
205 self.assertIsInstance(e, Enum)
206
207 def test_intenum_duplicates(self):
208 class WeekDay(IntEnum):
209 SUNDAY = 1
210 MONDAY = 2
211 TUESDAY = TEUSDAY = 3
212 WEDNESDAY = 4
213 THURSDAY = 5
214 FRIDAY = 6
215 SATURDAY = 7
216 self.assertTrue(WeekDay.TEUSDAY is WeekDay.TUESDAY)
ezio.melotti 2013/05/10 20:34:34 assertIs
stoneleaf 2013/05/12 14:47:01 Done.
217 self.assertEqual(WeekDay(3).name, 'TUESDAY')
218 self.assertEqual([k for k,v in WeekDay.__members__.items() if v.name != k], ['TEUSDAY', ])
ezio.melotti 2013/05/10 20:34:34 Line too long.
stoneleaf 2013/05/12 14:47:01 Done.
219
220 def test_pickle_enum(self):
221 self.assertTrue(Stooges.CURLY is loads(dumps(Stooges.CURLY)))
222
223 def test_pickle_int(self):
224 self.assertTrue(IntStooges.CURLY is loads(dumps(IntStooges.CURLY)))
225
226 def test_pickle_float(self):
227 self.assertTrue(FloatStooges.CURLY is loads(dumps(FloatStooges.CURLY)))
ezio.melotti 2013/05/10 20:34:34 There are more assertTrue that should be converted
stoneleaf 2013/05/12 14:47:01 Done.
228
229 def test_pickle_enum_function(self):
230 self.assertIs(Answer.him, loads(dumps(Answer.him)))
231
232 def test_pickle_enum_function_with_module(self):
233 self.assertIs(Question.who, loads(dumps(Question.who)))
234
235 # not sure how to make this work; create the .py file somewhere importable?
236 # def test_pickle_enum_function_from_other_module(self):
237 # from test_module import Duh
238 # self.assertIs(Duh.umm, loads(dumps(Duh.umm)))
239
240 def test_string_enum(self):
241 class SkillLevel(str, Enum):
242 master = 'what is the sound of one hand clapping?'
243 journeyman = 'why did the chicken cross the road?'
244 apprentice = 'knock, knock!'
245 self.assertEqual(SkillLevel.apprentice, 'knock, knock!')
246
247 def test_getattr_getitem(self):
248 class Period(Enum):
249 morning = 1
250 noon = 2
251 evening = 3
252 night = 4
253 self.assertTrue(Period(2) is Period.noon)
254 self.assertTrue(getattr(Period, 'night') is Period.night)
255 self.assertTrue(Period['morning'] is Period.morning)
256
257 def test_getattr_dunder(self):
258 Season = self.Season
259 self.assertTrue(getattr(Season, '__eq__'))
260
261 def test_iteration_order(self):
262 class Season(Enum):
263 SUMMER = 2
264 WINTER = 4
265 AUTUMN = 3
266 SPRING = 1
267 self.assertEqual(
268 list(Season),
269 [Season.SUMMER, Season.WINTER, Season.AUTUMN, Season.SPRING],
270 )
271
272 def test_programatic_function_string(self):
273 SummerMonth = Enum('SummerMonth', 'june july august')
274 lst = list(SummerMonth)
275 self.assertEqual(len(lst), len(SummerMonth))
276 self.assertEqual(len(SummerMonth), 3, SummerMonth)
277 self.assertEqual([SummerMonth.june, SummerMonth.july, SummerMonth.august ], lst)
278 for i, month in enumerate('june july august'.split(), 1):
279 e = SummerMonth(i)
280 self.assertEqual(int(e.value), i)
281 self.assertNotEqual(e, i)
282 self.assertEqual(e.name, month)
283 self.assertIn(e, SummerMonth)
284 self.assertTrue(type(e) is SummerMonth)
285
286 def test_programatic_function_string_list(self):
287 SummerMonth = Enum('SummerMonth', ['june', 'july', 'august'])
288 lst = list(SummerMonth)
289 self.assertEqual(len(lst), len(SummerMonth))
290 self.assertEqual(len(SummerMonth), 3, SummerMonth)
291 self.assertEqual([SummerMonth.june, SummerMonth.july, SummerMonth.august ], lst)
292 for i, month in enumerate('june july august'.split(), 1):
293 e = SummerMonth(i)
294 self.assertEqual(int(e.value), i)
295 self.assertNotEqual(e, i)
296 self.assertEqual(e.name, month)
297 self.assertIn(e, SummerMonth)
298 self.assertTrue(type(e) is SummerMonth)
299
300 def test_programatic_function_iterable(self):
301 SummerMonth = Enum('SummerMonth', (('june', 1), ('july', 2), ('august', 3)))
302 lst = list(SummerMonth)
303 self.assertEqual(len(lst), len(SummerMonth))
304 self.assertEqual(len(SummerMonth), 3, SummerMonth)
305 self.assertEqual([SummerMonth.june, SummerMonth.july, SummerMonth.august ], lst)
306 for i, month in enumerate('june july august'.split(), 1):
307 e = SummerMonth(i)
308 self.assertEqual(int(e.value), i)
309 self.assertNotEqual(e, i)
310 self.assertEqual(e.name, month)
311 self.assertIn(e, SummerMonth)
312 self.assertTrue(type(e) is SummerMonth)
313
314 def test_programatic_function_from_dict(self):
315 SummerMonth = Enum('SummerMonth', OrderedDict((('june', 1), ('july', 2), ('august', 3))))
316 lst = list(SummerMonth)
317 self.assertEqual(len(lst), len(SummerMonth))
318 self.assertEqual(len(SummerMonth), 3, SummerMonth)
319 self.assertEqual([SummerMonth.june, SummerMonth.july, SummerMonth.august ], lst)
320 for i, month in enumerate('june july august'.split(), 1):
321 e = SummerMonth(i)
322 self.assertEqual(int(e.value), i)
323 self.assertNotEqual(e, i)
324 self.assertEqual(e.name, month)
325 self.assertIn(e, SummerMonth)
326 self.assertTrue(type(e) is SummerMonth)
327
328 def test_programatic_function_type(self):
329 SummerMonth = Enum('SummerMonth', 'june july august', type=int)
330 lst = list(SummerMonth)
331 self.assertEqual(len(lst), len(SummerMonth))
332 self.assertEqual(len(SummerMonth), 3, SummerMonth)
333 self.assertEqual([SummerMonth.june, SummerMonth.july, SummerMonth.august ], lst)
334 for i, month in enumerate('june july august'.split(), 1):
335 e = SummerMonth(i)
336 self.assertEqual(e, i)
337 self.assertEqual(e.name, month)
338 self.assertIn(e, SummerMonth)
339 self.assertTrue(type(e) is SummerMonth)
340
341 def test_programatic_function_type_from_subclass(self):
342 SummerMonth = IntEnum('SummerMonth', 'june july august')
343 lst = list(SummerMonth)
344 self.assertEqual(len(lst), len(SummerMonth))
345 self.assertEqual(len(SummerMonth), 3, SummerMonth)
346 self.assertEqual([SummerMonth.june, SummerMonth.july, SummerMonth.august ], lst)
347 for i, month in enumerate('june july august'.split(), 1):
348 e = SummerMonth(i)
349 self.assertEqual(e, i)
350 self.assertEqual(e.name, month)
351 self.assertIn(e, SummerMonth)
352 self.assertTrue(type(e) is SummerMonth)
353
354 def test_subclassing(self):
355 self.assertEqual(Name.BDFL, 'Guido van Rossum')
356 self.assertTrue(Name.BDFL, Name('Guido van Rossum'))
357 self.assertTrue(Name.BDFL is getattr(Name, 'BDFL'))
358 self.assertTrue(Name.BDFL is loads(dumps(Name.BDFL)))
359
360 def test_extending(self):
361 class Color(Enum):
362 red = 1
363 green = 2
364 blue = 3
365 with self.assertRaises(TypeError):
366 class MoreColor(Color):
367 cyan = 4
368 magenta = 5
369 yellow = 6
370
371 def test_exclude_methods(self):
372 class whatever(Enum):
373 this = 'that'
374 these = 'those'
375 def really(self):
376 return 'no, not %s' % self.value
377 self.assertFalse(type(whatever.really) is whatever)
ezio.melotti 2013/05/10 20:34:34 assertIsNot
stoneleaf 2013/05/12 14:47:01 Done.
378 self.assertEqual(whatever.this.really(), 'no, not that')
379
380 def test_overwrite_enums(self):
381 class Why(Enum):
382 question = 1
383 answer = 2
384 propisition = 3
385 def question(self):
386 print(42)
387 self.assertFalse(type(Why.question) is Why)
388 self.assertFalse(Why.question in Why._enum_names)
389 self.assertFalse(Why.question in Why)
ezio.melotti 2013/05/10 20:34:34 assertNotIn
stoneleaf 2013/05/12 14:47:01 Done.
390
391 def test_wrong_inheritance_order(self):
392 with self.assertRaises(TypeError):
393 class Wrong(Enum, str):
394 NotHere = 'error before this point'
395
396 def test_wrong_enum_in_call(self):
397 class Monochrome(Enum):
398 black = 0
399 white = 1
400 class Gender(Enum):
401 male = 0
402 female = 1
403 self.assertRaises(ValueError, Monochrome, Gender.male)
404
405 def test_wrong_enum_in_mixed_call(self):
406 class Monochrome(IntEnum):
407 black = 0
408 white = 1
409 class Gender(Enum):
410 male = 0
411 female = 1
412 self.assertRaises(ValueError, Monochrome, Gender.male)
413
414 def test_mixed_enum_in_call_1(self):
415 class Monochrome(IntEnum):
416 black = 0
417 white = 1
418 class Gender(IntEnum):
419 male = 0
420 female = 1
421 self.assertIs(Monochrome(Gender.female), Monochrome.white)
422
423 def test_mixed_enum_in_call_2(self):
424 class Monochrome(Enum):
425 black = 0
426 white = 1
427 class Gender(IntEnum):
428 male = 0
429 female = 1
430 self.assertIs(Monochrome(Gender.male), Monochrome.black)
431
432 def test_flufl_enum(self):
433 class Fluflnum(Enum):
434 def __int__(self):
435 return int(self.value)
436 class MailManOptions(Fluflnum):
437 option1 = 1
438 option2 = 2
439 option3 = 3
440 self.assertEqual(int(MailManOptions.option1), 1)
441
442 def test_no_such_enum_member(self):
443 class Color(Enum):
444 red = 1
445 green = 2
446 blue = 3
447 with self.assertRaises(ValueError):
448 Color(4)
449 with self.assertRaises(KeyError):
450 Color['chartreuse']
451
452 def test_new_repr(self):
453 class Color(Enum):
454 red = 1
455 green = 2
456 blue = 3
457 def __repr__(self):
458 return "don't you just love shades of %s?" % self.name
459 self.assertEqual(repr(Color.blue), "don't you just love shades of blue?" )
460
461 def test_inherited_repr(self):
462 class MyEnum(Enum):
463 def __repr__(self):
464 return "My name is %s." % self.name
465 class MyIntEnum(int, MyEnum):
466 this = 1
467 that = 2
468 theother = 3
469 self.assertEqual(repr(MyIntEnum.that), "My name is that.")
470
471 def test_multiple_mixin_mro(self):
472 class auto_enum(type(Enum)):
473 def __new__(metacls, cls, bases, classdict):
474 temp = type(classdict)()
475 names = set(classdict._enum_names)
476 i = 0
477 for k in classdict._enum_names:
478 v = classdict[k]
479 if v is Ellipsis:
480 v = i
481 else:
482 i = v
483 i += 1
484 temp[k] = v
485 for k, v in classdict.items():
486 if k not in names:
487 temp[k] = v
488 return super(auto_enum, metacls).__new__(metacls, cls, bases, te mp)
489
490 class AutoNumberedEnum(Enum, metaclass=auto_enum):
491 pass
492
493 class AutoIntEnum(IntEnum, metaclass=auto_enum):
494 pass
495
496 class TestAutoNumber(AutoNumberedEnum):
497 a = ...
498 b = 3
499 c = ...
500
501 class TestAutoInt(AutoIntEnum):
502 a = ...
503 b = 3
504 c = ...
505
506 def test_subclasses_with_getnewargs(self):
507 class NamedInt( int ):
508 def __new__( cls, *args, **kwds ):
509 _args = args
510 name, *args = args
511 if len( args ) == 0:
512 raise TypeError("name and value must be specified")
513 self = int.__new__( cls, *args, **kwds )
514 self._intname = name
515 return self
516 @property
517 def __name__( self ):
518 return self._intname
519 def __repr__( self ):
520 # repr() is updated to include the name and type info
521 return "{}({!r}, {})".format(type(self).__name__,
522 self.__name__,
523 int.__repr__(self))
524 def __str__( self ):
525 # str() is unchanged, even if it relies on the repr() fallback
526 base = int
527 base_str = base.__str__
528 if base_str.__objclass__ is object:
529 return base.__repr__(self)
530 return base_str(self)
531 # for simplicity, we only define one operator that propagates expres sions
532 def __add__(self, other):
533 temp = int( self ) + int( other )
534 if isinstance( self, NamedInt ) and isinstance( other, NamedInt ):
ezio.melotti 2013/05/10 20:34:34 There are extra spaces here.
stoneleaf 2013/05/12 14:47:01 Done.
535 return NamedInt(
536 '({0} + {1})'.format(self.__name__, other.__name__),
537 temp )
538 else:
539 return temp
540
541 class NEI( NamedInt, Enum ):
542 x = ('the-x', 1 )
543 y = ('the-y', 2 )
ezio.melotti 2013/05/10 20:34:34 Here too.
stoneleaf 2013/05/12 14:47:01 Done.
544
545 self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
546 globals()['NamedInt'] = NamedInt
547 globals()['NEI'] = NEI
548 self.assertIs(loads(dumps(NEI.y)), NEI.y)
549
550 def test_tuple_subclass(self):
551 class SomeTuple(tuple, Enum):
552 first = (1, 'for the money')
553 second = (2, 'for the show')
554 third = (3, 'for the music')
555 self.assertTrue(type(SomeTuple.first) is SomeTuple)
556 self.assertTrue(isinstance(SomeTuple.second, tuple))
ezio.melotti 2013/05/10 20:34:34 assertIsInstance
stoneleaf 2013/05/12 14:47:01 Done.
557 self.assertEqual(SomeTuple.third, (3, 'for the music'))
558 globals()['SomeTuple'] = SomeTuple
559 self.assertTrue(loads(dumps(SomeTuple.first)), SomeTuple.first)
ezio.melotti 2013/05/10 20:34:34 I think you want an assertIs here too (as it is, S
stoneleaf 2013/05/12 14:47:01 Done.
560
561 def test_duplicate_values_give_unique_enum_items(self):
562 class AutoNumber(Enum):
563 first = ()
564 second = ()
565 third = ()
566 def __new__(cls):
567 value = len(cls.__members__) + 1
568 obj = object.__new__(cls)
569 obj._value = value
570 return obj
571 def __int__(self):
572 return self.value
573 self.assertEqual(list(AutoNumber), [AutoNumber.first, AutoNumber.second, AutoNumber.third])
574 self.assertEqual(int(AutoNumber.second), 2)
575 self.assertTrue(AutoNumber(1) is AutoNumber.first)
576
577 def test_inherited_new_from_enhanced_enum(self):
578 class AutoNumber(Enum):
579 def __new__(cls):
580 value = len(cls.__members__) + 1
581 obj = object.__new__(cls)
582 obj._value = value
583 return obj
584 def __int__(self):
585 return self._value
586 class Color(AutoNumber):
587 red = ()
588 green = ()
589 blue = ()
590 self.assertEqual(list(Color), [Color.red, Color.green, Color.blue])
591 self.assertEqual(list(map(int, Color)), [1, 2, 3])
592
593 def test_inherited_new_from_mixed_enum(self):
594 class AutoNumber(IntEnum):
595 def __new__(cls):
596 value = len(cls.__members__) + 1
597 obj = int.__new__(cls, value)
598 obj._value = value
599 return obj
600 class Color(AutoNumber):
601 red = ()
602 green = ()
603 blue = ()
604 self.assertEqual(list(Color), [Color.red, Color.green, Color.blue])
605 self.assertEqual(list(map(int, Color)), [1, 2, 3])
606
607 def test_ordered_mixin(self):
608 class OrderedEnum(Enum):
609 def __ge__(self, other):
610 if self.__class__ is other.__class__:
611 return self._value >= other._value
612 return NotImplemented
613 def __gt__(self, other):
614 if self.__class__ is other.__class__:
615 return self._value > other._value
616 return NotImplemented
617 def __le__(self, other):
618 if self.__class__ is other.__class__:
619 return self._value <= other._value
620 return NotImplemented
621 def __lt__(self, other):
622 if self.__class__ is other.__class__:
623 return self._value < other._value
624 return NotImplemented
625 class Grade(OrderedEnum):
626 A = 5
627 B = 4
628 C = 3
629 D = 2
630 F = 1
631 self.assertTrue(Grade.A > Grade.B)
632 self.assertTrue(Grade.F <= Grade.C)
633 self.assertTrue(Grade.D < Grade.A)
634 self.assertTrue(Grade.B >= Grade.B)
ezio.melotti 2013/05/10 20:34:34 You can use assertGreater and friends here.
stoneleaf 2013/05/12 14:47:01 Done.
635
636
637 if __name__ == '__main__':
638 unittest.main()
OLDNEW
« Lib/enum.py ('K') | « Lib/enum.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+