Files
md_to_gost/tests/test_word_fix.py
T
Igor20264 b38661f588
Python application / build (push) Has been cancelled
Update 0.4.0
- Add\Rework UI
- Add Split Table and Listing
- Add Support Customazeble schems
2026-09-04 22:28:39 +03:00

111 lines
3.6 KiB
Python

"""Tests for Word COM table/listing continuation post-process (pure + optional COM)."""
from __future__ import annotations
import os
import sys
import tempfile
import pytest
from md2gost.profiles import LISTING_CONTINUATION_MODES, TABLE_CONTINUATION_MODES
from md2gost.word_fix import (
continuation_label,
find_page_break_row,
fix_continuations,
parse_caption_text,
)
def test_word_mode_in_profiles():
assert "word" in TABLE_CONTINUATION_MODES
assert "word" in LISTING_CONTINUATION_MODES
@pytest.mark.parametrize(
"text, kind, number, cont",
[
("Таблица 2.1 — Название", "table", "2.1", False),
("Таблица 1", "table", "1", False),
("Продолжение Таблицы 2.1", "table", "2.1", True),
("Листинг 3 — Код", "listing", "3", False),
("Продолжение Листинга 3", "listing", "3", True),
("Таблицы 1.2", "table", "1.2", False),
],
)
def test_parse_caption_text(text, kind, number, cont):
info = parse_caption_text(text)
assert info is not None
assert info.kind == kind
assert info.number == number
assert info.is_continuation is cont
def test_parse_caption_rejects_noise():
assert parse_caption_text("") is None
assert parse_caption_text("Рисунок 1 — x") is None
assert parse_caption_text("просто текст") is None
def test_find_page_break_row():
assert find_page_break_row([]) is None
assert find_page_break_row([1]) is None
assert find_page_break_row([1, 1, 1]) is None
assert find_page_break_row([1, 1, 2, 2]) == 3
assert find_page_break_row([2, 2, 3]) == 3
assert find_page_break_row([1, 2]) == 2
def test_continuation_label():
assert continuation_label("table", "2.1") == "Продолжение Таблицы 2.1"
assert continuation_label("listing", "4") == "Продолжение Листинга 4"
def test_word_paged_modes_include_word():
from md2gost.renderable.table import _WORD_PAGED_MODES as t
from md2gost.renderable.listing import _WORD_PAGED_MODES as L
assert "word" in t
assert "word" in L
def test_fix_continuations_missing_file():
r = fix_continuations(os.path.join(tempfile.gettempdir(), "md2gost-no-such.docx"))
assert r.ok is False
assert "не найден" in r.message.lower() or "Файл" in r.message
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
@pytest.mark.skipif(os.environ.get("MD2GOST_TEST_WORD") != "1", reason="set MD2GOST_TEST_WORD=1 to run Word COM smoke")
def test_fix_continuations_com_smoke(tmp_path):
"""Build a tall table DOCX via python-docx, then run Word fix (opt-in)."""
try:
import win32com.client # noqa: F401
except ImportError:
pytest.skip("pywin32 not installed")
from docx import Document
from docx.shared import Pt
path = tmp_path / "tall_table.docx"
doc = Document()
p = doc.add_paragraph("Таблица 1 — Длинная")
try:
p.style = "Caption"
except KeyError:
pass
table = doc.add_table(rows=1, cols=2)
table.rows[0].cells[0].text = "A"
table.rows[0].cells[1].text = "B"
for i in range(80):
row = table.add_row()
row.cells[0].text = f"row {i}"
row.cells[1].text = "x" * 20
for cell in row.cells:
for para in cell.paragraphs:
para.paragraph_format.space_after = Pt(6)
doc.save(str(path))
result = fix_continuations(str(path), tables=True, listings=False)
assert result.ok, result.message
assert path.is_file()