Verified Commit 4113f44c authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Remove BitField; it is completely replaced with enum.IntFlag

parent 57f24026
Loading
Loading
Loading
Loading
+3 −23
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ if TYPE_CHECKING:
LONG = Struct("!L")

__all__ = [
	"Family", "Stage", "BitField", "ActionFlags", "ProtocolFlags",
	"Family", "Stage", "ActionFlags", "ProtocolFlags",
	"Message", "Negotiate", "Macro", "Connect", "Helo", "EnvelopeFrom",
	"EnvelopeRecipient", "Data", "Unknown", "Header", "EndOfHeaders", "Body",
	"EndOfMessage", "Abort", "Close", "Continue", "Reject", "Discard", "Accept",
@@ -115,27 +115,7 @@ class Stage(int, Enum):
	END_HEADERS = 6


BFSelf = TypeVar("BFSelf", bound="BitField")


class BitField(IntFlag):
	"""
	Base class for bit-field enums like ActionFlags and ProtocolFlags
	"""

	@classmethod
	def pack(cls: type[BFSelf], flags: Iterable[BFSelf]) -> int:
		rflag = 0
		for flag in flags:
			rflag |= flag
		return rflag

	@classmethod
	def unpack(cls: type[BFSelf], bitfield: int) -> set[BFSelf]:
		return {flag for flag in cls if flag & bitfield}


class ActionFlags(BitField):
class ActionFlags(IntFlag):
	"""
	Bit-field values for the `Negotiate.action_flags` field of the `Negotiate` message

@@ -157,7 +137,7 @@ class ActionFlags(BitField):
	SETSYMLIST = 0x100


class ProtocolFlags(BitField):
class ProtocolFlags(IntFlag):
	"""
	Bit-field values for the `Negotiate.protocol_flags` field of the `Negotiate` message

tests/test_bitfields.py

deleted100644 → 0
+0 −36
Original line number Diff line number Diff line
"""
Tests for BitField classes in the kilter.protocol.messages module
"""

from __future__ import annotations

from unittest import TestCase

from kilter.protocol.messages import ActionFlags


class ActionFlagsTests(TestCase):
	"""
	Tests for packing and unpacking ActionFlags
	"""

	def test_pack(self) -> None:
		"""
		Check that packing a set of ActionFlag values into an int works
		"""
		flags = {ActionFlags.ADD_HEADERS, ActionFlags.CHANGE_BODY, ActionFlags.QUARANTINE}

		bitfield = ActionFlags.pack(flags)

		assert bitfield == 0x23

	def test_unpack(self) -> None:
		"""
		Check that unpacking a set of ActionFlags from an int works
		"""
		flags = ActionFlags.unpack(0x23)

		self.assertSetEqual(
			flags,
			{ActionFlags.ADD_HEADERS, ActionFlags.CHANGE_BODY, ActionFlags.QUARANTINE},
		)