-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from MartinGotelli/matchers_as_plugin
feat: Create custom assert and messages to help to debug errors
- Loading branch information
Showing
92 changed files
with
996 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
[MESSAGES CONTROL] | ||
|
||
disable= | ||
missing-function-docstring, | ||
missing-module-docstring, | ||
|
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 +1 @@ | ||
echo "src/pytest_matchers/pytest_matchers src/pytest_matchers/tests" | ||
echo "src/pytest_matchers/pytest_matchers src/tests" |
File renamed without changes.
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,3 +1,4 @@ | ||
from .asserts.asserts import assert_match, assert_not_match | ||
from .main import ( | ||
anything, | ||
between, | ||
|
File renamed without changes.
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,25 @@ | ||
from typing import Any | ||
|
||
from pytest_matchers.asserts.comparer import Comparer | ||
|
||
|
||
def _match(actual: Any, expected: Any) -> bool: | ||
return Comparer().compare(actual, expected) | ||
|
||
|
||
def assert_match(actual: Any, expected: Any) -> None: | ||
try: | ||
assert _match(actual, expected) | ||
except AssertionError: | ||
assert actual == expected # Return the original assertion error | ||
|
||
|
||
def _not_match(actual: Any, expected: Any) -> bool: | ||
return not _match(actual, expected) | ||
|
||
|
||
def assert_not_match(actual: Any, expected: Any) -> None: | ||
try: | ||
assert _not_match(actual, expected) | ||
except AssertionError: | ||
assert actual != expected # Return the original assertion error |
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,22 @@ | ||
from typing import Any | ||
|
||
from pytest_matchers.asserts.comparers.dict import DictComparer | ||
from pytest_matchers.asserts.comparers.list import ListComparer | ||
from pytest_matchers.asserts.comparers.set import SetComparer | ||
from pytest_matchers.matchers import Matcher | ||
|
||
|
||
class Comparer: | ||
def compare(self, actual: Any, expected: Any, *, fail_fast: bool = True) -> bool: | ||
if isinstance(expected, Matcher) and not isinstance(actual, Matcher): | ||
return self.compare(expected, actual, fail_fast=fail_fast) # Reverse the order | ||
return self._compare_by_type(actual, expected, fail_fast=fail_fast) | ||
|
||
def _compare_by_type(self, actual: Any, expected: Any, fail_fast: bool = True) -> bool: | ||
if isinstance(expected, (list, tuple)): | ||
return ListComparer(actual, expected, fail_fast=fail_fast).compare() | ||
if isinstance(expected, dict): | ||
return DictComparer(actual, expected, fail_fast=fail_fast).compare() | ||
if isinstance(expected, set): | ||
return SetComparer(actual, expected, fail_fast=fail_fast).compare() | ||
return actual == expected |
File renamed without changes.
19 changes: 19 additions & 0 deletions
19
src/pytest_matchers/pytest_matchers/asserts/comparers/base.py
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,19 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Any | ||
|
||
|
||
class BaseComparer(ABC): | ||
def __init__(self, actual: Any, expected: Any, *, fail_fast: bool = True): | ||
self._actual = actual | ||
self._expected = expected | ||
self._fail_fast = fail_fast | ||
|
||
@abstractmethod | ||
def compare(self) -> bool: | ||
pass | ||
|
||
def _base_compare(self, actual: Any, expected: Any) -> bool: | ||
# pylint: disable=import-outside-toplevel | ||
from pytest_matchers.asserts.comparer import Comparer | ||
|
||
return Comparer().compare(actual, expected, fail_fast=self._fail_fast) |
25 changes: 25 additions & 0 deletions
25
src/pytest_matchers/pytest_matchers/asserts/comparers/dict.py
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,25 @@ | ||
from typing import Generator | ||
|
||
from pytest_matchers.asserts.comparers.base import BaseComparer | ||
|
||
|
||
class DictComparer(BaseComparer): | ||
def compare(self) -> bool: | ||
same_length = len(self._actual) == len(self._expected) | ||
if not same_length and self._fail_fast: | ||
return False | ||
|
||
return self._items_compare() and same_length | ||
|
||
def _items_compare(self) -> bool: | ||
compared_items = self._compared_items() | ||
if not self._fail_fast: | ||
compared_items = list(compared_items) | ||
return all(compared_items) | ||
|
||
def _compared_items(self) -> Generator: | ||
for key in self._expected: | ||
if key in self._actual: | ||
yield self._base_compare(self._actual[key], self._expected[key]) | ||
else: | ||
yield False |
19 changes: 19 additions & 0 deletions
19
src/pytest_matchers/pytest_matchers/asserts/comparers/list.py
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,19 @@ | ||
from pytest_matchers.asserts.comparers.base import BaseComparer | ||
|
||
|
||
class ListComparer(BaseComparer): | ||
def compare(self) -> bool: | ||
same_length = len(self._actual) == len(self._expected) | ||
if not same_length and self._fail_fast: | ||
return False | ||
|
||
return self._items_compare() and same_length | ||
|
||
def _items_compare(self) -> bool: | ||
compared_items = ( | ||
self._base_compare(actual_item, expected_item) | ||
for actual_item, expected_item in zip(self._actual, self._expected) | ||
) | ||
if not self._fail_fast: | ||
compared_items = list(compared_items) # Force full generation | ||
return all(compared_items) |
26 changes: 26 additions & 0 deletions
26
src/pytest_matchers/pytest_matchers/asserts/comparers/set.py
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,26 @@ | ||
from typing import Any, Set | ||
|
||
from pytest_matchers.asserts.comparers.base import BaseComparer | ||
|
||
|
||
class SetComparer(BaseComparer): | ||
def compare(self) -> bool: | ||
same_length = len(self._actual) == len(self._expected) | ||
if not same_length and self._fail_fast: | ||
return False | ||
|
||
return self._items_compare() and same_length | ||
|
||
def _items_compare(self) -> bool: | ||
expected = self._expected.copy() | ||
compared_items = (self._in(actual_value, expected) for actual_value in self._actual) | ||
if not self._fail_fast: | ||
compared_items = list(compared_items) | ||
return all(compared_items) and not expected | ||
|
||
def _in(self, actual_value: Any, expected: Set[Any]) -> bool: | ||
for expected_value in expected: | ||
if self._base_compare(actual_value, expected_value): | ||
expected.discard(expected_value) | ||
return True | ||
return False |
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
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
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
Oops, something went wrong.