Skip to content

Commit

Permalink
Reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart Regebro committed Jan 4, 2024
1 parent 9bdbac8 commit 204c1ba
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
1 change: 0 additions & 1 deletion tests/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,6 @@ def test_with_xmlid(self):
)

def test_change_attribs(self):

left = """<document>
<story firstPageTemplate="FirstPage">
<section xml:id="oldfirst" ref="3" single-ref="3">
Expand Down
14 changes: 6 additions & 8 deletions tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ def test_replace_text_in(self):
left = "<document><node>This is a bit of text, right" + END
action = actions.UpdateTextIn("/document/node", "Also a bit of text, rick")
expected = (
START + "><diff:replace old-text=\"This is\">Also</diff:replace>"
" a bit of text, ri<diff:replace old-text=\"ght\">ck"
START + '><diff:replace old-text="This is">Also</diff:replace>'
' a bit of text, ri<diff:replace old-text="ght">ck'
"</diff:replace>" + END
)

Expand All @@ -345,8 +345,8 @@ def test_replace_text_after_2(self):
left = "<document><node/>This is a bit of text, right</document>"
action = actions.UpdateTextAfter("/document/node", "Also a bit of text, rick")
expected = (
START + "/><diff:replace old-text=\"This is\">Also</diff:replace>"
" a bit of text, ri<diff:replace old-text=\"ght\">ck"
START + '/><diff:replace old-text="This is">Also</diff:replace>'
' a bit of text, ri<diff:replace old-text="ght">ck'
"</diff:replace></document>"
)

Expand All @@ -356,12 +356,13 @@ def test_replace_complete_text(self):
left = "<document><node>aaaaaaa bbbbbb</node></document>"
action = actions.UpdateTextIn("/document/node", "ccccc dddd eee")
expected = (
START + "><diff:replace old-text=\"aaaaaaa bbbbbb\">ccccc dddd eee"
START + '><diff:replace old-text="aaaaaaa bbbbbb">ccccc dddd eee'
"</diff:replace>" + END
)

self._format_test(left, action, expected, use_replace=True)


class DiffFormatTests(unittest.TestCase):
def _format_test(self, action, expected):
formatter = formatting.DiffFormatter()
Expand Down Expand Up @@ -559,7 +560,6 @@ def test_all_actions(self):


class FormatterFileTests(unittest.TestCase):

formatter = None # Override this
maxDiff = None

Expand All @@ -568,7 +568,6 @@ def process(self, left, right):


class XMLFormatterFileTests(FormatterFileTests):

# The XMLFormatter has no text or formatting tags, so
formatter = formatting.XMLFormatter(
pretty_print=False, normalize=formatting.WS_TEXT
Expand All @@ -579,7 +578,6 @@ class XMLFormatterFileTests(FormatterFileTests):


class HTMLFormatterFileTests(FormatterFileTests):

# We use a few tags for the placeholder tests.
# <br/> is intentionally left out, to test an edge case
# with empty non-formatting tags in text.
Expand Down
1 change: 0 additions & 1 deletion tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@


class PatcherTests(unittest.TestCase):

patcher = Patcher()

def _test(self, start, action, end):
Expand Down
1 change: 0 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def _diff(self, left, right, result):
self.assertEqual("".join(res), result)

def test_lcs(self):

self._diff("ABCDEF", "ABCDEF", "ABCDEF")

self._diff("ABCDEF", "GHIJKL", "")
Expand Down
14 changes: 7 additions & 7 deletions xmldiff/diff_match_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ def diff_prettyHtml(self, diffs):
HTML representation.
"""
html = []
for (op, data) in diffs:
for op, data in diffs:
text = (
data.replace("&", "&amp;")
.replace("<", "&lt;")
Expand All @@ -1161,7 +1161,7 @@ def diff_text1(self, diffs):
Source text.
"""
text = []
for (op, data) in diffs:
for op, data in diffs:
if op != self.DIFF_INSERT:
text.append(data)
return "".join(text)
Expand All @@ -1176,7 +1176,7 @@ def diff_text2(self, diffs):
Destination text.
"""
text = []
for (op, data) in diffs:
for op, data in diffs:
if op != self.DIFF_DELETE:
text.append(data)
return "".join(text)
Expand All @@ -1194,7 +1194,7 @@ def diff_levenshtein(self, diffs):
levenshtein = 0
insertions = 0
deletions = 0
for (op, data) in diffs:
for op, data in diffs:
if op == self.DIFF_INSERT:
insertions += len(data)
elif op == self.DIFF_DELETE:
Expand All @@ -1220,7 +1220,7 @@ def diff_toDelta(self, diffs):
Delta text.
"""
text = []
for (op, data) in diffs:
for op, data in diffs:
if op == self.DIFF_INSERT:
# High ascii will raise UnicodeDecodeError. Use Unicode instead.
data = data.encode("utf-8")
Expand Down Expand Up @@ -1708,7 +1708,7 @@ def patch_apply(self, patches, text):
else:
self.diff_cleanupSemanticLossless(diffs)
index1 = 0
for (op, data) in patch.diffs:
for op, data in patch.diffs:
if op != self.DIFF_EQUAL:
index2 = self.diff_xIndex(diffs, index1)
if op == self.DIFF_INSERT: # Insertion
Expand Down Expand Up @@ -2007,7 +2007,7 @@ def __str__(self):
coords2 = str(self.start2 + 1) + "," + str(self.length2)
text = ["@@ -", coords1, " +", coords2, " @@\n"]
# Escape the body of the patch with %xx notation.
for (op, data) in self.diffs:
for op, data in self.diffs:
if op == diff_match_patch.DIFF_INSERT:
text.append("+")
elif op == diff_match_patch.DIFF_DELETE:
Expand Down
25 changes: 18 additions & 7 deletions xmldiff/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,12 @@ class XMLFormatter(BaseFormatter):
"""

def __init__(
self, normalize=WS_NONE, pretty_print=True, text_tags=(), formatting_tags=(), use_replace=False
self,
normalize=WS_NONE,
pretty_print=True,
text_tags=(),
formatting_tags=(),
use_replace=False,
):
# Mapping from placeholders -> structural content and vice versa.
self.normalize = normalize
Expand Down Expand Up @@ -593,20 +598,26 @@ def _stack_pop():
def _join_delete_insert(self, diffs):
new_diffs = []
skip_next = False
for i in range(len(diffs)-1):
for i in range(len(diffs) - 1):
if skip_next:
skip_next = False
continue
op, text = diffs[i]
next_op, next_text = diffs[i+1]
next_op, next_text = diffs[i + 1]
# insert, then delete
if op==diff_match_patch.DIFF_INSERT and next_op==diff_match_patch.DIFF_DELETE:
if (
op == diff_match_patch.DIFF_INSERT
and next_op == diff_match_patch.DIFF_DELETE
):
new_diffs.append((diff_match_patch.DIFF_REPLACE, text, next_text))
skip_next = True # also skip upcoming delete
skip_next = True # also skip upcoming delete
# delete, then insert
elif next_op==diff_match_patch.DIFF_INSERT and op==diff_match_patch.DIFF_DELETE:
elif (
next_op == diff_match_patch.DIFF_INSERT
and op == diff_match_patch.DIFF_DELETE
):
new_diffs.append((diff_match_patch.DIFF_REPLACE, next_text, text))
skip_next = True # also skip upcoming insert
skip_next = True # also skip upcoming insert
else:
new_diffs.append(diffs[i])
# append last diff, if it shouldn't be skipped
Expand Down
1 change: 0 additions & 1 deletion xmldiff/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def breadth_first_traverse(node):
# It also skips any items that are equal in the beginning and end, speeding
# up the search, and using even less memory.
def longest_common_subsequence(left_sequence, right_sequence, eqfn=eq):

start = 0
lend = lslen = len(left_sequence)
rend = rslen = len(right_sequence)
Expand Down

0 comments on commit 204c1ba

Please sign in to comment.