pak.test¶
Utilities for testing, exposed for users to use as well.
- NO_DEFAULT = NO_DEFAULT¶
An object passed to the
type_behavior()functions to indicate that aTypehas no default value.
- type_behavior(type_cls, *values_and_data, static_size, alignment=None, default, ctx=None)[source]¶
Asserts values marshal to and from expected data using a
Type.When unpacking, the
Type.unpack()method will be used.Whether the reported size from
Type.size()for each value equals the size of the packed data is also asserted.- Parameters:
*values_and_data (pair of any and
bytes) – The values and data to test.static_size (
intorNone) –The size of
type_clsirrespective of any value.If
None, thentype_clsshould have no static size.alignment (
intorNone) –The alignment of
type_cls.If
None, thentype_clsshould have no alignment.If an
alignmentis provided, then astatic_sizeshould be as well.default –
The default value of
type_cls.If
NO_DEFAULT, thentype_clsshould have no default value.ctx (
Type.ContextorNone) – The context for theType.
Examples
>>> import pak >>> pak.test.type_behavior( ... pak.UInt8, ... ... (1, b"\x01"), ... (2, b"\x02"), ... ... static_size = 1, ... alignment = 1, ... default = 0, ... )
- type_behavior_func(*args, **kwargs)[source]¶
Generates a function that calls
type_behavior().This should be used only if you just need to compare values and raw data, and the values should be compared using equality. For anything else, you should create your own function, potentially one which uses
type_behavior().- Parameters:
*args – Forwarded to
type_behavior().**kwargs – Forwarded to
type_behavior().
Examples
>>> import pak >>> test_uint8 = pak.test.type_behavior_func( ... pak.UInt8, ... ... (1, b"\x01"), ... (2, b"\x02"), ... ... static_size = 1, ... alignment = 1, ... default = 0, ... ) >>> test_uint8()
- async type_behavior_async(type_cls, *values_and_data, static_size, alignment=None, default, ctx=None)[source]¶
Asserts values marshal to and from expected data using a
Typeasynchronously.When unpacking the raw data, the
Type.unpack_async()method will be used.Whether the reported size from
Type.size()for each value equals the size of the packed data is also asserted.- Parameters:
*values_and_data (pair of any and
bytes) – The values and data to test.static_size (
intorNone) –The size of
type_clsirrespective of any value.If
None, thentype_clsshould have no static size.alignment (
intorNone) –The alignment of
type_cls.If
None, thentype_clsshould have no alignment.If an
alignmentis provided, then astatic_sizeshould be as well.default –
The default value of
type_cls.If
NO_DEFAULT, thentype_clsshould have no default value.ctx (
Type.ContextorNone) – The context for theType.
Examples
>>> import pak >>> import asyncio >>> asyncio.run(pak.test.type_behavior_async( ... pak.UInt8, ... ... (1, b"\x01"), ... (2, b"\x02"), ... ... static_size = 1, ... alignment = 1, ... default = 0, ... ))
- type_behavior_func_async(*args, **kwargs)[source]¶
Generates a coroutine function that calls
type_behavior_async().This should be used only if you just need to compare values and raw data, and the values should be compared using equality. For anything else, you should create your own function, potentially one which uses
type_behavior_async().- Parameters:
*args – Forwarded to
type_behavior_async().**kwargs – Forwarded to
type_behavior_async().
Examples
>>> import pak >>> import asyncio >>> test_uint8 = pak.test.type_behavior_func_async( ... pak.UInt8, ... ... (1, b"\x01"), ... (2, b"\x02"), ... ... static_size = 1, ... alignment = 1, ... default = 0, ... ) >>> asyncio.run(test_uint8())
- async type_behavior_both(type_cls, *values_and_data, static_size, alignment=None, default, ctx=None)[source]¶
Asserts values marshal to and from expected data using a
Typeboth synchronously and asynchronously.When unpacking, both the
Type.unpack()andType.unpack_async()methods will be used.Whether the reported size from
Type.size()for each value equals the size of the packed data is also asserted.See also
- Parameters:
*values_and_data (pair of any and
bytes) – The values and data to test.static_size (
intorNone) –The size of
type_clsirrespective of any value.If
None, thentype_clsshould have no static size.alignment (
intorNone) –The alignment of
type_cls.If
None, thentype_clsshould have no alignment.If an
alignmentis provided, then astatic_sizeshould be as well.default –
The default value of
type_cls.If
NO_DEFAULT, thentype_clsshould have no default value.ctx (
Type.ContextorNone) – The context for theType.
Examples
>>> import pak >>> import asyncio >>> asyncio.run(pak.test.type_behavior_both( ... pak.UInt8, ... ... (1, b"\x01"), ... (2, b"\x02"), ... ... static_size = 1, ... alignment = 1, ... default = 0, ... ))
- type_behavior_func_both(*args, **kwargs)[source]¶
Generates a coroutine function that calls
type_behavior_both().This should be used only if you just need to compare values and raw data, and the values should be compared using equality. For anything else, you should create your own function, potentially one which uses
type_behavior_both().- Parameters:
*args – Forwarded to
type_behavior_both().**kwargs – Forwarded to
type_behavior_both().
Examples
>>> import pak >>> import asyncio >>> test_uint8 = pak.test.type_behavior_func_both( ... pak.UInt8, ... ... (1, b"\x01"), ... (2, b"\x02"), ... ... static_size = 1, ... alignment = 1, ... default = 0, ... ) >>> asyncio.run(test_uint8())
- packet_behavior(*packets_and_data, ctx=None)[source]¶
Asserts
Packets marshal to and from expected data.When unpacking the raw data, the
Packet.unpack()method will be used.- Parameters:
*packets_and_data (pair of
Packetandbytes) – ThePackets and data to test.ctx (
Packet.Context) – The context for thePackets.
Examples
>>> import pak >>> class MyPacket(pak.Packet): ... id = 1 ... field: pak.UInt8 ... class Header(pak.Packet.Header): ... id: pak.UInt8 ... >>> pak.test.packet_behavior( ... (MyPacket(field=2), b"\x01\x02"), ... (MyPacket(field=3), b"\x01\x03"), ... )
- packet_behavior_func(*args, **kwargs)[source]¶
Generates a function that calls
packet_behavior().This should be used only if you just need to compare
Packets and raw data, and thePackets should be compared using equality. For anything else, you should create your own function, potentially one which usespacket_behavior().- Parameters:
*args – Forwarded to
packet_behavior().**kwargs – Forwarded to
packet_behavior().
Examples
>>> import pak >>> class MyPacket(pak.Packet): ... id = 1 ... field: pak.UInt8 ... class Header(pak.Packet.Header): ... id: pak.UInt8 ... >>> test_my_packet = pak.test.packet_behavior_func( ... (MyPacket(field=2), b"\x01\x02"), ... (MyPacket(field=3), b"\x01\x03"), ... ) >>> test_my_packet()
- async packet_behavior_async(*packets_and_data, ctx=None)[source]¶
Asserts
Packets marshal to and from expected data asynchronously.When unpacking the raw data, the
Packet.unpack_async()method will be used.- Parameters:
*packets_and_data (pair of
Packetandbytes) – ThePackets and data to test.ctx (
Packet.Context) – The context for thePackets.
Examples
>>> import pak >>> import asyncio >>> class MyPacket(pak.Packet): ... id = 1 ... field: pak.UInt8 ... class Header(pak.Packet.Header): ... id: pak.UInt8 ... >>> asyncio.run(pak.test.packet_behavior_async( ... (MyPacket(field=2), b"\x01\x02"), ... (MyPacket(field=3), b"\x01\x03"), ... ))
- packet_behavior_func_async(*args, **kwargs)[source]¶
Generates a coroutine function that calls
packet_behavior_async().This should be used only if you just need to compare
Packets and raw data, and thePackets should be compared using equality. For anything else, you should create your own function, potentially one which usespacket_behavior_async().- Parameters:
*args – Forwarded to
packet_behavior_async().**kwargs – Forwarded to
packet_behavior_async().
Examples
>>> import pak >>> import asyncio >>> class MyPacket(pak.Packet): ... id = 1 ... field: pak.UInt8 ... class Header(pak.Packet.Header): ... id: pak.UInt8 ... >>> test_my_packet = pak.test.packet_behavior_func_async( ... (MyPacket(field=2), b"\x01\x02"), ... (MyPacket(field=3), b"\x01\x03"), ... ) >>> asyncio.run(test_my_packet())
- async packet_behavior_both(*packets_and_data, ctx=None)[source]¶
Asserts
Packets marshal to and from expected data asynchronously.When unpacking, both the
Packet.unpack()andPacket.unpack_async()methods will be used.- Parameters:
*packets_and_data (pair of
Packetandbytes) – ThePackets and data to test.ctx (
Packet.Context) – The context for thePackets.
Examples
>>> import pak >>> import asyncio >>> class MyPacket(pak.Packet): ... id = 1 ... field: pak.UInt8 ... class Header(pak.Packet.Header): ... id: pak.UInt8 ... >>> asyncio.run(pak.test.packet_behavior_both( ... (MyPacket(field=2), b"\x01\x02"), ... (MyPacket(field=3), b"\x01\x03"), ... ))
- packet_behavior_func_both(*args, **kwargs)[source]¶
Generates a function that calls
packet_behavior_both().This should be used only if you just need to compare
Packets and raw data, and thePackets should be compared using equality. For anything else, you should create your own function, potentially one which usespacket_behavior_both().- Parameters:
*args – Forwarded to
packet_behavior_both().**kwargs – Forwarded to
packet_behavior_both().
Examples
>>> import pak >>> import asyncio >>> class MyPacket(pak.Packet): ... id = 1 ... field: pak.UInt8 ... class Header(pak.Packet.Header): ... id: pak.UInt8 ... >>> test_my_packet = pak.test.packet_behavior_func_both( ... (MyPacket(field=2), b"\x01\x02"), ... (MyPacket(field=3), b"\x01\x03"), ... ) >>> asyncio.run(test_my_packet())