diff options
author | nkozlovskiy <[email protected]> | 2023-09-29 12:24:06 +0300 |
---|---|---|
committer | nkozlovskiy <[email protected]> | 2023-09-29 12:41:34 +0300 |
commit | e0e3e1717e3d33762ce61950504f9637a6e669ed (patch) | |
tree | bca3ff6939b10ed60c3d5c12439963a1146b9711 /contrib/python/PyHamcrest/py3/hamcrest/core | |
parent | 38f2c5852db84c7b4d83adfcb009eb61541d1ccd (diff) |
add ydb deps
Diffstat (limited to 'contrib/python/PyHamcrest/py3/hamcrest/core')
25 files changed, 1032 insertions, 0 deletions
diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/__init__.py b/contrib/python/PyHamcrest/py3/hamcrest/core/__init__.py new file mode 100644 index 00000000000..779fa723459 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/__init__.py @@ -0,0 +1,7 @@ +from __future__ import absolute_import +from hamcrest.core.assert_that import assert_that +from hamcrest.core.core import * + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/assert_that.py b/contrib/python/PyHamcrest/py3/hamcrest/core/assert_that.py new file mode 100644 index 00000000000..b38bc243ca0 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/assert_that.py @@ -0,0 +1,64 @@ +from __future__ import absolute_import +from hamcrest.core.matcher import Matcher +from hamcrest.core.string_description import StringDescription + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" +# unittest integration; hide these frames from tracebacks +__unittest = True +# py.test integration; hide these frames from tracebacks +__tracebackhide__ = True + +def assert_that(arg1, arg2=None, arg3=''): + """Asserts that actual value satisfies matcher. (Can also assert plain + boolean condition.) + + :param actual: The object to evaluate as the actual value. + :param matcher: The matcher to satisfy as the expected condition. + :param reason: Optional explanation to include in failure description. + + ``assert_that`` passes the actual value to the matcher for evaluation. If + the matcher is not satisfied, an exception is thrown describing the + mismatch. + + ``assert_that`` is designed to integrate well with PyUnit and other unit + testing frameworks. The exception raised for an unmet assertion is an + :py:exc:`AssertionError`, which PyUnit reports as a test failure. + + With a different set of parameters, ``assert_that`` can also verify a + boolean condition: + + .. function:: assert_that(assertion[, reason]) + + :param assertion: Boolean condition to verify. + :param reason: Optional explanation to include in failure description. + + This is equivalent to the :py:meth:`~unittest.TestCase.assertTrue` method + of :py:class:`unittest.TestCase`, but offers greater flexibility in test + writing by being a standalone function. + + """ + if isinstance(arg2, Matcher): + _assert_match(actual=arg1, matcher=arg2, reason=arg3) + else: + _assert_bool(assertion=arg1, reason=arg2) + + +def _assert_match(actual, matcher, reason): + if not matcher.matches(actual): + description = StringDescription() + description.append_text(reason) \ + .append_text('\nExpected: ') \ + .append_description_of(matcher) \ + .append_text('\n but: ') + matcher.describe_mismatch(actual, description) + description.append_text('\n') + raise AssertionError(description) + + +def _assert_bool(assertion, reason=None): + if not assertion: + if not reason: + reason = 'Assertion failed' + raise AssertionError(reason) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/base_description.py b/contrib/python/PyHamcrest/py3/hamcrest/core/base_description.py new file mode 100644 index 00000000000..8c7c51364de --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/base_description.py @@ -0,0 +1,92 @@ +from __future__ import absolute_import +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +import warnings +import six + +from hamcrest.core.description import Description +from hamcrest.core.selfdescribingvalue import SelfDescribingValue +from hamcrest.core.helpers.hasmethod import hasmethod + +class BaseDescription(Description): + """Base class for all :py:class:`~hamcrest.core.description.Description` + implementations. + + """ + + def append_text(self, text): + self.append(text) + return self + + def append_description_of(self, value): + if hasmethod(value, 'describe_to'): + value.describe_to(self) + elif six.PY3 and isinstance(value, six.text_type): + self.append(repr(value)) + elif six.PY2 and isinstance(value, six.binary_type): + self.append_string_in_python_syntax(value) + elif isinstance(value, six.text_type): + self.append_string_in_python_syntax(value) + else: + description = str(value) + if description[:1] == '<' and description[-1:] == '>': + self.append(description) + else: + self.append('<') + self.append(description) + self.append('>') + return self + + def append_value(self, value): + warnings.warn('Call append_description_of instead of append_value', + DeprecationWarning) + if isinstance(value, str): + self.append_string_in_python_syntax(value) + else: + self.append('<') + self.append(str(value)) + self.append('>') + return self + + def append_value_list(self, start, separator, end, list): + warnings.warn('Call append_list instead of append_value_list', + DeprecationWarning) + return self.append_list(start, separator, end, + map(SelfDescribingValue, list)) + + def append_list(self, start, separator, end, list): + separate = False + + self.append(start) + for item in list: + if separate: + self.append(separator) + self.append_description_of(item) + separate = True + self.append(end) + return self + + def append(self, string): + """Append the string to the description.""" + raise NotImplementedError('append') + + def append_string_in_python_syntax(self, string): + self.append("'") + for ch in string: + self.append(character_in_python_syntax(ch)) + self.append("'") + + +def character_in_python_syntax(ch): + if ch == "'": + return "\'" + elif ch == '\n': + return '\\n' + elif ch == '\r': + return '\\r' + elif ch == '\t': + return '\\t' + else: + return ch diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/base_matcher.py b/contrib/python/PyHamcrest/py3/hamcrest/core/base_matcher.py new file mode 100644 index 00000000000..951e2a7b95d --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/base_matcher.py @@ -0,0 +1,34 @@ +from __future__ import absolute_import +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +from hamcrest.core.matcher import Matcher +from hamcrest.core.string_description import tostring + + +class BaseMatcher(Matcher): + """Base class for all :py:class:`~hamcrest.core.matcher.Matcher` + implementations. + + Most implementations can just implement :py:obj:`_matches`, leaving the + handling of any mismatch description to the ``matches`` method. But if it + makes more sense to generate the mismatch description during the matching, + override :py:meth:`~hamcrest.core.matcher.Matcher.matches` instead. + + """ + + def __str__(self): + return tostring(self) + + def _matches(self, item): + raise NotImplementedError('_matches') + + def matches(self, item, mismatch_description=None): + match_result = self._matches(item) + if not match_result and mismatch_description: + self.describe_mismatch(item, mismatch_description) + return match_result + + def describe_mismatch(self, item, mismatch_description): + mismatch_description.append_text('was ').append_description_of(item) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/compat.py b/contrib/python/PyHamcrest/py3/hamcrest/core/compat.py new file mode 100644 index 00000000000..2c6d1fa74c8 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/compat.py @@ -0,0 +1,19 @@ +__author__ = "Per Fagrell" +__copyright__ = "Copyright 2013 hamcrest.org" +__license__ = "BSD, see License.txt" + +__all__ = ['is_callable'] + +import sys + +# callable was not part of py3k until 3.2, so we create this +# generic is_callable to use callable if possible, otherwise +# we use generic homebrew. +if sys.version_info[0] == 3 and sys.version_info[1] < 2: + def is_callable(function): + """Return whether the object is callable (i.e., some kind of function).""" + if function is None: + return False + return any("__call__" in klass.__dict__ for klass in type(function).__mro__) +else: + is_callable = callable diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/core/__init__.py b/contrib/python/PyHamcrest/py3/hamcrest/core/core/__init__.py new file mode 100644 index 00000000000..38e93e249a7 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/core/__init__.py @@ -0,0 +1,18 @@ +from __future__ import absolute_import +"""Fundamental matchers of objects and values, and composite matchers.""" + +from hamcrest.core.core.allof import all_of +from hamcrest.core.core.anyof import any_of +from hamcrest.core.core.described_as import described_as +from hamcrest.core.core.is_ import is_ +from hamcrest.core.core.isanything import anything +from hamcrest.core.core.isequal import equal_to +from hamcrest.core.core.isinstanceof import instance_of +from hamcrest.core.core.isnone import none, not_none +from hamcrest.core.core.isnot import is_not, not_ +from hamcrest.core.core.issame import same_instance +from hamcrest.core.core.raises import calling, raises + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/core/allof.py b/contrib/python/PyHamcrest/py3/hamcrest/core/core/allof.py new file mode 100644 index 00000000000..35c5d0bfeb6 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/core/allof.py @@ -0,0 +1,44 @@ +from hamcrest.core.base_matcher import BaseMatcher +from hamcrest.core.helpers.wrap_matcher import wrap_matcher + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + + +class AllOf(BaseMatcher): + + def __init__(self, *matchers): + self.matchers = matchers + + def matches(self, item, mismatch_description=None): + for matcher in self.matchers: + if not matcher.matches(item): + if mismatch_description: + mismatch_description.append_description_of(matcher) \ + .append_text(' ') + matcher.describe_mismatch(item, mismatch_description) + return False + return True + + def describe_mismatch(self, item, mismatch_description): + self.matches(item, mismatch_description) + + def describe_to(self, description): + description.append_list('(', ' and ', ')', self.matchers) + + +def all_of(*items): + """Matches if all of the given matchers evaluate to ``True``. + + :param matcher1,...: A comma-separated list of matchers. + + The matchers are evaluated from left to right using short-circuit + evaluation, so evaluation stops as soon as a matcher returns ``False``. + + Any argument that is not a matcher is implicitly wrapped in an + :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for + equality. + + """ + return AllOf(*[wrap_matcher(item) for item in items]) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/core/anyof.py b/contrib/python/PyHamcrest/py3/hamcrest/core/core/anyof.py new file mode 100644 index 00000000000..7a2bfc6627d --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/core/anyof.py @@ -0,0 +1,37 @@ +from hamcrest.core.base_matcher import BaseMatcher +from hamcrest.core.helpers.wrap_matcher import wrap_matcher + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + + +class AnyOf(BaseMatcher): + + def __init__(self, *matchers): + self.matchers = matchers + + def _matches(self, item): + for matcher in self.matchers: + if matcher.matches(item): + return True + return False + + def describe_to(self, description): + description.append_list('(', ' or ', ')', self.matchers) + + +def any_of(*items): + """Matches if any of the given matchers evaluate to ``True``. + + :param matcher1,...: A comma-separated list of matchers. + + The matchers are evaluated from left to right using short-circuit + evaluation, so evaluation stops as soon as a matcher returns ``True``. + + Any argument that is not a matcher is implicitly wrapped in an + :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for + equality. + + """ + return AnyOf(*[wrap_matcher(item) for item in items]) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/core/described_as.py b/contrib/python/PyHamcrest/py3/hamcrest/core/core/described_as.py new file mode 100644 index 00000000000..93b4d6ac458 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/core/described_as.py @@ -0,0 +1,48 @@ +from hamcrest.core.base_matcher import BaseMatcher +import re + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + + +ARG_PATTERN = re.compile('%([0-9]+)') + + +class DescribedAs(BaseMatcher): + + def __init__(self, description_template, matcher, *values): + self.template = description_template + self.matcher = matcher + self.values = values + + def matches(self, item, mismatch_description=None): + return self.matcher.matches(item, mismatch_description) + + def describe_mismatch(self, item, mismatch_description): + self.matcher.describe_mismatch(item, mismatch_description) + + def describe_to(self, description): + text_start = 0 + for match in re.finditer(ARG_PATTERN, self.template): + description.append_text(self.template[text_start:match.start()]) + arg_index = int(match.group()[1:]) + description.append_description_of(self.values[arg_index]) + text_start = match.end() + + if text_start < len(self.template): + description.append_text(self.template[text_start:]) + + +def described_as(description, matcher, *values): + """Adds custom failure description to a given matcher. + + :param description: Overrides the matcher's description. + :param matcher: The matcher to satisfy. + :param value1,...: Optional comma-separated list of substitution values. + + The description may contain substitution placeholders %0, %1, etc. These + will be replaced by any values that follow the matcher. + + """ + return DescribedAs(description, matcher, *values) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/core/is_.py b/contrib/python/PyHamcrest/py3/hamcrest/core/core/is_.py new file mode 100644 index 00000000000..ba11a762aeb --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/core/is_.py @@ -0,0 +1,76 @@ +from __future__ import absolute_import +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +from hamcrest.core.base_matcher import BaseMatcher +from hamcrest.core.helpers.wrap_matcher import wrap_matcher, is_matchable_type +from .isinstanceof import instance_of + + +class Is(BaseMatcher): + + def __init__(self, matcher): + self.matcher = matcher + + def matches(self, item, mismatch_description=None): + return self.matcher.matches(item, mismatch_description) + + def describe_mismatch(self, item, mismatch_description): + return self.matcher.describe_mismatch(item, mismatch_description) + + def describe_to(self, description): + description.append_description_of(self.matcher) + + +def wrap_value_or_type(x): + if is_matchable_type(x): + return instance_of(x) + else: + return wrap_matcher(x) + + +def is_(x): + """Decorates another matcher, or provides shortcuts to the frequently used + ``is(equal_to(x))`` and ``is(instance_of(x))``. + + :param x: The matcher to satisfy, or a type for + :py:func:`~hamcrest.core.core.isinstanceof.instance_of` matching, or an + expected value for :py:func:`~hamcrest.core.core.isequal.equal_to` + matching. + + This matcher compares the evaluated object to the given matcher. + + .. note:: + + PyHamcrest's ``is_`` matcher is unrelated to Python's ``is`` operator. + The matcher for object identity is + :py:func:`~hamcrest.core.core.issame.same_instance`. + + If the ``x`` argument is a matcher, its behavior is retained, but the test + may be more expressive. For example:: + + assert_that(value, less_than(5)) + assert_that(value, is_(less_than(5))) + + If the ``x`` argument is a type, it is wrapped in an + :py:func:`~hamcrest.core.core.isinstanceof.instance_of` matcher. This makes + the following statements equivalent:: + + assert_that(cheese, instance_of(Cheddar)) + assert_that(cheese, is_(instance_of(Cheddar))) + assert_that(cheese, is_(Cheddar)) + + Otherwise, if the ``x`` argument is not a matcher, it is wrapped in an + :py:func:`~hamcrest.core.core.isequal.equal_to` matcher. This makes the + following statements equivalent:: + + assert_that(cheese, equal_to(smelly)) + assert_that(cheese, is_(equal_to(smelly))) + assert_that(cheese, is_(smelly)) + + Choose the style that makes your expression most readable. This will vary + depending on context. + + """ + return Is(wrap_value_or_type(x)) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/core/isanything.py b/contrib/python/PyHamcrest/py3/hamcrest/core/core/isanything.py new file mode 100644 index 00000000000..f916811bc97 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/core/isanything.py @@ -0,0 +1,31 @@ +from hamcrest.core.base_matcher import BaseMatcher + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + + +class IsAnything(BaseMatcher): + + def __init__(self, description): + self.description = description + if not description: + self.description = 'ANYTHING' + + def _matches(self, item): + return True + + def describe_to(self, description): + description.append_text(self.description) + + +def anything(description=None): + """Matches anything. + + :param description: Optional string used to describe this matcher. + + This matcher always evaluates to ``True``. Specify this in composite + matchers when the value of a particular element is unimportant. + + """ + return IsAnything(description) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/core/isequal.py b/contrib/python/PyHamcrest/py3/hamcrest/core/core/isequal.py new file mode 100644 index 00000000000..119fd58a482 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/core/isequal.py @@ -0,0 +1,32 @@ +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +from hamcrest.core.base_matcher import BaseMatcher +from hamcrest.core.matcher import Matcher + + +class IsEqual(BaseMatcher): + + def __init__(self, equals): + self.object = equals + + def _matches(self, item): + return item == self.object + + def describe_to(self, description): + nested_matcher = isinstance(self.object, Matcher) + if nested_matcher: + description.append_text('<') + description.append_description_of(self.object) + if nested_matcher: + description.append_text('>') + + +def equal_to(obj): + """Matches if object is equal to a given object. + + :param obj: The object to compare against as the expected value. + + This matcher compares the evaluated object to ``obj`` for equality.""" + return IsEqual(obj) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/core/isinstanceof.py b/contrib/python/PyHamcrest/py3/hamcrest/core/core/isinstanceof.py new file mode 100644 index 00000000000..f8eb4a2fd10 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/core/isinstanceof.py @@ -0,0 +1,43 @@ +from hamcrest.core.base_matcher import BaseMatcher +from hamcrest.core.helpers.wrap_matcher import is_matchable_type + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +import types + +class IsInstanceOf(BaseMatcher): + + def __init__(self, expected_type): + if not is_matchable_type(expected_type): + raise TypeError('IsInstanceOf requires type or a tuple of classes and types') + self.expected_type = expected_type + + def _matches(self, item): + return isinstance(item, self.expected_type) + + def describe_to(self, description): + try: + type_description = self.expected_type.__name__ + except AttributeError: + type_description = "one of %s" % ",".join(str(e) for e in self.expected_type) + description.append_text('an instance of ') \ + .append_text(type_description) + + +def instance_of(atype): + """Matches if object is an instance of, or inherits from, a given type. + + :param atype: The type to compare against as the expected type or a tuple + of types. + + This matcher checks whether the evaluated object is an instance of + ``atype`` or an instance of any class that inherits from ``atype``. + + Example:: + + instance_of(str) + + """ + return IsInstanceOf(atype) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/core/isnone.py b/contrib/python/PyHamcrest/py3/hamcrest/core/core/isnone.py new file mode 100644 index 00000000000..511fd5ae461 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/core/isnone.py @@ -0,0 +1,26 @@ +from __future__ import absolute_import +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +from hamcrest.core.base_matcher import BaseMatcher +from .isnot import is_not + + +class IsNone(BaseMatcher): + + def _matches(self, item): + return item is None + + def describe_to(self, description): + description.append_text('None') + + +def none(): + """Matches if object is ``None``.""" + return IsNone() + + +def not_none(): + """Matches if object is not ``None``.""" + return is_not(none()) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/core/isnot.py b/contrib/python/PyHamcrest/py3/hamcrest/core/core/isnot.py new file mode 100644 index 00000000000..7567e6f325d --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/core/isnot.py @@ -0,0 +1,57 @@ +from __future__ import absolute_import +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +from hamcrest.core.base_matcher import BaseMatcher, Matcher +from hamcrest.core.helpers.wrap_matcher import wrap_matcher, is_matchable_type +from .isequal import equal_to +from .isinstanceof import instance_of + + +class IsNot(BaseMatcher): + + def __init__(self, matcher): + self.matcher = matcher + + def _matches(self, item): + return not self.matcher.matches(item) + + def describe_to(self, description): + description.append_text('not ').append_description_of(self.matcher) + + +def wrap_value_or_type(x): + if is_matchable_type(x): + return instance_of(x) + else: + return wrap_matcher(x) + + +def is_not(match): + """Inverts the given matcher to its logical negation. + + :param match: The matcher to negate. + + This matcher compares the evaluated object to the negation of the given + matcher. If the ``match`` argument is not a matcher, it is implicitly + wrapped in an :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to + check for equality, and thus matches for inequality. + + Examples:: + + assert_that(cheese, is_not(equal_to(smelly))) + assert_that(cheese, is_not(smelly)) + + """ + return IsNot(wrap_value_or_type(match)) + +def not_(match): + """Alias of :py:func:`is_not` for better readability of negations. + + Examples:: + + assert_that(alist, not_(has_item(item))) + + """ + return is_not(match) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/core/issame.py b/contrib/python/PyHamcrest/py3/hamcrest/core/core/issame.py new file mode 100644 index 00000000000..b1f85427d7a --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/core/issame.py @@ -0,0 +1,39 @@ +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +from hamcrest.core.base_matcher import BaseMatcher + + +class IsSame(BaseMatcher): + + def __init__(self, object): + self.object = object + + def _matches(self, item): + return item is self.object + + def describe_to(self, description): + description.append_text('same instance as ') \ + .append_text(hex(id(self.object))) \ + .append_text(' ') \ + .append_description_of(self.object) + + def describe_mismatch(self, item, mismatch_description): + mismatch_description.append_text('was ') + if item is not None: + mismatch_description.append_text(hex(id(item))) \ + .append_text(' ') + mismatch_description.append_description_of(item) + + +def same_instance(obj): + """Matches if evaluated object is the same instance as a given object. + + :param obj: The object to compare against as the expected value. + + This matcher invokes the ``is`` identity operator to determine if the + evaluated object is the the same object as ``obj``. + + """ + return IsSame(obj) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/core/raises.py b/contrib/python/PyHamcrest/py3/hamcrest/core/core/raises.py new file mode 100644 index 00000000000..16614cc1b77 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/core/raises.py @@ -0,0 +1,120 @@ +from weakref import ref +import re +import sys +from hamcrest.core.base_matcher import BaseMatcher +from hamcrest.core.compat import is_callable + +__author__ = "Per Fagrell" +__copyright__ = "Copyright 2013 hamcrest.org" +__license__ = "BSD, see License.txt" + + +class Raises(BaseMatcher): + def __init__(self, expected, pattern=None, matcher=None): + self.pattern = pattern + self.expected = expected + self.actual = None + self.function = None + self.matcher = matcher + self.actual_return_value = None + + def _matches(self, function): + if not is_callable(function): + return False + + self.function = ref(function) + return self._call_function(function) + + def _call_function(self, function): + self.actual = None + try: + self.actual_return_value = function() + except Exception: + self.actual = sys.exc_info()[1] + + if isinstance(self.actual, self.expected): + if self.pattern is not None: + return ( + re.search(self.pattern, str(self.actual)) is not None + and (self.matcher is None or self.matcher.matches(self.actual)) + ) + return self.matcher is None or self.matcher.matches(self.actual) + return False + + def describe_to(self, description): + description.append_text('Expected a callable raising %s' % self.expected) + if self.matcher is not None: + description.append_text("\n and ") + description.append_description_of(self.matcher) + + def describe_mismatch(self, item, description): + if not is_callable(item): + description.append_text('%s is not callable' % item) + return + + function = None if self.function is None else self.function() + if function is None or function is not item: + self.function = ref(item) + if not self._call_function(item): + return + + if self.actual is None: + description.append_text('No exception raised and actual return value = ') + description.append_value(self.actual_return_value) + elif isinstance(self.actual, self.expected): + if self.pattern is not None: + description.append_text('Correct assertion type raised, but the expected pattern ("%s") not found.' % self.pattern) + description.append_text('\n message was: "%s"' % str(self.actual)) + if self.matcher is not None: + description.append_text("\nAdditional exception matcher: ") + self.matcher.describe_mismatch(self.actual, description) + else: + description.append_text('%s was raised instead' % type(self.actual)) + + +def raises(exception, pattern=None, matcher=None): + """Matches if the called function raised the expected exception. + + :param exception: The class of the expected exception + :param pattern: Optional regular expression to match exception message. + + Expects the actual to be wrapped by using :py:func:`~hamcrest.core.core.raises.calling`, + or a callable taking no arguments. + Optional argument pattern should be a string containing a regular expression. If provided, + the string representation of the actual exception - e.g. `str(actual)` - must match pattern. + + Examples:: + + assert_that(calling(int).with_args('q'), raises(TypeError)) + assert_that(calling(parse, broken_input), raises(ValueError)) + """ + return Raises(exception, pattern, matcher) + + +class DeferredCallable(object): + def __init__(self, func): + self.func = func + self.args = tuple() + self.kwargs = {} + + def __call__(self): + return self.func(*self.args, **self.kwargs) + + def with_args(self, *args, **kwargs): + self.args = args + self.kwargs = kwargs + return self + + +def calling(func): + """Wrapper for function call that delays the actual execution so that + :py:func:`~hamcrest.core.core.raises.raises` matcher can catch any thrown exception. + + :param func: The function or method to be called + + The arguments can be provided with a call to the `with_args` function on the returned + object:: + + calling(my_method).with_args(arguments, and_='keywords') + """ + return DeferredCallable(func) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/description.py b/contrib/python/PyHamcrest/py3/hamcrest/core/description.py new file mode 100644 index 00000000000..6201b7f70f9 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/description.py @@ -0,0 +1,58 @@ +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + + +class Description(object): + """A description of a :py:class:`~hamcrest.core.matcher.Matcher`. + + A :py:class:`~hamcrest.core.matcher.Matcher` will describe itself to a + description which can later be used for reporting. + + """ + + def append_text(self, text): + """Appends some plain text to the description. + + :returns: ``self``, for chaining + + """ + raise NotImplementedError('append_text') + + def append_description_of(self, value): + """Appends description of given value to this description. + + If the value implements + :py:meth:`~hamcrest.core.selfdescribing.SelfDescribing.describe_to`, + then it will be used. + + :returns: ``self``, for chaining + + """ + raise NotImplementedError('append_description_of') + + def append_value(self, value): + """Appends an arbitary value to the description. + + **Deprecated:** Call + :py:meth:`~hamcrest.core.description.Description.append_description_of` + instead. + + :returns: ``self``, for chaining + + """ + raise NotImplementedError('append_value') + + def append_list(self, start, separator, end, list): + """Appends a list of objects to the description. + + :param start: String that will begin the list description. + :param separator: String that will separate each object in the + description. + :param end: String that will end the list description. + :param list: List of objects to be described. + + :returns: ``self``, for chaining + + """ + raise NotImplementedError('append_list') diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/helpers/__init__.py b/contrib/python/PyHamcrest/py3/hamcrest/core/helpers/__init__.py new file mode 100644 index 00000000000..61cb82d43b5 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/helpers/__init__.py @@ -0,0 +1,5 @@ +"""Utilities for writing Matchers.""" + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/helpers/hasmethod.py b/contrib/python/PyHamcrest/py3/hamcrest/core/helpers/hasmethod.py new file mode 100644 index 00000000000..a1f3bfb1549 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/helpers/hasmethod.py @@ -0,0 +1,12 @@ +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + + +def hasmethod(obj, methodname): + """Does ``obj`` have a method named ``methodname``?""" + + if not hasattr(obj, methodname): + return False + method = getattr(obj, methodname) + return callable(method) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/helpers/wrap_matcher.py b/contrib/python/PyHamcrest/py3/hamcrest/core/helpers/wrap_matcher.py new file mode 100644 index 00000000000..a5b506fb395 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/helpers/wrap_matcher.py @@ -0,0 +1,36 @@ +import six + +from hamcrest.core.base_matcher import Matcher +from hamcrest.core.core.isequal import equal_to + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +import types + +def wrap_matcher(x): + """Wraps argument in a matcher, if necessary. + + :returns: the argument as-is if it is already a matcher, otherwise wrapped + in an :py:func:`~hamcrest.core.core.isequal.equal_to` matcher. + + """ + if isinstance(x, Matcher): + return x + else: + return equal_to(x) + +def is_matchable_type(expected_type): + if isinstance(expected_type, type): + return True + + if isinstance(expected_type, six.class_types): + return True + + if isinstance(expected_type, tuple) and \ + expected_type and \ + all(map(is_matchable_type, expected_type)): + return True + + return False diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/matcher.py b/contrib/python/PyHamcrest/py3/hamcrest/core/matcher.py new file mode 100644 index 00000000000..81ee27c6d9d --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/matcher.py @@ -0,0 +1,52 @@ +from __future__ import absolute_import +from .selfdescribing import SelfDescribing + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + + +class Matcher(SelfDescribing): + """A matcher over acceptable values. + + A matcher is able to describe itself to give feedback when it fails. + + Matcher implementations should *not* directly implement this protocol. + Instead, *extend* the :py:class:`~hamcrest.core.base_matcher.BaseMatcher` + class, which will ensure that the + :py:class:`~hamcrest.core.matcher.Matcher` API can grow to support new + features and remain compatible with all + :py:class:`~hamcrest.core.matcher.Matcher` implementations. + + """ + + def matches(self, item, mismatch_description=None): + """Evaluates the matcher for argument item. + + If a mismatch is detected and argument ``mismatch_description`` is + provided, it will generate a description of why the matcher has not + accepted the item. + + :param item: The object against which the matcher is evaluated. + :returns: ``True`` if ``item`` matches, otherwise ``False``. + + """ + raise NotImplementedError('matches') + + def describe_mismatch(self, item, mismatch_description): + """Generates a description of why the matcher has not accepted the + item. + + The description will be part of a larger description of why a matching + failed, so it should be concise. + + This method assumes that ``matches(item)`` is ``False``, but will not + check this. + + :param item: The item that the + :py:class:`~hamcrest.core.matcher.Matcher` has rejected. + :param mismatch_description: The description to be built or appended + to. + + """ + raise NotImplementedError('describe_mismatch') diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/selfdescribing.py b/contrib/python/PyHamcrest/py3/hamcrest/core/selfdescribing.py new file mode 100644 index 00000000000..e77b0e0fdb6 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/selfdescribing.py @@ -0,0 +1,18 @@ +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + + +class SelfDescribing(object): + """The ability of an object to describe itself.""" + + def describe_to(self, description): + """Generates a description of the object. + + The description may be part of a description of a larger object of + which this is just a component, so it should be worded appropriately. + + :param description: The description to be built or appended to. + + """ + raise NotImplementedError('describe_to') diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/selfdescribingvalue.py b/contrib/python/PyHamcrest/py3/hamcrest/core/selfdescribingvalue.py new file mode 100644 index 00000000000..dfa4e3a20e4 --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/selfdescribingvalue.py @@ -0,0 +1,27 @@ +from hamcrest.core.selfdescribing import SelfDescribing + +import warnings + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + + +class SelfDescribingValue(SelfDescribing): + """Wrap any value in a ``SelfDescribingValue`` to satisfy the + :py:class:`~hamcrest.core.selfdescribing.SelfDescribing` interface. + + **Deprecated:** No need for this class now that + :py:meth:`~hamcrest.core.description.Description.append_description_of` + handles any type of value. + + """ + + def __init__(self, value): + warnings.warn('SelfDescribingValue no longer needed', + DeprecationWarning) + self.value = value + + def describe_to(self, description): + """Generates a description of the value.""" + description.append_value(self.value) diff --git a/contrib/python/PyHamcrest/py3/hamcrest/core/string_description.py b/contrib/python/PyHamcrest/py3/hamcrest/core/string_description.py new file mode 100644 index 00000000000..7626bf91e8e --- /dev/null +++ b/contrib/python/PyHamcrest/py3/hamcrest/core/string_description.py @@ -0,0 +1,37 @@ +from __future__ import absolute_import + +import codecs +import six + +from .base_description import BaseDescription + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + + +def tostring(selfdescribing): + """Returns the description of a + :py:class:`~hamcrest.core.selfdescribing.SelfDescribing` object as a + string. + + :param selfdescribing: The object to be described. + :returns: The description of the object. + """ + return str(StringDescription().append_description_of(selfdescribing)) + + +class StringDescription(BaseDescription): + """A :py:class:`~hamcrest.core.description.Description` that is stored as a + string. + """ + + def __init__(self): + self.__out_list = [] + + def __str__(self): + """Returns the description.""" + return ''.join(self.__out_list) + + def append(self, string): + self.__out_list.append(six.text_type(string)) |