Ausgabe der neuen DB Einträge
This commit is contained in:
parent
bad48e1627
commit
cfbbb9ee3d
2399 changed files with 843193 additions and 43 deletions
|
|
@ -0,0 +1,15 @@
|
|||
"""Matchers of collections."""
|
||||
from .is_empty import empty
|
||||
from .isdict_containing import has_entry
|
||||
from .isdict_containingentries import has_entries
|
||||
from .isdict_containingkey import has_key
|
||||
from .isdict_containingvalue import has_value
|
||||
from .isin import is_in
|
||||
from .issequence_containing import has_item, has_items
|
||||
from .issequence_containinginanyorder import contains_inanyorder
|
||||
from .issequence_containinginorder import contains, contains_exactly
|
||||
from .issequence_onlycontaining import only_contains
|
||||
|
||||
__author__ = "Chris Rose"
|
||||
__copyright__ = "Copyright 2013 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
from typing import Optional, Sized
|
||||
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.description import Description
|
||||
from hamcrest.core.matcher import Matcher
|
||||
|
||||
__author__ = "Chris Rose"
|
||||
__copyright__ = "Copyright 2012 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
|
||||
class IsEmpty(BaseMatcher[Sized]):
|
||||
def matches(self, item: Sized, mismatch_description: Optional[Description] = None) -> bool:
|
||||
try:
|
||||
if len(item) == 0:
|
||||
return True
|
||||
|
||||
if mismatch_description:
|
||||
mismatch_description.append_text("has %d item(s)" % len(item))
|
||||
|
||||
except TypeError:
|
||||
if mismatch_description:
|
||||
mismatch_description.append_text("does not support length")
|
||||
|
||||
return False
|
||||
|
||||
def describe_to(self, description: Description) -> None:
|
||||
description.append_text("an empty collection")
|
||||
|
||||
|
||||
def empty() -> Matcher[Sized]:
|
||||
"""
|
||||
This matcher matches any collection-like object that responds to the
|
||||
__len__ method, and has a length of 0.
|
||||
"""
|
||||
return IsEmpty()
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
from typing import Hashable, Mapping, TypeVar, Union
|
||||
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.description import Description
|
||||
from hamcrest.core.helpers.hasmethod import hasmethod
|
||||
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
|
||||
from hamcrest.core.matcher import Matcher
|
||||
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
|
||||
K = TypeVar("K", bound=Hashable) # TODO - covariant?
|
||||
V = TypeVar("V")
|
||||
|
||||
|
||||
class IsDictContaining(BaseMatcher[Mapping[K, V]]):
|
||||
def __init__(self, key_matcher: Matcher[K], value_matcher: Matcher[V]) -> None:
|
||||
self.key_matcher = key_matcher
|
||||
self.value_matcher = value_matcher
|
||||
|
||||
def _matches(self, item: Mapping[K, V]) -> bool:
|
||||
if hasmethod(item, "items"):
|
||||
for key, value in item.items():
|
||||
if self.key_matcher.matches(key) and self.value_matcher.matches(value):
|
||||
return True
|
||||
return False
|
||||
|
||||
def describe_to(self, description: Description) -> None:
|
||||
description.append_text("a dictionary containing [").append_description_of(
|
||||
self.key_matcher
|
||||
).append_text(": ").append_description_of(self.value_matcher).append_text("]")
|
||||
|
||||
|
||||
def has_entry(
|
||||
key_match: Union[K, Matcher[K]], value_match: Union[V, Matcher[V]]
|
||||
) -> Matcher[Mapping[K, V]]:
|
||||
"""Matches if dictionary contains key-value entry satisfying a given pair
|
||||
of matchers.
|
||||
|
||||
:param key_match: The matcher to satisfy for the key, or an expected value
|
||||
for :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
|
||||
:param value_match: The matcher to satisfy for the value, or an expected
|
||||
value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
|
||||
|
||||
This matcher iterates the evaluated dictionary, searching for any key-value
|
||||
entry that satisfies ``key_match`` and ``value_match``. If a matching entry
|
||||
is found, ``has_entry`` is satisfied.
|
||||
|
||||
Any argument that is not a matcher is implicitly wrapped in an
|
||||
:py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
|
||||
equality.
|
||||
|
||||
Examples::
|
||||
|
||||
has_entry(equal_to('foo'), equal_to(1))
|
||||
has_entry('foo', 1)
|
||||
|
||||
"""
|
||||
return IsDictContaining(wrap_matcher(key_match), wrap_matcher(value_match))
|
||||
|
|
@ -0,0 +1,160 @@
|
|||
from typing import Any, Hashable, Mapping, Optional, TypeVar, Union, overload
|
||||
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.description import Description
|
||||
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
|
||||
from hamcrest.core.matcher import Matcher
|
||||
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
K = TypeVar("K", bound=Hashable)
|
||||
V = TypeVar("V")
|
||||
|
||||
|
||||
class IsDictContainingEntries(BaseMatcher[Mapping[K, V]]):
|
||||
def __init__(self, value_matchers) -> None:
|
||||
self.value_matchers = sorted(value_matchers.items())
|
||||
|
||||
def _not_a_dictionary(
|
||||
self, item: Mapping[K, V], mismatch_description: Optional[Description]
|
||||
) -> bool:
|
||||
if mismatch_description:
|
||||
mismatch_description.append_description_of(item).append_text(" is not a mapping object")
|
||||
return False
|
||||
|
||||
def matches(
|
||||
self, item: Mapping[K, V], mismatch_description: Optional[Description] = None
|
||||
) -> bool:
|
||||
for key, value_matcher in self.value_matchers:
|
||||
|
||||
try:
|
||||
if not key in item:
|
||||
if mismatch_description:
|
||||
mismatch_description.append_text("no ").append_description_of(
|
||||
key
|
||||
).append_text(" key in ").append_description_of(item)
|
||||
return False
|
||||
except TypeError:
|
||||
return self._not_a_dictionary(item, mismatch_description)
|
||||
|
||||
try:
|
||||
actual_value = item[key]
|
||||
except TypeError:
|
||||
return self._not_a_dictionary(item, mismatch_description)
|
||||
|
||||
if not value_matcher.matches(actual_value):
|
||||
if mismatch_description:
|
||||
mismatch_description.append_text("value for ").append_description_of(
|
||||
key
|
||||
).append_text(" ")
|
||||
value_matcher.describe_mismatch(actual_value, mismatch_description)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def describe_mismatch(self, item: Mapping[K, V], mismatch_description: Description) -> None:
|
||||
self.matches(item, mismatch_description)
|
||||
|
||||
def describe_keyvalue(self, index: int, value: V, description: Description) -> None:
|
||||
"""Describes key-value pair at given index."""
|
||||
description.append_description_of(index).append_text(": ").append_description_of(value)
|
||||
|
||||
def describe_to(self, description: Description) -> None:
|
||||
description.append_text("a dictionary containing {")
|
||||
first = True
|
||||
for key, value in self.value_matchers:
|
||||
if not first:
|
||||
description.append_text(", ")
|
||||
self.describe_keyvalue(key, value, description)
|
||||
first = False
|
||||
description.append_text("}")
|
||||
|
||||
|
||||
# Keyword argument form
|
||||
@overload
|
||||
def has_entries(**keys_valuematchers: Union[Matcher[V], V]) -> Matcher[Mapping[str, V]]:
|
||||
...
|
||||
|
||||
|
||||
# Key to matcher dict form
|
||||
@overload
|
||||
def has_entries(keys_valuematchers: Mapping[K, Union[Matcher[V], V]]) -> Matcher[Mapping[K, V]]:
|
||||
...
|
||||
|
||||
|
||||
# Alternating key/matcher form
|
||||
@overload
|
||||
def has_entries(*keys_valuematchers: Any) -> Matcher[Mapping[Any, Any]]:
|
||||
...
|
||||
|
||||
|
||||
def has_entries(*keys_valuematchers, **kv_args):
|
||||
"""Matches if dictionary contains entries satisfying a dictionary of keys
|
||||
and corresponding value matchers.
|
||||
|
||||
:param matcher_dict: A dictionary mapping keys to associated value matchers,
|
||||
or to expected values for
|
||||
:py:func:`~hamcrest.core.core.isequal.equal_to` matching.
|
||||
|
||||
Note that the keys must be actual keys, not matchers. Any value argument
|
||||
that is not a matcher is implicitly wrapped in an
|
||||
:py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
|
||||
equality.
|
||||
|
||||
Examples::
|
||||
|
||||
has_entries({'foo':equal_to(1), 'bar':equal_to(2)})
|
||||
has_entries({'foo':1, 'bar':2})
|
||||
|
||||
``has_entries`` also accepts a list of keyword arguments:
|
||||
|
||||
.. function:: has_entries(keyword1=value_matcher1[, keyword2=value_matcher2[, ...]])
|
||||
|
||||
:param keyword1: A keyword to look up.
|
||||
:param valueMatcher1: The matcher to satisfy for the value, or an expected
|
||||
value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
|
||||
|
||||
Examples::
|
||||
|
||||
has_entries(foo=equal_to(1), bar=equal_to(2))
|
||||
has_entries(foo=1, bar=2)
|
||||
|
||||
Finally, ``has_entries`` also accepts a list of alternating keys and their
|
||||
value matchers:
|
||||
|
||||
.. function:: has_entries(key1, value_matcher1[, ...])
|
||||
|
||||
:param key1: A key (not a matcher) to look up.
|
||||
:param valueMatcher1: The matcher to satisfy for the value, or an expected
|
||||
value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
|
||||
|
||||
Examples::
|
||||
|
||||
has_entries('foo', equal_to(1), 'bar', equal_to(2))
|
||||
has_entries('foo', 1, 'bar', 2)
|
||||
|
||||
"""
|
||||
if len(keys_valuematchers) == 1:
|
||||
try:
|
||||
base_dict = keys_valuematchers[0].copy()
|
||||
for key in base_dict:
|
||||
base_dict[key] = wrap_matcher(base_dict[key])
|
||||
except AttributeError:
|
||||
raise ValueError(
|
||||
"single-argument calls to has_entries must pass a dict as the argument"
|
||||
)
|
||||
else:
|
||||
if len(keys_valuematchers) % 2:
|
||||
raise ValueError("has_entries requires key-value pairs")
|
||||
base_dict = {}
|
||||
for index in range(int(len(keys_valuematchers) / 2)):
|
||||
base_dict[keys_valuematchers[2 * index]] = wrap_matcher(
|
||||
keys_valuematchers[2 * index + 1]
|
||||
)
|
||||
|
||||
for key, value in kv_args.items():
|
||||
base_dict[key] = wrap_matcher(value)
|
||||
|
||||
return IsDictContainingEntries(base_dict)
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
from typing import Any, Hashable, Mapping, TypeVar, Union
|
||||
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.description import Description
|
||||
from hamcrest.core.helpers.hasmethod import hasmethod
|
||||
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
|
||||
from hamcrest.core.matcher import Matcher
|
||||
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
K = TypeVar("K", bound=Hashable)
|
||||
|
||||
|
||||
class IsDictContainingKey(BaseMatcher[Mapping[K, Any]]):
|
||||
def __init__(self, key_matcher: Matcher[K]) -> None:
|
||||
self.key_matcher = key_matcher
|
||||
|
||||
def _matches(self, item: Mapping[K, Any]) -> bool:
|
||||
if hasmethod(item, "keys"):
|
||||
for key in item.keys():
|
||||
if self.key_matcher.matches(key):
|
||||
return True
|
||||
return False
|
||||
|
||||
def describe_to(self, description: Description) -> None:
|
||||
description.append_text("a dictionary containing key ").append_description_of(
|
||||
self.key_matcher
|
||||
)
|
||||
|
||||
|
||||
def has_key(key_match: Union[K, Matcher[K]]) -> Matcher[Mapping[K, Any]]:
|
||||
"""Matches if dictionary contains an entry whose key satisfies a given
|
||||
matcher.
|
||||
|
||||
:param key_match: The matcher to satisfy for the key, or an expected value
|
||||
for :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
|
||||
|
||||
This matcher iterates the evaluated dictionary, searching for any key-value
|
||||
entry whose key satisfies the given matcher. If a matching entry is found,
|
||||
``has_key`` is satisfied.
|
||||
|
||||
Any argument that is not a matcher is implicitly wrapped in an
|
||||
:py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
|
||||
equality.
|
||||
|
||||
Examples::
|
||||
|
||||
has_key(equal_to('foo'))
|
||||
has_key('foo')
|
||||
|
||||
"""
|
||||
return IsDictContainingKey(wrap_matcher(key_match))
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
from typing import Any, Mapping, TypeVar, Union
|
||||
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.description import Description
|
||||
from hamcrest.core.helpers.hasmethod import hasmethod
|
||||
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
|
||||
from hamcrest.core.matcher import Matcher
|
||||
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
V = TypeVar("V")
|
||||
|
||||
|
||||
class IsDictContainingValue(BaseMatcher[Mapping[Any, V]]):
|
||||
def __init__(self, value_matcher: Matcher[V]) -> None:
|
||||
self.value_matcher = value_matcher
|
||||
|
||||
def _matches(self, item: Mapping[Any, V]) -> bool:
|
||||
if hasmethod(item, "values"):
|
||||
for value in item.values():
|
||||
if self.value_matcher.matches(value):
|
||||
return True
|
||||
return False
|
||||
|
||||
def describe_to(self, description: Description) -> None:
|
||||
description.append_text("a dictionary containing value ").append_description_of(
|
||||
self.value_matcher
|
||||
)
|
||||
|
||||
|
||||
def has_value(value: Union[V, Matcher[V]]) -> Matcher[Mapping[Any, V]]:
|
||||
"""Matches if dictionary contains an entry whose value satisfies a given
|
||||
matcher.
|
||||
|
||||
:param value_match: The matcher to satisfy for the value, or an expected
|
||||
value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
|
||||
|
||||
This matcher iterates the evaluated dictionary, searching for any key-value
|
||||
entry whose value satisfies the given matcher. If a matching entry is
|
||||
found, ``has_value`` is satisfied.
|
||||
|
||||
Any argument that is not a matcher is implicitly wrapped in an
|
||||
:py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
|
||||
equality.
|
||||
|
||||
Examples::
|
||||
|
||||
has_value(equal_to('bar'))
|
||||
has_value('bar')
|
||||
|
||||
"""
|
||||
return IsDictContainingValue(wrap_matcher(value))
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
from typing import Sequence, TypeVar
|
||||
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.description import Description
|
||||
from hamcrest.core.matcher import Matcher
|
||||
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class IsIn(BaseMatcher[T]):
|
||||
def __init__(self, sequence: Sequence[T]) -> None:
|
||||
self.sequence = sequence
|
||||
|
||||
def _matches(self, item: T) -> bool:
|
||||
return item in self.sequence
|
||||
|
||||
def describe_to(self, description: Description) -> None:
|
||||
description.append_text("one of ").append_list("(", ", ", ")", self.sequence)
|
||||
|
||||
|
||||
def is_in(sequence: Sequence[T]) -> Matcher[T]:
|
||||
"""Matches if evaluated object is present in a given sequence.
|
||||
|
||||
:param sequence: The sequence to search.
|
||||
|
||||
This matcher invokes the ``in`` membership operator to determine if the
|
||||
evaluated object is a member of the sequence.
|
||||
|
||||
"""
|
||||
return IsIn(sequence)
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
from typing import Sequence, TypeVar, Union, cast
|
||||
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.core.allof import all_of
|
||||
from hamcrest.core.description import Description
|
||||
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
|
||||
from hamcrest.core.matcher import Matcher
|
||||
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class IsSequenceContaining(BaseMatcher[Sequence[T]]):
|
||||
def __init__(self, element_matcher: Matcher[T]) -> None:
|
||||
self.element_matcher = element_matcher
|
||||
|
||||
def _matches(self, item: Sequence[T]) -> bool:
|
||||
try:
|
||||
for element in item:
|
||||
if self.element_matcher.matches(element):
|
||||
return True
|
||||
except TypeError: # not a sequence
|
||||
pass
|
||||
return False
|
||||
|
||||
def describe_to(self, description: Description) -> None:
|
||||
description.append_text("a sequence containing ").append_description_of(
|
||||
self.element_matcher
|
||||
)
|
||||
|
||||
|
||||
# It'd be great to make use of all_of, but we can't be sure we won't
|
||||
# be seeing a one-time sequence here (like a generator); see issue #20
|
||||
# Instead, we wrap it inside a class that will convert the sequence into
|
||||
# a concrete list and then hand it off to the all_of matcher.
|
||||
class IsSequenceContainingEvery(BaseMatcher[Sequence[T]]):
|
||||
def __init__(self, *element_matchers: Matcher[T]) -> None:
|
||||
delegates = [cast(Matcher[Sequence[T]], has_item(e)) for e in element_matchers]
|
||||
self.matcher = all_of(*delegates) # type: Matcher[Sequence[T]]
|
||||
|
||||
def _matches(self, item: Sequence[T]) -> bool:
|
||||
try:
|
||||
return self.matcher.matches(list(item))
|
||||
except TypeError:
|
||||
return False
|
||||
|
||||
def describe_mismatch(self, item: Sequence[T], mismatch_description: Description) -> None:
|
||||
self.matcher.describe_mismatch(item, mismatch_description)
|
||||
|
||||
def describe_to(self, description: Description) -> None:
|
||||
self.matcher.describe_to(description)
|
||||
|
||||
|
||||
def has_item(match: Union[Matcher[T], T]) -> Matcher[Sequence[T]]:
|
||||
"""Matches if any element of sequence satisfies a given matcher.
|
||||
|
||||
:param match: The matcher to satisfy, or an expected value for
|
||||
:py:func:`~hamcrest.core.core.isequal.equal_to` matching.
|
||||
|
||||
This matcher iterates the evaluated sequence, searching for any element
|
||||
that satisfies a given matcher. If a matching element is found,
|
||||
``has_item`` is satisfied.
|
||||
|
||||
If the ``match`` argument is not a matcher, it is implicitly wrapped in an
|
||||
:py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
|
||||
equality.
|
||||
|
||||
"""
|
||||
return IsSequenceContaining(wrap_matcher(match))
|
||||
|
||||
|
||||
def has_items(*items: Union[Matcher[T], T]) -> Matcher[Sequence[T]]:
|
||||
"""Matches if all of the given matchers are satisfied by any elements of
|
||||
the sequence.
|
||||
|
||||
:param match1,...: A comma-separated list of matchers.
|
||||
|
||||
This matcher iterates the given matchers, searching for any elements in the
|
||||
evaluated sequence that satisfy them. If each matcher is satisfied, then
|
||||
``has_items`` is satisfied.
|
||||
|
||||
Any argument that is not a matcher is implicitly wrapped in an
|
||||
:py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
|
||||
equality.
|
||||
|
||||
"""
|
||||
matchers = []
|
||||
for item in items:
|
||||
matchers.append(wrap_matcher(item))
|
||||
return IsSequenceContainingEvery(*matchers)
|
||||
|
|
@ -0,0 +1,103 @@
|
|||
from typing import MutableSequence, Optional, Sequence, TypeVar, Union, cast
|
||||
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.description import Description
|
||||
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
|
||||
from hamcrest.core.matcher import Matcher
|
||||
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class MatchInAnyOrder(object):
|
||||
def __init__(
|
||||
self, matchers: Sequence[Matcher[T]], mismatch_description: Optional[Description]
|
||||
) -> None:
|
||||
self.matchers = cast(MutableSequence[Matcher[T]], matchers[:])
|
||||
self.mismatch_description = mismatch_description
|
||||
|
||||
def matches(self, item: T) -> bool:
|
||||
return self.isnotsurplus(item) and self.ismatched(item)
|
||||
|
||||
def isfinished(self, item: Sequence[T]) -> bool:
|
||||
if not self.matchers:
|
||||
return True
|
||||
if self.mismatch_description:
|
||||
self.mismatch_description.append_text("no item matches: ").append_list(
|
||||
"", ", ", "", self.matchers
|
||||
).append_text(" in ").append_list("[", ", ", "]", item)
|
||||
return False
|
||||
|
||||
def isnotsurplus(self, item: T) -> bool:
|
||||
if not self.matchers:
|
||||
if self.mismatch_description:
|
||||
self.mismatch_description.append_text("not matched: ").append_description_of(item)
|
||||
return False
|
||||
return True
|
||||
|
||||
def ismatched(self, item: T) -> bool:
|
||||
for index, matcher in enumerate(self.matchers):
|
||||
if matcher.matches(item):
|
||||
del self.matchers[index]
|
||||
return True
|
||||
|
||||
if self.mismatch_description:
|
||||
self.mismatch_description.append_text("not matched: ").append_description_of(item)
|
||||
return False
|
||||
|
||||
|
||||
class IsSequenceContainingInAnyOrder(BaseMatcher[Sequence[T]]):
|
||||
def __init__(self, matchers: Sequence[Matcher[T]]) -> None:
|
||||
self.matchers = matchers
|
||||
|
||||
def matches(
|
||||
self, item: Sequence[T], mismatch_description: Optional[Description] = None
|
||||
) -> bool:
|
||||
try:
|
||||
sequence = list(item)
|
||||
matchsequence = MatchInAnyOrder(self.matchers, mismatch_description)
|
||||
for element in sequence:
|
||||
if not matchsequence.matches(element):
|
||||
return False
|
||||
return matchsequence.isfinished(sequence)
|
||||
except TypeError:
|
||||
if mismatch_description:
|
||||
super(IsSequenceContainingInAnyOrder, self).describe_mismatch(
|
||||
item, mismatch_description
|
||||
)
|
||||
return False
|
||||
|
||||
def describe_mismatch(self, item: Sequence[T], mismatch_description: Description) -> None:
|
||||
self.matches(item, mismatch_description)
|
||||
|
||||
def describe_to(self, description: Description) -> None:
|
||||
description.append_text("a sequence over ").append_list(
|
||||
"[", ", ", "]", self.matchers
|
||||
).append_text(" in any order")
|
||||
|
||||
|
||||
def contains_inanyorder(*items: Union[Matcher[T], T]) -> Matcher[Sequence[T]]:
|
||||
"""Matches if sequences's elements, in any order, satisfy a given list of
|
||||
matchers.
|
||||
|
||||
:param match1,...: A comma-separated list of matchers.
|
||||
|
||||
This matcher iterates the evaluated sequence, seeing if each element
|
||||
satisfies any of the given matchers. The matchers are tried from left to
|
||||
right, and when a satisfied matcher is found, it is no longer a candidate
|
||||
for the remaining elements. If a one-to-one correspondence is established
|
||||
between elements and matchers, ``contains_inanyorder`` is satisfied.
|
||||
|
||||
Any argument that is not a matcher is implicitly wrapped in an
|
||||
:py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
|
||||
equality.
|
||||
|
||||
"""
|
||||
|
||||
matchers = []
|
||||
for item in items:
|
||||
matchers.append(wrap_matcher(item))
|
||||
return IsSequenceContainingInAnyOrder(matchers)
|
||||
|
|
@ -0,0 +1,103 @@
|
|||
import warnings
|
||||
from typing import Optional, Sequence, TypeVar, Union
|
||||
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.description import Description
|
||||
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
|
||||
from hamcrest.core.matcher import Matcher
|
||||
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class MatchingInOrder(object):
|
||||
def __init__(
|
||||
self, matchers: Sequence[Matcher[T]], mismatch_description: Optional[Description]
|
||||
) -> None:
|
||||
self.matchers = matchers
|
||||
self.mismatch_description = mismatch_description
|
||||
self.next_match_index = 0
|
||||
|
||||
def matches(self, item: T) -> bool:
|
||||
return self.isnotsurplus(item) and self.ismatched(item)
|
||||
|
||||
def isfinished(self) -> bool:
|
||||
if self.next_match_index < len(self.matchers):
|
||||
if self.mismatch_description:
|
||||
self.mismatch_description.append_text("No item matched: ").append_description_of(
|
||||
self.matchers[self.next_match_index]
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
def ismatched(self, item: T) -> bool:
|
||||
matcher = self.matchers[self.next_match_index]
|
||||
if not matcher.matches(item):
|
||||
if self.mismatch_description:
|
||||
self.mismatch_description.append_text("item " + str(self.next_match_index) + ": ")
|
||||
matcher.describe_mismatch(item, self.mismatch_description)
|
||||
return False
|
||||
self.next_match_index += 1
|
||||
return True
|
||||
|
||||
def isnotsurplus(self, item: T) -> bool:
|
||||
if len(self.matchers) <= self.next_match_index:
|
||||
if self.mismatch_description:
|
||||
self.mismatch_description.append_text("Not matched: ").append_description_of(item)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
class IsSequenceContainingInOrder(BaseMatcher[Sequence[T]]):
|
||||
def __init__(self, matchers: Sequence[Matcher[T]]) -> None:
|
||||
self.matchers = matchers
|
||||
|
||||
def matches(
|
||||
self, item: Sequence[T], mismatch_description: Optional[Description] = None
|
||||
) -> bool:
|
||||
try:
|
||||
matchsequence = MatchingInOrder(self.matchers, mismatch_description)
|
||||
for element in item:
|
||||
if not matchsequence.matches(element):
|
||||
return False
|
||||
return matchsequence.isfinished()
|
||||
except TypeError:
|
||||
if mismatch_description:
|
||||
super(IsSequenceContainingInOrder, self).describe_mismatch(
|
||||
item, mismatch_description
|
||||
)
|
||||
return False
|
||||
|
||||
def describe_mismatch(self, item: Sequence[T], mismatch_description: Description) -> None:
|
||||
self.matches(item, mismatch_description)
|
||||
|
||||
def describe_to(self, description: Description) -> None:
|
||||
description.append_text("a sequence containing ").append_list("[", ", ", "]", self.matchers)
|
||||
|
||||
|
||||
def contains_exactly(*items: Union[Matcher[T], T]) -> Matcher[Sequence[T]]:
|
||||
"""Matches if sequence's elements satisfy a given list of matchers, in order.
|
||||
|
||||
:param match1,...: A comma-separated list of matchers.
|
||||
|
||||
This matcher iterates the evaluated sequence and a given list of matchers,
|
||||
seeing if each element satisfies its corresponding matcher.
|
||||
|
||||
Any argument that is not a matcher is implicitly wrapped in an
|
||||
:py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
|
||||
equality.
|
||||
|
||||
"""
|
||||
matchers = []
|
||||
for item in items:
|
||||
matchers.append(wrap_matcher(item))
|
||||
return IsSequenceContainingInOrder(matchers)
|
||||
|
||||
|
||||
def contains(*items: Union[Matcher[T], T]) -> Matcher[Sequence[T]]:
|
||||
"""Deprecated - use contains_exactly(*items)"""
|
||||
warnings.warn("deprecated - use contains_exactly(*items)", DeprecationWarning)
|
||||
return contains_exactly(*items)
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
from typing import Sequence, TypeVar, Union
|
||||
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.core.anyof import any_of
|
||||
from hamcrest.core.description import Description
|
||||
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
|
||||
from hamcrest.core.matcher import Matcher
|
||||
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class IsSequenceOnlyContaining(BaseMatcher[Sequence[T]]):
|
||||
def __init__(self, matcher: Matcher[T]) -> None:
|
||||
self.matcher = matcher
|
||||
|
||||
def _matches(self, item: Sequence[T]) -> bool:
|
||||
try:
|
||||
sequence = list(item)
|
||||
if len(sequence) == 0:
|
||||
return False
|
||||
for element in sequence:
|
||||
if not self.matcher.matches(element):
|
||||
return False
|
||||
return True
|
||||
except TypeError:
|
||||
return False
|
||||
|
||||
def describe_to(self, description: Description) -> None:
|
||||
description.append_text("a sequence containing items matching ").append_description_of(
|
||||
self.matcher
|
||||
)
|
||||
|
||||
|
||||
def only_contains(*items: Union[Matcher[T], T]) -> Matcher[Sequence[T]]:
|
||||
"""Matches if each element of sequence satisfies any of the given matchers.
|
||||
|
||||
:param match1,...: A comma-separated list of matchers.
|
||||
|
||||
This matcher iterates the evaluated sequence, confirming whether each
|
||||
element satisfies any of the given matchers.
|
||||
|
||||
Example::
|
||||
|
||||
only_contains(less_than(4))
|
||||
|
||||
will match ``[3,1,2]``.
|
||||
|
||||
Any argument that is not a matcher is implicitly wrapped in an
|
||||
:py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
|
||||
equality.
|
||||
|
||||
"""
|
||||
matchers = []
|
||||
for item in items:
|
||||
matchers.append(wrap_matcher(item))
|
||||
return IsSequenceOnlyContaining(any_of(*matchers))
|
||||
Loading…
Add table
Add a link
Reference in a new issue