1
0
Files
PPTX/openspec/changes/archive/2026-03-02-add-comprehensive-tests/specs/unit-tests/spec.md
lanyuanxiaoyao ab2510a400 test: add comprehensive pytest test suite
Add complete test infrastructure for yaml2pptx project with 245+ tests
covering unit, integration, and end-to-end scenarios.

Test structure:
- Unit tests: elements, template system, validators, loaders, utils
- Integration tests: presentation and rendering flows
- E2E tests: CLI commands (convert, check, preview)

Key features:
- PptxFileValidator for Level 2 PPTX validation (file structure,
  element count, content matching, position tolerance)
- Comprehensive fixtures for test data consistency
- Mock-based testing for external dependencies
- Test images generated with PIL/Pillow
- Boundary case coverage for edge scenarios

Dependencies added:
- pytest, pytest-cov, pytest-mock
- pillow (for test image generation)

Documentation updated:
- README.md: test running instructions
- README_DEV.md: test development guide

Co-authored-by: OpenSpec change: add-comprehensive-tests
2026-03-02 23:11:34 +08:00

124 lines
4.3 KiB
Markdown
Raw 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.
## ADDED Requirements
### Requirement: 元素类单元测试
系统 SHALL 为所有元素类TextElement、ImageElement、ShapeElement、TableElement提供单元测试。
#### Scenario: TextElement 创建验证
- **WHEN** 创建 TextElement 实例
- **THEN** box 参数被验证为包含 4 个数字的列表
- **AND** 无效的 box 会引发 ValueError
#### Scenario: TextElement 颜色验证
- **WHEN** TextElement 的 font.color 格式无效
- **THEN** validate() 返回包含 INVALID_COLOR_FORMAT 错误的 ValidationIssue 列表
#### Scenario: TextElement 字体大小验证
- **WHEN** TextElement 的 font.size 小于 8pt
- **THEN** validate() 返回包含 FONT_TOO_SMALL 警告的 ValidationIssue 列表
#### Scenario: ImageElement 创建验证
- **WHEN** 创建 ImageElement 实例
- **THEN** src 参数必须非空
- **AND** 空的 src 会引发 ValueError
#### Scenario: ShapeElement 类型验证
- **WHEN** ShapeElement 的 shape 类型不支持
- **THEN** validate() 返回包含 INVALID_SHAPE_TYPE 错误的 ValidationIssue 列表
#### Scenario: TableElement 数据验证
- **WHEN** TableElement 的 data 行列数不一致
- **THEN** validate() 返回包含 TABLE_INCONSISTENT_COLUMNS 错误的 ValidationIssue 列表
#### Scenario: 元素工厂函数
- **WHEN** 调用 create_element() 并传入不同 type
- **THEN** 返回对应类型的元素对象
- **AND** 不支持的 type 会引发 ValueError
### Requirement: 模板系统单元测试
系统 SHALL 为模板系统Template 类)提供单元测试。
#### Scenario: 模板初始化验证
- **WHEN** Template 初始化时未指定 templates_dir
- **THEN** 引发 YAMLError
#### Scenario: 模板名称验证
- **WHEN** Template 初始化时模板名称包含路径分隔符
- **THEN** 引发 YAMLError
#### Scenario: 变量解析
- **WHEN** 调用 resolve_value() 并包含变量引用
- **THEN** 返回解析后的值
- **AND** 未定义的变量会引发 YAMLError
#### Scenario: 条件渲染评估
- **WHEN** 调用 evaluate_condition() 并传入条件表达式
- **THEN** 返回正确的布尔值
#### Scenario: 模板渲染
- **WHEN** 调用 render() 并提供变量值
- **THEN** 返回渲染后的元素列表
- **AND** 缺少必需变量会引发 YAMLError
### Requirement: 验证器单元测试
系统 SHALL 为所有验证器提供单元测试。
#### Scenario: 几何验证器 - 元素边界检查
- **WHEN** 元素超出页面边界
- **THEN** GeometryValidator 返回 ELEMENT_OUT_OF_BOUNDS 警告
#### Scenario: 几何验证器 - 完全在页面外
- **WHEN** 元素完全在页面外
- **THEN** GeometryValidator 返回 ELEMENT_COMPLETELY_OUT_OF_BOUNDS 警告
#### Scenario: 资源验证器 - 图片检查
- **WHEN** 图片文件不存在
- **THEN** ResourceValidator 返回 IMAGE_FILE_NOT_FOUND 错误
#### Scenario: 资源验证器 - 模板检查
- **WHEN** 模板文件不存在
- **THEN** ResourceValidator 返回 TEMPLATE_FILE_NOT_FOUND 错误
#### Scenario: 主验证器协调
- **WHEN** Validator.validate() 执行
- **THEN** 调用所有子验证器
- **AND** 按级别分类问题ERROR/WARNING/INFO
### Requirement: YAML 加载器单元测试
系统 SHALL 为 YAML 加载器提供单元测试。
#### Scenario: 文件不存在
- **WHEN** 加载不存在的文件
- **THEN** 引发 YAMLError 并包含 "文件不存在" 消息
#### Scenario: YAML 语法错误
- **WHEN** 加载包含语法错误的 YAML 文件
- **THEN** 引发 YAMLError 并包含错误行号信息
#### Scenario: 演示文稿结构验证
- **WHEN** data 缺少 slides 字段
- **THEN** validate_presentation_yaml() 引发 YAMLError
#### Scenario: 模板结构验证
- **WHEN** 模板 data 缺少 elements 字段
- **THEN** validate_template_yaml() 引发 YAMLError
### Requirement: 工具函数单元测试
系统 SHALL 为工具函数提供单元测试。
#### Scenario: 颜色转换 - 完整格式
- **WHEN** 调用 hex_to_rgb() 并传入 #RRGGBB 格式
- **THEN** 返回正确的 (R, G, B) 元组
#### Scenario: 颜色转换 - 短格式
- **WHEN** 调用 hex_to_rgb() 并传入 #RGB 格式
- **THEN** 返回正确的 (R, G, B) 元组
#### Scenario: 颜色转换 - 无效格式
- **WHEN** 调用 hex_to_rgb() 并传入无效格式
- **THEN** 引发 ValueError
#### Scenario: 颜色格式验证
- **WHEN** 调用 validate_color() 并传入有效颜色
- **THEN** 返回 True
- **AND** 无效颜色返回 False