-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(matchers): enable explicit regex based matching. fix URL matcher.…
… several refactors
- Loading branch information
Showing
8 changed files
with
240 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1 @@ | ||
from .base import BaseMatcher | ||
from .url import URLMatcher | ||
from .body import BodyMatcher | ||
from .query import QueryMatcher | ||
from .method import MethodMatcher | ||
from .headers import HeadersMatcher | ||
from .path import PathMatcher | ||
from .xml import XMLMatcher | ||
from .json import JSONMatcher | ||
from .json_schema import JSONSchemaMatcher | ||
|
||
# Explicit symbols to export | ||
__all__ = ( | ||
'matchers', 'init', | ||
'add', 'get', | ||
'BaseMatcher', | ||
'MethodMatcher', | ||
'URLMatcher', | ||
'HeadersMatcher', | ||
'QueryMatcher', | ||
'PathMatcher', | ||
'BodyMatcher', | ||
'XMLMatcher', | ||
'JSONMatcher', | ||
'JSONSchemaMatcher', | ||
'QueryMatcher', | ||
) | ||
|
||
# List of built-in matchers | ||
# This is intended to be mutable. | ||
matchers = [ | ||
MethodMatcher, | ||
URLMatcher, | ||
HeadersMatcher, | ||
QueryMatcher, | ||
PathMatcher, | ||
BodyMatcher, | ||
XMLMatcher, | ||
JSONMatcher, | ||
JSONSchemaMatcher, | ||
QueryMatcher, | ||
] | ||
|
||
|
||
def add(*matchers): | ||
""" | ||
Registers one or multiple matchers to be used by default from | ||
mocking engine. | ||
""" | ||
matchers.append(*matchers) | ||
|
||
|
||
def get(name): | ||
""" | ||
Returns a matcher instance by class or alias name. | ||
Arguments: | ||
name (str): matcher class name or alias. | ||
Returns: | ||
matcher: found matcher instance, otherwise ``None``. | ||
""" | ||
for matcher in matchers: | ||
if matcher.__name__ == name or getattr(matcher, 'name', None) == name: | ||
return matcher | ||
|
||
|
||
def init(name, *args): | ||
""" | ||
Initializes a matcher instance passing variadic arguments to | ||
its constructor. Acts as a delegator proxy. | ||
Arguments: | ||
name (str): matcher class name or alias to execute. | ||
*args (mixed): variadic argument | ||
Returns: | ||
matcher: matcher instance. | ||
Raises: | ||
ValueError: if matcher was not found. | ||
""" | ||
matcher = get(name) | ||
if not matcher: | ||
raise ValueError('Cannot find matcher: {}'.format(name)) | ||
return matcher(*args) | ||
from .api import * # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from .base import BaseMatcher | ||
from .url import URLMatcher | ||
from .body import BodyMatcher | ||
from .query import QueryMatcher | ||
from .method import MethodMatcher | ||
from .headers import HeadersMatcher | ||
from .path import PathMatcher | ||
from .xml import XMLMatcher | ||
from .json import JSONMatcher | ||
from .json_schema import JSONSchemaMatcher | ||
|
||
# Explicit symbols to export | ||
__all__ = ( | ||
'init', | ||
'add', | ||
'get', | ||
'matchers', | ||
'BaseMatcher', | ||
'MethodMatcher', | ||
'URLMatcher', | ||
'HeadersMatcher', | ||
'QueryMatcher', | ||
'PathMatcher', | ||
'BodyMatcher', | ||
'XMLMatcher', | ||
'JSONMatcher', | ||
'JSONSchemaMatcher', | ||
'QueryMatcher', | ||
) | ||
|
||
# List of built-in matchers | ||
# This is intended to be mutable. | ||
matchers = [ | ||
MethodMatcher, | ||
URLMatcher, | ||
HeadersMatcher, | ||
QueryMatcher, | ||
PathMatcher, | ||
BodyMatcher, | ||
XMLMatcher, | ||
JSONMatcher, | ||
JSONSchemaMatcher, | ||
QueryMatcher, | ||
] | ||
|
||
|
||
def add(*matcher): | ||
""" | ||
Registers one or multiple matchers to be used by default from | ||
mocking engine. | ||
Arguments: | ||
*matcher (list[pook.BaseMatcher]): variadic matchers to add. | ||
""" | ||
[matchers.append(m) for m in matcher] | ||
|
||
|
||
def get(name): | ||
""" | ||
Returns a matcher instance by class or alias name. | ||
Arguments: | ||
name (str): matcher class name or alias. | ||
Returns: | ||
matcher: found matcher instance, otherwise ``None``. | ||
""" | ||
for matcher in matchers: | ||
if matcher.__name__ == name or getattr(matcher, 'name', None) == name: | ||
return matcher | ||
|
||
|
||
def init(name, *args): | ||
""" | ||
Initializes a matcher instance passing variadic arguments to | ||
its constructor. Acts as a delegator proxy. | ||
Arguments: | ||
name (str): matcher class name or alias to execute. | ||
*args (mixed): variadic argument | ||
Returns: | ||
matcher: matcher instance. | ||
Raises: | ||
ValueError: if matcher was not found. | ||
""" | ||
matcher = get(name) | ||
if not matcher: | ||
raise ValueError('Cannot find matcher: {}'.format(name)) | ||
return matcher(*args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.