Skip to content

Commit

Permalink
feat: allow extensions to override snapshot equality check
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Negin-Ulster authored and noahnu committed Oct 22, 2021
1 parent ddb6a5a commit f6c52f3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/syrupy/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ def _assert(self, data: "SerializableData") -> bool:
try:
snapshot_data = self._recall_data(index=self.num_executions)
serialized_data = self._serialize(data)
matches = snapshot_data is not None and serialized_data == snapshot_data
matches = snapshot_data is not None and self.extension.matches(
serialized_data=serialized_data, snapshot_data=snapshot_data
)
assertion_success = matches
if not matches and self._update_snapshots:
self.extension.write_snapshot(
Expand Down
18 changes: 17 additions & 1 deletion src/syrupy/extensions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,23 @@ def __strip_ends(self, line: str) -> str:
return line.rstrip("".join(self._ends.keys()))


class AbstractSyrupyExtension(SnapshotSerializer, SnapshotFossilizer, SnapshotReporter):
class SnapshotComparator(ABC):
def matches(
self,
*,
serialized_data: "SerializableData",
snapshot_data: "SerializableData",
) -> bool:
"""
Compares serialized data and snapshot data and returns
whether they match.
"""
return bool(serialized_data == snapshot_data)


class AbstractSyrupyExtension(
SnapshotSerializer, SnapshotFossilizer, SnapshotReporter, SnapshotComparator
):
def __init__(self, test_location: "PyTestLocation"):
self._test_location = test_location

Expand Down

0 comments on commit f6c52f3

Please sign in to comment.