1
|
|
|
from hamcrest.core.base_matcher import BaseMatcher |
2
|
|
|
from hamcrest.core.helpers.hasmethod import hasmethod |
3
|
|
|
from hamcrest.core.helpers.wrap_matcher import wrap_matcher |
4
|
|
|
|
5
|
|
|
__author__ = "Jon Reid" |
6
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
7
|
|
|
__license__ = "BSD, see License.txt" |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class IsDictContainingValue(BaseMatcher): |
11
|
|
|
|
12
|
|
|
def __init__(self, value_matcher): |
13
|
|
|
self.value_matcher = value_matcher |
14
|
|
|
|
15
|
|
|
def _matches(self, dictionary): |
16
|
|
|
if hasmethod(dictionary, 'values'): |
17
|
|
|
for value in dictionary.values(): |
18
|
|
|
if self.value_matcher.matches(value): |
19
|
|
|
return True |
20
|
|
|
return False |
21
|
|
|
|
22
|
|
|
def describe_to(self, description): |
23
|
|
|
description.append_text('a dictionary containing value ') \ |
24
|
|
|
.append_description_of(self.value_matcher) |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def has_value(value): |
28
|
|
|
"""Matches if dictionary contains an entry whose value satisfies a given |
29
|
|
|
matcher. |
30
|
|
|
|
31
|
|
|
:param value_match: The matcher to satisfy for the value, or an expected |
32
|
|
|
value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching. |
33
|
|
|
|
34
|
|
|
This matcher iterates the evaluated dictionary, searching for any key-value |
35
|
|
|
entry whose value satisfies the given matcher. If a matching entry is |
36
|
|
|
found, ``has_value`` is satisfied. |
37
|
|
|
|
38
|
|
|
Any argument that is not a matcher is implicitly wrapped in an |
39
|
|
|
:py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for |
40
|
|
|
equality. |
41
|
|
|
|
42
|
|
|
Examples:: |
43
|
|
|
|
44
|
|
|
has_value(equal_to('bar')) |
45
|
|
|
has_value('bar') |
46
|
|
|
|
47
|
|
|
""" |
48
|
|
|
return IsDictContainingValue(wrap_matcher(value)) |
49
|
|
|
|