Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1 | import enum |
| 2 | import unittest |
| 3 | from collections import OrderedDict |
| 4 | from pickle import dumps, loads, PicklingError |
Ethan Furman | f24bb35 | 2013-07-18 17:05:39 -0700 | [diff] [blame^] | 5 | from enum import Enum, IntEnum, unique |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 6 | |
| 7 | # for pickle tests |
| 8 | try: |
| 9 | class Stooges(Enum): |
| 10 | LARRY = 1 |
| 11 | CURLY = 2 |
| 12 | MOE = 3 |
| 13 | except Exception as exc: |
| 14 | Stooges = exc |
| 15 | |
| 16 | try: |
| 17 | class IntStooges(int, Enum): |
| 18 | LARRY = 1 |
| 19 | CURLY = 2 |
| 20 | MOE = 3 |
| 21 | except Exception as exc: |
| 22 | IntStooges = exc |
| 23 | |
| 24 | try: |
| 25 | class FloatStooges(float, Enum): |
| 26 | LARRY = 1.39 |
| 27 | CURLY = 2.72 |
| 28 | MOE = 3.142596 |
| 29 | except Exception as exc: |
| 30 | FloatStooges = exc |
| 31 | |
| 32 | # for pickle test and subclass tests |
| 33 | try: |
| 34 | class StrEnum(str, Enum): |
| 35 | 'accepts only string values' |
| 36 | class Name(StrEnum): |
| 37 | BDFL = 'Guido van Rossum' |
| 38 | FLUFL = 'Barry Warsaw' |
| 39 | except Exception as exc: |
| 40 | Name = exc |
| 41 | |
| 42 | try: |
| 43 | Question = Enum('Question', 'who what when where why', module=__name__) |
| 44 | except Exception as exc: |
| 45 | Question = exc |
| 46 | |
| 47 | try: |
| 48 | Answer = Enum('Answer', 'him this then there because') |
| 49 | except Exception as exc: |
| 50 | Answer = exc |
| 51 | |
| 52 | # for doctests |
| 53 | try: |
| 54 | class Fruit(Enum): |
| 55 | tomato = 1 |
| 56 | banana = 2 |
| 57 | cherry = 3 |
| 58 | except Exception: |
| 59 | pass |
| 60 | |
| 61 | class TestEnum(unittest.TestCase): |
| 62 | def setUp(self): |
| 63 | class Season(Enum): |
| 64 | SPRING = 1 |
| 65 | SUMMER = 2 |
| 66 | AUTUMN = 3 |
| 67 | WINTER = 4 |
| 68 | self.Season = Season |
| 69 | |
| 70 | def test_enum_in_enum_out(self): |
| 71 | Season = self.Season |
| 72 | self.assertIs(Season(Season.WINTER), Season.WINTER) |
| 73 | |
| 74 | def test_enum_value(self): |
| 75 | Season = self.Season |
| 76 | self.assertEqual(Season.SPRING.value, 1) |
| 77 | |
| 78 | def test_intenum_value(self): |
| 79 | self.assertEqual(IntStooges.CURLY.value, 2) |
| 80 | |
| 81 | def test_dir_on_class(self): |
| 82 | Season = self.Season |
| 83 | self.assertEqual( |
| 84 | set(dir(Season)), |
| 85 | set(['__class__', '__doc__', '__members__', |
| 86 | 'SPRING', 'SUMMER', 'AUTUMN', 'WINTER']), |
| 87 | ) |
| 88 | |
| 89 | def test_dir_on_item(self): |
| 90 | Season = self.Season |
| 91 | self.assertEqual( |
| 92 | set(dir(Season.WINTER)), |
| 93 | set(['__class__', '__doc__', 'name', 'value']), |
| 94 | ) |
| 95 | |
| 96 | def test_enum(self): |
| 97 | Season = self.Season |
| 98 | lst = list(Season) |
| 99 | self.assertEqual(len(lst), len(Season)) |
| 100 | self.assertEqual(len(Season), 4, Season) |
| 101 | self.assertEqual( |
| 102 | [Season.SPRING, Season.SUMMER, Season.AUTUMN, Season.WINTER], lst) |
| 103 | |
| 104 | for i, season in enumerate('SPRING SUMMER AUTUMN WINTER'.split(), 1): |
| 105 | e = Season(i) |
| 106 | self.assertEqual(e, getattr(Season, season)) |
| 107 | self.assertEqual(e.value, i) |
| 108 | self.assertNotEqual(e, i) |
| 109 | self.assertEqual(e.name, season) |
| 110 | self.assertIn(e, Season) |
| 111 | self.assertIs(type(e), Season) |
| 112 | self.assertIsInstance(e, Season) |
| 113 | self.assertEqual(str(e), 'Season.' + season) |
| 114 | self.assertEqual( |
| 115 | repr(e), |
| 116 | '<Season.{0}: {1}>'.format(season, i), |
| 117 | ) |
| 118 | |
| 119 | def test_value_name(self): |
| 120 | Season = self.Season |
| 121 | self.assertEqual(Season.SPRING.name, 'SPRING') |
| 122 | self.assertEqual(Season.SPRING.value, 1) |
| 123 | with self.assertRaises(AttributeError): |
| 124 | Season.SPRING.name = 'invierno' |
| 125 | with self.assertRaises(AttributeError): |
| 126 | Season.SPRING.value = 2 |
| 127 | |
| 128 | def test_invalid_names(self): |
| 129 | with self.assertRaises(ValueError): |
| 130 | class Wrong(Enum): |
| 131 | mro = 9 |
| 132 | with self.assertRaises(ValueError): |
| 133 | class Wrong(Enum): |
| 134 | _create_= 11 |
| 135 | with self.assertRaises(ValueError): |
| 136 | class Wrong(Enum): |
| 137 | _get_mixins_ = 9 |
| 138 | with self.assertRaises(ValueError): |
| 139 | class Wrong(Enum): |
| 140 | _find_new_ = 1 |
| 141 | with self.assertRaises(ValueError): |
| 142 | class Wrong(Enum): |
| 143 | _any_name_ = 9 |
| 144 | |
| 145 | def test_contains(self): |
| 146 | Season = self.Season |
| 147 | self.assertIn(Season.AUTUMN, Season) |
| 148 | self.assertNotIn(3, Season) |
| 149 | |
| 150 | val = Season(3) |
| 151 | self.assertIn(val, Season) |
| 152 | |
| 153 | class OtherEnum(Enum): |
| 154 | one = 1; two = 2 |
| 155 | self.assertNotIn(OtherEnum.two, Season) |
| 156 | |
| 157 | def test_comparisons(self): |
| 158 | Season = self.Season |
| 159 | with self.assertRaises(TypeError): |
| 160 | Season.SPRING < Season.WINTER |
| 161 | with self.assertRaises(TypeError): |
| 162 | Season.SPRING > 4 |
| 163 | |
| 164 | self.assertNotEqual(Season.SPRING, 1) |
| 165 | |
| 166 | class Part(Enum): |
| 167 | SPRING = 1 |
| 168 | CLIP = 2 |
| 169 | BARREL = 3 |
| 170 | |
| 171 | self.assertNotEqual(Season.SPRING, Part.SPRING) |
| 172 | with self.assertRaises(TypeError): |
| 173 | Season.SPRING < Part.CLIP |
| 174 | |
| 175 | def test_enum_duplicates(self): |
| 176 | class Season(Enum): |
| 177 | SPRING = 1 |
| 178 | SUMMER = 2 |
| 179 | AUTUMN = FALL = 3 |
| 180 | WINTER = 4 |
| 181 | ANOTHER_SPRING = 1 |
| 182 | lst = list(Season) |
| 183 | self.assertEqual( |
| 184 | lst, |
| 185 | [Season.SPRING, Season.SUMMER, |
| 186 | Season.AUTUMN, Season.WINTER, |
| 187 | ]) |
| 188 | self.assertIs(Season.FALL, Season.AUTUMN) |
| 189 | self.assertEqual(Season.FALL.value, 3) |
| 190 | self.assertEqual(Season.AUTUMN.value, 3) |
| 191 | self.assertIs(Season(3), Season.AUTUMN) |
| 192 | self.assertIs(Season(1), Season.SPRING) |
| 193 | self.assertEqual(Season.FALL.name, 'AUTUMN') |
| 194 | self.assertEqual( |
| 195 | [k for k,v in Season.__members__.items() if v.name != k], |
| 196 | ['FALL', 'ANOTHER_SPRING'], |
| 197 | ) |
| 198 | |
| 199 | def test_enum_with_value_name(self): |
| 200 | class Huh(Enum): |
| 201 | name = 1 |
| 202 | value = 2 |
| 203 | self.assertEqual( |
| 204 | list(Huh), |
| 205 | [Huh.name, Huh.value], |
| 206 | ) |
| 207 | self.assertIs(type(Huh.name), Huh) |
| 208 | self.assertEqual(Huh.name.name, 'name') |
| 209 | self.assertEqual(Huh.name.value, 1) |
| 210 | def test_hash(self): |
| 211 | Season = self.Season |
| 212 | dates = {} |
| 213 | dates[Season.WINTER] = '1225' |
| 214 | dates[Season.SPRING] = '0315' |
| 215 | dates[Season.SUMMER] = '0704' |
| 216 | dates[Season.AUTUMN] = '1031' |
| 217 | self.assertEqual(dates[Season.AUTUMN], '1031') |
| 218 | |
| 219 | def test_intenum_from_scratch(self): |
| 220 | class phy(int, Enum): |
| 221 | pi = 3 |
| 222 | tau = 2 * pi |
| 223 | self.assertTrue(phy.pi < phy.tau) |
| 224 | |
| 225 | def test_intenum_inherited(self): |
| 226 | class IntEnum(int, Enum): |
| 227 | pass |
| 228 | class phy(IntEnum): |
| 229 | pi = 3 |
| 230 | tau = 2 * pi |
| 231 | self.assertTrue(phy.pi < phy.tau) |
| 232 | |
| 233 | def test_floatenum_from_scratch(self): |
| 234 | class phy(float, Enum): |
| 235 | pi = 3.141596 |
| 236 | tau = 2 * pi |
| 237 | self.assertTrue(phy.pi < phy.tau) |
| 238 | |
| 239 | def test_floatenum_inherited(self): |
| 240 | class FloatEnum(float, Enum): |
| 241 | pass |
| 242 | class phy(FloatEnum): |
| 243 | pi = 3.141596 |
| 244 | tau = 2 * pi |
| 245 | self.assertTrue(phy.pi < phy.tau) |
| 246 | |
| 247 | def test_strenum_from_scratch(self): |
| 248 | class phy(str, Enum): |
| 249 | pi = 'Pi' |
| 250 | tau = 'Tau' |
| 251 | self.assertTrue(phy.pi < phy.tau) |
| 252 | |
| 253 | def test_strenum_inherited(self): |
| 254 | class StrEnum(str, Enum): |
| 255 | pass |
| 256 | class phy(StrEnum): |
| 257 | pi = 'Pi' |
| 258 | tau = 'Tau' |
| 259 | self.assertTrue(phy.pi < phy.tau) |
| 260 | |
| 261 | |
| 262 | def test_intenum(self): |
| 263 | class WeekDay(IntEnum): |
| 264 | SUNDAY = 1 |
| 265 | MONDAY = 2 |
| 266 | TUESDAY = 3 |
| 267 | WEDNESDAY = 4 |
| 268 | THURSDAY = 5 |
| 269 | FRIDAY = 6 |
| 270 | SATURDAY = 7 |
| 271 | |
| 272 | self.assertEqual(['a', 'b', 'c'][WeekDay.MONDAY], 'c') |
| 273 | self.assertEqual([i for i in range(WeekDay.TUESDAY)], [0, 1, 2]) |
| 274 | |
| 275 | lst = list(WeekDay) |
| 276 | self.assertEqual(len(lst), len(WeekDay)) |
| 277 | self.assertEqual(len(WeekDay), 7) |
| 278 | target = 'SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY' |
| 279 | target = target.split() |
| 280 | for i, weekday in enumerate(target, 1): |
| 281 | e = WeekDay(i) |
| 282 | self.assertEqual(e, i) |
| 283 | self.assertEqual(int(e), i) |
| 284 | self.assertEqual(e.name, weekday) |
| 285 | self.assertIn(e, WeekDay) |
| 286 | self.assertEqual(lst.index(e)+1, i) |
| 287 | self.assertTrue(0 < e < 8) |
| 288 | self.assertIs(type(e), WeekDay) |
| 289 | self.assertIsInstance(e, int) |
| 290 | self.assertIsInstance(e, Enum) |
| 291 | |
| 292 | def test_intenum_duplicates(self): |
| 293 | class WeekDay(IntEnum): |
| 294 | SUNDAY = 1 |
| 295 | MONDAY = 2 |
| 296 | TUESDAY = TEUSDAY = 3 |
| 297 | WEDNESDAY = 4 |
| 298 | THURSDAY = 5 |
| 299 | FRIDAY = 6 |
| 300 | SATURDAY = 7 |
| 301 | self.assertIs(WeekDay.TEUSDAY, WeekDay.TUESDAY) |
| 302 | self.assertEqual(WeekDay(3).name, 'TUESDAY') |
| 303 | self.assertEqual([k for k,v in WeekDay.__members__.items() |
| 304 | if v.name != k], ['TEUSDAY', ]) |
| 305 | |
| 306 | def test_pickle_enum(self): |
| 307 | if isinstance(Stooges, Exception): |
| 308 | raise Stooges |
| 309 | self.assertIs(Stooges.CURLY, loads(dumps(Stooges.CURLY))) |
| 310 | self.assertIs(Stooges, loads(dumps(Stooges))) |
| 311 | |
| 312 | def test_pickle_int(self): |
| 313 | if isinstance(IntStooges, Exception): |
| 314 | raise IntStooges |
| 315 | self.assertIs(IntStooges.CURLY, loads(dumps(IntStooges.CURLY))) |
| 316 | self.assertIs(IntStooges, loads(dumps(IntStooges))) |
| 317 | |
| 318 | def test_pickle_float(self): |
| 319 | if isinstance(FloatStooges, Exception): |
| 320 | raise FloatStooges |
| 321 | self.assertIs(FloatStooges.CURLY, loads(dumps(FloatStooges.CURLY))) |
| 322 | self.assertIs(FloatStooges, loads(dumps(FloatStooges))) |
| 323 | |
| 324 | def test_pickle_enum_function(self): |
| 325 | if isinstance(Answer, Exception): |
| 326 | raise Answer |
| 327 | self.assertIs(Answer.him, loads(dumps(Answer.him))) |
| 328 | self.assertIs(Answer, loads(dumps(Answer))) |
| 329 | |
| 330 | def test_pickle_enum_function_with_module(self): |
| 331 | if isinstance(Question, Exception): |
| 332 | raise Question |
| 333 | self.assertIs(Question.who, loads(dumps(Question.who))) |
| 334 | self.assertIs(Question, loads(dumps(Question))) |
| 335 | |
| 336 | def test_exploding_pickle(self): |
| 337 | BadPickle = Enum('BadPickle', 'dill sweet bread-n-butter') |
| 338 | enum._make_class_unpicklable(BadPickle) |
| 339 | globals()['BadPickle'] = BadPickle |
| 340 | with self.assertRaises(TypeError): |
| 341 | dumps(BadPickle.dill) |
| 342 | with self.assertRaises(PicklingError): |
| 343 | dumps(BadPickle) |
| 344 | |
| 345 | def test_string_enum(self): |
| 346 | class SkillLevel(str, Enum): |
| 347 | master = 'what is the sound of one hand clapping?' |
| 348 | journeyman = 'why did the chicken cross the road?' |
| 349 | apprentice = 'knock, knock!' |
| 350 | self.assertEqual(SkillLevel.apprentice, 'knock, knock!') |
| 351 | |
| 352 | def test_getattr_getitem(self): |
| 353 | class Period(Enum): |
| 354 | morning = 1 |
| 355 | noon = 2 |
| 356 | evening = 3 |
| 357 | night = 4 |
| 358 | self.assertIs(Period(2), Period.noon) |
| 359 | self.assertIs(getattr(Period, 'night'), Period.night) |
| 360 | self.assertIs(Period['morning'], Period.morning) |
| 361 | |
| 362 | def test_getattr_dunder(self): |
| 363 | Season = self.Season |
| 364 | self.assertTrue(getattr(Season, '__eq__')) |
| 365 | |
| 366 | def test_iteration_order(self): |
| 367 | class Season(Enum): |
| 368 | SUMMER = 2 |
| 369 | WINTER = 4 |
| 370 | AUTUMN = 3 |
| 371 | SPRING = 1 |
| 372 | self.assertEqual( |
| 373 | list(Season), |
| 374 | [Season.SUMMER, Season.WINTER, Season.AUTUMN, Season.SPRING], |
| 375 | ) |
| 376 | |
| 377 | def test_programatic_function_string(self): |
| 378 | SummerMonth = Enum('SummerMonth', 'june july august') |
| 379 | lst = list(SummerMonth) |
| 380 | self.assertEqual(len(lst), len(SummerMonth)) |
| 381 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 382 | self.assertEqual( |
| 383 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 384 | lst, |
| 385 | ) |
| 386 | for i, month in enumerate('june july august'.split(), 1): |
| 387 | e = SummerMonth(i) |
| 388 | self.assertEqual(int(e.value), i) |
| 389 | self.assertNotEqual(e, i) |
| 390 | self.assertEqual(e.name, month) |
| 391 | self.assertIn(e, SummerMonth) |
| 392 | self.assertIs(type(e), SummerMonth) |
| 393 | |
| 394 | def test_programatic_function_string_list(self): |
| 395 | SummerMonth = Enum('SummerMonth', ['june', 'july', 'august']) |
| 396 | lst = list(SummerMonth) |
| 397 | self.assertEqual(len(lst), len(SummerMonth)) |
| 398 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 399 | self.assertEqual( |
| 400 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 401 | lst, |
| 402 | ) |
| 403 | for i, month in enumerate('june july august'.split(), 1): |
| 404 | e = SummerMonth(i) |
| 405 | self.assertEqual(int(e.value), i) |
| 406 | self.assertNotEqual(e, i) |
| 407 | self.assertEqual(e.name, month) |
| 408 | self.assertIn(e, SummerMonth) |
| 409 | self.assertIs(type(e), SummerMonth) |
| 410 | |
| 411 | def test_programatic_function_iterable(self): |
| 412 | SummerMonth = Enum( |
| 413 | 'SummerMonth', |
| 414 | (('june', 1), ('july', 2), ('august', 3)) |
| 415 | ) |
| 416 | lst = list(SummerMonth) |
| 417 | self.assertEqual(len(lst), len(SummerMonth)) |
| 418 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 419 | self.assertEqual( |
| 420 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 421 | lst, |
| 422 | ) |
| 423 | for i, month in enumerate('june july august'.split(), 1): |
| 424 | e = SummerMonth(i) |
| 425 | self.assertEqual(int(e.value), i) |
| 426 | self.assertNotEqual(e, i) |
| 427 | self.assertEqual(e.name, month) |
| 428 | self.assertIn(e, SummerMonth) |
| 429 | self.assertIs(type(e), SummerMonth) |
| 430 | |
| 431 | def test_programatic_function_from_dict(self): |
| 432 | SummerMonth = Enum( |
| 433 | 'SummerMonth', |
| 434 | OrderedDict((('june', 1), ('july', 2), ('august', 3))) |
| 435 | ) |
| 436 | lst = list(SummerMonth) |
| 437 | self.assertEqual(len(lst), len(SummerMonth)) |
| 438 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 439 | self.assertEqual( |
| 440 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 441 | lst, |
| 442 | ) |
| 443 | for i, month in enumerate('june july august'.split(), 1): |
| 444 | e = SummerMonth(i) |
| 445 | self.assertEqual(int(e.value), i) |
| 446 | self.assertNotEqual(e, i) |
| 447 | self.assertEqual(e.name, month) |
| 448 | self.assertIn(e, SummerMonth) |
| 449 | self.assertIs(type(e), SummerMonth) |
| 450 | |
| 451 | def test_programatic_function_type(self): |
| 452 | SummerMonth = Enum('SummerMonth', 'june july august', type=int) |
| 453 | lst = list(SummerMonth) |
| 454 | self.assertEqual(len(lst), len(SummerMonth)) |
| 455 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 456 | self.assertEqual( |
| 457 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 458 | lst, |
| 459 | ) |
| 460 | for i, month in enumerate('june july august'.split(), 1): |
| 461 | e = SummerMonth(i) |
| 462 | self.assertEqual(e, i) |
| 463 | self.assertEqual(e.name, month) |
| 464 | self.assertIn(e, SummerMonth) |
| 465 | self.assertIs(type(e), SummerMonth) |
| 466 | |
| 467 | def test_programatic_function_type_from_subclass(self): |
| 468 | SummerMonth = IntEnum('SummerMonth', 'june july august') |
| 469 | lst = list(SummerMonth) |
| 470 | self.assertEqual(len(lst), len(SummerMonth)) |
| 471 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 472 | self.assertEqual( |
| 473 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 474 | lst, |
| 475 | ) |
| 476 | for i, month in enumerate('june july august'.split(), 1): |
| 477 | e = SummerMonth(i) |
| 478 | self.assertEqual(e, i) |
| 479 | self.assertEqual(e.name, month) |
| 480 | self.assertIn(e, SummerMonth) |
| 481 | self.assertIs(type(e), SummerMonth) |
| 482 | |
| 483 | def test_subclassing(self): |
| 484 | if isinstance(Name, Exception): |
| 485 | raise Name |
| 486 | self.assertEqual(Name.BDFL, 'Guido van Rossum') |
| 487 | self.assertTrue(Name.BDFL, Name('Guido van Rossum')) |
| 488 | self.assertIs(Name.BDFL, getattr(Name, 'BDFL')) |
| 489 | self.assertIs(Name.BDFL, loads(dumps(Name.BDFL))) |
| 490 | |
| 491 | def test_extending(self): |
| 492 | class Color(Enum): |
| 493 | red = 1 |
| 494 | green = 2 |
| 495 | blue = 3 |
| 496 | with self.assertRaises(TypeError): |
| 497 | class MoreColor(Color): |
| 498 | cyan = 4 |
| 499 | magenta = 5 |
| 500 | yellow = 6 |
| 501 | |
| 502 | def test_exclude_methods(self): |
| 503 | class whatever(Enum): |
| 504 | this = 'that' |
| 505 | these = 'those' |
| 506 | def really(self): |
| 507 | return 'no, not %s' % self.value |
| 508 | self.assertIsNot(type(whatever.really), whatever) |
| 509 | self.assertEqual(whatever.this.really(), 'no, not that') |
| 510 | |
| 511 | def test_overwrite_enums(self): |
| 512 | class Why(Enum): |
| 513 | question = 1 |
| 514 | answer = 2 |
| 515 | propisition = 3 |
| 516 | def question(self): |
| 517 | print(42) |
| 518 | self.assertIsNot(type(Why.question), Why) |
| 519 | self.assertNotIn(Why.question, Why._member_names) |
| 520 | self.assertNotIn(Why.question, Why) |
| 521 | |
| 522 | def test_wrong_inheritance_order(self): |
| 523 | with self.assertRaises(TypeError): |
| 524 | class Wrong(Enum, str): |
| 525 | NotHere = 'error before this point' |
| 526 | |
| 527 | def test_intenum_transitivity(self): |
| 528 | class number(IntEnum): |
| 529 | one = 1 |
| 530 | two = 2 |
| 531 | three = 3 |
| 532 | class numero(IntEnum): |
| 533 | uno = 1 |
| 534 | dos = 2 |
| 535 | tres = 3 |
| 536 | self.assertEqual(number.one, numero.uno) |
| 537 | self.assertEqual(number.two, numero.dos) |
| 538 | self.assertEqual(number.three, numero.tres) |
| 539 | |
| 540 | def test_wrong_enum_in_call(self): |
| 541 | class Monochrome(Enum): |
| 542 | black = 0 |
| 543 | white = 1 |
| 544 | class Gender(Enum): |
| 545 | male = 0 |
| 546 | female = 1 |
| 547 | self.assertRaises(ValueError, Monochrome, Gender.male) |
| 548 | |
| 549 | def test_wrong_enum_in_mixed_call(self): |
| 550 | class Monochrome(IntEnum): |
| 551 | black = 0 |
| 552 | white = 1 |
| 553 | class Gender(Enum): |
| 554 | male = 0 |
| 555 | female = 1 |
| 556 | self.assertRaises(ValueError, Monochrome, Gender.male) |
| 557 | |
| 558 | def test_mixed_enum_in_call_1(self): |
| 559 | class Monochrome(IntEnum): |
| 560 | black = 0 |
| 561 | white = 1 |
| 562 | class Gender(IntEnum): |
| 563 | male = 0 |
| 564 | female = 1 |
| 565 | self.assertIs(Monochrome(Gender.female), Monochrome.white) |
| 566 | |
| 567 | def test_mixed_enum_in_call_2(self): |
| 568 | class Monochrome(Enum): |
| 569 | black = 0 |
| 570 | white = 1 |
| 571 | class Gender(IntEnum): |
| 572 | male = 0 |
| 573 | female = 1 |
| 574 | self.assertIs(Monochrome(Gender.male), Monochrome.black) |
| 575 | |
| 576 | def test_flufl_enum(self): |
| 577 | class Fluflnum(Enum): |
| 578 | def __int__(self): |
| 579 | return int(self.value) |
| 580 | class MailManOptions(Fluflnum): |
| 581 | option1 = 1 |
| 582 | option2 = 2 |
| 583 | option3 = 3 |
| 584 | self.assertEqual(int(MailManOptions.option1), 1) |
| 585 | |
| 586 | def test_no_such_enum_member(self): |
| 587 | class Color(Enum): |
| 588 | red = 1 |
| 589 | green = 2 |
| 590 | blue = 3 |
| 591 | with self.assertRaises(ValueError): |
| 592 | Color(4) |
| 593 | with self.assertRaises(KeyError): |
| 594 | Color['chartreuse'] |
| 595 | |
| 596 | def test_new_repr(self): |
| 597 | class Color(Enum): |
| 598 | red = 1 |
| 599 | green = 2 |
| 600 | blue = 3 |
| 601 | def __repr__(self): |
| 602 | return "don't you just love shades of %s?" % self.name |
| 603 | self.assertEqual( |
| 604 | repr(Color.blue), |
| 605 | "don't you just love shades of blue?", |
| 606 | ) |
| 607 | |
| 608 | def test_inherited_repr(self): |
| 609 | class MyEnum(Enum): |
| 610 | def __repr__(self): |
| 611 | return "My name is %s." % self.name |
| 612 | class MyIntEnum(int, MyEnum): |
| 613 | this = 1 |
| 614 | that = 2 |
| 615 | theother = 3 |
| 616 | self.assertEqual(repr(MyIntEnum.that), "My name is that.") |
| 617 | |
| 618 | def test_multiple_mixin_mro(self): |
| 619 | class auto_enum(type(Enum)): |
| 620 | def __new__(metacls, cls, bases, classdict): |
| 621 | temp = type(classdict)() |
| 622 | names = set(classdict._member_names) |
| 623 | i = 0 |
| 624 | for k in classdict._member_names: |
| 625 | v = classdict[k] |
| 626 | if v is Ellipsis: |
| 627 | v = i |
| 628 | else: |
| 629 | i = v |
| 630 | i += 1 |
| 631 | temp[k] = v |
| 632 | for k, v in classdict.items(): |
| 633 | if k not in names: |
| 634 | temp[k] = v |
| 635 | return super(auto_enum, metacls).__new__( |
| 636 | metacls, cls, bases, temp) |
| 637 | |
| 638 | class AutoNumberedEnum(Enum, metaclass=auto_enum): |
| 639 | pass |
| 640 | |
| 641 | class AutoIntEnum(IntEnum, metaclass=auto_enum): |
| 642 | pass |
| 643 | |
| 644 | class TestAutoNumber(AutoNumberedEnum): |
| 645 | a = ... |
| 646 | b = 3 |
| 647 | c = ... |
| 648 | |
| 649 | class TestAutoInt(AutoIntEnum): |
| 650 | a = ... |
| 651 | b = 3 |
| 652 | c = ... |
| 653 | |
| 654 | def test_subclasses_with_getnewargs(self): |
| 655 | class NamedInt(int): |
| 656 | def __new__(cls, *args): |
| 657 | _args = args |
| 658 | name, *args = args |
| 659 | if len(args) == 0: |
| 660 | raise TypeError("name and value must be specified") |
| 661 | self = int.__new__(cls, *args) |
| 662 | self._intname = name |
| 663 | self._args = _args |
| 664 | return self |
| 665 | def __getnewargs__(self): |
| 666 | return self._args |
| 667 | @property |
| 668 | def __name__(self): |
| 669 | return self._intname |
| 670 | def __repr__(self): |
| 671 | # repr() is updated to include the name and type info |
| 672 | return "{}({!r}, {})".format(type(self).__name__, |
| 673 | self.__name__, |
| 674 | int.__repr__(self)) |
| 675 | def __str__(self): |
| 676 | # str() is unchanged, even if it relies on the repr() fallback |
| 677 | base = int |
| 678 | base_str = base.__str__ |
| 679 | if base_str.__objclass__ is object: |
| 680 | return base.__repr__(self) |
| 681 | return base_str(self) |
| 682 | # for simplicity, we only define one operator that |
| 683 | # propagates expressions |
| 684 | def __add__(self, other): |
| 685 | temp = int(self) + int( other) |
| 686 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 687 | return NamedInt( |
| 688 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 689 | temp ) |
| 690 | else: |
| 691 | return temp |
| 692 | |
| 693 | class NEI(NamedInt, Enum): |
| 694 | x = ('the-x', 1) |
| 695 | y = ('the-y', 2) |
| 696 | |
| 697 | self.assertIs(NEI.__new__, Enum.__new__) |
| 698 | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
| 699 | globals()['NamedInt'] = NamedInt |
| 700 | globals()['NEI'] = NEI |
| 701 | NI5 = NamedInt('test', 5) |
| 702 | self.assertEqual(NI5, 5) |
| 703 | self.assertEqual(loads(dumps(NI5)), 5) |
| 704 | self.assertEqual(NEI.y.value, 2) |
| 705 | self.assertIs(loads(dumps(NEI.y)), NEI.y) |
| 706 | |
| 707 | def test_subclasses_without_getnewargs(self): |
| 708 | class NamedInt(int): |
| 709 | def __new__(cls, *args): |
| 710 | _args = args |
| 711 | name, *args = args |
| 712 | if len(args) == 0: |
| 713 | raise TypeError("name and value must be specified") |
| 714 | self = int.__new__(cls, *args) |
| 715 | self._intname = name |
| 716 | self._args = _args |
| 717 | return self |
| 718 | @property |
| 719 | def __name__(self): |
| 720 | return self._intname |
| 721 | def __repr__(self): |
| 722 | # repr() is updated to include the name and type info |
| 723 | return "{}({!r}, {})".format(type(self).__name__, |
| 724 | self.__name__, |
| 725 | int.__repr__(self)) |
| 726 | def __str__(self): |
| 727 | # str() is unchanged, even if it relies on the repr() fallback |
| 728 | base = int |
| 729 | base_str = base.__str__ |
| 730 | if base_str.__objclass__ is object: |
| 731 | return base.__repr__(self) |
| 732 | return base_str(self) |
| 733 | # for simplicity, we only define one operator that |
| 734 | # propagates expressions |
| 735 | def __add__(self, other): |
| 736 | temp = int(self) + int( other) |
| 737 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 738 | return NamedInt( |
| 739 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 740 | temp ) |
| 741 | else: |
| 742 | return temp |
| 743 | |
| 744 | class NEI(NamedInt, Enum): |
| 745 | x = ('the-x', 1) |
| 746 | y = ('the-y', 2) |
| 747 | |
| 748 | self.assertIs(NEI.__new__, Enum.__new__) |
| 749 | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
| 750 | globals()['NamedInt'] = NamedInt |
| 751 | globals()['NEI'] = NEI |
| 752 | NI5 = NamedInt('test', 5) |
| 753 | self.assertEqual(NI5, 5) |
| 754 | self.assertEqual(NEI.y.value, 2) |
| 755 | with self.assertRaises(TypeError): |
| 756 | dumps(NEI.x) |
| 757 | with self.assertRaises(PicklingError): |
| 758 | dumps(NEI) |
| 759 | |
| 760 | def test_tuple_subclass(self): |
| 761 | class SomeTuple(tuple, Enum): |
| 762 | first = (1, 'for the money') |
| 763 | second = (2, 'for the show') |
| 764 | third = (3, 'for the music') |
| 765 | self.assertIs(type(SomeTuple.first), SomeTuple) |
| 766 | self.assertIsInstance(SomeTuple.second, tuple) |
| 767 | self.assertEqual(SomeTuple.third, (3, 'for the music')) |
| 768 | globals()['SomeTuple'] = SomeTuple |
| 769 | self.assertIs(loads(dumps(SomeTuple.first)), SomeTuple.first) |
| 770 | |
| 771 | def test_duplicate_values_give_unique_enum_items(self): |
| 772 | class AutoNumber(Enum): |
| 773 | first = () |
| 774 | second = () |
| 775 | third = () |
| 776 | def __new__(cls): |
| 777 | value = len(cls.__members__) + 1 |
| 778 | obj = object.__new__(cls) |
| 779 | obj._value = value |
| 780 | return obj |
| 781 | def __int__(self): |
| 782 | return int(self._value) |
| 783 | self.assertEqual( |
| 784 | list(AutoNumber), |
| 785 | [AutoNumber.first, AutoNumber.second, AutoNumber.third], |
| 786 | ) |
| 787 | self.assertEqual(int(AutoNumber.second), 2) |
| 788 | self.assertIs(AutoNumber(1), AutoNumber.first) |
| 789 | |
| 790 | def test_inherited_new_from_enhanced_enum(self): |
| 791 | class AutoNumber(Enum): |
| 792 | def __new__(cls): |
| 793 | value = len(cls.__members__) + 1 |
| 794 | obj = object.__new__(cls) |
| 795 | obj._value = value |
| 796 | return obj |
| 797 | def __int__(self): |
| 798 | return int(self._value) |
| 799 | class Color(AutoNumber): |
| 800 | red = () |
| 801 | green = () |
| 802 | blue = () |
| 803 | self.assertEqual(list(Color), [Color.red, Color.green, Color.blue]) |
| 804 | self.assertEqual(list(map(int, Color)), [1, 2, 3]) |
| 805 | |
| 806 | def test_inherited_new_from_mixed_enum(self): |
| 807 | class AutoNumber(IntEnum): |
| 808 | def __new__(cls): |
| 809 | value = len(cls.__members__) + 1 |
| 810 | obj = int.__new__(cls, value) |
| 811 | obj._value = value |
| 812 | return obj |
| 813 | class Color(AutoNumber): |
| 814 | red = () |
| 815 | green = () |
| 816 | blue = () |
| 817 | self.assertEqual(list(Color), [Color.red, Color.green, Color.blue]) |
| 818 | self.assertEqual(list(map(int, Color)), [1, 2, 3]) |
| 819 | |
| 820 | def test_ordered_mixin(self): |
| 821 | class OrderedEnum(Enum): |
| 822 | def __ge__(self, other): |
| 823 | if self.__class__ is other.__class__: |
| 824 | return self._value >= other._value |
| 825 | return NotImplemented |
| 826 | def __gt__(self, other): |
| 827 | if self.__class__ is other.__class__: |
| 828 | return self._value > other._value |
| 829 | return NotImplemented |
| 830 | def __le__(self, other): |
| 831 | if self.__class__ is other.__class__: |
| 832 | return self._value <= other._value |
| 833 | return NotImplemented |
| 834 | def __lt__(self, other): |
| 835 | if self.__class__ is other.__class__: |
| 836 | return self._value < other._value |
| 837 | return NotImplemented |
| 838 | class Grade(OrderedEnum): |
| 839 | A = 5 |
| 840 | B = 4 |
| 841 | C = 3 |
| 842 | D = 2 |
| 843 | F = 1 |
| 844 | self.assertGreater(Grade.A, Grade.B) |
| 845 | self.assertLessEqual(Grade.F, Grade.C) |
| 846 | self.assertLess(Grade.D, Grade.A) |
| 847 | self.assertGreaterEqual(Grade.B, Grade.B) |
| 848 | def test_extending2(self): |
| 849 | class Shade(Enum): |
| 850 | def shade(self): |
| 851 | print(self.name) |
| 852 | class Color(Shade): |
| 853 | red = 1 |
| 854 | green = 2 |
| 855 | blue = 3 |
| 856 | with self.assertRaises(TypeError): |
| 857 | class MoreColor(Color): |
| 858 | cyan = 4 |
| 859 | magenta = 5 |
| 860 | yellow = 6 |
| 861 | |
| 862 | def test_extending3(self): |
| 863 | class Shade(Enum): |
| 864 | def shade(self): |
| 865 | return self.name |
| 866 | class Color(Shade): |
| 867 | def hex(self): |
| 868 | return '%s hexlified!' % self.value |
| 869 | class MoreColor(Color): |
| 870 | cyan = 4 |
| 871 | magenta = 5 |
| 872 | yellow = 6 |
| 873 | self.assertEqual(MoreColor.magenta.hex(), '5 hexlified!') |
| 874 | |
| 875 | |
| 876 | def test_no_duplicates(self): |
| 877 | class UniqueEnum(Enum): |
| 878 | def __init__(self, *args): |
| 879 | cls = self.__class__ |
| 880 | if any(self.value == e.value for e in cls): |
| 881 | a = self.name |
| 882 | e = cls(self.value).name |
| 883 | raise ValueError( |
| 884 | "aliases not allowed in UniqueEnum: %r --> %r" |
| 885 | % (a, e) |
| 886 | ) |
| 887 | class Color(UniqueEnum): |
| 888 | red = 1 |
| 889 | green = 2 |
| 890 | blue = 3 |
| 891 | with self.assertRaises(ValueError): |
| 892 | class Color(UniqueEnum): |
| 893 | red = 1 |
| 894 | green = 2 |
| 895 | blue = 3 |
| 896 | grene = 2 |
| 897 | |
| 898 | def test_init(self): |
| 899 | class Planet(Enum): |
| 900 | MERCURY = (3.303e+23, 2.4397e6) |
| 901 | VENUS = (4.869e+24, 6.0518e6) |
| 902 | EARTH = (5.976e+24, 6.37814e6) |
| 903 | MARS = (6.421e+23, 3.3972e6) |
| 904 | JUPITER = (1.9e+27, 7.1492e7) |
| 905 | SATURN = (5.688e+26, 6.0268e7) |
| 906 | URANUS = (8.686e+25, 2.5559e7) |
| 907 | NEPTUNE = (1.024e+26, 2.4746e7) |
| 908 | def __init__(self, mass, radius): |
| 909 | self.mass = mass # in kilograms |
| 910 | self.radius = radius # in meters |
| 911 | @property |
| 912 | def surface_gravity(self): |
| 913 | # universal gravitational constant (m3 kg-1 s-2) |
| 914 | G = 6.67300E-11 |
| 915 | return G * self.mass / (self.radius * self.radius) |
| 916 | self.assertEqual(round(Planet.EARTH.surface_gravity, 2), 9.80) |
| 917 | self.assertEqual(Planet.EARTH.value, (5.976e+24, 6.37814e6)) |
| 918 | |
| 919 | |
Ethan Furman | f24bb35 | 2013-07-18 17:05:39 -0700 | [diff] [blame^] | 920 | class TestUnique(unittest.TestCase): |
| 921 | |
| 922 | def test_unique_clean(self): |
| 923 | @unique |
| 924 | class Clean(Enum): |
| 925 | one = 1 |
| 926 | two = 'dos' |
| 927 | tres = 4.0 |
| 928 | @unique |
| 929 | class Cleaner(IntEnum): |
| 930 | single = 1 |
| 931 | double = 2 |
| 932 | triple = 3 |
| 933 | |
| 934 | def test_unique_dirty(self): |
| 935 | with self.assertRaisesRegex(ValueError, 'tres.*one'): |
| 936 | @unique |
| 937 | class Dirty(Enum): |
| 938 | one = 1 |
| 939 | two = 'dos' |
| 940 | tres = 1 |
| 941 | with self.assertRaisesRegex( |
| 942 | ValueError, |
| 943 | 'double.*single.*turkey.*triple', |
| 944 | ): |
| 945 | @unique |
| 946 | class Dirtier(IntEnum): |
| 947 | single = 1 |
| 948 | double = 1 |
| 949 | triple = 3 |
| 950 | turkey = 3 |
| 951 | |
| 952 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 953 | if __name__ == '__main__': |
| 954 | unittest.main() |