class BadEnum(MatrixEnum): ONE = Member(code=1, description='one', plain_text='plaintext').extra(foo=1) TWO = Member(code=2, description='two', plain_text='something else plain').extra(foo=2, bar=2)
class BadEnum(MatrixEnum): ONE = Member(value=1, description='one', plain_text="plaintext") TWO = Member(value=2, description='one', plain_text="something else plain")
def test_simple_enum(self): class WorkingEnum(MatrixEnum): ONE = Member(code=1, description='one', plain_text='plaintext') TWO = Member(code=2, description='two', plain_text='something else plain') assert isinstance(WorkingEnum.ONE, WorkingEnum) assert WorkingEnum.ONE is WorkingEnum['ONE'] assert WorkingEnum(WorkingEnum.ONE) is WorkingEnum.ONE assert WorkingEnum('ONE') is WorkingEnum.ONE assert WorkingEnum(1) is WorkingEnum.ONE assert WorkingEnum(1) == WorkingEnum.ONE assert WorkingEnum('one') is WorkingEnum('plaintext') assert WorkingEnum.ONE.value != Member(code=1) assert WorkingEnum.ONE.description == 'one' assert WorkingEnum.ONE.plain_text == 'plaintext' assert WorkingEnum.ONE.code == 1 assert WorkingEnum.ONE.code == WorkingEnum.ONE.value.code assert WorkingEnum.ONE != WorkingEnum.TWO assert WorkingEnum.ONE.value != WorkingEnum.TWO.value assert WorkingEnum(1) != WorkingEnum(2) assert WorkingEnum('one') != WorkingEnum('two') assert WorkingEnum['ONE'] != WorkingEnum['TWO'] identical_member = Member(code=1, description='one', plain_text="plaintext") assert WorkingEnum.ONE.value == identical_member assert WorkingEnum.ONE.value is not identical_member for item in WorkingEnum: assert item in WorkingEnum
class MyEnum(MatrixEnum): ONE = Member(code=1, description='one').extra(foo='bar') TWO = Member(code=2, description='two').extra(foo='baz') @classmethod def test_func(cls): return "Test"
class WorkingEnum(MatrixEnum): ONE = Member(code=1, description='one', plain_text='plaintext').extra(foo='1') TWO = Member(code=2, description='two', plain_text='something else plain').extra(foo='2') THREE = Member(code=3, description='three', plain_text='even more plain').extra(foo='1')
class WorkingEnum(MatrixEnum): ONE = Member(code=1) TWO = Member(code=2) def code_label(self): return "My code is {}".format(self.code) @classmethod def total_codes(cls): return sum(e.code for e in cls)
class Enum(MatrixEnum): _order_ = 'TWO ONE' TWO = Member(code=2) ONE = Member(code=1)
class BadEnum(MatrixEnum): ONE = Member(code=1).extra(foo=1).extra(bar=3) TWO = Member(code=2).extra(foo=2).extra(bar=3)
class BadEnum(MatrixEnum): ONE = Member(foo=1) TWO = Member(bar=2)
class MyEnum(MatrixEnum): ONE = Member(code=1) NONE = Member(code=None)
class BadEnum(MatrixEnum): ONE = Member(code=1) code = Member(code=2)
class BadEnum(MatrixEnum): ONE = Member(code=1, description='foo', plain_text="plaintext") TWO = Member(code=2, description='ONE', plain_text="something else plain")
class WorkingEnum(MatrixEnum): ONE = Member(code=1, description='one', plain_text='plaintext') TWO = Member(code=2, description='two', plain_text='something else plain')
class BadEnum(MatrixEnum): ONE = Member(code=1, description='one') TWO = Member(code=2, description='two') THREE = (3, "three")
class MisorderedEnum(MatrixEnum): _order_ = 'TWO ONE' ONE = Member(code=1) TWO = Member(code=2)
class IgnoredEnum(MatrixEnum): _ignore_ = 'IGNORE_ME' ONE = Member(code=1) TWO = Member(code=2) IGNORE_ME = Member(code=3)
class BadEnum1(MatrixEnum): _reversed = Member(code=1) _other = Member(code=2)
def test_duplicated_attr_values(self): with pytest.raises( ValueError, match=r"Value '1' duplicated within Member specification."): Member(code=1, description='one', plain_text=1)
def test_invalid_attrs(self): with pytest.raises(ValueError, match=r"Member attribute 'extra' is not allowed."): Member( extra=6) # 'extra' is a function on Member and cannot be used
class MyEnum(MatrixEnum): ONE = Member(code=1) TWO = Member(code=2)
def test_member_hashing(self): assert hash(Member(foo=1)) != hash(Member(bar=1))