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

87 lines
2.5 KiB
Python

import pytest
from pathlib import Path
from docx import Document
from src.core.style_registry import StyleRegistry
def test_apply_style_font_and_color():
yaml_content = """
styles:
TestStyle:
font:
name: "Arial"
size: 16
bold: true
italic: true
color: "FF0000" # красный
paragraph:
alignment: center
space_before: 10
space_after: 5
"""
yaml_path = Path("tests/fixtures/test_style.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)
# Проверим, что стиль создался
style = doc.styles["TestStyle"]
assert style.font.name == "Arial"
assert style.font.size.pt == 16
assert style.font.bold is True
assert style.font.italic is True
assert style.font.color.rgb == (0xFF, 0x00, 0x00)
# Проверим paragraph_format
pf = style.paragraph_format
from docx.enum.text import WD_ALIGN_PARAGRAPH
assert pf.alignment == WD_ALIGN_PARAGRAPH.CENTER
assert pf.space_before.pt == 10
assert pf.space_after.pt == 5
yaml_path.unlink(missing_ok=True)
def test_apply_style_with_defaults():
yaml_content = """
styles:
TestStyle:
font:
name: "Times New Roman"
size: 14
"""
yaml_path = Path("tests/fixtures/test_style_defaults.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)
style = doc.styles["TestStyle"]
assert style.font.name == "Times New Roman"
assert style.font.size.pt == 14
# Проверим, что остальные параметры не изменились (None означает "не задано")
assert style.font.bold is None
assert style.font.italic is None
yaml_path.unlink(missing_ok=True)
def test_get_color():
yaml_content = """
colors:
MyRed: "FF0000"
MyBlue: "0000FF"
"""
yaml_path = Path("tests/fixtures/test_colors.yaml")
yaml_path.parent.mkdir(exist_ok=True)
yaml_path.write_text(yaml_content, encoding="utf-8")
style_reg = StyleRegistry(yaml_path)
assert style_reg.get_color("MyRed") == (0xFF, 0x00, 0x00)
assert style_reg.get_color("MyBlue") == (0x00, 0x00, 0xFF)
# Проверим fallback
assert style_reg.get_color("NonExistent") == (0x00, 0x00, 0x00) # чёрный
yaml_path.unlink(missing_ok=True)