From daa003f73b03fe56e1f4922a51138e01e306b34d Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Sat, 19 Oct 2019 14:32:59 +0100 Subject: [PATCH] Remove redundant split diff code --- ward/diff.py | 41 ++--------------------------------------- ward/terminal.py | 4 ++-- 2 files changed, 4 insertions(+), 41 deletions(-) diff --git a/ward/diff.py b/ward/diff.py index 88c78d2b..e5a6132b 100644 --- a/ward/diff.py +++ b/ward/diff.py @@ -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: @@ -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}" diff --git a/ward/terminal.py b/ward/terminal.py index 6da8dc17..416aa6da 100644 --- a/ward/terminal.py +++ b/ward/terminal.py @@ -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 @@ -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__", "")