Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, needle):
self.needle = needle
super().__init__(klass=List)
def __eq__(self, other):
return super().__eq__(other) and self.needle in other
def __repr__(self):
return "<{} 0x{:02X}{}>".format(
type(self).__name__,
id(self),
f" subset={self.subset}" if self.subset is not None else "",
)
class ListContainingAll(_RichComparison):
def __init__(self, subset: List):
self.subset = subset
super().__init__(klass=List)
def __eq__(self, other):
return super().__eq__(other) and all(x in other for x in self.subset)
def __repr__(self):
return "<{} 0x{:02X}{}>".format(
type(self).__name__,
id(self),
f" subset={self.subset}" if self.subset is not None else "",
)
class NotEmptyList(AnyList):
class Any(Matcher):
def __eq__(self, other):
return other is not None
class AnyTruthy(Matcher):
def __eq__(self, other):
return bool(other)
class AnyFalsey(Matcher):
def __eq__(self, other):
return not bool(other)
class AnyInstanceOf(_RichComparison):
def __init__(self, klass):
super().__init__(klass=klass)
f" ne={self.ne}" if self.ne is not None else "",
f" ge={self.ge}" if self.ge is not None else "",
f" gt={self.gt}" if self.gt is not None else "",
)
class _FloatComparison(_RichComparison):
"""
Compares true if other number passes all rich comparison cases given.
"""
def __init__(self, lt=None, le=None, eq=None, ne=None, ge=None, gt=None):
super().__init__(float, lt=lt, le=le, eq=eq, ne=ne, ge=ge, gt=gt)
class _IntComparison(_RichComparison):
"""
Compares true if other number passes all rich comparison cases given.
"""
def __init__(self, lt=None, le=None, eq=None, ne=None, ge=None, gt=None):
super().__init__(int, lt=lt, le=le, eq=eq, ne=ne, ge=ge, gt=gt)
# Ints
class AnyInt(_IntComparison):
def __init__(self):
super().__init__()
class NotThisInt(_IntComparison):
def __init__(self, ne: int):
class StrEndingWith(Matcher):
def __init__(self, needle):
self.needle = needle
def __eq__(self, other):
return isinstance(other, str) and other.endswith(self.needle)
# lists
class AnyList(_RichComparison):
def __init__(self):
super().__init__(klass=List)
class ListContaining(_RichComparison):
def __init__(self, needle):
self.needle = needle
super().__init__(klass=List)
def __eq__(self, other):
return super().__eq__(other) and self.needle in other
def __repr__(self):
return "<{} 0x{:02X}{}>".format(
type(self).__name__,
id(self),
f" subset={self.subset}" if self.subset is not None else "",
)
class ListContainingAll(_RichComparison):
return True
def __repr__(self):
return "<{} 0x{:02X}{}{}{}{}{}{}>".format(
type(self).__name__,
id(self),
f" lt={self.lt}" if self.lt is not None else "",
f" le={self.le}" if self.le is not None else "",
f" eq={self.eq}" if self.eq is not None else "",
f" ne={self.ne}" if self.ne is not None else "",
f" ge={self.ge}" if self.ge is not None else "",
f" gt={self.gt}" if self.gt is not None else "",
)
class _FloatComparison(_RichComparison):
"""
Compares true if other number passes all rich comparison cases given.
"""
def __init__(self, lt=None, le=None, eq=None, ne=None, ge=None, gt=None):
super().__init__(float, lt=lt, le=le, eq=eq, ne=ne, ge=ge, gt=gt)
class _IntComparison(_RichComparison):
"""
Compares true if other number passes all rich comparison cases given.
"""
def __init__(self, lt=None, le=None, eq=None, ne=None, ge=None, gt=None):
super().__init__(int, lt=lt, le=le, eq=eq, ne=ne, ge=ge, gt=gt)
f" subset={self.subset}" if self.subset is not None else "",
)
class NotEmptyList(AnyList):
def __eq__(self, other):
return super().__eq__(other) and len(other)
class EmptyList(AnyList):
def __eq__(self, other):
return super().__eq__(other) and not len(other)
# dicts
class AnyDict(_RichComparison):
def __init__(self):
super().__init__(klass=Dict)
class NotEmptyDict(AnyDict):
def __eq__(self, other):
return super().__eq__(other) and bool(other)
class EmptyDict(AnyDict):
def __eq__(self, other):
return super().__eq__(other) and not bool(other)
class DictContainingKeys(_RichComparison):
def __init__(self, expected_keys: List):
class AnyDict(_RichComparison):
def __init__(self):
super().__init__(klass=Dict)
class NotEmptyDict(AnyDict):
def __eq__(self, other):
return super().__eq__(other) and bool(other)
class EmptyDict(AnyDict):
def __eq__(self, other):
return super().__eq__(other) and not bool(other)
class DictContainingKeys(_RichComparison):
def __init__(self, expected_keys: List):
self.expected_keys = expected_keys
super().__init__(klass=Dict)
def __eq__(self, other):
try:
return super().__eq__(other) and all(
attr in other for attr in self.expected_keys
)
except KeyError:
return False
class DictSupersetOf(_RichComparison):
def __init__(self, subset: Dict):
self.subset = subset
self.needle = needle
def __eq__(self, other):
return isinstance(other, str) and other.startswith(self.needle)
class StrEndingWith(Matcher):
def __init__(self, needle):
self.needle = needle
def __eq__(self, other):
return isinstance(other, str) and other.endswith(self.needle)
# lists
class AnyList(_RichComparison):
def __init__(self):
super().__init__(klass=List)
class ListContaining(_RichComparison):
def __init__(self, needle):
self.needle = needle
super().__init__(klass=List)
def __eq__(self, other):
return super().__eq__(other) and self.needle in other
def __repr__(self):
return "<{} 0x{:02X}{}>".format(
type(self).__name__,
id(self),
class DictContainingKeys(_RichComparison):
def __init__(self, expected_keys: List):
self.expected_keys = expected_keys
super().__init__(klass=Dict)
def __eq__(self, other):
try:
return super().__eq__(other) and all(
attr in other for attr in self.expected_keys
)
except KeyError:
return False
class DictSupersetOf(_RichComparison):
def __init__(self, subset: Dict):
self.subset = subset
super().__init__(klass=Dict)
def __eq__(self, other):
try:
return super().__eq__(other) and all(
other[attr] == self.subset[attr] for attr in self.subset.keys()
)
except KeyError:
return False
# generic
class FloatLessThan(_FloatComparison):
def __init__(self, lt):
super().__init__(lt=lt)
class FloatLessOrEquals(_FloatComparison):
def __init__(self, le):
super().__init__(le=le)
# strings
class AnyStr(_RichComparison):
def __init__(self):
super().__init__(klass=str)
class RegexMatches(Matcher):
"""
Compares true if other mathes given regex.
"""
def __init__(self, pattern, flags=0):
self.pattern = pattern
self.flags = flags
self.prog = re.compile(pattern, flags)
def __eq__(self, other):
if not isinstance(other, str):