|
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, Matcher |
|
7
|
|
|
from hamcrest.core.helpers.wrap_matcher import wrap_matcher, is_matchable_type |
|
8
|
|
|
from .isequal import equal_to |
|
9
|
|
|
from .isinstanceof import instance_of |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class IsNot(BaseMatcher): |
|
13
|
|
|
|
|
14
|
|
|
def __init__(self, matcher): |
|
15
|
|
|
self.matcher = matcher |
|
16
|
|
|
|
|
17
|
|
|
def _matches(self, item): |
|
18
|
|
|
return not self.matcher.matches(item) |
|
19
|
|
|
|
|
20
|
|
|
def describe_to(self, description): |
|
21
|
|
|
description.append_text('not ').append_description_of(self.matcher) |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def wrap_value_or_type(x): |
|
25
|
|
|
if is_matchable_type(x): |
|
26
|
|
|
return instance_of(x) |
|
27
|
|
|
else: |
|
28
|
|
|
return wrap_matcher(x) |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
def is_not(match): |
|
32
|
|
|
"""Inverts the given matcher to its logical negation. |
|
33
|
|
|
|
|
34
|
|
|
:param match: The matcher to negate. |
|
35
|
|
|
|
|
36
|
|
|
This matcher compares the evaluated object to the negation of the given |
|
37
|
|
|
matcher. If the ``match`` argument is not a matcher, it is implicitly |
|
38
|
|
|
wrapped in an :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to |
|
39
|
|
|
check for equality, and thus matches for inequality. |
|
40
|
|
|
|
|
41
|
|
|
Examples:: |
|
42
|
|
|
|
|
43
|
|
|
assert_that(cheese, is_not(equal_to(smelly))) |
|
44
|
|
|
assert_that(cheese, is_not(smelly)) |
|
45
|
|
|
|
|
46
|
|
|
""" |
|
47
|
|
|
return IsNot(wrap_value_or_type(match)) |
|
48
|
|
|
|
|
49
|
|
|
def not_(match): |
|
50
|
|
|
"""Alias of :py:func:`is_not` for better readability of negations. |
|
51
|
|
|
|
|
52
|
|
|
Examples:: |
|
53
|
|
|
|
|
54
|
|
|
assert_that(alist, not_(has_item(item))) |
|
55
|
|
|
|
|
56
|
|
|
""" |
|
57
|
|
|
return is_not(match) |
|
58
|
|
|
|