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
|
# -*- coding: utf-8 -*-
import sys
import unittest
from mako import exceptions
from mako.template import Template
class ExceptionsTest(unittest.TestCase):
def test_html_error_template(self):
"""test the html_error_template"""
code = """
% i = 0
"""
try:
template = Template(code)
template.render()
except exceptions.CompileException, ce:
html_error = exceptions.html_error_template().render()
assert ("CompileException: Fragment 'i = 0' is not a partial "
"control statement") in html_error
assert '<style>' in html_error
assert '</style>' in html_error
html_error_stripped = html_error.strip()
assert html_error_stripped.startswith('<html>')
assert html_error_stripped.endswith('</html>')
not_full = exceptions.html_error_template().render(full=False)
assert '<html>' not in not_full
assert '</html>' not in not_full
assert '<style>' in not_full
assert '</style>' in not_full
no_css = exceptions.html_error_template().render(css=False)
assert '<style>' not in no_css
assert '</style>' not in no_css
else:
assert False, ("This function should trigger a CompileException, "
"but didn't")
def test_utf8_html_error_template(self):
"""test the html_error_template with a Template containing utf8 chars"""
code = """# -*- coding: utf-8 -*-
% if 2 == 2: /an error
${u'привет'}
% endif
"""
try:
template = Template(code)
template.render()
except exceptions.CompileException, ce:
html_error = exceptions.html_error_template().render()
assert ("CompileException: Fragment 'if 2 == 2: /an "
"error' is not a partial control "
"statement at line: 2 char: 1") in html_error
assert u"3 ${u'привет'}".encode(sys.getdefaultencoding(),
'htmlentityreplace') in html_error
else:
assert False, ("This function should trigger a CompileException, "
"but didn't")
if __name__ == '__main__':
unittest.main()
|