Yield the explanation for the diff between text. Unless --verbose is used this will skip leading and trailing characters which are identical to keep the diff minimal.
(
left: str, right: str, highlighter: _HighlightFunc, verbose: int = 0
)
| 39 | |
| 40 | |
| 41 | def _diff_text( |
| 42 | left: str, right: str, highlighter: _HighlightFunc, verbose: int = 0 |
| 43 | ) -> Iterator[str]: |
| 44 | class="st">"""Yield the explanation for the diff between text. |
| 45 | |
| 46 | Unless --verbose is used this will skip leading and trailing |
| 47 | characters which are identical to keep the diff minimal. |
| 48 | class="st">""" |
| 49 | from difflib import ndiff |
| 50 | |
| 51 | if verbose < 1: |
| 52 | i = 0 class="cm"># just in case left or right has zero length |
| 53 | for i in range(min(len(left), len(right))): |
| 54 | if left[i] != right[i]: |
| 55 | break |
| 56 | if i > 42: |
| 57 | i -= 10 class="cm"># Provide some context |
| 58 | yield fclass="st">"Skipping {i} identical leading characters in diff, use -v to show" |
| 59 | left = left[i:] |
| 60 | right = right[i:] |
| 61 | if len(left) == len(right): |
| 62 | for i in range(len(left)): |
| 63 | if left[-i] != right[-i]: |
| 64 | break |
| 65 | if i > 42: |
| 66 | i -= 10 class="cm"># Provide some context |
| 67 | yield ( |
| 68 | fclass="st">"Skipping {i} identical trailing " |
| 69 | class="st">"characters in diff, use -v to show" |
| 70 | ) |
| 71 | left = left[:-i] |
| 72 | right = right[:-i] |
| 73 | keepends = True |
| 74 | if left.isspace() or right.isspace(): |
| 75 | left = repr(str(left)) |
| 76 | right = repr(str(right)) |
| 77 | yield class="st">"Strings contain only whitespace, escaping them using repr()" |
| 78 | class="cm"># class="st">"right" is the expected base against which we compare class="st">"left", |
| 79 | class="cm"># see https://github.com/pytest-dev/pytest/issues/3333 |
| 80 | yield from highlighter( |
| 81 | class="st">"\n".join( |
| 82 | line.strip(class="st">"\n") |
| 83 | for line in ndiff(right.splitlines(keepends), left.splitlines(keepends)) |
| 84 | ), |
| 85 | lexer=class="st">"diff", |
| 86 | ).splitlines() |
| 87 | |
| 88 | |
| 89 | def _notin_text(term: str, text: str, verbose: int = 0) -> Iterator[str]: |
no test coverage detected