-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_test.py
More file actions
127 lines (102 loc) · 3.34 KB
/
quick_test.py
File metadata and controls
127 lines (102 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
"""
Quick test of the production template engine features.
"""
from template_engine import TemplateEngine, MissingVariableError, LoopRenderError
def test_basic_functionality():
"""Test basic template functionality."""
print("=== Basic Functionality Test ===")
engine = TemplateEngine(strict_mode=True)
context = {
'title': 'Hello World',
'users': [
{'name': 'Alice', 'age': 30},
{'name': 'Bob', 'age': 25}
]
}
template = """
<h1>$title</h1>
<ul>
{% for user in users %}
<li>$user.name is $user.age years old</li>
{% endfor %}
</ul>
"""
result = engine.render(template, context)
print("✅ Template rendered successfully:")
print(result)
return True
def test_missing_variable_strict():
"""Test missing variable detection in strict mode."""
print("\n=== Missing Variable Test (Strict) ===")
engine = TemplateEngine(strict_mode=True)
try:
result = engine.render("Hello $name!", {}) # Missing 'name'
print("❌ Should have failed!")
return False
except MissingVariableError as e:
print(f"✅ Correctly caught: {e}")
return True
def test_missing_variable_lenient():
"""Test missing variable handling in lenient mode."""
print("\n=== Missing Variable Test (Lenient) ===")
engine = TemplateEngine(strict_mode=False)
result = engine.render("Hello $name, welcome to $site!", {'name': 'Alice'})
print(f"✅ Result: {result}")
missing = engine.get_missing_variables()
print(f"✅ Missing variables: {missing}")
return 'site' in missing
def test_security():
"""Test security features."""
print("\n=== Security Test ===")
engine = TemplateEngine(strict_mode=False)
# Test XSS protection
context = {'message': '<script>alert("hack")</script>'}
result = engine.render("Message: $message", context)
if '<script>' in result:
print("✅ XSS protection working")
else:
print(f"❌ XSS not escaped: {result}")
return False
# Test dangerous variable filtering
dangerous_context = {
'__import__': 'bad',
'safe': 'good'
}
result = engine.render("$safe $__import__", dangerous_context)
if 'good' in result and 'bad' not in result:
print("✅ Dangerous variables filtered")
return True
else:
print(f"❌ Security issue: {result}")
return False
def test_loop_errors():
"""Test loop error handling."""
print("\n=== Loop Error Test ===")
engine = TemplateEngine(strict_mode=True)
# Test missing loop variable
try:
result = engine.render("""
{% for item in missing_list %}
$item.name
{% endfor %}
""", {})
print("❌ Should have failed!")
return False
except LoopRenderError as e:
print(f"✅ Loop error caught: {e}")
return True
if __name__ == "__main__":
tests = [
test_basic_functionality,
test_missing_variable_strict,
test_missing_variable_lenient,
test_security,
test_loop_errors
]
passed = 0
for test in tests:
if test():
passed += 1
print(f"\n🎉 {passed}/{len(tests)} tests passed!")
print("Production-grade template engine is working correctly!")