45 lines
2.0 KiB
Python
45 lines
2.0 KiB
Python
from genesis.data_models import ChapterMarker, ParsedTemplate
|
|
from genesis.writer.models import ChapterSpec
|
|
from genesis.writer.template_mapper import map_template
|
|
|
|
|
|
def _parsed():
|
|
sections = [
|
|
ChapterMarker(type="heading", name="はじめに", level=1),
|
|
ChapterMarker(type="placeholder", name="section:introduction", level=0),
|
|
ChapterMarker(type="heading", name="DB 設計", level=1),
|
|
ChapterMarker(type="placeholder", name="section:db_design", level=0),
|
|
ChapterMarker(type="bookmark", name="bm1", level=0),
|
|
]
|
|
return ParsedTemplate(file_name="t.docx", sections=sections, placeholders={"section:introduction": "x"}, styles={})
|
|
|
|
|
|
def test_map_template_ordered_and_assigns_ids():
|
|
specs = map_template(_parsed())
|
|
assert [s.title for s in specs] == ["はじめに", "DB 設計"]
|
|
assert specs[0].chapter_id == "introduction"
|
|
assert specs[0].section_placeholder == "section:introduction"
|
|
assert specs[1].chapter_id == "db_design"
|
|
assert specs[1].section_placeholder == "section:db_design"
|
|
assert all(isinstance(s, ChapterSpec) for s in specs)
|
|
|
|
|
|
def test_map_template_falls_back_without_placeholder():
|
|
sections = [ChapterMarker(type="heading", name="附録", level=1)]
|
|
parsed = ParsedTemplate(file_name="t.docx", sections=sections, placeholders={}, styles={})
|
|
specs = map_template(parsed)
|
|
assert specs[0].chapter_id == "chapter_1"
|
|
assert specs[0].section_placeholder is None
|
|
|
|
|
|
def test_map_template_section_re_case_insensitive():
|
|
# 宽容:占位符键名大小写不敏感(解析端已归一,防御性双保险)
|
|
sections = [
|
|
ChapterMarker(type="heading", name="画面一覧", level=1),
|
|
ChapterMarker(type="placeholder", name="Section:2", level=0),
|
|
]
|
|
parsed = ParsedTemplate(file_name="t.docx", sections=sections, placeholders={}, styles={})
|
|
specs = map_template(parsed)
|
|
assert specs[0].chapter_id == "2"
|
|
assert specs[0].section_placeholder == "Section:2"
|