67 lines
2.4 KiB
Python
67 lines
2.4 KiB
Python
#!/usr/bin/env python3.9
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Test template filters functionality
|
|
"""
|
|
|
|
from pyenjoy.template.Engine import Engine
|
|
from pyenjoy.kit.Kv import Kv
|
|
|
|
def test_filters():
|
|
"""Test template filters"""
|
|
print("Testing template filters...")
|
|
|
|
engine = Engine.use()
|
|
|
|
# Test cases
|
|
test_cases = [
|
|
# Template, data, expected result, description
|
|
("#(tags | join(', '))", {"tags": ["cdms", "管理系统"]}, "cdms, 管理系统", "Basic join filter"),
|
|
("#(tags | join(';'))", {"tags": ["cdms", "管理系统"]}, "cdms;管理系统", "Join with custom separator"),
|
|
("#(tags | join)", {"tags": ["cdms", "管理系统"]}, "cdms管理系统", "Join without separator"),
|
|
("#(title | upper)", {"title": "test"}, "TEST", "Upper case filter"),
|
|
("#(title | lower)", {"title": "TEST"}, "test", "Lower case filter"),
|
|
("#(title | strip)", {"title": " test "}, "test", "Strip whitespace filter"),
|
|
# Multiple filters
|
|
("#(title | upper | strip)", {"title": " test "}, "TEST", "Multiple filters"),
|
|
# Filter with null coalescing
|
|
("#(tags??[] | join(', '))", {"tags": None}, "", "Join with null coalescing"),
|
|
# Complex scenario from user input
|
|
("#for(item in data.items)#(item.tags | join(', '))#end",
|
|
{"data": {"items": [{"tags": ["cdms", "管理系统"]}]}},
|
|
"cdms, 管理系统", "Complex scenario with for loop"),
|
|
]
|
|
|
|
passed = 0
|
|
failed = 0
|
|
|
|
for i, (template_str, data, expected, description) in enumerate(test_cases):
|
|
try:
|
|
print(f"\nTest {i+1}: {description}")
|
|
print(f"Template: {template_str}")
|
|
print(f"Data: {data}")
|
|
|
|
template = engine.get_template_by_string(template_str)
|
|
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
|
|
|
|
except Exception as e:
|
|
print(f"✗ Error: {e}")
|
|
failed += 1
|
|
|
|
print(f"\nSummary: {passed} passed, {failed} failed")
|
|
return passed == len(test_cases)
|
|
|
|
if __name__ == "__main__":
|
|
success = test_filters()
|
|
exit(0 if success else 1)
|