1
|
|
|
from __future__ import absolute_import |
2
|
|
|
__author__ = "Jon Reid" |
3
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
4
|
|
|
__license__ = "BSD, see License.txt" |
5
|
|
|
|
6
|
|
|
from hamcrest.core.base_matcher import BaseMatcher |
7
|
|
|
from hamcrest.core.helpers.wrap_matcher import wrap_matcher, is_matchable_type |
8
|
|
|
from .isinstanceof import instance_of |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class Is(BaseMatcher): |
12
|
|
|
|
13
|
|
|
def __init__(self, matcher): |
14
|
|
|
self.matcher = matcher |
15
|
|
|
|
16
|
|
|
def matches(self, item, mismatch_description=None): |
17
|
|
|
return self.matcher.matches(item, mismatch_description) |
18
|
|
|
|
19
|
|
|
def describe_mismatch(self, item, mismatch_description): |
20
|
|
|
return self.matcher.describe_mismatch(item, mismatch_description) |
21
|
|
|
|
22
|
|
|
def describe_to(self, description): |
23
|
|
|
description.append_description_of(self.matcher) |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def wrap_value_or_type(x): |
27
|
|
|
if is_matchable_type(x): |
28
|
|
|
return instance_of(x) |
29
|
|
|
else: |
30
|
|
|
return wrap_matcher(x) |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def is_(x): |
34
|
|
|
"""Decorates another matcher, or provides shortcuts to the frequently used |
35
|
|
|
``is(equal_to(x))`` and ``is(instance_of(x))``. |
36
|
|
|
|
37
|
|
|
:param x: The matcher to satisfy, or a type for |
38
|
|
|
:py:func:`~hamcrest.core.core.isinstanceof.instance_of` matching, or an |
39
|
|
|
expected value for :py:func:`~hamcrest.core.core.isequal.equal_to` |
40
|
|
|
matching. |
41
|
|
|
|
42
|
|
|
This matcher compares the evaluated object to the given matcher. |
43
|
|
|
|
44
|
|
|
.. note:: |
45
|
|
|
|
46
|
|
|
PyHamcrest's ``is_`` matcher is unrelated to Python's ``is`` operator. |
47
|
|
|
The matcher for object identity is |
48
|
|
|
:py:func:`~hamcrest.core.core.issame.same_instance`. |
49
|
|
|
|
50
|
|
|
If the ``x`` argument is a matcher, its behavior is retained, but the test |
51
|
|
|
may be more expressive. For example:: |
52
|
|
|
|
53
|
|
|
assert_that(value, less_than(5)) |
54
|
|
|
assert_that(value, is_(less_than(5))) |
55
|
|
|
|
56
|
|
|
If the ``x`` argument is a type, it is wrapped in an |
57
|
|
|
:py:func:`~hamcrest.core.core.isinstanceof.instance_of` matcher. This makes |
58
|
|
|
the following statements equivalent:: |
59
|
|
|
|
60
|
|
|
assert_that(cheese, instance_of(Cheddar)) |
61
|
|
|
assert_that(cheese, is_(instance_of(Cheddar))) |
62
|
|
|
assert_that(cheese, is_(Cheddar)) |
63
|
|
|
|
64
|
|
|
Otherwise, if the ``x`` argument is not a matcher, it is wrapped in an |
65
|
|
|
:py:func:`~hamcrest.core.core.isequal.equal_to` matcher. This makes the |
66
|
|
|
following statements equivalent:: |
67
|
|
|
|
68
|
|
|
assert_that(cheese, equal_to(smelly)) |
69
|
|
|
assert_that(cheese, is_(equal_to(smelly))) |
70
|
|
|
assert_that(cheese, is_(smelly)) |
71
|
|
|
|
72
|
|
|
Choose the style that makes your expression most readable. This will vary |
73
|
|
|
depending on context. |
74
|
|
|
|
75
|
|
|
""" |
76
|
|
|
return Is(wrap_value_or_type(x)) |
77
|
|
|
|