src.hamcrest.core.core.IsSame   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 20
Duplicated Lines 0 %
Metric Value
dl 0
loc 20
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A _matches() 0 2 1
A __init__() 0 2 1
A describe_mismatch() 0 6 2
A describe_to() 0 5 1
1
__author__ = "Jon Reid"
2
__copyright__ = "Copyright 2011 hamcrest.org"
3
__license__ = "BSD, see License.txt"
4
5
from hamcrest.core.base_matcher import BaseMatcher
6
7
8
class IsSame(BaseMatcher):
9
10
    def __init__(self, object):
11
        self.object = object
12
13
    def _matches(self, item):
14
        return item is self.object
15
16
    def describe_to(self, description):
17
        description.append_text('same instance as ')            \
18
                   .append_text(hex(id(self.object)))           \
19
                   .append_text(' ')                            \
20
                   .append_description_of(self.object)
21
22
    def describe_mismatch(self, item, mismatch_description):
23
        mismatch_description.append_text('was ')
24
        if item is not None:
25
            mismatch_description.append_text(hex(id(item)))         \
26
                                .append_text(' ')
27
        mismatch_description.append_description_of(item)
28
29
30
def same_instance(obj):
31
    """Matches if evaluated object is the same instance as a given object.
32
33
    :param obj: The object to compare against as the expected value.
34
35
    This matcher invokes the ``is`` identity operator to determine if the
36
    evaluated object is the the same object as ``obj``.
37
38
    """
39
    return IsSame(obj)
40