mockify.matchers - Classes for wildcarding expected arguments¶
-
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.
-
format_repr
(*args, **kwargs)¶ Return matcher’s textual representation.
Typical use case of this class is to override it in child class without parameters and then call super giving it args you want to include in repr. Like in this example:
def format_repr(self): return super().format_repr(self._first_arg, self._second_arg, kwd=self._kwd_arg)
-
__repr__
()¶ Return repr(self).
-
-
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.
-
format_repr
()¶ Return matcher’s textual representation.
Typical use case of this class is to override it in child class without parameters and then call super giving it args you want to include in repr. Like in this example:
def format_repr(self): return super().format_repr(self._first_arg, self._second_arg, kwd=self._kwd_arg)
-
-
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.
-
format_repr
()¶ Return matcher’s textual representation.
Typical use case of this class is to override it in child class without parameters and then call super giving it args you want to include in repr. Like in this example:
def format_repr(self): return super().format_repr(self._first_arg, self._second_arg, kwd=self._kwd_arg)
-
-
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. This can also be imported as underscore:
from mockify.matchers import _
-
__eq__
(other)¶ Check if other can be accepted by this matcher.
-
format_repr
()¶ Return matcher’s textual representation.
Typical use case of this class is to override it in child class without parameters and then call super giving it args you want to include in repr. Like in this example:
def format_repr(self): return super().format_repr(self._first_arg, self._second_arg, kwd=self._kwd_arg)
-
-
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.
-
format_repr
()¶ Return matcher’s textual representation.
Typical use case of this class is to override it in child class without parameters and then call super giving it args you want to include in repr. Like in this example:
def format_repr(self): return super().format_repr(self._first_arg, self._second_arg, kwd=self._kwd_arg)
-
-
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.
-
format_repr
()¶ Return matcher’s textual representation.
Typical use case of this class is to override it in child class without parameters and then call super giving it args you want to include in repr. Like in this example:
def format_repr(self): return super().format_repr(self._first_arg, self._second_arg, kwd=self._kwd_arg)
-
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.
-
format_repr
()¶ Return matcher’s textual representation.
Typical use case of this class is to override it in child class without parameters and then call super giving it args you want to include in repr. Like in this example:
def format_repr(self): return super().format_repr(self._first_arg, self._second_arg, kwd=self._kwd_arg)
- matcher –
-
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 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.
-
format_repr
()¶ Return matcher’s textual representation.
Typical use case of this class is to override it in child class without parameters and then call super giving it args you want to include in repr. Like in this example:
def format_repr(self): return super().format_repr(self._first_arg, self._second_arg, kwd=self._kwd_arg)
-
-
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.
-
format_repr
()¶ Return matcher’s textual representation.
Typical use case of this class is to override it in child class without parameters and then call super giving it args you want to include in repr. Like in this example:
def format_repr(self): return super().format_repr(self._first_arg, self._second_arg, kwd=self._kwd_arg)