Back to index

plone3  3.1.7
test_encoding.py
Go to the documentation of this file.
00001 
00002 from Testing import ZopeTestCase
00003 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00004 
00005 from Products.MimetypesRegistry.encoding import guess_encoding
00006 
00007 class TestGuessEncoding(ATSiteTestCase):
00008 
00009     def testUTF8(self):
00010         e = guess_encoding('\xef\xbb\xbf any UTF-8 data')
00011         self.failUnlessEqual(e, 'UTF-8')
00012         e = guess_encoding(' any UTF-8 data \xef\xbb\xbf')
00013         self.failUnlessEqual(e, None)
00014 
00015     def testEmacs(self):
00016         e = guess_encoding('# -*- coding: UTF-8  -*-')
00017         self.failUnlessEqual(e, 'UTF-8')
00018         e = guess_encoding('''
00019         ### -*- coding: ISO-8859-1  -*-
00020         ''')
00021         self.failUnlessEqual(e, 'ISO-8859-1')
00022         e = guess_encoding('''
00023 
00024         ### -*- coding: ISO-8859-1  -*-
00025         ''')
00026         self.failUnlessEqual(e, None)
00027 
00028     def testVim(self):
00029         e = guess_encoding('# vim:fileencoding=UTF-8')
00030         self.failUnlessEqual(e, 'UTF-8')
00031         e = guess_encoding('''
00032         ### vim:fileencoding=ISO-8859-1
00033         ''')
00034         self.failUnlessEqual(e, 'ISO-8859-1')
00035         e = guess_encoding('''
00036 
00037         ### vim:fileencoding= ISO-8859-1
00038         ''')
00039         self.failUnlessEqual(e, None)
00040 
00041     def testXML(self):
00042         e = guess_encoding('<?xml?>')
00043         self.failUnlessEqual(e, 'UTF-8')
00044         e = guess_encoding('''<?xml version="1.0" encoding="ISO-8859-1" ?>
00045         ''')
00046         self.failUnlessEqual(e, 'ISO-8859-1')
00047         e = guess_encoding('''<?xml version="1.0" encoding="ISO-8859-1"?>
00048         ''')
00049         self.failUnlessEqual(e, 'ISO-8859-1')
00050         e = guess_encoding('''<?xml version="1.0" encoding="ISO-8859-1"?><truc encoding="UTF-8">
00051         </truc>
00052         ''')
00053         self.failUnlessEqual(e, 'ISO-8859-1')
00054 
00055     def testHTML(self):
00056         e = guess_encoding('''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
00057 <html>
00058 <head>
00059 <title>ASPN : Python Cookbook : Auto-detect XML encoding</title>
00060     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
00061     <meta name="robots" content="all" />
00062     <meta name="description" content="ActiveState Open Source Programming tools for Perl Python XML xslt scripting with free trials. Quality development tools for programmers systems administrators database administrators network administrators and webmasters" />
00063     <meta name="keywords" content="ActiveState,Perl,xml,xslt,mozilla,Open Source,Python,Perl for Win32,resources,PerlScript,ActivePerl,Programming,Programmers,Integrated,Development,Environment,SOAP,Linux,Solaris,Web,development,tools,free,software,download,support,Perl Resource Kit,System Administration,Sys Admin,WinNT,SQL,Oracle,Email,XML,Linux,Programming,perl,NT,2000,windows,Unix,Software,Security,   Administration,systems,windows,database,database,consulting,support,Microsoft,developer,resource,code,tutorials,IDE,Integrated development environment,developer,resources,tcl,php" />
00064 
00065 <link rel="stylesheet" href="/ASPN/aspn.css" />
00066 
00067 </head>
00068 
00069 <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
00070 charset=utf-8
00071 </body>
00072 </html> ''')
00073         self.failUnlessEqual(e, 'iso-8859-1')
00074 
00075 
00076     def test_broken_percent(self):
00077         e = guess_encoding(
00078 r"""<pre>
00079 &lt;metal:block tal:define="dummy python:
00080 request.RESPONSE.setHeader('Content-Type',
00081 'text/html;;charset=%s' % charset)" /&gt;
00082 &lt;metal:block tal:define="dummy
00083 python:request.RESPONSE.setHeader('Content-Language', lang)"
00084 /
00085 &gt;
00086 </pre>
00087 """
00088     )
00089         # unable to detect a valid encoding
00090         self.failUnlessEqual(e, None) 
00091 
00092 
00093 def test_suite():
00094     from unittest import TestSuite, makeSuite
00095     suite = TestSuite()
00096     suite.addTest(makeSuite(TestGuessEncoding))
00097     return suite