17.12
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
"""Matchers of collections."""
|
||||
from __future__ import absolute_import
|
||||
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
|
||||
from .issequence_onlycontaining import only_contains
|
||||
from .is_empty import empty
|
||||
|
||||
__author__ = "Chris Rose"
|
||||
__copyright__ = "Copyright 2013 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
@@ -0,0 +1,48 @@
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.helpers.hasmethod import hasmethod
|
||||
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
|
||||
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
|
||||
class IsDictContainingKey(BaseMatcher):
|
||||
|
||||
def __init__(self, key_matcher):
|
||||
self.key_matcher = key_matcher
|
||||
|
||||
def _matches(self, dictionary):
|
||||
if hasmethod(dictionary, 'keys'):
|
||||
for key in dictionary.keys():
|
||||
if self.key_matcher.matches(key):
|
||||
return True
|
||||
return False
|
||||
|
||||
def describe_to(self, description):
|
||||
description.append_text('a dictionary containing key ') \
|
||||
.append_description_of(self.key_matcher)
|
||||
|
||||
|
||||
def has_key(key_match):
|
||||
"""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))
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.helpers.hasmethod import hasmethod
|
||||
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
|
||||
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
|
||||
class IsDictContainingValue(BaseMatcher):
|
||||
|
||||
def __init__(self, value_matcher):
|
||||
self.value_matcher = value_matcher
|
||||
|
||||
def _matches(self, dictionary):
|
||||
if hasmethod(dictionary, 'values'):
|
||||
for value in dictionary.values():
|
||||
if self.value_matcher.matches(value):
|
||||
return True
|
||||
return False
|
||||
|
||||
def describe_to(self, description):
|
||||
description.append_text('a dictionary containing value ') \
|
||||
.append_description_of(self.value_matcher)
|
||||
|
||||
|
||||
def has_value(value):
|
||||
"""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,89 @@
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.core.allof import all_of
|
||||
from hamcrest.core.helpers.hasmethod import hasmethod
|
||||
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
|
||||
|
||||
|
||||
class IsSequenceContaining(BaseMatcher):
|
||||
|
||||
def __init__(self, element_matcher):
|
||||
self.element_matcher = element_matcher
|
||||
|
||||
def _matches(self, sequence):
|
||||
try:
|
||||
for item in sequence:
|
||||
if self.element_matcher.matches(item):
|
||||
return True
|
||||
except TypeError: # not a sequence
|
||||
return False
|
||||
|
||||
def describe_to(self, description):
|
||||
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):
|
||||
|
||||
def __init__(self, *element_matchers):
|
||||
delegates = [has_item(e) for e in element_matchers]
|
||||
self.matcher = all_of(*delegates)
|
||||
|
||||
def _matches(self, sequence):
|
||||
try:
|
||||
return self.matcher.matches(list(sequence))
|
||||
except TypeError:
|
||||
return False
|
||||
|
||||
def describe_mismatch(self, item, mismatch_description):
|
||||
self.matcher.describe_mismatch(item, mismatch_description)
|
||||
|
||||
def describe_to(self, description):
|
||||
self.matcher.describe_to(description)
|
||||
|
||||
|
||||
|
||||
def has_item(match):
|
||||
"""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):
|
||||
"""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)
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.helpers.hasmethod import hasmethod
|
||||
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
|
||||
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
|
||||
class MatchInAnyOrder(object):
|
||||
def __init__(self, matchers, mismatch_description):
|
||||
self.matchers = matchers[:]
|
||||
self.mismatch_description = mismatch_description
|
||||
|
||||
def matches(self, item):
|
||||
return self.isnotsurplus(item) and self.ismatched(item)
|
||||
|
||||
def isfinished(self, sequence):
|
||||
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('[', ', ', ']', sequence)
|
||||
return False
|
||||
|
||||
def isnotsurplus(self, item):
|
||||
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):
|
||||
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):
|
||||
|
||||
def __init__(self, matchers):
|
||||
self.matchers = matchers
|
||||
|
||||
def matches(self, sequence, mismatch_description=None):
|
||||
try:
|
||||
sequence = list(sequence)
|
||||
matchsequence = MatchInAnyOrder(self.matchers, mismatch_description)
|
||||
for item in sequence:
|
||||
if not matchsequence.matches(item):
|
||||
return False
|
||||
return matchsequence.isfinished(sequence)
|
||||
except TypeError:
|
||||
if mismatch_description:
|
||||
super(IsSequenceContainingInAnyOrder, self) \
|
||||
.describe_mismatch(sequence, mismatch_description)
|
||||
return False
|
||||
|
||||
def describe_mismatch(self, item, mismatch_description):
|
||||
self.matches(item, mismatch_description)
|
||||
|
||||
def describe_to(self, description):
|
||||
description.append_text('a sequence over ') \
|
||||
.append_list('[', ', ', ']', self.matchers) \
|
||||
.append_text(' in any order')
|
||||
|
||||
|
||||
def contains_inanyorder(*items):
|
||||
"""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)
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
from hamcrest.core.base_matcher import BaseMatcher
|
||||
from hamcrest.core.core.anyof import any_of
|
||||
from hamcrest.core.helpers.hasmethod import hasmethod
|
||||
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
|
||||
|
||||
__author__ = "Jon Reid"
|
||||
__copyright__ = "Copyright 2011 hamcrest.org"
|
||||
__license__ = "BSD, see License.txt"
|
||||
|
||||
|
||||
class IsSequenceOnlyContaining(BaseMatcher):
|
||||
|
||||
def __init__(self, matcher):
|
||||
self.matcher = matcher
|
||||
|
||||
def _matches(self, sequence):
|
||||
try:
|
||||
sequence = list(sequence)
|
||||
if len(sequence) == 0:
|
||||
return False
|
||||
for item in sequence:
|
||||
if not self.matcher.matches(item):
|
||||
return False
|
||||
return True
|
||||
except TypeError:
|
||||
return False
|
||||
|
||||
def describe_to(self, description):
|
||||
description.append_text('a sequence containing items matching ') \
|
||||
.append_description_of(self.matcher)
|
||||
|
||||
|
||||
def only_contains(*items):
|
||||
"""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))
|
||||
Reference in New Issue
Block a user