Better error diff and colorize it
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import difflib
|
||||||
|
|
||||||
from hexbytes import HexBytes
|
from hexbytes import HexBytes
|
||||||
from pydantic import BaseModel, Field, validator
|
from pydantic import BaseModel, Field, validator
|
||||||
from typing import List, Dict, Optional
|
from typing import List, Dict, Optional
|
||||||
@@ -36,14 +38,35 @@ class ProtocolComponentExpectation(BaseModel):
|
|||||||
def convert_creation_tx_to_hexbytes(cls, v):
|
def convert_creation_tx_to_hexbytes(cls, v):
|
||||||
return HexBytes(v.lower())
|
return HexBytes(v.lower())
|
||||||
|
|
||||||
def compare(self, other: "ProtocolComponentExpectation") -> Optional[str]:
|
def compare(
|
||||||
"""Compares the current instance with another ProtocolComponent instance and returns a message with the differences or None if there are no differences."""
|
self, other: "ProtocolComponentExpectation", colorize_output: bool = True
|
||||||
|
) -> Optional[str]:
|
||||||
|
"""Compares the current instance with another ProtocolComponent instance and returns a message with the
|
||||||
|
differences or None if there are no differences."""
|
||||||
|
|
||||||
|
def colorize_diff(diff):
|
||||||
|
colored_diff = []
|
||||||
|
for line in diff:
|
||||||
|
if line.startswith("-"):
|
||||||
|
colored_diff.append(f"\033[91m{line}\033[0m") # Red
|
||||||
|
elif line.startswith("+"):
|
||||||
|
colored_diff.append(f"\033[92m{line}\033[0m") # Green
|
||||||
|
elif line.startswith("?"):
|
||||||
|
colored_diff.append(f"\033[93m{line}\033[0m") # Yellow
|
||||||
|
else:
|
||||||
|
colored_diff.append(line)
|
||||||
|
return "\n".join(colored_diff)
|
||||||
|
|
||||||
differences = []
|
differences = []
|
||||||
for field_name, field_value in self.__dict__.items():
|
for field_name, field_value in self.__dict__.items():
|
||||||
other_value = getattr(other, field_name, None)
|
other_value = getattr(other, field_name, None)
|
||||||
if field_value != other_value:
|
if field_value != other_value:
|
||||||
|
diff = list(difflib.ndiff([str(field_value)], [str(other_value)]))
|
||||||
|
highlighted_diff = (
|
||||||
|
colorize_diff(diff) if colorize_output else "\n".join(diff)
|
||||||
|
)
|
||||||
differences.append(
|
differences.append(
|
||||||
f"Field '{field_name}' mismatch: '{field_value}' != '{other_value}'"
|
f"Field '{field_name}' mismatch:\n{highlighted_diff}"
|
||||||
)
|
)
|
||||||
if not differences:
|
if not differences:
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user