mockify.matchers - Classes for wildcarding expected arguments

Module with types representing matchers.

Matchers are used to wildcard some expected parameters when expectation is recorded. Matchers do that by overloading (in)equality operator in their specific way. With this you can record expectations using value ranges, type checking, regular expressions and more.

class mockify.matchers.Matcher

Bases: abc.ABC

Abstract base class for matchers.

Changed in version 0.6: Now this inherits from abc.ABC

__eq__(other)

Check if other can be accepted by this matcher.

__repr__()

Return textual representation of this matcher.

Returned string representation is later used in error reporting.

class mockify.matchers.AnyOf(*values)

Bases: mockify.matchers.Matcher

Matches any value from given list of values.

You can also use matchers in values.

New in version 0.6.

__eq__(other)

Check if other can be accepted by this matcher.

__repr__()

Return textual representation of this matcher.

Returned string representation is later used in error reporting.

class mockify.matchers.AllOf(*values)

Bases: mockify.matchers.Matcher

Matches if and only if received value is equal to all given values.

You can also use matchers in values.

New in version 0.6.

__eq__(other)

Check if other can be accepted by this matcher.

__repr__()

Return textual representation of this matcher.

Returned string representation is later used in error reporting.

class mockify.matchers.Any

Bases: mockify.matchers.Matcher

Matches any value.

This can be used as a wildcard, when you care about number of arguments in your expectation, not their values or types.

Note

This is also available as _ (underscore) member of mockify.matchers module:

from mockify.matchers import _, Any

assert isinstance(_, Any)
__eq__(other)

Check if other can be accepted by this matcher.

__repr__()

Return textual representation of this matcher.

Returned string representation is later used in error reporting.

class mockify.matchers.Type(*types)

Bases: mockify.matchers.Matcher

Matches any value that is instance of one of given types.

This is useful to record expectations where we do not care about expected value, but we do care about expected value type.

New in version 0.6.

__eq__(other)

Check if other can be accepted by this matcher.

__repr__()

Return textual representation of this matcher.

Returned string representation is later used in error reporting.

class mockify.matchers.Regex(pattern, name=None)

Bases: mockify.matchers.Matcher

Matches value if it is a string that matches given regular expression pattern.

Parameters:
  • pattern – Regular expression pattern
  • name

    Optional name for given pattern.

    If given, then name will be used in text representation of this matcher. This can be very handy, especially when regular expression is complex and hard to read. Example:

    >>> r = Regex(r'^[a-z]+$', 'LOWER_ASCII')
    >>> repr(r)
    'Regex(LOWER_ASCII)'
    

New in version 0.6.

__eq__(other)

Check if other can be accepted by this matcher.

__repr__()

Return textual representation of this matcher.

Returned string representation is later used in error reporting.

class mockify.matchers.List(matcher, min_length=None, max_length=None)

Bases: mockify.matchers.Matcher

Matches value if it is a list of values matching matcher.

Parameters:
  • matcher

    A matcher that every value in the list is expected to match.

    Use Any matcher if you want to match list containing any values.

  • min_length – Minimal accepted list length
  • max_length – Maximal accepted list length

New in version 0.6.

__eq__(other)

Check if other can be accepted by this matcher.

__repr__()

Return textual representation of this matcher.

Returned string representation is later used in error reporting.

class mockify.matchers.Object(**kwargs)

Bases: mockify.matchers.Matcher

Matches value if it is an object with attributes equal to names and values given via keyword args.

This matcher creates ad-hoc object using provided keyword args. These args are then used to compare with value’s attributes of same name. All attributes must match for this matcher to accept value.

Here’s an example:

from collections import namedtuple

from mockify.core import satisfied
from mockify.mock import Mock
from mockify.matchers import Object

CallArg = namedtuple('CallArg', 'foo, bar')

mock = Mock('mock')
mock.expect_call(Object(foo=1, bar=2))

with satisfied(mock):
    mock(CallArg(1, 2))

New in version 0.6.5.

Parameters:**kwargs – Arguments to compare value with
__eq__(other)

Check if other can be accepted by this matcher.

__repr__()

Return textual representation of this matcher.

Returned string representation is later used in error reporting.

class mockify.matchers.Func(func, name=None)

Bases: mockify.matchers.Matcher

Matches value if func returns True for that value.

This is the most generic matcher as you can use your own match function if needed.

Parameters:
  • func – Function to be used to calculate match.
  • name

    Optional name for this matcher.

    This can be used to set a name used to format matcher’s text representation for assertion errors. Here’s a simple example:

    >>> f = Func(lambda x: x > 0, 'POSITIVE_ONLY')
    >>> repr(f)
    'Func(POSITIVE_ONLY)'
    

New in version 0.6.

__eq__(other)

Check if other can be accepted by this matcher.

__repr__()

Return textual representation of this matcher.

Returned string representation is later used in error reporting.