bitmask.py: increase test coverage, remove redundant code

This commit is contained in:
dogeystamp 2022-07-31 21:38:24 -04:00
parent 0c333eee95
commit eab920abeb
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38
2 changed files with 12 additions and 36 deletions

View File

@ -158,13 +158,6 @@ class Bitmask:
"""Alias the + operator in reverse.""" """Alias the + operator in reverse."""
return self.__add__(other) return self.__add__(other)
def __iadd__(self, other):
"""Implement += operator.
Aliased to `Bitmask.__add__`.
"""
return self + other
def __or__(self, other): def __or__(self, other):
"""Implement | operator.""" """Implement | operator."""
return self + other return self + other
@ -173,13 +166,6 @@ class Bitmask:
"""Alias the | operator in reverse.""" """Alias the | operator in reverse."""
return self.__or__(other) return self.__or__(other)
def __ior__(self, other):
"""Implement |= operator.
Aliased to `Bitmask.__add__`.
"""
return self | other
def __xor__(self, other): def __xor__(self, other):
"""Implement ^ operator.""" """Implement ^ operator."""
return self.__mask_op(other, lambda a, b : a ^ b) return self.__mask_op(other, lambda a, b : a ^ b)
@ -188,13 +174,6 @@ class Bitmask:
"""Alias the ^ operator in reverse.""" """Alias the ^ operator in reverse."""
return self.__xor__(other) return self.__xor__(other)
def __ixor(self, other):
"""Implement ^= operator.
Aliased to `Bitmask.__xor__`.
"""
return self ^ other
def __and__(self, other): def __and__(self, other):
"""AND bitmasks/flags together.""" """AND bitmasks/flags together."""
return self.__mask_op(other, lambda a, b : a & b) return self.__mask_op(other, lambda a, b : a & b)
@ -203,25 +182,10 @@ class Bitmask:
"""Alias the & operator in reverse.""" """Alias the & operator in reverse."""
return self.__and__(other) return self.__and__(other)
def __iand(self, other):
"""AND bitmasks/flags together.
Aliased to `Bitmask.__and__`.
"""
return self & other
def __sub__(self, other): def __sub__(self, other):
"""Subtract by bitmask/flag.""" """Subtract by bitmask/flag."""
return self.__mask_op(other, lambda a, b : a & ~b) return self.__mask_op(other, lambda a, b : a & ~b)
def __isub__(self, other):
"""Subtract a bitmask/flag.
Aliased to `Bitmask.__sub__`.
"""
self = self - other
return self
def discard(self, flag): def discard(self, flag):
"""Remove flag bitmask if present. """Remove flag bitmask if present.

View File

@ -11,21 +11,31 @@ class Desc(IntFlag):
FUNKY = 1 << 2 FUNKY = 1 << 2
SONAR = 1 << 4 SONAR = 1 << 4
class Colors(IntFlag):
TEAL = 1
PINK = 1 << 1
BLUE = 1 << 2
def test_eq(): def test_eq():
"""Test equality checks.""" """Test equality checks."""
# Equality
assert Bitmask(Desc) == Bitmask(Desc) assert Bitmask(Desc) == Bitmask(Desc)
assert Bitmask(Desc, Desc.SMALL) == Bitmask(Desc, Desc.SMALL) assert Bitmask(Desc, Desc.SMALL) == Bitmask(Desc, Desc.SMALL)
assert Bitmask(Desc, Desc.SMALL, Desc.ROUND) == Bitmask(Desc, Desc.SMALL, Desc.ROUND) assert Bitmask(Desc, Desc.SMALL, Desc.ROUND) == Bitmask(Desc, Desc.SMALL, Desc.ROUND)
assert Bitmask(Desc, Desc.ROUND, Desc.SMALL) == Bitmask(Desc, Desc.SMALL, Desc.ROUND) assert Bitmask(Desc, Desc.ROUND, Desc.SMALL) == Bitmask(Desc, Desc.SMALL, Desc.ROUND)
# Inequality
assert Bitmask(Desc, Desc.SMALL) != Bitmask(Desc, Desc.SMALL, Desc.ROUND) assert Bitmask(Desc, Desc.SMALL) != Bitmask(Desc, Desc.SMALL, Desc.ROUND)
assert Bitmask(Desc, Desc.SMALL) != Bitmask(Desc, Desc.ROUND) assert Bitmask(Desc, Desc.SMALL) != Bitmask(Desc, Desc.ROUND)
assert Bitmask(Desc, Desc.SMALL) != Bitmask(Desc) assert Bitmask(Desc, Desc.SMALL) != Bitmask(Desc)
assert Bitmask(Desc, Desc.SMALL) != Desc.SMALL assert Bitmask(Desc, Desc.SMALL) != Desc.SMALL
assert Bitmask(Desc, Desc.SMALL) != Desc.ROUND assert Bitmask(Desc, Desc.SMALL) != Desc.ROUND
assert Bitmask(Desc) != Desc.ROUND assert Bitmask(Desc) != Desc.ROUND
# Wrong types
assert Bitmask(Desc) != "Hello World!" assert Bitmask(Desc) != "Hello World!"
assert Bitmask(Desc) != 0 assert Bitmask(Desc) != 0
assert Bitmask(Desc) != Bitmask(Colors)
def test_repr(): def test_repr():
"""Ensure evaluating __repr__ creates an identical object.""" """Ensure evaluating __repr__ creates an identical object."""
@ -49,6 +59,8 @@ def test_add():
with pytest.raises(TypeError, match="can only apply Desc to Bitmask"): with pytest.raises(TypeError, match="can only apply Desc to Bitmask"):
mask.add(1) mask.add(1)
with pytest.raises(TypeError, match="can only apply Bitmask or Desc to Bitmask"):
mask += 1
def test_add_operator(): def test_add_operator():
"""Test + operator.""" """Test + operator."""