| 1 |
|
| 2 |
|
| 3 |
import os |
| 4 |
from shutil import rmtree |
| 5 |
import tempfile |
| 6 |
import unittest |
| 7 |
from Cheetah.Compiler import Compiler |
| 8 |
from Cheetah.Template import Template |
| 9 |
from Cheetah import CheetahWrapper |
| 10 |
from Cheetah.compat import PY2, unicode, load_module_from_file |
| 11 |
|
| 12 |
|
| 13 |
class CommandLineTest(unittest.TestCase): |
| 14 |
def createAndCompile(self, source): |
| 15 |
tmpDir = tempfile.mkdtemp() |
| 16 |
try: |
| 17 |
sourcefile = os.path.join(tmpDir, "source") |
| 18 |
|
| 19 |
if PY2: |
| 20 |
fd = open('%s.tmpl' % sourcefile, 'w') |
| 21 |
else: |
| 22 |
fd = open('%s.tmpl' % sourcefile, 'w', encoding='utf-8') |
| 23 |
fd.write(source) |
| 24 |
fd.close() |
| 25 |
|
| 26 |
wrap = CheetahWrapper.CheetahWrapper() |
| 27 |
wrap.main(['cheetah', 'compile', |
| 28 |
'--encoding=utf-8', '--settings=encoding="utf-8"', |
| 29 |
'--quiet', '--nobackup', sourcefile]) |
| 30 |
module_name = os.path.split(sourcefile)[1] |
| 31 |
module = load_module_from_file( |
| 32 |
module_name, module_name, sourcefile + '.py') |
| 33 |
template = getattr(module, module_name) |
| 34 |
return template |
| 35 |
finally: |
| 36 |
rmtree(tmpDir, True) |
| 37 |
|
| 38 |
|
| 39 |
class JBQ_UTF8_Test1(unittest.TestCase): |
| 40 |
def runTest(self): |
| 41 |
t = Template.compile(source="""Main file with |$v| |
| 42 |
|
| 43 |
$other""") |
| 44 |
|
| 45 |
otherT = Template.compile(source="Other template with |$v|") |
| 46 |
other = otherT() |
| 47 |
t.other = other |
| 48 |
|
| 49 |
t.v = u'Unicode String' |
| 50 |
t.other.v = u'Unicode String' |
| 51 |
|
| 52 |
assert unicode(t()) |
| 53 |
|
| 54 |
|
| 55 |
class JBQ_UTF8_Test2(unittest.TestCase): |
| 56 |
def runTest(self): |
| 57 |
t = Template.compile(source="""Main file with |$v| |
| 58 |
|
| 59 |
$other""") |
| 60 |
|
| 61 |
otherT = Template.compile(source="Other template with |$v|") |
| 62 |
other = otherT() |
| 63 |
t.other = other |
| 64 |
|
| 65 |
t.v = u'Unicode String with eacute é' |
| 66 |
t.other.v = u'Unicode String' |
| 67 |
|
| 68 |
assert unicode(t()) |
| 69 |
|
| 70 |
|
| 71 |
class JBQ_UTF8_Test3(unittest.TestCase): |
| 72 |
def runTest(self): |
| 73 |
t = Template.compile(source="""Main file with |$v| |
| 74 |
|
| 75 |
$other""") |
| 76 |
|
| 77 |
otherT = Template.compile(source="Other template with |$v|") |
| 78 |
other = otherT() |
| 79 |
t.other = other |
| 80 |
|
| 81 |
t.v = u'Unicode String with eacute é' |
| 82 |
t.other.v = u'Unicode String and an eacute é' |
| 83 |
|
| 84 |
assert unicode(t()) |
| 85 |
|
| 86 |
|
| 87 |
class JBQ_UTF8_Test4(unittest.TestCase): |
| 88 |
def runTest(self): |
| 89 |
t = Template.compile(source="""#encoding utf-8 |
| 90 |
Main file with |$v| and eacute in the template é""") |
| 91 |
|
| 92 |
t.v = 'Unicode String' |
| 93 |
|
| 94 |
assert unicode(t()) |
| 95 |
|
| 96 |
|
| 97 |
class JBQ_UTF8_Test5(unittest.TestCase): |
| 98 |
def runTest(self): |
| 99 |
t = Template.compile(source="""#encoding utf-8 |
| 100 |
Main file with |$v| and eacute in the template é""") |
| 101 |
|
| 102 |
t.v = u'Unicode String' |
| 103 |
|
| 104 |
assert unicode(t()) |
| 105 |
|
| 106 |
|
| 107 |
class JBQ_UTF8_Test6(unittest.TestCase): |
| 108 |
def runTest(self): |
| 109 |
source = """#encoding utf-8 |
| 110 |
#set $someUnicodeString = u"Bébé" |
| 111 |
Main file with |$v| and eacute in the template é""" |
| 112 |
t = Template.compile(source=source) |
| 113 |
|
| 114 |
t.v = u'Unicode String' |
| 115 |
|
| 116 |
assert unicode(t()) |
| 117 |
|
| 118 |
|
| 119 |
class JBQ_UTF8_Test7(CommandLineTest): |
| 120 |
def runTest(self): |
| 121 |
source = """#encoding utf-8 |
| 122 |
#set $someUnicodeString = u"Bébé" |
| 123 |
Main file with |$v| and eacute in the template é""" |
| 124 |
|
| 125 |
template = self.createAndCompile(source) |
| 126 |
template.v = u'Unicode String' |
| 127 |
|
| 128 |
assert unicode(template()) |
| 129 |
|
| 130 |
|
| 131 |
class JBQ_UTF8_Test8(CommandLineTest): |
| 132 |
def testStaticCompile(self): |
| 133 |
source = """#encoding utf-8 |
| 134 |
#set $someUnicodeString = u"Bébé" |
| 135 |
$someUnicodeString""" |
| 136 |
|
| 137 |
template = self.createAndCompile(source)() |
| 138 |
|
| 139 |
a = unicode(template) |
| 140 |
if PY2: |
| 141 |
a = a.encode("utf-8") |
| 142 |
self.assertEqual("Bébé", a) |
| 143 |
|
| 144 |
def testDynamicCompile(self): |
| 145 |
source = """#encoding utf-8 |
| 146 |
#set $someUnicodeString = u"Bébé" |
| 147 |
$someUnicodeString""" |
| 148 |
|
| 149 |
template = Template(source=source) |
| 150 |
|
| 151 |
a = unicode(template) |
| 152 |
if PY2: |
| 153 |
a = a.encode("utf-8") |
| 154 |
self.assertEqual("Bébé", a) |
| 155 |
|
| 156 |
|
| 157 |
class EncodeUnicodeCompatTest(unittest.TestCase): |
| 158 |
""" |
| 159 |
Taken initially from Red Hat's bugzilla #529332 |
| 160 |
https://bugzilla.redhat.com/show_bug.cgi?id=529332 |
| 161 |
""" |
| 162 |
def runTest(self): |
| 163 |
t = Template("""Foo ${var}""", filter='EncodeUnicode') |
| 164 |
t.var = u"Text with some non-ascii characters: åäö" |
| 165 |
|
| 166 |
rc = t.respond() |
| 167 |
assert isinstance(rc, unicode), \ |
| 168 |
('Template.respond() should return unicode', rc) |
| 169 |
|
| 170 |
rc = str(t) |
| 171 |
assert isinstance(rc, str), \ |
| 172 |
('Template.__str__() should return a UTF-8 encoded string', rc) |
| 173 |
|
| 174 |
|
| 175 |
class Unicode_in_SearchList_Test(CommandLineTest): |
| 176 |
def test_BasicASCII(self): |
| 177 |
source = '''This is $adjective''' |
| 178 |
|
| 179 |
template = self.createAndCompile(source) |
| 180 |
assert template and issubclass(template, Template) |
| 181 |
template = template(searchList=[{'adjective': u'neat'}]) |
| 182 |
assert template.respond() |
| 183 |
|
| 184 |
def test_Thai(self): |
| 185 |
|
| 186 |
source = '''This is $foo $adjective''' |
| 187 |
template = self.createAndCompile(source) |
| 188 |
assert template and issubclass(template, Template) |
| 189 |
template = template( |
| 190 |
searchList=[{ |
| 191 |
'foo': 'bar', |
| 192 |
'adjective': |
| 193 |
u'\u0e22\u0e34\u0e19\u0e14\u0e35\u0e15' |
| 194 |
u'\u0e49\u0e2d\u0e19\u0e23\u0e31\u0e1a' |
| 195 |
}]) |
| 196 |
assert template.respond() |
| 197 |
|
| 198 |
def test_Thai_utf8(self): |
| 199 |
utf8 = '\xe0\xb8\xa2\xe0\xb8\xb4\xe0\xb8\x99\xe0' \ |
| 200 |
'\xb8\x94\xe0\xb8\xb5\xe0\xb8\x95\xe0\xb9\x89\xe0' \ |
| 201 |
'\xb8\xad\xe0\xb8\x99\xe0\xb8\xa3\xe0\xb8\xb1\xe0\xb8\x9a' |
| 202 |
|
| 203 |
source = '''This is $adjective''' |
| 204 |
template = self.createAndCompile(source) |
| 205 |
assert template and issubclass(template, Template) |
| 206 |
template = template(searchList=[{'adjective': utf8}]) |
| 207 |
assert template.respond() |
| 208 |
|
| 209 |
|
| 210 |
class InlineSpanishTest(unittest.TestCase): |
| 211 |
def setUp(self): |
| 212 |
super(InlineSpanishTest, self).setUp() |
| 213 |
self.template = ''' |
| 214 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 215 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
| 216 |
<head> |
| 217 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 218 |
<title>Pagina del vendedor</title> |
| 219 |
</head> |
| 220 |
<body> |
| 221 |
$header |
| 222 |
<h2>Bienvenido $nombre.</h2> |
| 223 |
<br /><br /><br /> |
| 224 |
<center> |
| 225 |
Usted tiene $numpedidos_noconf <a href="">pedidós</a> sin confirmar. |
| 226 |
<br /><br /> |
| 227 |
Bodega tiene fecha para $numpedidos_bodega <a href="">pedidos</a>. |
| 228 |
</center> |
| 229 |
</body> |
| 230 |
</html> |
| 231 |
''' |
| 232 |
|
| 233 |
if PY2: |
| 234 |
def test_failure(self): |
| 235 |
""" Test a template lacking a proper #encoding tag """ |
| 236 |
self.assertRaises(UnicodeDecodeError, Template, self.template, |
| 237 |
searchList=[{'header': '', |
| 238 |
'nombre': '', |
| 239 |
'numpedidos_bodega': '', |
| 240 |
'numpedidos_noconf': ''}]) |
| 241 |
|
| 242 |
def test_success(self): |
| 243 |
""" Test a template with a proper #encoding tag """ |
| 244 |
template = '#encoding utf-8\n%s' % self.template |
| 245 |
template = Template(template, searchList=[{'header': '', |
| 246 |
'nombre': '', |
| 247 |
'numpedidos_bodega': '', |
| 248 |
'numpedidos_noconf': ''}]) |
| 249 |
self.assertTrue(unicode(template)) |
| 250 |
|
| 251 |
|
| 252 |
class CompilerTest(unittest.TestCase): |
| 253 |
def test_compiler_str(self): |
| 254 |
""" Test Compiler.__str__ """ |
| 255 |
source = """#encoding utf-8 |
| 256 |
#set $someUnicodeString = u"Bébé" |
| 257 |
$someUnicodeString""" |
| 258 |
compiler = Compiler(source) |
| 259 |
self.assertIsInstance(str(compiler), str) |
| 260 |
self.assertEqual(compiler.getModuleEncoding(), 'utf-8') |