"Fossies" - the Fresh Open Source Software Archive

Member "cheetah3-3.2.6.post2/Cheetah/Tests/Unicode.py" (20 Apr 2021, 8317 Bytes) of package /linux/www/cheetah3-3.2.6.post2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "Unicode.py": 3-3.2.2_vs_3-3.2.3.

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