def test_create_new() -> None: data = Bytes(5, b'\x11\x22\x33\x44\x55') bytes_t = typeof(data) assert bytes_t.length() == 5 assert sizeof(bytes_t) == 5 assert data.length() == 5 assert sizeof(data) == 5 assert len(data) == 5
def test_typechecks_type_another() -> None: bytes4_t = typeof(Bytes(4)) bytes8_t = typeof(Bytes(8)) bytes4_v = Bytes(4, b'1234') assert not isinstance(bytes4_t, bytes8_t) assert not issubclass(bytes4_t, bytes8_t) assert not isinstance(bytes4_v, bytes8_t) with raises(TypeError): issubclass(bytes4_v, bytes8_t) # type: ignore
def test_eq() -> None: bytes16 = typeof(Bytes(16)) bytes8 = typeof(Bytes(8)) bytes16_clone = clone_type(bytes16, name='bytes16') assert bytes16 == bytes16 # pylint: disable=comparison-with-itself assert bytes16 != bytes8 assert bytes16 == bytes16_clone assert bytes16 != UInt16 assert bytes16 != Bytes assert bytes16 != bytes
def test_typechecks_type() -> None: bytes_t = typeof(Bytes(4)) bytes_v = Bytes(4, b'1234') assert not isinstance(Bytes, bytes_t) assert not issubclass(Bytes, bytes_t) assert not isinstance(bytes_t, bytes_t) assert issubclass(bytes_t, bytes_t) assert isinstance(bytes_v, bytes_t) with raises(TypeError): issubclass(bytes_v, bytes_t) # type: ignore assert not isinstance(b'string', bytes_t) with raises(TypeError): issubclass(b'string', bytes_t) # type: ignore
def test_member_change_another() -> None: class Container(Struct): field_b: Bytes[Literal[5]] field_u: UInt32 inst = Container() another = Bytes(5, b'\x01\x23\x45\x67\x89') data = inst.field_b inst.field_b = another assert bytes(data) == b'\x01\x23\x45\x67\x89'
def test_init_bad_3() -> None: with raises(ValueError): _ = Bytes(5, b'too_many_bytes')
def test_init_bad_2() -> None: with raises(TypeError): _ = Bytes(5, 'Bad value') # type: ignore
def test_init_another_2() -> None: data1 = Bytes(5, b'\x11\x22\x33\x44\x55') data2 = Bytes(5, data1) assert bytes(data2) == b'\x11\x22\x33\x44\x55'
def test_init_another_1() -> None: bytes_t = Bytes[Literal[5]] data1 = Bytes(5, b'\x11\x22\x33\x44\x55') data2 = bytes_t(data1) assert bytes(data2) == b'\x11\x22\x33\x44\x55'
def test_init_short_2() -> None: data = Bytes(5, b'\x11\x22\x33') assert bytes(data) == b'\x11\x22\x33\x00\x00'
def test_setitem() -> None: data = Bytes(6, b'abcdef') data[2] = b'X' assert bytes(data) == b'abXdef'
class Container(Struct): field = Bytes(5)
def test_setitem_bad_len() -> None: data = Bytes(6, b'abcdef') with raises(ValueError): data[2] = b'XY'
def test_setitem_bad_index() -> None: data = Bytes(6, b'abcdef') with raises(IndexError): data[6] = b'X'
def test_init_bad_4() -> None: with raises(TypeError): _ = Bytes('5', b'\x11\x22\x33\x44\x55') # type: ignore
def test_name_2() -> None: data = Bytes(5, b'abcde') bytes_t = typeof(data) assert type_name(bytes_t) == 'char[5]' assert type_name(data) == 'char[5]'
def test_getitem() -> None: data = Bytes(6, b'abcdef') assert data[2] == b'c'