Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1 | import enum |
Ethan Furman | 5875d74 | 2013-10-21 20:45:55 -0700 | [diff] [blame] | 2 | import inspect |
| 3 | import pydoc |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 4 | import unittest |
| 5 | from collections import OrderedDict |
Ethan Furman | 5875d74 | 2013-10-21 20:45:55 -0700 | [diff] [blame] | 6 | from enum import Enum, IntEnum, EnumMeta, unique |
| 7 | from io import StringIO |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 8 | from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 9 | |
| 10 | # for pickle tests |
| 11 | try: |
| 12 | class Stooges(Enum): |
| 13 | LARRY = 1 |
| 14 | CURLY = 2 |
| 15 | MOE = 3 |
| 16 | except Exception as exc: |
| 17 | Stooges = exc |
| 18 | |
| 19 | try: |
| 20 | class IntStooges(int, Enum): |
| 21 | LARRY = 1 |
| 22 | CURLY = 2 |
| 23 | MOE = 3 |
| 24 | except Exception as exc: |
| 25 | IntStooges = exc |
| 26 | |
| 27 | try: |
| 28 | class FloatStooges(float, Enum): |
| 29 | LARRY = 1.39 |
| 30 | CURLY = 2.72 |
| 31 | MOE = 3.142596 |
| 32 | except Exception as exc: |
| 33 | FloatStooges = exc |
| 34 | |
| 35 | # for pickle test and subclass tests |
| 36 | try: |
| 37 | class StrEnum(str, Enum): |
| 38 | 'accepts only string values' |
| 39 | class Name(StrEnum): |
| 40 | BDFL = 'Guido van Rossum' |
| 41 | FLUFL = 'Barry Warsaw' |
| 42 | except Exception as exc: |
| 43 | Name = exc |
| 44 | |
| 45 | try: |
| 46 | Question = Enum('Question', 'who what when where why', module=__name__) |
| 47 | except Exception as exc: |
| 48 | Question = exc |
| 49 | |
| 50 | try: |
| 51 | Answer = Enum('Answer', 'him this then there because') |
| 52 | except Exception as exc: |
| 53 | Answer = exc |
| 54 | |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 55 | try: |
| 56 | Theory = Enum('Theory', 'rule law supposition', qualname='spanish_inquisition') |
| 57 | except Exception as exc: |
| 58 | Theory = exc |
| 59 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 60 | # for doctests |
| 61 | try: |
| 62 | class Fruit(Enum): |
| 63 | tomato = 1 |
| 64 | banana = 2 |
| 65 | cherry = 3 |
| 66 | except Exception: |
| 67 | pass |
| 68 | |
Serhiy Storchaka | e50e780 | 2015-03-31 16:56:49 +0300 | [diff] [blame] | 69 | def test_pickle_dump_load(assertion, source, target=None): |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 70 | if target is None: |
| 71 | target = source |
Serhiy Storchaka | e50e780 | 2015-03-31 16:56:49 +0300 | [diff] [blame] | 72 | for protocol in range(HIGHEST_PROTOCOL + 1): |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 73 | assertion(loads(dumps(source, protocol=protocol)), target) |
| 74 | |
Serhiy Storchaka | e50e780 | 2015-03-31 16:56:49 +0300 | [diff] [blame] | 75 | def test_pickle_exception(assertion, exception, obj): |
| 76 | for protocol in range(HIGHEST_PROTOCOL + 1): |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 77 | with assertion(exception): |
| 78 | dumps(obj, protocol=protocol) |
Ethan Furman | 648f860 | 2013-10-06 17:19:54 -0700 | [diff] [blame] | 79 | |
| 80 | class TestHelpers(unittest.TestCase): |
| 81 | # _is_descriptor, _is_sunder, _is_dunder |
| 82 | |
| 83 | def test_is_descriptor(self): |
| 84 | class foo: |
| 85 | pass |
| 86 | for attr in ('__get__','__set__','__delete__'): |
| 87 | obj = foo() |
| 88 | self.assertFalse(enum._is_descriptor(obj)) |
| 89 | setattr(obj, attr, 1) |
| 90 | self.assertTrue(enum._is_descriptor(obj)) |
| 91 | |
| 92 | def test_is_sunder(self): |
| 93 | for s in ('_a_', '_aa_'): |
| 94 | self.assertTrue(enum._is_sunder(s)) |
| 95 | |
| 96 | for s in ('a', 'a_', '_a', '__a', 'a__', '__a__', '_a__', '__a_', '_', |
| 97 | '__', '___', '____', '_____',): |
| 98 | self.assertFalse(enum._is_sunder(s)) |
| 99 | |
| 100 | def test_is_dunder(self): |
| 101 | for s in ('__a__', '__aa__'): |
| 102 | self.assertTrue(enum._is_dunder(s)) |
| 103 | for s in ('a', 'a_', '_a', '__a', 'a__', '_a_', '_a__', '__a_', '_', |
| 104 | '__', '___', '____', '_____',): |
| 105 | self.assertFalse(enum._is_dunder(s)) |
| 106 | |
| 107 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 108 | class TestEnum(unittest.TestCase): |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 109 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 110 | def setUp(self): |
| 111 | class Season(Enum): |
| 112 | SPRING = 1 |
| 113 | SUMMER = 2 |
| 114 | AUTUMN = 3 |
| 115 | WINTER = 4 |
| 116 | self.Season = Season |
| 117 | |
Ethan Furman | ec15a82 | 2013-08-31 19:17:41 -0700 | [diff] [blame] | 118 | class Konstants(float, Enum): |
| 119 | E = 2.7182818 |
| 120 | PI = 3.1415926 |
| 121 | TAU = 2 * PI |
| 122 | self.Konstants = Konstants |
| 123 | |
| 124 | class Grades(IntEnum): |
| 125 | A = 5 |
| 126 | B = 4 |
| 127 | C = 3 |
| 128 | D = 2 |
| 129 | F = 0 |
| 130 | self.Grades = Grades |
| 131 | |
| 132 | class Directional(str, Enum): |
| 133 | EAST = 'east' |
| 134 | WEST = 'west' |
| 135 | NORTH = 'north' |
| 136 | SOUTH = 'south' |
| 137 | self.Directional = Directional |
| 138 | |
| 139 | from datetime import date |
| 140 | class Holiday(date, Enum): |
| 141 | NEW_YEAR = 2013, 1, 1 |
| 142 | IDES_OF_MARCH = 2013, 3, 15 |
| 143 | self.Holiday = Holiday |
| 144 | |
Ethan Furman | 388a392 | 2013-08-12 06:51:41 -0700 | [diff] [blame] | 145 | def test_dir_on_class(self): |
| 146 | Season = self.Season |
| 147 | self.assertEqual( |
| 148 | set(dir(Season)), |
Ethan Furman | c850f34 | 2013-09-15 16:59:35 -0700 | [diff] [blame] | 149 | set(['__class__', '__doc__', '__members__', '__module__', |
Ethan Furman | 388a392 | 2013-08-12 06:51:41 -0700 | [diff] [blame] | 150 | 'SPRING', 'SUMMER', 'AUTUMN', 'WINTER']), |
| 151 | ) |
| 152 | |
| 153 | def test_dir_on_item(self): |
| 154 | Season = self.Season |
| 155 | self.assertEqual( |
| 156 | set(dir(Season.WINTER)), |
Ethan Furman | c850f34 | 2013-09-15 16:59:35 -0700 | [diff] [blame] | 157 | set(['__class__', '__doc__', '__module__', 'name', 'value']), |
Ethan Furman | 388a392 | 2013-08-12 06:51:41 -0700 | [diff] [blame] | 158 | ) |
| 159 | |
Ethan Furman | c850f34 | 2013-09-15 16:59:35 -0700 | [diff] [blame] | 160 | def test_dir_with_added_behavior(self): |
| 161 | class Test(Enum): |
| 162 | this = 'that' |
| 163 | these = 'those' |
| 164 | def wowser(self): |
| 165 | return ("Wowser! I'm %s!" % self.name) |
| 166 | self.assertEqual( |
| 167 | set(dir(Test)), |
| 168 | set(['__class__', '__doc__', '__members__', '__module__', 'this', 'these']), |
| 169 | ) |
| 170 | self.assertEqual( |
| 171 | set(dir(Test.this)), |
| 172 | set(['__class__', '__doc__', '__module__', 'name', 'value', 'wowser']), |
| 173 | ) |
| 174 | |
Ethan Furman | 0ae550b | 2014-10-14 08:58:32 -0700 | [diff] [blame] | 175 | def test_dir_on_sub_with_behavior_on_super(self): |
| 176 | # see issue22506 |
| 177 | class SuperEnum(Enum): |
| 178 | def invisible(self): |
| 179 | return "did you see me?" |
| 180 | class SubEnum(SuperEnum): |
| 181 | sample = 5 |
| 182 | self.assertEqual( |
| 183 | set(dir(SubEnum.sample)), |
| 184 | set(['__class__', '__doc__', '__module__', 'name', 'value', 'invisible']), |
| 185 | ) |
| 186 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 187 | def test_enum_in_enum_out(self): |
| 188 | Season = self.Season |
| 189 | self.assertIs(Season(Season.WINTER), Season.WINTER) |
| 190 | |
| 191 | def test_enum_value(self): |
| 192 | Season = self.Season |
| 193 | self.assertEqual(Season.SPRING.value, 1) |
| 194 | |
| 195 | def test_intenum_value(self): |
| 196 | self.assertEqual(IntStooges.CURLY.value, 2) |
| 197 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 198 | def test_enum(self): |
| 199 | Season = self.Season |
| 200 | lst = list(Season) |
| 201 | self.assertEqual(len(lst), len(Season)) |
| 202 | self.assertEqual(len(Season), 4, Season) |
| 203 | self.assertEqual( |
| 204 | [Season.SPRING, Season.SUMMER, Season.AUTUMN, Season.WINTER], lst) |
| 205 | |
| 206 | for i, season in enumerate('SPRING SUMMER AUTUMN WINTER'.split(), 1): |
| 207 | e = Season(i) |
| 208 | self.assertEqual(e, getattr(Season, season)) |
| 209 | self.assertEqual(e.value, i) |
| 210 | self.assertNotEqual(e, i) |
| 211 | self.assertEqual(e.name, season) |
| 212 | self.assertIn(e, Season) |
| 213 | self.assertIs(type(e), Season) |
| 214 | self.assertIsInstance(e, Season) |
| 215 | self.assertEqual(str(e), 'Season.' + season) |
| 216 | self.assertEqual( |
| 217 | repr(e), |
| 218 | '<Season.{0}: {1}>'.format(season, i), |
| 219 | ) |
| 220 | |
| 221 | def test_value_name(self): |
| 222 | Season = self.Season |
| 223 | self.assertEqual(Season.SPRING.name, 'SPRING') |
| 224 | self.assertEqual(Season.SPRING.value, 1) |
| 225 | with self.assertRaises(AttributeError): |
| 226 | Season.SPRING.name = 'invierno' |
| 227 | with self.assertRaises(AttributeError): |
| 228 | Season.SPRING.value = 2 |
| 229 | |
Ethan Furman | f203f2d | 2013-09-06 07:16:48 -0700 | [diff] [blame] | 230 | def test_changing_member(self): |
| 231 | Season = self.Season |
| 232 | with self.assertRaises(AttributeError): |
| 233 | Season.WINTER = 'really cold' |
| 234 | |
Ethan Furman | 64a9972 | 2013-09-22 16:18:19 -0700 | [diff] [blame] | 235 | def test_attribute_deletion(self): |
| 236 | class Season(Enum): |
| 237 | SPRING = 1 |
| 238 | SUMMER = 2 |
| 239 | AUTUMN = 3 |
| 240 | WINTER = 4 |
| 241 | |
| 242 | def spam(cls): |
| 243 | pass |
| 244 | |
| 245 | self.assertTrue(hasattr(Season, 'spam')) |
| 246 | del Season.spam |
| 247 | self.assertFalse(hasattr(Season, 'spam')) |
| 248 | |
| 249 | with self.assertRaises(AttributeError): |
| 250 | del Season.SPRING |
| 251 | with self.assertRaises(AttributeError): |
| 252 | del Season.DRY |
| 253 | with self.assertRaises(AttributeError): |
| 254 | del Season.SPRING.name |
| 255 | |
Ethan Furman | 5de67b1 | 2016-04-13 23:52:09 -0700 | [diff] [blame] | 256 | def test_bool_of_class(self): |
| 257 | class Empty(Enum): |
| 258 | pass |
| 259 | self.assertTrue(bool(Empty)) |
| 260 | |
| 261 | def test_bool_of_member(self): |
| 262 | class Count(Enum): |
| 263 | zero = 0 |
| 264 | one = 1 |
| 265 | two = 2 |
| 266 | for member in Count: |
| 267 | self.assertTrue(bool(member)) |
| 268 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 269 | def test_invalid_names(self): |
| 270 | with self.assertRaises(ValueError): |
| 271 | class Wrong(Enum): |
| 272 | mro = 9 |
| 273 | with self.assertRaises(ValueError): |
| 274 | class Wrong(Enum): |
| 275 | _create_= 11 |
| 276 | with self.assertRaises(ValueError): |
| 277 | class Wrong(Enum): |
| 278 | _get_mixins_ = 9 |
| 279 | with self.assertRaises(ValueError): |
| 280 | class Wrong(Enum): |
| 281 | _find_new_ = 1 |
| 282 | with self.assertRaises(ValueError): |
| 283 | class Wrong(Enum): |
| 284 | _any_name_ = 9 |
| 285 | |
| 286 | def test_contains(self): |
| 287 | Season = self.Season |
| 288 | self.assertIn(Season.AUTUMN, Season) |
| 289 | self.assertNotIn(3, Season) |
| 290 | |
| 291 | val = Season(3) |
| 292 | self.assertIn(val, Season) |
| 293 | |
| 294 | class OtherEnum(Enum): |
| 295 | one = 1; two = 2 |
| 296 | self.assertNotIn(OtherEnum.two, Season) |
| 297 | |
| 298 | def test_comparisons(self): |
| 299 | Season = self.Season |
| 300 | with self.assertRaises(TypeError): |
| 301 | Season.SPRING < Season.WINTER |
| 302 | with self.assertRaises(TypeError): |
| 303 | Season.SPRING > 4 |
| 304 | |
| 305 | self.assertNotEqual(Season.SPRING, 1) |
| 306 | |
| 307 | class Part(Enum): |
| 308 | SPRING = 1 |
| 309 | CLIP = 2 |
| 310 | BARREL = 3 |
| 311 | |
| 312 | self.assertNotEqual(Season.SPRING, Part.SPRING) |
| 313 | with self.assertRaises(TypeError): |
| 314 | Season.SPRING < Part.CLIP |
| 315 | |
| 316 | def test_enum_duplicates(self): |
| 317 | class Season(Enum): |
| 318 | SPRING = 1 |
| 319 | SUMMER = 2 |
| 320 | AUTUMN = FALL = 3 |
| 321 | WINTER = 4 |
| 322 | ANOTHER_SPRING = 1 |
| 323 | lst = list(Season) |
| 324 | self.assertEqual( |
| 325 | lst, |
| 326 | [Season.SPRING, Season.SUMMER, |
| 327 | Season.AUTUMN, Season.WINTER, |
| 328 | ]) |
| 329 | self.assertIs(Season.FALL, Season.AUTUMN) |
| 330 | self.assertEqual(Season.FALL.value, 3) |
| 331 | self.assertEqual(Season.AUTUMN.value, 3) |
| 332 | self.assertIs(Season(3), Season.AUTUMN) |
| 333 | self.assertIs(Season(1), Season.SPRING) |
| 334 | self.assertEqual(Season.FALL.name, 'AUTUMN') |
| 335 | self.assertEqual( |
| 336 | [k for k,v in Season.__members__.items() if v.name != k], |
| 337 | ['FALL', 'ANOTHER_SPRING'], |
| 338 | ) |
| 339 | |
Ethan Furman | 101e074 | 2013-09-15 12:34:36 -0700 | [diff] [blame] | 340 | def test_duplicate_name(self): |
| 341 | with self.assertRaises(TypeError): |
| 342 | class Color(Enum): |
| 343 | red = 1 |
| 344 | green = 2 |
| 345 | blue = 3 |
| 346 | red = 4 |
| 347 | |
| 348 | with self.assertRaises(TypeError): |
| 349 | class Color(Enum): |
| 350 | red = 1 |
| 351 | green = 2 |
| 352 | blue = 3 |
| 353 | def red(self): |
| 354 | return 'red' |
| 355 | |
| 356 | with self.assertRaises(TypeError): |
| 357 | class Color(Enum): |
| 358 | @property |
| 359 | def red(self): |
| 360 | return 'redder' |
| 361 | red = 1 |
| 362 | green = 2 |
| 363 | blue = 3 |
| 364 | |
| 365 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 366 | def test_enum_with_value_name(self): |
| 367 | class Huh(Enum): |
| 368 | name = 1 |
| 369 | value = 2 |
| 370 | self.assertEqual( |
| 371 | list(Huh), |
| 372 | [Huh.name, Huh.value], |
| 373 | ) |
| 374 | self.assertIs(type(Huh.name), Huh) |
| 375 | self.assertEqual(Huh.name.name, 'name') |
| 376 | self.assertEqual(Huh.name.value, 1) |
Ethan Furman | ec15a82 | 2013-08-31 19:17:41 -0700 | [diff] [blame] | 377 | |
| 378 | def test_format_enum(self): |
| 379 | Season = self.Season |
| 380 | self.assertEqual('{}'.format(Season.SPRING), |
| 381 | '{}'.format(str(Season.SPRING))) |
| 382 | self.assertEqual( '{:}'.format(Season.SPRING), |
| 383 | '{:}'.format(str(Season.SPRING))) |
| 384 | self.assertEqual('{:20}'.format(Season.SPRING), |
| 385 | '{:20}'.format(str(Season.SPRING))) |
| 386 | self.assertEqual('{:^20}'.format(Season.SPRING), |
| 387 | '{:^20}'.format(str(Season.SPRING))) |
| 388 | self.assertEqual('{:>20}'.format(Season.SPRING), |
| 389 | '{:>20}'.format(str(Season.SPRING))) |
| 390 | self.assertEqual('{:<20}'.format(Season.SPRING), |
| 391 | '{:<20}'.format(str(Season.SPRING))) |
| 392 | |
| 393 | def test_format_enum_custom(self): |
| 394 | class TestFloat(float, Enum): |
| 395 | one = 1.0 |
| 396 | two = 2.0 |
| 397 | def __format__(self, spec): |
| 398 | return 'TestFloat success!' |
| 399 | self.assertEqual('{}'.format(TestFloat.one), 'TestFloat success!') |
| 400 | |
| 401 | def assertFormatIsValue(self, spec, member): |
| 402 | self.assertEqual(spec.format(member), spec.format(member.value)) |
| 403 | |
| 404 | def test_format_enum_date(self): |
| 405 | Holiday = self.Holiday |
| 406 | self.assertFormatIsValue('{}', Holiday.IDES_OF_MARCH) |
| 407 | self.assertFormatIsValue('{:}', Holiday.IDES_OF_MARCH) |
| 408 | self.assertFormatIsValue('{:20}', Holiday.IDES_OF_MARCH) |
| 409 | self.assertFormatIsValue('{:^20}', Holiday.IDES_OF_MARCH) |
| 410 | self.assertFormatIsValue('{:>20}', Holiday.IDES_OF_MARCH) |
| 411 | self.assertFormatIsValue('{:<20}', Holiday.IDES_OF_MARCH) |
| 412 | self.assertFormatIsValue('{:%Y %m}', Holiday.IDES_OF_MARCH) |
| 413 | self.assertFormatIsValue('{:%Y %m %M:00}', Holiday.IDES_OF_MARCH) |
| 414 | |
| 415 | def test_format_enum_float(self): |
| 416 | Konstants = self.Konstants |
| 417 | self.assertFormatIsValue('{}', Konstants.TAU) |
| 418 | self.assertFormatIsValue('{:}', Konstants.TAU) |
| 419 | self.assertFormatIsValue('{:20}', Konstants.TAU) |
| 420 | self.assertFormatIsValue('{:^20}', Konstants.TAU) |
| 421 | self.assertFormatIsValue('{:>20}', Konstants.TAU) |
| 422 | self.assertFormatIsValue('{:<20}', Konstants.TAU) |
| 423 | self.assertFormatIsValue('{:n}', Konstants.TAU) |
| 424 | self.assertFormatIsValue('{:5.2}', Konstants.TAU) |
| 425 | self.assertFormatIsValue('{:f}', Konstants.TAU) |
| 426 | |
| 427 | def test_format_enum_int(self): |
| 428 | Grades = self.Grades |
| 429 | self.assertFormatIsValue('{}', Grades.C) |
| 430 | self.assertFormatIsValue('{:}', Grades.C) |
| 431 | self.assertFormatIsValue('{:20}', Grades.C) |
| 432 | self.assertFormatIsValue('{:^20}', Grades.C) |
| 433 | self.assertFormatIsValue('{:>20}', Grades.C) |
| 434 | self.assertFormatIsValue('{:<20}', Grades.C) |
| 435 | self.assertFormatIsValue('{:+}', Grades.C) |
| 436 | self.assertFormatIsValue('{:08X}', Grades.C) |
| 437 | self.assertFormatIsValue('{:b}', Grades.C) |
| 438 | |
| 439 | def test_format_enum_str(self): |
| 440 | Directional = self.Directional |
| 441 | self.assertFormatIsValue('{}', Directional.WEST) |
| 442 | self.assertFormatIsValue('{:}', Directional.WEST) |
| 443 | self.assertFormatIsValue('{:20}', Directional.WEST) |
| 444 | self.assertFormatIsValue('{:^20}', Directional.WEST) |
| 445 | self.assertFormatIsValue('{:>20}', Directional.WEST) |
| 446 | self.assertFormatIsValue('{:<20}', Directional.WEST) |
| 447 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 448 | def test_hash(self): |
| 449 | Season = self.Season |
| 450 | dates = {} |
| 451 | dates[Season.WINTER] = '1225' |
| 452 | dates[Season.SPRING] = '0315' |
| 453 | dates[Season.SUMMER] = '0704' |
| 454 | dates[Season.AUTUMN] = '1031' |
| 455 | self.assertEqual(dates[Season.AUTUMN], '1031') |
| 456 | |
| 457 | def test_intenum_from_scratch(self): |
| 458 | class phy(int, Enum): |
| 459 | pi = 3 |
| 460 | tau = 2 * pi |
| 461 | self.assertTrue(phy.pi < phy.tau) |
| 462 | |
| 463 | def test_intenum_inherited(self): |
| 464 | class IntEnum(int, Enum): |
| 465 | pass |
| 466 | class phy(IntEnum): |
| 467 | pi = 3 |
| 468 | tau = 2 * pi |
| 469 | self.assertTrue(phy.pi < phy.tau) |
| 470 | |
| 471 | def test_floatenum_from_scratch(self): |
| 472 | class phy(float, Enum): |
Ethan Furman | ec15a82 | 2013-08-31 19:17:41 -0700 | [diff] [blame] | 473 | pi = 3.1415926 |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 474 | tau = 2 * pi |
| 475 | self.assertTrue(phy.pi < phy.tau) |
| 476 | |
| 477 | def test_floatenum_inherited(self): |
| 478 | class FloatEnum(float, Enum): |
| 479 | pass |
| 480 | class phy(FloatEnum): |
Ethan Furman | ec15a82 | 2013-08-31 19:17:41 -0700 | [diff] [blame] | 481 | pi = 3.1415926 |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 482 | tau = 2 * pi |
| 483 | self.assertTrue(phy.pi < phy.tau) |
| 484 | |
| 485 | def test_strenum_from_scratch(self): |
| 486 | class phy(str, Enum): |
| 487 | pi = 'Pi' |
| 488 | tau = 'Tau' |
| 489 | self.assertTrue(phy.pi < phy.tau) |
| 490 | |
| 491 | def test_strenum_inherited(self): |
| 492 | class StrEnum(str, Enum): |
| 493 | pass |
| 494 | class phy(StrEnum): |
| 495 | pi = 'Pi' |
| 496 | tau = 'Tau' |
| 497 | self.assertTrue(phy.pi < phy.tau) |
| 498 | |
| 499 | |
| 500 | def test_intenum(self): |
| 501 | class WeekDay(IntEnum): |
| 502 | SUNDAY = 1 |
| 503 | MONDAY = 2 |
| 504 | TUESDAY = 3 |
| 505 | WEDNESDAY = 4 |
| 506 | THURSDAY = 5 |
| 507 | FRIDAY = 6 |
| 508 | SATURDAY = 7 |
| 509 | |
| 510 | self.assertEqual(['a', 'b', 'c'][WeekDay.MONDAY], 'c') |
| 511 | self.assertEqual([i for i in range(WeekDay.TUESDAY)], [0, 1, 2]) |
| 512 | |
| 513 | lst = list(WeekDay) |
| 514 | self.assertEqual(len(lst), len(WeekDay)) |
| 515 | self.assertEqual(len(WeekDay), 7) |
| 516 | target = 'SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY' |
| 517 | target = target.split() |
| 518 | for i, weekday in enumerate(target, 1): |
| 519 | e = WeekDay(i) |
| 520 | self.assertEqual(e, i) |
| 521 | self.assertEqual(int(e), i) |
| 522 | self.assertEqual(e.name, weekday) |
| 523 | self.assertIn(e, WeekDay) |
| 524 | self.assertEqual(lst.index(e)+1, i) |
| 525 | self.assertTrue(0 < e < 8) |
| 526 | self.assertIs(type(e), WeekDay) |
| 527 | self.assertIsInstance(e, int) |
| 528 | self.assertIsInstance(e, Enum) |
| 529 | |
| 530 | def test_intenum_duplicates(self): |
| 531 | class WeekDay(IntEnum): |
| 532 | SUNDAY = 1 |
| 533 | MONDAY = 2 |
| 534 | TUESDAY = TEUSDAY = 3 |
| 535 | WEDNESDAY = 4 |
| 536 | THURSDAY = 5 |
| 537 | FRIDAY = 6 |
| 538 | SATURDAY = 7 |
| 539 | self.assertIs(WeekDay.TEUSDAY, WeekDay.TUESDAY) |
| 540 | self.assertEqual(WeekDay(3).name, 'TUESDAY') |
| 541 | self.assertEqual([k for k,v in WeekDay.__members__.items() |
| 542 | if v.name != k], ['TEUSDAY', ]) |
| 543 | |
Serhiy Storchaka | ea36c94 | 2016-05-12 10:37:58 +0300 | [diff] [blame] | 544 | def test_intenum_from_bytes(self): |
| 545 | self.assertIs(IntStooges.from_bytes(b'\x00\x03', 'big'), IntStooges.MOE) |
| 546 | with self.assertRaises(ValueError): |
| 547 | IntStooges.from_bytes(b'\x00\x05', 'big') |
| 548 | |
| 549 | def test_floatenum_fromhex(self): |
| 550 | h = float.hex(FloatStooges.MOE.value) |
| 551 | self.assertIs(FloatStooges.fromhex(h), FloatStooges.MOE) |
| 552 | h = float.hex(FloatStooges.MOE.value + 0.01) |
| 553 | with self.assertRaises(ValueError): |
| 554 | FloatStooges.fromhex(h) |
| 555 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 556 | def test_pickle_enum(self): |
| 557 | if isinstance(Stooges, Exception): |
| 558 | raise Stooges |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 559 | test_pickle_dump_load(self.assertIs, Stooges.CURLY) |
| 560 | test_pickle_dump_load(self.assertIs, Stooges) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 561 | |
| 562 | def test_pickle_int(self): |
| 563 | if isinstance(IntStooges, Exception): |
| 564 | raise IntStooges |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 565 | test_pickle_dump_load(self.assertIs, IntStooges.CURLY) |
| 566 | test_pickle_dump_load(self.assertIs, IntStooges) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 567 | |
| 568 | def test_pickle_float(self): |
| 569 | if isinstance(FloatStooges, Exception): |
| 570 | raise FloatStooges |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 571 | test_pickle_dump_load(self.assertIs, FloatStooges.CURLY) |
| 572 | test_pickle_dump_load(self.assertIs, FloatStooges) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 573 | |
| 574 | def test_pickle_enum_function(self): |
| 575 | if isinstance(Answer, Exception): |
| 576 | raise Answer |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 577 | test_pickle_dump_load(self.assertIs, Answer.him) |
| 578 | test_pickle_dump_load(self.assertIs, Answer) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 579 | |
| 580 | def test_pickle_enum_function_with_module(self): |
| 581 | if isinstance(Question, Exception): |
| 582 | raise Question |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 583 | test_pickle_dump_load(self.assertIs, Question.who) |
| 584 | test_pickle_dump_load(self.assertIs, Question) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 585 | |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 586 | def test_enum_function_with_qualname(self): |
| 587 | if isinstance(Theory, Exception): |
| 588 | raise Theory |
| 589 | self.assertEqual(Theory.__qualname__, 'spanish_inquisition') |
| 590 | |
| 591 | def test_class_nested_enum_and_pickle_protocol_four(self): |
| 592 | # would normally just have this directly in the class namespace |
| 593 | class NestedEnum(Enum): |
| 594 | twigs = 'common' |
| 595 | shiny = 'rare' |
| 596 | |
| 597 | self.__class__.NestedEnum = NestedEnum |
| 598 | self.NestedEnum.__qualname__ = '%s.NestedEnum' % self.__class__.__name__ |
Serhiy Storchaka | e50e780 | 2015-03-31 16:56:49 +0300 | [diff] [blame] | 599 | test_pickle_dump_load(self.assertIs, self.NestedEnum.twigs) |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 600 | |
Ethan Furman | 24e837f | 2015-03-18 17:27:57 -0700 | [diff] [blame] | 601 | def test_pickle_by_name(self): |
| 602 | class ReplaceGlobalInt(IntEnum): |
| 603 | ONE = 1 |
| 604 | TWO = 2 |
| 605 | ReplaceGlobalInt.__reduce_ex__ = enum._reduce_ex_by_name |
| 606 | for proto in range(HIGHEST_PROTOCOL): |
| 607 | self.assertEqual(ReplaceGlobalInt.TWO.__reduce_ex__(proto), 'TWO') |
| 608 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 609 | def test_exploding_pickle(self): |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 610 | BadPickle = Enum( |
| 611 | 'BadPickle', 'dill sweet bread-n-butter', module=__name__) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 612 | globals()['BadPickle'] = BadPickle |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 613 | # now break BadPickle to test exception raising |
| 614 | enum._make_class_unpicklable(BadPickle) |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 615 | test_pickle_exception(self.assertRaises, TypeError, BadPickle.dill) |
| 616 | test_pickle_exception(self.assertRaises, PicklingError, BadPickle) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 617 | |
| 618 | def test_string_enum(self): |
| 619 | class SkillLevel(str, Enum): |
| 620 | master = 'what is the sound of one hand clapping?' |
| 621 | journeyman = 'why did the chicken cross the road?' |
| 622 | apprentice = 'knock, knock!' |
| 623 | self.assertEqual(SkillLevel.apprentice, 'knock, knock!') |
| 624 | |
| 625 | def test_getattr_getitem(self): |
| 626 | class Period(Enum): |
| 627 | morning = 1 |
| 628 | noon = 2 |
| 629 | evening = 3 |
| 630 | night = 4 |
| 631 | self.assertIs(Period(2), Period.noon) |
| 632 | self.assertIs(getattr(Period, 'night'), Period.night) |
| 633 | self.assertIs(Period['morning'], Period.morning) |
| 634 | |
| 635 | def test_getattr_dunder(self): |
| 636 | Season = self.Season |
| 637 | self.assertTrue(getattr(Season, '__eq__')) |
| 638 | |
| 639 | def test_iteration_order(self): |
| 640 | class Season(Enum): |
| 641 | SUMMER = 2 |
| 642 | WINTER = 4 |
| 643 | AUTUMN = 3 |
| 644 | SPRING = 1 |
| 645 | self.assertEqual( |
| 646 | list(Season), |
| 647 | [Season.SUMMER, Season.WINTER, Season.AUTUMN, Season.SPRING], |
| 648 | ) |
| 649 | |
Ethan Furman | 2131a4a | 2013-09-14 18:11:24 -0700 | [diff] [blame] | 650 | def test_reversed_iteration_order(self): |
| 651 | self.assertEqual( |
| 652 | list(reversed(self.Season)), |
| 653 | [self.Season.WINTER, self.Season.AUTUMN, self.Season.SUMMER, |
| 654 | self.Season.SPRING] |
| 655 | ) |
| 656 | |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 657 | def test_programmatic_function_string(self): |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 658 | SummerMonth = Enum('SummerMonth', 'june july august') |
| 659 | lst = list(SummerMonth) |
| 660 | self.assertEqual(len(lst), len(SummerMonth)) |
| 661 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 662 | self.assertEqual( |
| 663 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 664 | lst, |
| 665 | ) |
| 666 | for i, month in enumerate('june july august'.split(), 1): |
| 667 | e = SummerMonth(i) |
| 668 | self.assertEqual(int(e.value), i) |
| 669 | self.assertNotEqual(e, i) |
| 670 | self.assertEqual(e.name, month) |
| 671 | self.assertIn(e, SummerMonth) |
| 672 | self.assertIs(type(e), SummerMonth) |
| 673 | |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 674 | def test_programmatic_function_string_with_start(self): |
Ethan Furman | d9925a1 | 2014-09-16 20:35:55 -0700 | [diff] [blame] | 675 | SummerMonth = Enum('SummerMonth', 'june july august', start=10) |
| 676 | lst = list(SummerMonth) |
| 677 | self.assertEqual(len(lst), len(SummerMonth)) |
| 678 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 679 | self.assertEqual( |
| 680 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 681 | lst, |
| 682 | ) |
| 683 | for i, month in enumerate('june july august'.split(), 10): |
| 684 | e = SummerMonth(i) |
| 685 | self.assertEqual(int(e.value), i) |
| 686 | self.assertNotEqual(e, i) |
| 687 | self.assertEqual(e.name, month) |
| 688 | self.assertIn(e, SummerMonth) |
| 689 | self.assertIs(type(e), SummerMonth) |
| 690 | |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 691 | def test_programmatic_function_string_list(self): |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 692 | SummerMonth = Enum('SummerMonth', ['june', 'july', 'august']) |
| 693 | lst = list(SummerMonth) |
| 694 | self.assertEqual(len(lst), len(SummerMonth)) |
| 695 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 696 | self.assertEqual( |
| 697 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 698 | lst, |
| 699 | ) |
| 700 | for i, month in enumerate('june july august'.split(), 1): |
| 701 | e = SummerMonth(i) |
| 702 | self.assertEqual(int(e.value), i) |
| 703 | self.assertNotEqual(e, i) |
| 704 | self.assertEqual(e.name, month) |
| 705 | self.assertIn(e, SummerMonth) |
| 706 | self.assertIs(type(e), SummerMonth) |
| 707 | |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 708 | def test_programmatic_function_string_list_with_start(self): |
Ethan Furman | d9925a1 | 2014-09-16 20:35:55 -0700 | [diff] [blame] | 709 | SummerMonth = Enum('SummerMonth', ['june', 'july', 'august'], start=20) |
| 710 | lst = list(SummerMonth) |
| 711 | self.assertEqual(len(lst), len(SummerMonth)) |
| 712 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 713 | self.assertEqual( |
| 714 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 715 | lst, |
| 716 | ) |
| 717 | for i, month in enumerate('june july august'.split(), 20): |
| 718 | e = SummerMonth(i) |
| 719 | self.assertEqual(int(e.value), i) |
| 720 | self.assertNotEqual(e, i) |
| 721 | self.assertEqual(e.name, month) |
| 722 | self.assertIn(e, SummerMonth) |
| 723 | self.assertIs(type(e), SummerMonth) |
| 724 | |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 725 | def test_programmatic_function_iterable(self): |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 726 | SummerMonth = Enum( |
| 727 | 'SummerMonth', |
| 728 | (('june', 1), ('july', 2), ('august', 3)) |
| 729 | ) |
| 730 | lst = list(SummerMonth) |
| 731 | self.assertEqual(len(lst), len(SummerMonth)) |
| 732 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 733 | self.assertEqual( |
| 734 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 735 | lst, |
| 736 | ) |
| 737 | for i, month in enumerate('june july august'.split(), 1): |
| 738 | e = SummerMonth(i) |
| 739 | self.assertEqual(int(e.value), i) |
| 740 | self.assertNotEqual(e, i) |
| 741 | self.assertEqual(e.name, month) |
| 742 | self.assertIn(e, SummerMonth) |
| 743 | self.assertIs(type(e), SummerMonth) |
| 744 | |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 745 | def test_programmatic_function_from_dict(self): |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 746 | SummerMonth = Enum( |
| 747 | 'SummerMonth', |
| 748 | OrderedDict((('june', 1), ('july', 2), ('august', 3))) |
| 749 | ) |
| 750 | lst = list(SummerMonth) |
| 751 | self.assertEqual(len(lst), len(SummerMonth)) |
| 752 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 753 | self.assertEqual( |
| 754 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 755 | lst, |
| 756 | ) |
| 757 | for i, month in enumerate('june july august'.split(), 1): |
| 758 | e = SummerMonth(i) |
| 759 | self.assertEqual(int(e.value), i) |
| 760 | self.assertNotEqual(e, i) |
| 761 | self.assertEqual(e.name, month) |
| 762 | self.assertIn(e, SummerMonth) |
| 763 | self.assertIs(type(e), SummerMonth) |
| 764 | |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 765 | def test_programmatic_function_type(self): |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 766 | SummerMonth = Enum('SummerMonth', 'june july august', type=int) |
| 767 | lst = list(SummerMonth) |
| 768 | self.assertEqual(len(lst), len(SummerMonth)) |
| 769 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 770 | self.assertEqual( |
| 771 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 772 | lst, |
| 773 | ) |
| 774 | for i, month in enumerate('june july august'.split(), 1): |
| 775 | e = SummerMonth(i) |
| 776 | self.assertEqual(e, i) |
| 777 | self.assertEqual(e.name, month) |
| 778 | self.assertIn(e, SummerMonth) |
| 779 | self.assertIs(type(e), SummerMonth) |
| 780 | |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 781 | def test_programmatic_function_type_with_start(self): |
Ethan Furman | d9925a1 | 2014-09-16 20:35:55 -0700 | [diff] [blame] | 782 | SummerMonth = Enum('SummerMonth', 'june july august', type=int, start=30) |
| 783 | lst = list(SummerMonth) |
| 784 | self.assertEqual(len(lst), len(SummerMonth)) |
| 785 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 786 | self.assertEqual( |
| 787 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 788 | lst, |
| 789 | ) |
| 790 | for i, month in enumerate('june july august'.split(), 30): |
| 791 | e = SummerMonth(i) |
| 792 | self.assertEqual(e, i) |
| 793 | self.assertEqual(e.name, month) |
| 794 | self.assertIn(e, SummerMonth) |
| 795 | self.assertIs(type(e), SummerMonth) |
| 796 | |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 797 | def test_programmatic_function_type_from_subclass(self): |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 798 | SummerMonth = IntEnum('SummerMonth', 'june july august') |
| 799 | lst = list(SummerMonth) |
| 800 | self.assertEqual(len(lst), len(SummerMonth)) |
| 801 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 802 | self.assertEqual( |
| 803 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 804 | lst, |
| 805 | ) |
| 806 | for i, month in enumerate('june july august'.split(), 1): |
| 807 | e = SummerMonth(i) |
| 808 | self.assertEqual(e, i) |
| 809 | self.assertEqual(e.name, month) |
| 810 | self.assertIn(e, SummerMonth) |
| 811 | self.assertIs(type(e), SummerMonth) |
| 812 | |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 813 | def test_programmatic_function_type_from_subclass_with_start(self): |
Ethan Furman | d9925a1 | 2014-09-16 20:35:55 -0700 | [diff] [blame] | 814 | SummerMonth = IntEnum('SummerMonth', 'june july august', start=40) |
| 815 | lst = list(SummerMonth) |
| 816 | self.assertEqual(len(lst), len(SummerMonth)) |
| 817 | self.assertEqual(len(SummerMonth), 3, SummerMonth) |
| 818 | self.assertEqual( |
| 819 | [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
| 820 | lst, |
| 821 | ) |
| 822 | for i, month in enumerate('june july august'.split(), 40): |
| 823 | e = SummerMonth(i) |
| 824 | self.assertEqual(e, i) |
| 825 | self.assertEqual(e.name, month) |
| 826 | self.assertIn(e, SummerMonth) |
| 827 | self.assertIs(type(e), SummerMonth) |
| 828 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 829 | def test_subclassing(self): |
| 830 | if isinstance(Name, Exception): |
| 831 | raise Name |
| 832 | self.assertEqual(Name.BDFL, 'Guido van Rossum') |
| 833 | self.assertTrue(Name.BDFL, Name('Guido van Rossum')) |
| 834 | self.assertIs(Name.BDFL, getattr(Name, 'BDFL')) |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 835 | test_pickle_dump_load(self.assertIs, Name.BDFL) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 836 | |
| 837 | def test_extending(self): |
| 838 | class Color(Enum): |
| 839 | red = 1 |
| 840 | green = 2 |
| 841 | blue = 3 |
| 842 | with self.assertRaises(TypeError): |
| 843 | class MoreColor(Color): |
| 844 | cyan = 4 |
| 845 | magenta = 5 |
| 846 | yellow = 6 |
| 847 | |
| 848 | def test_exclude_methods(self): |
| 849 | class whatever(Enum): |
| 850 | this = 'that' |
| 851 | these = 'those' |
| 852 | def really(self): |
| 853 | return 'no, not %s' % self.value |
| 854 | self.assertIsNot(type(whatever.really), whatever) |
| 855 | self.assertEqual(whatever.this.really(), 'no, not that') |
| 856 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 857 | def test_wrong_inheritance_order(self): |
| 858 | with self.assertRaises(TypeError): |
| 859 | class Wrong(Enum, str): |
| 860 | NotHere = 'error before this point' |
| 861 | |
| 862 | def test_intenum_transitivity(self): |
| 863 | class number(IntEnum): |
| 864 | one = 1 |
| 865 | two = 2 |
| 866 | three = 3 |
| 867 | class numero(IntEnum): |
| 868 | uno = 1 |
| 869 | dos = 2 |
| 870 | tres = 3 |
| 871 | self.assertEqual(number.one, numero.uno) |
| 872 | self.assertEqual(number.two, numero.dos) |
| 873 | self.assertEqual(number.three, numero.tres) |
| 874 | |
| 875 | def test_wrong_enum_in_call(self): |
| 876 | class Monochrome(Enum): |
| 877 | black = 0 |
| 878 | white = 1 |
| 879 | class Gender(Enum): |
| 880 | male = 0 |
| 881 | female = 1 |
| 882 | self.assertRaises(ValueError, Monochrome, Gender.male) |
| 883 | |
| 884 | def test_wrong_enum_in_mixed_call(self): |
| 885 | class Monochrome(IntEnum): |
| 886 | black = 0 |
| 887 | white = 1 |
| 888 | class Gender(Enum): |
| 889 | male = 0 |
| 890 | female = 1 |
| 891 | self.assertRaises(ValueError, Monochrome, Gender.male) |
| 892 | |
| 893 | def test_mixed_enum_in_call_1(self): |
| 894 | class Monochrome(IntEnum): |
| 895 | black = 0 |
| 896 | white = 1 |
| 897 | class Gender(IntEnum): |
| 898 | male = 0 |
| 899 | female = 1 |
| 900 | self.assertIs(Monochrome(Gender.female), Monochrome.white) |
| 901 | |
| 902 | def test_mixed_enum_in_call_2(self): |
| 903 | class Monochrome(Enum): |
| 904 | black = 0 |
| 905 | white = 1 |
| 906 | class Gender(IntEnum): |
| 907 | male = 0 |
| 908 | female = 1 |
| 909 | self.assertIs(Monochrome(Gender.male), Monochrome.black) |
| 910 | |
| 911 | def test_flufl_enum(self): |
| 912 | class Fluflnum(Enum): |
| 913 | def __int__(self): |
| 914 | return int(self.value) |
| 915 | class MailManOptions(Fluflnum): |
| 916 | option1 = 1 |
| 917 | option2 = 2 |
| 918 | option3 = 3 |
| 919 | self.assertEqual(int(MailManOptions.option1), 1) |
| 920 | |
Ethan Furman | 5e5a823 | 2013-08-04 08:42:23 -0700 | [diff] [blame] | 921 | def test_introspection(self): |
| 922 | class Number(IntEnum): |
| 923 | one = 100 |
| 924 | two = 200 |
| 925 | self.assertIs(Number.one._member_type_, int) |
| 926 | self.assertIs(Number._member_type_, int) |
| 927 | class String(str, Enum): |
| 928 | yarn = 'soft' |
| 929 | rope = 'rough' |
| 930 | wire = 'hard' |
| 931 | self.assertIs(String.yarn._member_type_, str) |
| 932 | self.assertIs(String._member_type_, str) |
| 933 | class Plain(Enum): |
| 934 | vanilla = 'white' |
| 935 | one = 1 |
| 936 | self.assertIs(Plain.vanilla._member_type_, object) |
| 937 | self.assertIs(Plain._member_type_, object) |
| 938 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 939 | def test_no_such_enum_member(self): |
| 940 | class Color(Enum): |
| 941 | red = 1 |
| 942 | green = 2 |
| 943 | blue = 3 |
| 944 | with self.assertRaises(ValueError): |
| 945 | Color(4) |
| 946 | with self.assertRaises(KeyError): |
| 947 | Color['chartreuse'] |
| 948 | |
| 949 | def test_new_repr(self): |
| 950 | class Color(Enum): |
| 951 | red = 1 |
| 952 | green = 2 |
| 953 | blue = 3 |
| 954 | def __repr__(self): |
| 955 | return "don't you just love shades of %s?" % self.name |
| 956 | self.assertEqual( |
| 957 | repr(Color.blue), |
| 958 | "don't you just love shades of blue?", |
| 959 | ) |
| 960 | |
| 961 | def test_inherited_repr(self): |
| 962 | class MyEnum(Enum): |
| 963 | def __repr__(self): |
| 964 | return "My name is %s." % self.name |
| 965 | class MyIntEnum(int, MyEnum): |
| 966 | this = 1 |
| 967 | that = 2 |
| 968 | theother = 3 |
| 969 | self.assertEqual(repr(MyIntEnum.that), "My name is that.") |
| 970 | |
| 971 | def test_multiple_mixin_mro(self): |
| 972 | class auto_enum(type(Enum)): |
| 973 | def __new__(metacls, cls, bases, classdict): |
| 974 | temp = type(classdict)() |
| 975 | names = set(classdict._member_names) |
| 976 | i = 0 |
| 977 | for k in classdict._member_names: |
| 978 | v = classdict[k] |
| 979 | if v is Ellipsis: |
| 980 | v = i |
| 981 | else: |
| 982 | i = v |
| 983 | i += 1 |
| 984 | temp[k] = v |
| 985 | for k, v in classdict.items(): |
| 986 | if k not in names: |
| 987 | temp[k] = v |
| 988 | return super(auto_enum, metacls).__new__( |
| 989 | metacls, cls, bases, temp) |
| 990 | |
| 991 | class AutoNumberedEnum(Enum, metaclass=auto_enum): |
| 992 | pass |
| 993 | |
| 994 | class AutoIntEnum(IntEnum, metaclass=auto_enum): |
| 995 | pass |
| 996 | |
| 997 | class TestAutoNumber(AutoNumberedEnum): |
| 998 | a = ... |
| 999 | b = 3 |
| 1000 | c = ... |
| 1001 | |
| 1002 | class TestAutoInt(AutoIntEnum): |
| 1003 | a = ... |
| 1004 | b = 3 |
| 1005 | c = ... |
| 1006 | |
| 1007 | def test_subclasses_with_getnewargs(self): |
| 1008 | class NamedInt(int): |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 1009 | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1010 | def __new__(cls, *args): |
| 1011 | _args = args |
| 1012 | name, *args = args |
| 1013 | if len(args) == 0: |
| 1014 | raise TypeError("name and value must be specified") |
| 1015 | self = int.__new__(cls, *args) |
| 1016 | self._intname = name |
| 1017 | self._args = _args |
| 1018 | return self |
| 1019 | def __getnewargs__(self): |
| 1020 | return self._args |
| 1021 | @property |
| 1022 | def __name__(self): |
| 1023 | return self._intname |
| 1024 | def __repr__(self): |
| 1025 | # repr() is updated to include the name and type info |
| 1026 | return "{}({!r}, {})".format(type(self).__name__, |
| 1027 | self.__name__, |
| 1028 | int.__repr__(self)) |
| 1029 | def __str__(self): |
| 1030 | # str() is unchanged, even if it relies on the repr() fallback |
| 1031 | base = int |
| 1032 | base_str = base.__str__ |
| 1033 | if base_str.__objclass__ is object: |
| 1034 | return base.__repr__(self) |
| 1035 | return base_str(self) |
| 1036 | # for simplicity, we only define one operator that |
| 1037 | # propagates expressions |
| 1038 | def __add__(self, other): |
| 1039 | temp = int(self) + int( other) |
| 1040 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 1041 | return NamedInt( |
| 1042 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 1043 | temp ) |
| 1044 | else: |
| 1045 | return temp |
| 1046 | |
| 1047 | class NEI(NamedInt, Enum): |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 1048 | __qualname__ = 'NEI' # needed for pickle protocol 4 |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1049 | x = ('the-x', 1) |
| 1050 | y = ('the-y', 2) |
| 1051 | |
Ethan Furman | 2aa2732 | 2013-07-19 19:35:56 -0700 | [diff] [blame] | 1052 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1053 | self.assertIs(NEI.__new__, Enum.__new__) |
| 1054 | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
| 1055 | globals()['NamedInt'] = NamedInt |
| 1056 | globals()['NEI'] = NEI |
| 1057 | NI5 = NamedInt('test', 5) |
| 1058 | self.assertEqual(NI5, 5) |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 1059 | test_pickle_dump_load(self.assertEqual, NI5, 5) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1060 | self.assertEqual(NEI.y.value, 2) |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 1061 | test_pickle_dump_load(self.assertIs, NEI.y) |
Ethan Furman | dc87052 | 2014-02-18 12:37:12 -0800 | [diff] [blame] | 1062 | test_pickle_dump_load(self.assertIs, NEI) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1063 | |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 1064 | def test_subclasses_with_getnewargs_ex(self): |
| 1065 | class NamedInt(int): |
| 1066 | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
| 1067 | def __new__(cls, *args): |
| 1068 | _args = args |
| 1069 | name, *args = args |
| 1070 | if len(args) == 0: |
| 1071 | raise TypeError("name and value must be specified") |
| 1072 | self = int.__new__(cls, *args) |
| 1073 | self._intname = name |
| 1074 | self._args = _args |
| 1075 | return self |
| 1076 | def __getnewargs_ex__(self): |
| 1077 | return self._args, {} |
| 1078 | @property |
| 1079 | def __name__(self): |
| 1080 | return self._intname |
| 1081 | def __repr__(self): |
| 1082 | # repr() is updated to include the name and type info |
| 1083 | return "{}({!r}, {})".format(type(self).__name__, |
| 1084 | self.__name__, |
| 1085 | int.__repr__(self)) |
| 1086 | def __str__(self): |
| 1087 | # str() is unchanged, even if it relies on the repr() fallback |
| 1088 | base = int |
| 1089 | base_str = base.__str__ |
| 1090 | if base_str.__objclass__ is object: |
| 1091 | return base.__repr__(self) |
| 1092 | return base_str(self) |
| 1093 | # for simplicity, we only define one operator that |
| 1094 | # propagates expressions |
| 1095 | def __add__(self, other): |
| 1096 | temp = int(self) + int( other) |
| 1097 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 1098 | return NamedInt( |
| 1099 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 1100 | temp ) |
| 1101 | else: |
| 1102 | return temp |
| 1103 | |
| 1104 | class NEI(NamedInt, Enum): |
| 1105 | __qualname__ = 'NEI' # needed for pickle protocol 4 |
| 1106 | x = ('the-x', 1) |
| 1107 | y = ('the-y', 2) |
| 1108 | |
| 1109 | |
| 1110 | self.assertIs(NEI.__new__, Enum.__new__) |
| 1111 | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
| 1112 | globals()['NamedInt'] = NamedInt |
| 1113 | globals()['NEI'] = NEI |
| 1114 | NI5 = NamedInt('test', 5) |
| 1115 | self.assertEqual(NI5, 5) |
Serhiy Storchaka | e50e780 | 2015-03-31 16:56:49 +0300 | [diff] [blame] | 1116 | test_pickle_dump_load(self.assertEqual, NI5, 5) |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 1117 | self.assertEqual(NEI.y.value, 2) |
Serhiy Storchaka | e50e780 | 2015-03-31 16:56:49 +0300 | [diff] [blame] | 1118 | test_pickle_dump_load(self.assertIs, NEI.y) |
Ethan Furman | dc87052 | 2014-02-18 12:37:12 -0800 | [diff] [blame] | 1119 | test_pickle_dump_load(self.assertIs, NEI) |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 1120 | |
| 1121 | def test_subclasses_with_reduce(self): |
| 1122 | class NamedInt(int): |
| 1123 | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
| 1124 | def __new__(cls, *args): |
| 1125 | _args = args |
| 1126 | name, *args = args |
| 1127 | if len(args) == 0: |
| 1128 | raise TypeError("name and value must be specified") |
| 1129 | self = int.__new__(cls, *args) |
| 1130 | self._intname = name |
| 1131 | self._args = _args |
| 1132 | return self |
| 1133 | def __reduce__(self): |
| 1134 | return self.__class__, self._args |
| 1135 | @property |
| 1136 | def __name__(self): |
| 1137 | return self._intname |
| 1138 | def __repr__(self): |
| 1139 | # repr() is updated to include the name and type info |
| 1140 | return "{}({!r}, {})".format(type(self).__name__, |
| 1141 | self.__name__, |
| 1142 | int.__repr__(self)) |
| 1143 | def __str__(self): |
| 1144 | # str() is unchanged, even if it relies on the repr() fallback |
| 1145 | base = int |
| 1146 | base_str = base.__str__ |
| 1147 | if base_str.__objclass__ is object: |
| 1148 | return base.__repr__(self) |
| 1149 | return base_str(self) |
| 1150 | # for simplicity, we only define one operator that |
| 1151 | # propagates expressions |
| 1152 | def __add__(self, other): |
| 1153 | temp = int(self) + int( other) |
| 1154 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 1155 | return NamedInt( |
| 1156 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 1157 | temp ) |
| 1158 | else: |
| 1159 | return temp |
| 1160 | |
| 1161 | class NEI(NamedInt, Enum): |
| 1162 | __qualname__ = 'NEI' # needed for pickle protocol 4 |
| 1163 | x = ('the-x', 1) |
| 1164 | y = ('the-y', 2) |
| 1165 | |
| 1166 | |
| 1167 | self.assertIs(NEI.__new__, Enum.__new__) |
| 1168 | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
| 1169 | globals()['NamedInt'] = NamedInt |
| 1170 | globals()['NEI'] = NEI |
| 1171 | NI5 = NamedInt('test', 5) |
| 1172 | self.assertEqual(NI5, 5) |
| 1173 | test_pickle_dump_load(self.assertEqual, NI5, 5) |
| 1174 | self.assertEqual(NEI.y.value, 2) |
| 1175 | test_pickle_dump_load(self.assertIs, NEI.y) |
Ethan Furman | dc87052 | 2014-02-18 12:37:12 -0800 | [diff] [blame] | 1176 | test_pickle_dump_load(self.assertIs, NEI) |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 1177 | |
| 1178 | def test_subclasses_with_reduce_ex(self): |
| 1179 | class NamedInt(int): |
| 1180 | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
| 1181 | def __new__(cls, *args): |
| 1182 | _args = args |
| 1183 | name, *args = args |
| 1184 | if len(args) == 0: |
| 1185 | raise TypeError("name and value must be specified") |
| 1186 | self = int.__new__(cls, *args) |
| 1187 | self._intname = name |
| 1188 | self._args = _args |
| 1189 | return self |
| 1190 | def __reduce_ex__(self, proto): |
| 1191 | return self.__class__, self._args |
| 1192 | @property |
| 1193 | def __name__(self): |
| 1194 | return self._intname |
| 1195 | def __repr__(self): |
| 1196 | # repr() is updated to include the name and type info |
| 1197 | return "{}({!r}, {})".format(type(self).__name__, |
| 1198 | self.__name__, |
| 1199 | int.__repr__(self)) |
| 1200 | def __str__(self): |
| 1201 | # str() is unchanged, even if it relies on the repr() fallback |
| 1202 | base = int |
| 1203 | base_str = base.__str__ |
| 1204 | if base_str.__objclass__ is object: |
| 1205 | return base.__repr__(self) |
| 1206 | return base_str(self) |
| 1207 | # for simplicity, we only define one operator that |
| 1208 | # propagates expressions |
| 1209 | def __add__(self, other): |
| 1210 | temp = int(self) + int( other) |
| 1211 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 1212 | return NamedInt( |
| 1213 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 1214 | temp ) |
| 1215 | else: |
| 1216 | return temp |
| 1217 | |
| 1218 | class NEI(NamedInt, Enum): |
| 1219 | __qualname__ = 'NEI' # needed for pickle protocol 4 |
| 1220 | x = ('the-x', 1) |
| 1221 | y = ('the-y', 2) |
| 1222 | |
| 1223 | |
| 1224 | self.assertIs(NEI.__new__, Enum.__new__) |
| 1225 | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
| 1226 | globals()['NamedInt'] = NamedInt |
| 1227 | globals()['NEI'] = NEI |
| 1228 | NI5 = NamedInt('test', 5) |
| 1229 | self.assertEqual(NI5, 5) |
| 1230 | test_pickle_dump_load(self.assertEqual, NI5, 5) |
| 1231 | self.assertEqual(NEI.y.value, 2) |
| 1232 | test_pickle_dump_load(self.assertIs, NEI.y) |
Ethan Furman | dc87052 | 2014-02-18 12:37:12 -0800 | [diff] [blame] | 1233 | test_pickle_dump_load(self.assertIs, NEI) |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 1234 | |
Ethan Furman | dc87052 | 2014-02-18 12:37:12 -0800 | [diff] [blame] | 1235 | def test_subclasses_without_direct_pickle_support(self): |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1236 | class NamedInt(int): |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 1237 | __qualname__ = 'NamedInt' |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1238 | def __new__(cls, *args): |
| 1239 | _args = args |
| 1240 | name, *args = args |
| 1241 | if len(args) == 0: |
| 1242 | raise TypeError("name and value must be specified") |
| 1243 | self = int.__new__(cls, *args) |
| 1244 | self._intname = name |
| 1245 | self._args = _args |
| 1246 | return self |
| 1247 | @property |
| 1248 | def __name__(self): |
| 1249 | return self._intname |
| 1250 | def __repr__(self): |
| 1251 | # repr() is updated to include the name and type info |
| 1252 | return "{}({!r}, {})".format(type(self).__name__, |
| 1253 | self.__name__, |
| 1254 | int.__repr__(self)) |
| 1255 | def __str__(self): |
| 1256 | # str() is unchanged, even if it relies on the repr() fallback |
| 1257 | base = int |
| 1258 | base_str = base.__str__ |
| 1259 | if base_str.__objclass__ is object: |
| 1260 | return base.__repr__(self) |
| 1261 | return base_str(self) |
| 1262 | # for simplicity, we only define one operator that |
| 1263 | # propagates expressions |
| 1264 | def __add__(self, other): |
| 1265 | temp = int(self) + int( other) |
| 1266 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 1267 | return NamedInt( |
| 1268 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 1269 | temp ) |
| 1270 | else: |
| 1271 | return temp |
| 1272 | |
| 1273 | class NEI(NamedInt, Enum): |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 1274 | __qualname__ = 'NEI' |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1275 | x = ('the-x', 1) |
| 1276 | y = ('the-y', 2) |
| 1277 | |
| 1278 | self.assertIs(NEI.__new__, Enum.__new__) |
| 1279 | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
| 1280 | globals()['NamedInt'] = NamedInt |
| 1281 | globals()['NEI'] = NEI |
| 1282 | NI5 = NamedInt('test', 5) |
| 1283 | self.assertEqual(NI5, 5) |
| 1284 | self.assertEqual(NEI.y.value, 2) |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 1285 | test_pickle_exception(self.assertRaises, TypeError, NEI.x) |
| 1286 | test_pickle_exception(self.assertRaises, PicklingError, NEI) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1287 | |
Ethan Furman | dc87052 | 2014-02-18 12:37:12 -0800 | [diff] [blame] | 1288 | def test_subclasses_without_direct_pickle_support_using_name(self): |
| 1289 | class NamedInt(int): |
| 1290 | __qualname__ = 'NamedInt' |
| 1291 | def __new__(cls, *args): |
| 1292 | _args = args |
| 1293 | name, *args = args |
| 1294 | if len(args) == 0: |
| 1295 | raise TypeError("name and value must be specified") |
| 1296 | self = int.__new__(cls, *args) |
| 1297 | self._intname = name |
| 1298 | self._args = _args |
| 1299 | return self |
| 1300 | @property |
| 1301 | def __name__(self): |
| 1302 | return self._intname |
| 1303 | def __repr__(self): |
| 1304 | # repr() is updated to include the name and type info |
| 1305 | return "{}({!r}, {})".format(type(self).__name__, |
| 1306 | self.__name__, |
| 1307 | int.__repr__(self)) |
| 1308 | def __str__(self): |
| 1309 | # str() is unchanged, even if it relies on the repr() fallback |
| 1310 | base = int |
| 1311 | base_str = base.__str__ |
| 1312 | if base_str.__objclass__ is object: |
| 1313 | return base.__repr__(self) |
| 1314 | return base_str(self) |
| 1315 | # for simplicity, we only define one operator that |
| 1316 | # propagates expressions |
| 1317 | def __add__(self, other): |
| 1318 | temp = int(self) + int( other) |
| 1319 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 1320 | return NamedInt( |
| 1321 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 1322 | temp ) |
| 1323 | else: |
| 1324 | return temp |
| 1325 | |
| 1326 | class NEI(NamedInt, Enum): |
| 1327 | __qualname__ = 'NEI' |
| 1328 | x = ('the-x', 1) |
| 1329 | y = ('the-y', 2) |
| 1330 | def __reduce_ex__(self, proto): |
| 1331 | return getattr, (self.__class__, self._name_) |
| 1332 | |
| 1333 | self.assertIs(NEI.__new__, Enum.__new__) |
| 1334 | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
| 1335 | globals()['NamedInt'] = NamedInt |
| 1336 | globals()['NEI'] = NEI |
| 1337 | NI5 = NamedInt('test', 5) |
| 1338 | self.assertEqual(NI5, 5) |
| 1339 | self.assertEqual(NEI.y.value, 2) |
| 1340 | test_pickle_dump_load(self.assertIs, NEI.y) |
| 1341 | test_pickle_dump_load(self.assertIs, NEI) |
| 1342 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1343 | def test_tuple_subclass(self): |
| 1344 | class SomeTuple(tuple, Enum): |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 1345 | __qualname__ = 'SomeTuple' # needed for pickle protocol 4 |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1346 | first = (1, 'for the money') |
| 1347 | second = (2, 'for the show') |
| 1348 | third = (3, 'for the music') |
| 1349 | self.assertIs(type(SomeTuple.first), SomeTuple) |
| 1350 | self.assertIsInstance(SomeTuple.second, tuple) |
| 1351 | self.assertEqual(SomeTuple.third, (3, 'for the music')) |
| 1352 | globals()['SomeTuple'] = SomeTuple |
Ethan Furman | 2ddb39a | 2014-02-06 17:28:50 -0800 | [diff] [blame] | 1353 | test_pickle_dump_load(self.assertIs, SomeTuple.first) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1354 | |
| 1355 | def test_duplicate_values_give_unique_enum_items(self): |
| 1356 | class AutoNumber(Enum): |
| 1357 | first = () |
| 1358 | second = () |
| 1359 | third = () |
| 1360 | def __new__(cls): |
| 1361 | value = len(cls.__members__) + 1 |
| 1362 | obj = object.__new__(cls) |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 1363 | obj._value_ = value |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1364 | return obj |
| 1365 | def __int__(self): |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 1366 | return int(self._value_) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1367 | self.assertEqual( |
| 1368 | list(AutoNumber), |
| 1369 | [AutoNumber.first, AutoNumber.second, AutoNumber.third], |
| 1370 | ) |
| 1371 | self.assertEqual(int(AutoNumber.second), 2) |
Ethan Furman | 2aa2732 | 2013-07-19 19:35:56 -0700 | [diff] [blame] | 1372 | self.assertEqual(AutoNumber.third.value, 3) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1373 | self.assertIs(AutoNumber(1), AutoNumber.first) |
| 1374 | |
| 1375 | def test_inherited_new_from_enhanced_enum(self): |
| 1376 | class AutoNumber(Enum): |
| 1377 | def __new__(cls): |
| 1378 | value = len(cls.__members__) + 1 |
| 1379 | obj = object.__new__(cls) |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 1380 | obj._value_ = value |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1381 | return obj |
| 1382 | def __int__(self): |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 1383 | return int(self._value_) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1384 | class Color(AutoNumber): |
| 1385 | red = () |
| 1386 | green = () |
| 1387 | blue = () |
| 1388 | self.assertEqual(list(Color), [Color.red, Color.green, Color.blue]) |
| 1389 | self.assertEqual(list(map(int, Color)), [1, 2, 3]) |
| 1390 | |
| 1391 | def test_inherited_new_from_mixed_enum(self): |
| 1392 | class AutoNumber(IntEnum): |
| 1393 | def __new__(cls): |
| 1394 | value = len(cls.__members__) + 1 |
| 1395 | obj = int.__new__(cls, value) |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 1396 | obj._value_ = value |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1397 | return obj |
| 1398 | class Color(AutoNumber): |
| 1399 | red = () |
| 1400 | green = () |
| 1401 | blue = () |
| 1402 | self.assertEqual(list(Color), [Color.red, Color.green, Color.blue]) |
| 1403 | self.assertEqual(list(map(int, Color)), [1, 2, 3]) |
| 1404 | |
Ethan Furman | be3c2fe | 2013-11-13 14:25:45 -0800 | [diff] [blame] | 1405 | def test_equality(self): |
| 1406 | class AlwaysEqual: |
| 1407 | def __eq__(self, other): |
| 1408 | return True |
| 1409 | class OrdinaryEnum(Enum): |
| 1410 | a = 1 |
| 1411 | self.assertEqual(AlwaysEqual(), OrdinaryEnum.a) |
| 1412 | self.assertEqual(OrdinaryEnum.a, AlwaysEqual()) |
| 1413 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1414 | def test_ordered_mixin(self): |
| 1415 | class OrderedEnum(Enum): |
| 1416 | def __ge__(self, other): |
| 1417 | if self.__class__ is other.__class__: |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 1418 | return self._value_ >= other._value_ |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1419 | return NotImplemented |
| 1420 | def __gt__(self, other): |
| 1421 | if self.__class__ is other.__class__: |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 1422 | return self._value_ > other._value_ |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1423 | return NotImplemented |
| 1424 | def __le__(self, other): |
| 1425 | if self.__class__ is other.__class__: |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 1426 | return self._value_ <= other._value_ |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1427 | return NotImplemented |
| 1428 | def __lt__(self, other): |
| 1429 | if self.__class__ is other.__class__: |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 1430 | return self._value_ < other._value_ |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1431 | return NotImplemented |
| 1432 | class Grade(OrderedEnum): |
| 1433 | A = 5 |
| 1434 | B = 4 |
| 1435 | C = 3 |
| 1436 | D = 2 |
| 1437 | F = 1 |
| 1438 | self.assertGreater(Grade.A, Grade.B) |
| 1439 | self.assertLessEqual(Grade.F, Grade.C) |
| 1440 | self.assertLess(Grade.D, Grade.A) |
| 1441 | self.assertGreaterEqual(Grade.B, Grade.B) |
Ethan Furman | be3c2fe | 2013-11-13 14:25:45 -0800 | [diff] [blame] | 1442 | self.assertEqual(Grade.B, Grade.B) |
| 1443 | self.assertNotEqual(Grade.C, Grade.D) |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 1444 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1445 | def test_extending2(self): |
| 1446 | class Shade(Enum): |
| 1447 | def shade(self): |
| 1448 | print(self.name) |
| 1449 | class Color(Shade): |
| 1450 | red = 1 |
| 1451 | green = 2 |
| 1452 | blue = 3 |
| 1453 | with self.assertRaises(TypeError): |
| 1454 | class MoreColor(Color): |
| 1455 | cyan = 4 |
| 1456 | magenta = 5 |
| 1457 | yellow = 6 |
| 1458 | |
| 1459 | def test_extending3(self): |
| 1460 | class Shade(Enum): |
| 1461 | def shade(self): |
| 1462 | return self.name |
| 1463 | class Color(Shade): |
| 1464 | def hex(self): |
| 1465 | return '%s hexlified!' % self.value |
| 1466 | class MoreColor(Color): |
| 1467 | cyan = 4 |
| 1468 | magenta = 5 |
| 1469 | yellow = 6 |
| 1470 | self.assertEqual(MoreColor.magenta.hex(), '5 hexlified!') |
| 1471 | |
| 1472 | |
| 1473 | def test_no_duplicates(self): |
| 1474 | class UniqueEnum(Enum): |
| 1475 | def __init__(self, *args): |
| 1476 | cls = self.__class__ |
| 1477 | if any(self.value == e.value for e in cls): |
| 1478 | a = self.name |
| 1479 | e = cls(self.value).name |
| 1480 | raise ValueError( |
| 1481 | "aliases not allowed in UniqueEnum: %r --> %r" |
| 1482 | % (a, e) |
| 1483 | ) |
| 1484 | class Color(UniqueEnum): |
| 1485 | red = 1 |
| 1486 | green = 2 |
| 1487 | blue = 3 |
| 1488 | with self.assertRaises(ValueError): |
| 1489 | class Color(UniqueEnum): |
| 1490 | red = 1 |
| 1491 | green = 2 |
| 1492 | blue = 3 |
| 1493 | grene = 2 |
| 1494 | |
| 1495 | def test_init(self): |
| 1496 | class Planet(Enum): |
| 1497 | MERCURY = (3.303e+23, 2.4397e6) |
| 1498 | VENUS = (4.869e+24, 6.0518e6) |
| 1499 | EARTH = (5.976e+24, 6.37814e6) |
| 1500 | MARS = (6.421e+23, 3.3972e6) |
| 1501 | JUPITER = (1.9e+27, 7.1492e7) |
| 1502 | SATURN = (5.688e+26, 6.0268e7) |
| 1503 | URANUS = (8.686e+25, 2.5559e7) |
| 1504 | NEPTUNE = (1.024e+26, 2.4746e7) |
| 1505 | def __init__(self, mass, radius): |
| 1506 | self.mass = mass # in kilograms |
| 1507 | self.radius = radius # in meters |
| 1508 | @property |
| 1509 | def surface_gravity(self): |
| 1510 | # universal gravitational constant (m3 kg-1 s-2) |
| 1511 | G = 6.67300E-11 |
| 1512 | return G * self.mass / (self.radius * self.radius) |
| 1513 | self.assertEqual(round(Planet.EARTH.surface_gravity, 2), 9.80) |
| 1514 | self.assertEqual(Planet.EARTH.value, (5.976e+24, 6.37814e6)) |
| 1515 | |
Ethan Furman | 2aa2732 | 2013-07-19 19:35:56 -0700 | [diff] [blame] | 1516 | def test_nonhash_value(self): |
| 1517 | class AutoNumberInAList(Enum): |
| 1518 | def __new__(cls): |
| 1519 | value = [len(cls.__members__) + 1] |
| 1520 | obj = object.__new__(cls) |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 1521 | obj._value_ = value |
Ethan Furman | 2aa2732 | 2013-07-19 19:35:56 -0700 | [diff] [blame] | 1522 | return obj |
| 1523 | class ColorInAList(AutoNumberInAList): |
| 1524 | red = () |
| 1525 | green = () |
| 1526 | blue = () |
| 1527 | self.assertEqual(list(ColorInAList), [ColorInAList.red, ColorInAList.green, ColorInAList.blue]) |
Ethan Furman | 1a16288 | 2013-10-16 19:09:31 -0700 | [diff] [blame] | 1528 | for enum, value in zip(ColorInAList, range(3)): |
| 1529 | value += 1 |
| 1530 | self.assertEqual(enum.value, [value]) |
| 1531 | self.assertIs(ColorInAList([value]), enum) |
Ethan Furman | 2aa2732 | 2013-07-19 19:35:56 -0700 | [diff] [blame] | 1532 | |
Ethan Furman | b41803e | 2013-07-25 13:50:45 -0700 | [diff] [blame] | 1533 | def test_conflicting_types_resolved_in_new(self): |
| 1534 | class LabelledIntEnum(int, Enum): |
| 1535 | def __new__(cls, *args): |
| 1536 | value, label = args |
| 1537 | obj = int.__new__(cls, value) |
| 1538 | obj.label = label |
| 1539 | obj._value_ = value |
| 1540 | return obj |
| 1541 | |
| 1542 | class LabelledList(LabelledIntEnum): |
| 1543 | unprocessed = (1, "Unprocessed") |
| 1544 | payment_complete = (2, "Payment Complete") |
| 1545 | |
| 1546 | self.assertEqual(list(LabelledList), [LabelledList.unprocessed, LabelledList.payment_complete]) |
| 1547 | self.assertEqual(LabelledList.unprocessed, 1) |
| 1548 | self.assertEqual(LabelledList(1), LabelledList.unprocessed) |
Ethan Furman | 2aa2732 | 2013-07-19 19:35:56 -0700 | [diff] [blame] | 1549 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1550 | |
Ethan Furman | f24bb35 | 2013-07-18 17:05:39 -0700 | [diff] [blame] | 1551 | class TestUnique(unittest.TestCase): |
| 1552 | |
| 1553 | def test_unique_clean(self): |
| 1554 | @unique |
| 1555 | class Clean(Enum): |
| 1556 | one = 1 |
| 1557 | two = 'dos' |
| 1558 | tres = 4.0 |
| 1559 | @unique |
| 1560 | class Cleaner(IntEnum): |
| 1561 | single = 1 |
| 1562 | double = 2 |
| 1563 | triple = 3 |
| 1564 | |
| 1565 | def test_unique_dirty(self): |
| 1566 | with self.assertRaisesRegex(ValueError, 'tres.*one'): |
| 1567 | @unique |
| 1568 | class Dirty(Enum): |
| 1569 | one = 1 |
| 1570 | two = 'dos' |
| 1571 | tres = 1 |
| 1572 | with self.assertRaisesRegex( |
| 1573 | ValueError, |
| 1574 | 'double.*single.*turkey.*triple', |
| 1575 | ): |
| 1576 | @unique |
| 1577 | class Dirtier(IntEnum): |
| 1578 | single = 1 |
| 1579 | double = 1 |
| 1580 | triple = 3 |
| 1581 | turkey = 3 |
| 1582 | |
Ethan Furman | 3803ad4 | 2016-05-01 10:03:53 -0700 | [diff] [blame] | 1583 | def test_unique_with_name(self): |
| 1584 | @unique |
| 1585 | class Silly(Enum): |
| 1586 | one = 1 |
| 1587 | two = 'dos' |
| 1588 | name = 3 |
| 1589 | @unique |
| 1590 | class Sillier(IntEnum): |
| 1591 | single = 1 |
| 1592 | name = 2 |
| 1593 | triple = 3 |
| 1594 | value = 4 |
| 1595 | |
Ethan Furman | f24bb35 | 2013-07-18 17:05:39 -0700 | [diff] [blame] | 1596 | |
Ethan Furman | 3323da9 | 2015-04-11 09:39:59 -0700 | [diff] [blame] | 1597 | expected_help_output_with_docs = """\ |
Ethan Furman | 5875d74 | 2013-10-21 20:45:55 -0700 | [diff] [blame] | 1598 | Help on class Color in module %s: |
| 1599 | |
| 1600 | class Color(enum.Enum) |
Ethan Furman | 48a724f | 2015-04-11 23:23:06 -0700 | [diff] [blame] | 1601 | | An enumeration. |
Serhiy Storchaka | b599ca8 | 2015-04-04 12:48:04 +0300 | [diff] [blame] | 1602 | |\x20\x20 |
Ethan Furman | 5875d74 | 2013-10-21 20:45:55 -0700 | [diff] [blame] | 1603 | | Method resolution order: |
| 1604 | | Color |
| 1605 | | enum.Enum |
| 1606 | | builtins.object |
| 1607 | |\x20\x20 |
| 1608 | | Data and other attributes defined here: |
| 1609 | |\x20\x20 |
| 1610 | | blue = <Color.blue: 3> |
| 1611 | |\x20\x20 |
| 1612 | | green = <Color.green: 2> |
| 1613 | |\x20\x20 |
| 1614 | | red = <Color.red: 1> |
| 1615 | |\x20\x20 |
| 1616 | | ---------------------------------------------------------------------- |
| 1617 | | Data descriptors inherited from enum.Enum: |
| 1618 | |\x20\x20 |
| 1619 | | name |
| 1620 | | The name of the Enum member. |
| 1621 | |\x20\x20 |
| 1622 | | value |
| 1623 | | The value of the Enum member. |
| 1624 | |\x20\x20 |
| 1625 | | ---------------------------------------------------------------------- |
| 1626 | | Data descriptors inherited from enum.EnumMeta: |
| 1627 | |\x20\x20 |
| 1628 | | __members__ |
| 1629 | | Returns a mapping of member name->value. |
| 1630 | |\x20\x20\x20\x20\x20\x20 |
| 1631 | | This mapping lists all enum members, including aliases. Note that this |
Ethan Furman | 3323da9 | 2015-04-11 09:39:59 -0700 | [diff] [blame] | 1632 | | is a read-only view of the internal mapping.""" |
| 1633 | |
| 1634 | expected_help_output_without_docs = """\ |
| 1635 | Help on class Color in module %s: |
| 1636 | |
| 1637 | class Color(enum.Enum) |
| 1638 | | Method resolution order: |
| 1639 | | Color |
| 1640 | | enum.Enum |
| 1641 | | builtins.object |
| 1642 | |\x20\x20 |
| 1643 | | Data and other attributes defined here: |
| 1644 | |\x20\x20 |
| 1645 | | blue = <Color.blue: 3> |
| 1646 | |\x20\x20 |
| 1647 | | green = <Color.green: 2> |
| 1648 | |\x20\x20 |
| 1649 | | red = <Color.red: 1> |
| 1650 | |\x20\x20 |
| 1651 | | ---------------------------------------------------------------------- |
| 1652 | | Data descriptors inherited from enum.Enum: |
| 1653 | |\x20\x20 |
| 1654 | | name |
| 1655 | |\x20\x20 |
| 1656 | | value |
| 1657 | |\x20\x20 |
| 1658 | | ---------------------------------------------------------------------- |
| 1659 | | Data descriptors inherited from enum.EnumMeta: |
| 1660 | |\x20\x20 |
| 1661 | | __members__""" |
Ethan Furman | 5875d74 | 2013-10-21 20:45:55 -0700 | [diff] [blame] | 1662 | |
| 1663 | class TestStdLib(unittest.TestCase): |
| 1664 | |
Ethan Furman | 48a724f | 2015-04-11 23:23:06 -0700 | [diff] [blame] | 1665 | maxDiff = None |
| 1666 | |
Ethan Furman | 5875d74 | 2013-10-21 20:45:55 -0700 | [diff] [blame] | 1667 | class Color(Enum): |
| 1668 | red = 1 |
| 1669 | green = 2 |
| 1670 | blue = 3 |
| 1671 | |
| 1672 | def test_pydoc(self): |
| 1673 | # indirectly test __objclass__ |
Ethan Furman | 3323da9 | 2015-04-11 09:39:59 -0700 | [diff] [blame] | 1674 | if StrEnum.__doc__ is None: |
| 1675 | expected_text = expected_help_output_without_docs % __name__ |
| 1676 | else: |
| 1677 | expected_text = expected_help_output_with_docs % __name__ |
Ethan Furman | 5875d74 | 2013-10-21 20:45:55 -0700 | [diff] [blame] | 1678 | output = StringIO() |
| 1679 | helper = pydoc.Helper(output=output) |
| 1680 | helper(self.Color) |
| 1681 | result = output.getvalue().strip() |
Victor Stinner | 4b0432d | 2014-06-16 22:48:43 +0200 | [diff] [blame] | 1682 | self.assertEqual(result, expected_text) |
Ethan Furman | 5875d74 | 2013-10-21 20:45:55 -0700 | [diff] [blame] | 1683 | |
| 1684 | def test_inspect_getmembers(self): |
| 1685 | values = dict(( |
| 1686 | ('__class__', EnumMeta), |
Ethan Furman | 48a724f | 2015-04-11 23:23:06 -0700 | [diff] [blame] | 1687 | ('__doc__', 'An enumeration.'), |
Ethan Furman | 5875d74 | 2013-10-21 20:45:55 -0700 | [diff] [blame] | 1688 | ('__members__', self.Color.__members__), |
| 1689 | ('__module__', __name__), |
| 1690 | ('blue', self.Color.blue), |
| 1691 | ('green', self.Color.green), |
| 1692 | ('name', Enum.__dict__['name']), |
| 1693 | ('red', self.Color.red), |
| 1694 | ('value', Enum.__dict__['value']), |
| 1695 | )) |
| 1696 | result = dict(inspect.getmembers(self.Color)) |
| 1697 | self.assertEqual(values.keys(), result.keys()) |
| 1698 | failed = False |
| 1699 | for k in values.keys(): |
| 1700 | if result[k] != values[k]: |
| 1701 | print() |
| 1702 | print('\n%s\n key: %s\n result: %s\nexpected: %s\n%s\n' % |
| 1703 | ('=' * 75, k, result[k], values[k], '=' * 75), sep='') |
| 1704 | failed = True |
| 1705 | if failed: |
| 1706 | self.fail("result does not equal expected, see print above") |
| 1707 | |
| 1708 | def test_inspect_classify_class_attrs(self): |
| 1709 | # indirectly test __objclass__ |
| 1710 | from inspect import Attribute |
| 1711 | values = [ |
| 1712 | Attribute(name='__class__', kind='data', |
| 1713 | defining_class=object, object=EnumMeta), |
| 1714 | Attribute(name='__doc__', kind='data', |
Ethan Furman | 48a724f | 2015-04-11 23:23:06 -0700 | [diff] [blame] | 1715 | defining_class=self.Color, object='An enumeration.'), |
Ethan Furman | 5875d74 | 2013-10-21 20:45:55 -0700 | [diff] [blame] | 1716 | Attribute(name='__members__', kind='property', |
| 1717 | defining_class=EnumMeta, object=EnumMeta.__members__), |
| 1718 | Attribute(name='__module__', kind='data', |
| 1719 | defining_class=self.Color, object=__name__), |
| 1720 | Attribute(name='blue', kind='data', |
| 1721 | defining_class=self.Color, object=self.Color.blue), |
| 1722 | Attribute(name='green', kind='data', |
| 1723 | defining_class=self.Color, object=self.Color.green), |
| 1724 | Attribute(name='red', kind='data', |
| 1725 | defining_class=self.Color, object=self.Color.red), |
| 1726 | Attribute(name='name', kind='data', |
| 1727 | defining_class=Enum, object=Enum.__dict__['name']), |
| 1728 | Attribute(name='value', kind='data', |
| 1729 | defining_class=Enum, object=Enum.__dict__['value']), |
| 1730 | ] |
| 1731 | values.sort(key=lambda item: item.name) |
| 1732 | result = list(inspect.classify_class_attrs(self.Color)) |
| 1733 | result.sort(key=lambda item: item.name) |
| 1734 | failed = False |
| 1735 | for v, r in zip(values, result): |
| 1736 | if r != v: |
| 1737 | print('\n%s\n%s\n%s\n%s\n' % ('=' * 75, r, v, '=' * 75), sep='') |
| 1738 | failed = True |
| 1739 | if failed: |
| 1740 | self.fail("result does not equal expected, see print above") |
| 1741 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1742 | if __name__ == '__main__': |
| 1743 | unittest.main() |