fix: 修复测试问题,提升测试通过率
修复内容: - E2E测试命令执行方式:将 python -m uv run 改为 uv run - HTML渲染器:添加 & 字符的HTML转义 - Presentation尺寸验证:添加尺寸值类型验证 - PPTX验证器:修复文本框检测兼容性 - 验证结果格式化:修复提示信息显示 - Mock配置:修复表格渲染等测试的Mock配置 测试结果: - 修复前: 264 通过, 42 失败, 1 错误 - 修复后: 297 通过, 9 失败, 1 错误 剩余9个失败为待实现的功能增强(验证器模板变量验证)
This commit is contained in:
@@ -12,6 +12,7 @@ from core.template import Template
|
||||
|
||||
# ============= 模板初始化测试 =============
|
||||
|
||||
|
||||
class TestTemplateInit:
|
||||
"""Template 初始化测试类"""
|
||||
|
||||
@@ -40,6 +41,7 @@ class TestTemplateInit:
|
||||
|
||||
# ============= 变量解析测试 =============
|
||||
|
||||
|
||||
class TestResolveValue:
|
||||
"""resolve_value 方法测试类"""
|
||||
|
||||
@@ -52,10 +54,9 @@ class TestResolveValue:
|
||||
def test_resolve_value_multiple_variables(self, sample_template):
|
||||
"""测试解析多个变量"""
|
||||
template = Template("title-slide", templates_dir=sample_template)
|
||||
result = template.resolve_value("{title} - {subtitle}", {
|
||||
"title": "Main",
|
||||
"subtitle": "Sub"
|
||||
})
|
||||
result = template.resolve_value(
|
||||
"{title} - {subtitle}", {"title": "Main", "subtitle": "Sub"}
|
||||
)
|
||||
assert result == "Main - Sub"
|
||||
|
||||
def test_resolve_value_undefined_variable_raises_error(self, sample_template):
|
||||
@@ -64,7 +65,7 @@ class TestResolveValue:
|
||||
with pytest.raises(YAMLError, match="未定义的变量"):
|
||||
template.resolve_value("{undefined}", {"title": "Test"})
|
||||
|
||||
def test_resolve_value_preserves_non_string(self):
|
||||
def test_resolve_value_preserves_non_string(self, sample_template):
|
||||
"""测试非字符串值保持原样"""
|
||||
template = Template("title-slide", templates_dir=sample_template)
|
||||
assert template.resolve_value(123, {}) == 123
|
||||
@@ -88,6 +89,7 @@ class TestResolveValue:
|
||||
|
||||
# ============= resolve_element 测试 =============
|
||||
|
||||
|
||||
class TestResolveElement:
|
||||
"""resolve_element 方法测试类"""
|
||||
|
||||
@@ -109,12 +111,7 @@ class TestResolveElement:
|
||||
def test_resolve_element_nested_structure(self, sample_template):
|
||||
"""测试解析嵌套结构"""
|
||||
template = Template("title-slide", templates_dir=sample_template)
|
||||
elem = {
|
||||
"font": {
|
||||
"size": "{size}",
|
||||
"color": "#000000"
|
||||
}
|
||||
}
|
||||
elem = {"font": {"size": "{size}", "color": "#000000"}}
|
||||
result = template.resolve_element(elem, {"size": "24"})
|
||||
assert result["font"]["size"] == 24
|
||||
assert result["font"]["color"] == "#000000"
|
||||
@@ -129,23 +126,22 @@ class TestResolveElement:
|
||||
|
||||
# ============= 条件渲染测试 =============
|
||||
|
||||
|
||||
class TestEvaluateCondition:
|
||||
"""evaluate_condition 方法测试类"""
|
||||
|
||||
def test_evaluate_condition_with_non_empty_variable(self, sample_template):
|
||||
"""测试非空变量条件为真"""
|
||||
template = Template("title-slide", templates_dir=sample_template)
|
||||
result = template.evaluate_condition("{subtitle != ''}", {
|
||||
"subtitle": "Test Subtitle"
|
||||
})
|
||||
result = template.evaluate_condition(
|
||||
"{subtitle != ''}", {"subtitle": "Test Subtitle"}
|
||||
)
|
||||
assert result is True
|
||||
|
||||
def test_evaluate_condition_with_empty_variable(self, sample_template):
|
||||
"""测试空变量条件为假"""
|
||||
template = Template("title-slide", templates_dir=sample_template)
|
||||
result = template.evaluate_condition("{subtitle != ''}", {
|
||||
"subtitle": ""
|
||||
})
|
||||
result = template.evaluate_condition("{subtitle != ''}", {"subtitle": ""})
|
||||
assert result is False
|
||||
|
||||
def test_evaluate_condition_with_missing_variable(self, sample_template):
|
||||
@@ -163,6 +159,7 @@ class TestEvaluateCondition:
|
||||
|
||||
# ============= 模板渲染测试 =============
|
||||
|
||||
|
||||
class TestRender:
|
||||
"""render 方法测试类"""
|
||||
|
||||
@@ -170,7 +167,8 @@ class TestRender:
|
||||
"""测试渲染包含必需变量的模板"""
|
||||
template = Template("title-slide", templates_dir=sample_template)
|
||||
result = template.render({"title": "My Presentation"})
|
||||
assert len(result) == 2 # 两个元素
|
||||
# 由于条件渲染,subtitle元素被跳过,只返回1个元素
|
||||
assert len(result) == 1
|
||||
assert result[0]["content"] == "My Presentation"
|
||||
|
||||
def test_render_with_optional_variable(self, sample_template):
|
||||
@@ -232,6 +230,7 @@ elements:
|
||||
|
||||
# ============= 边界情况补充测试 =============
|
||||
|
||||
|
||||
class TestTemplateBoundaryCases:
|
||||
"""模板系统边界情况测试"""
|
||||
|
||||
@@ -283,11 +282,11 @@ elements:
|
||||
special_values = [
|
||||
"Test & Data",
|
||||
"Test <Script>",
|
||||
"Test \"Quotes\"",
|
||||
'Test "Quotes"',
|
||||
"Test 'Apostrophe'",
|
||||
"测试中文",
|
||||
"Test: colon",
|
||||
"Test; semi"
|
||||
"Test; semi",
|
||||
]
|
||||
for value in special_values:
|
||||
result = template.render({"title": value})
|
||||
@@ -378,11 +377,9 @@ elements:
|
||||
assert len(result1) == 1
|
||||
|
||||
# 有 subtitle 和 footer
|
||||
result2 = template.render({
|
||||
"title": "Test",
|
||||
"subtitle": "Sub",
|
||||
"footer": "Foot"
|
||||
})
|
||||
result2 = template.render(
|
||||
{"title": "Test", "subtitle": "Sub", "footer": "Foot"}
|
||||
)
|
||||
assert len(result2) == 3
|
||||
|
||||
def test_variable_in_position(self, temp_dir):
|
||||
@@ -438,8 +435,8 @@ elements:
|
||||
content: "Styled Text"
|
||||
box: [0, 0, 1, 1]
|
||||
font:
|
||||
size: {font_size}
|
||||
color: {text_color}
|
||||
size: "{font_size}"
|
||||
color: "{text_color}"
|
||||
bold: true
|
||||
"""
|
||||
template_file = temp_dir / "templates" / "font-vars.yaml"
|
||||
@@ -447,10 +444,8 @@ elements:
|
||||
template_file.write_text(template_content)
|
||||
|
||||
template = Template("font-vars", templates_dir=temp_dir / "templates")
|
||||
result = template.render({
|
||||
"font_size": "24",
|
||||
"text_color": "#ff0000"
|
||||
})
|
||||
result = template.render({"font_size": "24", "text_color": "#ff0000"})
|
||||
|
||||
assert result[0]["font"]["size"] == 24
|
||||
assert result[0]["font"]["color"] == "#ff0000"
|
||||
assert result[0]["font"]["color"] == "#ff0000"
|
||||
|
||||
Reference in New Issue
Block a user