Exemple #1
0
def _describe_failure_of(probe):
    description = StringDescription()
    description.append_text('\nTried to look for...\n    ')
    probe.describe_to(description)
    description.append_text('\nbut...\n    ')
    probe.describe_failure_to(description)
    return str(description)
Exemple #2
0
def _assert_match(actual, matcher, reason):
    if not matcher.matches(actual):
        description = StringDescription()
        # print
        # print reason
        # print "-1 <%s>" % description.append_text(reason)
        # print matcher
        # print matcher.__class__
        # print dir(matcher)
        # print "-2 <%s>" % description.append_description_of(matcher)
        # print "-3 <%s>" % matcher.describe_mismatch(actual, description)
        # print

        # description.append_text(reason)     \
        #     .append_text('\n  Expected: ') \
        #     .append_description_of(matcher) \
        #     .append_text('\n  But: ')

        reason = ' (reason: {}) '.format(reason) if reason else ''

        description.append_text(reason)     \
            .append_text('expected ') \
            .append_description_of(matcher) \
            .append_text(', but ')

        matcher.describe_mismatch(actual, description)
        raise AssertionError(str(description))
Exemple #3
0
def _assert_match(actual: T, matcher: Matcher[T], reason: str) -> None:
    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)
Exemple #4
0
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)
Exemple #5
0
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)
Exemple #6
0
    def _matches(self, item):
        if not (isinstance(item, list) or isinstance(item, tuple)):
            self.messages.append(
                f'Can\'t perform ListSorted matcher on {type(item)} object.')

        pairs = [(item[i], item[i + 1]) for i in range(len(item) - 1)]
        for i, (left, right) in enumerate(pairs):
            matcher = self.pair_matcher(self.criteria(right))
            if not matcher.matches(self.criteria(left)):
                description = StringDescription()
                matcher.describe_to(description)
                description.append_text(' expected, but ')
                matcher.describe_mismatch(self.criteria(left), description)
                description.append(f'. items indexes are {i}, and {i + 1}')
                self.messages.append(str(description))
                return False
        return True
Exemple #7
0
def has_properties(*keys_valuematchers, **kv_args):
    """Matches if an object has properties satisfying all of a dictionary
    of string property names and corresponding value matchers.

    :param matcher_dict: A dictionary mapping keys to associated value matchers,
        or to expected values for
        :py:func:`~hamcrest.core.core.isequal.equal_to` matching.

    Note that the keys must be actual keys, not matchers. Any value argument
    that is not a matcher is implicitly wrapped in an
    :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
    equality.

    Examples::

        has_properties({'foo':equal_to(1), 'bar':equal_to(2)})
        has_properties({'foo':1, 'bar':2})

    ``has_properties`` also accepts a list of keyword arguments:

    .. function:: has_properties(keyword1=value_matcher1[, keyword2=value_matcher2[, ...]])

    :param keyword1: A keyword to look up.
    :param valueMatcher1: The matcher to satisfy for the value, or an expected
        value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching.

    Examples::

        has_properties(foo=equal_to(1), bar=equal_to(2))
        has_properties(foo=1, bar=2)

    Finally, ``has_properties`` also accepts a list of alternating keys and their
    value matchers:

    .. function:: has_properties(key1, value_matcher1[, ...])

    :param key1: A key (not a matcher) to look up.
    :param valueMatcher1: The matcher to satisfy for the value, or an expected
        value for :py:func:`~hamcrest.core.core.isequal.equal_to` matching.

    Examples::

        has_properties('foo', equal_to(1), 'bar', equal_to(2))
        has_properties('foo', 1, 'bar', 2)

    """
    if len(keys_valuematchers) == 1:
        try:
            base_dict = keys_valuematchers[0].copy()
            for key in base_dict:
                base_dict[key] = wrap_shortcut(base_dict[key])
        except AttributeError:
            raise ValueError('single-argument calls to has_properties must pass a dict as the argument')
    else:
        if len(keys_valuematchers) % 2:
            raise ValueError('has_properties requires key-value pairs')
        base_dict = {}
        for index in range(int(len(keys_valuematchers) / 2)):
            base_dict[keys_valuematchers[2 * index]] = wrap_shortcut(keys_valuematchers[2 * index + 1])

    for key, value in kv_args.items():
        base_dict[key] = wrap_shortcut(value)

    if len(base_dict) > 1:
        description = StringDescription().append_text('an object with properties ')
        for i, (property_name, property_value_matcher) in enumerate(sorted(base_dict.items())):
            description.append_value(property_name).append_text(' matching ').append_description_of(
                property_value_matcher)
            if i < len(base_dict) - 1:
                description.append_text(' and ')

        return described_as(str(description),
                            AllOf(*[has_property(property_name, property_value_matcher)
                                    for property_name, property_value_matcher
                                    in sorted(base_dict.items())],
                                  describe_all_mismatches=True,
                                  describe_matcher_in_mismatch=False))
    else:
        property_name, property_value_matcher = base_dict.popitem()
        return has_property(property_name, property_value_matcher)