调整版本并做测试

This commit is contained in:
2026-02-27 17:10:54 +08:00
parent fa673138f6
commit 31be9d0e97
77 changed files with 679 additions and 25 deletions

View File

@@ -0,0 +1,104 @@
#!/usr/bin/env python3.9
# -*- coding: utf-8 -*-
"""
Test template from file with newlines functionality
"""
import os
from pyenjoy.template.Engine import Engine
from pyenjoy.kit.Kv import Kv
def test_file_template_newline():
"""Test template from file with newlines"""
print("Testing template from file with newlines...")
# Create a test template file
test_dir = "./test_templates"
os.makedirs(test_dir, exist_ok=True)
# Test case 1: Template with expression containing newlines
test_file1 = os.path.join(test_dir, "test_newline.txt")
with open(test_file1, "w", encoding="utf-8") as f:
f.write("Name: #(name),\nAge: #(age),\nCity: #(city??'未知')")
# Test case 2: Template with multi-line expression (using triple quotes)
test_file2 = os.path.join(test_dir, "test_multiline_expr.txt")
with open(test_file2, "w", encoding="utf-8") as f:
f.write("""Result: #(data ??
'未知')""")
# Test case 3: Template with complex multi-line expression
test_file3 = os.path.join(test_dir, "test_complex_multiline.txt")
with open(test_file3, "w", encoding="utf-8") as f:
f.write("""Details: #(user.name ?? '匿名') -
#(user.age ?? '未知') -
#(user.city ?? '未知')""")
# Test case 4: Template with single expression on multiple lines
test_file4 = os.path.join(test_dir, "test_single_multiline.txt")
with open(test_file4, "w", encoding="utf-8") as f:
f.write("Value: #(\n data \n ?? \n 'default'\n)")
# Create engine and configure base template path
engine = Engine.use()
engine.base_template_path = os.getcwd()
# Test cases
test_cases = [
# (file_path, data, expected_result, description)
(test_file1, {"name": "张三", "age": 30}, "Name: 张三,\nAge: 30,\nCity: 未知", "Template with newlines"),
(test_file4, {"data": None}, "Value: default", "Single expression on multiple lines"),
(test_file4, {"data": "测试"}, "Value: 测试", "Single expression with value on multiple lines"),
]
passed = 0
failed = 0
for i, (file_path, data, expected, description) in enumerate(test_cases):
try:
print(f"\nTest {i+1}: {description}")
print(f"File: {file_path}")
print(f"Data: {data}")
# Get template
template = engine.get_template(file_path)
# Render template
result = template.render_to_string(Kv(data))
print(f"Expected: '{expected}'")
print(f"Got: '{result}'")
if result == expected:
print("✓ Passed")
passed += 1
else:
print("✗ Failed")
failed += 1
# Debug: Check content
print(f"\nDebug info:")
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
print(f"File content: '{content}'")
print(f"Content length: {len(content)}")
print(f"Content repr: {repr(content)}")
except Exception as e:
print(f"✗ Error: {e}")
failed += 1
print(f"\nSummary: {passed} passed, {failed} failed")
# Clean up
for file_path in [test_file1, test_file2, test_file3, test_file4]:
if os.path.exists(file_path):
os.remove(file_path)
if os.path.exists(test_dir):
os.rmdir(test_dir)
return passed == len(test_cases)
if __name__ == "__main__":
success = test_file_template_newline()
exit(0 if success else 1)