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 a Type has 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:
  • type_cls (subclass of Type) – The Type to test.

  • *values_and_data (pair of any and bytes) – The values and data to test.

  • static_size (int or None) –

    The size of type_cls irrespective of any value.

    If None, then type_cls should have no static size.

  • alignment (int or None) –

    The alignment of type_cls.

    If None, then type_cls should have no alignment.

    If an alignment is provided, then a static_size should be as well.

  • default

    The default value of type_cls.

    If NO_DEFAULT, then type_cls should have no default value.

  • ctx (Type.Context or None) – The context for the Type.

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:

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 Type asynchronously.

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:
  • type_cls (subclass of Type) – The Type to test.

  • *values_and_data (pair of any and bytes) – The values and data to test.

  • static_size (int or None) –

    The size of type_cls irrespective of any value.

    If None, then type_cls should have no static size.

  • alignment (int or None) –

    The alignment of type_cls.

    If None, then type_cls should have no alignment.

    If an alignment is provided, then a static_size should be as well.

  • default

    The default value of type_cls.

    If NO_DEFAULT, then type_cls should have no default value.

  • ctx (Type.Context or None) – The context for the Type.

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:

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 Type both synchronously and asynchronously.

When unpacking, both the Type.unpack() and Type.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.

Parameters:
  • type_cls (subclass of Type) – The Type to test.

  • *values_and_data (pair of any and bytes) – The values and data to test.

  • static_size (int or None) –

    The size of type_cls irrespective of any value.

    If None, then type_cls should have no static size.

  • alignment (int or None) –

    The alignment of type_cls.

    If None, then type_cls should have no alignment.

    If an alignment is provided, then a static_size should be as well.

  • default

    The default value of type_cls.

    If NO_DEFAULT, then type_cls should have no default value.

  • ctx (Type.Context or None) – The context for the Type.

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:

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 Packet and bytes) – The Packets and data to test.

  • ctx (Packet.Context) – The context for the Packets.

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 the Packets should be compared using equality. For anything else, you should create your own function, potentially one which uses packet_behavior().

Parameters:

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 Packet and bytes) – The Packets and data to test.

  • ctx (Packet.Context) – The context for the Packets.

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 the Packets should be compared using equality. For anything else, you should create your own function, potentially one which uses packet_behavior_async().

Parameters:

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() and Packet.unpack_async() methods will be used.

Parameters:
  • *packets_and_data (pair of Packet and bytes) – The Packets and data to test.

  • ctx (Packet.Context) – The context for the Packets.

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 the Packets should be compared using equality. For anything else, you should create your own function, potentially one which uses packet_behavior_both().

Parameters:

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())