添加页面级 enabled 布尔参数,用于临时禁用整个幻灯片,无需删除或注释 YAML 内容。
与元素级 visible 条件渲染独立工作,提供两层控制机制。
主要变更:
- 在 yaml2pptx.py 主循环中检查 enabled 参数,跳过禁用的幻灯片
- 实现独立的 slide_index 计数器,准确统计实际渲染的幻灯片数量
- 在 yaml_loader.py 中添加 enabled 字段验证,确保类型为布尔值
- 添加 11 个测试用例,覆盖各种使用场景
- 更新 README.md 和 README_DEV.md 文档,说明 enabled 和 visible 的区别
- 创建新的 slide-enabled-control capability 规范
- 更新 template-system 规范,添加 enabled 字段支持
使用示例:
slides:
- template: title-slide
vars:
title: "正常页面"
- enabled: false
template: work-in-progress
vars:
title: "临时禁用的页面"
测试:所有 282 个单元测试通过
222 lines
7.1 KiB
Python
222 lines
7.1 KiB
Python
"""
|
|
YAML 加载器单元测试
|
|
|
|
测试 load_yaml_file、validate_presentation_yaml 和 validate_template_yaml 函数
|
|
"""
|
|
|
|
import pytest
|
|
from pathlib import Path
|
|
from loaders.yaml_loader import (
|
|
load_yaml_file, validate_presentation_yaml,
|
|
validate_template_yaml, YAMLError
|
|
)
|
|
|
|
|
|
class TestLoadYamlFile:
|
|
"""load_yaml_file 函数测试类"""
|
|
|
|
def test_load_valid_yaml(self, sample_yaml):
|
|
"""测试加载有效的 YAML 文件"""
|
|
data = load_yaml_file(sample_yaml)
|
|
assert isinstance(data, dict)
|
|
assert "slides" in data
|
|
|
|
def test_load_nonexistent_file(self, temp_dir):
|
|
"""测试加载不存在的文件"""
|
|
nonexistent = temp_dir / "nonexistent.yaml"
|
|
with pytest.raises(YAMLError, match="文件不存在"):
|
|
load_yaml_file(nonexistent)
|
|
|
|
def test_load_directory_raises_error(self, temp_dir):
|
|
"""测试加载目录会引发错误"""
|
|
with pytest.raises(YAMLError, match="不是有效的文件"):
|
|
load_yaml_file(temp_dir)
|
|
|
|
def test_load_yaml_with_syntax_error(self, temp_dir):
|
|
"""测试加载包含语法错误的 YAML"""
|
|
invalid_file = temp_dir / "invalid.yaml"
|
|
invalid_file.write_text("key: [unclosed list")
|
|
with pytest.raises(YAMLError, match="YAML 语法错误"):
|
|
load_yaml_file(invalid_file)
|
|
|
|
def test_load_utf8_content(self, temp_dir):
|
|
"""测试加载 UTF-8 编码的内容"""
|
|
utf8_file = temp_dir / "utf8.yaml"
|
|
utf8_file.write_text("key: '中文内容'", encoding='utf-8')
|
|
data = load_yaml_file(utf8_file)
|
|
assert data["key"] == "中文内容"
|
|
|
|
|
|
class TestValidatePresentationYaml:
|
|
"""validate_presentation_yaml 函数测试类"""
|
|
|
|
def test_valid_presentation_structure(self):
|
|
"""测试有效的演示文稿结构"""
|
|
data = {
|
|
"metadata": {"size": "16:9"},
|
|
"slides": [{"elements": []}]
|
|
}
|
|
# 不应该引发错误
|
|
validate_presentation_yaml(data)
|
|
|
|
def test_missing_slides_field(self):
|
|
"""测试缺少 slides 字段"""
|
|
data = {"metadata": {"size": "16:9"}}
|
|
with pytest.raises(YAMLError, match="缺少必需字段 'slides'"):
|
|
validate_presentation_yaml(data)
|
|
|
|
def test_slides_not_a_list(self):
|
|
"""测试 slides 不是列表"""
|
|
data = {"slides": "not a list"}
|
|
with pytest.raises(YAMLError, match="'slides' 必须是一个列表"):
|
|
validate_presentation_yaml(data)
|
|
|
|
def test_not_a_dict(self):
|
|
"""测试不是字典"""
|
|
data = "not a dict"
|
|
with pytest.raises(YAMLError, match="必须是一个字典对象"):
|
|
validate_presentation_yaml(data)
|
|
|
|
def test_empty_slides_list(self):
|
|
"""测试空的 slides 列表"""
|
|
data = {"slides": []}
|
|
# 空列表是有效的
|
|
validate_presentation_yaml(data)
|
|
|
|
def test_with_file_path_in_error(self):
|
|
"""测试错误消息包含文件路径"""
|
|
data = {"not_slides": []}
|
|
with pytest.raises(YAMLError) as exc_info:
|
|
validate_presentation_yaml(data, "test.yaml")
|
|
assert "test.yaml" in str(exc_info.value)
|
|
|
|
def test_slide_enabled_true(self):
|
|
"""测试 enabled=true 的幻灯片"""
|
|
data = {
|
|
"slides": [
|
|
{"enabled": True, "elements": []}
|
|
]
|
|
}
|
|
# 不应该引发错误
|
|
validate_presentation_yaml(data)
|
|
|
|
def test_slide_enabled_false(self):
|
|
"""测试 enabled=false 的幻灯片"""
|
|
data = {
|
|
"slides": [
|
|
{"enabled": False, "elements": []}
|
|
]
|
|
}
|
|
# 不应该引发错误
|
|
validate_presentation_yaml(data)
|
|
|
|
def test_slide_enabled_invalid_string(self):
|
|
"""测试 enabled 为字符串时拒绝"""
|
|
data = {
|
|
"slides": [
|
|
{"enabled": "false", "elements": []}
|
|
]
|
|
}
|
|
with pytest.raises(YAMLError, match="enabled 必须是布尔值"):
|
|
validate_presentation_yaml(data)
|
|
|
|
def test_slide_enabled_invalid_number(self):
|
|
"""测试 enabled 为数字时拒绝"""
|
|
data = {
|
|
"slides": [
|
|
{"enabled": 0, "elements": []}
|
|
]
|
|
}
|
|
with pytest.raises(YAMLError, match="enabled 必须是布尔值"):
|
|
validate_presentation_yaml(data)
|
|
|
|
def test_slide_enabled_invalid_expression(self):
|
|
"""测试 enabled 为条件表达式时拒绝"""
|
|
data = {
|
|
"slides": [
|
|
{"enabled": "{some_var}", "elements": []}
|
|
]
|
|
}
|
|
with pytest.raises(YAMLError, match="enabled 必须是布尔值"):
|
|
validate_presentation_yaml(data)
|
|
|
|
|
|
class TestValidateTemplateYaml:
|
|
"""validate_template_yaml 函数测试类"""
|
|
|
|
def test_valid_template_structure(self):
|
|
"""测试有效的模板结构"""
|
|
data = {
|
|
"vars": [
|
|
{"name": "title", "required": True}
|
|
],
|
|
"elements": []
|
|
}
|
|
# 不应该引发错误
|
|
validate_template_yaml(data)
|
|
|
|
def test_missing_elements_field(self):
|
|
"""测试缺少 elements 字段"""
|
|
data = {"vars": []}
|
|
with pytest.raises(YAMLError, match="缺少必需字段 'elements'"):
|
|
validate_template_yaml(data)
|
|
|
|
def test_elements_not_a_list(self):
|
|
"""测试 elements 不是列表"""
|
|
data = {"elements": "not a list"}
|
|
with pytest.raises(YAMLError, match="'elements' 必须是一个列表"):
|
|
validate_template_yaml(data)
|
|
|
|
def test_vars_not_a_list(self):
|
|
"""测试 vars 不是列表"""
|
|
data = {
|
|
"vars": "not a list",
|
|
"elements": []
|
|
}
|
|
with pytest.raises(YAMLError, match="'vars' 必须是一个列表"):
|
|
validate_template_yaml(data)
|
|
|
|
def test_var_item_not_a_dict(self):
|
|
"""测试变量项不是字典"""
|
|
data = {
|
|
"vars": ["not a dict"],
|
|
"elements": []
|
|
}
|
|
with pytest.raises(YAMLError, match="必须是一个字典对象"):
|
|
validate_template_yaml(data)
|
|
|
|
def test_var_item_missing_name(self):
|
|
"""测试变量项缺少 name 字段"""
|
|
data = {
|
|
"vars": [{"required": True}],
|
|
"elements": []
|
|
}
|
|
with pytest.raises(YAMLError, match="缺少必需字段 'name'"):
|
|
validate_template_yaml(data)
|
|
|
|
def test_template_without_vars(self):
|
|
"""测试没有 vars 的模板"""
|
|
data = {"elements": []}
|
|
# vars 是可选的
|
|
validate_template_yaml(data)
|
|
|
|
def test_not_a_dict(self):
|
|
"""测试不是字典"""
|
|
data = "not a dict"
|
|
with pytest.raises(YAMLError, match="必须是一个字典对象"):
|
|
validate_template_yaml(data)
|
|
|
|
|
|
class TestYAMLError:
|
|
"""YAMLError 异常测试类"""
|
|
|
|
def test_is_an_exception(self):
|
|
"""测试 YAMLError 是异常类"""
|
|
error = YAMLError("Test error")
|
|
assert isinstance(error, Exception)
|
|
|
|
def test_can_be_raised_and_caught(self):
|
|
"""测试可以被引发和捕获"""
|
|
with pytest.raises(YAMLError):
|
|
raise YAMLError("Test")
|