def test_tenum_no_enumbase() -> None: class E(enum.Enum): a = 1 b = 2 cls = t.cast(t.Type[cst.EnumBase], E) assert raises(lambda: cst.TEnum(cs.Byte, cls)) == TypeError
def test_tstruct_wrong_enumbase() -> None: class E1(cst.EnumBase): a = 1 b = 2 class E2(cst.EnumBase): a = 1 b = 2 assert raises(cst.TEnum(cs.Byte, E1).build, E2.a) == TypeError
def test_tenum() -> None: class TestEnum(cst.EnumBase): one = 1 two = 2 four = 4 eight = 8 d = cst.TEnum(cs.Byte, TestEnum) common(d, b"\x01", TestEnum.one, 1) common(d, b"\xff", TestEnum(255), 1) assert d.parse(b"\x01") == TestEnum.one assert d.parse(b"\x01") == 1 assert int(d.parse(b"\x01")) == 1 assert d.parse(b"\xff") == TestEnum(255) assert d.parse(b"\xff") == 255 assert int(d.parse(b"\xff")) == 255 assert raises(d.build, 8) == TypeError
def test_tenum_in_tstruct() -> None: class TestEnum(cst.EnumBase): a = 1 b = 2 @dataclasses.dataclass class TestContainer(cst.TContainerBase): a: TestEnum = cst.TStructField(cst.TEnum(cs.Int8ub, TestEnum)) b: int = cst.TStructField(cs.Int8ub) common( cst.TStruct(TestContainer), b"\x01\x02", TestContainer(a=TestEnum.a, b=2), 2, ) assert (raises( cst.TEnum(cs.Byte, TestEnum).build, TestContainer(a=1, b=2)) == TypeError # type: ignore )
class Car(cst.TContainerMixin): brand: CarBrand = cst.sfield(cst.TEnum(cs.Int8ul, CarBrand)) wheels: int = cst.sfield(cs.Int8ul) color: CarColor = cst.sfield(cst.TEnum(cs.Int8ul, CarColor))
class TestContainer(cst.TContainerBase): a: TestEnum = cst.TStructField(cst.TEnum(cs.Int8ub, TestEnum)) b: int = cst.TStructField(cs.Int8ub)
def test_tenum_no_enumbase() -> None: class E(enum.Enum): a = 1 b = 2 assert raises(lambda: cst.TEnum(cs.Byte, E)) == TypeError
class TestContainer(DataclassMixin): a: TestEnum = csfield(cst.TEnum(cs.Int8ub, TestEnum)) b: int = csfield(cs.Int8ub)
class Event(ct.TContainerMixin): type_: EventType = ct.sfield(ct.TEnum(c.Byte, EventType)) time_in_ticks: int = ct.sfield(c.Int24ul) value: int = ct.sfield(c.Int32ul)