Code Duplication    Length = 64-65 lines in 2 locations

src/hamcrest/library/object/hasproperty.py 1 location

@@ 90-154 (lines=65) @@
87
    return IsObjectWithProperty(name, wrap_shortcut(match))
88
89
90
def has_properties(*keys_valuematchers, **kv_args):
91
    """Matches if an object has properties satisfying all of a dictionary
92
    of string property names and corresponding value matchers.
93
94
    :param matcher_dict: A dictionary mapping keys to associated value matchers,
95
        or to expected values for
96
        :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
97
98
    Note that the keys must be actual keys, not matchers. Any value argument
99
    that is not a matcher is implicitly wrapped in an
100
    :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
101
    equality.
102
103
    Examples::
104
105
        has_properties({'foo':equal_to(1), 'bar':equal_to(2)})
106
        has_properties({'foo':1, 'bar':2})
107
108
    ``has_properties`` also accepts a list of keyword arguments:
109
110
    .. function:: has_properties(keyword1=value_matcher1[, keyword2=value_matcher2[, ...]])
111
112
    :param keyword1: A keyword to look up.
113
    :param valueMatcher1: The matcher to satisfy for the value, or an expected
114
        value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
115
116
    Examples::
117
118
        has_properties(foo=equal_to(1), bar=equal_to(2))
119
        has_properties(foo=1, bar=2)
120
121
    Finally, ``has_properties`` also accepts a list of alternating keys and their
122
    value matchers:
123
124
    .. function:: has_properties(key1, value_matcher1[, ...])
125
126
    :param key1: A key (not a matcher) to look up.
127
    :param valueMatcher1: The matcher to satisfy for the value, or an expected
128
        value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
129
130
    Examples::
131
132
        has_properties('foo', equal_to(1), 'bar', equal_to(2))
133
        has_properties('foo', 1, 'bar', 2)
134
135
    """
136
    if len(keys_valuematchers) == 1:
137
        try:
138
            base_dict = keys_valuematchers[0].copy()
139
            for key in base_dict:
140
                base_dict[key] = wrap_shortcut(base_dict[key])
141
        except AttributeError:
142
            raise ValueError('single-argument calls to has_properties must pass a dict as the argument')
143
    else:
144
        if len(keys_valuematchers) % 2:
145
            raise ValueError('has_properties requires key-value pairs')
146
        base_dict = {}
147
        for index in range(int(len(keys_valuematchers) / 2)):
148
            base_dict[keys_valuematchers[2 * index]] = wrap_shortcut(keys_valuematchers[2 * index + 1])
149
150
    for key, value in kv_args.items():
151
        base_dict[key] = wrap_shortcut(value)
152
153
    return all_of(*[has_property(property_name, property_value_matcher) for \
154
                   property_name, property_value_matcher in base_dict.items()])
155

src/hamcrest/library/collection/isdict_containingentries.py 1 location

@@ 70-133 (lines=64) @@
67
        description.append_text('}')
68
69
70
def has_entries(*keys_valuematchers, **kv_args):
71
    """Matches if dictionary contains entries satisfying a dictionary of keys
72
    and corresponding value matchers.
73
74
    :param matcher_dict: A dictionary mapping keys to associated value matchers,
75
        or to expected values for
76
        :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
77
78
    Note that the keys must be actual keys, not matchers. Any value argument
79
    that is not a matcher is implicitly wrapped in an
80
    :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
81
    equality.
82
83
    Examples::
84
85
        has_entries({'foo':equal_to(1), 'bar':equal_to(2)})
86
        has_entries({'foo':1, 'bar':2})
87
88
    ``has_entries`` also accepts a list of keyword arguments:
89
90
    .. function:: has_entries(keyword1=value_matcher1[, keyword2=value_matcher2[, ...]])
91
92
    :param keyword1: A keyword to look up.
93
    :param valueMatcher1: The matcher to satisfy for the value, or an expected
94
        value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
95
96
    Examples::
97
98
        has_entries(foo=equal_to(1), bar=equal_to(2))
99
        has_entries(foo=1, bar=2)
100
101
    Finally, ``has_entries`` also accepts a list of alternating keys and their
102
    value matchers:
103
104
    .. function:: has_entries(key1, value_matcher1[, ...])
105
106
    :param key1: A key (not a matcher) to look up.
107
    :param valueMatcher1: The matcher to satisfy for the value, or an expected
108
        value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
109
110
    Examples::
111
112
        has_entries('foo', equal_to(1), 'bar', equal_to(2))
113
        has_entries('foo', 1, 'bar', 2)
114
115
    """
116
    if len(keys_valuematchers) == 1:
117
        try:
118
            base_dict = keys_valuematchers[0].copy()
119
            for key in base_dict:
120
                base_dict[key] = wrap_matcher(base_dict[key])
121
        except AttributeError:
122
            raise ValueError('single-argument calls to has_entries must pass a dict as the argument')
123
    else:
124
        if len(keys_valuematchers) % 2:
125
            raise ValueError('has_entries requires key-value pairs')
126
        base_dict = {}
127
        for index in range(int(len(keys_valuematchers) / 2)):
128
            base_dict[keys_valuematchers[2 * index]] = wrap_matcher(keys_valuematchers[2 * index + 1])
129
130
    for key, value in kv_args.items():
131
        base_dict[key] = wrap_matcher(value)
132
133
    return IsDictContainingEntries(base_dict)
134