Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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):
return False
return bool(self.prog.match(other))
def __repr__(self):
return "".format(
return super().__eq__(other) and all(
other[attr] == self.subset[attr] for attr in self.subset.keys()
)
except KeyError:
return False
# generic
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)
return "".format(
id(self),
repr(self.pattern),
f" flags={self.flags}" if self.flags != 0 else "",
)
class StrContaining(Matcher):
def __init__(self, needle):
self.needle = needle
def __eq__(self, other):
return isinstance(other, str) and self.needle in other
class StrStartingWith(Matcher):
def __init__(self, needle):
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
self.prog = re.compile(pattern, flags)
def __eq__(self, other):
if not isinstance(other, str):
return False
return bool(self.prog.match(other))
def __repr__(self):
return "".format(
id(self),
repr(self.pattern),
f" flags={self.flags}" if self.flags != 0 else "",
)
class StrContaining(Matcher):
def __init__(self, needle):
self.needle = needle
def __eq__(self, other):
return isinstance(other, str) and self.needle in other
class StrStartingWith(Matcher):
def __init__(self, needle):
self.needle = needle
def __eq__(self, other):
return isinstance(other, str) and other.startswith(self.needle)
class StrEndingWith(Matcher):
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 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):
# generic
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)
def __init__(self, needle):
self.needle = needle
def __eq__(self, other):
return isinstance(other, str) and self.needle in other
class StrStartingWith(Matcher):
def __init__(self, needle):
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
return f"! {self.matcher}"
class _OrMatcher(_AlreadyChainedMatcher):
def __init__(self, a, b):
self.a = a
self.b = b
def __eq__(self, other):
return self.a == other or self.b == other
def __repr__(self):
return f"{self.a} | {self.b}"
class _RichComparison(Matcher):
def __init__(self, klass, lt=None, le=None, eq=None, ne=None, ge=None, gt=None):
self.klass = klass
self.lt = lt
self.le = le
self.eq = eq
self.ne = ne
self.ge = ge
self.gt = gt
def __eq__(self, other):
if not isinstance(other, self.klass):
return False
if self.lt is not None and not (other < self.lt):
return False
if self.le is not None and not (other <= self.le):
return False