1
0

feat: 为metadata、模板和幻灯片添加description字段支持

添加可选的description字段用于文档目的,不影响渲染输出。

主要更改:
- core/presentation.py: 添加metadata.description属性
- core/template.py: 添加template.description属性
- tests: 添加16个新测试用例验证description功能
- docs: 更新README.md和README_DEV.md文档
- specs: 新增page-description规范文件
This commit is contained in:
2026-03-04 13:22:33 +08:00
parent 5d60f3c2c2
commit 2fd8bc1b4a
12 changed files with 845 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ import pytest
from pathlib import Path
from loaders.yaml_loader import YAMLError
from core.template import Template
from core.presentation import Presentation
# ============= 模板初始化测试 =============
@@ -892,3 +893,165 @@ slides:
slide_data = pres.data['slides'][0]
rendered = pres.render_slide(slide_data)
assert len(rendered['elements']) == 1
# ============= Description 字段测试 =============
class TestTemplateDescription:
"""Template description 字段测试类"""
def test_template_with_description(self, temp_dir):
"""测试模板包含 description 字段时正确加载"""
template_content = """
description: "用于章节标题页的模板,包含主标题和副标题"
vars:
- name: title
required: true
- name: subtitle
required: false
default: ""
elements:
- type: text
content: "{title}"
box: [1, 1, 8, 1]
font:
size: 44
bold: true
- type: text
content: "{subtitle}"
box: [1, 2, 8, 1]
font:
size: 24
"""
template_path = temp_dir / "test-template.yaml"
template_path.write_text(template_content)
template = Template("test-template", templates_dir=temp_dir)
assert template.description == "用于章节标题页的模板,包含主标题和副标题"
def test_template_without_description(self, sample_template):
"""测试模板不包含 description 字段时正常工作"""
template = Template("title-slide", templates_dir=sample_template)
assert template.description is None
def test_template_description_empty_string(self, temp_dir):
"""测试模板 description 为空字符串时正常工作"""
template_content = """
description: ""
vars:
- name: title
elements:
- type: text
content: "{title}"
box: [1, 1, 8, 1]
font: {}
"""
template_path = temp_dir / "test-template.yaml"
template_path.write_text(template_content)
template = Template("test-template", templates_dir=temp_dir)
assert template.description == ""
def test_template_description_chinese_characters(self, temp_dir):
"""测试模板 description 包含中文字符时正确处理"""
template_content = """
description: "这是中文模板描述,用于标题页面"
vars:
- name: title
elements:
- type: text
content: "{title}"
box: [1, 1, 8, 1]
font: {}
"""
template_path = temp_dir / "test-template.yaml"
template_path.write_text(template_content)
template = Template("test-template", templates_dir=temp_dir)
assert "这是中文模板描述" in template.description
assert "标题页面" in template.description
def test_template_description_multiline(self, temp_dir):
"""测试模板 description 支持多行文本"""
template_content = """
description: |
这是一个多行描述。
第一行说明模板的用途。
第二行说明使用场景。
vars:
- name: title
elements:
- type: text
content: "{title}"
box: [1, 1, 8, 1]
font: {}
"""
template_path = temp_dir / "test-template.yaml"
template_path.write_text(template_content)
template = Template("test-template", templates_dir=temp_dir)
# 多行文本应该被正确读取
assert "这是一个多行描述" in template.description
assert "第一行说明模板的用途" in template.description
assert "第二行说明使用场景" in template.description
def test_inline_template_with_description(self, temp_dir):
"""测试内联模板包含 description 字段"""
yaml_content = """
metadata:
size: "16:9"
templates:
test-template:
description: "内联模板描述"
vars:
- name: title
elements:
- type: text
content: "{title}"
box: [1, 1, 8, 1]
font: {}
slides:
- template: test-template
vars:
title: "Test"
"""
yaml_path = temp_dir / "test.yaml"
yaml_path.write_text(yaml_content)
pres = Presentation(str(yaml_path))
template = pres.get_template("test-template")
assert template.description == "内联模板描述"
def test_template_description_does_not_affect_rendering(self, temp_dir):
"""测试 description 不影响模板渲染"""
template_content = """
description: "这段描述不应该影响渲染"
vars:
- name: title
elements:
- type: text
content: "{title}"
box: [1, 1, 8, 1]
font:
size: 44
"""
template_path = temp_dir / "test-template.yaml"
template_path.write_text(template_content)
template = Template("test-template", templates_dir=temp_dir)
# 渲染应该正常工作description 不影响结果
result = template.render({"title": "Test Title"})
assert len(result) == 1
assert result[0]["content"] == "Test Title"