-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced_unicode_test.py
More file actions
247 lines (203 loc) · 8.3 KB
/
enhanced_unicode_test.py
File metadata and controls
247 lines (203 loc) · 8.3 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Enhanced Unicode and Template Type Test for the CustomTemplateEngine
"""
from template_engine import TemplateEngine
import os
def test_enhanced_unicode_support():
"""Test enhanced Unicode support with template types."""
print("🧪 Testing Enhanced Unicode Support for Text and HTML Templates")
print("=" * 70)
# Create engines with different settings
engines = {
'html_safe': TemplateEngine(auto_escape=True, strict_mode=False),
'text_mode': TemplateEngine(auto_escape=False, strict_mode=False),
'html_custom': TemplateEngine(auto_escape=True, strict_mode=False)
}
# Unicode-rich test context
unicode_context = {
'site_title': 'My International Site 🌍',
'welcome_msg': 'Welcome & Bienvenue',
'description': 'This contains "quotes" & special chars',
'languages': [
{'name': 'English', 'greeting': 'Hello', 'flag': '🇺🇸'},
{'name': 'French', 'greeting': 'Bonjour', 'flag': '🇫🇷'},
{'name': 'Japanese', 'greeting': 'こんにちは', 'flag': '🇯🇵'},
{'name': 'Arabic', 'greeting': 'مرحبا', 'flag': '🇸🇦'},
{'name': 'Chinese', 'greeting': '你好', 'flag': '🇨🇳'},
{'name': 'Russian', 'greeting': 'Привет', 'flag': '🇷🇺'},
{'name': 'German', 'greeting': 'Guten Tag', 'flag': '🇩🇪'},
{'name': 'Hebrew', 'greeting': 'שלום', 'flag': '🇮🇱'}
],
'math_symbols': '∑∏∆∇∂∫∞≠≤≥±×÷√',
'currency': '€£¥₹₽¥₩$',
'html_content': '<p>This is <strong>HTML</strong> content</p>'
}
# Test 1: HTML Template with escaping
print("1️⃣ HTML Template Test (with XSS protection)")
print("-" * 50)
html_template = '''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>$site_title</title>
</head>
<body>
<header>
<h1>$site_title</h1>
<p>$welcome_msg</p>
<p class="description">$description</p>
</header>
<main>
<section>
<h2>International Greetings</h2>
<div class="languages">
{% for lang in languages %}
<div class="language">
<span class="flag">$lang.flag</span>
<strong>$lang.name</strong>:
<em>$lang.greeting</em>
</div>
{% endfor %}
</div>
</section>
<section>
<h2>Unicode Symbols</h2>
<p>Math: $math_symbols</p>
<p>Currency: $currency</p>
</section>
<section>
<h2>Raw HTML Content</h2>
<div>$html_content</div>
</section>
</main>
</body>
</html>'''
try:
html_result = engines['html_safe'].render_html(html_template, unicode_context)
print("✅ HTML template rendered successfully")
# Save to file manually
with open('output_unicode.html', 'w', encoding='utf-8') as f:
f.write(html_result)
print("✅ HTML saved to file with Unicode preservation")
except Exception as e:
print(f"❌ HTML template failed: {e}")
# Test 2: Plain Text Template
print("\n2️⃣ Plain Text Template Test (no escaping)")
print("-" * 50)
text_template = '''International Website Content
$site_title
$welcome_msg
$description
Supported Languages:
{% for lang in languages %}
$lang.flag $lang.name: "$lang.greeting"
{% endfor %}
Unicode Support:
• Math Symbols: $math_symbols
• Currency: $currency
• HTML Content: $html_content
This template preserves all Unicode characters and symbols without HTML escaping.
'''
try:
text_result = engines['text_mode'].render_text(text_template, unicode_context)
print(text_result)
print("✅ Text template rendered successfully")
# Save to file manually
with open('output_unicode.txt', 'w', encoding='utf-8') as f:
f.write(text_result)
print("✅ Text saved to file with Unicode preservation")
except Exception as e:
print(f"❌ Text template failed: {e}")
# Test 3: Mixed content with selective escaping
print("\n3️⃣ Selective Escaping Test")
print("-" * 50)
mixed_context = {
'safe_html': '<em>This should be escaped</em>',
'user_input': '<script>alert("XSS")</script>',
'unicode_text': 'こんにちは & שלום'
}
mixed_template = '''Safe HTML: $safe_html
User Input: $user_input
Unicode: $unicode_text'''
# Test with escaping
escaped_result = engines['html_safe'].render(mixed_template, mixed_context)
print("With HTML escaping:")
print(escaped_result)
# Test without escaping
unescaped_result = engines['text_mode'].render(mixed_template, mixed_context)
print("\nWithout HTML escaping:")
print(unescaped_result)
print("✅ Selective escaping test completed")
# Test 4: Encoding test with different character sets
print("\n4️⃣ Multi-Encoding Test")
print("-" * 50)
encoding_context = {
'latin': 'Café résumé naïve',
'cyrillic': 'Москва Санкт-Петербург',
'arabic': 'العربية الفصحى',
'chinese': '简体中文 繁體中文',
'japanese': 'ひらがな カタカナ 漢字',
'emoji': '😀😃😄😁😆😅😂🤣'
}
encoding_template = '''Multi-Language Test:
Latin: $latin
Cyrillic: $cyrillic
Arabic: $arabic
Chinese: $chinese
Japanese: $japanese
Emoji: $emoji'''
try:
encoding_result = engines['html_custom'].render_text(encoding_template, encoding_context)
print(encoding_result)
# Test file output with different encodings
for encoding in ['utf-8', 'utf-16']:
try:
with open(f'unicode_test_{encoding.replace("-", "_")}.txt', 'w', encoding=encoding) as f:
f.write(encoding_result)
print(f"✅ Successfully saved with {encoding} encoding")
except Exception as e:
print(f"❌ Failed to save with {encoding}: {e}")
except Exception as e:
print(f"❌ Encoding test failed: {e}")
# Test 5: Template type auto-detection
print("\n5️⃣ Template Type Auto-Detection Test")
print("-" * 50)
auto_context = {'title': 'Auto-Detection Test', 'content': '<b>Bold text</b>'}
auto_template = '<h1>$title</h1><p>$content</p>'
try:
engine = TemplateEngine()
# Should auto-detect as HTML and escape
html_result = engine.render_html(auto_template, auto_context)
with open('auto_test.html', 'w', encoding='utf-8') as f:
f.write(html_result)
# Should auto-detect as text and not escape
text_result = engine.render_text(auto_template, auto_context)
with open('auto_test.txt', 'w', encoding='utf-8') as f:
f.write(text_result)
print("✅ Auto-detection test completed")
# Verify files were created correctly
with open('auto_test.html', 'r', encoding='utf-8') as f:
html_content = f.read()
if '<b>' in html_content:
print("✅ HTML file correctly escaped")
else:
print("❌ HTML file not escaped properly")
with open('auto_test.txt', 'r', encoding='utf-8') as f:
txt_content = f.read()
if '<b>' in txt_content:
print("✅ Text file correctly preserved HTML")
else:
print("❌ Text file incorrectly escaped")
except Exception as e:
print(f"❌ Auto-detection test failed: {e}")
print("\n🎉 Enhanced Unicode and Template Type Tests Completed!")
print("\nGenerated files:")
for filename in ['output_unicode.html', 'output_unicode.txt', 'unicode_test_utf_8.txt',
'unicode_test_utf_16.txt', 'auto_test.html', 'auto_test.txt']:
if os.path.exists(filename):
print(f" 📄 {filename}")
if __name__ == "__main__":
test_enhanced_unicode_support()