-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunicode_test.py
More file actions
158 lines (135 loc) · 4.93 KB
/
unicode_test.py
File metadata and controls
158 lines (135 loc) · 4.93 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test Unicode support for both text and HTML templates
"""
from template_engine import TemplateEngine
def test_unicode_support():
"""Test Unicode support with various character sets and templates."""
# Create engine
engine = TemplateEngine(strict_mode=False, auto_escape=True)
# Test Unicode context with various languages and emojis
unicode_context = {
'title': 'Héllo Wörld 🌍',
'name': 'José María',
'city': 'São Paulo',
'japanese': 'こんにちは',
'arabic': 'مرحبا',
'emoji': '🎉🚀💫',
'chinese': '你好世界',
'russian': 'Привет мир',
'users': [
{'name': 'François', 'country': 'France 🇫🇷', 'greeting': 'Bonjour'},
{'name': 'Müller', 'country': 'Deutschland 🇩🇪', 'greeting': 'Guten Tag'},
{'name': '田中太郎', 'country': '日本 🇯🇵', 'greeting': 'こんにちは'},
{'name': 'Владимир', 'country': 'Россия 🇷🇺', 'greeting': 'Привет'}
]
}
# Test 1: Plain text template with Unicode
print('=== TEXT TEMPLATE TEST ===')
text_template = '''Title: $title
User: $name from $city
Japanese: $japanese
Arabic: $arabic
Chinese: $chinese
Russian: $russian
Emojis: $emoji
International Users:
{% for user in users %}
- $user.name from $user.country says "$user.greeting"
{% endfor %}'''
try:
result = engine.render(text_template, unicode_context)
print(result)
print("✓ Text template with Unicode: SUCCESS")
except Exception as e:
print(f"✗ Text template with Unicode: FAILED - {e}")
# Test 2: HTML template with Unicode
print('\n=== HTML TEMPLATE TEST ===')
html_template = '''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$title</title>
</head>
<body>
<h1>$title</h1>
<p>Welcome <strong>$name</strong> from <em>$city</em>!</p>
<h2>Multilingual Greetings</h2>
<ul>
<li>Japanese: $japanese</li>
<li>Arabic: $arabic</li>
<li>Chinese: $chinese</li>
<li>Russian: $russian</li>
<li>Fun stuff: $emoji</li>
</ul>
<h2>International Users</h2>
<table border="1">
<tr><th>Name</th><th>Country</th><th>Greeting</th></tr>
{% for user in users %}
<tr>
<td>$user.name</td>
<td>$user.country</td>
<td>$user.greeting</td>
</tr>
{% endfor %}
</table>
</body>
</html>'''
try:
html_result = engine.render(html_template, unicode_context)
print(html_result)
print("✓ HTML template with Unicode: SUCCESS")
except Exception as e:
print(f"✗ HTML template with Unicode: FAILED - {e}")
# Test 3: Mixed content with special characters
print('\n=== SPECIAL CHARACTERS TEST ===')
special_context = {
'symbols': '©®™€£¥§¶†‡•…‰‹›""''',
'math': '∑∏∆∇∂∫∞≠≤≥±×÷√∝∈∉∪∩⊂⊃',
'arrows': '←→↑↓↔↕⇐⇒⇑⇓⇔⇕',
'mixed': 'Mix: αβγ + 한글 + العربية + עברית'
}
special_template = '''Special Characters Test:
Symbols: $symbols
Math: $math
Arrows: $arrows
Mixed: $mixed'''
try:
special_result = engine.render(special_template, special_context)
print(special_result)
print("✓ Special characters: SUCCESS")
except Exception as e:
print(f"✗ Special characters: FAILED - {e}")
# Test 4: Test with auto_escape disabled for HTML content
print('\n=== HTML NO-ESCAPE TEST ===')
no_escape_engine = TemplateEngine(strict_mode=False, auto_escape=False)
html_content_context = {
'content': '<p>This is <strong>HTML content</strong> with <em>tags</em></p>',
'script': '<script>alert("test")</script>'
}
no_escape_template = '''Raw HTML Content:
$content
Script tag (dangerous): $script'''
try:
no_escape_result = no_escape_engine.render(no_escape_template, html_content_context)
print(no_escape_result)
print("✓ No-escape mode: SUCCESS")
except Exception as e:
print(f"✗ No-escape mode: FAILED - {e}")
# Test 5: Test encoding preservation
print('\n=== ENCODING PRESERVATION TEST ===')
try:
# Test that we can handle various encodings properly
with open('unicode_output.txt', 'w', encoding='utf-8') as f:
f.write(result)
with open('unicode_output.txt', 'r', encoding='utf-8') as f:
read_back = f.read()
if read_back == result:
print("✓ UTF-8 encoding preservation: SUCCESS")
else:
print("✗ UTF-8 encoding preservation: FAILED")
except Exception as e:
print(f"✗ Encoding test: FAILED - {e}")
if __name__ == "__main__":
test_unicode_support()