def test_create(): def test_exact_gender(cls): assert_eq([cls.male, cls.female], cls.get_members()) assert_eq([cls.male, cls.female], list(cls)) assert_eq(1, cls.male) assert_eq(2, cls.female) assert_eq(cls.male, Gender.male) assert_eq(cls.female, Gender.female) assert_in(cls.male, cls) assert_eq(cls.male, cls.parse(1)) assert_eq(cls.male, cls.parse("male")) assert_eq(cls.male, cls.parse(cls.male)) assert_eq(cls.male, cls(1)) assert_eq(cls.male, cls("male")) assert_eq(cls.male, cls(cls.male)) class ExactGender(Enum): male = Gender.male female = Gender.female test_exact_gender(ExactGender) cls = Enum.create("ExactGender", [Gender.male, Gender.female]) test_exact_gender(cls) cls = Enum.create("ExactGender", {"male": 1, "female": 2}) test_exact_gender(cls) cls = Enum.create("ExactGender", [("male", 1), ("female", 2)]) test_exact_gender(cls)
member2 = 1 assert_raises(declare_bad_enum, AssertionError) try: _long_type = long except NameError: _long_type = int class LongEnum(Enum): x = _long_type(100) def test_long_enum(): assert_is_instance(LongEnum.x, LongEnum) DynamicEnum = Enum.create('DynamicEnum', [Gender.male, Gender.female]) def test_pickling(): assert_eq(Gender.female, pickle.loads(pickle.dumps(Gender.female))) assert_eq(DynamicEnum.male, pickle.loads(pickle.dumps(DynamicEnum.male))) # results of pickling Gender.male in Python 3 with protocol 0 and 2 proto0 = b'c__builtin__\ngetattr\np0\n(ctest_enum\nGender\np1\nVparse\np2\ntp3\nRp4\n(L1L\ntp5\nRp6\n.' proto2 = b'\x80\x02c__builtin__\ngetattr\nq\x00ctest_enum\nGender\nq\x01X\x05\x00\x00\x00parseq\x02\x86q\x03Rq\x04K\x01\x85q\x05Rq\x06.' assert_eq(Gender.male, pickle.loads(proto0)) assert_eq(Gender.male, pickle.loads(proto2))
def test_bad_enum(): def declare_bad_enum(): class BadEnum(Enum): member1 = 1 member2 = 1 assert_raises(declare_bad_enum, AssertionError) class LongEnum(Enum): x = 100 def test_long_enum(): assert_is_instance(LongEnum.x, LongEnum) # mypy doesn't recognize that Gender.male is a Gender instance DynamicEnum = Enum.create("DynamicEnum", [Gender.male, Gender.female]) # type: ignore def test_pickling(): assert_eq(Gender.female, pickle.loads(pickle.dumps(Gender.female))) assert_eq(DynamicEnum.male, pickle.loads(pickle.dumps(DynamicEnum.male))) # results of pickling Gender.male in Python 3 with protocol 0 and 2 proto0 = b"c__builtin__\ngetattr\np0\n(ctest_enum\nGender\np1\nVparse\np2\ntp3\nRp4\n(L1L\ntp5\nRp6\n." proto2 = b"\x80\x02c__builtin__\ngetattr\nq\x00ctest_enum\nGender\nq\x01X\x05\x00\x00\x00parseq\x02\x86q\x03Rq\x04K\x01\x85q\x05Rq\x06." assert_eq(Gender.male, pickle.loads(proto0)) assert_eq(Gender.male, pickle.loads(proto2))
member2 = 1 assert_raises(declare_bad_enum, AssertionError) try: _long_type = long except NameError: _long_type = int class LongEnum(Enum): x = _long_type(100) def test_long_enum(): assert_is_instance(LongEnum.x, LongEnum) DynamicEnum = Enum.create("DynamicEnum", [Gender.male, Gender.female]) def test_pickling(): assert_eq(Gender.female, pickle.loads(pickle.dumps(Gender.female))) assert_eq(DynamicEnum.male, pickle.loads(pickle.dumps(DynamicEnum.male))) # results of pickling Gender.male in Python 3 with protocol 0 and 2 proto0 = b"c__builtin__\ngetattr\np0\n(ctest_enum\nGender\np1\nVparse\np2\ntp3\nRp4\n(L1L\ntp5\nRp6\n." proto2 = b"\x80\x02c__builtin__\ngetattr\nq\x00ctest_enum\nGender\nq\x01X\x05\x00\x00\x00parseq\x02\x86q\x03Rq\x04K\x01\x85q\x05Rq\x06." assert_eq(Gender.male, pickle.loads(proto0)) assert_eq(Gender.male, pickle.loads(proto2))