Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Remove redundant split diff code
Browse files Browse the repository at this point in the history
  • Loading branch information
Darren Burns committed Oct 19, 2019
1 parent e7a9a4f commit daa003f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 41 deletions.
41 changes: 2 additions & 39 deletions ward/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from termcolor import colored


def build_auto_diff(lhs, rhs, width=60) -> str:
"""Determines the best type of diff to use based on the output"""
def make_diff(lhs, rhs, width=60) -> str:
"""Transform input into best format for diffing, then return output diff."""
if isinstance(lhs, str):
lhs_repr = lhs
else:
Expand All @@ -20,43 +20,6 @@ def build_auto_diff(lhs, rhs, width=60) -> str:
return build_unified_diff(lhs_repr, rhs_repr)


def build_split_diff(lhs_repr, rhs_repr) -> str:
lhs_out, rhs_out = "", ""

matcher = difflib.SequenceMatcher(None, lhs_repr, rhs_repr)
for op, i1, i2, j1, j2 in matcher.get_opcodes():

lhs_substring_lines = lhs_repr[i1:i2].splitlines()
rhs_substring_lines = rhs_repr[j1:j2].splitlines()

for i, lhs_substring in enumerate(lhs_substring_lines):
if op == "replace":
lhs_out += colored(lhs_substring, color="green")
elif op == "delete":
lhs_out += colored(lhs_substring, color="green")
elif op == "insert":
lhs_out += lhs_substring
elif op == "equal":
lhs_out += lhs_substring

if i != len(lhs_substring_lines) - 1:
lhs_out += f"\n"

for j, rhs_substring in enumerate(rhs_substring_lines):
if op == "replace":
rhs_out += colored(rhs_substring, color="red")
elif op == "insert":
rhs_out += colored(rhs_substring, color="red")
elif op == "equal":
rhs_out += rhs_substring

if j != len(rhs_substring_lines) - 1:
rhs_out += f"\n"

# TODO: Clean up the line below
return f"LHS: {lhs_out}\nRHS: {rhs_out}"


def bright_red(s: str) -> str:
return f"{Fore.LIGHTRED_EX}{s}{Style.RESET_ALL}"

Expand Down
4 changes: 2 additions & 2 deletions ward/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from colorama import Fore, Style
from termcolor import colored

from ward.diff import build_auto_diff
from ward.diff import make_diff
from ward.expect import ExpectationFailed
from ward.suite import Suite
from ward.test_result import TestOutcome, TestResult
Expand Down Expand Up @@ -149,7 +149,7 @@ def output_why_test_failed(self, test_result: TestResult):
f" vs {colored('actual value', color='red')}:\n"
)

diff = build_auto_diff(expect.that, expect.this, width=truncation_chars)
diff = make_diff(expect.that, expect.this, width=truncation_chars)
print(diff)
else:
trace = getattr(err, "__traceback__", "")
Expand Down

0 comments on commit daa003f

Please sign in to comment.