[3] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- encoding: utf8 -*- |
---|
| 3 | |
---|
| 4 | from Cheetah.Template import Template |
---|
| 5 | from Cheetah import CheetahWrapper |
---|
| 6 | from Cheetah import DummyTransaction |
---|
| 7 | import imp |
---|
| 8 | import os |
---|
| 9 | import sys |
---|
| 10 | import tempfile |
---|
| 11 | import unittest |
---|
| 12 | |
---|
| 13 | class CommandLineTest(unittest.TestCase): |
---|
| 14 | def createAndCompile(self, source): |
---|
| 15 | sourcefile = '-' |
---|
| 16 | while sourcefile.find('-') != -1: |
---|
| 17 | sourcefile = tempfile.mktemp() |
---|
| 18 | |
---|
| 19 | fd = open('%s.tmpl' % sourcefile, 'w') |
---|
| 20 | fd.write(source) |
---|
| 21 | fd.close() |
---|
| 22 | |
---|
| 23 | wrap = CheetahWrapper.CheetahWrapper() |
---|
| 24 | wrap.main(['cheetah', 'compile', '--nobackup', sourcefile]) |
---|
| 25 | module_path, module_name = os.path.split(sourcefile) |
---|
| 26 | module = loadModule(module_name, [module_path]) |
---|
| 27 | template = getattr(module, module_name) |
---|
| 28 | return template |
---|
| 29 | |
---|
| 30 | class JBQ_UTF8_Test1(unittest.TestCase): |
---|
| 31 | def runTest(self): |
---|
| 32 | t = Template.compile(source="""Main file with |$v| |
---|
| 33 | |
---|
| 34 | $other""") |
---|
| 35 | |
---|
| 36 | otherT = Template.compile(source="Other template with |$v|") |
---|
| 37 | other = otherT() |
---|
| 38 | t.other = other |
---|
| 39 | |
---|
| 40 | t.v = u'Unicode String' |
---|
| 41 | t.other.v = u'Unicode String' |
---|
| 42 | |
---|
| 43 | assert unicode(t()) |
---|
| 44 | |
---|
| 45 | class JBQ_UTF8_Test2(unittest.TestCase): |
---|
| 46 | def runTest(self): |
---|
| 47 | t = Template.compile(source="""Main file with |$v| |
---|
| 48 | |
---|
| 49 | $other""") |
---|
| 50 | |
---|
| 51 | otherT = Template.compile(source="Other template with |$v|") |
---|
| 52 | other = otherT() |
---|
| 53 | t.other = other |
---|
| 54 | |
---|
| 55 | t.v = u'Unicode String with eacute テゥ' |
---|
| 56 | t.other.v = u'Unicode String' |
---|
| 57 | |
---|
| 58 | assert unicode(t()) |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | class JBQ_UTF8_Test3(unittest.TestCase): |
---|
| 62 | def runTest(self): |
---|
| 63 | t = Template.compile(source="""Main file with |$v| |
---|
| 64 | |
---|
| 65 | $other""") |
---|
| 66 | |
---|
| 67 | otherT = Template.compile(source="Other template with |$v|") |
---|
| 68 | other = otherT() |
---|
| 69 | t.other = other |
---|
| 70 | |
---|
| 71 | t.v = u'Unicode String with eacute テゥ' |
---|
| 72 | t.other.v = u'Unicode String and an eacute テゥ' |
---|
| 73 | |
---|
| 74 | assert unicode(t()) |
---|
| 75 | |
---|
| 76 | class JBQ_UTF8_Test4(unittest.TestCase): |
---|
| 77 | def runTest(self): |
---|
| 78 | t = Template.compile(source="""#encoding utf-8 |
---|
| 79 | Main file with |$v| and eacute in the template テゥ""") |
---|
| 80 | |
---|
| 81 | t.v = 'Unicode String' |
---|
| 82 | |
---|
| 83 | assert unicode(t()) |
---|
| 84 | |
---|
| 85 | class JBQ_UTF8_Test5(unittest.TestCase): |
---|
| 86 | def runTest(self): |
---|
| 87 | t = Template.compile(source="""#encoding utf-8 |
---|
| 88 | Main file with |$v| and eacute in the template テゥ""") |
---|
| 89 | |
---|
| 90 | t.v = u'Unicode String' |
---|
| 91 | |
---|
| 92 | assert unicode(t()) |
---|
| 93 | |
---|
| 94 | def loadModule(moduleName, path=None): |
---|
| 95 | if path: |
---|
| 96 | assert isinstance(path, list) |
---|
| 97 | try: |
---|
| 98 | mod = sys.modules[moduleName] |
---|
| 99 | except KeyError: |
---|
| 100 | fp = None |
---|
| 101 | |
---|
| 102 | try: |
---|
| 103 | fp, pathname, description = imp.find_module(moduleName, path) |
---|
| 104 | mod = imp.load_module(moduleName, fp, pathname, description) |
---|
| 105 | finally: |
---|
| 106 | if fp: |
---|
| 107 | fp.close() |
---|
| 108 | return mod |
---|
| 109 | |
---|
| 110 | class JBQ_UTF8_Test6(unittest.TestCase): |
---|
| 111 | def runTest(self): |
---|
| 112 | source = """#encoding utf-8 |
---|
| 113 | #set $someUnicodeString = u"Bテゥbテゥ" |
---|
| 114 | Main file with |$v| and eacute in the template テゥ""" |
---|
| 115 | t = Template.compile(source=source) |
---|
| 116 | |
---|
| 117 | t.v = u'Unicode String' |
---|
| 118 | |
---|
| 119 | assert unicode(t()) |
---|
| 120 | |
---|
| 121 | class JBQ_UTF8_Test7(CommandLineTest): |
---|
| 122 | def runTest(self): |
---|
| 123 | source = """#encoding utf-8 |
---|
| 124 | #set $someUnicodeString = u"Bテゥbテゥ" |
---|
| 125 | Main file with |$v| and eacute in the template テゥ""" |
---|
| 126 | |
---|
| 127 | template = self.createAndCompile(source) |
---|
| 128 | template.v = u'Unicode String' |
---|
| 129 | |
---|
| 130 | assert unicode(template()) |
---|
| 131 | |
---|
| 132 | class JBQ_UTF8_Test8(CommandLineTest): |
---|
| 133 | def testStaticCompile(self): |
---|
| 134 | source = """#encoding utf-8 |
---|
| 135 | #set $someUnicodeString = u"Bテゥbテゥ" |
---|
| 136 | $someUnicodeString""" |
---|
| 137 | |
---|
| 138 | template = self.createAndCompile(source)() |
---|
| 139 | |
---|
| 140 | a = unicode(template).encode("utf-8") |
---|
| 141 | self.assertEquals("Bテゥbテゥ", a) |
---|
| 142 | |
---|
| 143 | def testDynamicCompile(self): |
---|
| 144 | source = """#encoding utf-8 |
---|
| 145 | #set $someUnicodeString = u"Bテゥbテゥ" |
---|
| 146 | $someUnicodeString""" |
---|
| 147 | |
---|
| 148 | template = Template(source = source) |
---|
| 149 | |
---|
| 150 | a = unicode(template).encode("utf-8") |
---|
| 151 | self.assertEquals("Bテゥbテゥ", a) |
---|
| 152 | |
---|
| 153 | class Unicode_in_SearchList_Test(CommandLineTest): |
---|
| 154 | def test_BasicASCII(self): |
---|
| 155 | source = '''This is $adjective''' |
---|
| 156 | |
---|
| 157 | template = self.createAndCompile(source) |
---|
| 158 | assert template and issubclass(template, Template) |
---|
| 159 | template = template(searchList=[{'adjective' : u'neat'}]) |
---|
| 160 | assert template.respond() |
---|
| 161 | |
---|
| 162 | def test_Thai(self): |
---|
| 163 | # The string is something in Thai |
---|
| 164 | source = '''This is $foo $adjective''' |
---|
| 165 | template = self.createAndCompile(source) |
---|
| 166 | assert template and issubclass(template, Template) |
---|
| 167 | template = template(searchList=[{'foo' : 'bar', |
---|
| 168 | 'adjective' : u'\u0e22\u0e34\u0e19\u0e14\u0e35\u0e15\u0e49\u0e2d\u0e19\u0e23\u0e31\u0e1a'}]) |
---|
| 169 | assert template.respond() |
---|
| 170 | |
---|
| 171 | def test_ErrorReporting(self): |
---|
| 172 | utf8 = '\xe0\xb8\xa2\xe0\xb8\xb4\xe0\xb8\x99\xe0\xb8\x94\xe0\xb8\xb5\xe0\xb8\x95\xe0\xb9\x89\xe0\xb8\xad\xe0\xb8\x99\xe0\xb8\xa3\xe0\xb8\xb1\xe0\xb8\x9a' |
---|
| 173 | |
---|
| 174 | source = '''This is $adjective''' |
---|
| 175 | template = self.createAndCompile(source) |
---|
| 176 | assert template and issubclass(template, Template) |
---|
| 177 | template = template(searchList=[{'adjective' : utf8}]) |
---|
| 178 | self.failUnlessRaises(DummyTransaction.DummyResponseFailure, template.respond) |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | if __name__ == '__main__': |
---|
| 183 | unittest.main() |
---|