|
1 | 1 | import io |
2 | 2 | import re |
3 | 3 | import zipfile |
| 4 | +import xml.etree.ElementTree as ET |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 |
|
7 | 8 | from python_redlines.engines import DocxodusEngine |
8 | 9 |
|
9 | | -# Measured against the Docxodus v12.1.0 binary on tests/fixtures/. Docxodus |
| 10 | +# Measured against the Docxodus v14.1.0 binary on tests/fixtures/. Docxodus |
10 | 11 | # v11.0.0 removed WmlComparer, so DocxDiff is the only algorithm; the count is |
11 | 12 | # neither the 9 the old default reported nor the 11 the old opt-in reported. |
12 | 13 | EXPECTED_REVISIONS = 10 |
@@ -71,6 +72,37 @@ def test_docxodus_output_is_a_valid_docx_with_tracked_changes(original_docx, mod |
71 | 72 | assert "<w:del " in document_xml |
72 | 73 |
|
73 | 74 |
|
| 75 | +def test_docxodus_marks_a_single_changed_cjk_character(original_docx): |
| 76 | + """Guard the CJK granularity fix needed by users upgrading from 0.3.0 (#34).""" |
| 77 | + namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" |
| 78 | + ns = {"w": namespace} |
| 79 | + |
| 80 | + def make_document(text): |
| 81 | + output = io.BytesIO() |
| 82 | + with zipfile.ZipFile(io.BytesIO(original_docx)) as template: |
| 83 | + document = ET.Element(f"{{{namespace}}}document") |
| 84 | + body = ET.SubElement(document, f"{{{namespace}}}body") |
| 85 | + paragraph = ET.SubElement(body, f"{{{namespace}}}p") |
| 86 | + run = ET.SubElement(paragraph, f"{{{namespace}}}r") |
| 87 | + ET.SubElement(run, f"{{{namespace}}}t").text = text |
| 88 | + with zipfile.ZipFile(output, "w") as archive: |
| 89 | + for member in template.infolist(): |
| 90 | + data = (ET.tostring(document, encoding="utf-8", xml_declaration=True) |
| 91 | + if member.filename == "word/document.xml" |
| 92 | + else template.read(member.filename)) |
| 93 | + archive.writestr(member, data) |
| 94 | + return output.getvalue() |
| 95 | + |
| 96 | + before = make_document("本合同的交付日期为三月十五日,双方应按约履行。") |
| 97 | + after = make_document("本合同的交付日期为一月十五日,双方应按约履行。") |
| 98 | + redline, _, stderr = DocxodusEngine().run_redline("Reviewer", before, after) |
| 99 | + assert stderr is None |
| 100 | + with zipfile.ZipFile(io.BytesIO(redline)) as archive: |
| 101 | + document = ET.fromstring(archive.read("word/document.xml")) |
| 102 | + assert "".join(node.text or "" for node in document.findall(".//w:delText", ns)) == "三" |
| 103 | + assert "".join(node.text or "" for node in document.findall(".//w:ins//w:t", ns)) == "一" |
| 104 | + |
| 105 | + |
74 | 106 | # --- Integration tests for comparison settings --- |
75 | 107 |
|
76 | 108 | def test_docxodus_with_detect_moves(original_docx, modified_docx): |
|
0 commit comments