def _compare_eq_iterable(left: Iterable[Any], right: Iterable[Any], verbose: int = 0) -> List[str]: if verbose <= 0 and not running_on_ci(): return ["Use -v to get more diff"] # dynamic import to speedup pytest import difflib left_formatting = pprint.pformat(left).splitlines() right_formatting = pprint.pformat(right).splitlines() # Re-format for different output lengths. lines_left = len(left_formatting) lines_right = len(right_formatting) if lines_left != lines_right: left_formatting = _pformat_dispatch(left).splitlines() right_formatting = _pformat_dispatch(right).splitlines() if lines_left > 1 or lines_right > 1: _surrounding_parens_on_own_lines(left_formatting) _surrounding_parens_on_own_lines(right_formatting) explanation = ["Full diff:"] # "right" is the expected base against which we compare "left", # see https://github.com/pytest-dev/pytest/issues/3333 explanation.extend( line.rstrip() for line in difflib.ndiff(right_formatting, left_formatting)) return explanation
def _compare_eq_iterable(left: Iterable[Any], right: Iterable[Any], verbose: int = 0) -> List[str]: if not verbose: return ["Use -v to get the full diff"] # dynamic import to speedup pytest import difflib left_formatting = pprint.pformat(left).splitlines() right_formatting = pprint.pformat(right).splitlines() # Re-format for different output lengths. lines_left = len(left_formatting) lines_right = len(right_formatting) if lines_left != lines_right: left_formatting = _pformat_dispatch(left).splitlines() right_formatting = _pformat_dispatch(right).splitlines() if lines_left > 1 or lines_right > 1: _surrounding_parens_on_own_lines(left_formatting) _surrounding_parens_on_own_lines(right_formatting) explanation = ["Full diff:"] explanation.extend( line.rstrip() for line in difflib.ndiff(left_formatting, right_formatting)) return explanation
def test_pformat_dispatch(): assert _pformat_dispatch("a") == "'a'" assert _pformat_dispatch("a" * 10, width=5) == "'aaaaaaaaaa'" assert _pformat_dispatch("foo bar", width=5) == "('foo '\n 'bar')"