src.hamcrest.library.integration.EqualityWrapper   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 13
Duplicated Lines 0 %
Metric Value
dl 0
loc 13
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __eq__() 0 2 1
A __repr__() 0 2 1
A __str__() 0 2 1
A __init__() 0 2 1
1
from hamcrest.core.string_description import tostring
2
from hamcrest.core.helpers.wrap_matcher import wrap_matcher
3
4
__author__ = "Chris Rose"
5
__copyright__ = "Copyright 2011 hamcrest.org"
6
__license__ = "BSD, see License.txt"
7
__unittest = True
8
9
10
class EqualityWrapper(object):
11
12
    def __init__(self, matcher):
13
        self.matcher = matcher
14
15
    def __eq__(self, object):
16
        return self.matcher.matches(object)
17
18
    def __str__(self):
19
        return repr(self)
20
21
    def __repr__(self):
22
        return tostring(self.matcher)
23
24
25
def match_equality(matcher):
26
    """Wraps a matcher to define equality in terms of satisfying the matcher.
27
28
    ``match_equality`` allows Hamcrest matchers to be used in libraries that
29
    are not Hamcrest-aware. They might use the equality operator::
30
31
        assert match_equality(matcher) == object
32
33
    Or they might provide a method that uses equality for its test::
34
35
        library.method_that_tests_eq(match_equality(matcher))
36
37
    One concrete example is integrating with the ``assert_called_with`` methods
38
    in Michael Foord's `mock <http://www.voidspace.org.uk/python/mock/>`_
39
    library.
40
41
    """
42
    return EqualityWrapper(wrap_matcher(matcher))
43