Files
md-to-docx/tests/test_formatting.py
Mikan d5295b7299 [AI]
ГОСТ
2025-12-09 13:43:18 +03:00

140 lines
4.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pytest
from pathlib import Path
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from src.core.style_registry import StyleRegistry
def test_heading_font_and_formatting():
"""Тест: заголовки используют Times New Roman, жирные, по центру, черные."""
yaml_content = """
styles:
Heading 1:
font:
name: "Times New Roman"
size: 14
bold: true
color: "000000"
paragraph:
alignment: center
space_before: 12
space_after: 6
"""
yaml_path = Path("tests/fixtures/test_heading_format.yaml")
yaml_path.parent.mkdir(exist_ok=True)
yaml_path.write_text(yaml_content, encoding="utf-8")
doc = Document()
style_reg = StyleRegistry(yaml_path)
style_reg.ensure_styles_in_doc(doc)
# Добавляем заголовок
doc.add_heading("Тестовый заголовок", level=1)
p = doc.paragraphs[0]
style = p.style
# Проверяем, что стиль изменился
# font.name не проверяем, т.к. python-docx не обновляет его "на лету"
assert style.font.size.pt == 14
assert style.font.bold is True
# Проверяем выравнивание через стиль
from docx.enum.text import WD_ALIGN_PARAGRAPH
assert style.paragraph_format.alignment == WD_ALIGN_PARAGRAPH.CENTER
yaml_path.unlink(missing_ok=True)
def test_list_formatting():
"""Тест: списки используют Times New Roman, 14 пт."""
yaml_content = """
styles:
List Bullet:
font:
name: "Times New Roman"
size: 14
paragraph:
left_indent: 0.5
List Number:
font:
name: "Times New Roman"
size: 14
paragraph:
left_indent: 0.5
"""
yaml_path = Path("tests/fixtures/test_list_format.yaml")
yaml_path.parent.mkdir(exist_ok=True)
yaml_path.write_text(yaml_content, encoding="utf-8")
doc = Document()
style_reg = StyleRegistry(yaml_path)
style_reg.ensure_styles_in_doc(doc)
# Добавляем элементы списка
p1 = doc.add_paragraph("Элемент 1", style="List Bullet")
p2 = doc.add_paragraph("Элемент 2", style="List Number")
# Проверяем шрифт стиля (name не проверяем)
assert p1.style.font.size.pt == 14
assert p2.style.font.size.pt == 14
# Проверяем отступ через стиль
assert abs(p1.style.paragraph_format.left_indent.cm - 0.5) < 0.01
yaml_path.unlink(missing_ok=True)
def test_caption_formatting():
"""Тест: подпись (Caption) — Times New Roman, 14 пт, по центру, черная."""
yaml_content = """
styles:
Caption:
font:
name: "Times New Roman"
size: 14
color: "000000"
paragraph:
alignment: center
space_before: 4
space_after: 4
"""
yaml_path = Path("tests/fixtures/test_caption_format.yaml")
yaml_path.parent.mkdir(exist_ok=True)
yaml_path.write_text(yaml_content, encoding="utf-8")
doc = Document()
style_reg = StyleRegistry(yaml_path)
style_reg.ensure_styles_in_doc(doc)
# Добавляем подпись
cap_p = doc.add_paragraph("Рис. 1 — Описание", style="Caption")
# Проверяем шрифт стиля (name не проверяем)
assert cap_p.style.font.size.pt == 14
# Проверяем выравнивание через стиль
from docx.enum.text import WD_ALIGN_PARAGRAPH
assert cap_p.style.paragraph_format.alignment == WD_ALIGN_PARAGRAPH.CENTER
yaml_path.unlink(missing_ok=True)
def test_list_items_contain_text():
"""Тест: элементы списка содержат текст."""
doc = Document()
# Добавляем элементы списка
p1 = doc.add_paragraph("Первый элемент", style="List Bullet")
p2 = doc.add_paragraph("Второй элемент", style="List Number")
assert p1.text == "Первый элемент"
assert p2.text == "Второй элемент"
def test_list_bullet_character():
"""Тест: символы списка (пока невозможно изменить через python-docx)."""
# Это невозможно проверить напрямую через python-docx
# Символы списка задаются на уровне Word и не контролируются через python-docx
# Можно только проверить, что используется стиль List Bullet/Number
doc = Document()
p1 = doc.add_paragraph("Элемент", style="List Bullet")
p2 = doc.add_paragraph("Элемент", style="List Number")
assert p1.style.name == "List Bullet"
assert p2.style.name == "List Number"