Commit 9ee89323 authored by Tres Seaver's avatar Tres Seaver

Fix PEP 8 line length violations.

parent df127f9e
......@@ -18,11 +18,14 @@ from zope.publisher.http import HTTPCharsets
from Testing.makerequest import makerequest
from Testing.ZopeTestCase import ZopeTestCase, installProduct
from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate, manage_addPageTemplate
from Products.PageTemplates.utils import encodingFromXMLPreamble, charsetFromMetaEquiv
from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
from Products.PageTemplates.ZopePageTemplate import manage_addPageTemplate
from Products.PageTemplates.utils import encodingFromXMLPreamble
from Products.PageTemplates.utils import charsetFromMetaEquiv
from zope.component import provideUtility
from Products.PageTemplates.interfaces import IUnicodeEncodingConflictResolver
from Products.PageTemplates.unicodeconflictresolver import PreferredCharsetResolver
from Products.PageTemplates.unicodeconflictresolver \
import PreferredCharsetResolver
ascii_str = '<html><body>hello world</body></html>'
......@@ -50,7 +53,8 @@ html_template_w_header = '''
'''
html_iso_8859_15_w_header = html_template_w_header % 'iso-8859-15'
html_utf8_w_header = unicode(html_template_w_header, 'iso-8859-15').encode('utf-8') % 'utf-8'
html_utf8_w_header = unicode(html_template_w_header,
'iso-8859-15').encode('utf-8') % 'utf-8'
html_template_wo_header = '''
<html>
......@@ -61,7 +65,8 @@ html_template_wo_header = '''
'''
html_iso_8859_15_wo_header = html_template_wo_header
html_utf8_wo_header = unicode(html_template_wo_header, 'iso-8859-15').encode('utf-8')
html_utf8_wo_header = unicode(html_template_wo_header,
'iso-8859-15').encode('utf-8')
xml_with_upper_attr = '''<?xml version="1.0"?>
<foo>
......@@ -84,16 +89,32 @@ class ZPTUtilsTests(unittest.TestCase):
def testExtractEncodingFromXMLPreamble(self):
extract = encodingFromXMLPreamble
self.assertEqual(extract('<?xml version="1.0" ?>'), 'utf-8')
self.assertEqual(extract('<?xml encoding="utf-8" version="1.0" ?>'), 'utf-8')
self.assertEqual(extract('<?xml encoding="UTF-8" version="1.0" ?>'), 'utf-8')
self.assertEqual(extract('<?xml encoding="ISO-8859-15" version="1.0" ?>'), 'iso-8859-15')
self.assertEqual(extract('<?xml encoding="iso-8859-15" version="1.0" ?>'), 'iso-8859-15')
self.assertEqual(extract('<?xml encoding="utf-8" '
'version="1.0" ?>'),
'utf-8')
self.assertEqual(extract('<?xml encoding="UTF-8" '
'version="1.0" ?>'),
'utf-8')
self.assertEqual(extract('<?xml encoding="ISO-8859-15" '
'version="1.0" ?>'),
'iso-8859-15')
self.assertEqual(extract('<?xml encoding="iso-8859-15" '
'version="1.0" ?>'),
'iso-8859-15')
def testExtractCharsetFromMetaHTTPEquivTag(self):
extract = charsetFromMetaEquiv
self.assertEqual(extract('<html><META http-equiv="content-type" content="text/html; charset=UTF-8"></html>'), 'utf-8')
self.assertEqual(extract('<html><META http-equiv="content-type" content="text/html; charset=iso-8859-15"></html>'), 'iso-8859-15')
self.assertEqual(extract('<html><META http-equiv="content-type" content="text/html"></html>'), None)
self.assertEqual(extract('<html><META http-equiv="content-type" '
'content="text/html; '
'charset=UTF-8"></html>'),
'utf-8')
self.assertEqual(extract('<html><META http-equiv="content-type" '
'content="text/html; '
'charset=iso-8859-15"></html>'),
'iso-8859-15')
self.assertEqual(extract('<html><META http-equiv="content-type" '
'content="text/html"></html>'),
None)
self.assertEqual(extract('<html>...<html>'), None)
......@@ -102,62 +123,79 @@ class ZPTUnicodeEncodingConflictResolution(ZopeTestCase):
def afterSetUp(self):
zope.component.provideAdapter(DefaultTraversable, (None,))
zope.component.provideAdapter(HTTPCharsets, (None,))
provideUtility(PreferredCharsetResolver, IUnicodeEncodingConflictResolver)
provideUtility(PreferredCharsetResolver,
IUnicodeEncodingConflictResolver)
def testISO_8859_15(self):
manage_addPageTemplate(self.app, 'test',
text='<div tal:content="python: request.get(\'data\')" />',
text=('<div tal:content="python: '
'request.get(\'data\')" />'),
encoding='ascii')
zpt = self.app['test']
self.app.REQUEST.set('HTTP_ACCEPT_CHARSET', 'ISO-8859-15,utf-8')
self.app.REQUEST.set('data', '')
result = zpt.pt_render()
self.assertEqual(result.startswith(unicode('<div></div>', 'iso-8859-15')), True)
self.failUnless(result.startswith(unicode('<div></div>',
'iso-8859-15')))
def testUTF8(self):
manage_addPageTemplate(self.app, 'test',
text='<div tal:content="python: request.get(\'data\')" />',
text=('<div tal:content="python: '
'request.get(\'data\')" />'),
encoding='ascii')
zpt = self.app['test']
self.app.REQUEST.set('HTTP_ACCEPT_CHARSET', 'utf-8,ISO-8859-15')
self.app.REQUEST.set('data', unicode('', 'iso-8859-15').encode('utf-8'))
self.app.REQUEST.set('data',
unicode('', 'iso-8859-15').encode('utf-8'))
result = zpt.pt_render()
self.assertEqual(result.startswith(unicode('<div></div>', 'iso-8859-15')), True)
self.failUnless(result.startswith(unicode('<div></div>',
'iso-8859-15')))
def testUTF8WrongPreferredCharset(self):
manage_addPageTemplate(self.app, 'test',
text='<div tal:content="python: request.get(\'data\')" />',
text=('<div tal:content="python: '
'request.get(\'data\')" />'),
encoding='ascii')
zpt = self.app['test']
self.app.REQUEST.set('HTTP_ACCEPT_CHARSET', 'iso-8859-15')
self.app.REQUEST.set('data', unicode('', 'iso-8859-15').encode('utf-8'))
self.app.REQUEST.set('data',
unicode('', 'iso-8859-15').encode('utf-8'))
result = zpt.pt_render()
self.assertEqual(result.startswith(unicode('<div></div>', 'iso-8859-15')), False)
self.failIf(result.startswith(unicode('<div></div>',
'iso-8859-15')))
def testStructureWithAccentedChars(self):
manage_addPageTemplate(self.app, 'test',
text='<div tal:content="structure python: %s" />' % "''",
text=('<div tal:content="structure '
'python: %s" />' % "''"),
encoding='iso-8859-15')
zpt = self.app['test']
self.app.REQUEST.set('HTTP_ACCEPT_CHARSET', 'iso-8859-15,utf-8')
self.app.REQUEST.set('data', unicode('', 'iso-8859-15').encode('utf-8'))
self.app.REQUEST.set('data', unicode('',
'iso-8859-15').encode('utf-8'))
result = zpt.pt_render()
self.assertEqual(result.startswith(unicode('<div></div>', 'iso-8859-15')), True)
self.failUnless(result.startswith(unicode('<div></div>',
'iso-8859-15')))
def testBug151020(self):
manage_addPageTemplate(self.app, 'test',
text='<div tal:content="structure python: %s" />' % "''",
text=('<div tal:content="structure '
'python: %s" />' % "''"),
encoding='iso-8859-15')
zpt = self.app['test']
self.app.REQUEST.set('HTTP_ACCEPT_CHARSET', 'x-user-defined, iso-8859-15,utf-8')
self.app.REQUEST.set('data', unicode('', 'iso-8859-15').encode('utf-8'))
self.app.REQUEST.set('HTTP_ACCEPT_CHARSET',
'x-user-defined, iso-8859-15,utf-8')
self.app.REQUEST.set('data',
unicode('', 'iso-8859-15').encode('utf-8'))
result = zpt.pt_render()
self.assertEqual(result.startswith(unicode('<div></div>', 'iso-8859-15')), True)
self.failUnless(result.startswith(unicode('<div></div>',
'iso-8859-15')))
class ZopePageTemplateFileTests(ZopeTestCase):
def testPT_RenderWithAscii(self):
manage_addPageTemplate(self.app, 'test', text=ascii_str, encoding='ascii')
manage_addPageTemplate(self.app, 'test',
text=ascii_str, encoding='ascii')
zpt = self.app['test']
result = zpt.pt_render()
# use startswith() because the renderer appends a trailing \n
......@@ -168,21 +206,25 @@ class ZopePageTemplateFileTests(ZopeTestCase):
# Check workaround for unicode incompatibility of ZRPythonExpr.
# See http://mail.zope.org/pipermail/zope/2007-February/170537.html
manage_addPageTemplate(self.app, 'test',
text='<span tal:content="python: unicode(\'\xfe\', \'iso-8859-15\')" />',
text=('<span tal:content="python: '
'unicode(\'\xfe\', \'iso-8859-15\')" />'),
encoding='iso-8859-15')
zpt = self.app['test']
result = zpt.pt_render() # should not raise a UnicodeDecodeError
def testPT_RenderWithISO885915(self):
manage_addPageTemplate(self.app, 'test', text=iso885915_str, encoding='iso-8859-15')
manage_addPageTemplate(self.app, 'test',
text=iso885915_str, encoding='iso-8859-15')
zpt = self.app['test']
result = zpt.pt_render()
# use startswith() because the renderer appends a trailing \n
self.assertEqual(result.encode('iso-8859-15').startswith(iso885915_str), True)
self.failUnless(result.encode('iso-8859-15'
).startswith(iso885915_str))
self.assertEqual(zpt.output_encoding, 'iso-8859-15')
def testPT_RenderWithUTF8(self):
manage_addPageTemplate(self.app, 'test', text=utf8_str, encoding='utf-8')
manage_addPageTemplate(self.app, 'test',
text=utf8_str, encoding='utf-8')
zpt = self.app['test']
result = zpt.pt_render()
# use startswith() because the renderer appends a trailing \n
......@@ -206,7 +248,8 @@ class ZopePageTemplateFileTests(ZopeTestCase):
self.assertEqual(zpt.read(), xml_unicode)
def _createZPT(self):
manage_addPageTemplate(self.app, 'test', text=utf8_str, encoding='utf-8')
manage_addPageTemplate(self.app, 'test',
text=utf8_str, encoding='utf-8')
zpt = self.app['test']
return zpt
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment