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
This commit is contained in:
104
tests/unit/test_utils.py
Normal file
104
tests/unit/test_utils.py
Normal file
@@ -0,0 +1,104 @@
|
||||
"""
|
||||
工具函数单元测试
|
||||
|
||||
测试颜色转换和格式验证函数
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from utils import hex_to_rgb, validate_color
|
||||
|
||||
|
||||
class TestHexToRgb:
|
||||
"""hex_to_rgb 函数测试类"""
|
||||
|
||||
def test_full_hex_format(self):
|
||||
"""测试完整的 #RRGGBB 格式"""
|
||||
assert hex_to_rgb("#ffffff") == (255, 255, 255)
|
||||
assert hex_to_rgb("#000000") == (0, 0, 0)
|
||||
assert hex_to_rgb("#4a90e2") == (74, 144, 226)
|
||||
assert hex_to_rgb("#FF0000") == (255, 0, 0)
|
||||
assert hex_to_rgb("#00FF00") == (0, 255, 0)
|
||||
assert hex_to_rgb("#0000FF") == (0, 0, 255)
|
||||
|
||||
def test_short_hex_format(self):
|
||||
"""测试短格式 #RGB"""
|
||||
assert hex_to_rgb("#fff") == (255, 255, 255)
|
||||
assert hex_to_rgb("#000") == (0, 0, 0)
|
||||
assert hex_to_rgb("#abc") == (170, 187, 204)
|
||||
assert hex_to_rgb("#ABC") == (170, 187, 204)
|
||||
|
||||
def test_without_hash_sign(self):
|
||||
"""测试没有 # 号"""
|
||||
assert hex_to_rgb("ffffff") == (255, 255, 255)
|
||||
assert hex_to_rgb("4a90e2") == (74, 144, 226)
|
||||
|
||||
def test_invalid_length(self):
|
||||
"""测试无效长度"""
|
||||
with pytest.raises(ValueError, match="无效的颜色格式"):
|
||||
hex_to_rgb("#ff") # 太短
|
||||
with pytest.raises(ValueError, match="无效的颜色格式"):
|
||||
hex_to_rgb("#fffff") # 5 位
|
||||
with pytest.raises(ValueError, match="无效的颜色格式"):
|
||||
hex_to_rgb("#fffffff") # 7 位
|
||||
|
||||
def test_invalid_characters(self):
|
||||
"""测试无效字符"""
|
||||
with pytest.raises(ValueError):
|
||||
hex_to_rgb("#gggggg")
|
||||
with pytest.raises(ValueError):
|
||||
hex_to_rgb("#xyz")
|
||||
|
||||
def test_mixed_case(self):
|
||||
"""测试大小写混合"""
|
||||
assert hex_to_rgb("#AbCdEf") == (171, 205, 239)
|
||||
assert hex_to_rgb("#aBcDeF") == (171, 205, 239)
|
||||
|
||||
|
||||
class TestValidateColor:
|
||||
"""validate_color 函数测试类"""
|
||||
|
||||
def test_valid_full_hex_colors(self):
|
||||
"""测试有效的完整十六进制颜色"""
|
||||
assert validate_color("#ffffff") is True
|
||||
assert validate_color("#000000") is True
|
||||
assert validate_color("#4a90e2") is True
|
||||
assert validate_color("#FF0000") is True
|
||||
assert validate_color("#ABCDEF") is True
|
||||
|
||||
def test_valid_short_hex_colors(self):
|
||||
"""测试有效的短格式十六进制颜色"""
|
||||
assert validate_color("#fff") is True
|
||||
assert validate_color("#000") is True
|
||||
assert validate_color("#abc") is True
|
||||
assert validate_color("#ABC") is True
|
||||
|
||||
def test_without_hash_sign(self):
|
||||
"""测试没有 # 号"""
|
||||
assert validate_color("ffffff") is False
|
||||
assert validate_color("fff") is False
|
||||
|
||||
def test_invalid_length(self):
|
||||
"""测试无效长度"""
|
||||
assert validate_color("#ff") is False
|
||||
assert validate_color("#ffff") is False
|
||||
assert validate_color("#fffff") is False
|
||||
assert validate_color("#fffffff") is False
|
||||
|
||||
def test_invalid_characters(self):
|
||||
"""测试无效字符"""
|
||||
assert validate_color("#gggggg") is False
|
||||
assert validate_color("#xyz") is False
|
||||
assert validate_color("red") is False
|
||||
assert validate_color("rgb(255,0,0)") is False
|
||||
|
||||
def test_non_string_input(self):
|
||||
"""测试非字符串输入"""
|
||||
assert validate_color(123) is False
|
||||
assert validate_color(None) is False
|
||||
assert validate_color([]) is False
|
||||
assert validate_color({"color": "#fff"}) is False
|
||||
|
||||
def test_empty_string(self):
|
||||
"""测试空字符串"""
|
||||
assert validate_color("") is False
|
||||
assert validate_color("#") is False
|
||||
Reference in New Issue
Block a user